2 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
8 * See MAINTAINERS file for support contact information.
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/delay.h>
17 #include <linux/ethtool.h>
18 #include <linux/mii.h>
19 #include <linux/phy.h>
20 #include <linux/if_vlan.h>
21 #include <linux/crc32.h>
24 #include <linux/tcp.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/firmware.h>
29 #include <linux/prefetch.h>
30 #include <linux/ipv6.h>
31 #include <net/ip6_checksum.h>
36 #define RTL8169_VERSION "2.3LK-NAPI"
37 #define MODULENAME "r8169"
39 #define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40 #define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
41 #define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42 #define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
43 #define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
44 #define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45 #define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
46 #define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
47 #define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
48 #define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
49 #define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
50 #define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
51 #define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
52 #define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
53 #define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
54 #define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
55 #define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
56 #define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
57 #define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
59 #define R8169_MSG_DEFAULT \
60 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
62 #define TX_SLOTS_AVAIL(tp) \
63 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
65 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
66 #define TX_FRAGS_READY_FOR(tp,nr_frags) \
67 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
69 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
70 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
71 static const int multicast_filter_limit = 32;
73 #define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
74 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76 #define R8169_REGS_SIZE 256
77 #define R8169_RX_BUF_SIZE (SZ_16K - 1)
78 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
79 #define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
80 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
81 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
83 #define RTL8169_TX_TIMEOUT (6*HZ)
85 /* write/read MMIO register */
86 #define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
87 #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
88 #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
89 #define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
90 #define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
91 #define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
94 RTL_GIGA_MAC_VER_01 = 0,
145 RTL_GIGA_MAC_NONE = 0xff,
148 enum rtl_tx_desc_version {
153 #define JUMBO_1K ETH_DATA_LEN
154 #define JUMBO_4K (4*1024 - ETH_HLEN - 2)
155 #define JUMBO_6K (6*1024 - ETH_HLEN - 2)
156 #define JUMBO_7K (7*1024 - ETH_HLEN - 2)
157 #define JUMBO_9K (9*1024 - ETH_HLEN - 2)
159 #define _R(NAME,TD,FW,SZ) { \
166 static const struct {
168 enum rtl_tx_desc_version txd_version;
171 } rtl_chip_infos[] = {
173 [RTL_GIGA_MAC_VER_01] =
174 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K),
175 [RTL_GIGA_MAC_VER_02] =
176 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K),
177 [RTL_GIGA_MAC_VER_03] =
178 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K),
179 [RTL_GIGA_MAC_VER_04] =
180 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K),
181 [RTL_GIGA_MAC_VER_05] =
182 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
183 [RTL_GIGA_MAC_VER_06] =
184 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K),
186 [RTL_GIGA_MAC_VER_07] =
187 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
188 [RTL_GIGA_MAC_VER_08] =
189 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
190 [RTL_GIGA_MAC_VER_09] =
191 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K),
192 [RTL_GIGA_MAC_VER_10] =
193 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
194 [RTL_GIGA_MAC_VER_11] =
195 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
196 [RTL_GIGA_MAC_VER_12] =
197 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
198 [RTL_GIGA_MAC_VER_13] =
199 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
200 [RTL_GIGA_MAC_VER_14] =
201 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
202 [RTL_GIGA_MAC_VER_15] =
203 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K),
204 [RTL_GIGA_MAC_VER_16] =
205 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K),
206 [RTL_GIGA_MAC_VER_17] =
207 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K),
208 [RTL_GIGA_MAC_VER_18] =
209 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
210 [RTL_GIGA_MAC_VER_19] =
211 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
212 [RTL_GIGA_MAC_VER_20] =
213 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
214 [RTL_GIGA_MAC_VER_21] =
215 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
216 [RTL_GIGA_MAC_VER_22] =
217 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K),
218 [RTL_GIGA_MAC_VER_23] =
219 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
220 [RTL_GIGA_MAC_VER_24] =
221 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K),
222 [RTL_GIGA_MAC_VER_25] =
223 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1, JUMBO_9K),
224 [RTL_GIGA_MAC_VER_26] =
225 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2, JUMBO_9K),
226 [RTL_GIGA_MAC_VER_27] =
227 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
228 [RTL_GIGA_MAC_VER_28] =
229 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
230 [RTL_GIGA_MAC_VER_29] =
231 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
232 [RTL_GIGA_MAC_VER_30] =
233 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1, JUMBO_1K),
234 [RTL_GIGA_MAC_VER_31] =
235 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K),
236 [RTL_GIGA_MAC_VER_32] =
237 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1, JUMBO_9K),
238 [RTL_GIGA_MAC_VER_33] =
239 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2, JUMBO_9K),
240 [RTL_GIGA_MAC_VER_34] =
241 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3, JUMBO_9K),
242 [RTL_GIGA_MAC_VER_35] =
243 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1, JUMBO_9K),
244 [RTL_GIGA_MAC_VER_36] =
245 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2, JUMBO_9K),
246 [RTL_GIGA_MAC_VER_37] =
247 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1, JUMBO_1K),
248 [RTL_GIGA_MAC_VER_38] =
249 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1, JUMBO_9K),
250 [RTL_GIGA_MAC_VER_39] =
251 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1, JUMBO_1K),
252 [RTL_GIGA_MAC_VER_40] =
253 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2, JUMBO_9K),
254 [RTL_GIGA_MAC_VER_41] =
255 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K),
256 [RTL_GIGA_MAC_VER_42] =
257 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3, JUMBO_9K),
258 [RTL_GIGA_MAC_VER_43] =
259 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2, JUMBO_1K),
260 [RTL_GIGA_MAC_VER_44] =
261 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2, JUMBO_9K),
262 [RTL_GIGA_MAC_VER_45] =
263 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1, JUMBO_9K),
264 [RTL_GIGA_MAC_VER_46] =
265 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2, JUMBO_9K),
266 [RTL_GIGA_MAC_VER_47] =
267 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1, JUMBO_1K),
268 [RTL_GIGA_MAC_VER_48] =
269 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2, JUMBO_1K),
270 [RTL_GIGA_MAC_VER_49] =
271 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
272 [RTL_GIGA_MAC_VER_50] =
273 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
274 [RTL_GIGA_MAC_VER_51] =
275 _R("RTL8168ep/8111ep", RTL_TD_1, NULL, JUMBO_9K),
285 static const struct pci_device_id rtl8169_pci_tbl[] = {
286 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
287 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
288 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
289 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
290 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
291 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
292 { PCI_VENDOR_ID_DLINK, 0x4300,
293 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
294 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
295 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
296 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
297 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
298 { PCI_VENDOR_ID_LINKSYS, 0x1032,
299 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
301 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
305 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
307 static int use_dac = -1;
313 MAC0 = 0, /* Ethernet hardware address. */
315 MAR0 = 8, /* Multicast filter. */
316 CounterAddrLow = 0x10,
317 CounterAddrHigh = 0x14,
318 TxDescStartAddrLow = 0x20,
319 TxDescStartAddrHigh = 0x24,
320 TxHDescStartAddrLow = 0x28,
321 TxHDescStartAddrHigh = 0x2c,
330 #define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
331 #define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
334 #define RX128_INT_EN (1 << 15) /* 8111c and later */
335 #define RX_MULTI_EN (1 << 14) /* 8111c only */
336 #define RXCFG_FIFO_SHIFT 13
337 /* No threshold before first PCI xfer */
338 #define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
339 #define RX_EARLY_OFF (1 << 11)
340 #define RXCFG_DMA_SHIFT 8
341 /* Unlimited maximum PCI burst. */
342 #define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
349 #define PME_SIGNAL (1 << 5) /* 8168c and later */
361 #define RTL_COALESCE_MASK 0x0f
362 #define RTL_COALESCE_SHIFT 4
363 #define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
364 #define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
366 RxDescAddrLow = 0xe4,
367 RxDescAddrHigh = 0xe8,
368 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
370 #define NoEarlyTx 0x3f /* Max value : no early transmit. */
372 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
374 #define TxPacketMax (8064 >> 7)
375 #define EarlySize 0x27
378 FuncEventMask = 0xf4,
379 FuncPresetState = 0xf8,
384 FuncForceEvent = 0xfc,
387 enum rtl8168_8101_registers {
390 #define CSIAR_FLAG 0x80000000
391 #define CSIAR_WRITE_CMD 0x80000000
392 #define CSIAR_BYTE_ENABLE 0x0000f000
393 #define CSIAR_ADDR_MASK 0x00000fff
396 #define EPHYAR_FLAG 0x80000000
397 #define EPHYAR_WRITE_CMD 0x80000000
398 #define EPHYAR_REG_MASK 0x1f
399 #define EPHYAR_REG_SHIFT 16
400 #define EPHYAR_DATA_MASK 0xffff
402 #define PFM_EN (1 << 6)
403 #define TX_10M_PS_EN (1 << 7)
405 #define FIX_NAK_1 (1 << 4)
406 #define FIX_NAK_2 (1 << 3)
409 #define NOW_IS_OOB (1 << 7)
410 #define TX_EMPTY (1 << 5)
411 #define RX_EMPTY (1 << 4)
412 #define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
413 #define EN_NDP (1 << 3)
414 #define EN_OOB_RESET (1 << 2)
415 #define LINK_LIST_RDY (1 << 1)
417 #define EFUSEAR_FLAG 0x80000000
418 #define EFUSEAR_WRITE_CMD 0x80000000
419 #define EFUSEAR_READ_CMD 0x00000000
420 #define EFUSEAR_REG_MASK 0x03ff
421 #define EFUSEAR_REG_SHIFT 8
422 #define EFUSEAR_DATA_MASK 0xff
424 #define PFM_D3COLD_EN (1 << 6)
427 enum rtl8168_registers {
432 #define ERIAR_FLAG 0x80000000
433 #define ERIAR_WRITE_CMD 0x80000000
434 #define ERIAR_READ_CMD 0x00000000
435 #define ERIAR_ADDR_BYTE_ALIGN 4
436 #define ERIAR_TYPE_SHIFT 16
437 #define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
438 #define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
439 #define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
440 #define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
441 #define ERIAR_MASK_SHIFT 12
442 #define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
443 #define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
444 #define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
445 #define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
446 #define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
447 EPHY_RXER_NUM = 0x7c,
448 OCPDR = 0xb0, /* OCP GPHY access */
449 #define OCPDR_WRITE_CMD 0x80000000
450 #define OCPDR_READ_CMD 0x00000000
451 #define OCPDR_REG_MASK 0x7f
452 #define OCPDR_GPHY_REG_SHIFT 16
453 #define OCPDR_DATA_MASK 0xffff
455 #define OCPAR_FLAG 0x80000000
456 #define OCPAR_GPHY_WRITE_CMD 0x8000f060
457 #define OCPAR_GPHY_READ_CMD 0x0000f060
459 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
460 MISC = 0xf0, /* 8168e only. */
461 #define TXPLA_RST (1 << 29)
462 #define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
463 #define PWM_EN (1 << 22)
464 #define RXDV_GATED_EN (1 << 19)
465 #define EARLY_TALLY_EN (1 << 16)
468 enum rtl_register_content {
469 /* InterruptStatusBits */
473 TxDescUnavail = 0x0080,
497 /* TXPoll register p.5 */
498 HPQ = 0x80, /* Poll cmd on the high prio queue */
499 NPQ = 0x40, /* Poll cmd on the low prio queue */
500 FSWInt = 0x01, /* Forced software interrupt */
504 Cfg9346_Unlock = 0xc0,
509 AcceptBroadcast = 0x08,
510 AcceptMulticast = 0x04,
512 AcceptAllPhys = 0x01,
513 #define RX_CONFIG_ACCEPT_MASK 0x3f
516 TxInterFrameGapShift = 24,
517 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
519 /* Config1 register p.24 */
522 Speed_down = (1 << 4),
526 PMEnable = (1 << 0), /* Power Management Enable */
528 /* Config2 register p. 25 */
529 ClkReqEn = (1 << 7), /* Clock Request Enable */
530 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
531 PCI_Clock_66MHz = 0x01,
532 PCI_Clock_33MHz = 0x00,
534 /* Config3 register p.25 */
535 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
536 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
537 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
538 Rdy_to_L23 = (1 << 1), /* L23 Enable */
539 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
541 /* Config4 register */
542 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
544 /* Config5 register p.27 */
545 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
546 MWF = (1 << 5), /* Accept Multicast wakeup frame */
547 UWF = (1 << 4), /* Accept Unicast wakeup frame */
549 LanWake = (1 << 1), /* LanWake enable/disable */
550 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
551 ASPM_en = (1 << 0), /* ASPM enable */
554 EnableBist = (1 << 15), // 8168 8101
555 Mac_dbgo_oe = (1 << 14), // 8168 8101
556 Normal_mode = (1 << 13), // unused
557 Force_half_dup = (1 << 12), // 8168 8101
558 Force_rxflow_en = (1 << 11), // 8168 8101
559 Force_txflow_en = (1 << 10), // 8168 8101
560 Cxpl_dbg_sel = (1 << 9), // 8168 8101
561 ASF = (1 << 8), // 8168 8101
562 PktCntrDisable = (1 << 7), // 8168 8101
563 Mac_dbgo_sel = 0x001c, // 8168
568 #define INTT_MASK GENMASK(1, 0)
569 INTT_0 = 0x0000, // 8168
570 INTT_1 = 0x0001, // 8168
571 INTT_2 = 0x0002, // 8168
572 INTT_3 = 0x0003, // 8168
574 /* rtl8169_PHYstatus */
585 TBILinkOK = 0x02000000,
587 /* ResetCounterCommand */
590 /* DumpCounterCommand */
593 /* magic enable v2 */
594 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
598 /* First doubleword. */
599 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
600 RingEnd = (1 << 30), /* End of descriptor ring */
601 FirstFrag = (1 << 29), /* First segment of a packet */
602 LastFrag = (1 << 28), /* Final segment of a packet */
606 enum rtl_tx_desc_bit {
607 /* First doubleword. */
608 TD_LSO = (1 << 27), /* Large Send Offload */
609 #define TD_MSS_MAX 0x07ffu /* MSS value */
611 /* Second doubleword. */
612 TxVlanTag = (1 << 17), /* Add VLAN tag */
615 /* 8169, 8168b and 810x except 8102e. */
616 enum rtl_tx_desc_bit_0 {
617 /* First doubleword. */
618 #define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
619 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
620 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
621 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
624 /* 8102e, 8168c and beyond. */
625 enum rtl_tx_desc_bit_1 {
626 /* First doubleword. */
627 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
628 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
629 #define GTTCPHO_SHIFT 18
630 #define GTTCPHO_MAX 0x7fU
632 /* Second doubleword. */
633 #define TCPHO_SHIFT 18
634 #define TCPHO_MAX 0x3ffU
635 #define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
636 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
637 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
638 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
639 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
642 enum rtl_rx_desc_bit {
644 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
645 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
647 #define RxProtoUDP (PID1)
648 #define RxProtoTCP (PID0)
649 #define RxProtoIP (PID1 | PID0)
650 #define RxProtoMask RxProtoIP
652 IPFail = (1 << 16), /* IP checksum failed */
653 UDPFail = (1 << 15), /* UDP/IP checksum failed */
654 TCPFail = (1 << 14), /* TCP/IP checksum failed */
655 RxVlanTag = (1 << 16), /* VLAN tag available */
658 #define RsvdMask 0x3fffc000
659 #define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
676 u8 __pad[sizeof(void *) - sizeof(u32)];
679 struct rtl8169_counters {
686 __le32 tx_one_collision;
687 __le32 tx_multi_collision;
695 struct rtl8169_tc_offsets {
698 __le32 tx_multi_collision;
703 RTL_FLAG_TASK_ENABLED,
704 RTL_FLAG_TASK_SLOW_PENDING,
705 RTL_FLAG_TASK_RESET_PENDING,
709 struct rtl8169_stats {
712 struct u64_stats_sync syncp;
715 struct rtl8169_private {
716 void __iomem *mmio_addr; /* memory map physical address */
717 struct pci_dev *pci_dev;
718 struct net_device *dev;
719 struct napi_struct napi;
722 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
723 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
725 struct rtl8169_stats rx_stats;
726 struct rtl8169_stats tx_stats;
727 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
728 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
729 dma_addr_t TxPhyAddr;
730 dma_addr_t RxPhyAddr;
731 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
732 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
736 const struct rtl_coalesce_info *coalesce_info;
739 void (*write)(struct rtl8169_private *, int, int);
740 int (*read)(struct rtl8169_private *, int);
744 void (*enable)(struct rtl8169_private *);
745 void (*disable)(struct rtl8169_private *);
748 void (*hw_start)(struct rtl8169_private *tp);
749 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
752 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
754 struct work_struct work;
757 struct mii_if_info mii;
758 struct mii_bus *mii_bus;
759 dma_addr_t counters_phys_addr;
760 struct rtl8169_counters *counters;
761 struct rtl8169_tc_offsets tc_offset;
765 const struct firmware *fw;
767 #define RTL_VER_SIZE 32
769 char version[RTL_VER_SIZE];
771 struct rtl_fw_phy_action {
776 #define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
781 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
782 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
783 module_param(use_dac, int, 0);
784 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
785 module_param_named(debug, debug.msg_enable, int, 0);
786 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
787 MODULE_LICENSE("GPL");
788 MODULE_VERSION(RTL8169_VERSION);
789 MODULE_FIRMWARE(FIRMWARE_8168D_1);
790 MODULE_FIRMWARE(FIRMWARE_8168D_2);
791 MODULE_FIRMWARE(FIRMWARE_8168E_1);
792 MODULE_FIRMWARE(FIRMWARE_8168E_2);
793 MODULE_FIRMWARE(FIRMWARE_8168E_3);
794 MODULE_FIRMWARE(FIRMWARE_8105E_1);
795 MODULE_FIRMWARE(FIRMWARE_8168F_1);
796 MODULE_FIRMWARE(FIRMWARE_8168F_2);
797 MODULE_FIRMWARE(FIRMWARE_8402_1);
798 MODULE_FIRMWARE(FIRMWARE_8411_1);
799 MODULE_FIRMWARE(FIRMWARE_8411_2);
800 MODULE_FIRMWARE(FIRMWARE_8106E_1);
801 MODULE_FIRMWARE(FIRMWARE_8106E_2);
802 MODULE_FIRMWARE(FIRMWARE_8168G_2);
803 MODULE_FIRMWARE(FIRMWARE_8168G_3);
804 MODULE_FIRMWARE(FIRMWARE_8168H_1);
805 MODULE_FIRMWARE(FIRMWARE_8168H_2);
806 MODULE_FIRMWARE(FIRMWARE_8107E_1);
807 MODULE_FIRMWARE(FIRMWARE_8107E_2);
809 static inline struct device *tp_to_dev(struct rtl8169_private *tp)
811 return &tp->pci_dev->dev;
814 static void rtl_lock_work(struct rtl8169_private *tp)
816 mutex_lock(&tp->wk.mutex);
819 static void rtl_unlock_work(struct rtl8169_private *tp)
821 mutex_unlock(&tp->wk.mutex);
824 static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
826 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
827 PCI_EXP_DEVCTL_READRQ, force);
831 bool (*check)(struct rtl8169_private *);
835 static void rtl_udelay(unsigned int d)
840 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
841 void (*delay)(unsigned int), unsigned int d, int n,
846 for (i = 0; i < n; i++) {
848 if (c->check(tp) == high)
851 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
852 c->msg, !high, n, d);
856 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
857 const struct rtl_cond *c,
858 unsigned int d, int n)
860 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
863 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
864 const struct rtl_cond *c,
865 unsigned int d, int n)
867 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
870 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
871 const struct rtl_cond *c,
872 unsigned int d, int n)
874 return rtl_loop_wait(tp, c, msleep, d, n, true);
877 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
878 const struct rtl_cond *c,
879 unsigned int d, int n)
881 return rtl_loop_wait(tp, c, msleep, d, n, false);
884 #define DECLARE_RTL_COND(name) \
885 static bool name ## _check(struct rtl8169_private *); \
887 static const struct rtl_cond name = { \
888 .check = name ## _check, \
892 static bool name ## _check(struct rtl8169_private *tp)
894 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
896 if (reg & 0xffff0001) {
897 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
903 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
905 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
908 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
910 if (rtl_ocp_reg_failure(tp, reg))
913 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
915 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
918 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
920 if (rtl_ocp_reg_failure(tp, reg))
923 RTL_W32(tp, GPHY_OCP, reg << 15);
925 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
926 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
929 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
931 if (rtl_ocp_reg_failure(tp, reg))
934 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
937 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
939 if (rtl_ocp_reg_failure(tp, reg))
942 RTL_W32(tp, OCPDR, reg << 15);
944 return RTL_R32(tp, OCPDR);
947 #define OCP_STD_PHY_BASE 0xa400
949 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
952 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
956 if (tp->ocp_base != OCP_STD_PHY_BASE)
959 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
962 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
964 if (tp->ocp_base != OCP_STD_PHY_BASE)
967 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
970 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
973 tp->ocp_base = value << 4;
977 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
980 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
982 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
985 DECLARE_RTL_COND(rtl_phyar_cond)
987 return RTL_R32(tp, PHYAR) & 0x80000000;
990 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
992 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
994 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
996 * According to hardware specs a 20us delay is required after write
997 * complete indication, but before sending next command.
1002 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1006 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
1008 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1009 RTL_R32(tp, PHYAR) & 0xffff : ~0;
1012 * According to hardware specs a 20us delay is required after read
1013 * complete indication, but before sending next command.
1020 DECLARE_RTL_COND(rtl_ocpar_cond)
1022 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
1025 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
1027 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
1028 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
1029 RTL_W32(tp, EPHY_RXER_NUM, 0);
1031 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
1034 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
1036 r8168dp_1_mdio_access(tp, reg,
1037 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
1040 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
1042 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
1045 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
1046 RTL_W32(tp, EPHY_RXER_NUM, 0);
1048 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1049 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
1052 #define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1054 static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
1056 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1059 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
1061 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1064 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
1066 r8168dp_2_mdio_start(tp);
1068 r8169_mdio_write(tp, reg, value);
1070 r8168dp_2_mdio_stop(tp);
1073 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1077 r8168dp_2_mdio_start(tp);
1079 value = r8169_mdio_read(tp, reg);
1081 r8168dp_2_mdio_stop(tp);
1086 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1088 tp->mdio_ops.write(tp, location, val);
1091 static int rtl_readphy(struct rtl8169_private *tp, int location)
1093 return tp->mdio_ops.read(tp, location);
1096 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1098 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1101 static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1105 val = rtl_readphy(tp, reg_addr);
1106 rtl_writephy(tp, reg_addr, (val & ~m) | p);
1109 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1112 struct rtl8169_private *tp = netdev_priv(dev);
1114 rtl_writephy(tp, location, val);
1117 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1119 struct rtl8169_private *tp = netdev_priv(dev);
1121 return rtl_readphy(tp, location);
1124 DECLARE_RTL_COND(rtl_ephyar_cond)
1126 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
1129 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1131 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1132 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1134 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1139 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1141 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1143 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1144 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
1147 DECLARE_RTL_COND(rtl_eriar_cond)
1149 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
1152 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1155 BUG_ON((addr & 3) || (mask == 0));
1156 RTL_W32(tp, ERIDR, val);
1157 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1159 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1162 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1164 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1166 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1167 RTL_R32(tp, ERIDR) : ~0;
1170 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1175 val = rtl_eri_read(tp, addr, type);
1176 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
1179 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1181 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1182 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1183 RTL_R32(tp, OCPDR) : ~0;
1186 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1188 return rtl_eri_read(tp, reg, ERIAR_OOB);
1191 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1193 switch (tp->mac_version) {
1194 case RTL_GIGA_MAC_VER_27:
1195 case RTL_GIGA_MAC_VER_28:
1196 case RTL_GIGA_MAC_VER_31:
1197 return r8168dp_ocp_read(tp, mask, reg);
1198 case RTL_GIGA_MAC_VER_49:
1199 case RTL_GIGA_MAC_VER_50:
1200 case RTL_GIGA_MAC_VER_51:
1201 return r8168ep_ocp_read(tp, mask, reg);
1208 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1211 RTL_W32(tp, OCPDR, data);
1212 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1213 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1216 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1219 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1223 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1225 switch (tp->mac_version) {
1226 case RTL_GIGA_MAC_VER_27:
1227 case RTL_GIGA_MAC_VER_28:
1228 case RTL_GIGA_MAC_VER_31:
1229 r8168dp_ocp_write(tp, mask, reg, data);
1231 case RTL_GIGA_MAC_VER_49:
1232 case RTL_GIGA_MAC_VER_50:
1233 case RTL_GIGA_MAC_VER_51:
1234 r8168ep_ocp_write(tp, mask, reg, data);
1242 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1244 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1246 ocp_write(tp, 0x1, 0x30, 0x00000001);
1249 #define OOB_CMD_RESET 0x00
1250 #define OOB_CMD_DRIVER_START 0x05
1251 #define OOB_CMD_DRIVER_STOP 0x06
1253 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1255 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1258 DECLARE_RTL_COND(rtl_ocp_read_cond)
1262 reg = rtl8168_get_ocp_reg(tp);
1264 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1267 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1269 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1272 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1274 return RTL_R8(tp, IBISR0) & 0x20;
1277 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1279 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
1280 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
1281 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1282 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
1285 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1287 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
1288 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1291 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1293 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1294 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1295 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1298 static void rtl8168_driver_start(struct rtl8169_private *tp)
1300 switch (tp->mac_version) {
1301 case RTL_GIGA_MAC_VER_27:
1302 case RTL_GIGA_MAC_VER_28:
1303 case RTL_GIGA_MAC_VER_31:
1304 rtl8168dp_driver_start(tp);
1306 case RTL_GIGA_MAC_VER_49:
1307 case RTL_GIGA_MAC_VER_50:
1308 case RTL_GIGA_MAC_VER_51:
1309 rtl8168ep_driver_start(tp);
1317 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1319 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1320 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1323 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1325 rtl8168ep_stop_cmac(tp);
1326 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1327 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1328 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1331 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1333 switch (tp->mac_version) {
1334 case RTL_GIGA_MAC_VER_27:
1335 case RTL_GIGA_MAC_VER_28:
1336 case RTL_GIGA_MAC_VER_31:
1337 rtl8168dp_driver_stop(tp);
1339 case RTL_GIGA_MAC_VER_49:
1340 case RTL_GIGA_MAC_VER_50:
1341 case RTL_GIGA_MAC_VER_51:
1342 rtl8168ep_driver_stop(tp);
1350 static bool r8168dp_check_dash(struct rtl8169_private *tp)
1352 u16 reg = rtl8168_get_ocp_reg(tp);
1354 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
1357 static bool r8168ep_check_dash(struct rtl8169_private *tp)
1359 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
1362 static bool r8168_check_dash(struct rtl8169_private *tp)
1364 switch (tp->mac_version) {
1365 case RTL_GIGA_MAC_VER_27:
1366 case RTL_GIGA_MAC_VER_28:
1367 case RTL_GIGA_MAC_VER_31:
1368 return r8168dp_check_dash(tp);
1369 case RTL_GIGA_MAC_VER_49:
1370 case RTL_GIGA_MAC_VER_50:
1371 case RTL_GIGA_MAC_VER_51:
1372 return r8168ep_check_dash(tp);
1384 static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
1385 const struct exgmac_reg *r, int len)
1388 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1393 DECLARE_RTL_COND(rtl_efusear_cond)
1395 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
1398 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1400 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1402 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1403 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1406 static u16 rtl_get_events(struct rtl8169_private *tp)
1408 return RTL_R16(tp, IntrStatus);
1411 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1413 RTL_W16(tp, IntrStatus, bits);
1417 static void rtl_irq_disable(struct rtl8169_private *tp)
1419 RTL_W16(tp, IntrMask, 0);
1423 static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1425 RTL_W16(tp, IntrMask, bits);
1428 #define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1429 #define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1430 #define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1432 static void rtl_irq_enable_all(struct rtl8169_private *tp)
1434 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1437 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1439 rtl_irq_disable(tp);
1440 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
1441 RTL_R8(tp, ChipCmd);
1444 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1446 struct net_device *dev = tp->dev;
1448 if (!netif_running(dev))
1451 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1452 tp->mac_version == RTL_GIGA_MAC_VER_38) {
1453 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
1454 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1456 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1458 } else if (RTL_R8(tp, PHYstatus) & _100bps) {
1459 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1461 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1464 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1466 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1469 /* Reset packet filter */
1470 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1472 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1474 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1475 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1476 if (RTL_R8(tp, PHYstatus) & _1000bpsF) {
1477 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1479 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1482 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1484 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1487 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1488 if (RTL_R8(tp, PHYstatus) & _10bps) {
1489 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1491 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1494 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1500 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1502 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1507 options = RTL_R8(tp, Config1);
1508 if (!(options & PMEnable))
1511 options = RTL_R8(tp, Config3);
1512 if (options & LinkUp)
1513 wolopts |= WAKE_PHY;
1514 switch (tp->mac_version) {
1515 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1516 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1517 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1518 wolopts |= WAKE_MAGIC;
1521 if (options & MagicPacket)
1522 wolopts |= WAKE_MAGIC;
1526 options = RTL_R8(tp, Config5);
1528 wolopts |= WAKE_UCAST;
1530 wolopts |= WAKE_BCAST;
1532 wolopts |= WAKE_MCAST;
1537 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1539 struct rtl8169_private *tp = netdev_priv(dev);
1542 wol->supported = WAKE_ANY;
1543 wol->wolopts = tp->saved_wolopts;
1544 rtl_unlock_work(tp);
1547 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1549 unsigned int i, tmp;
1550 static const struct {
1555 { WAKE_PHY, Config3, LinkUp },
1556 { WAKE_UCAST, Config5, UWF },
1557 { WAKE_BCAST, Config5, BWF },
1558 { WAKE_MCAST, Config5, MWF },
1559 { WAKE_ANY, Config5, LanWake },
1560 { WAKE_MAGIC, Config3, MagicPacket }
1564 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
1566 switch (tp->mac_version) {
1567 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1568 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1569 tmp = ARRAY_SIZE(cfg) - 1;
1570 if (wolopts & WAKE_MAGIC)
1586 tmp = ARRAY_SIZE(cfg);
1590 for (i = 0; i < tmp; i++) {
1591 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
1592 if (wolopts & cfg[i].opt)
1593 options |= cfg[i].mask;
1594 RTL_W8(tp, cfg[i].reg, options);
1597 switch (tp->mac_version) {
1598 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1599 options = RTL_R8(tp, Config1) & ~PMEnable;
1601 options |= PMEnable;
1602 RTL_W8(tp, Config1, options);
1605 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
1607 options |= PME_SIGNAL;
1608 RTL_W8(tp, Config2, options);
1612 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
1615 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1617 struct rtl8169_private *tp = netdev_priv(dev);
1618 struct device *d = tp_to_dev(tp);
1620 if (wol->wolopts & ~WAKE_ANY)
1623 pm_runtime_get_noresume(d);
1627 tp->saved_wolopts = wol->wolopts;
1629 if (pm_runtime_active(d))
1630 __rtl8169_set_wol(tp, tp->saved_wolopts);
1632 rtl_unlock_work(tp);
1634 device_set_wakeup_enable(d, tp->saved_wolopts);
1636 pm_runtime_put_noidle(d);
1641 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1643 return rtl_chip_infos[tp->mac_version].fw_name;
1646 static void rtl8169_get_drvinfo(struct net_device *dev,
1647 struct ethtool_drvinfo *info)
1649 struct rtl8169_private *tp = netdev_priv(dev);
1650 struct rtl_fw *rtl_fw = tp->rtl_fw;
1652 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1653 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1654 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1655 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1656 if (!IS_ERR_OR_NULL(rtl_fw))
1657 strlcpy(info->fw_version, rtl_fw->version,
1658 sizeof(info->fw_version));
1661 static int rtl8169_get_regs_len(struct net_device *dev)
1663 return R8169_REGS_SIZE;
1666 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1667 netdev_features_t features)
1669 struct rtl8169_private *tp = netdev_priv(dev);
1671 if (dev->mtu > TD_MSS_MAX)
1672 features &= ~NETIF_F_ALL_TSO;
1674 if (dev->mtu > JUMBO_1K &&
1675 tp->mac_version > RTL_GIGA_MAC_VER_06)
1676 features &= ~NETIF_F_IP_CSUM;
1681 static int rtl8169_set_features(struct net_device *dev,
1682 netdev_features_t features)
1684 struct rtl8169_private *tp = netdev_priv(dev);
1689 rx_config = RTL_R32(tp, RxConfig);
1690 if (features & NETIF_F_RXALL)
1691 rx_config |= (AcceptErr | AcceptRunt);
1693 rx_config &= ~(AcceptErr | AcceptRunt);
1695 RTL_W32(tp, RxConfig, rx_config);
1697 if (features & NETIF_F_RXCSUM)
1698 tp->cp_cmd |= RxChkSum;
1700 tp->cp_cmd &= ~RxChkSum;
1702 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1703 tp->cp_cmd |= RxVlan;
1705 tp->cp_cmd &= ~RxVlan;
1707 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1708 RTL_R16(tp, CPlusCmd);
1710 rtl_unlock_work(tp);
1715 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1717 return (skb_vlan_tag_present(skb)) ?
1718 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
1721 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1723 u32 opts2 = le32_to_cpu(desc->opts2);
1725 if (opts2 & RxVlanTag)
1726 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1729 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1732 struct rtl8169_private *tp = netdev_priv(dev);
1733 u32 __iomem *data = tp->mmio_addr;
1738 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1739 memcpy_fromio(dw++, data++, 4);
1740 rtl_unlock_work(tp);
1743 static u32 rtl8169_get_msglevel(struct net_device *dev)
1745 struct rtl8169_private *tp = netdev_priv(dev);
1747 return tp->msg_enable;
1750 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1752 struct rtl8169_private *tp = netdev_priv(dev);
1754 tp->msg_enable = value;
1757 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1764 "tx_single_collisions",
1765 "tx_multi_collisions",
1773 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1777 return ARRAY_SIZE(rtl8169_gstrings);
1783 DECLARE_RTL_COND(rtl_counters_cond)
1785 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
1788 static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
1790 dma_addr_t paddr = tp->counters_phys_addr;
1793 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1794 RTL_R32(tp, CounterAddrHigh);
1795 cmd = (u64)paddr & DMA_BIT_MASK(32);
1796 RTL_W32(tp, CounterAddrLow, cmd);
1797 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
1799 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
1802 static bool rtl8169_reset_counters(struct rtl8169_private *tp)
1805 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1808 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1811 return rtl8169_do_counters(tp, CounterReset);
1814 static bool rtl8169_update_counters(struct rtl8169_private *tp)
1817 * Some chips are unable to dump tally counters when the receiver
1820 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
1823 return rtl8169_do_counters(tp, CounterDump);
1826 static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
1828 struct rtl8169_counters *counters = tp->counters;
1832 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1833 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1834 * reset by a power cycle, while the counter values collected by the
1835 * driver are reset at every driver unload/load cycle.
1837 * To make sure the HW values returned by @get_stats64 match the SW
1838 * values, we collect the initial values at first open(*) and use them
1839 * as offsets to normalize the values returned by @get_stats64.
1841 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1842 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1843 * set at open time by rtl_hw_start.
1846 if (tp->tc_offset.inited)
1849 /* If both, reset and update fail, propagate to caller. */
1850 if (rtl8169_reset_counters(tp))
1853 if (rtl8169_update_counters(tp))
1856 tp->tc_offset.tx_errors = counters->tx_errors;
1857 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1858 tp->tc_offset.tx_aborted = counters->tx_aborted;
1859 tp->tc_offset.inited = true;
1864 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1865 struct ethtool_stats *stats, u64 *data)
1867 struct rtl8169_private *tp = netdev_priv(dev);
1868 struct device *d = tp_to_dev(tp);
1869 struct rtl8169_counters *counters = tp->counters;
1873 pm_runtime_get_noresume(d);
1875 if (pm_runtime_active(d))
1876 rtl8169_update_counters(tp);
1878 pm_runtime_put_noidle(d);
1880 data[0] = le64_to_cpu(counters->tx_packets);
1881 data[1] = le64_to_cpu(counters->rx_packets);
1882 data[2] = le64_to_cpu(counters->tx_errors);
1883 data[3] = le32_to_cpu(counters->rx_errors);
1884 data[4] = le16_to_cpu(counters->rx_missed);
1885 data[5] = le16_to_cpu(counters->align_errors);
1886 data[6] = le32_to_cpu(counters->tx_one_collision);
1887 data[7] = le32_to_cpu(counters->tx_multi_collision);
1888 data[8] = le64_to_cpu(counters->rx_unicast);
1889 data[9] = le64_to_cpu(counters->rx_broadcast);
1890 data[10] = le32_to_cpu(counters->rx_multicast);
1891 data[11] = le16_to_cpu(counters->tx_aborted);
1892 data[12] = le16_to_cpu(counters->tx_underun);
1895 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1899 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1905 * Interrupt coalescing
1907 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1908 * > 8169, 8168 and 810x line of chipsets
1910 * 8169, 8168, and 8136(810x) serial chipsets support it.
1912 * > 2 - the Tx timer unit at gigabit speed
1914 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1915 * (0xe0) bit 1 and bit 0.
1918 * bit[1:0] \ speed 1000M 100M 10M
1919 * 0 0 320ns 2.56us 40.96us
1920 * 0 1 2.56us 20.48us 327.7us
1921 * 1 0 5.12us 40.96us 655.4us
1922 * 1 1 10.24us 81.92us 1.31ms
1925 * bit[1:0] \ speed 1000M 100M 10M
1926 * 0 0 5us 2.56us 40.96us
1927 * 0 1 40us 20.48us 327.7us
1928 * 1 0 80us 40.96us 655.4us
1929 * 1 1 160us 81.92us 1.31ms
1932 /* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1933 struct rtl_coalesce_scale {
1938 /* rx/tx scale factors for all CPlusCmd[0:1] cases */
1939 struct rtl_coalesce_info {
1941 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1944 /* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1945 #define rxtx_x1822(r, t) { \
1948 {{(r)*8*2, (t)*8*2}}, \
1949 {{(r)*8*2*2, (t)*8*2*2}}, \
1951 static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1952 /* speed delays: rx00 tx00 */
1953 { SPEED_10, rxtx_x1822(40960, 40960) },
1954 { SPEED_100, rxtx_x1822( 2560, 2560) },
1955 { SPEED_1000, rxtx_x1822( 320, 320) },
1959 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1960 /* speed delays: rx00 tx00 */
1961 { SPEED_10, rxtx_x1822(40960, 40960) },
1962 { SPEED_100, rxtx_x1822( 2560, 2560) },
1963 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1968 /* get rx/tx scale vector corresponding to current speed */
1969 static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1971 struct rtl8169_private *tp = netdev_priv(dev);
1972 struct ethtool_link_ksettings ecmd;
1973 const struct rtl_coalesce_info *ci;
1976 rc = phy_ethtool_get_link_ksettings(dev, &ecmd);
1980 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
1981 if (ecmd.base.speed == ci->speed) {
1986 return ERR_PTR(-ELNRNG);
1989 static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1991 struct rtl8169_private *tp = netdev_priv(dev);
1992 const struct rtl_coalesce_info *ci;
1993 const struct rtl_coalesce_scale *scale;
1997 } coal_settings [] = {
1998 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1999 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2000 }, *p = coal_settings;
2004 memset(ec, 0, sizeof(*ec));
2006 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2007 ci = rtl_coalesce_info(dev);
2011 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
2013 /* read IntrMitigate and adjust according to scale */
2014 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
2015 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2016 w >>= RTL_COALESCE_SHIFT;
2017 *p->usecs = w & RTL_COALESCE_MASK;
2020 for (i = 0; i < 2; i++) {
2021 p = coal_settings + i;
2022 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2025 * ethtool_coalesce says it is illegal to set both usecs and
2028 if (!*p->usecs && !*p->max_frames)
2035 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2036 static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2037 struct net_device *dev, u32 nsec, u16 *cp01)
2039 const struct rtl_coalesce_info *ci;
2042 ci = rtl_coalesce_info(dev);
2044 return ERR_CAST(ci);
2046 for (i = 0; i < 4; i++) {
2047 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2048 ci->scalev[i].nsecs[1]);
2049 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2051 return &ci->scalev[i];
2055 return ERR_PTR(-EINVAL);
2058 static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2060 struct rtl8169_private *tp = netdev_priv(dev);
2061 const struct rtl_coalesce_scale *scale;
2065 } coal_settings [] = {
2066 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2067 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2068 }, *p = coal_settings;
2072 scale = rtl_coalesce_choose_scale(dev,
2073 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2075 return PTR_ERR(scale);
2077 for (i = 0; i < 2; i++, p++) {
2081 * accept max_frames=1 we returned in rtl_get_coalesce.
2082 * accept it not only when usecs=0 because of e.g. the following scenario:
2084 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2085 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2086 * - then user does `ethtool -C eth0 rx-usecs 100`
2088 * since ethtool sends to kernel whole ethtool_coalesce
2089 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2090 * we'll reject it below in `frames % 4 != 0`.
2092 if (p->frames == 1) {
2096 units = p->usecs * 1000 / scale->nsecs[i];
2097 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2100 w <<= RTL_COALESCE_SHIFT;
2102 w <<= RTL_COALESCE_SHIFT;
2103 w |= p->frames >> 2;
2108 RTL_W16(tp, IntrMitigate, swab16(w));
2110 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
2111 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2112 RTL_R16(tp, CPlusCmd);
2114 rtl_unlock_work(tp);
2119 static const struct ethtool_ops rtl8169_ethtool_ops = {
2120 .get_drvinfo = rtl8169_get_drvinfo,
2121 .get_regs_len = rtl8169_get_regs_len,
2122 .get_link = ethtool_op_get_link,
2123 .get_coalesce = rtl_get_coalesce,
2124 .set_coalesce = rtl_set_coalesce,
2125 .get_msglevel = rtl8169_get_msglevel,
2126 .set_msglevel = rtl8169_set_msglevel,
2127 .get_regs = rtl8169_get_regs,
2128 .get_wol = rtl8169_get_wol,
2129 .set_wol = rtl8169_set_wol,
2130 .get_strings = rtl8169_get_strings,
2131 .get_sset_count = rtl8169_get_sset_count,
2132 .get_ethtool_stats = rtl8169_get_ethtool_stats,
2133 .get_ts_info = ethtool_op_get_ts_info,
2134 .nway_reset = phy_ethtool_nway_reset,
2135 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2136 .set_link_ksettings = phy_ethtool_set_link_ksettings,
2139 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
2143 * The driver currently handles the 8168Bf and the 8168Be identically
2144 * but they can be identified more specifically through the test below
2147 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2149 * Same thing for the 8101Eb and the 8101Ec:
2151 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2153 static const struct rtl_mac_info {
2158 /* 8168EP family. */
2159 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2160 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2161 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2164 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2165 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2168 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
2169 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
2170 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2171 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2174 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
2175 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2176 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2179 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
2180 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2181 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2184 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
2185 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
2187 /* 8168DP family. */
2188 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2189 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
2190 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
2193 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
2194 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
2195 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
2196 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2197 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
2198 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
2199 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
2202 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2203 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2204 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2207 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
2208 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
2209 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2210 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2211 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2212 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2213 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2214 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
2215 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2216 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
2217 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2218 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2219 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
2220 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2221 /* FIXME: where did these entries come from ? -- FR */
2222 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2223 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2226 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2227 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2228 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2229 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2230 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2231 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2234 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
2236 const struct rtl_mac_info *p = mac_info;
2239 reg = RTL_R32(tp, TxConfig);
2240 while ((reg & p->mask) != p->val)
2242 tp->mac_version = p->mac_version;
2244 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2245 dev_notice(tp_to_dev(tp),
2246 "unknown MAC, using family default\n");
2247 tp->mac_version = default_version;
2248 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2249 tp->mac_version = tp->mii.supports_gmii ?
2250 RTL_GIGA_MAC_VER_42 :
2251 RTL_GIGA_MAC_VER_43;
2252 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2253 tp->mac_version = tp->mii.supports_gmii ?
2254 RTL_GIGA_MAC_VER_45 :
2255 RTL_GIGA_MAC_VER_47;
2256 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2257 tp->mac_version = tp->mii.supports_gmii ?
2258 RTL_GIGA_MAC_VER_46 :
2259 RTL_GIGA_MAC_VER_48;
2263 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2265 netif_dbg(tp, drv, tp->dev, "mac_version = 0x%02x\n", tp->mac_version);
2273 static void rtl_writephy_batch(struct rtl8169_private *tp,
2274 const struct phy_reg *regs, int len)
2277 rtl_writephy(tp, regs->reg, regs->val);
2282 #define PHY_READ 0x00000000
2283 #define PHY_DATA_OR 0x10000000
2284 #define PHY_DATA_AND 0x20000000
2285 #define PHY_BJMPN 0x30000000
2286 #define PHY_MDIO_CHG 0x40000000
2287 #define PHY_CLEAR_READCOUNT 0x70000000
2288 #define PHY_WRITE 0x80000000
2289 #define PHY_READCOUNT_EQ_SKIP 0x90000000
2290 #define PHY_COMP_EQ_SKIPN 0xa0000000
2291 #define PHY_COMP_NEQ_SKIPN 0xb0000000
2292 #define PHY_WRITE_PREVIOUS 0xc0000000
2293 #define PHY_SKIPN 0xd0000000
2294 #define PHY_DELAY_MS 0xe0000000
2298 char version[RTL_VER_SIZE];
2304 #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2306 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2308 const struct firmware *fw = rtl_fw->fw;
2309 struct fw_info *fw_info = (struct fw_info *)fw->data;
2310 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2311 char *version = rtl_fw->version;
2314 if (fw->size < FW_OPCODE_SIZE)
2317 if (!fw_info->magic) {
2318 size_t i, size, start;
2321 if (fw->size < sizeof(*fw_info))
2324 for (i = 0; i < fw->size; i++)
2325 checksum += fw->data[i];
2329 start = le32_to_cpu(fw_info->fw_start);
2330 if (start > fw->size)
2333 size = le32_to_cpu(fw_info->fw_len);
2334 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2337 memcpy(version, fw_info->version, RTL_VER_SIZE);
2339 pa->code = (__le32 *)(fw->data + start);
2342 if (fw->size % FW_OPCODE_SIZE)
2345 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2347 pa->code = (__le32 *)fw->data;
2348 pa->size = fw->size / FW_OPCODE_SIZE;
2350 version[RTL_VER_SIZE - 1] = 0;
2357 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2358 struct rtl_fw_phy_action *pa)
2363 for (index = 0; index < pa->size; index++) {
2364 u32 action = le32_to_cpu(pa->code[index]);
2365 u32 regno = (action & 0x0fff0000) >> 16;
2367 switch(action & 0xf0000000) {
2372 case PHY_CLEAR_READCOUNT:
2374 case PHY_WRITE_PREVIOUS:
2379 if (regno > index) {
2380 netif_err(tp, ifup, tp->dev,
2381 "Out of range of firmware\n");
2385 case PHY_READCOUNT_EQ_SKIP:
2386 if (index + 2 >= pa->size) {
2387 netif_err(tp, ifup, tp->dev,
2388 "Out of range of firmware\n");
2392 case PHY_COMP_EQ_SKIPN:
2393 case PHY_COMP_NEQ_SKIPN:
2395 if (index + 1 + regno >= pa->size) {
2396 netif_err(tp, ifup, tp->dev,
2397 "Out of range of firmware\n");
2403 netif_err(tp, ifup, tp->dev,
2404 "Invalid action 0x%08x\n", action);
2413 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2415 struct net_device *dev = tp->dev;
2418 if (!rtl_fw_format_ok(tp, rtl_fw)) {
2419 netif_err(tp, ifup, dev, "invalid firmware\n");
2423 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2429 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2431 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2432 struct mdio_ops org, *ops = &tp->mdio_ops;
2436 predata = count = 0;
2437 org.write = ops->write;
2438 org.read = ops->read;
2440 for (index = 0; index < pa->size; ) {
2441 u32 action = le32_to_cpu(pa->code[index]);
2442 u32 data = action & 0x0000ffff;
2443 u32 regno = (action & 0x0fff0000) >> 16;
2448 switch(action & 0xf0000000) {
2450 predata = rtl_readphy(tp, regno);
2467 ops->write = org.write;
2468 ops->read = org.read;
2469 } else if (data == 1) {
2470 ops->write = mac_mcu_write;
2471 ops->read = mac_mcu_read;
2476 case PHY_CLEAR_READCOUNT:
2481 rtl_writephy(tp, regno, data);
2484 case PHY_READCOUNT_EQ_SKIP:
2485 index += (count == data) ? 2 : 1;
2487 case PHY_COMP_EQ_SKIPN:
2488 if (predata == data)
2492 case PHY_COMP_NEQ_SKIPN:
2493 if (predata != data)
2497 case PHY_WRITE_PREVIOUS:
2498 rtl_writephy(tp, regno, predata);
2514 ops->write = org.write;
2515 ops->read = org.read;
2518 static void rtl_release_firmware(struct rtl8169_private *tp)
2520 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2521 release_firmware(tp->rtl_fw->fw);
2524 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
2527 static void rtl_apply_firmware(struct rtl8169_private *tp)
2529 struct rtl_fw *rtl_fw = tp->rtl_fw;
2531 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2532 if (!IS_ERR_OR_NULL(rtl_fw))
2533 rtl_phy_write_fw(tp, rtl_fw);
2536 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2538 if (rtl_readphy(tp, reg) != val)
2539 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2541 rtl_apply_firmware(tp);
2544 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2546 static const struct phy_reg phy_reg_init[] = {
2608 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2611 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2613 static const struct phy_reg phy_reg_init[] = {
2619 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2622 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2624 struct pci_dev *pdev = tp->pci_dev;
2626 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2627 (pdev->subsystem_device != 0xe000))
2630 rtl_writephy(tp, 0x1f, 0x0001);
2631 rtl_writephy(tp, 0x10, 0xf01b);
2632 rtl_writephy(tp, 0x1f, 0x0000);
2635 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2637 static const struct phy_reg phy_reg_init[] = {
2677 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2679 rtl8169scd_hw_phy_config_quirk(tp);
2682 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2684 static const struct phy_reg phy_reg_init[] = {
2732 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2735 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2737 static const struct phy_reg phy_reg_init[] = {
2742 rtl_writephy(tp, 0x1f, 0x0001);
2743 rtl_patchphy(tp, 0x16, 1 << 0);
2745 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2748 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2750 static const struct phy_reg phy_reg_init[] = {
2756 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2759 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2761 static const struct phy_reg phy_reg_init[] = {
2769 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2772 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2774 static const struct phy_reg phy_reg_init[] = {
2780 rtl_writephy(tp, 0x1f, 0x0000);
2781 rtl_patchphy(tp, 0x14, 1 << 5);
2782 rtl_patchphy(tp, 0x0d, 1 << 5);
2784 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2787 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2789 static const struct phy_reg phy_reg_init[] = {
2809 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2811 rtl_patchphy(tp, 0x14, 1 << 5);
2812 rtl_patchphy(tp, 0x0d, 1 << 5);
2813 rtl_writephy(tp, 0x1f, 0x0000);
2816 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2818 static const struct phy_reg phy_reg_init[] = {
2836 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2838 rtl_patchphy(tp, 0x16, 1 << 0);
2839 rtl_patchphy(tp, 0x14, 1 << 5);
2840 rtl_patchphy(tp, 0x0d, 1 << 5);
2841 rtl_writephy(tp, 0x1f, 0x0000);
2844 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2846 static const struct phy_reg phy_reg_init[] = {
2858 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2860 rtl_patchphy(tp, 0x16, 1 << 0);
2861 rtl_patchphy(tp, 0x14, 1 << 5);
2862 rtl_patchphy(tp, 0x0d, 1 << 5);
2863 rtl_writephy(tp, 0x1f, 0x0000);
2866 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2868 rtl8168c_3_hw_phy_config(tp);
2871 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2873 static const struct phy_reg phy_reg_init_0[] = {
2874 /* Channel Estimation */
2895 * Enhance line driver power
2904 * Can not link to 1Gbps with bad cable
2905 * Decrease SNR threshold form 21.07dB to 19.04dB
2914 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2918 * Fine Tune Switching regulator parameter
2920 rtl_writephy(tp, 0x1f, 0x0002);
2921 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2922 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
2924 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2925 static const struct phy_reg phy_reg_init[] = {
2935 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2937 val = rtl_readphy(tp, 0x0d);
2939 if ((val & 0x00ff) != 0x006c) {
2940 static const u32 set[] = {
2941 0x0065, 0x0066, 0x0067, 0x0068,
2942 0x0069, 0x006a, 0x006b, 0x006c
2946 rtl_writephy(tp, 0x1f, 0x0002);
2949 for (i = 0; i < ARRAY_SIZE(set); i++)
2950 rtl_writephy(tp, 0x0d, val | set[i]);
2953 static const struct phy_reg phy_reg_init[] = {
2961 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2964 /* RSET couple improve */
2965 rtl_writephy(tp, 0x1f, 0x0002);
2966 rtl_patchphy(tp, 0x0d, 0x0300);
2967 rtl_patchphy(tp, 0x0f, 0x0010);
2969 /* Fine tune PLL performance */
2970 rtl_writephy(tp, 0x1f, 0x0002);
2971 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2972 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
2974 rtl_writephy(tp, 0x1f, 0x0005);
2975 rtl_writephy(tp, 0x05, 0x001b);
2977 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2979 rtl_writephy(tp, 0x1f, 0x0000);
2982 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2984 static const struct phy_reg phy_reg_init_0[] = {
2985 /* Channel Estimation */
3006 * Enhance line driver power
3015 * Can not link to 1Gbps with bad cable
3016 * Decrease SNR threshold form 21.07dB to 19.04dB
3025 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
3027 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3028 static const struct phy_reg phy_reg_init[] = {
3039 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3041 val = rtl_readphy(tp, 0x0d);
3042 if ((val & 0x00ff) != 0x006c) {
3043 static const u32 set[] = {
3044 0x0065, 0x0066, 0x0067, 0x0068,
3045 0x0069, 0x006a, 0x006b, 0x006c
3049 rtl_writephy(tp, 0x1f, 0x0002);
3052 for (i = 0; i < ARRAY_SIZE(set); i++)
3053 rtl_writephy(tp, 0x0d, val | set[i]);
3056 static const struct phy_reg phy_reg_init[] = {
3064 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3067 /* Fine tune PLL performance */
3068 rtl_writephy(tp, 0x1f, 0x0002);
3069 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3070 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3072 /* Switching regulator Slew rate */
3073 rtl_writephy(tp, 0x1f, 0x0002);
3074 rtl_patchphy(tp, 0x0f, 0x0017);
3076 rtl_writephy(tp, 0x1f, 0x0005);
3077 rtl_writephy(tp, 0x05, 0x001b);
3079 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
3081 rtl_writephy(tp, 0x1f, 0x0000);
3084 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
3086 static const struct phy_reg phy_reg_init[] = {
3142 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3145 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3147 static const struct phy_reg phy_reg_init[] = {
3157 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3158 rtl_patchphy(tp, 0x0d, 1 << 5);
3161 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3163 static const struct phy_reg phy_reg_init[] = {
3164 /* Enable Delay cap */
3170 /* Channel estimation fine tune */
3179 /* Update PFM & 10M TX idle timer */
3191 rtl_apply_firmware(tp);
3193 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3195 /* DCO enable for 10M IDLE Power */
3196 rtl_writephy(tp, 0x1f, 0x0007);
3197 rtl_writephy(tp, 0x1e, 0x0023);
3198 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3199 rtl_writephy(tp, 0x1f, 0x0000);
3201 /* For impedance matching */
3202 rtl_writephy(tp, 0x1f, 0x0002);
3203 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
3204 rtl_writephy(tp, 0x1f, 0x0000);
3206 /* PHY auto speed down */
3207 rtl_writephy(tp, 0x1f, 0x0007);
3208 rtl_writephy(tp, 0x1e, 0x002d);
3209 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
3210 rtl_writephy(tp, 0x1f, 0x0000);
3211 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3213 rtl_writephy(tp, 0x1f, 0x0005);
3214 rtl_writephy(tp, 0x05, 0x8b86);
3215 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3216 rtl_writephy(tp, 0x1f, 0x0000);
3218 rtl_writephy(tp, 0x1f, 0x0005);
3219 rtl_writephy(tp, 0x05, 0x8b85);
3220 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3221 rtl_writephy(tp, 0x1f, 0x0007);
3222 rtl_writephy(tp, 0x1e, 0x0020);
3223 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
3224 rtl_writephy(tp, 0x1f, 0x0006);
3225 rtl_writephy(tp, 0x00, 0x5a00);
3226 rtl_writephy(tp, 0x1f, 0x0000);
3227 rtl_writephy(tp, 0x0d, 0x0007);
3228 rtl_writephy(tp, 0x0e, 0x003c);
3229 rtl_writephy(tp, 0x0d, 0x4007);
3230 rtl_writephy(tp, 0x0e, 0x0000);
3231 rtl_writephy(tp, 0x0d, 0x0000);
3234 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3237 addr[0] | (addr[1] << 8),
3238 addr[2] | (addr[3] << 8),
3239 addr[4] | (addr[5] << 8)
3241 const struct exgmac_reg e[] = {
3242 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3243 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3244 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3245 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3248 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3251 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3253 static const struct phy_reg phy_reg_init[] = {
3254 /* Enable Delay cap */
3263 /* Channel estimation fine tune */
3280 rtl_apply_firmware(tp);
3282 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3284 /* For 4-corner performance improve */
3285 rtl_writephy(tp, 0x1f, 0x0005);
3286 rtl_writephy(tp, 0x05, 0x8b80);
3287 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3288 rtl_writephy(tp, 0x1f, 0x0000);
3290 /* PHY auto speed down */
3291 rtl_writephy(tp, 0x1f, 0x0004);
3292 rtl_writephy(tp, 0x1f, 0x0007);
3293 rtl_writephy(tp, 0x1e, 0x002d);
3294 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3295 rtl_writephy(tp, 0x1f, 0x0002);
3296 rtl_writephy(tp, 0x1f, 0x0000);
3297 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3299 /* improve 10M EEE waveform */
3300 rtl_writephy(tp, 0x1f, 0x0005);
3301 rtl_writephy(tp, 0x05, 0x8b86);
3302 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3303 rtl_writephy(tp, 0x1f, 0x0000);
3305 /* Improve 2-pair detection performance */
3306 rtl_writephy(tp, 0x1f, 0x0005);
3307 rtl_writephy(tp, 0x05, 0x8b85);
3308 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3309 rtl_writephy(tp, 0x1f, 0x0000);
3312 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
3313 rtl_writephy(tp, 0x1f, 0x0005);
3314 rtl_writephy(tp, 0x05, 0x8b85);
3315 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
3316 rtl_writephy(tp, 0x1f, 0x0004);
3317 rtl_writephy(tp, 0x1f, 0x0007);
3318 rtl_writephy(tp, 0x1e, 0x0020);
3319 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
3320 rtl_writephy(tp, 0x1f, 0x0002);
3321 rtl_writephy(tp, 0x1f, 0x0000);
3322 rtl_writephy(tp, 0x0d, 0x0007);
3323 rtl_writephy(tp, 0x0e, 0x003c);
3324 rtl_writephy(tp, 0x0d, 0x4007);
3325 rtl_writephy(tp, 0x0e, 0x0006);
3326 rtl_writephy(tp, 0x0d, 0x0000);
3329 rtl_writephy(tp, 0x1f, 0x0003);
3330 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3331 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
3332 rtl_writephy(tp, 0x1f, 0x0000);
3333 rtl_writephy(tp, 0x1f, 0x0005);
3334 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3335 rtl_writephy(tp, 0x1f, 0x0000);
3337 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3338 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3341 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3343 /* For 4-corner performance improve */
3344 rtl_writephy(tp, 0x1f, 0x0005);
3345 rtl_writephy(tp, 0x05, 0x8b80);
3346 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
3347 rtl_writephy(tp, 0x1f, 0x0000);
3349 /* PHY auto speed down */
3350 rtl_writephy(tp, 0x1f, 0x0007);
3351 rtl_writephy(tp, 0x1e, 0x002d);
3352 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3353 rtl_writephy(tp, 0x1f, 0x0000);
3354 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3356 /* Improve 10M EEE waveform */
3357 rtl_writephy(tp, 0x1f, 0x0005);
3358 rtl_writephy(tp, 0x05, 0x8b86);
3359 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3360 rtl_writephy(tp, 0x1f, 0x0000);
3363 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3365 static const struct phy_reg phy_reg_init[] = {
3366 /* Channel estimation fine tune */
3371 /* Modify green table for giga & fnet */
3388 /* Modify green table for 10M */
3394 /* Disable hiimpedance detection (RTCT) */
3400 rtl_apply_firmware(tp);
3402 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3404 rtl8168f_hw_phy_config(tp);
3406 /* Improve 2-pair detection performance */
3407 rtl_writephy(tp, 0x1f, 0x0005);
3408 rtl_writephy(tp, 0x05, 0x8b85);
3409 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3410 rtl_writephy(tp, 0x1f, 0x0000);
3413 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3415 rtl_apply_firmware(tp);
3417 rtl8168f_hw_phy_config(tp);
3420 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3422 static const struct phy_reg phy_reg_init[] = {
3423 /* Channel estimation fine tune */
3428 /* Modify green table for giga & fnet */
3445 /* Modify green table for 10M */
3451 /* Disable hiimpedance detection (RTCT) */
3458 rtl_apply_firmware(tp);
3460 rtl8168f_hw_phy_config(tp);
3462 /* Improve 2-pair detection performance */
3463 rtl_writephy(tp, 0x1f, 0x0005);
3464 rtl_writephy(tp, 0x05, 0x8b85);
3465 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3466 rtl_writephy(tp, 0x1f, 0x0000);
3468 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3470 /* Modify green table for giga */
3471 rtl_writephy(tp, 0x1f, 0x0005);
3472 rtl_writephy(tp, 0x05, 0x8b54);
3473 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3474 rtl_writephy(tp, 0x05, 0x8b5d);
3475 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3476 rtl_writephy(tp, 0x05, 0x8a7c);
3477 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3478 rtl_writephy(tp, 0x05, 0x8a7f);
3479 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
3480 rtl_writephy(tp, 0x05, 0x8a82);
3481 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3482 rtl_writephy(tp, 0x05, 0x8a85);
3483 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3484 rtl_writephy(tp, 0x05, 0x8a88);
3485 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3486 rtl_writephy(tp, 0x1f, 0x0000);
3488 /* uc same-seed solution */
3489 rtl_writephy(tp, 0x1f, 0x0005);
3490 rtl_writephy(tp, 0x05, 0x8b85);
3491 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
3492 rtl_writephy(tp, 0x1f, 0x0000);
3495 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
3496 rtl_writephy(tp, 0x1f, 0x0005);
3497 rtl_writephy(tp, 0x05, 0x8b85);
3498 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3499 rtl_writephy(tp, 0x1f, 0x0004);
3500 rtl_writephy(tp, 0x1f, 0x0007);
3501 rtl_writephy(tp, 0x1e, 0x0020);
3502 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
3503 rtl_writephy(tp, 0x1f, 0x0000);
3504 rtl_writephy(tp, 0x0d, 0x0007);
3505 rtl_writephy(tp, 0x0e, 0x003c);
3506 rtl_writephy(tp, 0x0d, 0x4007);
3507 rtl_writephy(tp, 0x0e, 0x0000);
3508 rtl_writephy(tp, 0x0d, 0x0000);
3511 rtl_writephy(tp, 0x1f, 0x0003);
3512 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3513 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3514 rtl_writephy(tp, 0x1f, 0x0000);
3517 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3519 rtl_apply_firmware(tp);
3521 rtl_writephy(tp, 0x1f, 0x0a46);
3522 if (rtl_readphy(tp, 0x10) & 0x0100) {
3523 rtl_writephy(tp, 0x1f, 0x0bcc);
3524 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
3526 rtl_writephy(tp, 0x1f, 0x0bcc);
3527 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
3530 rtl_writephy(tp, 0x1f, 0x0a46);
3531 if (rtl_readphy(tp, 0x13) & 0x0100) {
3532 rtl_writephy(tp, 0x1f, 0x0c41);
3533 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
3535 rtl_writephy(tp, 0x1f, 0x0c41);
3536 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
3539 /* Enable PHY auto speed down */
3540 rtl_writephy(tp, 0x1f, 0x0a44);
3541 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3543 rtl_writephy(tp, 0x1f, 0x0bcc);
3544 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
3545 rtl_writephy(tp, 0x1f, 0x0a44);
3546 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3547 rtl_writephy(tp, 0x1f, 0x0a43);
3548 rtl_writephy(tp, 0x13, 0x8084);
3549 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3550 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3552 /* EEE auto-fallback function */
3553 rtl_writephy(tp, 0x1f, 0x0a4b);
3554 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3556 /* Enable UC LPF tune function */
3557 rtl_writephy(tp, 0x1f, 0x0a43);
3558 rtl_writephy(tp, 0x13, 0x8012);
3559 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3561 rtl_writephy(tp, 0x1f, 0x0c42);
3562 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3564 /* Improve SWR Efficiency */
3565 rtl_writephy(tp, 0x1f, 0x0bcd);
3566 rtl_writephy(tp, 0x14, 0x5065);
3567 rtl_writephy(tp, 0x14, 0xd065);
3568 rtl_writephy(tp, 0x1f, 0x0bc8);
3569 rtl_writephy(tp, 0x11, 0x5655);
3570 rtl_writephy(tp, 0x1f, 0x0bcd);
3571 rtl_writephy(tp, 0x14, 0x1065);
3572 rtl_writephy(tp, 0x14, 0x9065);
3573 rtl_writephy(tp, 0x14, 0x1065);
3575 /* Check ALDPS bit, disable it if enabled */
3576 rtl_writephy(tp, 0x1f, 0x0a43);
3577 if (rtl_readphy(tp, 0x10) & 0x0004)
3578 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3580 rtl_writephy(tp, 0x1f, 0x0000);
3583 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3585 rtl_apply_firmware(tp);
3588 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3593 rtl_apply_firmware(tp);
3595 /* CHN EST parameters adjust - giga master */
3596 rtl_writephy(tp, 0x1f, 0x0a43);
3597 rtl_writephy(tp, 0x13, 0x809b);
3598 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
3599 rtl_writephy(tp, 0x13, 0x80a2);
3600 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
3601 rtl_writephy(tp, 0x13, 0x80a4);
3602 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
3603 rtl_writephy(tp, 0x13, 0x809c);
3604 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
3605 rtl_writephy(tp, 0x1f, 0x0000);
3607 /* CHN EST parameters adjust - giga slave */
3608 rtl_writephy(tp, 0x1f, 0x0a43);
3609 rtl_writephy(tp, 0x13, 0x80ad);
3610 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
3611 rtl_writephy(tp, 0x13, 0x80b4);
3612 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
3613 rtl_writephy(tp, 0x13, 0x80ac);
3614 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
3615 rtl_writephy(tp, 0x1f, 0x0000);
3617 /* CHN EST parameters adjust - fnet */
3618 rtl_writephy(tp, 0x1f, 0x0a43);
3619 rtl_writephy(tp, 0x13, 0x808e);
3620 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
3621 rtl_writephy(tp, 0x13, 0x8090);
3622 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
3623 rtl_writephy(tp, 0x13, 0x8092);
3624 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
3625 rtl_writephy(tp, 0x1f, 0x0000);
3627 /* enable R-tune & PGA-retune function */
3629 rtl_writephy(tp, 0x1f, 0x0a46);
3630 data = rtl_readphy(tp, 0x13);
3633 dout_tapbin |= data;
3634 data = rtl_readphy(tp, 0x12);
3637 dout_tapbin |= data;
3638 dout_tapbin = ~(dout_tapbin^0x08);
3640 dout_tapbin &= 0xf000;
3641 rtl_writephy(tp, 0x1f, 0x0a43);
3642 rtl_writephy(tp, 0x13, 0x827a);
3643 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3644 rtl_writephy(tp, 0x13, 0x827b);
3645 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3646 rtl_writephy(tp, 0x13, 0x827c);
3647 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3648 rtl_writephy(tp, 0x13, 0x827d);
3649 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3651 rtl_writephy(tp, 0x1f, 0x0a43);
3652 rtl_writephy(tp, 0x13, 0x0811);
3653 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3654 rtl_writephy(tp, 0x1f, 0x0a42);
3655 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3656 rtl_writephy(tp, 0x1f, 0x0000);
3658 /* enable GPHY 10M */
3659 rtl_writephy(tp, 0x1f, 0x0a44);
3660 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3661 rtl_writephy(tp, 0x1f, 0x0000);
3663 /* SAR ADC performance */
3664 rtl_writephy(tp, 0x1f, 0x0bca);
3665 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
3666 rtl_writephy(tp, 0x1f, 0x0000);
3668 rtl_writephy(tp, 0x1f, 0x0a43);
3669 rtl_writephy(tp, 0x13, 0x803f);
3670 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3671 rtl_writephy(tp, 0x13, 0x8047);
3672 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3673 rtl_writephy(tp, 0x13, 0x804f);
3674 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3675 rtl_writephy(tp, 0x13, 0x8057);
3676 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3677 rtl_writephy(tp, 0x13, 0x805f);
3678 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3679 rtl_writephy(tp, 0x13, 0x8067);
3680 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3681 rtl_writephy(tp, 0x13, 0x806f);
3682 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3683 rtl_writephy(tp, 0x1f, 0x0000);
3685 /* disable phy pfm mode */
3686 rtl_writephy(tp, 0x1f, 0x0a44);
3687 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3688 rtl_writephy(tp, 0x1f, 0x0000);
3690 /* Check ALDPS bit, disable it if enabled */
3691 rtl_writephy(tp, 0x1f, 0x0a43);
3692 if (rtl_readphy(tp, 0x10) & 0x0004)
3693 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3695 rtl_writephy(tp, 0x1f, 0x0000);
3698 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3700 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3704 rtl_apply_firmware(tp);
3706 /* CHIN EST parameter update */
3707 rtl_writephy(tp, 0x1f, 0x0a43);
3708 rtl_writephy(tp, 0x13, 0x808a);
3709 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
3710 rtl_writephy(tp, 0x1f, 0x0000);
3712 /* enable R-tune & PGA-retune function */
3713 rtl_writephy(tp, 0x1f, 0x0a43);
3714 rtl_writephy(tp, 0x13, 0x0811);
3715 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3716 rtl_writephy(tp, 0x1f, 0x0a42);
3717 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3718 rtl_writephy(tp, 0x1f, 0x0000);
3720 /* enable GPHY 10M */
3721 rtl_writephy(tp, 0x1f, 0x0a44);
3722 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3723 rtl_writephy(tp, 0x1f, 0x0000);
3725 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3726 data = r8168_mac_ocp_read(tp, 0xdd02);
3727 ioffset_p3 = ((data & 0x80)>>7);
3730 data = r8168_mac_ocp_read(tp, 0xdd00);
3731 ioffset_p3 |= ((data & (0xe000))>>13);
3732 ioffset_p2 = ((data & (0x1e00))>>9);
3733 ioffset_p1 = ((data & (0x01e0))>>5);
3734 ioffset_p0 = ((data & 0x0010)>>4);
3736 ioffset_p0 |= (data & (0x07));
3737 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3739 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3740 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
3741 rtl_writephy(tp, 0x1f, 0x0bcf);
3742 rtl_writephy(tp, 0x16, data);
3743 rtl_writephy(tp, 0x1f, 0x0000);
3746 /* Modify rlen (TX LPF corner frequency) level */
3747 rtl_writephy(tp, 0x1f, 0x0bcd);
3748 data = rtl_readphy(tp, 0x16);
3753 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3754 rtl_writephy(tp, 0x17, data);
3755 rtl_writephy(tp, 0x1f, 0x0bcd);
3756 rtl_writephy(tp, 0x1f, 0x0000);
3758 /* disable phy pfm mode */
3759 rtl_writephy(tp, 0x1f, 0x0a44);
3760 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3761 rtl_writephy(tp, 0x1f, 0x0000);
3763 /* Check ALDPS bit, disable it if enabled */
3764 rtl_writephy(tp, 0x1f, 0x0a43);
3765 if (rtl_readphy(tp, 0x10) & 0x0004)
3766 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3768 rtl_writephy(tp, 0x1f, 0x0000);
3771 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3773 /* Enable PHY auto speed down */
3774 rtl_writephy(tp, 0x1f, 0x0a44);
3775 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3776 rtl_writephy(tp, 0x1f, 0x0000);
3778 /* patch 10M & ALDPS */
3779 rtl_writephy(tp, 0x1f, 0x0bcc);
3780 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3781 rtl_writephy(tp, 0x1f, 0x0a44);
3782 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3783 rtl_writephy(tp, 0x1f, 0x0a43);
3784 rtl_writephy(tp, 0x13, 0x8084);
3785 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3786 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3787 rtl_writephy(tp, 0x1f, 0x0000);
3789 /* Enable EEE auto-fallback function */
3790 rtl_writephy(tp, 0x1f, 0x0a4b);
3791 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3792 rtl_writephy(tp, 0x1f, 0x0000);
3794 /* Enable UC LPF tune function */
3795 rtl_writephy(tp, 0x1f, 0x0a43);
3796 rtl_writephy(tp, 0x13, 0x8012);
3797 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3798 rtl_writephy(tp, 0x1f, 0x0000);
3800 /* set rg_sel_sdm_rate */
3801 rtl_writephy(tp, 0x1f, 0x0c42);
3802 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3803 rtl_writephy(tp, 0x1f, 0x0000);
3805 /* Check ALDPS bit, disable it if enabled */
3806 rtl_writephy(tp, 0x1f, 0x0a43);
3807 if (rtl_readphy(tp, 0x10) & 0x0004)
3808 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3810 rtl_writephy(tp, 0x1f, 0x0000);
3813 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3815 /* patch 10M & ALDPS */
3816 rtl_writephy(tp, 0x1f, 0x0bcc);
3817 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3818 rtl_writephy(tp, 0x1f, 0x0a44);
3819 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3820 rtl_writephy(tp, 0x1f, 0x0a43);
3821 rtl_writephy(tp, 0x13, 0x8084);
3822 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3823 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3824 rtl_writephy(tp, 0x1f, 0x0000);
3826 /* Enable UC LPF tune function */
3827 rtl_writephy(tp, 0x1f, 0x0a43);
3828 rtl_writephy(tp, 0x13, 0x8012);
3829 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3830 rtl_writephy(tp, 0x1f, 0x0000);
3832 /* Set rg_sel_sdm_rate */
3833 rtl_writephy(tp, 0x1f, 0x0c42);
3834 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3835 rtl_writephy(tp, 0x1f, 0x0000);
3837 /* Channel estimation parameters */
3838 rtl_writephy(tp, 0x1f, 0x0a43);
3839 rtl_writephy(tp, 0x13, 0x80f3);
3840 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3841 rtl_writephy(tp, 0x13, 0x80f0);
3842 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3843 rtl_writephy(tp, 0x13, 0x80ef);
3844 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3845 rtl_writephy(tp, 0x13, 0x80f6);
3846 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3847 rtl_writephy(tp, 0x13, 0x80ec);
3848 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3849 rtl_writephy(tp, 0x13, 0x80ed);
3850 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3851 rtl_writephy(tp, 0x13, 0x80f2);
3852 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3853 rtl_writephy(tp, 0x13, 0x80f4);
3854 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3855 rtl_writephy(tp, 0x1f, 0x0a43);
3856 rtl_writephy(tp, 0x13, 0x8110);
3857 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3858 rtl_writephy(tp, 0x13, 0x810f);
3859 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3860 rtl_writephy(tp, 0x13, 0x8111);
3861 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3862 rtl_writephy(tp, 0x13, 0x8113);
3863 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3864 rtl_writephy(tp, 0x13, 0x8115);
3865 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3866 rtl_writephy(tp, 0x13, 0x810e);
3867 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3868 rtl_writephy(tp, 0x13, 0x810c);
3869 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3870 rtl_writephy(tp, 0x13, 0x810b);
3871 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3872 rtl_writephy(tp, 0x1f, 0x0a43);
3873 rtl_writephy(tp, 0x13, 0x80d1);
3874 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3875 rtl_writephy(tp, 0x13, 0x80cd);
3876 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3877 rtl_writephy(tp, 0x13, 0x80d3);
3878 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3879 rtl_writephy(tp, 0x13, 0x80d5);
3880 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3881 rtl_writephy(tp, 0x13, 0x80d7);
3882 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3884 /* Force PWM-mode */
3885 rtl_writephy(tp, 0x1f, 0x0bcd);
3886 rtl_writephy(tp, 0x14, 0x5065);
3887 rtl_writephy(tp, 0x14, 0xd065);
3888 rtl_writephy(tp, 0x1f, 0x0bc8);
3889 rtl_writephy(tp, 0x12, 0x00ed);
3890 rtl_writephy(tp, 0x1f, 0x0bcd);
3891 rtl_writephy(tp, 0x14, 0x1065);
3892 rtl_writephy(tp, 0x14, 0x9065);
3893 rtl_writephy(tp, 0x14, 0x1065);
3894 rtl_writephy(tp, 0x1f, 0x0000);
3896 /* Check ALDPS bit, disable it if enabled */
3897 rtl_writephy(tp, 0x1f, 0x0a43);
3898 if (rtl_readphy(tp, 0x10) & 0x0004)
3899 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3901 rtl_writephy(tp, 0x1f, 0x0000);
3904 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3906 static const struct phy_reg phy_reg_init[] = {
3913 rtl_writephy(tp, 0x1f, 0x0000);
3914 rtl_patchphy(tp, 0x11, 1 << 12);
3915 rtl_patchphy(tp, 0x19, 1 << 13);
3916 rtl_patchphy(tp, 0x10, 1 << 15);
3918 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3921 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3923 static const struct phy_reg phy_reg_init[] = {
3937 /* Disable ALDPS before ram code */
3938 rtl_writephy(tp, 0x1f, 0x0000);
3939 rtl_writephy(tp, 0x18, 0x0310);
3942 rtl_apply_firmware(tp);
3944 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3947 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3949 /* Disable ALDPS before setting firmware */
3950 rtl_writephy(tp, 0x1f, 0x0000);
3951 rtl_writephy(tp, 0x18, 0x0310);
3954 rtl_apply_firmware(tp);
3957 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3958 rtl_writephy(tp, 0x1f, 0x0004);
3959 rtl_writephy(tp, 0x10, 0x401f);
3960 rtl_writephy(tp, 0x19, 0x7030);
3961 rtl_writephy(tp, 0x1f, 0x0000);
3964 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3966 static const struct phy_reg phy_reg_init[] = {
3973 /* Disable ALDPS before ram code */
3974 rtl_writephy(tp, 0x1f, 0x0000);
3975 rtl_writephy(tp, 0x18, 0x0310);
3978 rtl_apply_firmware(tp);
3980 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3981 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3983 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
3986 static void rtl_hw_phy_config(struct net_device *dev)
3988 struct rtl8169_private *tp = netdev_priv(dev);
3990 rtl8169_print_mac_version(tp);
3992 switch (tp->mac_version) {
3993 case RTL_GIGA_MAC_VER_01:
3995 case RTL_GIGA_MAC_VER_02:
3996 case RTL_GIGA_MAC_VER_03:
3997 rtl8169s_hw_phy_config(tp);
3999 case RTL_GIGA_MAC_VER_04:
4000 rtl8169sb_hw_phy_config(tp);
4002 case RTL_GIGA_MAC_VER_05:
4003 rtl8169scd_hw_phy_config(tp);
4005 case RTL_GIGA_MAC_VER_06:
4006 rtl8169sce_hw_phy_config(tp);
4008 case RTL_GIGA_MAC_VER_07:
4009 case RTL_GIGA_MAC_VER_08:
4010 case RTL_GIGA_MAC_VER_09:
4011 rtl8102e_hw_phy_config(tp);
4013 case RTL_GIGA_MAC_VER_11:
4014 rtl8168bb_hw_phy_config(tp);
4016 case RTL_GIGA_MAC_VER_12:
4017 rtl8168bef_hw_phy_config(tp);
4019 case RTL_GIGA_MAC_VER_17:
4020 rtl8168bef_hw_phy_config(tp);
4022 case RTL_GIGA_MAC_VER_18:
4023 rtl8168cp_1_hw_phy_config(tp);
4025 case RTL_GIGA_MAC_VER_19:
4026 rtl8168c_1_hw_phy_config(tp);
4028 case RTL_GIGA_MAC_VER_20:
4029 rtl8168c_2_hw_phy_config(tp);
4031 case RTL_GIGA_MAC_VER_21:
4032 rtl8168c_3_hw_phy_config(tp);
4034 case RTL_GIGA_MAC_VER_22:
4035 rtl8168c_4_hw_phy_config(tp);
4037 case RTL_GIGA_MAC_VER_23:
4038 case RTL_GIGA_MAC_VER_24:
4039 rtl8168cp_2_hw_phy_config(tp);
4041 case RTL_GIGA_MAC_VER_25:
4042 rtl8168d_1_hw_phy_config(tp);
4044 case RTL_GIGA_MAC_VER_26:
4045 rtl8168d_2_hw_phy_config(tp);
4047 case RTL_GIGA_MAC_VER_27:
4048 rtl8168d_3_hw_phy_config(tp);
4050 case RTL_GIGA_MAC_VER_28:
4051 rtl8168d_4_hw_phy_config(tp);
4053 case RTL_GIGA_MAC_VER_29:
4054 case RTL_GIGA_MAC_VER_30:
4055 rtl8105e_hw_phy_config(tp);
4057 case RTL_GIGA_MAC_VER_31:
4060 case RTL_GIGA_MAC_VER_32:
4061 case RTL_GIGA_MAC_VER_33:
4062 rtl8168e_1_hw_phy_config(tp);
4064 case RTL_GIGA_MAC_VER_34:
4065 rtl8168e_2_hw_phy_config(tp);
4067 case RTL_GIGA_MAC_VER_35:
4068 rtl8168f_1_hw_phy_config(tp);
4070 case RTL_GIGA_MAC_VER_36:
4071 rtl8168f_2_hw_phy_config(tp);
4074 case RTL_GIGA_MAC_VER_37:
4075 rtl8402_hw_phy_config(tp);
4078 case RTL_GIGA_MAC_VER_38:
4079 rtl8411_hw_phy_config(tp);
4082 case RTL_GIGA_MAC_VER_39:
4083 rtl8106e_hw_phy_config(tp);
4086 case RTL_GIGA_MAC_VER_40:
4087 rtl8168g_1_hw_phy_config(tp);
4089 case RTL_GIGA_MAC_VER_42:
4090 case RTL_GIGA_MAC_VER_43:
4091 case RTL_GIGA_MAC_VER_44:
4092 rtl8168g_2_hw_phy_config(tp);
4094 case RTL_GIGA_MAC_VER_45:
4095 case RTL_GIGA_MAC_VER_47:
4096 rtl8168h_1_hw_phy_config(tp);
4098 case RTL_GIGA_MAC_VER_46:
4099 case RTL_GIGA_MAC_VER_48:
4100 rtl8168h_2_hw_phy_config(tp);
4103 case RTL_GIGA_MAC_VER_49:
4104 rtl8168ep_1_hw_phy_config(tp);
4106 case RTL_GIGA_MAC_VER_50:
4107 case RTL_GIGA_MAC_VER_51:
4108 rtl8168ep_2_hw_phy_config(tp);
4111 case RTL_GIGA_MAC_VER_41:
4117 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4119 if (!test_and_set_bit(flag, tp->wk.flags))
4120 schedule_work(&tp->wk.work);
4123 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4125 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4126 (RTL_R8(tp, PHYstatus) & TBI_Enable);
4129 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
4131 rtl_hw_phy_config(dev);
4133 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4134 netif_dbg(tp, drv, dev,
4135 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4136 RTL_W8(tp, 0x82, 0x01);
4139 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4141 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4142 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4144 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
4145 netif_dbg(tp, drv, dev,
4146 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4147 RTL_W8(tp, 0x82, 0x01);
4148 netif_dbg(tp, drv, dev,
4149 "Set PHY Reg 0x0bh = 0x00h\n");
4150 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
4153 /* We may have called phy_speed_down before */
4154 phy_speed_up(dev->phydev);
4156 genphy_soft_reset(dev->phydev);
4159 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4163 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4165 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4168 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4171 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4172 rtl_rar_exgmac_set(tp, addr);
4174 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4176 rtl_unlock_work(tp);
4179 static int rtl_set_mac_address(struct net_device *dev, void *p)
4181 struct rtl8169_private *tp = netdev_priv(dev);
4182 struct device *d = tp_to_dev(tp);
4185 ret = eth_mac_addr(dev, p);
4189 pm_runtime_get_noresume(d);
4191 if (pm_runtime_active(d))
4192 rtl_rar_set(tp, dev->dev_addr);
4194 pm_runtime_put_noidle(d);
4199 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4201 if (!netif_running(dev))
4204 return phy_mii_ioctl(dev->phydev, ifr, cmd);
4207 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
4209 struct mdio_ops *ops = &tp->mdio_ops;
4211 switch (tp->mac_version) {
4212 case RTL_GIGA_MAC_VER_27:
4213 ops->write = r8168dp_1_mdio_write;
4214 ops->read = r8168dp_1_mdio_read;
4216 case RTL_GIGA_MAC_VER_28:
4217 case RTL_GIGA_MAC_VER_31:
4218 ops->write = r8168dp_2_mdio_write;
4219 ops->read = r8168dp_2_mdio_read;
4221 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4222 ops->write = r8168g_mdio_write;
4223 ops->read = r8168g_mdio_read;
4226 ops->write = r8169_mdio_write;
4227 ops->read = r8169_mdio_read;
4232 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4234 switch (tp->mac_version) {
4235 case RTL_GIGA_MAC_VER_25:
4236 case RTL_GIGA_MAC_VER_26:
4237 case RTL_GIGA_MAC_VER_29:
4238 case RTL_GIGA_MAC_VER_30:
4239 case RTL_GIGA_MAC_VER_32:
4240 case RTL_GIGA_MAC_VER_33:
4241 case RTL_GIGA_MAC_VER_34:
4242 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
4243 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
4244 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4251 static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4253 if (!netif_running(tp->dev) || !__rtl8169_get_wol(tp))
4256 phy_speed_down(tp->dev->phydev, false);
4257 rtl_wol_suspend_quirk(tp);
4262 static void r8168_pll_power_down(struct rtl8169_private *tp)
4264 if (r8168_check_dash(tp))
4267 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4268 tp->mac_version == RTL_GIGA_MAC_VER_33)
4269 rtl_ephy_write(tp, 0x19, 0xff64);
4271 if (rtl_wol_pll_power_down(tp))
4274 switch (tp->mac_version) {
4275 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
4276 case RTL_GIGA_MAC_VER_37:
4277 case RTL_GIGA_MAC_VER_39:
4278 case RTL_GIGA_MAC_VER_43:
4279 case RTL_GIGA_MAC_VER_44:
4280 case RTL_GIGA_MAC_VER_45:
4281 case RTL_GIGA_MAC_VER_46:
4282 case RTL_GIGA_MAC_VER_47:
4283 case RTL_GIGA_MAC_VER_48:
4284 case RTL_GIGA_MAC_VER_50:
4285 case RTL_GIGA_MAC_VER_51:
4286 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4288 case RTL_GIGA_MAC_VER_40:
4289 case RTL_GIGA_MAC_VER_41:
4290 case RTL_GIGA_MAC_VER_49:
4291 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
4292 0xfc000000, ERIAR_EXGMAC);
4293 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4298 static void r8168_pll_power_up(struct rtl8169_private *tp)
4300 switch (tp->mac_version) {
4301 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
4302 case RTL_GIGA_MAC_VER_37:
4303 case RTL_GIGA_MAC_VER_39:
4304 case RTL_GIGA_MAC_VER_43:
4305 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
4307 case RTL_GIGA_MAC_VER_44:
4308 case RTL_GIGA_MAC_VER_45:
4309 case RTL_GIGA_MAC_VER_46:
4310 case RTL_GIGA_MAC_VER_47:
4311 case RTL_GIGA_MAC_VER_48:
4312 case RTL_GIGA_MAC_VER_50:
4313 case RTL_GIGA_MAC_VER_51:
4314 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4316 case RTL_GIGA_MAC_VER_40:
4317 case RTL_GIGA_MAC_VER_41:
4318 case RTL_GIGA_MAC_VER_49:
4319 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4320 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
4321 0x00000000, ERIAR_EXGMAC);
4325 phy_resume(tp->dev->phydev);
4326 /* give MAC/PHY some time to resume */
4330 static void rtl_pll_power_down(struct rtl8169_private *tp)
4332 switch (tp->mac_version) {
4333 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4334 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4337 r8168_pll_power_down(tp);
4341 static void rtl_pll_power_up(struct rtl8169_private *tp)
4343 switch (tp->mac_version) {
4344 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4345 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4348 r8168_pll_power_up(tp);
4352 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4354 switch (tp->mac_version) {
4355 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4356 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
4357 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4359 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
4360 case RTL_GIGA_MAC_VER_34:
4361 case RTL_GIGA_MAC_VER_35:
4362 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4364 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4365 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4368 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
4373 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4375 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
4378 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4380 if (tp->jumbo_ops.enable) {
4381 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4382 tp->jumbo_ops.enable(tp);
4383 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4387 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4389 if (tp->jumbo_ops.disable) {
4390 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4391 tp->jumbo_ops.disable(tp);
4392 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4396 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4398 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4399 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
4400 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
4403 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4405 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4406 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
4407 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4410 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4412 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4415 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4417 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4420 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4422 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4423 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4424 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
4425 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
4428 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4430 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4431 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4432 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
4433 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4436 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4438 rtl_tx_performance_tweak(tp,
4439 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
4442 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4444 rtl_tx_performance_tweak(tp,
4445 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
4448 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4450 r8168b_0_hw_jumbo_enable(tp);
4452 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
4455 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4457 r8168b_0_hw_jumbo_disable(tp);
4459 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
4462 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
4464 struct jumbo_ops *ops = &tp->jumbo_ops;
4466 switch (tp->mac_version) {
4467 case RTL_GIGA_MAC_VER_11:
4468 ops->disable = r8168b_0_hw_jumbo_disable;
4469 ops->enable = r8168b_0_hw_jumbo_enable;
4471 case RTL_GIGA_MAC_VER_12:
4472 case RTL_GIGA_MAC_VER_17:
4473 ops->disable = r8168b_1_hw_jumbo_disable;
4474 ops->enable = r8168b_1_hw_jumbo_enable;
4476 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4477 case RTL_GIGA_MAC_VER_19:
4478 case RTL_GIGA_MAC_VER_20:
4479 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4480 case RTL_GIGA_MAC_VER_22:
4481 case RTL_GIGA_MAC_VER_23:
4482 case RTL_GIGA_MAC_VER_24:
4483 case RTL_GIGA_MAC_VER_25:
4484 case RTL_GIGA_MAC_VER_26:
4485 ops->disable = r8168c_hw_jumbo_disable;
4486 ops->enable = r8168c_hw_jumbo_enable;
4488 case RTL_GIGA_MAC_VER_27:
4489 case RTL_GIGA_MAC_VER_28:
4490 ops->disable = r8168dp_hw_jumbo_disable;
4491 ops->enable = r8168dp_hw_jumbo_enable;
4493 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4494 case RTL_GIGA_MAC_VER_32:
4495 case RTL_GIGA_MAC_VER_33:
4496 case RTL_GIGA_MAC_VER_34:
4497 ops->disable = r8168e_hw_jumbo_disable;
4498 ops->enable = r8168e_hw_jumbo_enable;
4502 * No action needed for jumbo frames with 8169.
4503 * No jumbo for 810x at all.
4505 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4507 ops->disable = NULL;
4513 DECLARE_RTL_COND(rtl_chipcmd_cond)
4515 return RTL_R8(tp, ChipCmd) & CmdReset;
4518 static void rtl_hw_reset(struct rtl8169_private *tp)
4520 RTL_W8(tp, ChipCmd, CmdReset);
4522 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
4525 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4527 struct rtl_fw *rtl_fw;
4531 name = rtl_lookup_firmware_name(tp);
4533 goto out_no_firmware;
4535 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4539 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
4543 rc = rtl_check_firmware(tp, rtl_fw);
4545 goto err_release_firmware;
4547 tp->rtl_fw = rtl_fw;
4551 err_release_firmware:
4552 release_firmware(rtl_fw->fw);
4556 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4563 static void rtl_request_firmware(struct rtl8169_private *tp)
4565 if (IS_ERR(tp->rtl_fw))
4566 rtl_request_uncached_firmware(tp);
4569 static void rtl_rx_close(struct rtl8169_private *tp)
4571 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4574 DECLARE_RTL_COND(rtl_npq_cond)
4576 return RTL_R8(tp, TxPoll) & NPQ;
4579 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4581 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
4584 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4586 /* Disable interrupts */
4587 rtl8169_irq_mask_and_ack(tp);
4591 switch (tp->mac_version) {
4592 case RTL_GIGA_MAC_VER_27:
4593 case RTL_GIGA_MAC_VER_28:
4594 case RTL_GIGA_MAC_VER_31:
4595 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
4597 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4598 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4599 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4600 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4603 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4611 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
4613 /* Set DMA burst size and Interframe Gap Time */
4614 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
4615 (InterFrameGap << TxInterFrameGapShift));
4618 static void rtl_set_rx_max_size(struct rtl8169_private *tp)
4620 /* Low hurts. Let's disable the filtering. */
4621 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
4624 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
4627 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4628 * register to be written before TxDescAddrLow to work.
4629 * Switching from MMIO to I/O access fixes the issue as well.
4631 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4632 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4633 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4634 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4637 static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
4639 static const struct rtl_cfg2_info {
4644 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4645 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4646 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4647 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
4649 const struct rtl_cfg2_info *p = cfg2_info;
4653 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
4654 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
4655 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4656 RTL_W32(tp, 0x7c, p->val);
4662 static void rtl_set_rx_mode(struct net_device *dev)
4664 struct rtl8169_private *tp = netdev_priv(dev);
4665 u32 mc_filter[2]; /* Multicast hash filter */
4669 if (dev->flags & IFF_PROMISC) {
4670 /* Unconditionally log net taps. */
4671 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4673 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4675 mc_filter[1] = mc_filter[0] = 0xffffffff;
4676 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4677 (dev->flags & IFF_ALLMULTI)) {
4678 /* Too many to filter perfectly -- accept all multicasts. */
4679 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4680 mc_filter[1] = mc_filter[0] = 0xffffffff;
4682 struct netdev_hw_addr *ha;
4684 rx_mode = AcceptBroadcast | AcceptMyPhys;
4685 mc_filter[1] = mc_filter[0] = 0;
4686 netdev_for_each_mc_addr(ha, dev) {
4687 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4688 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4689 rx_mode |= AcceptMulticast;
4693 if (dev->features & NETIF_F_RXALL)
4694 rx_mode |= (AcceptErr | AcceptRunt);
4696 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4698 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4699 u32 data = mc_filter[0];
4701 mc_filter[0] = swab32(mc_filter[1]);
4702 mc_filter[1] = swab32(data);
4705 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4706 mc_filter[1] = mc_filter[0] = 0xffffffff;
4708 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4709 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
4711 RTL_W32(tp, RxConfig, tmp);
4714 static void rtl_hw_start(struct rtl8169_private *tp)
4716 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4720 rtl_set_rx_max_size(tp);
4721 rtl_set_rx_tx_desc_registers(tp);
4722 rtl_set_rx_tx_config_registers(tp);
4723 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4725 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4726 RTL_R8(tp, IntrMask);
4727 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
4728 rtl_set_rx_mode(tp->dev);
4729 /* no early-rx interrupts */
4730 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
4731 rtl_irq_enable_all(tp);
4734 static void rtl_hw_start_8169(struct rtl8169_private *tp)
4736 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4737 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4739 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
4741 tp->cp_cmd |= PCIMulRW;
4743 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4744 tp->mac_version == RTL_GIGA_MAC_VER_03) {
4745 netif_dbg(tp, drv, tp->dev,
4746 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
4747 tp->cp_cmd |= (1 << 14);
4750 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4752 rtl8169_set_magic_reg(tp, tp->mac_version);
4755 * Undocumented corner. Supposedly:
4756 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4758 RTL_W16(tp, IntrMitigate, 0x0000);
4760 RTL_W32(tp, RxMissed, 0);
4763 DECLARE_RTL_COND(rtl_csiar_cond)
4765 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
4768 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4770 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4772 RTL_W32(tp, CSIDR, value);
4773 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4774 CSIAR_BYTE_ENABLE | func << 16);
4776 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4779 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4781 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4783 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4786 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4787 RTL_R32(tp, CSIDR) : ~0;
4790 static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
4792 struct pci_dev *pdev = tp->pci_dev;
4795 /* According to Realtek the value at config space address 0x070f
4796 * controls the L0s/L1 entrance latency. We try standard ECAM access
4797 * first and if it fails fall back to CSI.
4799 if (pdev->cfg_size > 0x070f &&
4800 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4803 netdev_notice_once(tp->dev,
4804 "No native access to PCI extended config space, falling back to CSI\n");
4805 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4806 rtl_csi_write(tp, 0x070c, csi | val << 24);
4809 static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
4811 rtl_csi_access_enable(tp, 0x27);
4815 unsigned int offset;
4820 static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4826 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4827 rtl_ephy_write(tp, e->offset, w);
4832 static void rtl_disable_clock_request(struct rtl8169_private *tp)
4834 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
4835 PCI_EXP_LNKCTL_CLKREQ_EN);
4838 static void rtl_enable_clock_request(struct rtl8169_private *tp)
4840 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
4841 PCI_EXP_LNKCTL_CLKREQ_EN);
4844 static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
4848 data = RTL_R8(tp, Config3);
4853 data &= ~Rdy_to_L23;
4855 RTL_W8(tp, Config3, data);
4858 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4861 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
4862 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
4864 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4865 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4869 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
4871 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4873 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4874 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4876 if (tp->dev->mtu <= ETH_DATA_LEN) {
4877 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
4878 PCI_EXP_DEVCTL_NOSNOOP_EN);
4882 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
4884 rtl_hw_start_8168bb(tp);
4886 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4888 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
4891 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4893 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
4895 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4897 if (tp->dev->mtu <= ETH_DATA_LEN)
4898 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4900 rtl_disable_clock_request(tp);
4902 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4903 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4906 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4908 static const struct ephy_info e_info_8168cp[] = {
4909 { 0x01, 0, 0x0001 },
4910 { 0x02, 0x0800, 0x1000 },
4911 { 0x03, 0, 0x0042 },
4912 { 0x06, 0x0080, 0x0000 },
4916 rtl_set_def_aspm_entry_latency(tp);
4918 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4920 __rtl_hw_start_8168cp(tp);
4923 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4925 rtl_set_def_aspm_entry_latency(tp);
4927 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4929 if (tp->dev->mtu <= ETH_DATA_LEN)
4930 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4932 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4933 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4936 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4938 rtl_set_def_aspm_entry_latency(tp);
4940 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4943 RTL_W8(tp, DBG_REG, 0x20);
4945 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4947 if (tp->dev->mtu <= ETH_DATA_LEN)
4948 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4950 tp->cp_cmd &= CPCMD_QUIRK_MASK;
4951 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4954 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
4956 static const struct ephy_info e_info_8168c_1[] = {
4957 { 0x02, 0x0800, 0x1000 },
4958 { 0x03, 0, 0x0002 },
4959 { 0x06, 0x0080, 0x0000 }
4962 rtl_set_def_aspm_entry_latency(tp);
4964 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4966 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4968 __rtl_hw_start_8168cp(tp);
4971 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
4973 static const struct ephy_info e_info_8168c_2[] = {
4974 { 0x01, 0, 0x0001 },
4975 { 0x03, 0x0400, 0x0220 }
4978 rtl_set_def_aspm_entry_latency(tp);
4980 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4982 __rtl_hw_start_8168cp(tp);
4985 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
4987 rtl_hw_start_8168c_2(tp);
4990 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
4992 rtl_set_def_aspm_entry_latency(tp);
4994 __rtl_hw_start_8168cp(tp);
4997 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
4999 rtl_set_def_aspm_entry_latency(tp);
5001 rtl_disable_clock_request(tp);
5003 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5005 if (tp->dev->mtu <= ETH_DATA_LEN)
5006 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5008 tp->cp_cmd &= CPCMD_QUIRK_MASK;
5009 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5012 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
5014 rtl_set_def_aspm_entry_latency(tp);
5016 if (tp->dev->mtu <= ETH_DATA_LEN)
5017 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5019 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5021 rtl_disable_clock_request(tp);
5024 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
5026 static const struct ephy_info e_info_8168d_4[] = {
5027 { 0x0b, 0x0000, 0x0048 },
5028 { 0x19, 0x0020, 0x0050 },
5029 { 0x0c, 0x0100, 0x0020 }
5032 rtl_set_def_aspm_entry_latency(tp);
5034 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5036 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5038 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
5040 rtl_enable_clock_request(tp);
5043 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
5045 static const struct ephy_info e_info_8168e_1[] = {
5046 { 0x00, 0x0200, 0x0100 },
5047 { 0x00, 0x0000, 0x0004 },
5048 { 0x06, 0x0002, 0x0001 },
5049 { 0x06, 0x0000, 0x0030 },
5050 { 0x07, 0x0000, 0x2000 },
5051 { 0x00, 0x0000, 0x0020 },
5052 { 0x03, 0x5800, 0x2000 },
5053 { 0x03, 0x0000, 0x0001 },
5054 { 0x01, 0x0800, 0x1000 },
5055 { 0x07, 0x0000, 0x4000 },
5056 { 0x1e, 0x0000, 0x2000 },
5057 { 0x19, 0xffff, 0xfe6c },
5058 { 0x0a, 0x0000, 0x0040 }
5061 rtl_set_def_aspm_entry_latency(tp);
5063 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
5065 if (tp->dev->mtu <= ETH_DATA_LEN)
5066 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5068 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5070 rtl_disable_clock_request(tp);
5072 /* Reset tx FIFO pointer */
5073 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5074 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
5076 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5079 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
5081 static const struct ephy_info e_info_8168e_2[] = {
5082 { 0x09, 0x0000, 0x0080 },
5083 { 0x19, 0x0000, 0x0224 }
5086 rtl_set_def_aspm_entry_latency(tp);
5088 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
5090 if (tp->dev->mtu <= ETH_DATA_LEN)
5091 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5093 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5094 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5095 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5096 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5097 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5098 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
5099 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5100 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5102 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5104 rtl_disable_clock_request(tp);
5106 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5107 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5109 /* Adjust EEE LED frequency */
5110 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5112 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5113 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5114 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5116 rtl_hw_aspm_clkreq_enable(tp, true);
5119 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
5121 rtl_set_def_aspm_entry_latency(tp);
5123 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5125 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5126 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5127 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5128 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5129 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5130 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5131 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5132 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5133 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5134 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
5136 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5138 rtl_disable_clock_request(tp);
5140 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5141 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5142 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5143 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5144 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5147 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5149 static const struct ephy_info e_info_8168f_1[] = {
5150 { 0x06, 0x00c0, 0x0020 },
5151 { 0x08, 0x0001, 0x0002 },
5152 { 0x09, 0x0000, 0x0080 },
5153 { 0x19, 0x0000, 0x0224 }
5156 rtl_hw_start_8168f(tp);
5158 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5160 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5162 /* Adjust EEE LED frequency */
5163 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5166 static void rtl_hw_start_8411(struct rtl8169_private *tp)
5168 static const struct ephy_info e_info_8168f_1[] = {
5169 { 0x06, 0x00c0, 0x0020 },
5170 { 0x0f, 0xffff, 0x5200 },
5171 { 0x1e, 0x0000, 0x4000 },
5172 { 0x19, 0x0000, 0x0224 }
5175 rtl_hw_start_8168f(tp);
5176 rtl_pcie_state_l2l3_enable(tp, false);
5178 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5180 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
5183 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
5185 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5187 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5188 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5189 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5190 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5192 rtl_set_def_aspm_entry_latency(tp);
5194 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5196 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5197 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5198 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
5200 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5201 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5203 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5204 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5206 /* Adjust EEE LED frequency */
5207 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5209 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5210 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5212 rtl_pcie_state_l2l3_enable(tp, false);
5215 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5217 static const struct ephy_info e_info_8168g_1[] = {
5218 { 0x00, 0x0000, 0x0008 },
5219 { 0x0c, 0x37d0, 0x0820 },
5220 { 0x1e, 0x0000, 0x0001 },
5221 { 0x19, 0x8000, 0x0000 }
5224 rtl_hw_start_8168g(tp);
5226 /* disable aspm and clock request before access ephy */
5227 rtl_hw_aspm_clkreq_enable(tp, false);
5228 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
5229 rtl_hw_aspm_clkreq_enable(tp, true);
5232 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5234 static const struct ephy_info e_info_8168g_2[] = {
5235 { 0x00, 0x0000, 0x0008 },
5236 { 0x0c, 0x3df0, 0x0200 },
5237 { 0x19, 0xffff, 0xfc00 },
5238 { 0x1e, 0xffff, 0x20eb }
5241 rtl_hw_start_8168g(tp);
5243 /* disable aspm and clock request before access ephy */
5244 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5245 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
5246 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5249 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5251 static const struct ephy_info e_info_8411_2[] = {
5252 { 0x00, 0x0000, 0x0008 },
5253 { 0x0c, 0x3df0, 0x0200 },
5254 { 0x0f, 0xffff, 0x5200 },
5255 { 0x19, 0x0020, 0x0000 },
5256 { 0x1e, 0x0000, 0x2000 }
5259 rtl_hw_start_8168g(tp);
5261 /* disable aspm and clock request before access ephy */
5262 rtl_hw_aspm_clkreq_enable(tp, false);
5263 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
5264 rtl_hw_aspm_clkreq_enable(tp, true);
5267 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5271 static const struct ephy_info e_info_8168h_1[] = {
5272 { 0x1e, 0x0800, 0x0001 },
5273 { 0x1d, 0x0000, 0x0800 },
5274 { 0x05, 0xffff, 0x2089 },
5275 { 0x06, 0xffff, 0x5881 },
5276 { 0x04, 0xffff, 0x154a },
5277 { 0x01, 0xffff, 0x068b }
5280 /* disable aspm and clock request before access ephy */
5281 rtl_hw_aspm_clkreq_enable(tp, false);
5282 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5284 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5286 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5287 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5288 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5289 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5291 rtl_set_def_aspm_entry_latency(tp);
5293 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5295 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5296 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5298 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
5300 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
5302 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5304 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5305 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5307 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5308 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5310 /* Adjust EEE LED frequency */
5311 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5313 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5314 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5316 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
5318 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
5320 rtl_pcie_state_l2l3_enable(tp, false);
5322 rtl_writephy(tp, 0x1f, 0x0c42);
5323 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
5324 rtl_writephy(tp, 0x1f, 0x0000);
5325 if (rg_saw_cnt > 0) {
5328 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5329 sw_cnt_1ms_ini &= 0x0fff;
5330 data = r8168_mac_ocp_read(tp, 0xd412);
5332 data |= sw_cnt_1ms_ini;
5333 r8168_mac_ocp_write(tp, 0xd412, data);
5336 data = r8168_mac_ocp_read(tp, 0xe056);
5339 r8168_mac_ocp_write(tp, 0xe056, data);
5341 data = r8168_mac_ocp_read(tp, 0xe052);
5344 r8168_mac_ocp_write(tp, 0xe052, data);
5346 data = r8168_mac_ocp_read(tp, 0xe0d6);
5349 r8168_mac_ocp_write(tp, 0xe0d6, data);
5351 data = r8168_mac_ocp_read(tp, 0xd420);
5354 r8168_mac_ocp_write(tp, 0xd420, data);
5356 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5357 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5358 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5359 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
5361 rtl_hw_aspm_clkreq_enable(tp, true);
5364 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5366 rtl8168ep_stop_cmac(tp);
5368 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5370 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5371 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5372 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5373 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5375 rtl_set_def_aspm_entry_latency(tp);
5377 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5379 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5380 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5382 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5384 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5386 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5387 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5389 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5390 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5392 /* Adjust EEE LED frequency */
5393 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
5395 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5397 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
5399 rtl_pcie_state_l2l3_enable(tp, false);
5402 static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5404 static const struct ephy_info e_info_8168ep_1[] = {
5405 { 0x00, 0xffff, 0x10ab },
5406 { 0x06, 0xffff, 0xf030 },
5407 { 0x08, 0xffff, 0x2006 },
5408 { 0x0d, 0xffff, 0x1666 },
5409 { 0x0c, 0x3ff0, 0x0000 }
5412 /* disable aspm and clock request before access ephy */
5413 rtl_hw_aspm_clkreq_enable(tp, false);
5414 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5416 rtl_hw_start_8168ep(tp);
5418 rtl_hw_aspm_clkreq_enable(tp, true);
5421 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5423 static const struct ephy_info e_info_8168ep_2[] = {
5424 { 0x00, 0xffff, 0x10a3 },
5425 { 0x19, 0xffff, 0xfc00 },
5426 { 0x1e, 0xffff, 0x20ea }
5429 /* disable aspm and clock request before access ephy */
5430 rtl_hw_aspm_clkreq_enable(tp, false);
5431 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5433 rtl_hw_start_8168ep(tp);
5435 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5436 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5438 rtl_hw_aspm_clkreq_enable(tp, true);
5441 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5444 static const struct ephy_info e_info_8168ep_3[] = {
5445 { 0x00, 0xffff, 0x10a3 },
5446 { 0x19, 0xffff, 0x7c00 },
5447 { 0x1e, 0xffff, 0x20eb },
5448 { 0x0d, 0xffff, 0x1666 }
5451 /* disable aspm and clock request before access ephy */
5452 rtl_hw_aspm_clkreq_enable(tp, false);
5453 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5455 rtl_hw_start_8168ep(tp);
5457 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5458 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5460 data = r8168_mac_ocp_read(tp, 0xd3e2);
5463 r8168_mac_ocp_write(tp, 0xd3e2, data);
5465 data = r8168_mac_ocp_read(tp, 0xd3e4);
5467 r8168_mac_ocp_write(tp, 0xd3e4, data);
5469 data = r8168_mac_ocp_read(tp, 0xe860);
5471 r8168_mac_ocp_write(tp, 0xe860, data);
5473 rtl_hw_aspm_clkreq_enable(tp, true);
5476 static void rtl_hw_start_8168(struct rtl8169_private *tp)
5478 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5480 tp->cp_cmd &= ~INTT_MASK;
5481 tp->cp_cmd |= PktCntrDisable | INTT_1;
5482 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5484 RTL_W16(tp, IntrMitigate, 0x5151);
5486 /* Work around for RxFIFO overflow. */
5487 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
5488 tp->event_slow |= RxFIFOOver | PCSTimeout;
5489 tp->event_slow &= ~RxOverflow;
5492 switch (tp->mac_version) {
5493 case RTL_GIGA_MAC_VER_11:
5494 rtl_hw_start_8168bb(tp);
5497 case RTL_GIGA_MAC_VER_12:
5498 case RTL_GIGA_MAC_VER_17:
5499 rtl_hw_start_8168bef(tp);
5502 case RTL_GIGA_MAC_VER_18:
5503 rtl_hw_start_8168cp_1(tp);
5506 case RTL_GIGA_MAC_VER_19:
5507 rtl_hw_start_8168c_1(tp);
5510 case RTL_GIGA_MAC_VER_20:
5511 rtl_hw_start_8168c_2(tp);
5514 case RTL_GIGA_MAC_VER_21:
5515 rtl_hw_start_8168c_3(tp);
5518 case RTL_GIGA_MAC_VER_22:
5519 rtl_hw_start_8168c_4(tp);
5522 case RTL_GIGA_MAC_VER_23:
5523 rtl_hw_start_8168cp_2(tp);
5526 case RTL_GIGA_MAC_VER_24:
5527 rtl_hw_start_8168cp_3(tp);
5530 case RTL_GIGA_MAC_VER_25:
5531 case RTL_GIGA_MAC_VER_26:
5532 case RTL_GIGA_MAC_VER_27:
5533 rtl_hw_start_8168d(tp);
5536 case RTL_GIGA_MAC_VER_28:
5537 rtl_hw_start_8168d_4(tp);
5540 case RTL_GIGA_MAC_VER_31:
5541 rtl_hw_start_8168dp(tp);
5544 case RTL_GIGA_MAC_VER_32:
5545 case RTL_GIGA_MAC_VER_33:
5546 rtl_hw_start_8168e_1(tp);
5548 case RTL_GIGA_MAC_VER_34:
5549 rtl_hw_start_8168e_2(tp);
5552 case RTL_GIGA_MAC_VER_35:
5553 case RTL_GIGA_MAC_VER_36:
5554 rtl_hw_start_8168f_1(tp);
5557 case RTL_GIGA_MAC_VER_38:
5558 rtl_hw_start_8411(tp);
5561 case RTL_GIGA_MAC_VER_40:
5562 case RTL_GIGA_MAC_VER_41:
5563 rtl_hw_start_8168g_1(tp);
5565 case RTL_GIGA_MAC_VER_42:
5566 rtl_hw_start_8168g_2(tp);
5569 case RTL_GIGA_MAC_VER_44:
5570 rtl_hw_start_8411_2(tp);
5573 case RTL_GIGA_MAC_VER_45:
5574 case RTL_GIGA_MAC_VER_46:
5575 rtl_hw_start_8168h_1(tp);
5578 case RTL_GIGA_MAC_VER_49:
5579 rtl_hw_start_8168ep_1(tp);
5582 case RTL_GIGA_MAC_VER_50:
5583 rtl_hw_start_8168ep_2(tp);
5586 case RTL_GIGA_MAC_VER_51:
5587 rtl_hw_start_8168ep_3(tp);
5591 netif_err(tp, drv, tp->dev,
5592 "unknown chipset (mac_version = %d)\n",
5598 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
5600 static const struct ephy_info e_info_8102e_1[] = {
5601 { 0x01, 0, 0x6e65 },
5602 { 0x02, 0, 0x091f },
5603 { 0x03, 0, 0xc2f9 },
5604 { 0x06, 0, 0xafb5 },
5605 { 0x07, 0, 0x0e00 },
5606 { 0x19, 0, 0xec80 },
5607 { 0x01, 0, 0x2e65 },
5612 rtl_set_def_aspm_entry_latency(tp);
5614 RTL_W8(tp, DBG_REG, FIX_NAK_1);
5616 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5619 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5620 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5622 cfg1 = RTL_R8(tp, Config1);
5623 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5624 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
5626 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
5629 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
5631 rtl_set_def_aspm_entry_latency(tp);
5633 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5635 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5636 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5639 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
5641 rtl_hw_start_8102e_2(tp);
5643 rtl_ephy_write(tp, 0x03, 0xc2f9);
5646 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5648 static const struct ephy_info e_info_8105e_1[] = {
5649 { 0x07, 0, 0x4000 },
5650 { 0x19, 0, 0x0200 },
5651 { 0x19, 0, 0x0020 },
5652 { 0x1e, 0, 0x2000 },
5653 { 0x03, 0, 0x0001 },
5654 { 0x19, 0, 0x0100 },
5655 { 0x19, 0, 0x0004 },
5659 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5660 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5662 /* Disable Early Tally Counter */
5663 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
5665 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5666 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5668 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
5670 rtl_pcie_state_l2l3_enable(tp, false);
5673 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5675 rtl_hw_start_8105e_1(tp);
5676 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
5679 static void rtl_hw_start_8402(struct rtl8169_private *tp)
5681 static const struct ephy_info e_info_8402[] = {
5682 { 0x19, 0xffff, 0xff64 },
5686 rtl_set_def_aspm_entry_latency(tp);
5688 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5689 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5691 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5692 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5694 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
5696 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5698 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5699 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
5700 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5701 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5702 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5703 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5704 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
5706 rtl_pcie_state_l2l3_enable(tp, false);
5709 static void rtl_hw_start_8106(struct rtl8169_private *tp)
5711 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5712 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5714 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5715 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5716 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5718 rtl_pcie_state_l2l3_enable(tp, false);
5721 static void rtl_hw_start_8101(struct rtl8169_private *tp)
5723 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5724 tp->event_slow &= ~RxFIFOOver;
5726 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5727 tp->mac_version == RTL_GIGA_MAC_VER_16)
5728 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
5729 PCI_EXP_DEVCTL_NOSNOOP_EN);
5731 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5733 tp->cp_cmd &= CPCMD_QUIRK_MASK;
5734 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5736 switch (tp->mac_version) {
5737 case RTL_GIGA_MAC_VER_07:
5738 rtl_hw_start_8102e_1(tp);
5741 case RTL_GIGA_MAC_VER_08:
5742 rtl_hw_start_8102e_3(tp);
5745 case RTL_GIGA_MAC_VER_09:
5746 rtl_hw_start_8102e_2(tp);
5749 case RTL_GIGA_MAC_VER_29:
5750 rtl_hw_start_8105e_1(tp);
5752 case RTL_GIGA_MAC_VER_30:
5753 rtl_hw_start_8105e_2(tp);
5756 case RTL_GIGA_MAC_VER_37:
5757 rtl_hw_start_8402(tp);
5760 case RTL_GIGA_MAC_VER_39:
5761 rtl_hw_start_8106(tp);
5763 case RTL_GIGA_MAC_VER_43:
5764 rtl_hw_start_8168g_2(tp);
5766 case RTL_GIGA_MAC_VER_47:
5767 case RTL_GIGA_MAC_VER_48:
5768 rtl_hw_start_8168h_1(tp);
5772 RTL_W16(tp, IntrMitigate, 0x0000);
5775 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5777 struct rtl8169_private *tp = netdev_priv(dev);
5779 if (new_mtu > ETH_DATA_LEN)
5780 rtl_hw_jumbo_enable(tp);
5782 rtl_hw_jumbo_disable(tp);
5785 netdev_update_features(dev);
5790 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5792 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5793 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5796 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5797 void **data_buff, struct RxDesc *desc)
5799 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5800 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5804 rtl8169_make_unusable_by_asic(desc);
5807 static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
5809 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5811 /* Force memory writes to complete before releasing descriptor */
5814 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
5817 static inline void *rtl8169_align(void *data)
5819 return (void *)ALIGN((long)data, 16);
5822 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5823 struct RxDesc *desc)
5827 struct device *d = tp_to_dev(tp);
5828 int node = dev_to_node(d);
5830 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
5834 if (rtl8169_align(data) != data) {
5836 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
5841 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
5843 if (unlikely(dma_mapping_error(d, mapping))) {
5844 if (net_ratelimit())
5845 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5849 desc->addr = cpu_to_le64(mapping);
5850 rtl8169_mark_to_asic(desc);
5858 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5862 for (i = 0; i < NUM_RX_DESC; i++) {
5863 if (tp->Rx_databuff[i]) {
5864 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5865 tp->RxDescArray + i);
5870 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5872 desc->opts1 |= cpu_to_le32(RingEnd);
5875 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5879 for (i = 0; i < NUM_RX_DESC; i++) {
5882 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5884 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5887 tp->Rx_databuff[i] = data;
5890 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5894 rtl8169_rx_clear(tp);
5898 static int rtl8169_init_ring(struct rtl8169_private *tp)
5900 rtl8169_init_ring_indexes(tp);
5902 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5903 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
5905 return rtl8169_rx_fill(tp);
5908 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5909 struct TxDesc *desc)
5911 unsigned int len = tx_skb->len;
5913 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5921 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5926 for (i = 0; i < n; i++) {
5927 unsigned int entry = (start + i) % NUM_TX_DESC;
5928 struct ring_info *tx_skb = tp->tx_skb + entry;
5929 unsigned int len = tx_skb->len;
5932 struct sk_buff *skb = tx_skb->skb;
5934 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
5935 tp->TxDescArray + entry);
5937 dev_consume_skb_any(skb);
5944 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5946 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5947 tp->cur_tx = tp->dirty_tx = 0;
5950 static void rtl_reset_work(struct rtl8169_private *tp)
5952 struct net_device *dev = tp->dev;
5955 napi_disable(&tp->napi);
5956 netif_stop_queue(dev);
5957 synchronize_sched();
5959 rtl8169_hw_reset(tp);
5961 for (i = 0; i < NUM_RX_DESC; i++)
5962 rtl8169_mark_to_asic(tp->RxDescArray + i);
5964 rtl8169_tx_clear(tp);
5965 rtl8169_init_ring_indexes(tp);
5967 napi_enable(&tp->napi);
5969 netif_wake_queue(dev);
5972 static void rtl8169_tx_timeout(struct net_device *dev)
5974 struct rtl8169_private *tp = netdev_priv(dev);
5976 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5979 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5982 struct skb_shared_info *info = skb_shinfo(skb);
5983 unsigned int cur_frag, entry;
5984 struct TxDesc *uninitialized_var(txd);
5985 struct device *d = tp_to_dev(tp);
5988 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5989 const skb_frag_t *frag = info->frags + cur_frag;
5994 entry = (entry + 1) % NUM_TX_DESC;
5996 txd = tp->TxDescArray + entry;
5997 len = skb_frag_size(frag);
5998 addr = skb_frag_address(frag);
5999 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
6000 if (unlikely(dma_mapping_error(d, mapping))) {
6001 if (net_ratelimit())
6002 netif_err(tp, drv, tp->dev,
6003 "Failed to map TX fragments DMA!\n");
6007 /* Anti gcc 2.95.3 bugware (sic) */
6008 status = opts[0] | len |
6009 (RingEnd * !((entry + 1) % NUM_TX_DESC));
6011 txd->opts1 = cpu_to_le32(status);
6012 txd->opts2 = cpu_to_le32(opts[1]);
6013 txd->addr = cpu_to_le64(mapping);
6015 tp->tx_skb[entry].len = len;
6019 tp->tx_skb[entry].skb = skb;
6020 txd->opts1 |= cpu_to_le32(LastFrag);
6026 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
6030 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
6032 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
6035 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6036 struct net_device *dev);
6037 /* r8169_csum_workaround()
6038 * The hw limites the value the transport offset. When the offset is out of the
6039 * range, calculate the checksum by sw.
6041 static void r8169_csum_workaround(struct rtl8169_private *tp,
6042 struct sk_buff *skb)
6044 if (skb_shinfo(skb)->gso_size) {
6045 netdev_features_t features = tp->dev->features;
6046 struct sk_buff *segs, *nskb;
6048 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6049 segs = skb_gso_segment(skb, features);
6050 if (IS_ERR(segs) || !segs)
6057 rtl8169_start_xmit(nskb, tp->dev);
6060 dev_consume_skb_any(skb);
6061 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6062 if (skb_checksum_help(skb) < 0)
6065 rtl8169_start_xmit(skb, tp->dev);
6067 struct net_device_stats *stats;
6070 stats = &tp->dev->stats;
6071 stats->tx_dropped++;
6072 dev_kfree_skb_any(skb);
6076 /* msdn_giant_send_check()
6077 * According to the document of microsoft, the TCP Pseudo Header excludes the
6078 * packet length for IPv6 TCP large packets.
6080 static int msdn_giant_send_check(struct sk_buff *skb)
6082 const struct ipv6hdr *ipv6h;
6086 ret = skb_cow_head(skb, 0);
6090 ipv6h = ipv6_hdr(skb);
6094 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6099 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6100 struct sk_buff *skb, u32 *opts)
6102 u32 mss = skb_shinfo(skb)->gso_size;
6106 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6107 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6108 const struct iphdr *ip = ip_hdr(skb);
6110 if (ip->protocol == IPPROTO_TCP)
6111 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6112 else if (ip->protocol == IPPROTO_UDP)
6113 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6121 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6122 struct sk_buff *skb, u32 *opts)
6124 u32 transport_offset = (u32)skb_transport_offset(skb);
6125 u32 mss = skb_shinfo(skb)->gso_size;
6128 if (transport_offset > GTTCPHO_MAX) {
6129 netif_warn(tp, tx_err, tp->dev,
6130 "Invalid transport offset 0x%x for TSO\n",
6135 switch (vlan_get_protocol(skb)) {
6136 case htons(ETH_P_IP):
6137 opts[0] |= TD1_GTSENV4;
6140 case htons(ETH_P_IPV6):
6141 if (msdn_giant_send_check(skb))
6144 opts[0] |= TD1_GTSENV6;
6152 opts[0] |= transport_offset << GTTCPHO_SHIFT;
6153 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
6154 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6157 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6158 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
6160 if (transport_offset > TCPHO_MAX) {
6161 netif_warn(tp, tx_err, tp->dev,
6162 "Invalid transport offset 0x%x\n",
6167 switch (vlan_get_protocol(skb)) {
6168 case htons(ETH_P_IP):
6169 opts[1] |= TD1_IPv4_CS;
6170 ip_protocol = ip_hdr(skb)->protocol;
6173 case htons(ETH_P_IPV6):
6174 opts[1] |= TD1_IPv6_CS;
6175 ip_protocol = ipv6_hdr(skb)->nexthdr;
6179 ip_protocol = IPPROTO_RAW;
6183 if (ip_protocol == IPPROTO_TCP)
6184 opts[1] |= TD1_TCP_CS;
6185 else if (ip_protocol == IPPROTO_UDP)
6186 opts[1] |= TD1_UDP_CS;
6190 opts[1] |= transport_offset << TCPHO_SHIFT;
6192 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
6193 return !eth_skb_pad(skb);
6199 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6200 struct net_device *dev)
6202 struct rtl8169_private *tp = netdev_priv(dev);
6203 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
6204 struct TxDesc *txd = tp->TxDescArray + entry;
6205 struct device *d = tp_to_dev(tp);
6211 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
6212 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
6216 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
6219 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6222 if (!tp->tso_csum(tp, skb, opts)) {
6223 r8169_csum_workaround(tp, skb);
6224 return NETDEV_TX_OK;
6227 len = skb_headlen(skb);
6228 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
6229 if (unlikely(dma_mapping_error(d, mapping))) {
6230 if (net_ratelimit())
6231 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
6235 tp->tx_skb[entry].len = len;
6236 txd->addr = cpu_to_le64(mapping);
6238 frags = rtl8169_xmit_frags(tp, skb, opts);
6242 opts[0] |= FirstFrag;
6244 opts[0] |= FirstFrag | LastFrag;
6245 tp->tx_skb[entry].skb = skb;
6248 txd->opts2 = cpu_to_le32(opts[1]);
6250 skb_tx_timestamp(skb);
6252 /* Force memory writes to complete before releasing descriptor */
6255 /* Anti gcc 2.95.3 bugware (sic) */
6256 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
6257 txd->opts1 = cpu_to_le32(status);
6259 /* Force all memory writes to complete before notifying device */
6262 tp->cur_tx += frags + 1;
6264 RTL_W8(tp, TxPoll, NPQ);
6268 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6269 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6270 * not miss a ring update when it notices a stopped queue.
6273 netif_stop_queue(dev);
6274 /* Sync with rtl_tx:
6275 * - publish queue status and cur_tx ring index (write barrier)
6276 * - refresh dirty_tx ring index (read barrier).
6277 * May the current thread have a pessimistic view of the ring
6278 * status and forget to wake up queue, a racing rtl_tx thread
6282 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
6283 netif_wake_queue(dev);
6286 return NETDEV_TX_OK;
6289 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
6291 dev_kfree_skb_any(skb);
6292 dev->stats.tx_dropped++;
6293 return NETDEV_TX_OK;
6296 netif_stop_queue(dev);
6297 dev->stats.tx_dropped++;
6298 return NETDEV_TX_BUSY;
6301 static void rtl8169_pcierr_interrupt(struct net_device *dev)
6303 struct rtl8169_private *tp = netdev_priv(dev);
6304 struct pci_dev *pdev = tp->pci_dev;
6305 u16 pci_status, pci_cmd;
6307 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6308 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6310 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6311 pci_cmd, pci_status);
6314 * The recovery sequence below admits a very elaborated explanation:
6315 * - it seems to work;
6316 * - I did not see what else could be done;
6317 * - it makes iop3xx happy.
6319 * Feel free to adjust to your needs.
6321 if (pdev->broken_parity_status)
6322 pci_cmd &= ~PCI_COMMAND_PARITY;
6324 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6326 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
6328 pci_write_config_word(pdev, PCI_STATUS,
6329 pci_status & (PCI_STATUS_DETECTED_PARITY |
6330 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6331 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6333 /* The infamous DAC f*ckup only happens at boot time */
6334 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
6335 netif_info(tp, intr, dev, "disabling PCI DAC\n");
6336 tp->cp_cmd &= ~PCIDAC;
6337 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
6338 dev->features &= ~NETIF_F_HIGHDMA;
6341 rtl8169_hw_reset(tp);
6343 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6346 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
6348 unsigned int dirty_tx, tx_left;
6350 dirty_tx = tp->dirty_tx;
6352 tx_left = tp->cur_tx - dirty_tx;
6354 while (tx_left > 0) {
6355 unsigned int entry = dirty_tx % NUM_TX_DESC;
6356 struct ring_info *tx_skb = tp->tx_skb + entry;
6359 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6360 if (status & DescOwn)
6363 /* This barrier is needed to keep us from reading
6364 * any other fields out of the Tx descriptor until
6365 * we know the status of DescOwn
6369 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
6370 tp->TxDescArray + entry);
6371 if (status & LastFrag) {
6372 u64_stats_update_begin(&tp->tx_stats.syncp);
6373 tp->tx_stats.packets++;
6374 tp->tx_stats.bytes += tx_skb->skb->len;
6375 u64_stats_update_end(&tp->tx_stats.syncp);
6376 dev_consume_skb_any(tx_skb->skb);
6383 if (tp->dirty_tx != dirty_tx) {
6384 tp->dirty_tx = dirty_tx;
6385 /* Sync with rtl8169_start_xmit:
6386 * - publish dirty_tx ring index (write barrier)
6387 * - refresh cur_tx ring index and queue status (read barrier)
6388 * May the current thread miss the stopped queue condition,
6389 * a racing xmit thread can only have a right view of the
6393 if (netif_queue_stopped(dev) &&
6394 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
6395 netif_wake_queue(dev);
6398 * 8168 hack: TxPoll requests are lost when the Tx packets are
6399 * too close. Let's kick an extra TxPoll request when a burst
6400 * of start_xmit activity is detected (if it is not detected,
6401 * it is slow enough). -- FR
6403 if (tp->cur_tx != dirty_tx)
6404 RTL_W8(tp, TxPoll, NPQ);
6408 static inline int rtl8169_fragmented_frame(u32 status)
6410 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6413 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
6415 u32 status = opts1 & RxProtoMask;
6417 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
6418 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
6419 skb->ip_summed = CHECKSUM_UNNECESSARY;
6421 skb_checksum_none_assert(skb);
6424 static struct sk_buff *rtl8169_try_rx_copy(void *data,
6425 struct rtl8169_private *tp,
6429 struct sk_buff *skb;
6430 struct device *d = tp_to_dev(tp);
6432 data = rtl8169_align(data);
6433 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
6435 skb = napi_alloc_skb(&tp->napi, pkt_size);
6437 skb_copy_to_linear_data(skb, data, pkt_size);
6438 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6443 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
6445 unsigned int cur_rx, rx_left;
6448 cur_rx = tp->cur_rx;
6450 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
6451 unsigned int entry = cur_rx % NUM_RX_DESC;
6452 struct RxDesc *desc = tp->RxDescArray + entry;
6455 status = le32_to_cpu(desc->opts1);
6456 if (status & DescOwn)
6459 /* This barrier is needed to keep us from reading
6460 * any other fields out of the Rx descriptor until
6461 * we know the status of DescOwn
6465 if (unlikely(status & RxRES)) {
6466 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6468 dev->stats.rx_errors++;
6469 if (status & (RxRWT | RxRUNT))
6470 dev->stats.rx_length_errors++;
6472 dev->stats.rx_crc_errors++;
6473 /* RxFOVF is a reserved bit on later chip versions */
6474 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6476 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6477 dev->stats.rx_fifo_errors++;
6478 } else if (status & (RxRUNT | RxCRC) &&
6479 !(status & RxRWT) &&
6480 dev->features & NETIF_F_RXALL) {
6484 struct sk_buff *skb;
6489 addr = le64_to_cpu(desc->addr);
6490 if (likely(!(dev->features & NETIF_F_RXFCS)))
6491 pkt_size = (status & 0x00003fff) - 4;
6493 pkt_size = status & 0x00003fff;
6496 * The driver does not support incoming fragmented
6497 * frames. They are seen as a symptom of over-mtu
6500 if (unlikely(rtl8169_fragmented_frame(status))) {
6501 dev->stats.rx_dropped++;
6502 dev->stats.rx_length_errors++;
6503 goto release_descriptor;
6506 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6507 tp, pkt_size, addr);
6509 dev->stats.rx_dropped++;
6510 goto release_descriptor;
6513 rtl8169_rx_csum(skb, status);
6514 skb_put(skb, pkt_size);
6515 skb->protocol = eth_type_trans(skb, dev);
6517 rtl8169_rx_vlan_tag(desc, skb);
6519 if (skb->pkt_type == PACKET_MULTICAST)
6520 dev->stats.multicast++;
6522 napi_gro_receive(&tp->napi, skb);
6524 u64_stats_update_begin(&tp->rx_stats.syncp);
6525 tp->rx_stats.packets++;
6526 tp->rx_stats.bytes += pkt_size;
6527 u64_stats_update_end(&tp->rx_stats.syncp);
6531 rtl8169_mark_to_asic(desc);
6534 count = cur_rx - tp->cur_rx;
6535 tp->cur_rx = cur_rx;
6540 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
6542 struct rtl8169_private *tp = dev_instance;
6546 status = rtl_get_events(tp);
6547 if (status && status != 0xffff) {
6548 status &= RTL_EVENT_NAPI | tp->event_slow;
6552 rtl_irq_disable(tp);
6553 napi_schedule_irqoff(&tp->napi);
6556 return IRQ_RETVAL(handled);
6560 * Workqueue context.
6562 static void rtl_slow_event_work(struct rtl8169_private *tp)
6564 struct net_device *dev = tp->dev;
6567 status = rtl_get_events(tp) & tp->event_slow;
6568 rtl_ack_events(tp, status);
6570 if (unlikely(status & RxFIFOOver)) {
6571 switch (tp->mac_version) {
6572 /* Work around for rx fifo overflow */
6573 case RTL_GIGA_MAC_VER_11:
6574 netif_stop_queue(dev);
6575 /* XXX - Hack alert. See rtl_task(). */
6576 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6582 if (unlikely(status & SYSErr))
6583 rtl8169_pcierr_interrupt(dev);
6585 if (status & LinkChg)
6586 phy_mac_interrupt(dev->phydev);
6588 rtl_irq_enable_all(tp);
6591 static void rtl_task(struct work_struct *work)
6593 static const struct {
6595 void (*action)(struct rtl8169_private *);
6597 /* XXX - keep rtl_slow_event_work() as first element. */
6598 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6599 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
6601 struct rtl8169_private *tp =
6602 container_of(work, struct rtl8169_private, wk.work);
6603 struct net_device *dev = tp->dev;
6608 if (!netif_running(dev) ||
6609 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
6612 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6615 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
6617 rtl_work[i].action(tp);
6621 rtl_unlock_work(tp);
6624 static int rtl8169_poll(struct napi_struct *napi, int budget)
6626 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6627 struct net_device *dev = tp->dev;
6628 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6632 status = rtl_get_events(tp);
6633 rtl_ack_events(tp, status & ~tp->event_slow);
6635 if (status & RTL_EVENT_NAPI_RX)
6636 work_done = rtl_rx(dev, tp, (u32) budget);
6638 if (status & RTL_EVENT_NAPI_TX)
6641 if (status & tp->event_slow) {
6642 enable_mask &= ~tp->event_slow;
6644 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6647 if (work_done < budget) {
6648 napi_complete_done(napi, work_done);
6650 rtl_irq_enable(tp, enable_mask);
6657 static void rtl8169_rx_missed(struct net_device *dev)
6659 struct rtl8169_private *tp = netdev_priv(dev);
6661 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6664 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6665 RTL_W32(tp, RxMissed, 0);
6668 static void r8169_phylink_handler(struct net_device *ndev)
6670 struct rtl8169_private *tp = netdev_priv(ndev);
6672 if (netif_carrier_ok(ndev)) {
6673 rtl_link_chg_patch(tp);
6674 pm_request_resume(&tp->pci_dev->dev);
6676 pm_runtime_idle(&tp->pci_dev->dev);
6679 if (net_ratelimit())
6680 phy_print_status(ndev->phydev);
6683 static int r8169_phy_connect(struct rtl8169_private *tp)
6685 struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
6686 phy_interface_t phy_mode;
6689 phy_mode = tp->mii.supports_gmii ? PHY_INTERFACE_MODE_GMII :
6690 PHY_INTERFACE_MODE_MII;
6692 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6697 if (!tp->mii.supports_gmii)
6698 phy_set_max_speed(phydev, SPEED_100);
6700 /* Ensure to advertise everything, incl. pause */
6701 phydev->advertising = phydev->supported;
6703 phy_attached_info(phydev);
6708 static void rtl8169_down(struct net_device *dev)
6710 struct rtl8169_private *tp = netdev_priv(dev);
6712 phy_stop(dev->phydev);
6714 napi_disable(&tp->napi);
6715 netif_stop_queue(dev);
6717 rtl8169_hw_reset(tp);
6719 * At this point device interrupts can not be enabled in any function,
6720 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6721 * and napi is disabled (rtl8169_poll).
6723 rtl8169_rx_missed(dev);
6725 /* Give a racing hard_start_xmit a few cycles to complete. */
6726 synchronize_sched();
6728 rtl8169_tx_clear(tp);
6730 rtl8169_rx_clear(tp);
6732 rtl_pll_power_down(tp);
6735 static int rtl8169_close(struct net_device *dev)
6737 struct rtl8169_private *tp = netdev_priv(dev);
6738 struct pci_dev *pdev = tp->pci_dev;
6740 pm_runtime_get_sync(&pdev->dev);
6742 /* Update counters before going down */
6743 rtl8169_update_counters(tp);
6746 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6749 rtl_unlock_work(tp);
6751 cancel_work_sync(&tp->wk.work);
6753 phy_disconnect(dev->phydev);
6755 pci_free_irq(pdev, 0, tp);
6757 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6759 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6761 tp->TxDescArray = NULL;
6762 tp->RxDescArray = NULL;
6764 pm_runtime_put_sync(&pdev->dev);
6769 #ifdef CONFIG_NET_POLL_CONTROLLER
6770 static void rtl8169_netpoll(struct net_device *dev)
6772 struct rtl8169_private *tp = netdev_priv(dev);
6774 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
6778 static int rtl_open(struct net_device *dev)
6780 struct rtl8169_private *tp = netdev_priv(dev);
6781 struct pci_dev *pdev = tp->pci_dev;
6782 int retval = -ENOMEM;
6784 pm_runtime_get_sync(&pdev->dev);
6787 * Rx and Tx descriptors needs 256 bytes alignment.
6788 * dma_alloc_coherent provides more.
6790 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6791 &tp->TxPhyAddr, GFP_KERNEL);
6792 if (!tp->TxDescArray)
6793 goto err_pm_runtime_put;
6795 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6796 &tp->RxPhyAddr, GFP_KERNEL);
6797 if (!tp->RxDescArray)
6800 retval = rtl8169_init_ring(tp);
6804 INIT_WORK(&tp->wk.work, rtl_task);
6808 rtl_request_firmware(tp);
6810 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
6813 goto err_release_fw_2;
6815 retval = r8169_phy_connect(tp);
6821 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6823 napi_enable(&tp->napi);
6825 rtl8169_init_phy(dev, tp);
6827 rtl_pll_power_up(tp);
6831 if (!rtl8169_init_counter_offsets(tp))
6832 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6834 phy_start(dev->phydev);
6835 netif_start_queue(dev);
6837 rtl_unlock_work(tp);
6839 pm_runtime_put_sync(&pdev->dev);
6844 pci_free_irq(pdev, 0, tp);
6846 rtl_release_firmware(tp);
6847 rtl8169_rx_clear(tp);
6849 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6851 tp->RxDescArray = NULL;
6853 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6855 tp->TxDescArray = NULL;
6857 pm_runtime_put_noidle(&pdev->dev);
6862 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6864 struct rtl8169_private *tp = netdev_priv(dev);
6865 struct pci_dev *pdev = tp->pci_dev;
6866 struct rtl8169_counters *counters = tp->counters;
6869 pm_runtime_get_noresume(&pdev->dev);
6871 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
6872 rtl8169_rx_missed(dev);
6875 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
6876 stats->rx_packets = tp->rx_stats.packets;
6877 stats->rx_bytes = tp->rx_stats.bytes;
6878 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
6881 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
6882 stats->tx_packets = tp->tx_stats.packets;
6883 stats->tx_bytes = tp->tx_stats.bytes;
6884 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
6886 stats->rx_dropped = dev->stats.rx_dropped;
6887 stats->tx_dropped = dev->stats.tx_dropped;
6888 stats->rx_length_errors = dev->stats.rx_length_errors;
6889 stats->rx_errors = dev->stats.rx_errors;
6890 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6891 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6892 stats->rx_missed_errors = dev->stats.rx_missed_errors;
6893 stats->multicast = dev->stats.multicast;
6896 * Fetch additonal counter values missing in stats collected by driver
6897 * from tally counters.
6899 if (pm_runtime_active(&pdev->dev))
6900 rtl8169_update_counters(tp);
6903 * Subtract values fetched during initalization.
6904 * See rtl8169_init_counter_offsets for a description why we do that.
6906 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
6907 le64_to_cpu(tp->tc_offset.tx_errors);
6908 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
6909 le32_to_cpu(tp->tc_offset.tx_multi_collision);
6910 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
6911 le16_to_cpu(tp->tc_offset.tx_aborted);
6913 pm_runtime_put_noidle(&pdev->dev);
6916 static void rtl8169_net_suspend(struct net_device *dev)
6918 struct rtl8169_private *tp = netdev_priv(dev);
6920 if (!netif_running(dev))
6923 phy_stop(dev->phydev);
6924 netif_device_detach(dev);
6925 netif_stop_queue(dev);
6928 napi_disable(&tp->napi);
6929 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6930 rtl_unlock_work(tp);
6932 rtl_pll_power_down(tp);
6937 static int rtl8169_suspend(struct device *device)
6939 struct pci_dev *pdev = to_pci_dev(device);
6940 struct net_device *dev = pci_get_drvdata(pdev);
6942 rtl8169_net_suspend(dev);
6947 static void __rtl8169_resume(struct net_device *dev)
6949 struct rtl8169_private *tp = netdev_priv(dev);
6951 netif_device_attach(dev);
6953 rtl_pll_power_up(tp);
6954 rtl8169_init_phy(dev, tp);
6956 phy_start(tp->dev->phydev);
6959 napi_enable(&tp->napi);
6960 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6961 rtl_unlock_work(tp);
6963 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6966 static int rtl8169_resume(struct device *device)
6968 struct pci_dev *pdev = to_pci_dev(device);
6969 struct net_device *dev = pci_get_drvdata(pdev);
6971 if (netif_running(dev))
6972 __rtl8169_resume(dev);
6977 static int rtl8169_runtime_suspend(struct device *device)
6979 struct pci_dev *pdev = to_pci_dev(device);
6980 struct net_device *dev = pci_get_drvdata(pdev);
6981 struct rtl8169_private *tp = netdev_priv(dev);
6983 if (!tp->TxDescArray) {
6984 rtl_pll_power_down(tp);
6989 __rtl8169_set_wol(tp, WAKE_ANY);
6990 rtl_unlock_work(tp);
6992 rtl8169_net_suspend(dev);
6994 /* Update counters before going runtime suspend */
6995 rtl8169_rx_missed(dev);
6996 rtl8169_update_counters(tp);
7001 static int rtl8169_runtime_resume(struct device *device)
7003 struct pci_dev *pdev = to_pci_dev(device);
7004 struct net_device *dev = pci_get_drvdata(pdev);
7005 struct rtl8169_private *tp = netdev_priv(dev);
7006 rtl_rar_set(tp, dev->dev_addr);
7008 if (!tp->TxDescArray)
7012 __rtl8169_set_wol(tp, tp->saved_wolopts);
7013 rtl_unlock_work(tp);
7015 __rtl8169_resume(dev);
7020 static int rtl8169_runtime_idle(struct device *device)
7022 struct pci_dev *pdev = to_pci_dev(device);
7023 struct net_device *dev = pci_get_drvdata(pdev);
7025 if (!netif_running(dev) || !netif_carrier_ok(dev))
7026 pm_schedule_suspend(device, 10000);
7031 static const struct dev_pm_ops rtl8169_pm_ops = {
7032 .suspend = rtl8169_suspend,
7033 .resume = rtl8169_resume,
7034 .freeze = rtl8169_suspend,
7035 .thaw = rtl8169_resume,
7036 .poweroff = rtl8169_suspend,
7037 .restore = rtl8169_resume,
7038 .runtime_suspend = rtl8169_runtime_suspend,
7039 .runtime_resume = rtl8169_runtime_resume,
7040 .runtime_idle = rtl8169_runtime_idle,
7043 #define RTL8169_PM_OPS (&rtl8169_pm_ops)
7045 #else /* !CONFIG_PM */
7047 #define RTL8169_PM_OPS NULL
7049 #endif /* !CONFIG_PM */
7051 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
7053 /* WoL fails with 8168b when the receiver is disabled. */
7054 switch (tp->mac_version) {
7055 case RTL_GIGA_MAC_VER_11:
7056 case RTL_GIGA_MAC_VER_12:
7057 case RTL_GIGA_MAC_VER_17:
7058 pci_clear_master(tp->pci_dev);
7060 RTL_W8(tp, ChipCmd, CmdRxEnb);
7062 RTL_R8(tp, ChipCmd);
7069 static void rtl_shutdown(struct pci_dev *pdev)
7071 struct net_device *dev = pci_get_drvdata(pdev);
7072 struct rtl8169_private *tp = netdev_priv(dev);
7074 rtl8169_net_suspend(dev);
7076 /* Restore original MAC address */
7077 rtl_rar_set(tp, dev->perm_addr);
7079 rtl8169_hw_reset(tp);
7081 if (system_state == SYSTEM_POWER_OFF) {
7082 if (tp->saved_wolopts) {
7083 rtl_wol_suspend_quirk(tp);
7084 rtl_wol_shutdown_quirk(tp);
7087 pci_wake_from_d3(pdev, true);
7088 pci_set_power_state(pdev, PCI_D3hot);
7092 static void rtl_remove_one(struct pci_dev *pdev)
7094 struct net_device *dev = pci_get_drvdata(pdev);
7095 struct rtl8169_private *tp = netdev_priv(dev);
7097 if (r8168_check_dash(tp))
7098 rtl8168_driver_stop(tp);
7100 netif_napi_del(&tp->napi);
7102 unregister_netdev(dev);
7103 mdiobus_unregister(tp->mii_bus);
7105 rtl_release_firmware(tp);
7107 if (pci_dev_run_wake(pdev))
7108 pm_runtime_get_noresume(&pdev->dev);
7110 /* restore original MAC address */
7111 rtl_rar_set(tp, dev->perm_addr);
7114 static const struct net_device_ops rtl_netdev_ops = {
7115 .ndo_open = rtl_open,
7116 .ndo_stop = rtl8169_close,
7117 .ndo_get_stats64 = rtl8169_get_stats64,
7118 .ndo_start_xmit = rtl8169_start_xmit,
7119 .ndo_tx_timeout = rtl8169_tx_timeout,
7120 .ndo_validate_addr = eth_validate_addr,
7121 .ndo_change_mtu = rtl8169_change_mtu,
7122 .ndo_fix_features = rtl8169_fix_features,
7123 .ndo_set_features = rtl8169_set_features,
7124 .ndo_set_mac_address = rtl_set_mac_address,
7125 .ndo_do_ioctl = rtl8169_ioctl,
7126 .ndo_set_rx_mode = rtl_set_rx_mode,
7127 #ifdef CONFIG_NET_POLL_CONTROLLER
7128 .ndo_poll_controller = rtl8169_netpoll,
7133 static const struct rtl_cfg_info {
7134 void (*hw_start)(struct rtl8169_private *tp);
7136 unsigned int has_gmii:1;
7137 const struct rtl_coalesce_info *coalesce_info;
7139 } rtl_cfg_infos [] = {
7141 .hw_start = rtl_hw_start_8169,
7142 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
7144 .coalesce_info = rtl_coalesce_info_8169,
7145 .default_ver = RTL_GIGA_MAC_VER_01,
7148 .hw_start = rtl_hw_start_8168,
7149 .event_slow = SYSErr | LinkChg | RxOverflow,
7151 .coalesce_info = rtl_coalesce_info_8168_8136,
7152 .default_ver = RTL_GIGA_MAC_VER_11,
7155 .hw_start = rtl_hw_start_8101,
7156 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7158 .coalesce_info = rtl_coalesce_info_8168_8136,
7159 .default_ver = RTL_GIGA_MAC_VER_13,
7163 static int rtl_alloc_irq(struct rtl8169_private *tp)
7167 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
7168 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7169 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7170 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
7171 flags = PCI_IRQ_LEGACY;
7173 flags = PCI_IRQ_ALL_TYPES;
7176 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
7179 DECLARE_RTL_COND(rtl_link_list_ready_cond)
7181 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
7184 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7186 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
7189 static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
7191 struct rtl8169_private *tp = mii_bus->priv;
7196 return rtl_readphy(tp, phyreg);
7199 static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
7200 int phyreg, u16 val)
7202 struct rtl8169_private *tp = mii_bus->priv;
7207 rtl_writephy(tp, phyreg, val);
7212 static int r8169_mdio_register(struct rtl8169_private *tp)
7214 struct pci_dev *pdev = tp->pci_dev;
7215 struct phy_device *phydev;
7216 struct mii_bus *new_bus;
7219 new_bus = devm_mdiobus_alloc(&pdev->dev);
7223 new_bus->name = "r8169";
7225 new_bus->parent = &pdev->dev;
7226 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
7227 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
7228 PCI_DEVID(pdev->bus->number, pdev->devfn));
7230 new_bus->read = r8169_mdio_read_reg;
7231 new_bus->write = r8169_mdio_write_reg;
7233 ret = mdiobus_register(new_bus);
7237 phydev = mdiobus_get_phy(new_bus, 0);
7239 mdiobus_unregister(new_bus);
7243 /* PHY will be woken up in rtl_open() */
7244 phy_suspend(phydev);
7246 tp->mii_bus = new_bus;
7251 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
7255 tp->ocp_base = OCP_STD_PHY_BASE;
7257 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
7259 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7262 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7265 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
7267 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
7269 data = r8168_mac_ocp_read(tp, 0xe8de);
7271 r8168_mac_ocp_write(tp, 0xe8de, data);
7273 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7276 data = r8168_mac_ocp_read(tp, 0xe8de);
7278 r8168_mac_ocp_write(tp, 0xe8de, data);
7280 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7284 static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7286 rtl8168ep_stop_cmac(tp);
7287 rtl_hw_init_8168g(tp);
7290 static void rtl_hw_initialize(struct rtl8169_private *tp)
7292 switch (tp->mac_version) {
7293 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
7294 rtl_hw_init_8168g(tp);
7296 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
7297 rtl_hw_init_8168ep(tp);
7304 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7306 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
7307 struct rtl8169_private *tp;
7308 struct mii_if_info *mii;
7309 struct net_device *dev;
7310 int chipset, region, i;
7313 if (netif_msg_drv(&debug)) {
7314 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
7315 MODULENAME, RTL8169_VERSION);
7318 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7322 SET_NETDEV_DEV(dev, &pdev->dev);
7323 dev->netdev_ops = &rtl_netdev_ops;
7324 tp = netdev_priv(dev);
7327 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7331 mii->mdio_read = rtl_mdio_read;
7332 mii->mdio_write = rtl_mdio_write;
7333 mii->phy_id_mask = 0x1f;
7334 mii->reg_num_mask = 0x1f;
7335 mii->supports_gmii = cfg->has_gmii;
7337 /* enable device (incl. PCI PM wakeup and hotplug setup) */
7338 rc = pcim_enable_device(pdev);
7340 dev_err(&pdev->dev, "enable failure\n");
7344 if (pcim_set_mwi(pdev) < 0)
7345 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
7347 /* use first MMIO region */
7348 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7350 dev_err(&pdev->dev, "no MMIO resource found\n");
7354 /* check for weird/broken PCI region reporting */
7355 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7356 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
7360 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
7362 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
7366 tp->mmio_addr = pcim_iomap_table(pdev)[region];
7368 if (!pci_is_pcie(pdev))
7369 dev_info(&pdev->dev, "not PCI Express\n");
7371 /* Identify chip attached to board */
7372 rtl8169_get_mac_version(tp, cfg->default_ver);
7374 if (rtl_tbi_enabled(tp)) {
7375 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7379 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
7381 if ((sizeof(dma_addr_t) > 4) &&
7382 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7383 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
7384 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7385 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
7387 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7388 if (!pci_is_pcie(pdev))
7389 tp->cp_cmd |= PCIDAC;
7390 dev->features |= NETIF_F_HIGHDMA;
7392 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7394 dev_err(&pdev->dev, "DMA configuration failed\n");
7401 rtl_irq_disable(tp);
7403 rtl_hw_initialize(tp);
7407 rtl_ack_events(tp, 0xffff);
7409 pci_set_master(pdev);
7411 rtl_init_mdio_ops(tp);
7412 rtl_init_jumbo_ops(tp);
7414 rtl8169_print_mac_version(tp);
7416 chipset = tp->mac_version;
7418 rc = rtl_alloc_irq(tp);
7420 dev_err(&pdev->dev, "Can't allocate interrupt\n");
7424 /* override BIOS settings, use userspace tools to enable WOL */
7425 __rtl8169_set_wol(tp, 0);
7427 mutex_init(&tp->wk.mutex);
7428 u64_stats_init(&tp->rx_stats.syncp);
7429 u64_stats_init(&tp->tx_stats.syncp);
7431 /* Get MAC address */
7432 switch (tp->mac_version) {
7433 u8 mac_addr[ETH_ALEN] __aligned(4);
7434 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7435 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
7436 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
7437 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
7439 if (is_valid_ether_addr(mac_addr))
7440 rtl_rar_set(tp, mac_addr);
7445 for (i = 0; i < ETH_ALEN; i++)
7446 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
7448 dev->ethtool_ops = &rtl8169_ethtool_ops;
7449 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
7451 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
7453 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7454 * properly for all devices */
7455 dev->features |= NETIF_F_RXCSUM |
7456 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
7458 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7459 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7460 NETIF_F_HW_VLAN_CTAG_RX;
7461 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7464 tp->cp_cmd |= RxChkSum | RxVlan;
7467 * Pretend we are using VLANs; This bypasses a nasty bug where
7468 * Interrupts stop flowing on high load on 8110SCd controllers.
7470 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7471 /* Disallow toggling */
7472 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
7474 switch (rtl_chip_infos[chipset].txd_version) {
7476 tp->tso_csum = rtl8169_tso_csum_v1;
7479 tp->tso_csum = rtl8169_tso_csum_v2;
7480 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
7486 dev->hw_features |= NETIF_F_RXALL;
7487 dev->hw_features |= NETIF_F_RXFCS;
7489 /* MTU range: 60 - hw-specific max */
7490 dev->min_mtu = ETH_ZLEN;
7491 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
7493 tp->hw_start = cfg->hw_start;
7494 tp->event_slow = cfg->event_slow;
7495 tp->coalesce_info = cfg->coalesce_info;
7497 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7499 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7500 &tp->counters_phys_addr,
7505 pci_set_drvdata(pdev, dev);
7507 rc = r8169_mdio_register(tp);
7511 rc = register_netdev(dev);
7513 goto err_mdio_unregister;
7515 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7516 rtl_chip_infos[chipset].name, dev->dev_addr,
7517 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
7518 pci_irq_vector(pdev, 0));
7519 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
7520 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
7521 "tx checksumming: %s]\n",
7522 rtl_chip_infos[chipset].jumbo_max,
7523 tp->mac_version <= RTL_GIGA_MAC_VER_06 ? "ok" : "ko");
7526 if (r8168_check_dash(tp))
7527 rtl8168_driver_start(tp);
7529 if (pci_dev_run_wake(pdev))
7530 pm_runtime_put_sync(&pdev->dev);
7534 err_mdio_unregister:
7535 mdiobus_unregister(tp->mii_bus);
7539 static struct pci_driver rtl8169_pci_driver = {
7541 .id_table = rtl8169_pci_tbl,
7542 .probe = rtl_init_one,
7543 .remove = rtl_remove_one,
7544 .shutdown = rtl_shutdown,
7545 .driver.pm = RTL8169_PM_OPS,
7548 module_pci_driver(rtl8169_pci_driver);