blob: 7d89826cb6e3d3f4f2d844057885ae915a987208 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Francois Romieu07d3f512007-02-21 22:40:46 +01003 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 *
5 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
6 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
7 * Copyright (c) a lot of people too. Please respect their work.
8 *
9 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/pci.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
Hans de Goedec2f6f3e2018-09-12 11:34:55 +020017#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
19#include <linux/ethtool.h>
Heiner Kallweitf1e911d2018-07-17 22:51:26 +020020#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/if_vlan.h>
22#include <linux/crc32.h>
23#include <linux/in.h>
Heiner Kallweit098b01a2018-08-10 22:37:31 +020024#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/ip.h>
26#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000027#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000029#include <linux/pm_runtime.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +020031#include <linux/pci-aspm.h>
hayeswange9746042014-07-11 16:25:58 +080032#include <linux/ipv6.h>
33#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Heiner Kallweit8197f9d2019-06-05 08:02:31 +020035#include "r8169_firmware.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080043#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080044#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080046#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080047#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080048#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080049#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080050#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000051#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000052#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000053#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080054#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"
françois romieubca03d52011-01-03 15:07:31 +000058
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070060 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
63 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Heiner Kallweit81cd17a2019-07-24 23:34:45 +020064#define MC_FILTER_LIMIT 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Michal Schmidtaee77e42012-09-09 13:55:26 +000066#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
68
69#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020070#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000072#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
74#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
75
Heiner Kallweit145a40e2019-06-10 18:25:29 +020076#define RTL_CFG_NO_GBIT 1
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +020079#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
80#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
81#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
82#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
83#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
84#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86enum mac_version {
Heiner Kallweite9588eb2019-05-25 21:14:39 +020087 /* support for ancient RTL_GIGA_MAC_VER_01 has been removed */
Francois Romieu85bffe62011-04-27 08:22:39 +020088 RTL_GIGA_MAC_VER_02,
89 RTL_GIGA_MAC_VER_03,
90 RTL_GIGA_MAC_VER_04,
91 RTL_GIGA_MAC_VER_05,
92 RTL_GIGA_MAC_VER_06,
93 RTL_GIGA_MAC_VER_07,
94 RTL_GIGA_MAC_VER_08,
95 RTL_GIGA_MAC_VER_09,
96 RTL_GIGA_MAC_VER_10,
97 RTL_GIGA_MAC_VER_11,
98 RTL_GIGA_MAC_VER_12,
99 RTL_GIGA_MAC_VER_13,
100 RTL_GIGA_MAC_VER_14,
101 RTL_GIGA_MAC_VER_15,
102 RTL_GIGA_MAC_VER_16,
103 RTL_GIGA_MAC_VER_17,
104 RTL_GIGA_MAC_VER_18,
105 RTL_GIGA_MAC_VER_19,
106 RTL_GIGA_MAC_VER_20,
107 RTL_GIGA_MAC_VER_21,
108 RTL_GIGA_MAC_VER_22,
109 RTL_GIGA_MAC_VER_23,
110 RTL_GIGA_MAC_VER_24,
111 RTL_GIGA_MAC_VER_25,
112 RTL_GIGA_MAC_VER_26,
113 RTL_GIGA_MAC_VER_27,
114 RTL_GIGA_MAC_VER_28,
115 RTL_GIGA_MAC_VER_29,
116 RTL_GIGA_MAC_VER_30,
117 RTL_GIGA_MAC_VER_31,
118 RTL_GIGA_MAC_VER_32,
119 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800120 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800121 RTL_GIGA_MAC_VER_35,
122 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800123 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800124 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800125 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800126 RTL_GIGA_MAC_VER_40,
127 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000128 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000129 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800130 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800131 RTL_GIGA_MAC_VER_45,
132 RTL_GIGA_MAC_VER_46,
133 RTL_GIGA_MAC_VER_47,
134 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800135 RTL_GIGA_MAC_VER_49,
136 RTL_GIGA_MAC_VER_50,
137 RTL_GIGA_MAC_VER_51,
Heiner Kallweit76719ee2019-05-25 20:45:04 +0200138 RTL_GIGA_MAC_NONE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Francois Romieud58d46b2011-05-03 16:38:29 +0200141#define JUMBO_1K ETH_DATA_LEN
142#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
143#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
144#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
145#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
146
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800147static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *name;
Francois Romieu85bffe62011-04-27 08:22:39 +0200149 const char *fw_name;
150} rtl_chip_infos[] = {
151 /* PCI devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200152 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
153 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
154 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
155 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
156 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
Francois Romieu85bffe62011-04-27 08:22:39 +0200157 /* PCI-E devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200158 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
159 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
Heiner Kallweit9e0773c2019-07-07 13:59:54 +0200160 [RTL_GIGA_MAC_VER_09] = {"RTL8102e/RTL8103e" },
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200161 [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
162 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
163 [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
164 [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
165 [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
166 [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
167 [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
168 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
169 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
170 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
171 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
172 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
173 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
174 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
175 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
176 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
177 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
178 [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
179 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
180 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
181 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
182 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
183 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
184 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
185 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
186 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
187 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
188 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
189 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
190 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
191 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
192 [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
Heiner Kallweit9e0773c2019-07-07 13:59:54 +0200193 [RTL_GIGA_MAC_VER_42] = {"RTL8168gu/8111gu", FIRMWARE_8168G_3},
194 [RTL_GIGA_MAC_VER_43] = {"RTL8106eus", FIRMWARE_8106E_2},
195 [RTL_GIGA_MAC_VER_44] = {"RTL8411b", FIRMWARE_8411_2 },
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200196 [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
197 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
198 [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
199 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
200 [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
201 [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
202 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Benoit Taine9baa3c32014-08-08 15:56:03 +0200205static const struct pci_device_id rtl8169_pci_tbl[] = {
Heiner Kallweit145a40e2019-06-10 18:25:29 +0200206 { PCI_VDEVICE(REALTEK, 0x2502) },
207 { PCI_VDEVICE(REALTEK, 0x2600) },
208 { PCI_VDEVICE(REALTEK, 0x8129) },
209 { PCI_VDEVICE(REALTEK, 0x8136), RTL_CFG_NO_GBIT },
210 { PCI_VDEVICE(REALTEK, 0x8161) },
211 { PCI_VDEVICE(REALTEK, 0x8167) },
212 { PCI_VDEVICE(REALTEK, 0x8168) },
213 { PCI_VDEVICE(NCUBE, 0x8168) },
214 { PCI_VDEVICE(REALTEK, 0x8169) },
Heiner Kallweit6f0d3082018-11-19 22:35:08 +0100215 { PCI_VENDOR_ID_DLINK, 0x4300,
Heiner Kallweit145a40e2019-06-10 18:25:29 +0200216 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0 },
Heiner Kallweit9d9f3fb2019-06-15 09:58:21 +0200217 { PCI_VDEVICE(DLINK, 0x4300) },
218 { PCI_VDEVICE(DLINK, 0x4302) },
219 { PCI_VDEVICE(AT, 0xc107) },
220 { PCI_VDEVICE(USR, 0x0116) },
221 { PCI_VENDOR_ID_LINKSYS, 0x1032, PCI_ANY_ID, 0x0024 },
222 { 0x0001, 0x8168, PCI_ANY_ID, 0x2410 },
Heiner Kallweit6f0d3082018-11-19 22:35:08 +0100223 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
226MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
227
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200228static struct {
229 u32 msg_enable;
230} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Francois Romieu07d3f512007-02-21 22:40:46 +0100232enum rtl_registers {
233 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100234 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100235 MAR0 = 8, /* Multicast filter. */
236 CounterAddrLow = 0x10,
237 CounterAddrHigh = 0x14,
238 TxDescStartAddrLow = 0x20,
239 TxDescStartAddrHigh = 0x24,
240 TxHDescStartAddrLow = 0x28,
241 TxHDescStartAddrHigh = 0x2c,
242 FLASH = 0x30,
243 ERSR = 0x36,
244 ChipCmd = 0x37,
245 TxPoll = 0x38,
246 IntrMask = 0x3c,
247 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700248
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800249 TxConfig = 0x40,
250#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
251#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
252
253 RxConfig = 0x44,
254#define RX128_INT_EN (1 << 15) /* 8111c and later */
255#define RX_MULTI_EN (1 << 14) /* 8111c only */
256#define RXCFG_FIFO_SHIFT 13
257 /* No threshold before first PCI xfer */
258#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000259#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800260#define RXCFG_DMA_SHIFT 8
261 /* Unlimited maximum PCI burst. */
262#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700263
Francois Romieu07d3f512007-02-21 22:40:46 +0100264 RxMissed = 0x4c,
265 Cfg9346 = 0x50,
266 Config0 = 0x51,
267 Config1 = 0x52,
268 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200269#define PME_SIGNAL (1 << 5) /* 8168c and later */
270
Francois Romieu07d3f512007-02-21 22:40:46 +0100271 Config3 = 0x54,
272 Config4 = 0x55,
273 Config5 = 0x56,
Francois Romieu07d3f512007-02-21 22:40:46 +0100274 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100275 PHYstatus = 0x6c,
276 RxMaxSize = 0xda,
277 CPlusCmd = 0xe0,
278 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300279
280#define RTL_COALESCE_MASK 0x0f
281#define RTL_COALESCE_SHIFT 4
282#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
283#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
284
Francois Romieu07d3f512007-02-21 22:40:46 +0100285 RxDescAddrLow = 0xe4,
286 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000287 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
288
289#define NoEarlyTx 0x3f /* Max value : no early transmit. */
290
291 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
292
293#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800294#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000295
Francois Romieu07d3f512007-02-21 22:40:46 +0100296 FuncEvent = 0xf0,
297 FuncEventMask = 0xf4,
298 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800299 IBCR0 = 0xf8,
300 IBCR2 = 0xf9,
301 IBIMR0 = 0xfa,
302 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100303 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Francois Romieuf162a5d2008-06-01 22:37:49 +0200306enum rtl8168_8101_registers {
307 CSIDR = 0x64,
308 CSIAR = 0x68,
309#define CSIAR_FLAG 0x80000000
310#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200311#define CSIAR_BYTE_ENABLE 0x0000f000
312#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000313 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200314 EPHYAR = 0x80,
315#define EPHYAR_FLAG 0x80000000
316#define EPHYAR_WRITE_CMD 0x80000000
317#define EPHYAR_REG_MASK 0x1f
318#define EPHYAR_REG_SHIFT 16
319#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800320 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800321#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800322#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200323 DBG_REG = 0xd1,
324#define FIX_NAK_1 (1 << 4)
325#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800326 TWSI = 0xd2,
327 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800328#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800329#define TX_EMPTY (1 << 5)
330#define RX_EMPTY (1 << 4)
331#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800332#define EN_NDP (1 << 3)
333#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800334#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000335 EFUSEAR = 0xdc,
336#define EFUSEAR_FLAG 0x80000000
337#define EFUSEAR_WRITE_CMD 0x80000000
338#define EFUSEAR_READ_CMD 0x00000000
339#define EFUSEAR_REG_MASK 0x03ff
340#define EFUSEAR_REG_SHIFT 8
341#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800342 MISC_1 = 0xf2,
343#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200344};
345
françois romieuc0e45c12011-01-03 15:08:04 +0000346enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800347 LED_FREQ = 0x1a,
348 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000349 ERIDR = 0x70,
350 ERIAR = 0x74,
351#define ERIAR_FLAG 0x80000000
352#define ERIAR_WRITE_CMD 0x80000000
353#define ERIAR_READ_CMD 0x00000000
354#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000355#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800356#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
357#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
358#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800359#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800360#define ERIAR_MASK_SHIFT 12
361#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
362#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800363#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800364#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800365#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000366 EPHY_RXER_NUM = 0x7c,
367 OCPDR = 0xb0, /* OCP GPHY access */
368#define OCPDR_WRITE_CMD 0x80000000
369#define OCPDR_READ_CMD 0x00000000
370#define OCPDR_REG_MASK 0x7f
371#define OCPDR_GPHY_REG_SHIFT 16
372#define OCPDR_DATA_MASK 0xffff
373 OCPAR = 0xb4,
374#define OCPAR_FLAG 0x80000000
375#define OCPAR_GPHY_WRITE_CMD 0x8000f060
376#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800377 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000378 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
379 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200380#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800381#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800382#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800383#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800384#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000385};
386
Francois Romieu07d3f512007-02-21 22:40:46 +0100387enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100389 SYSErr = 0x8000,
390 PCSTimeout = 0x4000,
391 SWInt = 0x0100,
392 TxDescUnavail = 0x0080,
393 RxFIFOOver = 0x0040,
394 LinkChg = 0x0020,
395 RxOverflow = 0x0010,
396 TxErr = 0x0008,
397 TxOK = 0x0004,
398 RxErr = 0x0002,
399 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200402 RxRWT = (1 << 22),
403 RxRES = (1 << 21),
404 RxRUNT = (1 << 20),
405 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800408 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100409 CmdReset = 0x10,
410 CmdRxEnb = 0x08,
411 CmdTxEnb = 0x04,
412 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Francois Romieu275391a2007-02-23 23:50:28 +0100414 /* TXPoll register p.5 */
415 HPQ = 0x80, /* Poll cmd on the high prio queue */
416 NPQ = 0x40, /* Poll cmd on the low prio queue */
417 FSWInt = 0x01, /* Forced software interrupt */
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100420 Cfg9346_Lock = 0x00,
421 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100424 AcceptErr = 0x20,
425 AcceptRunt = 0x10,
426 AcceptBroadcast = 0x08,
427 AcceptMulticast = 0x04,
428 AcceptMyPhys = 0x02,
429 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200430#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* TxConfigBits */
433 TxInterFrameGapShift = 24,
434 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
435
Francois Romieu5d06a992006-02-23 00:47:58 +0100436 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200437 LEDS1 = (1 << 7),
438 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200439 Speed_down = (1 << 4),
440 MEMMAP = (1 << 3),
441 IOMAP = (1 << 2),
442 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100443 PMEnable = (1 << 0), /* Power Management Enable */
444
Francois Romieu6dccd162007-02-13 23:38:05 +0100445 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000446 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000447 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100448 PCI_Clock_66MHz = 0x01,
449 PCI_Clock_33MHz = 0x00,
450
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100451 /* Config3 register p.25 */
452 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
453 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200454 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800455 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200456 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100457
Francois Romieud58d46b2011-05-03 16:38:29 +0200458 /* Config4 register */
459 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
460
Francois Romieu5d06a992006-02-23 00:47:58 +0100461 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100462 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
463 MWF = (1 << 5), /* Accept Multicast wakeup frame */
464 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200465 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100466 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100467 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000468 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200471 EnableBist = (1 << 15), // 8168 8101
472 Mac_dbgo_oe = (1 << 14), // 8168 8101
473 Normal_mode = (1 << 13), // unused
474 Force_half_dup = (1 << 12), // 8168 8101
475 Force_rxflow_en = (1 << 11), // 8168 8101
476 Force_txflow_en = (1 << 10), // 8168 8101
477 Cxpl_dbg_sel = (1 << 9), // 8168 8101
478 ASF = (1 << 8), // 8168 8101
479 PktCntrDisable = (1 << 7), // 8168 8101
480 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 RxVlan = (1 << 6),
482 RxChkSum = (1 << 5),
483 PCIDAC = (1 << 4),
484 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200485#define INTT_MASK GENMASK(1, 0)
Heiner Kallweitbc732412019-06-10 18:22:33 +0200486#define CPCMD_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100489 TBI_Enable = 0x80,
490 TxFlowCtrl = 0x40,
491 RxFlowCtrl = 0x20,
492 _1000bpsF = 0x10,
493 _100bps = 0x08,
494 _10bps = 0x04,
495 LinkStatus = 0x02,
496 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200498 /* ResetCounterCommand */
499 CounterReset = 0x1,
500
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200501 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100502 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800503
504 /* magic enable v2 */
505 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506};
507
Francois Romieu2b7b4312011-04-18 22:53:24 -0700508enum rtl_desc_bit {
509 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
511 RingEnd = (1 << 30), /* End of descriptor ring */
512 FirstFrag = (1 << 29), /* First segment of a packet */
513 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700514};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Francois Romieu2b7b4312011-04-18 22:53:24 -0700516/* Generic case. */
517enum rtl_tx_desc_bit {
518 /* First doubleword. */
519 TD_LSO = (1 << 27), /* Large Send Offload */
520#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Francois Romieu2b7b4312011-04-18 22:53:24 -0700522 /* Second doubleword. */
523 TxVlanTag = (1 << 17), /* Add VLAN tag */
524};
525
526/* 8169, 8168b and 810x except 8102e. */
527enum rtl_tx_desc_bit_0 {
528 /* First doubleword. */
529#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
530 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
531 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
532 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
533};
534
535/* 8102e, 8168c and beyond. */
536enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800537 /* First doubleword. */
538 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800539 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800540#define GTTCPHO_SHIFT 18
Heiner Kallweite64e0c82019-07-26 21:49:22 +0200541#define GTTCPHO_MAX 0x7f
hayeswangbdfa4ed2014-07-11 16:25:57 +0800542
Francois Romieu2b7b4312011-04-18 22:53:24 -0700543 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800544#define TCPHO_SHIFT 18
Heiner Kallweite64e0c82019-07-26 21:49:22 +0200545#define TCPHO_MAX 0x3ff
Francois Romieu2b7b4312011-04-18 22:53:24 -0700546#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800547 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
548 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700549 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
550 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
551};
552
Francois Romieu2b7b4312011-04-18 22:53:24 -0700553enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* Rx private */
555 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500556 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558#define RxProtoUDP (PID1)
559#define RxProtoTCP (PID0)
560#define RxProtoIP (PID1 | PID0)
561#define RxProtoMask RxProtoIP
562
563 IPFail = (1 << 16), /* IP checksum failed */
564 UDPFail = (1 << 15), /* UDP/IP checksum failed */
565 TCPFail = (1 << 14), /* TCP/IP checksum failed */
566 RxVlanTag = (1 << 16), /* VLAN tag available */
567};
568
569#define RsvdMask 0x3fffc000
570
Heiner Kallweit0170d592019-07-26 21:48:32 +0200571#define RTL_GSO_MAX_SIZE_V1 32000
572#define RTL_GSO_MAX_SEGS_V1 24
573#define RTL_GSO_MAX_SIZE_V2 64000
574#define RTL_GSO_MAX_SEGS_V2 64
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200577 __le32 opts1;
578 __le32 opts2;
579 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580};
581
582struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200583 __le32 opts1;
584 __le32 opts2;
585 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586};
587
588struct ring_info {
589 struct sk_buff *skb;
590 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591};
592
Ivan Vecera355423d2009-02-06 21:49:57 -0800593struct rtl8169_counters {
594 __le64 tx_packets;
595 __le64 rx_packets;
596 __le64 tx_errors;
597 __le32 rx_errors;
598 __le16 rx_missed;
599 __le16 align_errors;
600 __le32 tx_one_collision;
601 __le32 tx_multi_collision;
602 __le64 rx_unicast;
603 __le64 rx_broadcast;
604 __le32 rx_multicast;
605 __le16 tx_aborted;
606 __le16 tx_underun;
607};
608
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200609struct rtl8169_tc_offsets {
610 bool inited;
611 __le64 tx_errors;
612 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200613 __le16 tx_aborted;
614};
615
Francois Romieuda78dbf2012-01-26 14:18:23 +0100616enum rtl_flag {
Kai-Heng Feng6ad56902018-09-11 01:51:43 +0800617 RTL_FLAG_TASK_ENABLED = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100618 RTL_FLAG_TASK_RESET_PENDING,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100619 RTL_FLAG_MAX
620};
621
Junchang Wang8027aa22012-03-04 23:30:32 +0100622struct rtl8169_stats {
623 u64 packets;
624 u64 bytes;
625 struct u64_stats_sync syncp;
626};
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628struct rtl8169_private {
629 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200630 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000631 struct net_device *dev;
Heiner Kallweit703732f2019-01-19 22:07:05 +0100632 struct phy_device *phydev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700633 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200634 u32 msg_enable;
Heiner Kallweit76719ee2019-05-25 20:45:04 +0200635 enum mac_version mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
637 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100639 struct rtl8169_stats rx_stats;
640 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
642 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
643 dma_addr_t TxPhyAddr;
644 dma_addr_t RxPhyAddr;
Heiner Kallweit32879f02019-08-07 21:38:22 +0200645 struct page *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 u16 cp_cmd;
Heiner Kallweitc1d532d2019-08-28 22:24:13 +0200648 u32 irq_mask;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +0200649 struct clk *clk;
françois romieuc0e45c12011-01-03 15:08:04 +0000650
Francois Romieu4422bcd2012-01-26 11:23:32 +0100651 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100652 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
653 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100654 struct work_struct work;
655 } wk;
656
Heiner Kallweitc8248c62019-03-21 21:23:14 +0100657 unsigned irq_enabled:1;
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +0200658 unsigned supports_gmii:1;
Heiner Kallweit62b1b3b2019-06-18 23:14:50 +0200659 unsigned aspm_manageable:1;
Corinna Vinschen42020322015-09-10 10:47:35 +0200660 dma_addr_t counters_phys_addr;
661 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200662 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000663 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000664
Heiner Kallweit254764e2019-01-22 22:23:41 +0100665 const char *fw_name;
Heiner Kallweit8197f9d2019-06-05 08:02:31 +0200666 struct rtl_fw *rtl_fw;
Hayes Wangc5583862012-07-02 17:23:22 +0800667
668 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669};
670
Heiner Kallweit1fcd1652019-04-14 10:30:24 +0200671typedef void (*rtl_generic_fct)(struct rtl8169_private *tp);
672
Ralf Baechle979b6c12005-06-13 14:30:40 -0700673MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200675module_param_named(debug, debug.msg_enable, int, 0);
676MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Heiner Kallweit11287b62019-01-07 21:49:09 +0100677MODULE_SOFTDEP("pre: realtek");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678MODULE_LICENSE("GPL");
françois romieubca03d52011-01-03 15:07:31 +0000679MODULE_FIRMWARE(FIRMWARE_8168D_1);
680MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000681MODULE_FIRMWARE(FIRMWARE_8168E_1);
682MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400683MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800684MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800685MODULE_FIRMWARE(FIRMWARE_8168F_1);
686MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800687MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800688MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800689MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800690MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000691MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000692MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000693MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800694MODULE_FIRMWARE(FIRMWARE_8168H_1);
695MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200696MODULE_FIRMWARE(FIRMWARE_8107E_1);
697MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100699static inline struct device *tp_to_dev(struct rtl8169_private *tp)
700{
701 return &tp->pci_dev->dev;
702}
703
Francois Romieuda78dbf2012-01-26 14:18:23 +0100704static void rtl_lock_work(struct rtl8169_private *tp)
705{
706 mutex_lock(&tp->wk.mutex);
707}
708
709static void rtl_unlock_work(struct rtl8169_private *tp)
710{
711 mutex_unlock(&tp->wk.mutex);
712}
713
Heiner Kallweitdf320ed2019-01-19 22:05:48 +0100714static void rtl_lock_config_regs(struct rtl8169_private *tp)
715{
716 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
717}
718
719static void rtl_unlock_config_regs(struct rtl8169_private *tp)
720{
721 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
722}
723
Heiner Kallweitcb732002018-03-20 07:45:35 +0100724static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200725{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100726 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800727 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200728}
729
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +0200730static bool rtl_is_8168evl_up(struct rtl8169_private *tp)
731{
732 return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
Heiner Kallweitc6233052019-08-28 22:24:54 +0200733 tp->mac_version != RTL_GIGA_MAC_VER_39 &&
734 tp->mac_version <= RTL_GIGA_MAC_VER_51;
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +0200735}
736
Heiner Kallweit2e779dd2019-08-15 14:14:18 +0200737static bool rtl_supports_eee(struct rtl8169_private *tp)
738{
739 return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
740 tp->mac_version != RTL_GIGA_MAC_VER_37 &&
741 tp->mac_version != RTL_GIGA_MAC_VER_39;
742}
743
Heiner Kallweitce37115e32019-08-28 22:25:32 +0200744static void rtl_read_mac_from_reg(struct rtl8169_private *tp, u8 *mac, int reg)
745{
746 int i;
747
748 for (i = 0; i < ETH_ALEN; i++)
749 mac[i] = RTL_R8(tp, reg + i);
750}
751
Francois Romieuffc46952012-07-06 14:19:23 +0200752struct rtl_cond {
753 bool (*check)(struct rtl8169_private *);
754 const char *msg;
755};
756
757static void rtl_udelay(unsigned int d)
758{
759 udelay(d);
760}
761
762static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
763 void (*delay)(unsigned int), unsigned int d, int n,
764 bool high)
765{
766 int i;
767
768 for (i = 0; i < n; i++) {
Francois Romieuffc46952012-07-06 14:19:23 +0200769 if (c->check(tp) == high)
770 return true;
Heiner Kallweitd1f50502019-05-04 15:20:38 +0200771 delay(d);
Francois Romieuffc46952012-07-06 14:19:23 +0200772 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200773 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
774 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200775 return false;
776}
777
778static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
779 const struct rtl_cond *c,
780 unsigned int d, int n)
781{
782 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
783}
784
785static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
786 const struct rtl_cond *c,
787 unsigned int d, int n)
788{
789 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
790}
791
792static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
793 const struct rtl_cond *c,
794 unsigned int d, int n)
795{
796 return rtl_loop_wait(tp, c, msleep, d, n, true);
797}
798
799static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
800 const struct rtl_cond *c,
801 unsigned int d, int n)
802{
803 return rtl_loop_wait(tp, c, msleep, d, n, false);
804}
805
806#define DECLARE_RTL_COND(name) \
807static bool name ## _check(struct rtl8169_private *); \
808 \
809static const struct rtl_cond name = { \
810 .check = name ## _check, \
811 .msg = #name \
812}; \
813 \
814static bool name ## _check(struct rtl8169_private *tp)
815
Hayes Wangc5583862012-07-02 17:23:22 +0800816static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
817{
818 if (reg & 0xffff0001) {
819 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
820 return true;
821 }
822 return false;
823}
824
825DECLARE_RTL_COND(rtl_ocp_gphy_cond)
826{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200827 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800828}
829
830static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
831{
Hayes Wangc5583862012-07-02 17:23:22 +0800832 if (rtl_ocp_reg_failure(tp, reg))
833 return;
834
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200835 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800836
837 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
838}
839
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200840static int r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
Hayes Wangc5583862012-07-02 17:23:22 +0800841{
Hayes Wangc5583862012-07-02 17:23:22 +0800842 if (rtl_ocp_reg_failure(tp, reg))
843 return 0;
844
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200845 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800846
847 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200848 (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;
Hayes Wangc5583862012-07-02 17:23:22 +0800849}
850
Hayes Wangc5583862012-07-02 17:23:22 +0800851static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
852{
Hayes Wangc5583862012-07-02 17:23:22 +0800853 if (rtl_ocp_reg_failure(tp, reg))
854 return;
855
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200856 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800857}
858
859static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
860{
Hayes Wangc5583862012-07-02 17:23:22 +0800861 if (rtl_ocp_reg_failure(tp, reg))
862 return 0;
863
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200864 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800865
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200866 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800867}
868
Heiner Kallweitef712ed2019-08-04 09:47:51 +0200869static void r8168_mac_ocp_modify(struct rtl8169_private *tp, u32 reg, u16 mask,
870 u16 set)
871{
872 u16 data = r8168_mac_ocp_read(tp, reg);
873
874 r8168_mac_ocp_write(tp, reg, (data & ~mask) | set);
875}
876
Hayes Wangc5583862012-07-02 17:23:22 +0800877#define OCP_STD_PHY_BASE 0xa400
878
879static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
880{
881 if (reg == 0x1f) {
882 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
883 return;
884 }
885
886 if (tp->ocp_base != OCP_STD_PHY_BASE)
887 reg -= 0x10;
888
889 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
890}
891
892static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
893{
894 if (tp->ocp_base != OCP_STD_PHY_BASE)
895 reg -= 0x10;
896
897 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
898}
899
hayeswangeee37862013-04-01 22:23:38 +0000900static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
901{
902 if (reg == 0x1f) {
903 tp->ocp_base = value << 4;
904 return;
905 }
906
907 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
908}
909
910static int mac_mcu_read(struct rtl8169_private *tp, int reg)
911{
912 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
913}
914
Francois Romieuffc46952012-07-06 14:19:23 +0200915DECLARE_RTL_COND(rtl_phyar_cond)
916{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200917 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200918}
919
Francois Romieu24192212012-07-06 20:19:42 +0200920static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200922 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Francois Romieuffc46952012-07-06 14:19:23 +0200924 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700925 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700926 * According to hardware specs a 20us delay is required after write
927 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700928 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700929 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Francois Romieu24192212012-07-06 20:19:42 +0200932static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Francois Romieuffc46952012-07-06 14:19:23 +0200934 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200936 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Francois Romieuffc46952012-07-06 14:19:23 +0200938 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200939 RTL_R32(tp, PHYAR) & 0xffff : -ETIMEDOUT;
Francois Romieuffc46952012-07-06 14:19:23 +0200940
Timo Teräs81a95f02010-06-09 17:31:48 -0700941 /*
942 * According to hardware specs a 20us delay is required after read
943 * complete indication, but before sending next command.
944 */
945 udelay(20);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return value;
948}
949
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800950DECLARE_RTL_COND(rtl_ocpar_cond)
951{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200952 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800953}
954
Francois Romieu24192212012-07-06 20:19:42 +0200955static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000956{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200957 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
958 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
959 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000960
Francois Romieuffc46952012-07-06 14:19:23 +0200961 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +0000962}
963
Francois Romieu24192212012-07-06 20:19:42 +0200964static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000965{
Francois Romieu24192212012-07-06 20:19:42 +0200966 r8168dp_1_mdio_access(tp, reg,
967 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000968}
969
Francois Romieu24192212012-07-06 20:19:42 +0200970static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000971{
Francois Romieu24192212012-07-06 20:19:42 +0200972 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +0000973
974 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200975 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
976 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000977
Francois Romieuffc46952012-07-06 14:19:23 +0200978 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200979 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : -ETIMEDOUT;
françois romieuc0e45c12011-01-03 15:08:04 +0000980}
981
françois romieue6de30d2011-01-03 15:08:37 +0000982#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
983
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200984static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000985{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200986 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000987}
988
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200989static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000990{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200991 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000992}
993
Francois Romieu24192212012-07-06 20:19:42 +0200994static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +0000995{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200996 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000997
Francois Romieu24192212012-07-06 20:19:42 +0200998 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +0000999
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001000 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001001}
1002
Francois Romieu24192212012-07-06 20:19:42 +02001003static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001004{
1005 int value;
1006
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001007 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001008
Francois Romieu24192212012-07-06 20:19:42 +02001009 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001010
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001011 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001012
1013 return value;
1014}
1015
Heiner Kallweitce8843a2019-05-29 21:15:06 +02001016static void rtl_writephy(struct rtl8169_private *tp, int location, int val)
Francois Romieudacf8152008-08-02 20:44:13 +02001017{
Heiner Kallweit5f950522019-05-31 19:53:28 +02001018 switch (tp->mac_version) {
1019 case RTL_GIGA_MAC_VER_27:
1020 r8168dp_1_mdio_write(tp, location, val);
1021 break;
1022 case RTL_GIGA_MAC_VER_28:
1023 case RTL_GIGA_MAC_VER_31:
1024 r8168dp_2_mdio_write(tp, location, val);
1025 break;
1026 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1027 r8168g_mdio_write(tp, location, val);
1028 break;
1029 default:
1030 r8169_mdio_write(tp, location, val);
1031 break;
1032 }
Francois Romieudacf8152008-08-02 20:44:13 +02001033}
1034
françois romieu4da19632011-01-03 15:07:55 +00001035static int rtl_readphy(struct rtl8169_private *tp, int location)
1036{
Heiner Kallweit5f950522019-05-31 19:53:28 +02001037 switch (tp->mac_version) {
1038 case RTL_GIGA_MAC_VER_27:
1039 return r8168dp_1_mdio_read(tp, location);
1040 case RTL_GIGA_MAC_VER_28:
1041 case RTL_GIGA_MAC_VER_31:
1042 return r8168dp_2_mdio_read(tp, location);
1043 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1044 return r8168g_mdio_read(tp, location);
1045 default:
1046 return r8169_mdio_read(tp, location);
1047 }
françois romieu4da19632011-01-03 15:07:55 +00001048}
1049
1050static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1051{
1052 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1053}
1054
Chun-Hao Lin76564422014-10-01 23:17:17 +08001055static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001056{
1057 int val;
1058
françois romieu4da19632011-01-03 15:07:55 +00001059 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001060 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001061}
1062
Francois Romieuffc46952012-07-06 14:19:23 +02001063DECLARE_RTL_COND(rtl_ephyar_cond)
1064{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001065 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001066}
1067
Francois Romieufdf6fc02012-07-06 22:40:38 +02001068static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001069{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001070 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001071 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1072
Francois Romieuffc46952012-07-06 14:19:23 +02001073 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1074
1075 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001076}
1077
Francois Romieufdf6fc02012-07-06 22:40:38 +02001078static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001079{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001080 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001081
Francois Romieuffc46952012-07-06 14:19:23 +02001082 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001083 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001084}
1085
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001086DECLARE_RTL_COND(rtl_eriar_cond)
1087{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001088 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001089}
1090
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001091static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1092 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001093{
Hayes Wang133ac402011-07-06 15:58:05 +08001094 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001095 RTL_W32(tp, ERIDR, val);
1096 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001097
Francois Romieuffc46952012-07-06 14:19:23 +02001098 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001099}
1100
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001101static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1102 u32 val)
1103{
1104 _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
1105}
1106
1107static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001108{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001109 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001110
Francois Romieuffc46952012-07-06 14:19:23 +02001111 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001112 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001113}
1114
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001115static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
1116{
1117 return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
1118}
1119
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001120static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001121 u32 m)
Hayes Wang133ac402011-07-06 15:58:05 +08001122{
1123 u32 val;
1124
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001125 val = rtl_eri_read(tp, addr);
1126 rtl_eri_write(tp, addr, mask, (val & ~m) | p);
Hayes Wang133ac402011-07-06 15:58:05 +08001127}
1128
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001129static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 mask,
1130 u32 p)
1131{
1132 rtl_w0w1_eri(tp, addr, mask, p, 0);
1133}
1134
1135static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 mask,
1136 u32 m)
1137{
1138 rtl_w0w1_eri(tp, addr, mask, 0, m);
1139}
1140
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001141static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1142{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001143 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001144 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001145 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001146}
1147
1148static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1149{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001150 return _rtl_eri_read(tp, reg, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001151}
1152
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001153static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1154 u32 data)
1155{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001156 RTL_W32(tp, OCPDR, data);
1157 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001158 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1159}
1160
1161static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1162 u32 data)
1163{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001164 _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1165 data, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001166}
1167
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001168static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001169{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001170 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001171
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001172 r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001173}
1174
1175#define OOB_CMD_RESET 0x00
1176#define OOB_CMD_DRIVER_START 0x05
1177#define OOB_CMD_DRIVER_STOP 0x06
1178
1179static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1180{
1181 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1182}
1183
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001184DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001185{
1186 u16 reg;
1187
1188 reg = rtl8168_get_ocp_reg(tp);
1189
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001190 return r8168dp_ocp_read(tp, 0x0f, reg) & 0x00000800;
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001191}
1192
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001193DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1194{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001195 return r8168ep_ocp_read(tp, 0x0f, 0x124) & 0x00000001;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001196}
1197
1198DECLARE_RTL_COND(rtl_ocp_tx_cond)
1199{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001200 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001201}
1202
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001203static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1204{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001205 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001206 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001207 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1208 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001209}
1210
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001211static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001212{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001213 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1214 rtl_msleep_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001215}
1216
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001217static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1218{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001219 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1220 r8168ep_ocp_write(tp, 0x01, 0x30,
1221 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001222 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1223}
1224
1225static void rtl8168_driver_start(struct rtl8169_private *tp)
1226{
1227 switch (tp->mac_version) {
1228 case RTL_GIGA_MAC_VER_27:
1229 case RTL_GIGA_MAC_VER_28:
1230 case RTL_GIGA_MAC_VER_31:
1231 rtl8168dp_driver_start(tp);
1232 break;
1233 case RTL_GIGA_MAC_VER_49:
1234 case RTL_GIGA_MAC_VER_50:
1235 case RTL_GIGA_MAC_VER_51:
1236 rtl8168ep_driver_start(tp);
1237 break;
1238 default:
1239 BUG();
1240 break;
1241 }
1242}
1243
1244static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1245{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001246 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1247 rtl_msleep_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001248}
1249
1250static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1251{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001252 rtl8168ep_stop_cmac(tp);
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001253 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1254 r8168ep_ocp_write(tp, 0x01, 0x30,
1255 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001256 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1257}
1258
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001259static void rtl8168_driver_stop(struct rtl8169_private *tp)
1260{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001261 switch (tp->mac_version) {
1262 case RTL_GIGA_MAC_VER_27:
1263 case RTL_GIGA_MAC_VER_28:
1264 case RTL_GIGA_MAC_VER_31:
1265 rtl8168dp_driver_stop(tp);
1266 break;
1267 case RTL_GIGA_MAC_VER_49:
1268 case RTL_GIGA_MAC_VER_50:
1269 case RTL_GIGA_MAC_VER_51:
1270 rtl8168ep_driver_stop(tp);
1271 break;
1272 default:
1273 BUG();
1274 break;
1275 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001276}
1277
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001278static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001279{
1280 u16 reg = rtl8168_get_ocp_reg(tp);
1281
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001282 return !!(r8168dp_ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001283}
1284
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001285static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001286{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001287 return !!(r8168ep_ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001288}
1289
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001290static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001291{
1292 switch (tp->mac_version) {
1293 case RTL_GIGA_MAC_VER_27:
1294 case RTL_GIGA_MAC_VER_28:
1295 case RTL_GIGA_MAC_VER_31:
1296 return r8168dp_check_dash(tp);
1297 case RTL_GIGA_MAC_VER_49:
1298 case RTL_GIGA_MAC_VER_50:
1299 case RTL_GIGA_MAC_VER_51:
1300 return r8168ep_check_dash(tp);
1301 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001302 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001303 }
1304}
1305
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001306static void rtl_reset_packet_filter(struct rtl8169_private *tp)
1307{
1308 rtl_eri_clear_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1309 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1310}
1311
Francois Romieuffc46952012-07-06 14:19:23 +02001312DECLARE_RTL_COND(rtl_efusear_cond)
1313{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001314 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001315}
1316
Francois Romieufdf6fc02012-07-06 22:40:38 +02001317static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001318{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001319 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001320
Francois Romieuffc46952012-07-06 14:19:23 +02001321 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001322 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001323}
1324
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02001325static u32 rtl_get_events(struct rtl8169_private *tp)
1326{
1327 return RTL_R16(tp, IntrStatus);
1328}
1329
1330static void rtl_ack_events(struct rtl8169_private *tp, u32 bits)
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001331{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001332 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001333}
1334
1335static void rtl_irq_disable(struct rtl8169_private *tp)
1336{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001337 RTL_W16(tp, IntrMask, 0);
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001338 tp->irq_enabled = 0;
Francois Romieu3e990ff2012-01-26 12:50:01 +01001339}
1340
Francois Romieuda78dbf2012-01-26 14:18:23 +01001341#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1342#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1343#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1344
Heiner Kallweitfe716f82018-11-19 22:31:32 +01001345static void rtl_irq_enable(struct rtl8169_private *tp)
Francois Romieuda78dbf2012-01-26 14:18:23 +01001346{
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001347 tp->irq_enabled = 1;
Heiner Kallweit559c3c02018-11-19 22:34:17 +01001348 RTL_W16(tp, IntrMask, tp->irq_mask);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001349}
1350
françois romieu811fd302011-12-04 20:30:45 +00001351static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001353 rtl_irq_disable(tp);
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02001354 rtl_ack_events(tp, 0xffffffff);
Heiner Kallweitde20e122018-09-25 07:58:00 +02001355 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001356 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Hayes Wang70090422011-07-06 15:58:06 +08001359static void rtl_link_chg_patch(struct rtl8169_private *tp)
1360{
Hayes Wang70090422011-07-06 15:58:06 +08001361 struct net_device *dev = tp->dev;
Heiner Kallweit703732f2019-01-19 22:07:05 +01001362 struct phy_device *phydev = tp->phydev;
Hayes Wang70090422011-07-06 15:58:06 +08001363
1364 if (!netif_running(dev))
1365 return;
1366
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001367 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1368 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001369 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001370 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1371 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001372 } else if (phydev->speed == SPEED_100) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001373 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1374 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wang70090422011-07-06 15:58:06 +08001375 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001376 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1377 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wang70090422011-07-06 15:58:06 +08001378 }
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001379 rtl_reset_packet_filter(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08001380 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1381 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001382 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001383 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1384 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wangc2218922011-09-06 16:55:18 +08001385 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001386 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1387 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wangc2218922011-09-06 16:55:18 +08001388 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001389 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001390 if (phydev->speed == SPEED_10) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001391 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
1392 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001393 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001394 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001395 }
Hayes Wang70090422011-07-06 15:58:06 +08001396 }
1397}
1398
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001399#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1400
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001401static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1402{
1403 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001404
Francois Romieuda78dbf2012-01-26 14:18:23 +01001405 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001406 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001407 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001408 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001409}
1410
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001411static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001412{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001413 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001414 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001415 u32 opt;
1416 u16 reg;
1417 u8 mask;
1418 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001419 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001420 { WAKE_UCAST, Config5, UWF },
1421 { WAKE_BCAST, Config5, BWF },
1422 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001423 { WAKE_ANY, Config5, LanWake },
1424 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001425 };
Francois Romieu851e6022012-04-17 11:10:11 +02001426 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001427
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001428 rtl_unlock_config_regs(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001429
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02001430 if (rtl_is_8168evl_up(tp)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001431 tmp = ARRAY_SIZE(cfg) - 1;
1432 if (wolopts & WAKE_MAGIC)
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001433 rtl_eri_set_bits(tp, 0x0dc, ERIAR_MASK_0100,
1434 MagicPacket_v2);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001435 else
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001436 rtl_eri_clear_bits(tp, 0x0dc, ERIAR_MASK_0100,
1437 MagicPacket_v2);
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02001438 } else {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001439 tmp = ARRAY_SIZE(cfg);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001440 }
1441
1442 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001443 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001444 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001445 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001446 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001447 }
1448
Francois Romieu851e6022012-04-17 11:10:11 +02001449 switch (tp->mac_version) {
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001450 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001451 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001452 if (wolopts)
1453 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001454 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001455 break;
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001456 case RTL_GIGA_MAC_VER_34:
1457 case RTL_GIGA_MAC_VER_37:
1458 case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001459 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001460 if (wolopts)
1461 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001462 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001463 break;
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001464 default:
1465 break;
Francois Romieu851e6022012-04-17 11:10:11 +02001466 }
1467
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001468 rtl_lock_config_regs(tp);
Heiner Kallweit3bd82642018-12-30 13:16:12 +01001469
1470 device_set_wakeup_enable(tp_to_dev(tp), wolopts);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001471}
1472
1473static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1474{
1475 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001476 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001477
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001478 if (wol->wolopts & ~WAKE_ANY)
1479 return -EINVAL;
1480
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001481 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001482
Francois Romieuda78dbf2012-01-26 14:18:23 +01001483 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001484
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001485 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001486
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001487 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001488 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001489
1490 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001491
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001492 pm_runtime_put_noidle(d);
1493
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001494 return 0;
1495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497static void rtl8169_get_drvinfo(struct net_device *dev,
1498 struct ethtool_drvinfo *info)
1499{
1500 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001501 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Rick Jones68aad782011-11-07 13:29:27 +00001503 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001504 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001505 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Heiner Kallweit254764e2019-01-22 22:23:41 +01001506 if (rtl_fw)
Rick Jones8ac72d12011-11-22 14:06:26 +00001507 strlcpy(info->fw_version, rtl_fw->version,
1508 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511static int rtl8169_get_regs_len(struct net_device *dev)
1512{
1513 return R8169_REGS_SIZE;
1514}
1515
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001516static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1517 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Francois Romieud58d46b2011-05-03 16:38:29 +02001519 struct rtl8169_private *tp = netdev_priv(dev);
1520
Francois Romieu2b7b4312011-04-18 22:53:24 -07001521 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001522 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Francois Romieud58d46b2011-05-03 16:38:29 +02001524 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001525 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001526 features &= ~NETIF_F_IP_CSUM;
1527
Michał Mirosław350fb322011-04-08 06:35:56 +00001528 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Heiner Kallweita3984572018-04-28 22:19:15 +02001531static int rtl8169_set_features(struct net_device *dev,
1532 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
1534 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001535 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Heiner Kallweita3984572018-04-28 22:19:15 +02001537 rtl_lock_work(tp);
1538
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001539 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001540 if (features & NETIF_F_RXALL)
1541 rx_config |= (AcceptErr | AcceptRunt);
1542 else
1543 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001545 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001546
hayeswang929a0312014-09-16 11:40:47 +08001547 if (features & NETIF_F_RXCSUM)
1548 tp->cp_cmd |= RxChkSum;
1549 else
1550 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001551
hayeswang929a0312014-09-16 11:40:47 +08001552 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1553 tp->cp_cmd |= RxVlan;
1554 else
1555 tp->cp_cmd &= ~RxVlan;
1556
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001557 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1558 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Francois Romieuda78dbf2012-01-26 14:18:23 +01001560 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 return 0;
1563}
1564
Kirill Smelkov810f4892012-11-10 21:11:02 +04001565static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001567 return (skb_vlan_tag_present(skb)) ?
Heiner Kallweit7424edb2019-07-02 07:59:17 +02001568 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Francois Romieu7a8fc772011-03-01 17:18:33 +01001571static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Francois Romieu7a8fc772011-03-01 17:18:33 +01001575 if (opts2 & RxVlanTag)
Heiner Kallweit7424edb2019-07-02 07:59:17 +02001576 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1580 void *p)
1581{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001582 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001583 u32 __iomem *data = tp->mmio_addr;
1584 u32 *dw = p;
1585 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Francois Romieuda78dbf2012-01-26 14:18:23 +01001587 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001588 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1589 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001590 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001593static u32 rtl8169_get_msglevel(struct net_device *dev)
1594{
1595 struct rtl8169_private *tp = netdev_priv(dev);
1596
1597 return tp->msg_enable;
1598}
1599
1600static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1601{
1602 struct rtl8169_private *tp = netdev_priv(dev);
1603
1604 tp->msg_enable = value;
1605}
1606
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001607static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1608 "tx_packets",
1609 "rx_packets",
1610 "tx_errors",
1611 "rx_errors",
1612 "rx_missed",
1613 "align_errors",
1614 "tx_single_collisions",
1615 "tx_multi_collisions",
1616 "unicast",
1617 "broadcast",
1618 "multicast",
1619 "tx_aborted",
1620 "tx_underrun",
1621};
1622
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001623static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001624{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001625 switch (sset) {
1626 case ETH_SS_STATS:
1627 return ARRAY_SIZE(rtl8169_gstrings);
1628 default:
1629 return -EOPNOTSUPP;
1630 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001631}
1632
Corinna Vinschen42020322015-09-10 10:47:35 +02001633DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001634{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001635 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001636}
1637
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001638static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001639{
Corinna Vinschen42020322015-09-10 10:47:35 +02001640 dma_addr_t paddr = tp->counters_phys_addr;
1641 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001642
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001643 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1644 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001645 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001646 RTL_W32(tp, CounterAddrLow, cmd);
1647 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001648
Francois Romieua78e9362018-01-26 01:53:26 +01001649 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001650}
1651
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001652static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001653{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001654 /*
1655 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1656 * tally counters.
1657 */
1658 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1659 return true;
1660
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001661 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001662}
1663
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001664static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001665{
Heiner Kallweit10262b02019-01-06 20:44:00 +01001666 u8 val = RTL_R8(tp, ChipCmd);
1667
Ivan Vecera355423d2009-02-06 21:49:57 -08001668 /*
1669 * Some chips are unable to dump tally counters when the receiver
Heiner Kallweit10262b02019-01-06 20:44:00 +01001670 * is disabled. If 0xff chip may be in a PCI power-save state.
Ivan Vecera355423d2009-02-06 21:49:57 -08001671 */
Heiner Kallweit10262b02019-01-06 20:44:00 +01001672 if (!(val & CmdRxEnb) || val == 0xff)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001673 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001674
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001675 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001676}
1677
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001678static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001679{
Corinna Vinschen42020322015-09-10 10:47:35 +02001680 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001681 bool ret = false;
1682
1683 /*
1684 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1685 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1686 * reset by a power cycle, while the counter values collected by the
1687 * driver are reset at every driver unload/load cycle.
1688 *
1689 * To make sure the HW values returned by @get_stats64 match the SW
1690 * values, we collect the initial values at first open(*) and use them
1691 * as offsets to normalize the values returned by @get_stats64.
1692 *
1693 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1694 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1695 * set at open time by rtl_hw_start.
1696 */
1697
1698 if (tp->tc_offset.inited)
1699 return true;
1700
1701 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001702 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001703 ret = true;
1704
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001705 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001706 ret = true;
1707
Corinna Vinschen42020322015-09-10 10:47:35 +02001708 tp->tc_offset.tx_errors = counters->tx_errors;
1709 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1710 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001711 tp->tc_offset.inited = true;
1712
1713 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001714}
1715
Ivan Vecera355423d2009-02-06 21:49:57 -08001716static void rtl8169_get_ethtool_stats(struct net_device *dev,
1717 struct ethtool_stats *stats, u64 *data)
1718{
1719 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001720 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02001721 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08001722
1723 ASSERT_RTNL();
1724
Chun-Hao Line0636232016-07-29 16:37:55 +08001725 pm_runtime_get_noresume(d);
1726
1727 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001728 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08001729
1730 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08001731
Corinna Vinschen42020322015-09-10 10:47:35 +02001732 data[0] = le64_to_cpu(counters->tx_packets);
1733 data[1] = le64_to_cpu(counters->rx_packets);
1734 data[2] = le64_to_cpu(counters->tx_errors);
1735 data[3] = le32_to_cpu(counters->rx_errors);
1736 data[4] = le16_to_cpu(counters->rx_missed);
1737 data[5] = le16_to_cpu(counters->align_errors);
1738 data[6] = le32_to_cpu(counters->tx_one_collision);
1739 data[7] = le32_to_cpu(counters->tx_multi_collision);
1740 data[8] = le64_to_cpu(counters->rx_unicast);
1741 data[9] = le64_to_cpu(counters->rx_broadcast);
1742 data[10] = le32_to_cpu(counters->rx_multicast);
1743 data[11] = le16_to_cpu(counters->tx_aborted);
1744 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08001745}
1746
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001747static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1748{
1749 switch(stringset) {
1750 case ETH_SS_STATS:
1751 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1752 break;
1753 }
1754}
1755
Francois Romieu50970832017-10-27 13:24:49 +03001756/*
1757 * Interrupt coalescing
1758 *
1759 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1760 * > 8169, 8168 and 810x line of chipsets
1761 *
1762 * 8169, 8168, and 8136(810x) serial chipsets support it.
1763 *
1764 * > 2 - the Tx timer unit at gigabit speed
1765 *
1766 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1767 * (0xe0) bit 1 and bit 0.
1768 *
1769 * For 8169
1770 * bit[1:0] \ speed 1000M 100M 10M
1771 * 0 0 320ns 2.56us 40.96us
1772 * 0 1 2.56us 20.48us 327.7us
1773 * 1 0 5.12us 40.96us 655.4us
1774 * 1 1 10.24us 81.92us 1.31ms
1775 *
1776 * For the other
1777 * bit[1:0] \ speed 1000M 100M 10M
1778 * 0 0 5us 2.56us 40.96us
1779 * 0 1 40us 20.48us 327.7us
1780 * 1 0 80us 40.96us 655.4us
1781 * 1 1 160us 81.92us 1.31ms
1782 */
1783
1784/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1785struct rtl_coalesce_scale {
1786 /* Rx / Tx */
1787 u32 nsecs[2];
1788};
1789
1790/* rx/tx scale factors for all CPlusCmd[0:1] cases */
1791struct rtl_coalesce_info {
1792 u32 speed;
1793 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1794};
1795
1796/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1797#define rxtx_x1822(r, t) { \
1798 {{(r), (t)}}, \
1799 {{(r)*8, (t)*8}}, \
1800 {{(r)*8*2, (t)*8*2}}, \
1801 {{(r)*8*2*2, (t)*8*2*2}}, \
1802}
1803static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1804 /* speed delays: rx00 tx00 */
1805 { SPEED_10, rxtx_x1822(40960, 40960) },
1806 { SPEED_100, rxtx_x1822( 2560, 2560) },
1807 { SPEED_1000, rxtx_x1822( 320, 320) },
1808 { 0 },
1809};
1810
1811static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1812 /* speed delays: rx00 tx00 */
1813 { SPEED_10, rxtx_x1822(40960, 40960) },
1814 { SPEED_100, rxtx_x1822( 2560, 2560) },
1815 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1816 { 0 },
1817};
1818#undef rxtx_x1822
1819
1820/* get rx/tx scale vector corresponding to current speed */
1821static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1822{
1823 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001824 const struct rtl_coalesce_info *ci;
Francois Romieu50970832017-10-27 13:24:49 +03001825
Heiner Kallweit20023d32019-06-11 21:09:19 +02001826 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
1827 ci = rtl_coalesce_info_8169;
1828 else
1829 ci = rtl_coalesce_info_8168_8136;
Francois Romieu50970832017-10-27 13:24:49 +03001830
Heiner Kallweit20023d32019-06-11 21:09:19 +02001831 for (; ci->speed; ci++) {
1832 if (tp->phydev->speed == ci->speed)
Francois Romieu50970832017-10-27 13:24:49 +03001833 return ci;
Francois Romieu50970832017-10-27 13:24:49 +03001834 }
1835
1836 return ERR_PTR(-ELNRNG);
1837}
1838
1839static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1840{
1841 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001842 const struct rtl_coalesce_info *ci;
1843 const struct rtl_coalesce_scale *scale;
1844 struct {
1845 u32 *max_frames;
1846 u32 *usecs;
1847 } coal_settings [] = {
1848 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1849 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1850 }, *p = coal_settings;
1851 int i;
1852 u16 w;
1853
1854 memset(ec, 0, sizeof(*ec));
1855
1856 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1857 ci = rtl_coalesce_info(dev);
1858 if (IS_ERR(ci))
1859 return PTR_ERR(ci);
1860
Heiner Kallweit0ae09742018-04-28 22:19:26 +02001861 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03001862
1863 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001864 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03001865 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1866 w >>= RTL_COALESCE_SHIFT;
1867 *p->usecs = w & RTL_COALESCE_MASK;
1868 }
1869
1870 for (i = 0; i < 2; i++) {
1871 p = coal_settings + i;
1872 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1873
1874 /*
1875 * ethtool_coalesce says it is illegal to set both usecs and
1876 * max_frames to 0.
1877 */
1878 if (!*p->usecs && !*p->max_frames)
1879 *p->max_frames = 1;
1880 }
1881
1882 return 0;
1883}
1884
1885/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1886static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1887 struct net_device *dev, u32 nsec, u16 *cp01)
1888{
1889 const struct rtl_coalesce_info *ci;
1890 u16 i;
1891
1892 ci = rtl_coalesce_info(dev);
1893 if (IS_ERR(ci))
1894 return ERR_CAST(ci);
1895
1896 for (i = 0; i < 4; i++) {
1897 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1898 ci->scalev[i].nsecs[1]);
1899 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1900 *cp01 = i;
1901 return &ci->scalev[i];
1902 }
1903 }
1904
1905 return ERR_PTR(-EINVAL);
1906}
1907
1908static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1909{
1910 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001911 const struct rtl_coalesce_scale *scale;
1912 struct {
1913 u32 frames;
1914 u32 usecs;
1915 } coal_settings [] = {
1916 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1917 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1918 }, *p = coal_settings;
1919 u16 w = 0, cp01;
1920 int i;
1921
1922 scale = rtl_coalesce_choose_scale(dev,
1923 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1924 if (IS_ERR(scale))
1925 return PTR_ERR(scale);
1926
1927 for (i = 0; i < 2; i++, p++) {
1928 u32 units;
1929
1930 /*
1931 * accept max_frames=1 we returned in rtl_get_coalesce.
1932 * accept it not only when usecs=0 because of e.g. the following scenario:
1933 *
1934 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
1935 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
1936 * - then user does `ethtool -C eth0 rx-usecs 100`
1937 *
1938 * since ethtool sends to kernel whole ethtool_coalesce
1939 * settings, if we do not handle rx_usecs=!0, rx_frames=1
1940 * we'll reject it below in `frames % 4 != 0`.
1941 */
1942 if (p->frames == 1) {
1943 p->frames = 0;
1944 }
1945
1946 units = p->usecs * 1000 / scale->nsecs[i];
1947 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
1948 return -EINVAL;
1949
1950 w <<= RTL_COALESCE_SHIFT;
1951 w |= units;
1952 w <<= RTL_COALESCE_SHIFT;
1953 w |= p->frames >> 2;
1954 }
1955
1956 rtl_lock_work(tp);
1957
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001958 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03001959
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02001960 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001961 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1962 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03001963
1964 rtl_unlock_work(tp);
1965
1966 return 0;
1967}
1968
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001969static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
1970{
1971 struct rtl8169_private *tp = netdev_priv(dev);
1972 struct device *d = tp_to_dev(tp);
1973 int ret;
1974
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001975 if (!rtl_supports_eee(tp))
1976 return -EOPNOTSUPP;
1977
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001978 pm_runtime_get_noresume(d);
1979
1980 if (!pm_runtime_active(d)) {
1981 ret = -EOPNOTSUPP;
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001982 } else {
1983 ret = phy_ethtool_get_eee(tp->phydev, data);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001984 }
1985
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001986 pm_runtime_put_noidle(d);
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001987
1988 return ret;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001989}
1990
1991static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
1992{
1993 struct rtl8169_private *tp = netdev_priv(dev);
1994 struct device *d = tp_to_dev(tp);
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001995 int ret;
1996
1997 if (!rtl_supports_eee(tp))
1998 return -EOPNOTSUPP;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001999
2000 pm_runtime_get_noresume(d);
2001
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002002 if (!pm_runtime_active(d)) {
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002003 ret = -EOPNOTSUPP;
2004 goto out;
2005 }
2006
2007 if (dev->phydev->autoneg == AUTONEG_DISABLE ||
2008 dev->phydev->duplex != DUPLEX_FULL) {
2009 ret = -EPROTONOSUPPORT;
2010 goto out;
2011 }
2012
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002013 ret = phy_ethtool_set_eee(tp->phydev, data);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002014out:
2015 pm_runtime_put_noidle(d);
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002016 return ret;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002017}
2018
Jeff Garzik7282d492006-09-13 14:30:00 -04002019static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 .get_drvinfo = rtl8169_get_drvinfo,
2021 .get_regs_len = rtl8169_get_regs_len,
2022 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002023 .get_coalesce = rtl_get_coalesce,
2024 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002025 .get_msglevel = rtl8169_get_msglevel,
2026 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002028 .get_wol = rtl8169_get_wol,
2029 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002030 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002031 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002032 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002033 .get_ts_info = ethtool_op_get_ts_info,
Heiner Kallweitdd849572018-07-17 22:51:48 +02002034 .nway_reset = phy_ethtool_nway_reset,
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002035 .get_eee = rtl8169_get_eee,
2036 .set_eee = rtl8169_set_eee,
Heiner Kallweit45772432018-07-17 22:51:44 +02002037 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2038 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039};
2040
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002041static void rtl_enable_eee(struct rtl8169_private *tp)
2042{
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002043 struct phy_device *phydev = tp->phydev;
2044 int supported = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002045
2046 if (supported > 0)
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002047 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, supported);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002048}
2049
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002050static void rtl8169_get_mac_version(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Francois Romieu0e485152007-02-20 00:00:26 +01002052 /*
2053 * The driver currently handles the 8168Bf and the 8168Be identically
2054 * but they can be identified more specifically through the test below
2055 * if needed:
2056 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002057 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002058 *
2059 * Same thing for the 8101Eb and the 8101Ec:
2060 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002061 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002062 */
Francois Romieu37441002011-06-17 22:58:54 +02002063 static const struct rtl_mac_info {
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002064 u16 mask;
2065 u16 val;
2066 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002068 /* 8168EP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002069 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
2070 { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
2071 { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002072
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002073 /* 8168H family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002074 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
2075 { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002076
Hayes Wangc5583862012-07-02 17:23:22 +08002077 /* 8168G family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002078 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
2079 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
2080 { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
2081 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
Hayes Wangc5583862012-07-02 17:23:22 +08002082
Hayes Wangc2218922011-09-06 16:55:18 +08002083 /* 8168F family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002084 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
2085 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
2086 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
Hayes Wangc2218922011-09-06 16:55:18 +08002087
hayeswang01dc7fe2011-03-21 01:50:28 +00002088 /* 8168E family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002089 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
2090 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
2091 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002092
Francois Romieu5b538df2008-07-20 16:22:45 +02002093 /* 8168D family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002094 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
2095 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002096
françois romieue6de30d2011-01-03 15:08:37 +00002097 /* 8168DP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002098 { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
2099 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
2100 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002101
Francois Romieuef808d52008-06-29 13:10:54 +02002102 /* 8168C family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002103 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
2104 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
2105 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
2106 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
2107 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
2108 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
2109 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002110
2111 /* 8168B family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002112 { 0x7cf, 0x380, RTL_GIGA_MAC_VER_12 },
2113 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
2114 { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002115
2116 /* 8101 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002117 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
2118 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
2119 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
2120 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
2121 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
2122 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
2123 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
2124 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
2125 { 0x7cf, 0x340, RTL_GIGA_MAC_VER_13 },
2126 { 0x7cf, 0x343, RTL_GIGA_MAC_VER_10 },
2127 { 0x7cf, 0x342, RTL_GIGA_MAC_VER_16 },
2128 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
2129 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
2130 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_16 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002131 /* FIXME: where did these entries come from ? -- FR */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002132 { 0xfc8, 0x388, RTL_GIGA_MAC_VER_15 },
2133 { 0xfc8, 0x308, RTL_GIGA_MAC_VER_14 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002134
2135 /* 8110 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002136 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
2137 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
2138 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
2139 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
2140 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002141
Jean Delvaref21b75e2009-05-26 20:54:48 -07002142 /* Catch-all */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002143 { 0x000, 0x000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002144 };
2145 const struct rtl_mac_info *p = mac_info;
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002146 u16 reg = RTL_R32(tp, TxConfig) >> 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002148 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 p++;
2150 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002151
2152 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002153 dev_err(tp_to_dev(tp), "unknown chip XID %03x\n", reg & 0xfcf);
Heiner Kallweit45f19962018-11-22 22:00:10 +01002154 } else if (!tp->supports_gmii) {
2155 if (tp->mac_version == RTL_GIGA_MAC_VER_42)
2156 tp->mac_version = RTL_GIGA_MAC_VER_43;
2157 else if (tp->mac_version == RTL_GIGA_MAC_VER_45)
2158 tp->mac_version = RTL_GIGA_MAC_VER_47;
2159 else if (tp->mac_version == RTL_GIGA_MAC_VER_46)
2160 tp->mac_version = RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Francois Romieu867763c2007-08-17 18:21:58 +02002164struct phy_reg {
2165 u16 reg;
2166 u16 val;
2167};
2168
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002169static void __rtl_writephy_batch(struct rtl8169_private *tp,
2170 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002171{
2172 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002173 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002174 regs++;
2175 }
2176}
2177
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002178#define rtl_writephy_batch(tp, a) __rtl_writephy_batch(tp, a, ARRAY_SIZE(a))
2179
françois romieuf1e02ed2011-01-13 13:07:53 +00002180static void rtl_release_firmware(struct rtl8169_private *tp)
2181{
Heiner Kallweit254764e2019-01-22 22:23:41 +01002182 if (tp->rtl_fw) {
Heiner Kallweit47ad5932019-06-03 21:26:31 +02002183 rtl_fw_release_firmware(tp->rtl_fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02002184 kfree(tp->rtl_fw);
Heiner Kallweit254764e2019-01-22 22:23:41 +01002185 tp->rtl_fw = NULL;
Francois Romieub6ffd972011-06-17 17:00:05 +02002186 }
françois romieuf1e02ed2011-01-13 13:07:53 +00002187}
2188
François Romieu953a12c2011-04-24 17:38:48 +02002189static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002190{
Heiner Kallweitce8843a2019-05-29 21:15:06 +02002191 /* TODO: release firmware if rtl_fw_write_firmware signals failure. */
Heiner Kallweit254764e2019-01-22 22:23:41 +01002192 if (tp->rtl_fw)
Heiner Kallweitce8843a2019-05-29 21:15:06 +02002193 rtl_fw_write_firmware(tp, tp->rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002194}
2195
2196static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2197{
2198 if (rtl_readphy(tp, reg) != val)
2199 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2200 else
2201 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002202}
2203
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002204static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
2205{
Heiner Kallweitf4528252019-05-04 17:13:09 +02002206 /* Adjust EEE LED frequency */
2207 if (tp->mac_version != RTL_GIGA_MAC_VER_38)
2208 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
2209
Heiner Kallweite719b3e2019-04-28 11:11:47 +02002210 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_1111, 0x0003);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002211}
2212
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002213static void rtl8168f_config_eee_phy(struct rtl8169_private *tp)
2214{
2215 struct phy_device *phydev = tp->phydev;
2216
2217 phy_write(phydev, 0x1f, 0x0007);
2218 phy_write(phydev, 0x1e, 0x0020);
2219 phy_set_bits(phydev, 0x15, BIT(8));
2220
2221 phy_write(phydev, 0x1f, 0x0005);
2222 phy_write(phydev, 0x05, 0x8b85);
2223 phy_set_bits(phydev, 0x06, BIT(13));
2224
2225 phy_write(phydev, 0x1f, 0x0000);
2226}
2227
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002228static void rtl8168g_config_eee_phy(struct rtl8169_private *tp)
2229{
Heiner Kallweita2928d22019-06-02 10:53:49 +02002230 phy_modify_paged(tp->phydev, 0x0a43, 0x11, 0, BIT(4));
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002231}
2232
Heiner Kallweitb6cef262019-08-15 14:21:30 +02002233static void rtl8168h_config_eee_phy(struct rtl8169_private *tp)
2234{
2235 struct phy_device *phydev = tp->phydev;
2236
2237 rtl8168g_config_eee_phy(tp);
2238
2239 phy_modify_paged(phydev, 0xa4a, 0x11, 0x0000, 0x0200);
2240 phy_modify_paged(phydev, 0xa42, 0x14, 0x0000, 0x0080);
2241}
2242
françois romieu4da19632011-01-03 15:07:55 +00002243static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002245 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002246 { 0x1f, 0x0001 },
2247 { 0x06, 0x006e },
2248 { 0x08, 0x0708 },
2249 { 0x15, 0x4000 },
2250 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
françois romieu0b9b5712009-08-10 19:44:56 +00002252 { 0x1f, 0x0001 },
2253 { 0x03, 0x00a1 },
2254 { 0x02, 0x0008 },
2255 { 0x01, 0x0120 },
2256 { 0x00, 0x1000 },
2257 { 0x04, 0x0800 },
2258 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
françois romieu0b9b5712009-08-10 19:44:56 +00002260 { 0x03, 0xff41 },
2261 { 0x02, 0xdf60 },
2262 { 0x01, 0x0140 },
2263 { 0x00, 0x0077 },
2264 { 0x04, 0x7800 },
2265 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
françois romieu0b9b5712009-08-10 19:44:56 +00002267 { 0x03, 0x802f },
2268 { 0x02, 0x4f02 },
2269 { 0x01, 0x0409 },
2270 { 0x00, 0xf0f9 },
2271 { 0x04, 0x9800 },
2272 { 0x04, 0x9000 },
2273
2274 { 0x03, 0xdf01 },
2275 { 0x02, 0xdf20 },
2276 { 0x01, 0xff95 },
2277 { 0x00, 0xba00 },
2278 { 0x04, 0xa800 },
2279 { 0x04, 0xa000 },
2280
2281 { 0x03, 0xff41 },
2282 { 0x02, 0xdf20 },
2283 { 0x01, 0x0140 },
2284 { 0x00, 0x00bb },
2285 { 0x04, 0xb800 },
2286 { 0x04, 0xb000 },
2287
2288 { 0x03, 0xdf41 },
2289 { 0x02, 0xdc60 },
2290 { 0x01, 0x6340 },
2291 { 0x00, 0x007d },
2292 { 0x04, 0xd800 },
2293 { 0x04, 0xd000 },
2294
2295 { 0x03, 0xdf01 },
2296 { 0x02, 0xdf20 },
2297 { 0x01, 0x100a },
2298 { 0x00, 0xa0ff },
2299 { 0x04, 0xf800 },
2300 { 0x04, 0xf000 },
2301
2302 { 0x1f, 0x0000 },
2303 { 0x0b, 0x0000 },
2304 { 0x00, 0x9200 }
2305 };
2306
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002307 rtl_writephy_batch(tp, phy_reg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308}
2309
françois romieu4da19632011-01-03 15:07:55 +00002310static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002311{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002312 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002313 { 0x1f, 0x0002 },
2314 { 0x01, 0x90d0 },
2315 { 0x1f, 0x0000 }
2316 };
2317
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002318 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002319}
2320
françois romieu4da19632011-01-03 15:07:55 +00002321static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002322{
2323 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002324
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002325 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2326 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002327 return;
2328
françois romieu4da19632011-01-03 15:07:55 +00002329 rtl_writephy(tp, 0x1f, 0x0001);
2330 rtl_writephy(tp, 0x10, 0xf01b);
2331 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002332}
2333
françois romieu4da19632011-01-03 15:07:55 +00002334static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002335{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002336 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002337 { 0x1f, 0x0001 },
2338 { 0x04, 0x0000 },
2339 { 0x03, 0x00a1 },
2340 { 0x02, 0x0008 },
2341 { 0x01, 0x0120 },
2342 { 0x00, 0x1000 },
2343 { 0x04, 0x0800 },
2344 { 0x04, 0x9000 },
2345 { 0x03, 0x802f },
2346 { 0x02, 0x4f02 },
2347 { 0x01, 0x0409 },
2348 { 0x00, 0xf099 },
2349 { 0x04, 0x9800 },
2350 { 0x04, 0xa000 },
2351 { 0x03, 0xdf01 },
2352 { 0x02, 0xdf20 },
2353 { 0x01, 0xff95 },
2354 { 0x00, 0xba00 },
2355 { 0x04, 0xa800 },
2356 { 0x04, 0xf000 },
2357 { 0x03, 0xdf01 },
2358 { 0x02, 0xdf20 },
2359 { 0x01, 0x101a },
2360 { 0x00, 0xa0ff },
2361 { 0x04, 0xf800 },
2362 { 0x04, 0x0000 },
2363 { 0x1f, 0x0000 },
2364
2365 { 0x1f, 0x0001 },
2366 { 0x10, 0xf41b },
2367 { 0x14, 0xfb54 },
2368 { 0x18, 0xf5c7 },
2369 { 0x1f, 0x0000 },
2370
2371 { 0x1f, 0x0001 },
2372 { 0x17, 0x0cc0 },
2373 { 0x1f, 0x0000 }
2374 };
2375
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002376 rtl_writephy_batch(tp, phy_reg_init);
françois romieu2e9558562009-08-10 19:44:19 +00002377
françois romieu4da19632011-01-03 15:07:55 +00002378 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002379}
2380
françois romieu4da19632011-01-03 15:07:55 +00002381static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002382{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002383 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002384 { 0x1f, 0x0001 },
2385 { 0x04, 0x0000 },
2386 { 0x03, 0x00a1 },
2387 { 0x02, 0x0008 },
2388 { 0x01, 0x0120 },
2389 { 0x00, 0x1000 },
2390 { 0x04, 0x0800 },
2391 { 0x04, 0x9000 },
2392 { 0x03, 0x802f },
2393 { 0x02, 0x4f02 },
2394 { 0x01, 0x0409 },
2395 { 0x00, 0xf099 },
2396 { 0x04, 0x9800 },
2397 { 0x04, 0xa000 },
2398 { 0x03, 0xdf01 },
2399 { 0x02, 0xdf20 },
2400 { 0x01, 0xff95 },
2401 { 0x00, 0xba00 },
2402 { 0x04, 0xa800 },
2403 { 0x04, 0xf000 },
2404 { 0x03, 0xdf01 },
2405 { 0x02, 0xdf20 },
2406 { 0x01, 0x101a },
2407 { 0x00, 0xa0ff },
2408 { 0x04, 0xf800 },
2409 { 0x04, 0x0000 },
2410 { 0x1f, 0x0000 },
2411
2412 { 0x1f, 0x0001 },
2413 { 0x0b, 0x8480 },
2414 { 0x1f, 0x0000 },
2415
2416 { 0x1f, 0x0001 },
2417 { 0x18, 0x67c7 },
2418 { 0x04, 0x2000 },
2419 { 0x03, 0x002f },
2420 { 0x02, 0x4360 },
2421 { 0x01, 0x0109 },
2422 { 0x00, 0x3022 },
2423 { 0x04, 0x2800 },
2424 { 0x1f, 0x0000 },
2425
2426 { 0x1f, 0x0001 },
2427 { 0x17, 0x0cc0 },
2428 { 0x1f, 0x0000 }
2429 };
2430
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002431 rtl_writephy_batch(tp, phy_reg_init);
françois romieu8c7006a2009-08-10 19:43:29 +00002432}
2433
françois romieu4da19632011-01-03 15:07:55 +00002434static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002435{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002436 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002437 { 0x10, 0xf41b },
2438 { 0x1f, 0x0000 }
2439 };
2440
françois romieu4da19632011-01-03 15:07:55 +00002441 rtl_writephy(tp, 0x1f, 0x0001);
2442 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002443
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002444 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002445}
2446
françois romieu4da19632011-01-03 15:07:55 +00002447static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002448{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002449 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002450 { 0x1f, 0x0001 },
2451 { 0x10, 0xf41b },
2452 { 0x1f, 0x0000 }
2453 };
2454
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002455 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002456}
2457
françois romieu4da19632011-01-03 15:07:55 +00002458static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002459{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002460 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002461 { 0x1f, 0x0000 },
2462 { 0x1d, 0x0f00 },
2463 { 0x1f, 0x0002 },
2464 { 0x0c, 0x1ec8 },
2465 { 0x1f, 0x0000 }
2466 };
2467
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002468 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu867763c2007-08-17 18:21:58 +02002469}
2470
françois romieu4da19632011-01-03 15:07:55 +00002471static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002472{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002473 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002474 { 0x1f, 0x0001 },
2475 { 0x1d, 0x3d98 },
2476 { 0x1f, 0x0000 }
2477 };
2478
françois romieu4da19632011-01-03 15:07:55 +00002479 rtl_writephy(tp, 0x1f, 0x0000);
2480 rtl_patchphy(tp, 0x14, 1 << 5);
2481 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002482
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002483 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuef3386f2008-06-29 12:24:30 +02002484}
2485
françois romieu4da19632011-01-03 15:07:55 +00002486static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002487{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002488 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002489 { 0x1f, 0x0001 },
2490 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002491 { 0x1f, 0x0002 },
2492 { 0x00, 0x88d4 },
2493 { 0x01, 0x82b1 },
2494 { 0x03, 0x7002 },
2495 { 0x08, 0x9e30 },
2496 { 0x09, 0x01f0 },
2497 { 0x0a, 0x5500 },
2498 { 0x0c, 0x00c8 },
2499 { 0x1f, 0x0003 },
2500 { 0x12, 0xc096 },
2501 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002502 { 0x1f, 0x0000 },
2503 { 0x1f, 0x0000 },
2504 { 0x09, 0x2000 },
2505 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002506 };
2507
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002508 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002509
françois romieu4da19632011-01-03 15:07:55 +00002510 rtl_patchphy(tp, 0x14, 1 << 5);
2511 rtl_patchphy(tp, 0x0d, 1 << 5);
2512 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002513}
2514
françois romieu4da19632011-01-03 15:07:55 +00002515static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002516{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002517 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002518 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002519 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002520 { 0x03, 0x802f },
2521 { 0x02, 0x4f02 },
2522 { 0x01, 0x0409 },
2523 { 0x00, 0xf099 },
2524 { 0x04, 0x9800 },
2525 { 0x04, 0x9000 },
2526 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002527 { 0x1f, 0x0002 },
2528 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002529 { 0x06, 0x0761 },
2530 { 0x1f, 0x0003 },
2531 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002532 { 0x1f, 0x0000 }
2533 };
2534
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002535 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002536
françois romieu4da19632011-01-03 15:07:55 +00002537 rtl_patchphy(tp, 0x16, 1 << 0);
2538 rtl_patchphy(tp, 0x14, 1 << 5);
2539 rtl_patchphy(tp, 0x0d, 1 << 5);
2540 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002541}
2542
françois romieu4da19632011-01-03 15:07:55 +00002543static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002544{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002545 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002546 { 0x1f, 0x0001 },
2547 { 0x12, 0x2300 },
2548 { 0x1d, 0x3d98 },
2549 { 0x1f, 0x0002 },
2550 { 0x0c, 0x7eb8 },
2551 { 0x06, 0x5461 },
2552 { 0x1f, 0x0003 },
2553 { 0x16, 0x0f0a },
2554 { 0x1f, 0x0000 }
2555 };
2556
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002557 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu197ff762008-06-28 13:16:02 +02002558
françois romieu4da19632011-01-03 15:07:55 +00002559 rtl_patchphy(tp, 0x16, 1 << 0);
2560 rtl_patchphy(tp, 0x14, 1 << 5);
2561 rtl_patchphy(tp, 0x0d, 1 << 5);
2562 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002563}
2564
françois romieu4da19632011-01-03 15:07:55 +00002565static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002566{
françois romieu4da19632011-01-03 15:07:55 +00002567 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002568}
2569
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002570static const struct phy_reg rtl8168d_1_phy_reg_init_0[] = {
2571 /* Channel Estimation */
2572 { 0x1f, 0x0001 },
2573 { 0x06, 0x4064 },
2574 { 0x07, 0x2863 },
2575 { 0x08, 0x059c },
2576 { 0x09, 0x26b4 },
2577 { 0x0a, 0x6a19 },
2578 { 0x0b, 0xdcc8 },
2579 { 0x10, 0xf06d },
2580 { 0x14, 0x7f68 },
2581 { 0x18, 0x7fd9 },
2582 { 0x1c, 0xf0ff },
2583 { 0x1d, 0x3d9c },
2584 { 0x1f, 0x0003 },
2585 { 0x12, 0xf49f },
2586 { 0x13, 0x070b },
2587 { 0x1a, 0x05ad },
2588 { 0x14, 0x94c0 },
2589
2590 /*
2591 * Tx Error Issue
2592 * Enhance line driver power
2593 */
2594 { 0x1f, 0x0002 },
2595 { 0x06, 0x5561 },
2596 { 0x1f, 0x0005 },
2597 { 0x05, 0x8332 },
2598 { 0x06, 0x5561 },
2599
2600 /*
2601 * Can not link to 1Gbps with bad cable
2602 * Decrease SNR threshold form 21.07dB to 19.04dB
2603 */
2604 { 0x1f, 0x0001 },
2605 { 0x17, 0x0cc0 },
2606
2607 { 0x1f, 0x0000 },
2608 { 0x0d, 0xf880 }
2609};
2610
2611static const struct phy_reg rtl8168d_1_phy_reg_init_1[] = {
2612 { 0x1f, 0x0002 },
2613 { 0x05, 0x669a },
2614 { 0x1f, 0x0005 },
2615 { 0x05, 0x8330 },
2616 { 0x06, 0x669a },
2617 { 0x1f, 0x0002 }
2618};
2619
françois romieubca03d52011-01-03 15:07:31 +00002620static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002621{
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002622 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
Francois Romieu5b538df2008-07-20 16:22:45 +02002623
françois romieubca03d52011-01-03 15:07:31 +00002624 /*
2625 * Rx Error Issue
2626 * Fine Tune Switching regulator parameter
2627 */
françois romieu4da19632011-01-03 15:07:55 +00002628 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002629 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2630 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002631
Francois Romieufdf6fc02012-07-06 22:40:38 +02002632 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
françois romieudaf9df62009-10-07 12:44:20 +00002633 int val;
2634
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002635 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
françois romieudaf9df62009-10-07 12:44:20 +00002636
françois romieu4da19632011-01-03 15:07:55 +00002637 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002638
2639 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002640 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002641 0x0065, 0x0066, 0x0067, 0x0068,
2642 0x0069, 0x006a, 0x006b, 0x006c
2643 };
2644 int i;
2645
françois romieu4da19632011-01-03 15:07:55 +00002646 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002647
2648 val &= 0xff00;
2649 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002650 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002651 }
2652 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002653 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002654 { 0x1f, 0x0002 },
2655 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002656 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002657 { 0x05, 0x8330 },
2658 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002659 };
2660
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002661 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02002662 }
2663
françois romieubca03d52011-01-03 15:07:31 +00002664 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002665 rtl_writephy(tp, 0x1f, 0x0002);
2666 rtl_patchphy(tp, 0x0d, 0x0300);
2667 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002668
françois romieubca03d52011-01-03 15:07:31 +00002669 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002670 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002671 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2672 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002673
françois romieu4da19632011-01-03 15:07:55 +00002674 rtl_writephy(tp, 0x1f, 0x0005);
2675 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002676
2677 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002678
françois romieu4da19632011-01-03 15:07:55 +00002679 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002680}
2681
françois romieubca03d52011-01-03 15:07:31 +00002682static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002683{
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002684 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
françois romieudaf9df62009-10-07 12:44:20 +00002685
Francois Romieufdf6fc02012-07-06 22:40:38 +02002686 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
françois romieudaf9df62009-10-07 12:44:20 +00002687 int val;
2688
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002689 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
françois romieudaf9df62009-10-07 12:44:20 +00002690
françois romieu4da19632011-01-03 15:07:55 +00002691 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002692 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002693 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002694 0x0065, 0x0066, 0x0067, 0x0068,
2695 0x0069, 0x006a, 0x006b, 0x006c
2696 };
2697 int i;
2698
françois romieu4da19632011-01-03 15:07:55 +00002699 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002700
2701 val &= 0xff00;
2702 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002703 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002704 }
2705 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002706 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002707 { 0x1f, 0x0002 },
2708 { 0x05, 0x2642 },
2709 { 0x1f, 0x0005 },
2710 { 0x05, 0x8330 },
2711 { 0x06, 0x2642 }
2712 };
2713
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002714 rtl_writephy_batch(tp, phy_reg_init);
françois romieudaf9df62009-10-07 12:44:20 +00002715 }
2716
françois romieubca03d52011-01-03 15:07:31 +00002717 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002718 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002719 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2720 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002721
françois romieubca03d52011-01-03 15:07:31 +00002722 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002723 rtl_writephy(tp, 0x1f, 0x0002);
2724 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002725
françois romieu4da19632011-01-03 15:07:55 +00002726 rtl_writephy(tp, 0x1f, 0x0005);
2727 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002728
2729 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002730
françois romieu4da19632011-01-03 15:07:55 +00002731 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002732}
2733
françois romieu4da19632011-01-03 15:07:55 +00002734static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002735{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002736 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002737 { 0x1f, 0x0002 },
2738 { 0x10, 0x0008 },
2739 { 0x0d, 0x006c },
2740
2741 { 0x1f, 0x0000 },
2742 { 0x0d, 0xf880 },
2743
2744 { 0x1f, 0x0001 },
2745 { 0x17, 0x0cc0 },
2746
2747 { 0x1f, 0x0001 },
2748 { 0x0b, 0xa4d8 },
2749 { 0x09, 0x281c },
2750 { 0x07, 0x2883 },
2751 { 0x0a, 0x6b35 },
2752 { 0x1d, 0x3da4 },
2753 { 0x1c, 0xeffd },
2754 { 0x14, 0x7f52 },
2755 { 0x18, 0x7fc6 },
2756 { 0x08, 0x0601 },
2757 { 0x06, 0x4063 },
2758 { 0x10, 0xf074 },
2759 { 0x1f, 0x0003 },
2760 { 0x13, 0x0789 },
2761 { 0x12, 0xf4bd },
2762 { 0x1a, 0x04fd },
2763 { 0x14, 0x84b0 },
2764 { 0x1f, 0x0000 },
2765 { 0x00, 0x9200 },
2766
2767 { 0x1f, 0x0005 },
2768 { 0x01, 0x0340 },
2769 { 0x1f, 0x0001 },
2770 { 0x04, 0x4000 },
2771 { 0x03, 0x1d21 },
2772 { 0x02, 0x0c32 },
2773 { 0x01, 0x0200 },
2774 { 0x00, 0x5554 },
2775 { 0x04, 0x4800 },
2776 { 0x04, 0x4000 },
2777 { 0x04, 0xf000 },
2778 { 0x03, 0xdf01 },
2779 { 0x02, 0xdf20 },
2780 { 0x01, 0x101a },
2781 { 0x00, 0xa0ff },
2782 { 0x04, 0xf800 },
2783 { 0x04, 0xf000 },
2784 { 0x1f, 0x0000 },
2785
2786 { 0x1f, 0x0007 },
2787 { 0x1e, 0x0023 },
2788 { 0x16, 0x0000 },
2789 { 0x1f, 0x0000 }
2790 };
2791
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002792 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02002793}
2794
françois romieue6de30d2011-01-03 15:08:37 +00002795static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2796{
2797 static const struct phy_reg phy_reg_init[] = {
2798 { 0x1f, 0x0001 },
2799 { 0x17, 0x0cc0 },
2800
2801 { 0x1f, 0x0007 },
2802 { 0x1e, 0x002d },
2803 { 0x18, 0x0040 },
2804 { 0x1f, 0x0000 }
2805 };
2806
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002807 rtl_writephy_batch(tp, phy_reg_init);
françois romieue6de30d2011-01-03 15:08:37 +00002808 rtl_patchphy(tp, 0x0d, 1 << 5);
2809}
2810
Hayes Wang70090422011-07-06 15:58:06 +08002811static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002812{
2813 static const struct phy_reg phy_reg_init[] = {
2814 /* Enable Delay cap */
2815 { 0x1f, 0x0005 },
2816 { 0x05, 0x8b80 },
2817 { 0x06, 0xc896 },
2818 { 0x1f, 0x0000 },
2819
2820 /* Channel estimation fine tune */
2821 { 0x1f, 0x0001 },
2822 { 0x0b, 0x6c20 },
2823 { 0x07, 0x2872 },
2824 { 0x1c, 0xefff },
2825 { 0x1f, 0x0003 },
2826 { 0x14, 0x6420 },
2827 { 0x1f, 0x0000 },
2828
2829 /* Update PFM & 10M TX idle timer */
2830 { 0x1f, 0x0007 },
2831 { 0x1e, 0x002f },
2832 { 0x15, 0x1919 },
2833 { 0x1f, 0x0000 },
2834
2835 { 0x1f, 0x0007 },
2836 { 0x1e, 0x00ac },
2837 { 0x18, 0x0006 },
2838 { 0x1f, 0x0000 }
2839 };
2840
Francois Romieu15ecd032011-04-27 13:52:22 -07002841 rtl_apply_firmware(tp);
2842
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002843 rtl_writephy_batch(tp, phy_reg_init);
hayeswang01dc7fe2011-03-21 01:50:28 +00002844
2845 /* DCO enable for 10M IDLE Power */
2846 rtl_writephy(tp, 0x1f, 0x0007);
2847 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002848 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002849 rtl_writephy(tp, 0x1f, 0x0000);
2850
2851 /* For impedance matching */
2852 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002853 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002854 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002855
2856 /* PHY auto speed down */
2857 rtl_writephy(tp, 0x1f, 0x0007);
2858 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002859 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002860 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002861 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002862
2863 rtl_writephy(tp, 0x1f, 0x0005);
2864 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002865 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002866 rtl_writephy(tp, 0x1f, 0x0000);
2867
2868 rtl_writephy(tp, 0x1f, 0x0005);
2869 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002870 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002871 rtl_writephy(tp, 0x1f, 0x0007);
2872 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002873 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00002874 rtl_writephy(tp, 0x1f, 0x0006);
2875 rtl_writephy(tp, 0x00, 0x5a00);
2876 rtl_writephy(tp, 0x1f, 0x0000);
2877 rtl_writephy(tp, 0x0d, 0x0007);
2878 rtl_writephy(tp, 0x0e, 0x003c);
2879 rtl_writephy(tp, 0x0d, 0x4007);
2880 rtl_writephy(tp, 0x0e, 0x0000);
2881 rtl_writephy(tp, 0x0d, 0x0000);
2882}
2883
françois romieu9ecb9aa2012-12-07 11:20:21 +00002884static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
2885{
2886 const u16 w[] = {
2887 addr[0] | (addr[1] << 8),
2888 addr[2] | (addr[3] << 8),
2889 addr[4] | (addr[5] << 8)
2890 };
françois romieu9ecb9aa2012-12-07 11:20:21 +00002891
Heiner Kallweit3aa4c492019-05-02 20:46:52 +02002892 rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, w[0] | (w[1] << 16));
2893 rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, w[2]);
2894 rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, w[0] << 16);
2895 rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, w[1] | (w[2] << 16));
françois romieu9ecb9aa2012-12-07 11:20:21 +00002896}
2897
Hayes Wang70090422011-07-06 15:58:06 +08002898static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2899{
2900 static const struct phy_reg phy_reg_init[] = {
2901 /* Enable Delay cap */
2902 { 0x1f, 0x0004 },
2903 { 0x1f, 0x0007 },
2904 { 0x1e, 0x00ac },
2905 { 0x18, 0x0006 },
2906 { 0x1f, 0x0002 },
2907 { 0x1f, 0x0000 },
2908 { 0x1f, 0x0000 },
2909
2910 /* Channel estimation fine tune */
2911 { 0x1f, 0x0003 },
2912 { 0x09, 0xa20f },
2913 { 0x1f, 0x0000 },
2914 { 0x1f, 0x0000 },
2915
2916 /* Green Setting */
2917 { 0x1f, 0x0005 },
2918 { 0x05, 0x8b5b },
2919 { 0x06, 0x9222 },
2920 { 0x05, 0x8b6d },
2921 { 0x06, 0x8000 },
2922 { 0x05, 0x8b76 },
2923 { 0x06, 0x8000 },
2924 { 0x1f, 0x0000 }
2925 };
2926
2927 rtl_apply_firmware(tp);
2928
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002929 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang70090422011-07-06 15:58:06 +08002930
2931 /* For 4-corner performance improve */
2932 rtl_writephy(tp, 0x1f, 0x0005);
2933 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002934 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002935 rtl_writephy(tp, 0x1f, 0x0000);
2936
2937 /* PHY auto speed down */
2938 rtl_writephy(tp, 0x1f, 0x0004);
2939 rtl_writephy(tp, 0x1f, 0x0007);
2940 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002941 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002942 rtl_writephy(tp, 0x1f, 0x0002);
2943 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002944 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002945
2946 /* improve 10M EEE waveform */
2947 rtl_writephy(tp, 0x1f, 0x0005);
2948 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002949 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002950 rtl_writephy(tp, 0x1f, 0x0000);
2951
2952 /* Improve 2-pair detection performance */
2953 rtl_writephy(tp, 0x1f, 0x0005);
2954 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002955 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002956 rtl_writephy(tp, 0x1f, 0x0000);
2957
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002958 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01002959 rtl_enable_eee(tp);
Hayes Wang70090422011-07-06 15:58:06 +08002960
2961 /* Green feature */
2962 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01002963 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
2964 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002965 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01002966 rtl_writephy(tp, 0x1f, 0x0005);
2967 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
2968 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00002969
françois romieu9ecb9aa2012-12-07 11:20:21 +00002970 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
2971 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08002972}
2973
Hayes Wang5f886e02012-03-30 14:33:03 +08002974static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
2975{
2976 /* For 4-corner performance improve */
2977 rtl_writephy(tp, 0x1f, 0x0005);
2978 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002979 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002980 rtl_writephy(tp, 0x1f, 0x0000);
2981
2982 /* PHY auto speed down */
2983 rtl_writephy(tp, 0x1f, 0x0007);
2984 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002985 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002986 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002987 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002988
2989 /* Improve 10M EEE waveform */
2990 rtl_writephy(tp, 0x1f, 0x0005);
2991 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002992 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002993 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002994
2995 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01002996 rtl_enable_eee(tp);
Hayes Wang5f886e02012-03-30 14:33:03 +08002997}
2998
Hayes Wangc2218922011-09-06 16:55:18 +08002999static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3000{
3001 static const struct phy_reg phy_reg_init[] = {
3002 /* Channel estimation fine tune */
3003 { 0x1f, 0x0003 },
3004 { 0x09, 0xa20f },
3005 { 0x1f, 0x0000 },
3006
3007 /* Modify green table for giga & fnet */
3008 { 0x1f, 0x0005 },
3009 { 0x05, 0x8b55 },
3010 { 0x06, 0x0000 },
3011 { 0x05, 0x8b5e },
3012 { 0x06, 0x0000 },
3013 { 0x05, 0x8b67 },
3014 { 0x06, 0x0000 },
3015 { 0x05, 0x8b70 },
3016 { 0x06, 0x0000 },
3017 { 0x1f, 0x0000 },
3018 { 0x1f, 0x0007 },
3019 { 0x1e, 0x0078 },
3020 { 0x17, 0x0000 },
3021 { 0x19, 0x00fb },
3022 { 0x1f, 0x0000 },
3023
3024 /* Modify green table for 10M */
3025 { 0x1f, 0x0005 },
3026 { 0x05, 0x8b79 },
3027 { 0x06, 0xaa00 },
3028 { 0x1f, 0x0000 },
3029
3030 /* Disable hiimpedance detection (RTCT) */
3031 { 0x1f, 0x0003 },
3032 { 0x01, 0x328a },
3033 { 0x1f, 0x0000 }
3034 };
3035
3036 rtl_apply_firmware(tp);
3037
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003038 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangc2218922011-09-06 16:55:18 +08003039
Hayes Wang5f886e02012-03-30 14:33:03 +08003040 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003041
3042 /* Improve 2-pair detection performance */
3043 rtl_writephy(tp, 0x1f, 0x0005);
3044 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003045 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003046 rtl_writephy(tp, 0x1f, 0x0000);
3047}
3048
3049static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3050{
3051 rtl_apply_firmware(tp);
3052
Hayes Wang5f886e02012-03-30 14:33:03 +08003053 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003054}
3055
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003056static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3057{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003058 static const struct phy_reg phy_reg_init[] = {
3059 /* Channel estimation fine tune */
3060 { 0x1f, 0x0003 },
3061 { 0x09, 0xa20f },
3062 { 0x1f, 0x0000 },
3063
3064 /* Modify green table for giga & fnet */
3065 { 0x1f, 0x0005 },
3066 { 0x05, 0x8b55 },
3067 { 0x06, 0x0000 },
3068 { 0x05, 0x8b5e },
3069 { 0x06, 0x0000 },
3070 { 0x05, 0x8b67 },
3071 { 0x06, 0x0000 },
3072 { 0x05, 0x8b70 },
3073 { 0x06, 0x0000 },
3074 { 0x1f, 0x0000 },
3075 { 0x1f, 0x0007 },
3076 { 0x1e, 0x0078 },
3077 { 0x17, 0x0000 },
3078 { 0x19, 0x00aa },
3079 { 0x1f, 0x0000 },
3080
3081 /* Modify green table for 10M */
3082 { 0x1f, 0x0005 },
3083 { 0x05, 0x8b79 },
3084 { 0x06, 0xaa00 },
3085 { 0x1f, 0x0000 },
3086
3087 /* Disable hiimpedance detection (RTCT) */
3088 { 0x1f, 0x0003 },
3089 { 0x01, 0x328a },
3090 { 0x1f, 0x0000 }
3091 };
3092
3093
3094 rtl_apply_firmware(tp);
3095
3096 rtl8168f_hw_phy_config(tp);
3097
3098 /* Improve 2-pair detection performance */
3099 rtl_writephy(tp, 0x1f, 0x0005);
3100 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003101 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003102 rtl_writephy(tp, 0x1f, 0x0000);
3103
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003104 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003105
3106 /* Modify green table for giga */
3107 rtl_writephy(tp, 0x1f, 0x0005);
3108 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003109 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003110 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003111 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003112 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003113 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003114 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003115 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003116 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003117 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003118 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003119 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003120 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003121 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003122 rtl_writephy(tp, 0x1f, 0x0000);
3123
3124 /* uc same-seed solution */
3125 rtl_writephy(tp, 0x1f, 0x0005);
3126 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003127 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003128 rtl_writephy(tp, 0x1f, 0x0000);
3129
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003130 /* Green feature */
3131 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003132 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3133 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003134 rtl_writephy(tp, 0x1f, 0x0000);
3135}
3136
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003137static void rtl8168g_disable_aldps(struct rtl8169_private *tp)
3138{
Heiner Kallweita2928d22019-06-02 10:53:49 +02003139 phy_modify_paged(tp->phydev, 0x0a43, 0x10, BIT(2), 0);
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003140}
3141
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003142static void rtl8168g_phy_adjust_10m_aldps(struct rtl8169_private *tp)
3143{
3144 struct phy_device *phydev = tp->phydev;
3145
Heiner Kallweita2928d22019-06-02 10:53:49 +02003146 phy_modify_paged(phydev, 0x0bcc, 0x14, BIT(8), 0);
3147 phy_modify_paged(phydev, 0x0a44, 0x11, 0, BIT(7) | BIT(6));
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003148 phy_write(phydev, 0x1f, 0x0a43);
3149 phy_write(phydev, 0x13, 0x8084);
3150 phy_clear_bits(phydev, 0x14, BIT(14) | BIT(13));
3151 phy_set_bits(phydev, 0x10, BIT(12) | BIT(1) | BIT(0));
3152
3153 phy_write(phydev, 0x1f, 0x0000);
3154}
3155
Hayes Wangc5583862012-07-02 17:23:22 +08003156static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3157{
Heiner Kallweita2928d22019-06-02 10:53:49 +02003158 int ret;
3159
Hayes Wangc5583862012-07-02 17:23:22 +08003160 rtl_apply_firmware(tp);
3161
Heiner Kallweita2928d22019-06-02 10:53:49 +02003162 ret = phy_read_paged(tp->phydev, 0x0a46, 0x10);
3163 if (ret & BIT(8))
3164 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, BIT(15), 0);
3165 else
3166 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, 0, BIT(15));
Hayes Wangc5583862012-07-02 17:23:22 +08003167
Heiner Kallweita2928d22019-06-02 10:53:49 +02003168 ret = phy_read_paged(tp->phydev, 0x0a46, 0x13);
3169 if (ret & BIT(8))
Thomas Voegtle1a03bb52019-07-20 19:01:22 +02003170 phy_modify_paged(tp->phydev, 0x0c41, 0x15, 0, BIT(1));
Heiner Kallweita2928d22019-06-02 10:53:49 +02003171 else
Thomas Voegtle1a03bb52019-07-20 19:01:22 +02003172 phy_modify_paged(tp->phydev, 0x0c41, 0x15, BIT(1), 0);
Hayes Wangc5583862012-07-02 17:23:22 +08003173
hayeswang41f44d12013-04-01 22:23:36 +00003174 /* Enable PHY auto speed down */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003175 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
Hayes Wangc5583862012-07-02 17:23:22 +08003176
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003177 rtl8168g_phy_adjust_10m_aldps(tp);
hayeswangfe7524c2013-04-01 22:23:37 +00003178
hayeswang41f44d12013-04-01 22:23:36 +00003179 /* EEE auto-fallback function */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003180 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
Hayes Wangc5583862012-07-02 17:23:22 +08003181
hayeswang41f44d12013-04-01 22:23:36 +00003182 /* Enable UC LPF tune function */
3183 rtl_writephy(tp, 0x1f, 0x0a43);
3184 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003185 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003186
Heiner Kallweita2928d22019-06-02 10:53:49 +02003187 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
hayeswang41f44d12013-04-01 22:23:36 +00003188
hayeswangfe7524c2013-04-01 22:23:37 +00003189 /* Improve SWR Efficiency */
3190 rtl_writephy(tp, 0x1f, 0x0bcd);
3191 rtl_writephy(tp, 0x14, 0x5065);
3192 rtl_writephy(tp, 0x14, 0xd065);
3193 rtl_writephy(tp, 0x1f, 0x0bc8);
3194 rtl_writephy(tp, 0x11, 0x5655);
3195 rtl_writephy(tp, 0x1f, 0x0bcd);
3196 rtl_writephy(tp, 0x14, 0x1065);
3197 rtl_writephy(tp, 0x14, 0x9065);
3198 rtl_writephy(tp, 0x14, 0x1065);
Heiner Kallweita2928d22019-06-02 10:53:49 +02003199 rtl_writephy(tp, 0x1f, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003200
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003201 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003202 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003203 rtl_enable_eee(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08003204}
3205
hayeswang57538c42013-04-01 22:23:40 +00003206static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3207{
3208 rtl_apply_firmware(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003209 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003210 rtl_enable_eee(tp);
hayeswang57538c42013-04-01 22:23:40 +00003211}
3212
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003213static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3214{
3215 u16 dout_tapbin;
3216 u32 data;
3217
3218 rtl_apply_firmware(tp);
3219
3220 /* CHN EST parameters adjust - giga master */
3221 rtl_writephy(tp, 0x1f, 0x0a43);
3222 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003223 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003224 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003225 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003226 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003227 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003228 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003229 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003230 rtl_writephy(tp, 0x1f, 0x0000);
3231
3232 /* CHN EST parameters adjust - giga slave */
3233 rtl_writephy(tp, 0x1f, 0x0a43);
3234 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003235 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003236 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003237 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003238 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003239 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003240 rtl_writephy(tp, 0x1f, 0x0000);
3241
3242 /* CHN EST parameters adjust - fnet */
3243 rtl_writephy(tp, 0x1f, 0x0a43);
3244 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003245 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003246 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003247 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003248 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003249 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003250 rtl_writephy(tp, 0x1f, 0x0000);
3251
3252 /* enable R-tune & PGA-retune function */
3253 dout_tapbin = 0;
3254 rtl_writephy(tp, 0x1f, 0x0a46);
3255 data = rtl_readphy(tp, 0x13);
3256 data &= 3;
3257 data <<= 2;
3258 dout_tapbin |= data;
3259 data = rtl_readphy(tp, 0x12);
3260 data &= 0xc000;
3261 data >>= 14;
3262 dout_tapbin |= data;
3263 dout_tapbin = ~(dout_tapbin^0x08);
3264 dout_tapbin <<= 12;
3265 dout_tapbin &= 0xf000;
3266 rtl_writephy(tp, 0x1f, 0x0a43);
3267 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003268 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003269 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003270 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003271 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003272 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003273 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003274 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003275
3276 rtl_writephy(tp, 0x1f, 0x0a43);
3277 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003278 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003279 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003280 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003281 rtl_writephy(tp, 0x1f, 0x0000);
3282
3283 /* enable GPHY 10M */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003284 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003285
3286 /* SAR ADC performance */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003287 phy_modify_paged(tp->phydev, 0x0bca, 0x17, BIT(12) | BIT(13), BIT(14));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003288
3289 rtl_writephy(tp, 0x1f, 0x0a43);
3290 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003291 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003292 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003293 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003294 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003295 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003296 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003297 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003298 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003299 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003300 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003301 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003302 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003303 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003304 rtl_writephy(tp, 0x1f, 0x0000);
3305
3306 /* disable phy pfm mode */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003307 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003308
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003309 rtl8168g_disable_aldps(tp);
Heiner Kallweitb6cef262019-08-15 14:21:30 +02003310 rtl8168h_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003311 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003312}
3313
3314static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3315{
3316 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3317 u16 rlen;
3318 u32 data;
3319
3320 rtl_apply_firmware(tp);
3321
3322 /* CHIN EST parameter update */
3323 rtl_writephy(tp, 0x1f, 0x0a43);
3324 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003325 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003326 rtl_writephy(tp, 0x1f, 0x0000);
3327
3328 /* enable R-tune & PGA-retune function */
3329 rtl_writephy(tp, 0x1f, 0x0a43);
3330 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003331 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003332 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003333 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003334 rtl_writephy(tp, 0x1f, 0x0000);
3335
3336 /* enable GPHY 10M */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003337 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003338
3339 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3340 data = r8168_mac_ocp_read(tp, 0xdd02);
3341 ioffset_p3 = ((data & 0x80)>>7);
3342 ioffset_p3 <<= 3;
3343
3344 data = r8168_mac_ocp_read(tp, 0xdd00);
3345 ioffset_p3 |= ((data & (0xe000))>>13);
3346 ioffset_p2 = ((data & (0x1e00))>>9);
3347 ioffset_p1 = ((data & (0x01e0))>>5);
3348 ioffset_p0 = ((data & 0x0010)>>4);
3349 ioffset_p0 <<= 3;
3350 ioffset_p0 |= (data & (0x07));
3351 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3352
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003353 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003354 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003355 rtl_writephy(tp, 0x1f, 0x0bcf);
3356 rtl_writephy(tp, 0x16, data);
3357 rtl_writephy(tp, 0x1f, 0x0000);
3358 }
3359
3360 /* Modify rlen (TX LPF corner frequency) level */
3361 rtl_writephy(tp, 0x1f, 0x0bcd);
3362 data = rtl_readphy(tp, 0x16);
3363 data &= 0x000f;
3364 rlen = 0;
3365 if (data > 3)
3366 rlen = data - 3;
3367 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3368 rtl_writephy(tp, 0x17, data);
3369 rtl_writephy(tp, 0x1f, 0x0bcd);
3370 rtl_writephy(tp, 0x1f, 0x0000);
3371
3372 /* disable phy pfm mode */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003373 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003374
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003375 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003376 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003377 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003378}
3379
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003380static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3381{
3382 /* Enable PHY auto speed down */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003383 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003384
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003385 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003386
3387 /* Enable EEE auto-fallback function */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003388 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003389
3390 /* Enable UC LPF tune function */
3391 rtl_writephy(tp, 0x1f, 0x0a43);
3392 rtl_writephy(tp, 0x13, 0x8012);
3393 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3394 rtl_writephy(tp, 0x1f, 0x0000);
3395
3396 /* set rg_sel_sdm_rate */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003397 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003398
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003399 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003400 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003401 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003402}
3403
3404static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3405{
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003406 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003407
3408 /* Enable UC LPF tune function */
3409 rtl_writephy(tp, 0x1f, 0x0a43);
3410 rtl_writephy(tp, 0x13, 0x8012);
3411 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3412 rtl_writephy(tp, 0x1f, 0x0000);
3413
3414 /* Set rg_sel_sdm_rate */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003415 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003416
3417 /* Channel estimation parameters */
3418 rtl_writephy(tp, 0x1f, 0x0a43);
3419 rtl_writephy(tp, 0x13, 0x80f3);
3420 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3421 rtl_writephy(tp, 0x13, 0x80f0);
3422 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3423 rtl_writephy(tp, 0x13, 0x80ef);
3424 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3425 rtl_writephy(tp, 0x13, 0x80f6);
3426 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3427 rtl_writephy(tp, 0x13, 0x80ec);
3428 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3429 rtl_writephy(tp, 0x13, 0x80ed);
3430 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3431 rtl_writephy(tp, 0x13, 0x80f2);
3432 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3433 rtl_writephy(tp, 0x13, 0x80f4);
3434 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3435 rtl_writephy(tp, 0x1f, 0x0a43);
3436 rtl_writephy(tp, 0x13, 0x8110);
3437 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3438 rtl_writephy(tp, 0x13, 0x810f);
3439 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3440 rtl_writephy(tp, 0x13, 0x8111);
3441 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3442 rtl_writephy(tp, 0x13, 0x8113);
3443 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3444 rtl_writephy(tp, 0x13, 0x8115);
3445 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3446 rtl_writephy(tp, 0x13, 0x810e);
3447 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3448 rtl_writephy(tp, 0x13, 0x810c);
3449 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3450 rtl_writephy(tp, 0x13, 0x810b);
3451 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3452 rtl_writephy(tp, 0x1f, 0x0a43);
3453 rtl_writephy(tp, 0x13, 0x80d1);
3454 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3455 rtl_writephy(tp, 0x13, 0x80cd);
3456 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3457 rtl_writephy(tp, 0x13, 0x80d3);
3458 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3459 rtl_writephy(tp, 0x13, 0x80d5);
3460 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3461 rtl_writephy(tp, 0x13, 0x80d7);
3462 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3463
3464 /* Force PWM-mode */
3465 rtl_writephy(tp, 0x1f, 0x0bcd);
3466 rtl_writephy(tp, 0x14, 0x5065);
3467 rtl_writephy(tp, 0x14, 0xd065);
3468 rtl_writephy(tp, 0x1f, 0x0bc8);
3469 rtl_writephy(tp, 0x12, 0x00ed);
3470 rtl_writephy(tp, 0x1f, 0x0bcd);
3471 rtl_writephy(tp, 0x14, 0x1065);
3472 rtl_writephy(tp, 0x14, 0x9065);
3473 rtl_writephy(tp, 0x14, 0x1065);
3474 rtl_writephy(tp, 0x1f, 0x0000);
3475
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003476 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003477 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003478 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003479}
3480
françois romieu4da19632011-01-03 15:07:55 +00003481static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003482{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003483 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003484 { 0x1f, 0x0003 },
3485 { 0x08, 0x441d },
3486 { 0x01, 0x9100 },
3487 { 0x1f, 0x0000 }
3488 };
3489
françois romieu4da19632011-01-03 15:07:55 +00003490 rtl_writephy(tp, 0x1f, 0x0000);
3491 rtl_patchphy(tp, 0x11, 1 << 12);
3492 rtl_patchphy(tp, 0x19, 1 << 13);
3493 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003494
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003495 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003496}
3497
Hayes Wang5a5e4442011-02-22 17:26:21 +08003498static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3499{
3500 static const struct phy_reg phy_reg_init[] = {
3501 { 0x1f, 0x0005 },
3502 { 0x1a, 0x0000 },
3503 { 0x1f, 0x0000 },
3504
3505 { 0x1f, 0x0004 },
3506 { 0x1c, 0x0000 },
3507 { 0x1f, 0x0000 },
3508
3509 { 0x1f, 0x0001 },
3510 { 0x15, 0x7701 },
3511 { 0x1f, 0x0000 }
3512 };
3513
3514 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003515 rtl_writephy(tp, 0x1f, 0x0000);
3516 rtl_writephy(tp, 0x18, 0x0310);
3517 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003518
François Romieu953a12c2011-04-24 17:38:48 +02003519 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003520
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003521 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003522}
3523
Hayes Wang7e18dca2012-03-30 14:33:02 +08003524static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3525{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003526 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003527 rtl_writephy(tp, 0x1f, 0x0000);
3528 rtl_writephy(tp, 0x18, 0x0310);
3529 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003530
3531 rtl_apply_firmware(tp);
3532
3533 /* EEE setting */
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003534 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003535 rtl_writephy(tp, 0x1f, 0x0004);
3536 rtl_writephy(tp, 0x10, 0x401f);
3537 rtl_writephy(tp, 0x19, 0x7030);
3538 rtl_writephy(tp, 0x1f, 0x0000);
3539}
3540
Hayes Wang5598bfe2012-07-02 17:23:21 +08003541static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3542{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003543 static const struct phy_reg phy_reg_init[] = {
3544 { 0x1f, 0x0004 },
3545 { 0x10, 0xc07f },
3546 { 0x19, 0x7030 },
3547 { 0x1f, 0x0000 }
3548 };
3549
3550 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003551 rtl_writephy(tp, 0x1f, 0x0000);
3552 rtl_writephy(tp, 0x18, 0x0310);
3553 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003554
3555 rtl_apply_firmware(tp);
3556
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003557 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003558 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003559
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003560 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003561}
3562
Francois Romieu5615d9f2007-08-17 17:50:46 +02003563static void rtl_hw_phy_config(struct net_device *dev)
3564{
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003565 static const rtl_generic_fct phy_configs[] = {
3566 /* PCI devices. */
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003567 [RTL_GIGA_MAC_VER_02] = rtl8169s_hw_phy_config,
3568 [RTL_GIGA_MAC_VER_03] = rtl8169s_hw_phy_config,
3569 [RTL_GIGA_MAC_VER_04] = rtl8169sb_hw_phy_config,
3570 [RTL_GIGA_MAC_VER_05] = rtl8169scd_hw_phy_config,
3571 [RTL_GIGA_MAC_VER_06] = rtl8169sce_hw_phy_config,
3572 /* PCI-E devices. */
3573 [RTL_GIGA_MAC_VER_07] = rtl8102e_hw_phy_config,
3574 [RTL_GIGA_MAC_VER_08] = rtl8102e_hw_phy_config,
3575 [RTL_GIGA_MAC_VER_09] = rtl8102e_hw_phy_config,
3576 [RTL_GIGA_MAC_VER_10] = NULL,
3577 [RTL_GIGA_MAC_VER_11] = rtl8168bb_hw_phy_config,
3578 [RTL_GIGA_MAC_VER_12] = rtl8168bef_hw_phy_config,
3579 [RTL_GIGA_MAC_VER_13] = NULL,
3580 [RTL_GIGA_MAC_VER_14] = NULL,
3581 [RTL_GIGA_MAC_VER_15] = NULL,
3582 [RTL_GIGA_MAC_VER_16] = NULL,
3583 [RTL_GIGA_MAC_VER_17] = rtl8168bef_hw_phy_config,
3584 [RTL_GIGA_MAC_VER_18] = rtl8168cp_1_hw_phy_config,
3585 [RTL_GIGA_MAC_VER_19] = rtl8168c_1_hw_phy_config,
3586 [RTL_GIGA_MAC_VER_20] = rtl8168c_2_hw_phy_config,
3587 [RTL_GIGA_MAC_VER_21] = rtl8168c_3_hw_phy_config,
3588 [RTL_GIGA_MAC_VER_22] = rtl8168c_4_hw_phy_config,
3589 [RTL_GIGA_MAC_VER_23] = rtl8168cp_2_hw_phy_config,
3590 [RTL_GIGA_MAC_VER_24] = rtl8168cp_2_hw_phy_config,
3591 [RTL_GIGA_MAC_VER_25] = rtl8168d_1_hw_phy_config,
3592 [RTL_GIGA_MAC_VER_26] = rtl8168d_2_hw_phy_config,
3593 [RTL_GIGA_MAC_VER_27] = rtl8168d_3_hw_phy_config,
3594 [RTL_GIGA_MAC_VER_28] = rtl8168d_4_hw_phy_config,
3595 [RTL_GIGA_MAC_VER_29] = rtl8105e_hw_phy_config,
3596 [RTL_GIGA_MAC_VER_30] = rtl8105e_hw_phy_config,
3597 [RTL_GIGA_MAC_VER_31] = NULL,
3598 [RTL_GIGA_MAC_VER_32] = rtl8168e_1_hw_phy_config,
3599 [RTL_GIGA_MAC_VER_33] = rtl8168e_1_hw_phy_config,
3600 [RTL_GIGA_MAC_VER_34] = rtl8168e_2_hw_phy_config,
3601 [RTL_GIGA_MAC_VER_35] = rtl8168f_1_hw_phy_config,
3602 [RTL_GIGA_MAC_VER_36] = rtl8168f_2_hw_phy_config,
3603 [RTL_GIGA_MAC_VER_37] = rtl8402_hw_phy_config,
3604 [RTL_GIGA_MAC_VER_38] = rtl8411_hw_phy_config,
3605 [RTL_GIGA_MAC_VER_39] = rtl8106e_hw_phy_config,
3606 [RTL_GIGA_MAC_VER_40] = rtl8168g_1_hw_phy_config,
3607 [RTL_GIGA_MAC_VER_41] = NULL,
3608 [RTL_GIGA_MAC_VER_42] = rtl8168g_2_hw_phy_config,
3609 [RTL_GIGA_MAC_VER_43] = rtl8168g_2_hw_phy_config,
3610 [RTL_GIGA_MAC_VER_44] = rtl8168g_2_hw_phy_config,
3611 [RTL_GIGA_MAC_VER_45] = rtl8168h_1_hw_phy_config,
3612 [RTL_GIGA_MAC_VER_46] = rtl8168h_2_hw_phy_config,
3613 [RTL_GIGA_MAC_VER_47] = rtl8168h_1_hw_phy_config,
3614 [RTL_GIGA_MAC_VER_48] = rtl8168h_2_hw_phy_config,
3615 [RTL_GIGA_MAC_VER_49] = rtl8168ep_1_hw_phy_config,
3616 [RTL_GIGA_MAC_VER_50] = rtl8168ep_2_hw_phy_config,
3617 [RTL_GIGA_MAC_VER_51] = rtl8168ep_2_hw_phy_config,
3618 };
Francois Romieu5615d9f2007-08-17 17:50:46 +02003619 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003620
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003621 if (phy_configs[tp->mac_version])
3622 phy_configs[tp->mac_version](tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003623}
3624
Francois Romieuda78dbf2012-01-26 14:18:23 +01003625static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3626{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003627 if (!test_and_set_bit(flag, tp->wk.flags))
3628 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003629}
3630
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003631static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632{
Francois Romieu5615d9f2007-08-17 17:50:46 +02003633 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003634
Marcus Sundberg773328942008-07-10 21:28:08 +02003635 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit7a67e112018-09-19 22:00:24 +02003636 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3637 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Heiner Kallweit49d17512018-06-28 20:36:15 +02003638 netif_dbg(tp, drv, dev,
3639 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003640 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02003641 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003642
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003643 /* We may have called phy_speed_down before */
Heiner Kallweit703732f2019-01-19 22:07:05 +01003644 phy_speed_up(tp->phydev);
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003645
Heiner Kallweit703732f2019-01-19 22:07:05 +01003646 genphy_soft_reset(tp->phydev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003647}
3648
Francois Romieu773d2022007-01-31 23:47:43 +01003649static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3650{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003651 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003652
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01003653 rtl_unlock_config_regs(tp);
françois romieu908ba2bf2010-04-26 11:42:58 +00003654
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003655 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
3656 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00003657
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003658 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
3659 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00003660
françois romieu9ecb9aa2012-12-07 11:20:21 +00003661 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3662 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00003663
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01003664 rtl_lock_config_regs(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003665
Francois Romieuda78dbf2012-01-26 14:18:23 +01003666 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003667}
3668
3669static int rtl_set_mac_address(struct net_device *dev, void *p)
3670{
3671 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01003672 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01003673 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01003674
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01003675 ret = eth_mac_addr(dev, p);
3676 if (ret)
3677 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01003678
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08003679 pm_runtime_get_noresume(d);
3680
3681 if (pm_runtime_active(d))
3682 rtl_rar_set(tp, dev->dev_addr);
3683
3684 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01003685
3686 return 0;
3687}
3688
Heiner Kallweite3972862018-06-29 08:07:04 +02003689static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003690{
Heiner Kallweit703732f2019-01-19 22:07:05 +01003691 struct rtl8169_private *tp = netdev_priv(dev);
3692
Heiner Kallweit69b3c592018-07-17 22:51:53 +02003693 if (!netif_running(dev))
3694 return -ENODEV;
Heiner Kallweite3972862018-06-29 08:07:04 +02003695
Heiner Kallweit703732f2019-01-19 22:07:05 +01003696 return phy_mii_ioctl(tp->phydev, ifr, cmd);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003697}
3698
David S. Miller1805b2f2011-10-24 18:18:09 -04003699static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3700{
David S. Miller1805b2f2011-10-24 18:18:09 -04003701 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00003702 case RTL_GIGA_MAC_VER_25:
3703 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003704 case RTL_GIGA_MAC_VER_29:
3705 case RTL_GIGA_MAC_VER_30:
3706 case RTL_GIGA_MAC_VER_32:
3707 case RTL_GIGA_MAC_VER_33:
3708 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02003709 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003710 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04003711 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3712 break;
3713 default:
3714 break;
3715 }
3716}
3717
Heiner Kallweit25e94112019-05-29 20:52:03 +02003718static void rtl_pll_power_down(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00003719{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01003720 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003721 return;
3722
hayeswang01dc7fe2011-03-21 01:50:28 +00003723 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3724 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02003725 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00003726
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01003727 if (device_may_wakeup(tp_to_dev(tp))) {
3728 phy_speed_down(tp->phydev, false);
3729 rtl_wol_suspend_quirk(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003730 return;
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01003731 }
françois romieu065c27c2011-01-03 15:08:12 +00003732
françois romieu065c27c2011-01-03 15:08:12 +00003733 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02003734 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003735 case RTL_GIGA_MAC_VER_37:
3736 case RTL_GIGA_MAC_VER_39:
3737 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08003738 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003739 case RTL_GIGA_MAC_VER_45:
3740 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003741 case RTL_GIGA_MAC_VER_47:
3742 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003743 case RTL_GIGA_MAC_VER_50:
3744 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003745 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00003746 break;
hayeswangbeb330a2013-04-01 22:23:39 +00003747 case RTL_GIGA_MAC_VER_40:
3748 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003749 case RTL_GIGA_MAC_VER_49:
Heiner Kallweite719b3e2019-04-28 11:11:47 +02003750 rtl_eri_clear_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003751 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00003752 break;
Heiner Kallweit76719ee2019-05-25 20:45:04 +02003753 default:
3754 break;
françois romieu065c27c2011-01-03 15:08:12 +00003755 }
3756}
3757
Heiner Kallweit25e94112019-05-29 20:52:03 +02003758static void rtl_pll_power_up(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00003759{
françois romieu065c27c2011-01-03 15:08:12 +00003760 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02003761 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003762 case RTL_GIGA_MAC_VER_37:
3763 case RTL_GIGA_MAC_VER_39:
3764 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003765 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00003766 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08003767 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003768 case RTL_GIGA_MAC_VER_45:
3769 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003770 case RTL_GIGA_MAC_VER_47:
3771 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003772 case RTL_GIGA_MAC_VER_50:
3773 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003774 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003775 break;
hayeswangbeb330a2013-04-01 22:23:39 +00003776 case RTL_GIGA_MAC_VER_40:
3777 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003778 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003779 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02003780 rtl_eri_set_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
hayeswangbeb330a2013-04-01 22:23:39 +00003781 break;
Heiner Kallweit76719ee2019-05-25 20:45:04 +02003782 default:
3783 break;
françois romieu065c27c2011-01-03 15:08:12 +00003784 }
3785
Heiner Kallweit703732f2019-01-19 22:07:05 +01003786 phy_resume(tp->phydev);
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02003787 /* give MAC/PHY some time to resume */
3788 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00003789}
3790
Hayes Wange542a222011-07-06 15:58:04 +08003791static void rtl_init_rxcfg(struct rtl8169_private *tp)
3792{
Hayes Wange542a222011-07-06 15:58:04 +08003793 switch (tp->mac_version) {
Heiner Kallweite9588eb2019-05-25 21:14:39 +02003794 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweit2a718832018-05-02 21:39:49 +02003795 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003796 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003797 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02003798 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
Maciej S. Szmigiero511cfd52018-10-11 16:02:10 +02003799 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
3800 case RTL_GIGA_MAC_VER_38:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003801 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003802 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02003803 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003804 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00003805 break;
Hayes Wange542a222011-07-06 15:58:04 +08003806 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003807 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003808 break;
3809 }
3810}
3811
Hayes Wang92fc43b2011-07-06 15:58:03 +08003812static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3813{
Timo Teräs9fba0812013-01-15 21:01:24 +00003814 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003815}
3816
Francois Romieud58d46b2011-05-03 16:38:29 +02003817static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3818{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003819 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3820 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01003821 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003822}
3823
3824static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3825{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003826 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3827 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003828 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003829}
3830
3831static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3832{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003833 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02003834}
3835
3836static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3837{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003838 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02003839}
3840
3841static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3842{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003843 RTL_W8(tp, MaxTxPacketSize, 0x3f);
3844 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3845 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01003846 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003847}
3848
3849static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3850{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003851 RTL_W8(tp, MaxTxPacketSize, 0x0c);
3852 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3853 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003854 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003855}
3856
3857static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3858{
Heiner Kallweitcb732002018-03-20 07:45:35 +01003859 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01003860 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02003861}
3862
3863static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3864{
Heiner Kallweitcb732002018-03-20 07:45:35 +01003865 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003866 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02003867}
3868
3869static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3870{
Francois Romieud58d46b2011-05-03 16:38:29 +02003871 r8168b_0_hw_jumbo_enable(tp);
3872
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003873 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02003874}
3875
3876static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3877{
Francois Romieud58d46b2011-05-03 16:38:29 +02003878 r8168b_0_hw_jumbo_disable(tp);
3879
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003880 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02003881}
3882
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003883static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02003884{
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003885 rtl_unlock_config_regs(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003886 switch (tp->mac_version) {
3887 case RTL_GIGA_MAC_VER_11:
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003888 r8168b_0_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003889 break;
3890 case RTL_GIGA_MAC_VER_12:
3891 case RTL_GIGA_MAC_VER_17:
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003892 r8168b_1_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003893 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003894 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
3895 r8168c_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003896 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003897 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
3898 r8168dp_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003899 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003900 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
3901 r8168e_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003902 break;
Francois Romieud58d46b2011-05-03 16:38:29 +02003903 default:
Francois Romieud58d46b2011-05-03 16:38:29 +02003904 break;
3905 }
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003906 rtl_lock_config_regs(tp);
3907}
3908
3909static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3910{
3911 rtl_unlock_config_regs(tp);
3912 switch (tp->mac_version) {
3913 case RTL_GIGA_MAC_VER_11:
3914 r8168b_0_hw_jumbo_disable(tp);
3915 break;
3916 case RTL_GIGA_MAC_VER_12:
3917 case RTL_GIGA_MAC_VER_17:
3918 r8168b_1_hw_jumbo_disable(tp);
3919 break;
3920 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
3921 r8168c_hw_jumbo_disable(tp);
3922 break;
3923 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
3924 r8168dp_hw_jumbo_disable(tp);
3925 break;
3926 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
3927 r8168e_hw_jumbo_disable(tp);
3928 break;
3929 default:
3930 break;
3931 }
3932 rtl_lock_config_regs(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003933}
3934
Francois Romieuffc46952012-07-06 14:19:23 +02003935DECLARE_RTL_COND(rtl_chipcmd_cond)
3936{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003937 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02003938}
3939
Francois Romieu6f43adc2011-04-29 15:05:51 +02003940static void rtl_hw_reset(struct rtl8169_private *tp)
3941{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003942 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003943
Francois Romieuffc46952012-07-06 14:19:23 +02003944 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003945}
3946
Heiner Kallweit254764e2019-01-22 22:23:41 +01003947static void rtl_request_firmware(struct rtl8169_private *tp)
Francois Romieub6ffd972011-06-17 17:00:05 +02003948{
3949 struct rtl_fw *rtl_fw;
Francois Romieub6ffd972011-06-17 17:00:05 +02003950
Heiner Kallweit254764e2019-01-22 22:23:41 +01003951 /* firmware loaded already or no firmware available */
3952 if (tp->rtl_fw || !tp->fw_name)
3953 return;
Francois Romieub6ffd972011-06-17 17:00:05 +02003954
3955 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
Heiner Kallweit47ad5932019-06-03 21:26:31 +02003956 if (!rtl_fw) {
3957 netif_warn(tp, ifup, tp->dev, "Unable to load firmware, out of memory\n");
3958 return;
3959 }
Francois Romieub6ffd972011-06-17 17:00:05 +02003960
Heiner Kallweitce8843a2019-05-29 21:15:06 +02003961 rtl_fw->phy_write = rtl_writephy;
3962 rtl_fw->phy_read = rtl_readphy;
3963 rtl_fw->mac_mcu_write = mac_mcu_write;
3964 rtl_fw->mac_mcu_read = mac_mcu_read;
Heiner Kallweit4edb00f2019-06-03 21:25:43 +02003965 rtl_fw->fw_name = tp->fw_name;
3966 rtl_fw->dev = tp_to_dev(tp);
Heiner Kallweitce8843a2019-05-29 21:15:06 +02003967
Heiner Kallweit47ad5932019-06-03 21:26:31 +02003968 if (rtl_fw_request_firmware(rtl_fw))
3969 kfree(rtl_fw);
3970 else
3971 tp->rtl_fw = rtl_fw;
François Romieu953a12c2011-04-24 17:38:48 +02003972}
3973
Hayes Wang92fc43b2011-07-06 15:58:03 +08003974static void rtl_rx_close(struct rtl8169_private *tp)
3975{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003976 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003977}
3978
Francois Romieuffc46952012-07-06 14:19:23 +02003979DECLARE_RTL_COND(rtl_npq_cond)
3980{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003981 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02003982}
3983
3984DECLARE_RTL_COND(rtl_txcfg_empty_cond)
3985{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003986 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02003987}
3988
françois romieue6de30d2011-01-03 15:08:37 +00003989static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
3991 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00003992 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Hayes Wang92fc43b2011-07-06 15:58:03 +08003994 rtl_rx_close(tp);
3995
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02003996 switch (tp->mac_version) {
3997 case RTL_GIGA_MAC_VER_27:
3998 case RTL_GIGA_MAC_VER_28:
3999 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02004000 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004001 break;
4002 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4003 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004004 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004005 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004006 break;
4007 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004008 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004009 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004010 break;
françois romieue6de30d2011-01-03 15:08:37 +00004011 }
4012
Hayes Wang92fc43b2011-07-06 15:58:03 +08004013 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014}
4015
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004016static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004017{
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004018 u32 val = TX_DMA_BURST << TxDMAShift |
4019 InterFrameGap << TxInterFrameGapShift;
4020
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02004021 if (rtl_is_8168evl_up(tp))
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004022 val |= TXCFG_AUTO_FIFO;
4023
4024 RTL_W32(tp, TxConfig, val);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004025}
4026
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004027static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004029 /* Low hurts. Let's disable the filtering. */
4030 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004031}
4032
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004033static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004034{
4035 /*
4036 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4037 * register to be written before TxDescAddrLow to work.
4038 * Switching from MMIO to I/O access fixes the issue as well.
4039 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004040 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4041 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4042 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4043 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004044}
4045
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004046static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004047{
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004048 u32 val;
Francois Romieu6dccd162007-02-13 23:38:05 +01004049
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004050 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4051 val = 0x000fff00;
4052 else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
4053 val = 0x00ffff00;
4054 else
4055 return;
4056
4057 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
4058 val |= 0xff;
4059
4060 RTL_W32(tp, 0x7c, val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004061}
4062
Francois Romieue6b763e2012-03-08 09:35:39 +01004063static void rtl_set_rx_mode(struct net_device *dev)
4064{
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004065 u32 rx_mode = AcceptBroadcast | AcceptMyPhys | AcceptMulticast;
4066 /* Multicast hash filter */
4067 u32 mc_filter[2] = { 0xffffffff, 0xffffffff };
Francois Romieue6b763e2012-03-08 09:35:39 +01004068 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004069 u32 tmp;
Francois Romieue6b763e2012-03-08 09:35:39 +01004070
4071 if (dev->flags & IFF_PROMISC) {
4072 /* Unconditionally log net taps. */
4073 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004074 rx_mode |= AcceptAllPhys;
4075 } else if (netdev_mc_count(dev) > MC_FILTER_LIMIT ||
4076 dev->flags & IFF_ALLMULTI ||
4077 tp->mac_version == RTL_GIGA_MAC_VER_35) {
4078 /* accept all multicasts */
4079 } else if (netdev_mc_empty(dev)) {
4080 rx_mode &= ~AcceptMulticast;
Francois Romieue6b763e2012-03-08 09:35:39 +01004081 } else {
4082 struct netdev_hw_addr *ha;
4083
Francois Romieue6b763e2012-03-08 09:35:39 +01004084 mc_filter[1] = mc_filter[0] = 0;
4085 netdev_for_each_mc_addr(ha, dev) {
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004086 u32 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4087 mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31);
4088 }
4089
4090 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4091 tmp = mc_filter[0];
4092 mc_filter[0] = swab32(mc_filter[1]);
4093 mc_filter[1] = swab32(tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004094 }
4095 }
4096
4097 if (dev->features & NETIF_F_RXALL)
4098 rx_mode |= (AcceptErr | AcceptRunt);
4099
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004100 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4101 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004102
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004103 tmp = RTL_R32(tp, RxConfig);
4104 RTL_W32(tp, RxConfig, (tmp & ~RX_CONFIG_ACCEPT_MASK) | rx_mode);
Francois Romieue6b763e2012-03-08 09:35:39 +01004105}
4106
Francois Romieuffc46952012-07-06 14:19:23 +02004107DECLARE_RTL_COND(rtl_csiar_cond)
4108{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004109 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02004110}
4111
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004112static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004113{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004114 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4115
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004116 RTL_W32(tp, CSIDR, value);
4117 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004118 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004119
Francois Romieuffc46952012-07-06 14:19:23 +02004120 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004121}
4122
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004123static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004124{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004125 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4126
4127 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4128 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004129
Francois Romieuffc46952012-07-06 14:19:23 +02004130 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004131 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004132}
4133
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004134static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004135{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004136 struct pci_dev *pdev = tp->pci_dev;
4137 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004138
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004139 /* According to Realtek the value at config space address 0x070f
4140 * controls the L0s/L1 entrance latency. We try standard ECAM access
4141 * first and if it fails fall back to CSI.
4142 */
4143 if (pdev->cfg_size > 0x070f &&
4144 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4145 return;
4146
4147 netdev_notice_once(tp->dev,
4148 "No native access to PCI extended config space, falling back to CSI\n");
4149 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4150 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004151}
4152
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004153static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08004154{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004155 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02004156}
4157
4158struct ephy_info {
4159 unsigned int offset;
4160 u16 mask;
4161 u16 bits;
4162};
4163
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004164static void __rtl_ephy_init(struct rtl8169_private *tp,
4165 const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004166{
4167 u16 w;
4168
4169 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004170 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4171 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004172 e++;
4173 }
4174}
4175
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004176#define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
4177
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004178static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02004179{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004180 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004181 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004182}
4183
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004184static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004185{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004186 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004187 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004188}
4189
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004190static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
hayeswangb51ecea2014-07-09 14:52:51 +08004191{
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004192 /* work around an issue when PCI reset occurs during L2/L3 state */
4193 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
hayeswangb51ecea2014-07-09 14:52:51 +08004194}
4195
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004196static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4197{
Heiner Kallweit62b1b3b2019-06-18 23:14:50 +02004198 /* Don't enable ASPM in the chip if OS can't control ASPM */
4199 if (enable && tp->aspm_manageable) {
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004200 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
Kai-Heng Feng94235462018-09-12 14:58:20 +08004201 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004202 } else {
4203 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4204 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4205 }
Kai-Heng Feng94235462018-09-12 14:58:20 +08004206
4207 udelay(10);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004208}
4209
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004210static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
4211 u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
4212{
4213 /* Usage of dynamic vs. static FIFO is controlled by bit
4214 * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
4215 */
4216 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
4217 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
4218}
4219
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004220static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
4221 u8 low, u8 high)
4222{
4223 /* FIFO thresholds for pause flow control */
4224 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
4225 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
4226}
4227
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004228static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004229{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004230 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004231
françois romieufaf1e782013-02-27 13:01:57 +00004232 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004233 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00004234 PCI_EXP_DEVCTL_NOSNOOP_EN);
4235 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004236}
4237
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004238static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004239{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004240 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004241
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004242 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004243}
4244
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004245static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004246{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004247 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02004248
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004249 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004250
françois romieufaf1e782013-02-27 13:01:57 +00004251 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004252 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02004253
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004254 rtl_disable_clock_request(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004255}
4256
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004257static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004258{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004259 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004260 { 0x01, 0, 0x0001 },
4261 { 0x02, 0x0800, 0x1000 },
4262 { 0x03, 0, 0x0042 },
4263 { 0x06, 0x0080, 0x0000 },
4264 { 0x07, 0, 0x2000 }
4265 };
4266
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004267 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004268
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004269 rtl_ephy_init(tp, e_info_8168cp);
Francois Romieub726e492008-06-28 12:22:59 +02004270
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004271 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004272}
4273
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004274static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004275{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004276 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004277
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004278 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02004279
françois romieufaf1e782013-02-27 13:01:57 +00004280 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004281 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02004282}
4283
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004284static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004285{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004286 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004287
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004288 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004289
4290 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004291 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004292
françois romieufaf1e782013-02-27 13:01:57 +00004293 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004294 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004295}
4296
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004297static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004298{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004299 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004300 { 0x02, 0x0800, 0x1000 },
4301 { 0x03, 0, 0x0002 },
4302 { 0x06, 0x0080, 0x0000 }
4303 };
4304
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004305 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004306
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004307 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02004308
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004309 rtl_ephy_init(tp, e_info_8168c_1);
Francois Romieub726e492008-06-28 12:22:59 +02004310
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004311 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004312}
4313
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004314static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004315{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004316 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004317 { 0x01, 0, 0x0001 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004318 { 0x03, 0x0400, 0x0020 }
Francois Romieub726e492008-06-28 12:22:59 +02004319 };
4320
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004321 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004322
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004323 rtl_ephy_init(tp, e_info_8168c_2);
Francois Romieub726e492008-06-28 12:22:59 +02004324
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004325 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004326}
4327
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004328static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004329{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004330 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004331}
4332
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004333static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004334{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004335 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004336
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004337 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004338}
4339
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004340static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004341{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004342 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004343
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004344 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004345
françois romieufaf1e782013-02-27 13:01:57 +00004346 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004347 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02004348}
4349
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004350static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004351{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004352 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004353
françois romieufaf1e782013-02-27 13:01:57 +00004354 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004355 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00004356
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004357 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004358}
4359
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004360static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004361{
4362 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004363 { 0x0b, 0x0000, 0x0048 },
4364 { 0x19, 0x0020, 0x0050 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004365 { 0x0c, 0x0100, 0x0020 },
4366 { 0x10, 0x0004, 0x0000 },
françois romieue6de30d2011-01-03 15:08:37 +00004367 };
françois romieue6de30d2011-01-03 15:08:37 +00004368
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004369 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004370
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004371 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00004372
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004373 rtl_ephy_init(tp, e_info_8168d_4);
françois romieue6de30d2011-01-03 15:08:37 +00004374
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004375 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004376}
4377
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004378static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004379{
Hayes Wang70090422011-07-06 15:58:06 +08004380 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004381 { 0x00, 0x0200, 0x0100 },
4382 { 0x00, 0x0000, 0x0004 },
4383 { 0x06, 0x0002, 0x0001 },
4384 { 0x06, 0x0000, 0x0030 },
4385 { 0x07, 0x0000, 0x2000 },
4386 { 0x00, 0x0000, 0x0020 },
4387 { 0x03, 0x5800, 0x2000 },
4388 { 0x03, 0x0000, 0x0001 },
4389 { 0x01, 0x0800, 0x1000 },
4390 { 0x07, 0x0000, 0x4000 },
4391 { 0x1e, 0x0000, 0x2000 },
4392 { 0x19, 0xffff, 0xfe6c },
4393 { 0x0a, 0x0000, 0x0040 }
4394 };
4395
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004396 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004397
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004398 rtl_ephy_init(tp, e_info_8168e_1);
hayeswang01dc7fe2011-03-21 01:50:28 +00004399
françois romieufaf1e782013-02-27 13:01:57 +00004400 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004401 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00004402
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004403 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004404
4405 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004406 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4407 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004408
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004409 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004410}
4411
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004412static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004413{
4414 static const struct ephy_info e_info_8168e_2[] = {
4415 { 0x09, 0x0000, 0x0080 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004416 { 0x19, 0x0000, 0x0224 },
4417 { 0x00, 0x0000, 0x0004 },
4418 { 0x0c, 0x3df0, 0x0200 },
Hayes Wang70090422011-07-06 15:58:06 +08004419 };
4420
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004421 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004422
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004423 rtl_ephy_init(tp, e_info_8168e_2);
Hayes Wang70090422011-07-06 15:58:06 +08004424
françois romieufaf1e782013-02-27 13:01:57 +00004425 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004426 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08004427
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004428 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4429 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004430 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004431 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4432 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004433 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004434 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang70090422011-07-06 15:58:06 +08004435
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004436 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004437
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004438 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08004439
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01004440 rtl8168_config_eee_mac(tp);
4441
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004442 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4443 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4444 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02004445
4446 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08004447}
4448
Hayes Wang5f886e02012-03-30 14:33:03 +08004449static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004450{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004451 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004452
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004453 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08004454
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004455 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4456 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004457 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004458 rtl_reset_packet_filter(tp);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004459 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
4460 rtl_eri_set_bits(tp, 0x1d0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004461 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4462 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
Hayes Wangc2218922011-09-06 16:55:18 +08004463
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004464 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004465
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004466 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
4467 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4468 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4469 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01004470
4471 rtl8168_config_eee_mac(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004472}
4473
Hayes Wang5f886e02012-03-30 14:33:03 +08004474static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4475{
Hayes Wang5f886e02012-03-30 14:33:03 +08004476 static const struct ephy_info e_info_8168f_1[] = {
4477 { 0x06, 0x00c0, 0x0020 },
4478 { 0x08, 0x0001, 0x0002 },
4479 { 0x09, 0x0000, 0x0080 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004480 { 0x19, 0x0000, 0x0224 },
4481 { 0x00, 0x0000, 0x0004 },
4482 { 0x0c, 0x3df0, 0x0200 },
Hayes Wang5f886e02012-03-30 14:33:03 +08004483 };
4484
4485 rtl_hw_start_8168f(tp);
4486
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004487 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wang5f886e02012-03-30 14:33:03 +08004488
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004489 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang5f886e02012-03-30 14:33:03 +08004490}
4491
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004492static void rtl_hw_start_8411(struct rtl8169_private *tp)
4493{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004494 static const struct ephy_info e_info_8168f_1[] = {
4495 { 0x06, 0x00c0, 0x0020 },
4496 { 0x0f, 0xffff, 0x5200 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004497 { 0x19, 0x0000, 0x0224 },
4498 { 0x00, 0x0000, 0x0004 },
4499 { 0x0c, 0x3df0, 0x0200 },
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004500 };
4501
4502 rtl_hw_start_8168f(tp);
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004503 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004504
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004505 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004506
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004507 rtl_eri_set_bits(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004508}
4509
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004510static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08004511{
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004512 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004513 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Hayes Wangc5583862012-07-02 17:23:22 +08004514
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004515 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08004516
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004517 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08004518
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004519 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004520 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
Hayes Wangc5583862012-07-02 17:23:22 +08004521
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004522 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08004523
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004524 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4525 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004526
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004527 rtl8168_config_eee_mac(tp);
4528
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004529 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004530 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
hayeswangb51ecea2014-07-09 14:52:51 +08004531
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004532 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08004533}
4534
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004535static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
4536{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004537 static const struct ephy_info e_info_8168g_1[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004538 { 0x00, 0x0008, 0x0000 },
4539 { 0x0c, 0x3ff0, 0x0820 },
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004540 { 0x1e, 0x0000, 0x0001 },
4541 { 0x19, 0x8000, 0x0000 }
4542 };
4543
4544 rtl_hw_start_8168g(tp);
4545
4546 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004547 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004548 rtl_ephy_init(tp, e_info_8168g_1);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004549 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004550}
4551
hayeswang57538c42013-04-01 22:23:40 +00004552static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
4553{
hayeswang57538c42013-04-01 22:23:40 +00004554 static const struct ephy_info e_info_8168g_2[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004555 { 0x00, 0x0008, 0x0000 },
4556 { 0x0c, 0x3ff0, 0x0820 },
4557 { 0x19, 0xffff, 0x7c00 },
4558 { 0x1e, 0xffff, 0x20eb },
4559 { 0x0d, 0xffff, 0x1666 },
4560 { 0x00, 0xffff, 0x10a3 },
4561 { 0x06, 0xffff, 0xf050 },
4562 { 0x04, 0x0000, 0x0010 },
4563 { 0x1d, 0x4000, 0x0000 },
hayeswang57538c42013-04-01 22:23:40 +00004564 };
4565
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004566 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00004567
4568 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004569 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4570 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004571 rtl_ephy_init(tp, e_info_8168g_2);
hayeswang57538c42013-04-01 22:23:40 +00004572}
4573
hayeswang45dd95c2013-07-08 17:09:01 +08004574static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
4575{
hayeswang45dd95c2013-07-08 17:09:01 +08004576 static const struct ephy_info e_info_8411_2[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004577 { 0x00, 0x0008, 0x0000 },
4578 { 0x0c, 0x37d0, 0x0820 },
4579 { 0x1e, 0x0000, 0x0001 },
4580 { 0x19, 0x8021, 0x0000 },
4581 { 0x1e, 0x0000, 0x2000 },
4582 { 0x0d, 0x0100, 0x0200 },
4583 { 0x00, 0x0000, 0x0080 },
4584 { 0x06, 0x0000, 0x0010 },
4585 { 0x04, 0x0000, 0x0010 },
4586 { 0x1d, 0x0000, 0x4000 },
hayeswang45dd95c2013-07-08 17:09:01 +08004587 };
4588
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004589 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08004590
4591 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004592 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004593 rtl_ephy_init(tp, e_info_8411_2);
Heiner Kallweitfe4e8db02019-07-13 13:45:47 +02004594
4595 /* The following Realtek-provided magic fixes an issue with the RX unit
4596 * getting confused after the PHY having been powered-down.
4597 */
4598 r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
4599 r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
4600 r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
4601 r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
4602 r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
4603 r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
4604 r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
4605 r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
4606 mdelay(3);
4607 r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
4608
4609 r8168_mac_ocp_write(tp, 0xF800, 0xE008);
4610 r8168_mac_ocp_write(tp, 0xF802, 0xE00A);
4611 r8168_mac_ocp_write(tp, 0xF804, 0xE00C);
4612 r8168_mac_ocp_write(tp, 0xF806, 0xE00E);
4613 r8168_mac_ocp_write(tp, 0xF808, 0xE027);
4614 r8168_mac_ocp_write(tp, 0xF80A, 0xE04F);
4615 r8168_mac_ocp_write(tp, 0xF80C, 0xE05E);
4616 r8168_mac_ocp_write(tp, 0xF80E, 0xE065);
4617 r8168_mac_ocp_write(tp, 0xF810, 0xC602);
4618 r8168_mac_ocp_write(tp, 0xF812, 0xBE00);
4619 r8168_mac_ocp_write(tp, 0xF814, 0x0000);
4620 r8168_mac_ocp_write(tp, 0xF816, 0xC502);
4621 r8168_mac_ocp_write(tp, 0xF818, 0xBD00);
4622 r8168_mac_ocp_write(tp, 0xF81A, 0x074C);
4623 r8168_mac_ocp_write(tp, 0xF81C, 0xC302);
4624 r8168_mac_ocp_write(tp, 0xF81E, 0xBB00);
4625 r8168_mac_ocp_write(tp, 0xF820, 0x080A);
4626 r8168_mac_ocp_write(tp, 0xF822, 0x6420);
4627 r8168_mac_ocp_write(tp, 0xF824, 0x48C2);
4628 r8168_mac_ocp_write(tp, 0xF826, 0x8C20);
4629 r8168_mac_ocp_write(tp, 0xF828, 0xC516);
4630 r8168_mac_ocp_write(tp, 0xF82A, 0x64A4);
4631 r8168_mac_ocp_write(tp, 0xF82C, 0x49C0);
4632 r8168_mac_ocp_write(tp, 0xF82E, 0xF009);
4633 r8168_mac_ocp_write(tp, 0xF830, 0x74A2);
4634 r8168_mac_ocp_write(tp, 0xF832, 0x8CA5);
4635 r8168_mac_ocp_write(tp, 0xF834, 0x74A0);
4636 r8168_mac_ocp_write(tp, 0xF836, 0xC50E);
4637 r8168_mac_ocp_write(tp, 0xF838, 0x9CA2);
4638 r8168_mac_ocp_write(tp, 0xF83A, 0x1C11);
4639 r8168_mac_ocp_write(tp, 0xF83C, 0x9CA0);
4640 r8168_mac_ocp_write(tp, 0xF83E, 0xE006);
4641 r8168_mac_ocp_write(tp, 0xF840, 0x74F8);
4642 r8168_mac_ocp_write(tp, 0xF842, 0x48C4);
4643 r8168_mac_ocp_write(tp, 0xF844, 0x8CF8);
4644 r8168_mac_ocp_write(tp, 0xF846, 0xC404);
4645 r8168_mac_ocp_write(tp, 0xF848, 0xBC00);
4646 r8168_mac_ocp_write(tp, 0xF84A, 0xC403);
4647 r8168_mac_ocp_write(tp, 0xF84C, 0xBC00);
4648 r8168_mac_ocp_write(tp, 0xF84E, 0x0BF2);
4649 r8168_mac_ocp_write(tp, 0xF850, 0x0C0A);
4650 r8168_mac_ocp_write(tp, 0xF852, 0xE434);
4651 r8168_mac_ocp_write(tp, 0xF854, 0xD3C0);
4652 r8168_mac_ocp_write(tp, 0xF856, 0x49D9);
4653 r8168_mac_ocp_write(tp, 0xF858, 0xF01F);
4654 r8168_mac_ocp_write(tp, 0xF85A, 0xC526);
4655 r8168_mac_ocp_write(tp, 0xF85C, 0x64A5);
4656 r8168_mac_ocp_write(tp, 0xF85E, 0x1400);
4657 r8168_mac_ocp_write(tp, 0xF860, 0xF007);
4658 r8168_mac_ocp_write(tp, 0xF862, 0x0C01);
4659 r8168_mac_ocp_write(tp, 0xF864, 0x8CA5);
4660 r8168_mac_ocp_write(tp, 0xF866, 0x1C15);
4661 r8168_mac_ocp_write(tp, 0xF868, 0xC51B);
4662 r8168_mac_ocp_write(tp, 0xF86A, 0x9CA0);
4663 r8168_mac_ocp_write(tp, 0xF86C, 0xE013);
4664 r8168_mac_ocp_write(tp, 0xF86E, 0xC519);
4665 r8168_mac_ocp_write(tp, 0xF870, 0x74A0);
4666 r8168_mac_ocp_write(tp, 0xF872, 0x48C4);
4667 r8168_mac_ocp_write(tp, 0xF874, 0x8CA0);
4668 r8168_mac_ocp_write(tp, 0xF876, 0xC516);
4669 r8168_mac_ocp_write(tp, 0xF878, 0x74A4);
4670 r8168_mac_ocp_write(tp, 0xF87A, 0x48C8);
4671 r8168_mac_ocp_write(tp, 0xF87C, 0x48CA);
4672 r8168_mac_ocp_write(tp, 0xF87E, 0x9CA4);
4673 r8168_mac_ocp_write(tp, 0xF880, 0xC512);
4674 r8168_mac_ocp_write(tp, 0xF882, 0x1B00);
4675 r8168_mac_ocp_write(tp, 0xF884, 0x9BA0);
4676 r8168_mac_ocp_write(tp, 0xF886, 0x1B1C);
4677 r8168_mac_ocp_write(tp, 0xF888, 0x483F);
4678 r8168_mac_ocp_write(tp, 0xF88A, 0x9BA2);
4679 r8168_mac_ocp_write(tp, 0xF88C, 0x1B04);
4680 r8168_mac_ocp_write(tp, 0xF88E, 0xC508);
4681 r8168_mac_ocp_write(tp, 0xF890, 0x9BA0);
4682 r8168_mac_ocp_write(tp, 0xF892, 0xC505);
4683 r8168_mac_ocp_write(tp, 0xF894, 0xBD00);
4684 r8168_mac_ocp_write(tp, 0xF896, 0xC502);
4685 r8168_mac_ocp_write(tp, 0xF898, 0xBD00);
4686 r8168_mac_ocp_write(tp, 0xF89A, 0x0300);
4687 r8168_mac_ocp_write(tp, 0xF89C, 0x051E);
4688 r8168_mac_ocp_write(tp, 0xF89E, 0xE434);
4689 r8168_mac_ocp_write(tp, 0xF8A0, 0xE018);
4690 r8168_mac_ocp_write(tp, 0xF8A2, 0xE092);
4691 r8168_mac_ocp_write(tp, 0xF8A4, 0xDE20);
4692 r8168_mac_ocp_write(tp, 0xF8A6, 0xD3C0);
4693 r8168_mac_ocp_write(tp, 0xF8A8, 0xC50F);
4694 r8168_mac_ocp_write(tp, 0xF8AA, 0x76A4);
4695 r8168_mac_ocp_write(tp, 0xF8AC, 0x49E3);
4696 r8168_mac_ocp_write(tp, 0xF8AE, 0xF007);
4697 r8168_mac_ocp_write(tp, 0xF8B0, 0x49C0);
4698 r8168_mac_ocp_write(tp, 0xF8B2, 0xF103);
4699 r8168_mac_ocp_write(tp, 0xF8B4, 0xC607);
4700 r8168_mac_ocp_write(tp, 0xF8B6, 0xBE00);
4701 r8168_mac_ocp_write(tp, 0xF8B8, 0xC606);
4702 r8168_mac_ocp_write(tp, 0xF8BA, 0xBE00);
4703 r8168_mac_ocp_write(tp, 0xF8BC, 0xC602);
4704 r8168_mac_ocp_write(tp, 0xF8BE, 0xBE00);
4705 r8168_mac_ocp_write(tp, 0xF8C0, 0x0C4C);
4706 r8168_mac_ocp_write(tp, 0xF8C2, 0x0C28);
4707 r8168_mac_ocp_write(tp, 0xF8C4, 0x0C2C);
4708 r8168_mac_ocp_write(tp, 0xF8C6, 0xDC00);
4709 r8168_mac_ocp_write(tp, 0xF8C8, 0xC707);
4710 r8168_mac_ocp_write(tp, 0xF8CA, 0x1D00);
4711 r8168_mac_ocp_write(tp, 0xF8CC, 0x8DE2);
4712 r8168_mac_ocp_write(tp, 0xF8CE, 0x48C1);
4713 r8168_mac_ocp_write(tp, 0xF8D0, 0xC502);
4714 r8168_mac_ocp_write(tp, 0xF8D2, 0xBD00);
4715 r8168_mac_ocp_write(tp, 0xF8D4, 0x00AA);
4716 r8168_mac_ocp_write(tp, 0xF8D6, 0xE0C0);
4717 r8168_mac_ocp_write(tp, 0xF8D8, 0xC502);
4718 r8168_mac_ocp_write(tp, 0xF8DA, 0xBD00);
4719 r8168_mac_ocp_write(tp, 0xF8DC, 0x0132);
4720
4721 r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
4722
4723 r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
4724 r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
4725 r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
4726 r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
4727 r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
4728 r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
4729 r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
4730
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004731 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08004732}
4733
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004734static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
4735{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004736 static const struct ephy_info e_info_8168h_1[] = {
4737 { 0x1e, 0x0800, 0x0001 },
4738 { 0x1d, 0x0000, 0x0800 },
4739 { 0x05, 0xffff, 0x2089 },
4740 { 0x06, 0xffff, 0x5881 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004741 { 0x04, 0xffff, 0x854a },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004742 { 0x01, 0xffff, 0x068b }
4743 };
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004744 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004745
4746 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004747 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004748 rtl_ephy_init(tp, e_info_8168h_1);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004749
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004750 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004751 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004752
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004753 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004754
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004755 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004756
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004757 rtl_reset_packet_filter(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004758
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004759 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_1111, BIT(4));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004760
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004761 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004762
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004763 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004764
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004765 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004766
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004767 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4768 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004769
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004770 rtl8168_config_eee_mac(tp);
4771
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004772 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4773 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004774
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004775 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004776
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004777 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004778
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004779 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004780
4781 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08004782 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004783 rtl_writephy(tp, 0x1f, 0x0000);
4784 if (rg_saw_cnt > 0) {
4785 u16 sw_cnt_1ms_ini;
4786
4787 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
4788 sw_cnt_1ms_ini &= 0x0fff;
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004789 r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004790 }
4791
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004792 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
4793 r8168_mac_ocp_modify(tp, 0xe052, 0x6000, 0x8008);
4794 r8168_mac_ocp_modify(tp, 0xe0d6, 0x01ff, 0x017f);
4795 r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004796
4797 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
4798 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
4799 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
4800 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004801
4802 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004803}
4804
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004805static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
4806{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08004807 rtl8168ep_stop_cmac(tp);
4808
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004809 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004810 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004811
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004812 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004813
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004814 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004815
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004816 rtl_reset_packet_filter(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004817
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004818 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f80);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004819
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004820 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004821
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004822 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004823
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004824 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4825 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004826
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004827 rtl8168_config_eee_mac(tp);
4828
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004829 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004830
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004831 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004832
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004833 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004834}
4835
4836static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
4837{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004838 static const struct ephy_info e_info_8168ep_1[] = {
4839 { 0x00, 0xffff, 0x10ab },
4840 { 0x06, 0xffff, 0xf030 },
4841 { 0x08, 0xffff, 0x2006 },
4842 { 0x0d, 0xffff, 0x1666 },
4843 { 0x0c, 0x3ff0, 0x0000 }
4844 };
4845
4846 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004847 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004848 rtl_ephy_init(tp, e_info_8168ep_1);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004849
4850 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004851
4852 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004853}
4854
4855static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
4856{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004857 static const struct ephy_info e_info_8168ep_2[] = {
4858 { 0x00, 0xffff, 0x10a3 },
4859 { 0x19, 0xffff, 0xfc00 },
4860 { 0x1e, 0xffff, 0x20ea }
4861 };
4862
4863 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004864 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004865 rtl_ephy_init(tp, e_info_8168ep_2);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004866
4867 rtl_hw_start_8168ep(tp);
4868
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004869 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4870 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004871
4872 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004873}
4874
4875static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
4876{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004877 static const struct ephy_info e_info_8168ep_3[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004878 { 0x00, 0x0000, 0x0080 },
4879 { 0x0d, 0x0100, 0x0200 },
4880 { 0x19, 0x8021, 0x0000 },
4881 { 0x1e, 0x0000, 0x2000 },
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004882 };
4883
4884 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004885 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004886 rtl_ephy_init(tp, e_info_8168ep_3);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004887
4888 rtl_hw_start_8168ep(tp);
4889
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004890 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4891 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004892
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004893 r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x0271);
4894 r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
4895 r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004896
4897 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004898}
4899
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004900static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004901{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004902 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004903 { 0x01, 0, 0x6e65 },
4904 { 0x02, 0, 0x091f },
4905 { 0x03, 0, 0xc2f9 },
4906 { 0x06, 0, 0xafb5 },
4907 { 0x07, 0, 0x0e00 },
4908 { 0x19, 0, 0xec80 },
4909 { 0x01, 0, 0x2e65 },
4910 { 0x01, 0, 0x6e65 }
4911 };
4912 u8 cfg1;
4913
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004914 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004915
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004916 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004917
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004918 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004919
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004920 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02004921 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004922 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004923
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004924 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004925 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004926 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004927
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004928 rtl_ephy_init(tp, e_info_8102e_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004929}
4930
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004931static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004932{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004933 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004934
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004935 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004936
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004937 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
4938 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004939}
4940
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004941static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004942{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004943 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004944
Francois Romieufdf6fc02012-07-06 22:40:38 +02004945 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004946}
4947
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004948static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08004949{
4950 static const struct ephy_info e_info_8105e_1[] = {
4951 { 0x07, 0, 0x4000 },
4952 { 0x19, 0, 0x0200 },
4953 { 0x19, 0, 0x0020 },
4954 { 0x1e, 0, 0x2000 },
4955 { 0x03, 0, 0x0001 },
4956 { 0x19, 0, 0x0100 },
4957 { 0x19, 0, 0x0004 },
4958 { 0x0a, 0, 0x0020 }
4959 };
4960
Francois Romieucecb5fd2011-04-01 10:21:07 +02004961 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004962 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004963
Francois Romieucecb5fd2011-04-01 10:21:07 +02004964 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004965 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004966
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004967 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
4968 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004969
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004970 rtl_ephy_init(tp, e_info_8105e_1);
hayeswangb51ecea2014-07-09 14:52:51 +08004971
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004972 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004973}
4974
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004975static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08004976{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004977 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02004978 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004979}
4980
Hayes Wang7e18dca2012-03-30 14:33:02 +08004981static void rtl_hw_start_8402(struct rtl8169_private *tp)
4982{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004983 static const struct ephy_info e_info_8402[] = {
4984 { 0x19, 0xffff, 0xff64 },
4985 { 0x1e, 0, 0x4000 }
4986 };
4987
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004988 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004989
4990 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004991 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004992
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004993 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004994
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004995 rtl_ephy_init(tp, e_info_8402);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004996
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004997 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004998
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004999 rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02005000 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005001 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5002 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
5003 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00);
hayeswangb51ecea2014-07-09 14:52:51 +08005004
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005005 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005006}
5007
Hayes Wang5598bfe2012-07-02 17:23:21 +08005008static void rtl_hw_start_8106(struct rtl8169_private *tp)
5009{
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005010 rtl_hw_aspm_clkreq_enable(tp, false);
5011
Hayes Wang5598bfe2012-07-02 17:23:21 +08005012 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005013 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005014
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005015 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5016 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5017 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005018
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005019 rtl_pcie_state_l2l3_disable(tp);
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005020 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005021}
5022
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005023static void rtl_hw_config(struct rtl8169_private *tp)
5024{
5025 static const rtl_generic_fct hw_configs[] = {
5026 [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
5027 [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
5028 [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
5029 [RTL_GIGA_MAC_VER_10] = NULL,
5030 [RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168bb,
5031 [RTL_GIGA_MAC_VER_12] = rtl_hw_start_8168bef,
5032 [RTL_GIGA_MAC_VER_13] = NULL,
5033 [RTL_GIGA_MAC_VER_14] = NULL,
5034 [RTL_GIGA_MAC_VER_15] = NULL,
5035 [RTL_GIGA_MAC_VER_16] = NULL,
5036 [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168bef,
5037 [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
5038 [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
5039 [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
5040 [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_3,
5041 [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
5042 [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
5043 [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
5044 [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
5045 [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
5046 [RTL_GIGA_MAC_VER_27] = rtl_hw_start_8168d,
5047 [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
5048 [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
5049 [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
5050 [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168dp,
5051 [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
5052 [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
5053 [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
5054 [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
5055 [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
5056 [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
5057 [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
5058 [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
5059 [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
5060 [RTL_GIGA_MAC_VER_41] = rtl_hw_start_8168g_1,
5061 [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
5062 [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
5063 [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
5064 [RTL_GIGA_MAC_VER_45] = rtl_hw_start_8168h_1,
5065 [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
5066 [RTL_GIGA_MAC_VER_47] = rtl_hw_start_8168h_1,
5067 [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
5068 [RTL_GIGA_MAC_VER_49] = rtl_hw_start_8168ep_1,
5069 [RTL_GIGA_MAC_VER_50] = rtl_hw_start_8168ep_2,
5070 [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
5071 };
5072
5073 if (hw_configs[tp->mac_version])
5074 hw_configs[tp->mac_version](tp);
5075}
5076
5077static void rtl_hw_start_8168(struct rtl8169_private *tp)
5078{
Francois Romieucecb5fd2011-04-01 10:21:07 +02005079 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005080 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005081 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005082 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005083
Heiner Kallweit272b2262019-06-14 07:55:21 +02005084 if (rtl_is_8168evl_up(tp))
5085 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5086 else
5087 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005088
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005089 rtl_hw_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090}
5091
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005092static void rtl_hw_start_8169(struct rtl8169_private *tp)
5093{
5094 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
5095 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
5096
5097 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
5098
5099 tp->cp_cmd |= PCIMulRW;
5100
5101 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5102 tp->mac_version == RTL_GIGA_MAC_VER_03) {
5103 netif_dbg(tp, drv, tp->dev,
5104 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
5105 tp->cp_cmd |= (1 << 14);
5106 }
5107
5108 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5109
5110 rtl8169_set_magic_reg(tp, tp->mac_version);
5111
5112 RTL_W32(tp, RxMissed, 0);
5113}
5114
5115static void rtl_hw_start(struct rtl8169_private *tp)
5116{
5117 rtl_unlock_config_regs(tp);
5118
5119 tp->cp_cmd &= CPCMD_MASK;
5120 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5121
5122 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
5123 rtl_hw_start_8169(tp);
5124 else
5125 rtl_hw_start_8168(tp);
5126
5127 rtl_set_rx_max_size(tp);
5128 rtl_set_rx_tx_desc_registers(tp);
5129 rtl_lock_config_regs(tp);
5130
5131 /* disable interrupt coalescing */
5132 RTL_W16(tp, IntrMitigate, 0x0000);
5133 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5134 RTL_R8(tp, IntrMask);
5135 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5136 rtl_init_rxcfg(tp);
5137 rtl_set_tx_config_registers(tp);
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005138 rtl_set_rx_mode(tp->dev);
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005139 rtl_irq_enable(tp);
5140}
5141
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5143{
Francois Romieud58d46b2011-05-03 16:38:29 +02005144 struct rtl8169_private *tp = netdev_priv(dev);
5145
Francois Romieud58d46b2011-05-03 16:38:29 +02005146 if (new_mtu > ETH_DATA_LEN)
5147 rtl_hw_jumbo_enable(tp);
5148 else
5149 rtl_hw_jumbo_disable(tp);
5150
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005152 netdev_update_features(dev);
5153
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155}
5156
5157static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5158{
Al Viro95e09182007-12-22 18:55:39 +00005159 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5161}
5162
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005163static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164{
5165 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5166
Alexander Duycka0750132014-12-11 15:02:17 -08005167 /* Force memory writes to complete before releasing descriptor */
5168 dma_wmb();
5169
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005170 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171}
5172
Heiner Kallweit32879f02019-08-07 21:38:22 +02005173static struct page *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5174 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005175{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005176 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02005177 int node = dev_to_node(d);
Heiner Kallweit32879f02019-08-07 21:38:22 +02005178 dma_addr_t mapping;
5179 struct page *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
Heiner Kallweit32879f02019-08-07 21:38:22 +02005181 data = alloc_pages_node(node, GFP_KERNEL, get_order(R8169_RX_BUF_SIZE));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005182 if (!data)
5183 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005184
Heiner Kallweit32879f02019-08-07 21:38:22 +02005185 mapping = dma_map_page(d, data, 0, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005186 if (unlikely(dma_mapping_error(d, mapping))) {
5187 if (net_ratelimit())
5188 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Heiner Kallweit32879f02019-08-07 21:38:22 +02005189 __free_pages(data, get_order(R8169_RX_BUF_SIZE));
5190 return NULL;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
Heiner Kallweitd731af72018-04-17 23:26:41 +02005193 desc->addr = cpu_to_le64(mapping);
5194 rtl8169_mark_to_asic(desc);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005195
Heiner Kallweit32879f02019-08-07 21:38:22 +02005196 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197}
5198
5199static void rtl8169_rx_clear(struct rtl8169_private *tp)
5200{
Francois Romieu07d3f512007-02-21 22:40:46 +01005201 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
Heiner Kallweiteb2e7f02019-08-09 22:59:07 +02005203 for (i = 0; i < NUM_RX_DESC && tp->Rx_databuff[i]; i++) {
5204 dma_unmap_page(tp_to_dev(tp),
5205 le64_to_cpu(tp->RxDescArray[i].addr),
5206 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5207 __free_pages(tp->Rx_databuff[i], get_order(R8169_RX_BUF_SIZE));
5208 tp->Rx_databuff[i] = NULL;
5209 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 }
5211}
5212
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005213static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005215 desc->opts1 |= cpu_to_le32(RingEnd);
5216}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005217
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005218static int rtl8169_rx_fill(struct rtl8169_private *tp)
5219{
5220 unsigned int i;
5221
5222 for (i = 0; i < NUM_RX_DESC; i++) {
Heiner Kallweit32879f02019-08-07 21:38:22 +02005223 struct page *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005224
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005225 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005226 if (!data) {
5227 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005228 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005229 }
5230 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005233 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5234 return 0;
5235
5236err_out:
5237 rtl8169_rx_clear(tp);
5238 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239}
5240
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005241static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 rtl8169_init_ring_indexes(tp);
5244
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005245 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5246 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005248 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249}
5250
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005251static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 struct TxDesc *desc)
5253{
5254 unsigned int len = tx_skb->len;
5255
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005256 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5257
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 desc->opts1 = 0x00;
5259 desc->opts2 = 0x00;
5260 desc->addr = 0x00;
5261 tx_skb->len = 0;
5262}
5263
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005264static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5265 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
5267 unsigned int i;
5268
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005269 for (i = 0; i < n; i++) {
5270 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 struct ring_info *tx_skb = tp->tx_skb + entry;
5272 unsigned int len = tx_skb->len;
5273
5274 if (len) {
5275 struct sk_buff *skb = tx_skb->skb;
5276
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005277 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 tp->TxDescArray + entry);
5279 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07005280 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 tx_skb->skb = NULL;
5282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 }
5284 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005285}
5286
5287static void rtl8169_tx_clear(struct rtl8169_private *tp)
5288{
5289 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 tp->cur_tx = tp->dirty_tx = 0;
Florian Westphald92060b2018-10-20 12:25:27 +02005291 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292}
5293
Francois Romieu4422bcd2012-01-26 11:23:32 +01005294static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295{
David Howellsc4028952006-11-22 14:57:56 +00005296 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005297 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
Francois Romieuda78dbf2012-01-26 14:18:23 +01005299 napi_disable(&tp->napi);
5300 netif_stop_queue(dev);
Paul E. McKenney16f11502018-11-05 17:07:39 -08005301 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
françois romieuc7c2c392011-12-04 20:30:52 +00005303 rtl8169_hw_reset(tp);
5304
Francois Romieu56de4142011-03-15 17:29:31 +01005305 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005306 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01005307
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005309 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Francois Romieuda78dbf2012-01-26 14:18:23 +01005311 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005312 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005313 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314}
5315
5316static void rtl8169_tx_timeout(struct net_device *dev)
5317{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005318 struct rtl8169_private *tp = netdev_priv(dev);
5319
5320 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321}
5322
Heiner Kallweit734c1402018-11-22 21:56:48 +01005323static __le32 rtl8169_get_txd_opts1(u32 opts0, u32 len, unsigned int entry)
5324{
5325 u32 status = opts0 | len;
5326
5327 if (entry == NUM_TX_DESC - 1)
5328 status |= RingEnd;
5329
5330 return cpu_to_le32(status);
5331}
5332
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005334 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335{
5336 struct skb_shared_info *info = skb_shinfo(skb);
5337 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005338 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005339 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
5341 entry = tp->cur_tx;
5342 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005343 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005345 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 void *addr;
5347
5348 entry = (entry + 1) % NUM_TX_DESC;
5349
5350 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005351 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005352 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005353 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005354 if (unlikely(dma_mapping_error(d, mapping))) {
5355 if (net_ratelimit())
5356 netif_err(tp, drv, tp->dev,
5357 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005358 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
Heiner Kallweit734c1402018-11-22 21:56:48 +01005361 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005362 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 txd->addr = cpu_to_le64(mapping);
5364
5365 tp->tx_skb[entry].len = len;
5366 }
5367
5368 if (cur_frag) {
5369 tp->tx_skb[entry].skb = skb;
5370 txd->opts1 |= cpu_to_le32(LastFrag);
5371 }
5372
5373 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005374
5375err_out:
5376 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5377 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378}
5379
françois romieub423e9a2013-05-18 01:24:46 +00005380static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5381{
5382 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5383}
5384
hayeswange9746042014-07-11 16:25:58 +08005385/* msdn_giant_send_check()
5386 * According to the document of microsoft, the TCP Pseudo Header excludes the
5387 * packet length for IPv6 TCP large packets.
5388 */
5389static int msdn_giant_send_check(struct sk_buff *skb)
5390{
5391 const struct ipv6hdr *ipv6h;
5392 struct tcphdr *th;
5393 int ret;
5394
5395 ret = skb_cow_head(skb, 0);
5396 if (ret)
5397 return ret;
5398
5399 ipv6h = ipv6_hdr(skb);
5400 th = tcp_hdr(skb);
5401
5402 th->check = 0;
5403 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
5404
5405 return ret;
5406}
5407
Heiner Kallweit87945b62019-05-31 19:55:11 +02005408static void rtl8169_tso_csum_v1(struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409{
Michał Mirosław350fb322011-04-08 06:35:56 +00005410 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
Francois Romieu2b7b4312011-04-18 22:53:24 -07005412 if (mss) {
5413 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08005414 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5415 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5416 const struct iphdr *ip = ip_hdr(skb);
5417
5418 if (ip->protocol == IPPROTO_TCP)
5419 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5420 else if (ip->protocol == IPPROTO_UDP)
5421 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5422 else
5423 WARN_ON_ONCE(1);
5424 }
hayeswang5888d3f2014-07-11 16:25:56 +08005425}
5426
5427static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5428 struct sk_buff *skb, u32 *opts)
5429{
hayeswangbdfa4ed2014-07-11 16:25:57 +08005430 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08005431 u32 mss = skb_shinfo(skb)->gso_size;
5432
5433 if (mss) {
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005434 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005435 case htons(ETH_P_IP):
5436 opts[0] |= TD1_GTSENV4;
5437 break;
5438
5439 case htons(ETH_P_IPV6):
5440 if (msdn_giant_send_check(skb))
5441 return false;
5442
5443 opts[0] |= TD1_GTSENV6;
5444 break;
5445
5446 default:
5447 WARN_ON_ONCE(1);
5448 break;
5449 }
5450
hayeswangbdfa4ed2014-07-11 16:25:57 +08005451 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08005452 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005453 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08005454 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005456 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005457 case htons(ETH_P_IP):
5458 opts[1] |= TD1_IPv4_CS;
5459 ip_protocol = ip_hdr(skb)->protocol;
5460 break;
5461
5462 case htons(ETH_P_IPV6):
5463 opts[1] |= TD1_IPv6_CS;
5464 ip_protocol = ipv6_hdr(skb)->nexthdr;
5465 break;
5466
5467 default:
5468 ip_protocol = IPPROTO_RAW;
5469 break;
5470 }
5471
5472 if (ip_protocol == IPPROTO_TCP)
5473 opts[1] |= TD1_TCP_CS;
5474 else if (ip_protocol == IPPROTO_UDP)
5475 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005476 else
5477 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08005478
5479 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00005480 } else {
5481 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08005482 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 }
hayeswang5888d3f2014-07-11 16:25:56 +08005484
françois romieub423e9a2013-05-18 01:24:46 +00005485 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486}
5487
Heiner Kallweit76085c92018-11-22 22:03:08 +01005488static bool rtl_tx_slots_avail(struct rtl8169_private *tp,
5489 unsigned int nr_frags)
5490{
5491 unsigned int slots_avail = tp->dirty_tx + NUM_TX_DESC - tp->cur_tx;
5492
5493 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
5494 return slots_avail > nr_frags;
5495}
5496
Heiner Kallweit87945b62019-05-31 19:55:11 +02005497/* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
5498static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
5499{
5500 switch (tp->mac_version) {
5501 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
5502 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
5503 return false;
5504 default:
5505 return true;
5506 }
5507}
5508
Stephen Hemminger613573252009-08-31 19:50:58 +00005509static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5510 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511{
5512 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005513 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005515 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005517 u32 opts[2], len;
Heiner Kallweitef143582019-07-28 11:25:19 +02005518 bool stop_queue;
5519 bool door_bell;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005520 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005521
Heiner Kallweit76085c92018-11-22 22:03:08 +01005522 if (unlikely(!rtl_tx_slots_avail(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005523 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005524 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 }
5526
5527 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005528 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Heiner Kallweit355f9482019-06-06 07:49:17 +02005530 opts[1] = rtl8169_tx_vlan_tag(skb);
françois romieub423e9a2013-05-18 01:24:46 +00005531 opts[0] = DescOwn;
5532
Heiner Kallweit87945b62019-05-31 19:55:11 +02005533 if (rtl_chip_supports_csum_v2(tp)) {
Heiner Kallweit96ea7722019-07-26 21:50:34 +02005534 if (!rtl8169_tso_csum_v2(tp, skb, opts))
5535 goto err_dma_0;
Heiner Kallweit87945b62019-05-31 19:55:11 +02005536 } else {
5537 rtl8169_tso_csum_v1(skb, opts);
hayeswange9746042014-07-11 16:25:58 +08005538 }
françois romieub423e9a2013-05-18 01:24:46 +00005539
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005540 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005541 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005542 if (unlikely(dma_mapping_error(d, mapping))) {
5543 if (net_ratelimit())
5544 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005545 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
5548 tp->tx_skb[entry].len = len;
5549 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
Francois Romieu2b7b4312011-04-18 22:53:24 -07005551 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005552 if (frags < 0)
5553 goto err_dma_1;
5554 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005555 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005556 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005557 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005558 tp->tx_skb[entry].skb = skb;
5559 }
5560
Francois Romieu2b7b4312011-04-18 22:53:24 -07005561 txd->opts2 = cpu_to_le32(opts[1]);
5562
Richard Cochran5047fb52012-03-10 07:29:42 +00005563 skb_tx_timestamp(skb);
5564
Alexander Duycka0750132014-12-11 15:02:17 -08005565 /* Force memory writes to complete before releasing descriptor */
5566 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
Heiner Kallweitef143582019-07-28 11:25:19 +02005568 door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more());
5569
Heiner Kallweit734c1402018-11-22 21:56:48 +01005570 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
Alexander Duycka0750132014-12-11 15:02:17 -08005572 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00005573 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
Alexander Duycka0750132014-12-11 15:02:17 -08005575 tp->cur_tx += frags + 1;
5576
Heiner Kallweitef143582019-07-28 11:25:19 +02005577 stop_queue = !rtl_tx_slots_avail(tp, MAX_SKB_FRAGS);
5578 if (unlikely(stop_queue)) {
Heiner Kallweit0255d592019-02-10 15:28:04 +01005579 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5580 * not miss a ring update when it notices a stopped queue.
5581 */
5582 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 netif_stop_queue(dev);
Heiner Kallweit4773f9b2019-08-12 20:47:40 +02005584 door_bell = true;
Heiner Kallweitef143582019-07-28 11:25:19 +02005585 }
5586
5587 if (door_bell)
5588 RTL_W8(tp, TxPoll, NPQ);
5589
5590 if (unlikely(stop_queue)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005591 /* Sync with rtl_tx:
5592 * - publish queue status and cur_tx ring index (write barrier)
5593 * - refresh dirty_tx ring index (read barrier).
5594 * May the current thread have a pessimistic view of the ring
5595 * status and forget to wake up queue, a racing rtl_tx thread
5596 * can't.
5597 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005598 smp_mb();
Heiner Kallweit76085c92018-11-22 22:03:08 +01005599 if (rtl_tx_slots_avail(tp, MAX_SKB_FRAGS))
Heiner Kallweit601ed4d2019-03-21 21:41:48 +01005600 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 }
5602
Stephen Hemminger613573252009-08-31 19:50:58 +00005603 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005605err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005606 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005607err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07005608 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005609 dev->stats.tx_dropped++;
5610 return NETDEV_TX_OK;
5611
5612err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005614 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005615 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616}
5617
Heiner Kallweite64e0c82019-07-26 21:49:22 +02005618static netdev_features_t rtl8169_features_check(struct sk_buff *skb,
5619 struct net_device *dev,
5620 netdev_features_t features)
5621{
5622 int transport_offset = skb_transport_offset(skb);
5623 struct rtl8169_private *tp = netdev_priv(dev);
5624
5625 if (skb_is_gso(skb)) {
5626 if (transport_offset > GTTCPHO_MAX &&
5627 rtl_chip_supports_csum_v2(tp))
5628 features &= ~NETIF_F_ALL_TSO;
5629 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5630 if (skb->len < ETH_ZLEN) {
5631 switch (tp->mac_version) {
5632 case RTL_GIGA_MAC_VER_11:
5633 case RTL_GIGA_MAC_VER_12:
5634 case RTL_GIGA_MAC_VER_17:
5635 case RTL_GIGA_MAC_VER_34:
5636 features &= ~NETIF_F_CSUM_MASK;
5637 break;
5638 default:
5639 break;
5640 }
5641 }
5642
5643 if (transport_offset > TCPHO_MAX &&
5644 rtl_chip_supports_csum_v2(tp))
5645 features &= ~NETIF_F_CSUM_MASK;
5646 }
5647
5648 return vlan_features_check(skb, features);
5649}
5650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651static void rtl8169_pcierr_interrupt(struct net_device *dev)
5652{
5653 struct rtl8169_private *tp = netdev_priv(dev);
5654 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 u16 pci_status, pci_cmd;
5656
5657 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5658 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5659
Joe Perchesbf82c182010-02-09 11:49:50 +00005660 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5661 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
5663 /*
5664 * The recovery sequence below admits a very elaborated explanation:
5665 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005666 * - I did not see what else could be done;
5667 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 *
5669 * Feel free to adjust to your needs.
5670 */
Francois Romieua27993f2006-12-18 00:04:19 +01005671 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005672 pci_cmd &= ~PCI_COMMAND_PARITY;
5673 else
5674 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5675
5676 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
5678 pci_write_config_word(pdev, PCI_STATUS,
5679 pci_status & (PCI_STATUS_DETECTED_PARITY |
5680 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5681 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5682
Francois Romieu98ddf982012-01-31 10:47:34 +01005683 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684}
5685
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005686static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
5687 int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688{
Florian Westphald92060b2018-10-20 12:25:27 +02005689 unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 dirty_tx = tp->dirty_tx;
5692 smp_rmb();
5693 tx_left = tp->cur_tx - dirty_tx;
5694
5695 while (tx_left > 0) {
5696 unsigned int entry = dirty_tx % NUM_TX_DESC;
5697 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 u32 status;
5699
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5701 if (status & DescOwn)
5702 break;
5703
Alexander Duycka0750132014-12-11 15:02:17 -08005704 /* This barrier is needed to keep us from reading
5705 * any other fields out of the Tx descriptor until
5706 * we know the status of DescOwn
5707 */
5708 dma_rmb();
5709
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005710 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005711 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 if (status & LastFrag) {
Florian Westphald92060b2018-10-20 12:25:27 +02005713 pkts_compl++;
5714 bytes_compl += tx_skb->skb->len;
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005715 napi_consume_skb(tx_skb->skb, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 tx_skb->skb = NULL;
5717 }
5718 dirty_tx++;
5719 tx_left--;
5720 }
5721
5722 if (tp->dirty_tx != dirty_tx) {
Florian Westphald92060b2018-10-20 12:25:27 +02005723 netdev_completed_queue(dev, pkts_compl, bytes_compl);
5724
5725 u64_stats_update_begin(&tp->tx_stats.syncp);
5726 tp->tx_stats.packets += pkts_compl;
5727 tp->tx_stats.bytes += bytes_compl;
5728 u64_stats_update_end(&tp->tx_stats.syncp);
5729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005731 /* Sync with rtl8169_start_xmit:
5732 * - publish dirty_tx ring index (write barrier)
5733 * - refresh cur_tx ring index and queue status (read barrier)
5734 * May the current thread miss the stopped queue condition,
5735 * a racing xmit thread can only have a right view of the
5736 * ring status.
5737 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005738 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 if (netif_queue_stopped(dev) &&
Heiner Kallweit76085c92018-11-22 22:03:08 +01005740 rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 netif_wake_queue(dev);
5742 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005743 /*
5744 * 8168 hack: TxPoll requests are lost when the Tx packets are
5745 * too close. Let's kick an extra TxPoll request when a burst
5746 * of start_xmit activity is detected (if it is not detected,
5747 * it is slow enough). -- FR
5748 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005749 if (tp->cur_tx != dirty_tx)
5750 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 }
5752}
5753
Francois Romieu126fa4b2005-05-12 20:09:17 -04005754static inline int rtl8169_fragmented_frame(u32 status)
5755{
5756 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5757}
5758
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005759static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 u32 status = opts1 & RxProtoMask;
5762
5763 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005764 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 skb->ip_summed = CHECKSUM_UNNECESSARY;
5766 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005767 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768}
5769
Francois Romieuda78dbf2012-01-26 14:18:23 +01005770static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771{
5772 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005773 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776
Timo Teräs9fba0812013-01-15 21:01:24 +00005777 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 unsigned int entry = cur_rx % NUM_RX_DESC;
Heiner Kallweit32879f02019-08-07 21:38:22 +02005779 const void *rx_buf = page_address(tp->Rx_databuff[entry]);
Francois Romieu126fa4b2005-05-12 20:09:17 -04005780 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 u32 status;
5782
Heiner Kallweit62028062018-04-17 23:30:29 +02005783 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 if (status & DescOwn)
5785 break;
Alexander Duycka0750132014-12-11 15:02:17 -08005786
5787 /* This barrier is needed to keep us from reading
5788 * any other fields out of the Rx descriptor until
5789 * we know the status of DescOwn
5790 */
5791 dma_rmb();
5792
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005793 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005794 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5795 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005796 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005798 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005800 dev->stats.rx_crc_errors++;
Heiner Kallweite9588eb2019-05-25 21:14:39 +02005801 if (status & (RxRUNT | RxCRC) && !(status & RxRWT) &&
5802 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00005803 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02005804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 } else {
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005806 unsigned int pkt_size;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005807 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005808
5809process_pkt:
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005810 pkt_size = status & GENMASK(13, 0);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005811 if (likely(!(dev->features & NETIF_F_RXFCS)))
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005812 pkt_size -= ETH_FCS_LEN;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005813 /*
5814 * The driver does not support incoming fragmented
5815 * frames. They are seen as a symptom of over-mtu
5816 * sized frames.
5817 */
5818 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005819 dev->stats.rx_dropped++;
5820 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00005821 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005822 }
5823
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005824 skb = napi_alloc_skb(&tp->napi, pkt_size);
5825 if (unlikely(!skb)) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005826 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00005827 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 }
5829
Heiner Kallweit3c95e502019-08-26 22:52:36 +02005830 dma_sync_single_for_cpu(tp_to_dev(tp),
5831 le64_to_cpu(desc->addr),
5832 pkt_size, DMA_FROM_DEVICE);
Heiner Kallweit32879f02019-08-07 21:38:22 +02005833 prefetch(rx_buf);
5834 skb_copy_to_linear_data(skb, rx_buf, pkt_size);
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005835 skb->tail += pkt_size;
5836 skb->len = pkt_size;
5837
Heiner Kallweitd4ed7462019-08-23 20:07:26 +02005838 dma_sync_single_for_device(tp_to_dev(tp),
5839 le64_to_cpu(desc->addr),
5840 pkt_size, DMA_FROM_DEVICE);
5841
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005842 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 skb->protocol = eth_type_trans(skb, dev);
5844
Francois Romieu7a8fc772011-03-01 17:18:33 +01005845 rtl8169_rx_vlan_tag(desc, skb);
5846
françois romieu39174292015-11-11 23:35:18 +01005847 if (skb->pkt_type == PACKET_MULTICAST)
5848 dev->stats.multicast++;
5849
Heiner Kallweit448a2412019-04-03 19:54:12 +02005850 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
Junchang Wang8027aa22012-03-04 23:30:32 +01005852 u64_stats_update_begin(&tp->rx_stats.syncp);
5853 tp->rx_stats.packets++;
5854 tp->rx_stats.bytes += pkt_size;
5855 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 }
françois romieuce11ff52013-01-24 13:30:06 +00005857release_descriptor:
5858 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005859 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 }
5861
5862 count = cur_rx - tp->cur_rx;
5863 tp->cur_rx = cur_rx;
5864
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 return count;
5866}
5867
Francois Romieu07d3f512007-02-21 22:40:46 +01005868static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02005870 struct rtl8169_private *tp = dev_instance;
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02005871 u32 status = rtl_get_events(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02005873 if (!tp->irq_enabled || (status & 0xffff) == 0xffff ||
5874 !(status & tp->irq_mask))
Heiner Kallweit05bbe552018-08-10 22:38:29 +02005875 return IRQ_NONE;
françois romieu811fd302011-12-04 20:30:45 +00005876
Heiner Kallweit38caff52018-10-18 22:19:28 +02005877 if (unlikely(status & SYSErr)) {
5878 rtl8169_pcierr_interrupt(tp->dev);
5879 goto out;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005880 }
5881
Heiner Kallweit703732f2019-01-19 22:07:05 +01005882 if (status & LinkChg)
5883 phy_mac_interrupt(tp->phydev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005884
Heiner Kallweit38caff52018-10-18 22:19:28 +02005885 if (unlikely(status & RxFIFOOver &&
5886 tp->mac_version == RTL_GIGA_MAC_VER_11)) {
5887 netif_stop_queue(tp->dev);
5888 /* XXX - Hack alert. See rtl_task(). */
5889 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
5890 }
5891
Heiner Kallweite62b2fd2019-04-14 11:48:39 +02005892 rtl_irq_disable(tp);
5893 napi_schedule_irqoff(&tp->napi);
Heiner Kallweit38caff52018-10-18 22:19:28 +02005894out:
5895 rtl_ack_events(tp, status);
5896
5897 return IRQ_HANDLED;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005898}
5899
Francois Romieu4422bcd2012-01-26 11:23:32 +01005900static void rtl_task(struct work_struct *work)
5901{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005902 static const struct {
5903 int bitnr;
5904 void (*action)(struct rtl8169_private *);
5905 } rtl_work[] = {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005906 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01005907 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005908 struct rtl8169_private *tp =
5909 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005910 struct net_device *dev = tp->dev;
5911 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005912
Francois Romieuda78dbf2012-01-26 14:18:23 +01005913 rtl_lock_work(tp);
5914
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005915 if (!netif_running(dev) ||
5916 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005917 goto out_unlock;
5918
5919 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5920 bool pending;
5921
Francois Romieuda78dbf2012-01-26 14:18:23 +01005922 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005923 if (pending)
5924 rtl_work[i].action(tp);
5925 }
5926
5927out_unlock:
5928 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005929}
5930
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005931static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005933 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5934 struct net_device *dev = tp->dev;
Heiner Kallweit6b839b62018-10-18 19:56:01 +02005935 int work_done;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005936
Heiner Kallweit6b839b62018-10-18 19:56:01 +02005937 work_done = rtl_rx(dev, tp, (u32) budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005938
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005939 rtl_tx(dev, tp, budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005940
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005941 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08005942 napi_complete_done(napi, work_done);
Heiner Kallweitfe716f82018-11-19 22:31:32 +01005943 rtl_irq_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 }
5945
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005946 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005949static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02005950{
5951 struct rtl8169_private *tp = netdev_priv(dev);
5952
5953 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5954 return;
5955
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005956 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
5957 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02005958}
5959
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005960static void r8169_phylink_handler(struct net_device *ndev)
5961{
5962 struct rtl8169_private *tp = netdev_priv(ndev);
5963
5964 if (netif_carrier_ok(ndev)) {
5965 rtl_link_chg_patch(tp);
5966 pm_request_resume(&tp->pci_dev->dev);
5967 } else {
5968 pm_runtime_idle(&tp->pci_dev->dev);
5969 }
5970
5971 if (net_ratelimit())
Heiner Kallweit703732f2019-01-19 22:07:05 +01005972 phy_print_status(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005973}
5974
5975static int r8169_phy_connect(struct rtl8169_private *tp)
5976{
Heiner Kallweit703732f2019-01-19 22:07:05 +01005977 struct phy_device *phydev = tp->phydev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005978 phy_interface_t phy_mode;
5979 int ret;
5980
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02005981 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005982 PHY_INTERFACE_MODE_MII;
5983
5984 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
5985 phy_mode);
5986 if (ret)
5987 return ret;
5988
Heiner Kallweit66058b12019-07-27 12:32:28 +02005989 if (!tp->supports_gmii)
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005990 phy_set_max_speed(phydev, SPEED_100);
5991
Heiner Kallweit9cf9b842019-05-04 12:01:03 +02005992 phy_support_asym_pause(phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005993
5994 phy_attached_info(phydev);
5995
5996 return 0;
5997}
5998
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999static void rtl8169_down(struct net_device *dev)
6000{
6001 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
Heiner Kallweit703732f2019-01-19 22:07:05 +01006003 phy_stop(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006004
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006005 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006006 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
Hayes Wang92fc43b2011-07-06 15:58:03 +08006008 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006009 /*
6010 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006011 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6012 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006013 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006014 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenney16f11502018-11-05 17:07:39 -08006017 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 rtl8169_tx_clear(tp);
6020
6021 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006022
6023 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024}
6025
6026static int rtl8169_close(struct net_device *dev)
6027{
6028 struct rtl8169_private *tp = netdev_priv(dev);
6029 struct pci_dev *pdev = tp->pci_dev;
6030
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006031 pm_runtime_get_sync(&pdev->dev);
6032
Francois Romieucecb5fd2011-04-01 10:21:07 +02006033 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006034 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006035
Francois Romieuda78dbf2012-01-26 14:18:23 +01006036 rtl_lock_work(tp);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006037 /* Clear all task flags */
6038 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006039
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006041 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
Lekensteyn4ea72442013-07-22 09:53:30 +02006043 cancel_work_sync(&tp->wk.work);
6044
Heiner Kallweit703732f2019-01-19 22:07:05 +01006045 phy_disconnect(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006046
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006047 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006049 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6050 tp->RxPhyAddr);
6051 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6052 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 tp->TxDescArray = NULL;
6054 tp->RxDescArray = NULL;
6055
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006056 pm_runtime_put_sync(&pdev->dev);
6057
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 return 0;
6059}
6060
Francois Romieudc1c00c2012-03-08 10:06:18 +01006061#ifdef CONFIG_NET_POLL_CONTROLLER
6062static void rtl8169_netpoll(struct net_device *dev)
6063{
6064 struct rtl8169_private *tp = netdev_priv(dev);
6065
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03006066 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01006067}
6068#endif
6069
Francois Romieudf43ac72012-03-08 09:48:40 +01006070static int rtl_open(struct net_device *dev)
6071{
6072 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006073 struct pci_dev *pdev = tp->pci_dev;
6074 int retval = -ENOMEM;
6075
6076 pm_runtime_get_sync(&pdev->dev);
6077
6078 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006079 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006080 * dma_alloc_coherent provides more.
6081 */
6082 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6083 &tp->TxPhyAddr, GFP_KERNEL);
6084 if (!tp->TxDescArray)
6085 goto err_pm_runtime_put;
6086
6087 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6088 &tp->RxPhyAddr, GFP_KERNEL);
6089 if (!tp->RxDescArray)
6090 goto err_free_tx_0;
6091
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006092 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006093 if (retval < 0)
6094 goto err_free_rx_1;
6095
Francois Romieudf43ac72012-03-08 09:48:40 +01006096 rtl_request_firmware(tp);
6097
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006098 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006099 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01006100 if (retval < 0)
6101 goto err_release_fw_2;
6102
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006103 retval = r8169_phy_connect(tp);
6104 if (retval)
6105 goto err_free_irq;
6106
Francois Romieudf43ac72012-03-08 09:48:40 +01006107 rtl_lock_work(tp);
6108
6109 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6110
6111 napi_enable(&tp->napi);
6112
6113 rtl8169_init_phy(dev, tp);
6114
Francois Romieudf43ac72012-03-08 09:48:40 +01006115 rtl_pll_power_up(tp);
6116
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006117 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006118
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006119 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006120 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6121
Heiner Kallweit703732f2019-01-19 22:07:05 +01006122 phy_start(tp->phydev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006123 netif_start_queue(dev);
6124
6125 rtl_unlock_work(tp);
6126
Heiner Kallweita92a0842018-01-08 21:39:13 +01006127 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006128out:
6129 return retval;
6130
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006131err_free_irq:
6132 pci_free_irq(pdev, 0, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006133err_release_fw_2:
6134 rtl_release_firmware(tp);
6135 rtl8169_rx_clear(tp);
6136err_free_rx_1:
6137 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6138 tp->RxPhyAddr);
6139 tp->RxDescArray = NULL;
6140err_free_tx_0:
6141 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6142 tp->TxPhyAddr);
6143 tp->TxDescArray = NULL;
6144err_pm_runtime_put:
6145 pm_runtime_put_noidle(&pdev->dev);
6146 goto out;
6147}
6148
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006149static void
Junchang Wang8027aa22012-03-04 23:30:32 +01006150rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151{
6152 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006153 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02006154 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01006155 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006157 pm_runtime_get_noresume(&pdev->dev);
6158
6159 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006160 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006161
Junchang Wang8027aa22012-03-04 23:30:32 +01006162 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006163 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006164 stats->rx_packets = tp->rx_stats.packets;
6165 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006166 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006167
Junchang Wang8027aa22012-03-04 23:30:32 +01006168 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006169 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006170 stats->tx_packets = tp->tx_stats.packets;
6171 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006172 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006173
6174 stats->rx_dropped = dev->stats.rx_dropped;
6175 stats->tx_dropped = dev->stats.tx_dropped;
6176 stats->rx_length_errors = dev->stats.rx_length_errors;
6177 stats->rx_errors = dev->stats.rx_errors;
6178 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6179 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6180 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02006181 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01006182
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006183 /*
Corentin Musarded72a9b2019-07-24 14:34:43 +02006184 * Fetch additional counter values missing in stats collected by driver
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006185 * from tally counters.
6186 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006187 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006188 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006189
6190 /*
6191 * Subtract values fetched during initalization.
6192 * See rtl8169_init_counter_offsets for a description why we do that.
6193 */
Corinna Vinschen42020322015-09-10 10:47:35 +02006194 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006195 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02006196 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006197 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02006198 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006199 le16_to_cpu(tp->tc_offset.tx_aborted);
6200
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006201 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202}
6203
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006204static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006205{
françois romieu065c27c2011-01-03 15:08:12 +00006206 struct rtl8169_private *tp = netdev_priv(dev);
6207
Francois Romieu5d06a992006-02-23 00:47:58 +01006208 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006209 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006210
Heiner Kallweit703732f2019-01-19 22:07:05 +01006211 phy_stop(tp->phydev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006212 netif_device_detach(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006213
6214 rtl_lock_work(tp);
6215 napi_disable(&tp->napi);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006216 /* Clear all task flags */
6217 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6218
Francois Romieuda78dbf2012-01-26 14:18:23 +01006219 rtl_unlock_work(tp);
6220
6221 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006222}
Francois Romieu5d06a992006-02-23 00:47:58 +01006223
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006224#ifdef CONFIG_PM
6225
6226static int rtl8169_suspend(struct device *device)
6227{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006228 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006229 struct rtl8169_private *tp = netdev_priv(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006230
6231 rtl8169_net_suspend(dev);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006232 clk_disable_unprepare(tp->clk);
Francois Romieu1371fa62007-04-02 23:01:11 +02006233
Francois Romieu5d06a992006-02-23 00:47:58 +01006234 return 0;
6235}
6236
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006237static void __rtl8169_resume(struct net_device *dev)
6238{
françois romieu065c27c2011-01-03 15:08:12 +00006239 struct rtl8169_private *tp = netdev_priv(dev);
6240
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006241 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006242
6243 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02006244 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00006245
Heiner Kallweit703732f2019-01-19 22:07:05 +01006246 phy_start(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006247
Artem Savkovcff4c162012-04-03 10:29:11 +00006248 rtl_lock_work(tp);
6249 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006250 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Heiner Kallweit58ba5662019-01-19 22:06:25 +01006251 rtl_reset_work(tp);
Artem Savkovcff4c162012-04-03 10:29:11 +00006252 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006253}
6254
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006255static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006256{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006257 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006258 struct rtl8169_private *tp = netdev_priv(dev);
6259
Heiner Kallweit59715172019-05-29 07:44:01 +02006260 rtl_rar_set(tp, dev->dev_addr);
6261
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006262 clk_prepare_enable(tp->clk);
Francois Romieu5d06a992006-02-23 00:47:58 +01006263
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006264 if (netif_running(dev))
6265 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006266
Francois Romieu5d06a992006-02-23 00:47:58 +01006267 return 0;
6268}
6269
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006270static int rtl8169_runtime_suspend(struct device *device)
6271{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006272 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006273 struct rtl8169_private *tp = netdev_priv(dev);
6274
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006275 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006276 return 0;
6277
Francois Romieuda78dbf2012-01-26 14:18:23 +01006278 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006279 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006280 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006281
6282 rtl8169_net_suspend(dev);
6283
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006284 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006285 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006286 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006287
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006288 return 0;
6289}
6290
6291static int rtl8169_runtime_resume(struct device *device)
6292{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006293 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006294 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit59715172019-05-29 07:44:01 +02006295
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08006296 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006297
6298 if (!tp->TxDescArray)
6299 return 0;
6300
Francois Romieuda78dbf2012-01-26 14:18:23 +01006301 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006302 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006303 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006304
6305 __rtl8169_resume(dev);
6306
6307 return 0;
6308}
6309
6310static int rtl8169_runtime_idle(struct device *device)
6311{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006312 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006313
Heiner Kallweita92a0842018-01-08 21:39:13 +01006314 if (!netif_running(dev) || !netif_carrier_ok(dev))
6315 pm_schedule_suspend(device, 10000);
6316
6317 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006318}
6319
Alexey Dobriyan47145212009-12-14 18:00:08 -08006320static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006321 .suspend = rtl8169_suspend,
6322 .resume = rtl8169_resume,
6323 .freeze = rtl8169_suspend,
6324 .thaw = rtl8169_resume,
6325 .poweroff = rtl8169_suspend,
6326 .restore = rtl8169_resume,
6327 .runtime_suspend = rtl8169_runtime_suspend,
6328 .runtime_resume = rtl8169_runtime_resume,
6329 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006330};
6331
6332#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6333
6334#else /* !CONFIG_PM */
6335
6336#define RTL8169_PM_OPS NULL
6337
6338#endif /* !CONFIG_PM */
6339
David S. Miller1805b2f2011-10-24 18:18:09 -04006340static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6341{
David S. Miller1805b2f2011-10-24 18:18:09 -04006342 /* WoL fails with 8168b when the receiver is disabled. */
6343 switch (tp->mac_version) {
6344 case RTL_GIGA_MAC_VER_11:
6345 case RTL_GIGA_MAC_VER_12:
6346 case RTL_GIGA_MAC_VER_17:
6347 pci_clear_master(tp->pci_dev);
6348
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006349 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006350 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006351 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04006352 break;
6353 default:
6354 break;
6355 }
6356}
6357
Francois Romieu1765f952008-09-13 17:21:40 +02006358static void rtl_shutdown(struct pci_dev *pdev)
6359{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006360 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006361 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006362
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006363 rtl8169_net_suspend(dev);
6364
Francois Romieucecb5fd2011-04-01 10:21:07 +02006365 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006366 rtl_rar_set(tp, dev->perm_addr);
6367
Hayes Wang92fc43b2011-07-06 15:58:03 +08006368 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006369
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006370 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02006371 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006372 rtl_wol_suspend_quirk(tp);
6373 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006374 }
6375
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006376 pci_wake_from_d3(pdev, true);
6377 pci_set_power_state(pdev, PCI_D3hot);
6378 }
6379}
Francois Romieu5d06a992006-02-23 00:47:58 +01006380
Bill Pembertonbaf63292012-12-03 09:23:28 -05006381static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006382{
6383 struct net_device *dev = pci_get_drvdata(pdev);
6384 struct rtl8169_private *tp = netdev_priv(dev);
6385
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006386 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01006387 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01006388
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006389 netif_napi_del(&tp->napi);
6390
Francois Romieue27566e2012-03-08 09:54:01 +01006391 unregister_netdev(dev);
Heiner Kallweit703732f2019-01-19 22:07:05 +01006392 mdiobus_unregister(tp->phydev->mdio.bus);
Francois Romieue27566e2012-03-08 09:54:01 +01006393
6394 rtl_release_firmware(tp);
6395
6396 if (pci_dev_run_wake(pdev))
6397 pm_runtime_get_noresume(&pdev->dev);
6398
6399 /* restore original MAC address */
6400 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01006401}
6402
Francois Romieufa9c3852012-03-08 10:01:50 +01006403static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006404 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006405 .ndo_stop = rtl8169_close,
6406 .ndo_get_stats64 = rtl8169_get_stats64,
6407 .ndo_start_xmit = rtl8169_start_xmit,
Heiner Kallweite64e0c82019-07-26 21:49:22 +02006408 .ndo_features_check = rtl8169_features_check,
Francois Romieufa9c3852012-03-08 10:01:50 +01006409 .ndo_tx_timeout = rtl8169_tx_timeout,
6410 .ndo_validate_addr = eth_validate_addr,
6411 .ndo_change_mtu = rtl8169_change_mtu,
6412 .ndo_fix_features = rtl8169_fix_features,
6413 .ndo_set_features = rtl8169_set_features,
6414 .ndo_set_mac_address = rtl_set_mac_address,
6415 .ndo_do_ioctl = rtl8169_ioctl,
6416 .ndo_set_rx_mode = rtl_set_rx_mode,
6417#ifdef CONFIG_NET_POLL_CONTROLLER
6418 .ndo_poll_controller = rtl8169_netpoll,
6419#endif
6420
6421};
6422
Heiner Kallweitec9a4082019-06-10 18:21:50 +02006423static void rtl_set_irq_mask(struct rtl8169_private *tp)
6424{
6425 tp->irq_mask = RTL_EVENT_NAPI | LinkChg;
6426
6427 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6428 tp->irq_mask |= SYSErr | RxOverflow | RxFIFOOver;
6429 else if (tp->mac_version == RTL_GIGA_MAC_VER_11)
6430 /* special workaround needed */
6431 tp->irq_mask |= RxFIFOOver;
6432 else
6433 tp->irq_mask |= RxOverflow;
6434}
6435
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006436static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01006437{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006438 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006439
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006440 switch (tp->mac_version) {
6441 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006442 rtl_unlock_config_regs(tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006443 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006444 rtl_lock_config_regs(tp);
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006445 /* fall through */
6446 case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_24:
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006447 flags = PCI_IRQ_LEGACY;
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006448 break;
6449 default:
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006450 flags = PCI_IRQ_ALL_TYPES;
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006451 break;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006452 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006453
6454 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01006455}
6456
Thierry Reding04c77882019-02-06 13:30:17 +01006457static void rtl_read_mac_address(struct rtl8169_private *tp,
6458 u8 mac_addr[ETH_ALEN])
6459{
6460 /* Get MAC address */
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02006461 if (rtl_is_8168evl_up(tp) && tp->mac_version != RTL_GIGA_MAC_VER_34) {
6462 u32 value = rtl_eri_read(tp, 0xe0);
6463
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006464 mac_addr[0] = (value >> 0) & 0xff;
6465 mac_addr[1] = (value >> 8) & 0xff;
6466 mac_addr[2] = (value >> 16) & 0xff;
6467 mac_addr[3] = (value >> 24) & 0xff;
6468
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02006469 value = rtl_eri_read(tp, 0xe4);
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006470 mac_addr[4] = (value >> 0) & 0xff;
6471 mac_addr[5] = (value >> 8) & 0xff;
Thierry Reding04c77882019-02-06 13:30:17 +01006472 }
6473}
6474
Hayes Wangc5583862012-07-02 17:23:22 +08006475DECLARE_RTL_COND(rtl_link_list_ready_cond)
6476{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006477 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08006478}
6479
6480DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6481{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006482 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08006483}
6484
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006485static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
6486{
6487 struct rtl8169_private *tp = mii_bus->priv;
6488
6489 if (phyaddr > 0)
6490 return -ENODEV;
6491
6492 return rtl_readphy(tp, phyreg);
6493}
6494
6495static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
6496 int phyreg, u16 val)
6497{
6498 struct rtl8169_private *tp = mii_bus->priv;
6499
6500 if (phyaddr > 0)
6501 return -ENODEV;
6502
6503 rtl_writephy(tp, phyreg, val);
6504
6505 return 0;
6506}
6507
6508static int r8169_mdio_register(struct rtl8169_private *tp)
6509{
6510 struct pci_dev *pdev = tp->pci_dev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006511 struct mii_bus *new_bus;
6512 int ret;
6513
6514 new_bus = devm_mdiobus_alloc(&pdev->dev);
6515 if (!new_bus)
6516 return -ENOMEM;
6517
6518 new_bus->name = "r8169";
6519 new_bus->priv = tp;
6520 new_bus->parent = &pdev->dev;
6521 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
Heiner Kallweita1950162019-04-24 21:13:25 +02006522 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x", pci_dev_id(pdev));
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006523
6524 new_bus->read = r8169_mdio_read_reg;
6525 new_bus->write = r8169_mdio_write_reg;
6526
6527 ret = mdiobus_register(new_bus);
6528 if (ret)
6529 return ret;
6530
Heiner Kallweit703732f2019-01-19 22:07:05 +01006531 tp->phydev = mdiobus_get_phy(new_bus, 0);
6532 if (!tp->phydev) {
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006533 mdiobus_unregister(new_bus);
6534 return -ENODEV;
6535 }
6536
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02006537 /* PHY will be woken up in rtl_open() */
Heiner Kallweit703732f2019-01-19 22:07:05 +01006538 phy_suspend(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006539
6540 return 0;
6541}
6542
Bill Pembertonbaf63292012-12-03 09:23:28 -05006543static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006544{
Hayes Wangc5583862012-07-02 17:23:22 +08006545 tp->ocp_base = OCP_STD_PHY_BASE;
6546
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006547 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006548
6549 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6550 return;
6551
6552 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6553 return;
6554
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006555 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08006556 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006557 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08006558
Heiner Kallweitef712ed2019-08-04 09:47:51 +02006559 r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
Hayes Wangc5583862012-07-02 17:23:22 +08006560
6561 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6562 return;
6563
Heiner Kallweitef712ed2019-08-04 09:47:51 +02006564 r8168_mac_ocp_modify(tp, 0xe8de, 0, BIT(15));
Hayes Wangc5583862012-07-02 17:23:22 +08006565
Heiner Kallweit7160be22019-05-25 20:44:01 +02006566 rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
Hayes Wangc5583862012-07-02 17:23:22 +08006567}
6568
Bill Pembertonbaf63292012-12-03 09:23:28 -05006569static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006570{
6571 switch (tp->mac_version) {
Heiner Kallweit29ec0482019-05-25 20:43:25 +02006572 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
6573 rtl8168ep_stop_cmac(tp);
6574 /* fall through */
Heiner Kallweit2a718832018-05-02 21:39:49 +02006575 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006576 rtl_hw_init_8168g(tp);
6577 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006578 default:
6579 break;
6580 }
6581}
6582
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006583static int rtl_jumbo_max(struct rtl8169_private *tp)
6584{
6585 /* Non-GBit versions don't support jumbo frames */
6586 if (!tp->supports_gmii)
6587 return JUMBO_1K;
6588
6589 switch (tp->mac_version) {
6590 /* RTL8169 */
Heiner Kallweite9588eb2019-05-25 21:14:39 +02006591 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006592 return JUMBO_7K;
6593 /* RTL8168b */
6594 case RTL_GIGA_MAC_VER_11:
6595 case RTL_GIGA_MAC_VER_12:
6596 case RTL_GIGA_MAC_VER_17:
6597 return JUMBO_4K;
6598 /* RTL8168c */
6599 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
6600 return JUMBO_6K;
6601 default:
6602 return JUMBO_9K;
6603 }
6604}
6605
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006606static void rtl_disable_clk(void *data)
6607{
6608 clk_disable_unprepare(data);
6609}
6610
Heiner Kallweitb779dae2019-01-19 22:07:34 +01006611static int rtl_get_ether_clk(struct rtl8169_private *tp)
6612{
6613 struct device *d = tp_to_dev(tp);
6614 struct clk *clk;
6615 int rc;
6616
6617 clk = devm_clk_get(d, "ether_clk");
6618 if (IS_ERR(clk)) {
6619 rc = PTR_ERR(clk);
6620 if (rc == -ENOENT)
6621 /* clk-core allows NULL (for suspend / resume) */
6622 rc = 0;
6623 else if (rc != -EPROBE_DEFER)
6624 dev_err(d, "failed to get clk: %d\n", rc);
6625 } else {
6626 tp->clk = clk;
6627 rc = clk_prepare_enable(clk);
6628 if (rc)
6629 dev_err(d, "failed to enable clk: %d\n", rc);
6630 else
6631 rc = devm_add_action_or_reset(d, rtl_disable_clk, clk);
6632 }
6633
6634 return rc;
6635}
6636
Heiner Kallweitc782e202019-07-02 20:46:09 +02006637static void rtl_init_mac_address(struct rtl8169_private *tp)
6638{
6639 struct net_device *dev = tp->dev;
6640 u8 *mac_addr = dev->dev_addr;
Heiner Kallweitce37115e32019-08-28 22:25:32 +02006641 int rc;
Heiner Kallweitc782e202019-07-02 20:46:09 +02006642
6643 rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr);
6644 if (!rc)
6645 goto done;
6646
6647 rtl_read_mac_address(tp, mac_addr);
6648 if (is_valid_ether_addr(mac_addr))
6649 goto done;
6650
Heiner Kallweitce37115e32019-08-28 22:25:32 +02006651 rtl_read_mac_from_reg(tp, mac_addr, MAC0);
Heiner Kallweitc782e202019-07-02 20:46:09 +02006652 if (is_valid_ether_addr(mac_addr))
6653 goto done;
6654
6655 eth_hw_addr_random(dev);
6656 dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n");
6657done:
6658 rtl_rar_set(tp, mac_addr);
6659}
6660
hayeswang929a0312014-09-16 11:40:47 +08006661static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01006662{
Francois Romieu3b6cf252012-03-08 09:59:04 +01006663 struct rtl8169_private *tp;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006664 struct net_device *dev;
Heiner Kallweitc782e202019-07-02 20:46:09 +02006665 int chipset, region;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006666 int jumbo_max, rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006667
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006668 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
6669 if (!dev)
6670 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006671
6672 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006673 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006674 tp = netdev_priv(dev);
6675 tp->dev = dev;
6676 tp->pci_dev = pdev;
6677 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Heiner Kallweit145a40e2019-06-10 18:25:29 +02006678 tp->supports_gmii = ent->driver_data == RTL_CFG_NO_GBIT ? 0 : 1;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006679
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006680 /* Get the *optional* external "ether_clk" used on some boards */
Heiner Kallweitb779dae2019-01-19 22:07:34 +01006681 rc = rtl_get_ether_clk(tp);
6682 if (rc)
6683 return rc;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006684
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +02006685 /* Disable ASPM completely as that cause random device stop working
6686 * problems as well as full system hangs for some PCIe devices users.
6687 */
Heiner Kallweit62b1b3b2019-06-18 23:14:50 +02006688 rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
6689 PCIE_LINK_STATE_L1);
6690 tp->aspm_manageable = !rc;
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +02006691
Francois Romieu3b6cf252012-03-08 09:59:04 +01006692 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006693 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006694 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006695 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006696 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006697 }
6698
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006699 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02006700 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01006701
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02006702 /* use first MMIO region */
6703 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
6704 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006705 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006706 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006707 }
6708
6709 /* check for weird/broken PCI region reporting */
6710 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006711 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006712 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006713 }
6714
Andy Shevchenko93a00d42018-03-01 13:27:35 +02006715 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006716 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006717 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006718 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006719 }
6720
Andy Shevchenko93a00d42018-03-01 13:27:35 +02006721 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01006722
Francois Romieu3b6cf252012-03-08 09:59:04 +01006723 /* Identify chip attached to board */
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01006724 rtl8169_get_mac_version(tp);
6725 if (tp->mac_version == RTL_GIGA_MAC_NONE)
6726 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006727
Heiner Kallweit0ae09742018-04-28 22:19:26 +02006728 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006729
Heiner Kallweit10b63e82019-01-20 11:45:20 +01006730 if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
Heiner Kallweit3c18cbe2019-06-27 23:12:39 +02006731 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)))
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006732 dev->features |= NETIF_F_HIGHDMA;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006733
Francois Romieu3b6cf252012-03-08 09:59:04 +01006734 rtl_init_rxcfg(tp);
6735
Heiner Kallweitde20e122018-09-25 07:58:00 +02006736 rtl8169_irq_mask_and_ack(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006737
Hayes Wangc5583862012-07-02 17:23:22 +08006738 rtl_hw_initialize(tp);
6739
Francois Romieu3b6cf252012-03-08 09:59:04 +01006740 rtl_hw_reset(tp);
6741
Francois Romieu3b6cf252012-03-08 09:59:04 +01006742 pci_set_master(pdev);
6743
Francois Romieu3b6cf252012-03-08 09:59:04 +01006744 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006745
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006746 rc = rtl_alloc_irq(tp);
6747 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006748 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006749 return rc;
6750 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01006751
Francois Romieu3b6cf252012-03-08 09:59:04 +01006752 mutex_init(&tp->wk.mutex);
Heiner Kallweit5c41e782019-01-19 22:03:49 +01006753 INIT_WORK(&tp->wk.work, rtl_task);
Kyle McMartin340fea32014-02-24 20:12:28 -05006754 u64_stats_init(&tp->rx_stats.syncp);
6755 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006756
Heiner Kallweitc782e202019-07-02 20:46:09 +02006757 rtl_init_mac_address(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006758
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00006759 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006760
Heiner Kallweit37621492018-04-17 23:20:03 +02006761 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006762
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006763 dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6764 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6765 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006766 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00006767 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6768 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006769 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6770 NETIF_F_HIGHDMA;
Heiner Kallweit2d0ec542018-07-02 22:49:35 +02006771 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006772
hayeswang929a0312014-09-16 11:40:47 +08006773 tp->cp_cmd |= RxChkSum | RxVlan;
6774
6775 /*
6776 * Pretend we are using VLANs; This bypasses a nasty bug where
6777 * Interrupts stop flowing on high load on 8110SCd controllers.
6778 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01006779 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08006780 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00006781 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006782
Heiner Kallweit0170d592019-07-26 21:48:32 +02006783 if (rtl_chip_supports_csum_v2(tp)) {
hayeswange9746042014-07-11 16:25:58 +08006784 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006785 dev->features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweit0170d592019-07-26 21:48:32 +02006786 dev->gso_max_size = RTL_GSO_MAX_SIZE_V2;
6787 dev->gso_max_segs = RTL_GSO_MAX_SEGS_V2;
6788 } else {
6789 dev->gso_max_size = RTL_GSO_MAX_SIZE_V1;
6790 dev->gso_max_segs = RTL_GSO_MAX_SEGS_V1;
6791 }
hayeswang5888d3f2014-07-11 16:25:56 +08006792
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006793 /* RTL8168e-vl has a HW issue with TSO */
6794 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
Holger Hoffstättea7eb6a42019-08-09 00:02:40 +02006795 dev->vlan_features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
6796 dev->hw_features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
6797 dev->features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006798 }
6799
Francois Romieu3b6cf252012-03-08 09:59:04 +01006800 dev->hw_features |= NETIF_F_RXALL;
6801 dev->hw_features |= NETIF_F_RXFCS;
6802
Jarod Wilsonc7315a92016-10-17 15:54:09 -04006803 /* MTU range: 60 - hw-specific max */
6804 dev->min_mtu = ETH_ZLEN;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006805 jumbo_max = rtl_jumbo_max(tp);
6806 dev->max_mtu = jumbo_max;
Jarod Wilsonc7315a92016-10-17 15:54:09 -04006807
Heiner Kallweitec9a4082019-06-10 18:21:50 +02006808 rtl_set_irq_mask(tp);
Heiner Kallweit9fa0a8e2019-06-10 18:24:25 +02006809
Heiner Kallweit254764e2019-01-22 22:23:41 +01006810 tp->fw_name = rtl_chip_infos[chipset].fw_name;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006811
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006812 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
6813 &tp->counters_phys_addr,
6814 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01006815 if (!tp->counters)
6816 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02006817
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02006818 pci_set_drvdata(pdev, dev);
6819
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006820 rc = r8169_mdio_register(tp);
6821 if (rc)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01006822 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006823
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006824 /* chip gets powered up in rtl_open() */
6825 rtl_pll_power_down(tp);
6826
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006827 rc = register_netdev(dev);
6828 if (rc)
6829 goto err_mdio_unregister;
6830
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01006831 netif_info(tp, probe, dev, "%s, %pM, XID %03x, IRQ %d\n",
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02006832 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01006833 (RTL_R32(tp, TxConfig) >> 20) & 0xfcf,
Heiner Kallweit29274992018-02-28 20:43:38 +01006834 pci_irq_vector(pdev, 0));
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006835
6836 if (jumbo_max > JUMBO_1K)
6837 netif_info(tp, probe, dev,
6838 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
6839 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
6840 "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01006841
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006842 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01006843 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006844
Heiner Kallweita92a0842018-01-08 21:39:13 +01006845 if (pci_dev_run_wake(pdev))
6846 pm_runtime_put_sync(&pdev->dev);
6847
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006848 return 0;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006849
6850err_mdio_unregister:
Heiner Kallweit703732f2019-01-19 22:07:05 +01006851 mdiobus_unregister(tp->phydev->mdio.bus);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006852 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006853}
6854
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855static struct pci_driver rtl8169_pci_driver = {
6856 .name = MODULENAME,
6857 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006858 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05006859 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02006860 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006861 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862};
6863
Devendra Naga3eeb7da2012-10-26 09:27:42 +00006864module_pci_driver(rtl8169_pci_driver);