blob: e9d900c112dcd0b714b4a1e9162969f7cd7201c6 [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
Francois Romieuffc46952012-07-06 14:19:23 +0200744struct rtl_cond {
745 bool (*check)(struct rtl8169_private *);
746 const char *msg;
747};
748
749static void rtl_udelay(unsigned int d)
750{
751 udelay(d);
752}
753
754static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
755 void (*delay)(unsigned int), unsigned int d, int n,
756 bool high)
757{
758 int i;
759
760 for (i = 0; i < n; i++) {
Francois Romieuffc46952012-07-06 14:19:23 +0200761 if (c->check(tp) == high)
762 return true;
Heiner Kallweitd1f50502019-05-04 15:20:38 +0200763 delay(d);
Francois Romieuffc46952012-07-06 14:19:23 +0200764 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200765 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
766 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200767 return false;
768}
769
770static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
771 const struct rtl_cond *c,
772 unsigned int d, int n)
773{
774 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
775}
776
777static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
778 const struct rtl_cond *c,
779 unsigned int d, int n)
780{
781 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
782}
783
784static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
785 const struct rtl_cond *c,
786 unsigned int d, int n)
787{
788 return rtl_loop_wait(tp, c, msleep, d, n, true);
789}
790
791static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
792 const struct rtl_cond *c,
793 unsigned int d, int n)
794{
795 return rtl_loop_wait(tp, c, msleep, d, n, false);
796}
797
798#define DECLARE_RTL_COND(name) \
799static bool name ## _check(struct rtl8169_private *); \
800 \
801static const struct rtl_cond name = { \
802 .check = name ## _check, \
803 .msg = #name \
804}; \
805 \
806static bool name ## _check(struct rtl8169_private *tp)
807
Hayes Wangc5583862012-07-02 17:23:22 +0800808static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
809{
810 if (reg & 0xffff0001) {
811 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
812 return true;
813 }
814 return false;
815}
816
817DECLARE_RTL_COND(rtl_ocp_gphy_cond)
818{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200819 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800820}
821
822static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
823{
Hayes Wangc5583862012-07-02 17:23:22 +0800824 if (rtl_ocp_reg_failure(tp, reg))
825 return;
826
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200827 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800828
829 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
830}
831
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200832static int r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
Hayes Wangc5583862012-07-02 17:23:22 +0800833{
Hayes Wangc5583862012-07-02 17:23:22 +0800834 if (rtl_ocp_reg_failure(tp, reg))
835 return 0;
836
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200837 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800838
839 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200840 (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;
Hayes Wangc5583862012-07-02 17:23:22 +0800841}
842
Hayes Wangc5583862012-07-02 17:23:22 +0800843static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
844{
Hayes Wangc5583862012-07-02 17:23:22 +0800845 if (rtl_ocp_reg_failure(tp, reg))
846 return;
847
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200848 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800849}
850
851static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
852{
Hayes Wangc5583862012-07-02 17:23:22 +0800853 if (rtl_ocp_reg_failure(tp, reg))
854 return 0;
855
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200856 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800857
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200858 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800859}
860
Heiner Kallweitef712ed2019-08-04 09:47:51 +0200861static void r8168_mac_ocp_modify(struct rtl8169_private *tp, u32 reg, u16 mask,
862 u16 set)
863{
864 u16 data = r8168_mac_ocp_read(tp, reg);
865
866 r8168_mac_ocp_write(tp, reg, (data & ~mask) | set);
867}
868
Hayes Wangc5583862012-07-02 17:23:22 +0800869#define OCP_STD_PHY_BASE 0xa400
870
871static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
872{
873 if (reg == 0x1f) {
874 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
875 return;
876 }
877
878 if (tp->ocp_base != OCP_STD_PHY_BASE)
879 reg -= 0x10;
880
881 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
882}
883
884static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
885{
886 if (tp->ocp_base != OCP_STD_PHY_BASE)
887 reg -= 0x10;
888
889 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
890}
891
hayeswangeee37862013-04-01 22:23:38 +0000892static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
893{
894 if (reg == 0x1f) {
895 tp->ocp_base = value << 4;
896 return;
897 }
898
899 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
900}
901
902static int mac_mcu_read(struct rtl8169_private *tp, int reg)
903{
904 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
905}
906
Francois Romieuffc46952012-07-06 14:19:23 +0200907DECLARE_RTL_COND(rtl_phyar_cond)
908{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200909 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200910}
911
Francois Romieu24192212012-07-06 20:19:42 +0200912static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200914 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Francois Romieuffc46952012-07-06 14:19:23 +0200916 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700917 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700918 * According to hardware specs a 20us delay is required after write
919 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700920 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700921 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Francois Romieu24192212012-07-06 20:19:42 +0200924static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Francois Romieuffc46952012-07-06 14:19:23 +0200926 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200928 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Francois Romieuffc46952012-07-06 14:19:23 +0200930 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200931 RTL_R32(tp, PHYAR) & 0xffff : -ETIMEDOUT;
Francois Romieuffc46952012-07-06 14:19:23 +0200932
Timo Teräs81a95f02010-06-09 17:31:48 -0700933 /*
934 * According to hardware specs a 20us delay is required after read
935 * complete indication, but before sending next command.
936 */
937 udelay(20);
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return value;
940}
941
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800942DECLARE_RTL_COND(rtl_ocpar_cond)
943{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200944 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800945}
946
Francois Romieu24192212012-07-06 20:19:42 +0200947static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000948{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200949 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
950 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
951 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000952
Francois Romieuffc46952012-07-06 14:19:23 +0200953 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +0000954}
955
Francois Romieu24192212012-07-06 20:19:42 +0200956static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000957{
Francois Romieu24192212012-07-06 20:19:42 +0200958 r8168dp_1_mdio_access(tp, reg,
959 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000960}
961
Francois Romieu24192212012-07-06 20:19:42 +0200962static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000963{
Francois Romieu24192212012-07-06 20:19:42 +0200964 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +0000965
966 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200967 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
968 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000969
Francois Romieuffc46952012-07-06 14:19:23 +0200970 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200971 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : -ETIMEDOUT;
françois romieuc0e45c12011-01-03 15:08:04 +0000972}
973
françois romieue6de30d2011-01-03 15:08:37 +0000974#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
975
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200976static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000977{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200978 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000979}
980
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200981static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000982{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200983 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000984}
985
Francois Romieu24192212012-07-06 20:19:42 +0200986static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +0000987{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200988 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000989
Francois Romieu24192212012-07-06 20:19:42 +0200990 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +0000991
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200992 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000993}
994
Francois Romieu24192212012-07-06 20:19:42 +0200995static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +0000996{
997 int value;
998
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200999 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001000
Francois Romieu24192212012-07-06 20:19:42 +02001001 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001002
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001003 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001004
1005 return value;
1006}
1007
Heiner Kallweitce8843a2019-05-29 21:15:06 +02001008static void rtl_writephy(struct rtl8169_private *tp, int location, int val)
Francois Romieudacf8152008-08-02 20:44:13 +02001009{
Heiner Kallweit5f950522019-05-31 19:53:28 +02001010 switch (tp->mac_version) {
1011 case RTL_GIGA_MAC_VER_27:
1012 r8168dp_1_mdio_write(tp, location, val);
1013 break;
1014 case RTL_GIGA_MAC_VER_28:
1015 case RTL_GIGA_MAC_VER_31:
1016 r8168dp_2_mdio_write(tp, location, val);
1017 break;
1018 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1019 r8168g_mdio_write(tp, location, val);
1020 break;
1021 default:
1022 r8169_mdio_write(tp, location, val);
1023 break;
1024 }
Francois Romieudacf8152008-08-02 20:44:13 +02001025}
1026
françois romieu4da19632011-01-03 15:07:55 +00001027static int rtl_readphy(struct rtl8169_private *tp, int location)
1028{
Heiner Kallweit5f950522019-05-31 19:53:28 +02001029 switch (tp->mac_version) {
1030 case RTL_GIGA_MAC_VER_27:
1031 return r8168dp_1_mdio_read(tp, location);
1032 case RTL_GIGA_MAC_VER_28:
1033 case RTL_GIGA_MAC_VER_31:
1034 return r8168dp_2_mdio_read(tp, location);
1035 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1036 return r8168g_mdio_read(tp, location);
1037 default:
1038 return r8169_mdio_read(tp, location);
1039 }
françois romieu4da19632011-01-03 15:07:55 +00001040}
1041
1042static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1043{
1044 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1045}
1046
Chun-Hao Lin76564422014-10-01 23:17:17 +08001047static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001048{
1049 int val;
1050
françois romieu4da19632011-01-03 15:07:55 +00001051 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001052 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001053}
1054
Francois Romieuffc46952012-07-06 14:19:23 +02001055DECLARE_RTL_COND(rtl_ephyar_cond)
1056{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001057 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001058}
1059
Francois Romieufdf6fc02012-07-06 22:40:38 +02001060static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001061{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001062 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001063 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1064
Francois Romieuffc46952012-07-06 14:19:23 +02001065 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1066
1067 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001068}
1069
Francois Romieufdf6fc02012-07-06 22:40:38 +02001070static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001071{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001072 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001073
Francois Romieuffc46952012-07-06 14:19:23 +02001074 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001075 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001076}
1077
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001078DECLARE_RTL_COND(rtl_eriar_cond)
1079{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001080 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001081}
1082
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001083static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1084 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001085{
Hayes Wang133ac402011-07-06 15:58:05 +08001086 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001087 RTL_W32(tp, ERIDR, val);
1088 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001089
Francois Romieuffc46952012-07-06 14:19:23 +02001090 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001091}
1092
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001093static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1094 u32 val)
1095{
1096 _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
1097}
1098
1099static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001100{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001101 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001102
Francois Romieuffc46952012-07-06 14:19:23 +02001103 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001104 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001105}
1106
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001107static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
1108{
1109 return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
1110}
1111
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001112static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001113 u32 m)
Hayes Wang133ac402011-07-06 15:58:05 +08001114{
1115 u32 val;
1116
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001117 val = rtl_eri_read(tp, addr);
1118 rtl_eri_write(tp, addr, mask, (val & ~m) | p);
Hayes Wang133ac402011-07-06 15:58:05 +08001119}
1120
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001121static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 mask,
1122 u32 p)
1123{
1124 rtl_w0w1_eri(tp, addr, mask, p, 0);
1125}
1126
1127static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 mask,
1128 u32 m)
1129{
1130 rtl_w0w1_eri(tp, addr, mask, 0, m);
1131}
1132
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001133static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1134{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001135 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001136 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001137 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001138}
1139
1140static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1141{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001142 return _rtl_eri_read(tp, reg, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001143}
1144
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001145static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1146 u32 data)
1147{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001148 RTL_W32(tp, OCPDR, data);
1149 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001150 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1151}
1152
1153static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1154 u32 data)
1155{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001156 _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1157 data, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001158}
1159
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001160static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001161{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001162 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001163
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001164 r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001165}
1166
1167#define OOB_CMD_RESET 0x00
1168#define OOB_CMD_DRIVER_START 0x05
1169#define OOB_CMD_DRIVER_STOP 0x06
1170
1171static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1172{
1173 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1174}
1175
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001176DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001177{
1178 u16 reg;
1179
1180 reg = rtl8168_get_ocp_reg(tp);
1181
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001182 return r8168dp_ocp_read(tp, 0x0f, reg) & 0x00000800;
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001183}
1184
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001185DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1186{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001187 return r8168ep_ocp_read(tp, 0x0f, 0x124) & 0x00000001;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001188}
1189
1190DECLARE_RTL_COND(rtl_ocp_tx_cond)
1191{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001192 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001193}
1194
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001195static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1196{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001197 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001198 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001199 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1200 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001201}
1202
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001203static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001204{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001205 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1206 rtl_msleep_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001207}
1208
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001209static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1210{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001211 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1212 r8168ep_ocp_write(tp, 0x01, 0x30,
1213 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001214 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1215}
1216
1217static void rtl8168_driver_start(struct rtl8169_private *tp)
1218{
1219 switch (tp->mac_version) {
1220 case RTL_GIGA_MAC_VER_27:
1221 case RTL_GIGA_MAC_VER_28:
1222 case RTL_GIGA_MAC_VER_31:
1223 rtl8168dp_driver_start(tp);
1224 break;
1225 case RTL_GIGA_MAC_VER_49:
1226 case RTL_GIGA_MAC_VER_50:
1227 case RTL_GIGA_MAC_VER_51:
1228 rtl8168ep_driver_start(tp);
1229 break;
1230 default:
1231 BUG();
1232 break;
1233 }
1234}
1235
1236static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1237{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001238 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1239 rtl_msleep_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001240}
1241
1242static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1243{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001244 rtl8168ep_stop_cmac(tp);
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001245 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1246 r8168ep_ocp_write(tp, 0x01, 0x30,
1247 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001248 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1249}
1250
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001251static void rtl8168_driver_stop(struct rtl8169_private *tp)
1252{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001253 switch (tp->mac_version) {
1254 case RTL_GIGA_MAC_VER_27:
1255 case RTL_GIGA_MAC_VER_28:
1256 case RTL_GIGA_MAC_VER_31:
1257 rtl8168dp_driver_stop(tp);
1258 break;
1259 case RTL_GIGA_MAC_VER_49:
1260 case RTL_GIGA_MAC_VER_50:
1261 case RTL_GIGA_MAC_VER_51:
1262 rtl8168ep_driver_stop(tp);
1263 break;
1264 default:
1265 BUG();
1266 break;
1267 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001268}
1269
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001270static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001271{
1272 u16 reg = rtl8168_get_ocp_reg(tp);
1273
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001274 return !!(r8168dp_ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001275}
1276
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001277static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001278{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001279 return !!(r8168ep_ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001280}
1281
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001282static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001283{
1284 switch (tp->mac_version) {
1285 case RTL_GIGA_MAC_VER_27:
1286 case RTL_GIGA_MAC_VER_28:
1287 case RTL_GIGA_MAC_VER_31:
1288 return r8168dp_check_dash(tp);
1289 case RTL_GIGA_MAC_VER_49:
1290 case RTL_GIGA_MAC_VER_50:
1291 case RTL_GIGA_MAC_VER_51:
1292 return r8168ep_check_dash(tp);
1293 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001294 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001295 }
1296}
1297
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001298static void rtl_reset_packet_filter(struct rtl8169_private *tp)
1299{
1300 rtl_eri_clear_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1301 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1302}
1303
Francois Romieuffc46952012-07-06 14:19:23 +02001304DECLARE_RTL_COND(rtl_efusear_cond)
1305{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001306 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001307}
1308
Francois Romieufdf6fc02012-07-06 22:40:38 +02001309static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001310{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001311 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001312
Francois Romieuffc46952012-07-06 14:19:23 +02001313 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001314 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001315}
1316
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02001317static u32 rtl_get_events(struct rtl8169_private *tp)
1318{
1319 return RTL_R16(tp, IntrStatus);
1320}
1321
1322static void rtl_ack_events(struct rtl8169_private *tp, u32 bits)
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001323{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001324 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001325}
1326
1327static void rtl_irq_disable(struct rtl8169_private *tp)
1328{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001329 RTL_W16(tp, IntrMask, 0);
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001330 tp->irq_enabled = 0;
Francois Romieu3e990ff2012-01-26 12:50:01 +01001331}
1332
Francois Romieuda78dbf2012-01-26 14:18:23 +01001333#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1334#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1335#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1336
Heiner Kallweitfe716f82018-11-19 22:31:32 +01001337static void rtl_irq_enable(struct rtl8169_private *tp)
Francois Romieuda78dbf2012-01-26 14:18:23 +01001338{
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001339 tp->irq_enabled = 1;
Heiner Kallweit559c3c02018-11-19 22:34:17 +01001340 RTL_W16(tp, IntrMask, tp->irq_mask);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001341}
1342
françois romieu811fd302011-12-04 20:30:45 +00001343static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001345 rtl_irq_disable(tp);
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02001346 rtl_ack_events(tp, 0xffffffff);
Heiner Kallweitde20e122018-09-25 07:58:00 +02001347 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001348 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Hayes Wang70090422011-07-06 15:58:06 +08001351static void rtl_link_chg_patch(struct rtl8169_private *tp)
1352{
Hayes Wang70090422011-07-06 15:58:06 +08001353 struct net_device *dev = tp->dev;
Heiner Kallweit703732f2019-01-19 22:07:05 +01001354 struct phy_device *phydev = tp->phydev;
Hayes Wang70090422011-07-06 15:58:06 +08001355
1356 if (!netif_running(dev))
1357 return;
1358
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001359 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1360 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001361 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001362 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1363 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001364 } else if (phydev->speed == SPEED_100) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001365 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1366 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wang70090422011-07-06 15:58:06 +08001367 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001368 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1369 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wang70090422011-07-06 15:58:06 +08001370 }
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001371 rtl_reset_packet_filter(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08001372 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1373 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001374 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001375 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1376 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wangc2218922011-09-06 16:55:18 +08001377 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001378 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1379 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wangc2218922011-09-06 16:55:18 +08001380 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001381 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001382 if (phydev->speed == SPEED_10) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001383 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
1384 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001385 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001386 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001387 }
Hayes Wang70090422011-07-06 15:58:06 +08001388 }
1389}
1390
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001391#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1392
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001393static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1394{
1395 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001396
Francois Romieuda78dbf2012-01-26 14:18:23 +01001397 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001398 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001399 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001400 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001401}
1402
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001403static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001404{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001405 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001406 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001407 u32 opt;
1408 u16 reg;
1409 u8 mask;
1410 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001411 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001412 { WAKE_UCAST, Config5, UWF },
1413 { WAKE_BCAST, Config5, BWF },
1414 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001415 { WAKE_ANY, Config5, LanWake },
1416 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001417 };
Francois Romieu851e6022012-04-17 11:10:11 +02001418 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001419
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001420 rtl_unlock_config_regs(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001421
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02001422 if (rtl_is_8168evl_up(tp)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001423 tmp = ARRAY_SIZE(cfg) - 1;
1424 if (wolopts & WAKE_MAGIC)
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001425 rtl_eri_set_bits(tp, 0x0dc, ERIAR_MASK_0100,
1426 MagicPacket_v2);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001427 else
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001428 rtl_eri_clear_bits(tp, 0x0dc, ERIAR_MASK_0100,
1429 MagicPacket_v2);
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02001430 } else {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001431 tmp = ARRAY_SIZE(cfg);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001432 }
1433
1434 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001435 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001436 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001437 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001438 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001439 }
1440
Francois Romieu851e6022012-04-17 11:10:11 +02001441 switch (tp->mac_version) {
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001442 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001443 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001444 if (wolopts)
1445 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001446 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001447 break;
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001448 case RTL_GIGA_MAC_VER_34:
1449 case RTL_GIGA_MAC_VER_37:
1450 case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001451 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001452 if (wolopts)
1453 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001454 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001455 break;
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001456 default:
1457 break;
Francois Romieu851e6022012-04-17 11:10:11 +02001458 }
1459
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001460 rtl_lock_config_regs(tp);
Heiner Kallweit3bd82642018-12-30 13:16:12 +01001461
1462 device_set_wakeup_enable(tp_to_dev(tp), wolopts);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001463}
1464
1465static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1466{
1467 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001468 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001469
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001470 if (wol->wolopts & ~WAKE_ANY)
1471 return -EINVAL;
1472
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001473 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001474
Francois Romieuda78dbf2012-01-26 14:18:23 +01001475 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001476
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001477 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001478
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001479 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001480 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001481
1482 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001483
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001484 pm_runtime_put_noidle(d);
1485
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001486 return 0;
1487}
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489static void rtl8169_get_drvinfo(struct net_device *dev,
1490 struct ethtool_drvinfo *info)
1491{
1492 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001493 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Rick Jones68aad782011-11-07 13:29:27 +00001495 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001496 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001497 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Heiner Kallweit254764e2019-01-22 22:23:41 +01001498 if (rtl_fw)
Rick Jones8ac72d12011-11-22 14:06:26 +00001499 strlcpy(info->fw_version, rtl_fw->version,
1500 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
1503static int rtl8169_get_regs_len(struct net_device *dev)
1504{
1505 return R8169_REGS_SIZE;
1506}
1507
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001508static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1509 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Francois Romieud58d46b2011-05-03 16:38:29 +02001511 struct rtl8169_private *tp = netdev_priv(dev);
1512
Francois Romieu2b7b4312011-04-18 22:53:24 -07001513 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001514 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Francois Romieud58d46b2011-05-03 16:38:29 +02001516 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001517 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001518 features &= ~NETIF_F_IP_CSUM;
1519
Michał Mirosław350fb322011-04-08 06:35:56 +00001520 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Heiner Kallweita3984572018-04-28 22:19:15 +02001523static int rtl8169_set_features(struct net_device *dev,
1524 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001527 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Heiner Kallweita3984572018-04-28 22:19:15 +02001529 rtl_lock_work(tp);
1530
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001531 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001532 if (features & NETIF_F_RXALL)
1533 rx_config |= (AcceptErr | AcceptRunt);
1534 else
1535 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001537 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001538
hayeswang929a0312014-09-16 11:40:47 +08001539 if (features & NETIF_F_RXCSUM)
1540 tp->cp_cmd |= RxChkSum;
1541 else
1542 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001543
hayeswang929a0312014-09-16 11:40:47 +08001544 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1545 tp->cp_cmd |= RxVlan;
1546 else
1547 tp->cp_cmd &= ~RxVlan;
1548
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001549 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1550 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Francois Romieuda78dbf2012-01-26 14:18:23 +01001552 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 return 0;
1555}
1556
Kirill Smelkov810f4892012-11-10 21:11:02 +04001557static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001559 return (skb_vlan_tag_present(skb)) ?
Heiner Kallweit7424edb2019-07-02 07:59:17 +02001560 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
Francois Romieu7a8fc772011-03-01 17:18:33 +01001563static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Francois Romieu7a8fc772011-03-01 17:18:33 +01001567 if (opts2 & RxVlanTag)
Heiner Kallweit7424edb2019-07-02 07:59:17 +02001568 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1572 void *p)
1573{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001574 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001575 u32 __iomem *data = tp->mmio_addr;
1576 u32 *dw = p;
1577 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Francois Romieuda78dbf2012-01-26 14:18:23 +01001579 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001580 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1581 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001582 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001585static u32 rtl8169_get_msglevel(struct net_device *dev)
1586{
1587 struct rtl8169_private *tp = netdev_priv(dev);
1588
1589 return tp->msg_enable;
1590}
1591
1592static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1593{
1594 struct rtl8169_private *tp = netdev_priv(dev);
1595
1596 tp->msg_enable = value;
1597}
1598
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001599static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1600 "tx_packets",
1601 "rx_packets",
1602 "tx_errors",
1603 "rx_errors",
1604 "rx_missed",
1605 "align_errors",
1606 "tx_single_collisions",
1607 "tx_multi_collisions",
1608 "unicast",
1609 "broadcast",
1610 "multicast",
1611 "tx_aborted",
1612 "tx_underrun",
1613};
1614
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001615static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001616{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001617 switch (sset) {
1618 case ETH_SS_STATS:
1619 return ARRAY_SIZE(rtl8169_gstrings);
1620 default:
1621 return -EOPNOTSUPP;
1622 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001623}
1624
Corinna Vinschen42020322015-09-10 10:47:35 +02001625DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001626{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001627 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001628}
1629
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001630static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001631{
Corinna Vinschen42020322015-09-10 10:47:35 +02001632 dma_addr_t paddr = tp->counters_phys_addr;
1633 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001634
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001635 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1636 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001637 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001638 RTL_W32(tp, CounterAddrLow, cmd);
1639 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001640
Francois Romieua78e9362018-01-26 01:53:26 +01001641 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001642}
1643
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001644static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001645{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001646 /*
1647 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1648 * tally counters.
1649 */
1650 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1651 return true;
1652
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001653 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001654}
1655
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001656static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001657{
Heiner Kallweit10262b02019-01-06 20:44:00 +01001658 u8 val = RTL_R8(tp, ChipCmd);
1659
Ivan Vecera355423d2009-02-06 21:49:57 -08001660 /*
1661 * Some chips are unable to dump tally counters when the receiver
Heiner Kallweit10262b02019-01-06 20:44:00 +01001662 * is disabled. If 0xff chip may be in a PCI power-save state.
Ivan Vecera355423d2009-02-06 21:49:57 -08001663 */
Heiner Kallweit10262b02019-01-06 20:44:00 +01001664 if (!(val & CmdRxEnb) || val == 0xff)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001665 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001666
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001667 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001668}
1669
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001670static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001671{
Corinna Vinschen42020322015-09-10 10:47:35 +02001672 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001673 bool ret = false;
1674
1675 /*
1676 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1677 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1678 * reset by a power cycle, while the counter values collected by the
1679 * driver are reset at every driver unload/load cycle.
1680 *
1681 * To make sure the HW values returned by @get_stats64 match the SW
1682 * values, we collect the initial values at first open(*) and use them
1683 * as offsets to normalize the values returned by @get_stats64.
1684 *
1685 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1686 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1687 * set at open time by rtl_hw_start.
1688 */
1689
1690 if (tp->tc_offset.inited)
1691 return true;
1692
1693 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001694 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001695 ret = true;
1696
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001697 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001698 ret = true;
1699
Corinna Vinschen42020322015-09-10 10:47:35 +02001700 tp->tc_offset.tx_errors = counters->tx_errors;
1701 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1702 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001703 tp->tc_offset.inited = true;
1704
1705 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001706}
1707
Ivan Vecera355423d2009-02-06 21:49:57 -08001708static void rtl8169_get_ethtool_stats(struct net_device *dev,
1709 struct ethtool_stats *stats, u64 *data)
1710{
1711 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001712 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02001713 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08001714
1715 ASSERT_RTNL();
1716
Chun-Hao Line0636232016-07-29 16:37:55 +08001717 pm_runtime_get_noresume(d);
1718
1719 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001720 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08001721
1722 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08001723
Corinna Vinschen42020322015-09-10 10:47:35 +02001724 data[0] = le64_to_cpu(counters->tx_packets);
1725 data[1] = le64_to_cpu(counters->rx_packets);
1726 data[2] = le64_to_cpu(counters->tx_errors);
1727 data[3] = le32_to_cpu(counters->rx_errors);
1728 data[4] = le16_to_cpu(counters->rx_missed);
1729 data[5] = le16_to_cpu(counters->align_errors);
1730 data[6] = le32_to_cpu(counters->tx_one_collision);
1731 data[7] = le32_to_cpu(counters->tx_multi_collision);
1732 data[8] = le64_to_cpu(counters->rx_unicast);
1733 data[9] = le64_to_cpu(counters->rx_broadcast);
1734 data[10] = le32_to_cpu(counters->rx_multicast);
1735 data[11] = le16_to_cpu(counters->tx_aborted);
1736 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08001737}
1738
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001739static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1740{
1741 switch(stringset) {
1742 case ETH_SS_STATS:
1743 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1744 break;
1745 }
1746}
1747
Francois Romieu50970832017-10-27 13:24:49 +03001748/*
1749 * Interrupt coalescing
1750 *
1751 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1752 * > 8169, 8168 and 810x line of chipsets
1753 *
1754 * 8169, 8168, and 8136(810x) serial chipsets support it.
1755 *
1756 * > 2 - the Tx timer unit at gigabit speed
1757 *
1758 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1759 * (0xe0) bit 1 and bit 0.
1760 *
1761 * For 8169
1762 * bit[1:0] \ speed 1000M 100M 10M
1763 * 0 0 320ns 2.56us 40.96us
1764 * 0 1 2.56us 20.48us 327.7us
1765 * 1 0 5.12us 40.96us 655.4us
1766 * 1 1 10.24us 81.92us 1.31ms
1767 *
1768 * For the other
1769 * bit[1:0] \ speed 1000M 100M 10M
1770 * 0 0 5us 2.56us 40.96us
1771 * 0 1 40us 20.48us 327.7us
1772 * 1 0 80us 40.96us 655.4us
1773 * 1 1 160us 81.92us 1.31ms
1774 */
1775
1776/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1777struct rtl_coalesce_scale {
1778 /* Rx / Tx */
1779 u32 nsecs[2];
1780};
1781
1782/* rx/tx scale factors for all CPlusCmd[0:1] cases */
1783struct rtl_coalesce_info {
1784 u32 speed;
1785 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1786};
1787
1788/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1789#define rxtx_x1822(r, t) { \
1790 {{(r), (t)}}, \
1791 {{(r)*8, (t)*8}}, \
1792 {{(r)*8*2, (t)*8*2}}, \
1793 {{(r)*8*2*2, (t)*8*2*2}}, \
1794}
1795static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1796 /* speed delays: rx00 tx00 */
1797 { SPEED_10, rxtx_x1822(40960, 40960) },
1798 { SPEED_100, rxtx_x1822( 2560, 2560) },
1799 { SPEED_1000, rxtx_x1822( 320, 320) },
1800 { 0 },
1801};
1802
1803static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1804 /* speed delays: rx00 tx00 */
1805 { SPEED_10, rxtx_x1822(40960, 40960) },
1806 { SPEED_100, rxtx_x1822( 2560, 2560) },
1807 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1808 { 0 },
1809};
1810#undef rxtx_x1822
1811
1812/* get rx/tx scale vector corresponding to current speed */
1813static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1814{
1815 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001816 const struct rtl_coalesce_info *ci;
Francois Romieu50970832017-10-27 13:24:49 +03001817
Heiner Kallweit20023d32019-06-11 21:09:19 +02001818 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
1819 ci = rtl_coalesce_info_8169;
1820 else
1821 ci = rtl_coalesce_info_8168_8136;
Francois Romieu50970832017-10-27 13:24:49 +03001822
Heiner Kallweit20023d32019-06-11 21:09:19 +02001823 for (; ci->speed; ci++) {
1824 if (tp->phydev->speed == ci->speed)
Francois Romieu50970832017-10-27 13:24:49 +03001825 return ci;
Francois Romieu50970832017-10-27 13:24:49 +03001826 }
1827
1828 return ERR_PTR(-ELNRNG);
1829}
1830
1831static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1832{
1833 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001834 const struct rtl_coalesce_info *ci;
1835 const struct rtl_coalesce_scale *scale;
1836 struct {
1837 u32 *max_frames;
1838 u32 *usecs;
1839 } coal_settings [] = {
1840 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1841 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1842 }, *p = coal_settings;
1843 int i;
1844 u16 w;
1845
1846 memset(ec, 0, sizeof(*ec));
1847
1848 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1849 ci = rtl_coalesce_info(dev);
1850 if (IS_ERR(ci))
1851 return PTR_ERR(ci);
1852
Heiner Kallweit0ae09742018-04-28 22:19:26 +02001853 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03001854
1855 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001856 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03001857 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1858 w >>= RTL_COALESCE_SHIFT;
1859 *p->usecs = w & RTL_COALESCE_MASK;
1860 }
1861
1862 for (i = 0; i < 2; i++) {
1863 p = coal_settings + i;
1864 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1865
1866 /*
1867 * ethtool_coalesce says it is illegal to set both usecs and
1868 * max_frames to 0.
1869 */
1870 if (!*p->usecs && !*p->max_frames)
1871 *p->max_frames = 1;
1872 }
1873
1874 return 0;
1875}
1876
1877/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1878static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1879 struct net_device *dev, u32 nsec, u16 *cp01)
1880{
1881 const struct rtl_coalesce_info *ci;
1882 u16 i;
1883
1884 ci = rtl_coalesce_info(dev);
1885 if (IS_ERR(ci))
1886 return ERR_CAST(ci);
1887
1888 for (i = 0; i < 4; i++) {
1889 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1890 ci->scalev[i].nsecs[1]);
1891 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1892 *cp01 = i;
1893 return &ci->scalev[i];
1894 }
1895 }
1896
1897 return ERR_PTR(-EINVAL);
1898}
1899
1900static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1901{
1902 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001903 const struct rtl_coalesce_scale *scale;
1904 struct {
1905 u32 frames;
1906 u32 usecs;
1907 } coal_settings [] = {
1908 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1909 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1910 }, *p = coal_settings;
1911 u16 w = 0, cp01;
1912 int i;
1913
1914 scale = rtl_coalesce_choose_scale(dev,
1915 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1916 if (IS_ERR(scale))
1917 return PTR_ERR(scale);
1918
1919 for (i = 0; i < 2; i++, p++) {
1920 u32 units;
1921
1922 /*
1923 * accept max_frames=1 we returned in rtl_get_coalesce.
1924 * accept it not only when usecs=0 because of e.g. the following scenario:
1925 *
1926 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
1927 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
1928 * - then user does `ethtool -C eth0 rx-usecs 100`
1929 *
1930 * since ethtool sends to kernel whole ethtool_coalesce
1931 * settings, if we do not handle rx_usecs=!0, rx_frames=1
1932 * we'll reject it below in `frames % 4 != 0`.
1933 */
1934 if (p->frames == 1) {
1935 p->frames = 0;
1936 }
1937
1938 units = p->usecs * 1000 / scale->nsecs[i];
1939 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
1940 return -EINVAL;
1941
1942 w <<= RTL_COALESCE_SHIFT;
1943 w |= units;
1944 w <<= RTL_COALESCE_SHIFT;
1945 w |= p->frames >> 2;
1946 }
1947
1948 rtl_lock_work(tp);
1949
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001950 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03001951
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02001952 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001953 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1954 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03001955
1956 rtl_unlock_work(tp);
1957
1958 return 0;
1959}
1960
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001961static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
1962{
1963 struct rtl8169_private *tp = netdev_priv(dev);
1964 struct device *d = tp_to_dev(tp);
1965 int ret;
1966
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001967 if (!rtl_supports_eee(tp))
1968 return -EOPNOTSUPP;
1969
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001970 pm_runtime_get_noresume(d);
1971
1972 if (!pm_runtime_active(d)) {
1973 ret = -EOPNOTSUPP;
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001974 } else {
1975 ret = phy_ethtool_get_eee(tp->phydev, data);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001976 }
1977
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001978 pm_runtime_put_noidle(d);
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001979
1980 return ret;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001981}
1982
1983static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
1984{
1985 struct rtl8169_private *tp = netdev_priv(dev);
1986 struct device *d = tp_to_dev(tp);
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001987 int ret;
1988
1989 if (!rtl_supports_eee(tp))
1990 return -EOPNOTSUPP;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001991
1992 pm_runtime_get_noresume(d);
1993
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001994 if (!pm_runtime_active(d)) {
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001995 ret = -EOPNOTSUPP;
1996 goto out;
1997 }
1998
1999 if (dev->phydev->autoneg == AUTONEG_DISABLE ||
2000 dev->phydev->duplex != DUPLEX_FULL) {
2001 ret = -EPROTONOSUPPORT;
2002 goto out;
2003 }
2004
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002005 ret = phy_ethtool_set_eee(tp->phydev, data);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002006out:
2007 pm_runtime_put_noidle(d);
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002008 return ret;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002009}
2010
Jeff Garzik7282d492006-09-13 14:30:00 -04002011static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 .get_drvinfo = rtl8169_get_drvinfo,
2013 .get_regs_len = rtl8169_get_regs_len,
2014 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002015 .get_coalesce = rtl_get_coalesce,
2016 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002017 .get_msglevel = rtl8169_get_msglevel,
2018 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002020 .get_wol = rtl8169_get_wol,
2021 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002022 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002023 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002024 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002025 .get_ts_info = ethtool_op_get_ts_info,
Heiner Kallweitdd849572018-07-17 22:51:48 +02002026 .nway_reset = phy_ethtool_nway_reset,
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002027 .get_eee = rtl8169_get_eee,
2028 .set_eee = rtl8169_set_eee,
Heiner Kallweit45772432018-07-17 22:51:44 +02002029 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2030 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031};
2032
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002033static void rtl_enable_eee(struct rtl8169_private *tp)
2034{
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002035 struct phy_device *phydev = tp->phydev;
2036 int supported = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002037
2038 if (supported > 0)
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002039 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, supported);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002040}
2041
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002042static void rtl8169_get_mac_version(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Francois Romieu0e485152007-02-20 00:00:26 +01002044 /*
2045 * The driver currently handles the 8168Bf and the 8168Be identically
2046 * but they can be identified more specifically through the test below
2047 * if needed:
2048 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002049 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002050 *
2051 * Same thing for the 8101Eb and the 8101Ec:
2052 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002053 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002054 */
Francois Romieu37441002011-06-17 22:58:54 +02002055 static const struct rtl_mac_info {
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002056 u16 mask;
2057 u16 val;
2058 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002060 /* 8168EP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002061 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
2062 { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
2063 { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002064
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002065 /* 8168H family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002066 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
2067 { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002068
Hayes Wangc5583862012-07-02 17:23:22 +08002069 /* 8168G family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002070 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
2071 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
2072 { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
2073 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
Hayes Wangc5583862012-07-02 17:23:22 +08002074
Hayes Wangc2218922011-09-06 16:55:18 +08002075 /* 8168F family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002076 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
2077 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
2078 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
Hayes Wangc2218922011-09-06 16:55:18 +08002079
hayeswang01dc7fe2011-03-21 01:50:28 +00002080 /* 8168E family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002081 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
2082 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
2083 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002084
Francois Romieu5b538df2008-07-20 16:22:45 +02002085 /* 8168D family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002086 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
2087 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002088
françois romieue6de30d2011-01-03 15:08:37 +00002089 /* 8168DP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002090 { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
2091 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
2092 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002093
Francois Romieuef808d52008-06-29 13:10:54 +02002094 /* 8168C family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002095 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
2096 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
2097 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
2098 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
2099 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
2100 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
2101 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002102
2103 /* 8168B family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002104 { 0x7cf, 0x380, RTL_GIGA_MAC_VER_12 },
2105 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
2106 { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002107
2108 /* 8101 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002109 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
2110 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
2111 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
2112 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
2113 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
2114 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
2115 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
2116 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
2117 { 0x7cf, 0x340, RTL_GIGA_MAC_VER_13 },
2118 { 0x7cf, 0x343, RTL_GIGA_MAC_VER_10 },
2119 { 0x7cf, 0x342, RTL_GIGA_MAC_VER_16 },
2120 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
2121 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
2122 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_16 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002123 /* FIXME: where did these entries come from ? -- FR */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002124 { 0xfc8, 0x388, RTL_GIGA_MAC_VER_15 },
2125 { 0xfc8, 0x308, RTL_GIGA_MAC_VER_14 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002126
2127 /* 8110 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002128 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
2129 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
2130 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
2131 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
2132 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002133
Jean Delvaref21b75e2009-05-26 20:54:48 -07002134 /* Catch-all */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002135 { 0x000, 0x000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002136 };
2137 const struct rtl_mac_info *p = mac_info;
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002138 u16 reg = RTL_R32(tp, TxConfig) >> 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002140 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 p++;
2142 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002143
2144 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002145 dev_err(tp_to_dev(tp), "unknown chip XID %03x\n", reg & 0xfcf);
Heiner Kallweit45f19962018-11-22 22:00:10 +01002146 } else if (!tp->supports_gmii) {
2147 if (tp->mac_version == RTL_GIGA_MAC_VER_42)
2148 tp->mac_version = RTL_GIGA_MAC_VER_43;
2149 else if (tp->mac_version == RTL_GIGA_MAC_VER_45)
2150 tp->mac_version = RTL_GIGA_MAC_VER_47;
2151 else if (tp->mac_version == RTL_GIGA_MAC_VER_46)
2152 tp->mac_version = RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Francois Romieu867763c2007-08-17 18:21:58 +02002156struct phy_reg {
2157 u16 reg;
2158 u16 val;
2159};
2160
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002161static void __rtl_writephy_batch(struct rtl8169_private *tp,
2162 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002163{
2164 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002165 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002166 regs++;
2167 }
2168}
2169
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002170#define rtl_writephy_batch(tp, a) __rtl_writephy_batch(tp, a, ARRAY_SIZE(a))
2171
françois romieuf1e02ed2011-01-13 13:07:53 +00002172static void rtl_release_firmware(struct rtl8169_private *tp)
2173{
Heiner Kallweit254764e2019-01-22 22:23:41 +01002174 if (tp->rtl_fw) {
Heiner Kallweit47ad5932019-06-03 21:26:31 +02002175 rtl_fw_release_firmware(tp->rtl_fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02002176 kfree(tp->rtl_fw);
Heiner Kallweit254764e2019-01-22 22:23:41 +01002177 tp->rtl_fw = NULL;
Francois Romieub6ffd972011-06-17 17:00:05 +02002178 }
françois romieuf1e02ed2011-01-13 13:07:53 +00002179}
2180
François Romieu953a12c2011-04-24 17:38:48 +02002181static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002182{
Heiner Kallweitce8843a2019-05-29 21:15:06 +02002183 /* TODO: release firmware if rtl_fw_write_firmware signals failure. */
Heiner Kallweit254764e2019-01-22 22:23:41 +01002184 if (tp->rtl_fw)
Heiner Kallweitce8843a2019-05-29 21:15:06 +02002185 rtl_fw_write_firmware(tp, tp->rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002186}
2187
2188static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2189{
2190 if (rtl_readphy(tp, reg) != val)
2191 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2192 else
2193 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002194}
2195
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002196static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
2197{
Heiner Kallweitf4528252019-05-04 17:13:09 +02002198 /* Adjust EEE LED frequency */
2199 if (tp->mac_version != RTL_GIGA_MAC_VER_38)
2200 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
2201
Heiner Kallweite719b3e2019-04-28 11:11:47 +02002202 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_1111, 0x0003);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002203}
2204
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002205static void rtl8168f_config_eee_phy(struct rtl8169_private *tp)
2206{
2207 struct phy_device *phydev = tp->phydev;
2208
2209 phy_write(phydev, 0x1f, 0x0007);
2210 phy_write(phydev, 0x1e, 0x0020);
2211 phy_set_bits(phydev, 0x15, BIT(8));
2212
2213 phy_write(phydev, 0x1f, 0x0005);
2214 phy_write(phydev, 0x05, 0x8b85);
2215 phy_set_bits(phydev, 0x06, BIT(13));
2216
2217 phy_write(phydev, 0x1f, 0x0000);
2218}
2219
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002220static void rtl8168g_config_eee_phy(struct rtl8169_private *tp)
2221{
Heiner Kallweita2928d22019-06-02 10:53:49 +02002222 phy_modify_paged(tp->phydev, 0x0a43, 0x11, 0, BIT(4));
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002223}
2224
Heiner Kallweitb6cef262019-08-15 14:21:30 +02002225static void rtl8168h_config_eee_phy(struct rtl8169_private *tp)
2226{
2227 struct phy_device *phydev = tp->phydev;
2228
2229 rtl8168g_config_eee_phy(tp);
2230
2231 phy_modify_paged(phydev, 0xa4a, 0x11, 0x0000, 0x0200);
2232 phy_modify_paged(phydev, 0xa42, 0x14, 0x0000, 0x0080);
2233}
2234
françois romieu4da19632011-01-03 15:07:55 +00002235static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002237 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002238 { 0x1f, 0x0001 },
2239 { 0x06, 0x006e },
2240 { 0x08, 0x0708 },
2241 { 0x15, 0x4000 },
2242 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
françois romieu0b9b5712009-08-10 19:44:56 +00002244 { 0x1f, 0x0001 },
2245 { 0x03, 0x00a1 },
2246 { 0x02, 0x0008 },
2247 { 0x01, 0x0120 },
2248 { 0x00, 0x1000 },
2249 { 0x04, 0x0800 },
2250 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
françois romieu0b9b5712009-08-10 19:44:56 +00002252 { 0x03, 0xff41 },
2253 { 0x02, 0xdf60 },
2254 { 0x01, 0x0140 },
2255 { 0x00, 0x0077 },
2256 { 0x04, 0x7800 },
2257 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
françois romieu0b9b5712009-08-10 19:44:56 +00002259 { 0x03, 0x802f },
2260 { 0x02, 0x4f02 },
2261 { 0x01, 0x0409 },
2262 { 0x00, 0xf0f9 },
2263 { 0x04, 0x9800 },
2264 { 0x04, 0x9000 },
2265
2266 { 0x03, 0xdf01 },
2267 { 0x02, 0xdf20 },
2268 { 0x01, 0xff95 },
2269 { 0x00, 0xba00 },
2270 { 0x04, 0xa800 },
2271 { 0x04, 0xa000 },
2272
2273 { 0x03, 0xff41 },
2274 { 0x02, 0xdf20 },
2275 { 0x01, 0x0140 },
2276 { 0x00, 0x00bb },
2277 { 0x04, 0xb800 },
2278 { 0x04, 0xb000 },
2279
2280 { 0x03, 0xdf41 },
2281 { 0x02, 0xdc60 },
2282 { 0x01, 0x6340 },
2283 { 0x00, 0x007d },
2284 { 0x04, 0xd800 },
2285 { 0x04, 0xd000 },
2286
2287 { 0x03, 0xdf01 },
2288 { 0x02, 0xdf20 },
2289 { 0x01, 0x100a },
2290 { 0x00, 0xa0ff },
2291 { 0x04, 0xf800 },
2292 { 0x04, 0xf000 },
2293
2294 { 0x1f, 0x0000 },
2295 { 0x0b, 0x0000 },
2296 { 0x00, 0x9200 }
2297 };
2298
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002299 rtl_writephy_batch(tp, phy_reg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
françois romieu4da19632011-01-03 15:07:55 +00002302static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002303{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002304 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002305 { 0x1f, 0x0002 },
2306 { 0x01, 0x90d0 },
2307 { 0x1f, 0x0000 }
2308 };
2309
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002310 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002311}
2312
françois romieu4da19632011-01-03 15:07:55 +00002313static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002314{
2315 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002316
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002317 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2318 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002319 return;
2320
françois romieu4da19632011-01-03 15:07:55 +00002321 rtl_writephy(tp, 0x1f, 0x0001);
2322 rtl_writephy(tp, 0x10, 0xf01b);
2323 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002324}
2325
françois romieu4da19632011-01-03 15:07:55 +00002326static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002327{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002328 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002329 { 0x1f, 0x0001 },
2330 { 0x04, 0x0000 },
2331 { 0x03, 0x00a1 },
2332 { 0x02, 0x0008 },
2333 { 0x01, 0x0120 },
2334 { 0x00, 0x1000 },
2335 { 0x04, 0x0800 },
2336 { 0x04, 0x9000 },
2337 { 0x03, 0x802f },
2338 { 0x02, 0x4f02 },
2339 { 0x01, 0x0409 },
2340 { 0x00, 0xf099 },
2341 { 0x04, 0x9800 },
2342 { 0x04, 0xa000 },
2343 { 0x03, 0xdf01 },
2344 { 0x02, 0xdf20 },
2345 { 0x01, 0xff95 },
2346 { 0x00, 0xba00 },
2347 { 0x04, 0xa800 },
2348 { 0x04, 0xf000 },
2349 { 0x03, 0xdf01 },
2350 { 0x02, 0xdf20 },
2351 { 0x01, 0x101a },
2352 { 0x00, 0xa0ff },
2353 { 0x04, 0xf800 },
2354 { 0x04, 0x0000 },
2355 { 0x1f, 0x0000 },
2356
2357 { 0x1f, 0x0001 },
2358 { 0x10, 0xf41b },
2359 { 0x14, 0xfb54 },
2360 { 0x18, 0xf5c7 },
2361 { 0x1f, 0x0000 },
2362
2363 { 0x1f, 0x0001 },
2364 { 0x17, 0x0cc0 },
2365 { 0x1f, 0x0000 }
2366 };
2367
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002368 rtl_writephy_batch(tp, phy_reg_init);
françois romieu2e9558562009-08-10 19:44:19 +00002369
françois romieu4da19632011-01-03 15:07:55 +00002370 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002371}
2372
françois romieu4da19632011-01-03 15:07:55 +00002373static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002374{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002375 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002376 { 0x1f, 0x0001 },
2377 { 0x04, 0x0000 },
2378 { 0x03, 0x00a1 },
2379 { 0x02, 0x0008 },
2380 { 0x01, 0x0120 },
2381 { 0x00, 0x1000 },
2382 { 0x04, 0x0800 },
2383 { 0x04, 0x9000 },
2384 { 0x03, 0x802f },
2385 { 0x02, 0x4f02 },
2386 { 0x01, 0x0409 },
2387 { 0x00, 0xf099 },
2388 { 0x04, 0x9800 },
2389 { 0x04, 0xa000 },
2390 { 0x03, 0xdf01 },
2391 { 0x02, 0xdf20 },
2392 { 0x01, 0xff95 },
2393 { 0x00, 0xba00 },
2394 { 0x04, 0xa800 },
2395 { 0x04, 0xf000 },
2396 { 0x03, 0xdf01 },
2397 { 0x02, 0xdf20 },
2398 { 0x01, 0x101a },
2399 { 0x00, 0xa0ff },
2400 { 0x04, 0xf800 },
2401 { 0x04, 0x0000 },
2402 { 0x1f, 0x0000 },
2403
2404 { 0x1f, 0x0001 },
2405 { 0x0b, 0x8480 },
2406 { 0x1f, 0x0000 },
2407
2408 { 0x1f, 0x0001 },
2409 { 0x18, 0x67c7 },
2410 { 0x04, 0x2000 },
2411 { 0x03, 0x002f },
2412 { 0x02, 0x4360 },
2413 { 0x01, 0x0109 },
2414 { 0x00, 0x3022 },
2415 { 0x04, 0x2800 },
2416 { 0x1f, 0x0000 },
2417
2418 { 0x1f, 0x0001 },
2419 { 0x17, 0x0cc0 },
2420 { 0x1f, 0x0000 }
2421 };
2422
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002423 rtl_writephy_batch(tp, phy_reg_init);
françois romieu8c7006a2009-08-10 19:43:29 +00002424}
2425
françois romieu4da19632011-01-03 15:07:55 +00002426static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002427{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002428 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002429 { 0x10, 0xf41b },
2430 { 0x1f, 0x0000 }
2431 };
2432
françois romieu4da19632011-01-03 15:07:55 +00002433 rtl_writephy(tp, 0x1f, 0x0001);
2434 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002435
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002436 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002437}
2438
françois romieu4da19632011-01-03 15:07:55 +00002439static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002440{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002441 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002442 { 0x1f, 0x0001 },
2443 { 0x10, 0xf41b },
2444 { 0x1f, 0x0000 }
2445 };
2446
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002447 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002448}
2449
françois romieu4da19632011-01-03 15:07:55 +00002450static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002451{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002452 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002453 { 0x1f, 0x0000 },
2454 { 0x1d, 0x0f00 },
2455 { 0x1f, 0x0002 },
2456 { 0x0c, 0x1ec8 },
2457 { 0x1f, 0x0000 }
2458 };
2459
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002460 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu867763c2007-08-17 18:21:58 +02002461}
2462
françois romieu4da19632011-01-03 15:07:55 +00002463static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002464{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002465 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002466 { 0x1f, 0x0001 },
2467 { 0x1d, 0x3d98 },
2468 { 0x1f, 0x0000 }
2469 };
2470
françois romieu4da19632011-01-03 15:07:55 +00002471 rtl_writephy(tp, 0x1f, 0x0000);
2472 rtl_patchphy(tp, 0x14, 1 << 5);
2473 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002474
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002475 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuef3386f2008-06-29 12:24:30 +02002476}
2477
françois romieu4da19632011-01-03 15:07:55 +00002478static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002479{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002480 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002481 { 0x1f, 0x0001 },
2482 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002483 { 0x1f, 0x0002 },
2484 { 0x00, 0x88d4 },
2485 { 0x01, 0x82b1 },
2486 { 0x03, 0x7002 },
2487 { 0x08, 0x9e30 },
2488 { 0x09, 0x01f0 },
2489 { 0x0a, 0x5500 },
2490 { 0x0c, 0x00c8 },
2491 { 0x1f, 0x0003 },
2492 { 0x12, 0xc096 },
2493 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002494 { 0x1f, 0x0000 },
2495 { 0x1f, 0x0000 },
2496 { 0x09, 0x2000 },
2497 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002498 };
2499
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002500 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002501
françois romieu4da19632011-01-03 15:07:55 +00002502 rtl_patchphy(tp, 0x14, 1 << 5);
2503 rtl_patchphy(tp, 0x0d, 1 << 5);
2504 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002505}
2506
françois romieu4da19632011-01-03 15:07:55 +00002507static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002508{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002509 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002510 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002511 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002512 { 0x03, 0x802f },
2513 { 0x02, 0x4f02 },
2514 { 0x01, 0x0409 },
2515 { 0x00, 0xf099 },
2516 { 0x04, 0x9800 },
2517 { 0x04, 0x9000 },
2518 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002519 { 0x1f, 0x0002 },
2520 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002521 { 0x06, 0x0761 },
2522 { 0x1f, 0x0003 },
2523 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002524 { 0x1f, 0x0000 }
2525 };
2526
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002527 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002528
françois romieu4da19632011-01-03 15:07:55 +00002529 rtl_patchphy(tp, 0x16, 1 << 0);
2530 rtl_patchphy(tp, 0x14, 1 << 5);
2531 rtl_patchphy(tp, 0x0d, 1 << 5);
2532 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002533}
2534
françois romieu4da19632011-01-03 15:07:55 +00002535static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002536{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002537 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002538 { 0x1f, 0x0001 },
2539 { 0x12, 0x2300 },
2540 { 0x1d, 0x3d98 },
2541 { 0x1f, 0x0002 },
2542 { 0x0c, 0x7eb8 },
2543 { 0x06, 0x5461 },
2544 { 0x1f, 0x0003 },
2545 { 0x16, 0x0f0a },
2546 { 0x1f, 0x0000 }
2547 };
2548
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002549 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu197ff762008-06-28 13:16:02 +02002550
françois romieu4da19632011-01-03 15:07:55 +00002551 rtl_patchphy(tp, 0x16, 1 << 0);
2552 rtl_patchphy(tp, 0x14, 1 << 5);
2553 rtl_patchphy(tp, 0x0d, 1 << 5);
2554 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002555}
2556
françois romieu4da19632011-01-03 15:07:55 +00002557static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002558{
françois romieu4da19632011-01-03 15:07:55 +00002559 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002560}
2561
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002562static const struct phy_reg rtl8168d_1_phy_reg_init_0[] = {
2563 /* Channel Estimation */
2564 { 0x1f, 0x0001 },
2565 { 0x06, 0x4064 },
2566 { 0x07, 0x2863 },
2567 { 0x08, 0x059c },
2568 { 0x09, 0x26b4 },
2569 { 0x0a, 0x6a19 },
2570 { 0x0b, 0xdcc8 },
2571 { 0x10, 0xf06d },
2572 { 0x14, 0x7f68 },
2573 { 0x18, 0x7fd9 },
2574 { 0x1c, 0xf0ff },
2575 { 0x1d, 0x3d9c },
2576 { 0x1f, 0x0003 },
2577 { 0x12, 0xf49f },
2578 { 0x13, 0x070b },
2579 { 0x1a, 0x05ad },
2580 { 0x14, 0x94c0 },
2581
2582 /*
2583 * Tx Error Issue
2584 * Enhance line driver power
2585 */
2586 { 0x1f, 0x0002 },
2587 { 0x06, 0x5561 },
2588 { 0x1f, 0x0005 },
2589 { 0x05, 0x8332 },
2590 { 0x06, 0x5561 },
2591
2592 /*
2593 * Can not link to 1Gbps with bad cable
2594 * Decrease SNR threshold form 21.07dB to 19.04dB
2595 */
2596 { 0x1f, 0x0001 },
2597 { 0x17, 0x0cc0 },
2598
2599 { 0x1f, 0x0000 },
2600 { 0x0d, 0xf880 }
2601};
2602
2603static const struct phy_reg rtl8168d_1_phy_reg_init_1[] = {
2604 { 0x1f, 0x0002 },
2605 { 0x05, 0x669a },
2606 { 0x1f, 0x0005 },
2607 { 0x05, 0x8330 },
2608 { 0x06, 0x669a },
2609 { 0x1f, 0x0002 }
2610};
2611
françois romieubca03d52011-01-03 15:07:31 +00002612static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002613{
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002614 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
Francois Romieu5b538df2008-07-20 16:22:45 +02002615
françois romieubca03d52011-01-03 15:07:31 +00002616 /*
2617 * Rx Error Issue
2618 * Fine Tune Switching regulator parameter
2619 */
françois romieu4da19632011-01-03 15:07:55 +00002620 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002621 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2622 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002623
Francois Romieufdf6fc02012-07-06 22:40:38 +02002624 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
françois romieudaf9df62009-10-07 12:44:20 +00002625 int val;
2626
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002627 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
françois romieudaf9df62009-10-07 12:44:20 +00002628
françois romieu4da19632011-01-03 15:07:55 +00002629 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002630
2631 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002632 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002633 0x0065, 0x0066, 0x0067, 0x0068,
2634 0x0069, 0x006a, 0x006b, 0x006c
2635 };
2636 int i;
2637
françois romieu4da19632011-01-03 15:07:55 +00002638 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002639
2640 val &= 0xff00;
2641 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002642 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002643 }
2644 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002645 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002646 { 0x1f, 0x0002 },
2647 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002648 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002649 { 0x05, 0x8330 },
2650 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002651 };
2652
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002653 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02002654 }
2655
françois romieubca03d52011-01-03 15:07:31 +00002656 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002657 rtl_writephy(tp, 0x1f, 0x0002);
2658 rtl_patchphy(tp, 0x0d, 0x0300);
2659 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002660
françois romieubca03d52011-01-03 15:07:31 +00002661 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002662 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002663 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2664 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002665
françois romieu4da19632011-01-03 15:07:55 +00002666 rtl_writephy(tp, 0x1f, 0x0005);
2667 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002668
2669 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002670
françois romieu4da19632011-01-03 15:07:55 +00002671 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002672}
2673
françois romieubca03d52011-01-03 15:07:31 +00002674static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002675{
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002676 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
françois romieudaf9df62009-10-07 12:44:20 +00002677
Francois Romieufdf6fc02012-07-06 22:40:38 +02002678 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
françois romieudaf9df62009-10-07 12:44:20 +00002679 int val;
2680
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002681 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
françois romieudaf9df62009-10-07 12:44:20 +00002682
françois romieu4da19632011-01-03 15:07:55 +00002683 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002684 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002685 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002686 0x0065, 0x0066, 0x0067, 0x0068,
2687 0x0069, 0x006a, 0x006b, 0x006c
2688 };
2689 int i;
2690
françois romieu4da19632011-01-03 15:07:55 +00002691 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002692
2693 val &= 0xff00;
2694 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002695 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002696 }
2697 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002698 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002699 { 0x1f, 0x0002 },
2700 { 0x05, 0x2642 },
2701 { 0x1f, 0x0005 },
2702 { 0x05, 0x8330 },
2703 { 0x06, 0x2642 }
2704 };
2705
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002706 rtl_writephy_batch(tp, phy_reg_init);
françois romieudaf9df62009-10-07 12:44:20 +00002707 }
2708
françois romieubca03d52011-01-03 15:07:31 +00002709 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002710 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002711 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2712 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002713
françois romieubca03d52011-01-03 15:07:31 +00002714 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002715 rtl_writephy(tp, 0x1f, 0x0002);
2716 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002717
françois romieu4da19632011-01-03 15:07:55 +00002718 rtl_writephy(tp, 0x1f, 0x0005);
2719 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002720
2721 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002722
françois romieu4da19632011-01-03 15:07:55 +00002723 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002724}
2725
françois romieu4da19632011-01-03 15:07:55 +00002726static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002727{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002728 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002729 { 0x1f, 0x0002 },
2730 { 0x10, 0x0008 },
2731 { 0x0d, 0x006c },
2732
2733 { 0x1f, 0x0000 },
2734 { 0x0d, 0xf880 },
2735
2736 { 0x1f, 0x0001 },
2737 { 0x17, 0x0cc0 },
2738
2739 { 0x1f, 0x0001 },
2740 { 0x0b, 0xa4d8 },
2741 { 0x09, 0x281c },
2742 { 0x07, 0x2883 },
2743 { 0x0a, 0x6b35 },
2744 { 0x1d, 0x3da4 },
2745 { 0x1c, 0xeffd },
2746 { 0x14, 0x7f52 },
2747 { 0x18, 0x7fc6 },
2748 { 0x08, 0x0601 },
2749 { 0x06, 0x4063 },
2750 { 0x10, 0xf074 },
2751 { 0x1f, 0x0003 },
2752 { 0x13, 0x0789 },
2753 { 0x12, 0xf4bd },
2754 { 0x1a, 0x04fd },
2755 { 0x14, 0x84b0 },
2756 { 0x1f, 0x0000 },
2757 { 0x00, 0x9200 },
2758
2759 { 0x1f, 0x0005 },
2760 { 0x01, 0x0340 },
2761 { 0x1f, 0x0001 },
2762 { 0x04, 0x4000 },
2763 { 0x03, 0x1d21 },
2764 { 0x02, 0x0c32 },
2765 { 0x01, 0x0200 },
2766 { 0x00, 0x5554 },
2767 { 0x04, 0x4800 },
2768 { 0x04, 0x4000 },
2769 { 0x04, 0xf000 },
2770 { 0x03, 0xdf01 },
2771 { 0x02, 0xdf20 },
2772 { 0x01, 0x101a },
2773 { 0x00, 0xa0ff },
2774 { 0x04, 0xf800 },
2775 { 0x04, 0xf000 },
2776 { 0x1f, 0x0000 },
2777
2778 { 0x1f, 0x0007 },
2779 { 0x1e, 0x0023 },
2780 { 0x16, 0x0000 },
2781 { 0x1f, 0x0000 }
2782 };
2783
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002784 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02002785}
2786
françois romieue6de30d2011-01-03 15:08:37 +00002787static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2788{
2789 static const struct phy_reg phy_reg_init[] = {
2790 { 0x1f, 0x0001 },
2791 { 0x17, 0x0cc0 },
2792
2793 { 0x1f, 0x0007 },
2794 { 0x1e, 0x002d },
2795 { 0x18, 0x0040 },
2796 { 0x1f, 0x0000 }
2797 };
2798
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002799 rtl_writephy_batch(tp, phy_reg_init);
françois romieue6de30d2011-01-03 15:08:37 +00002800 rtl_patchphy(tp, 0x0d, 1 << 5);
2801}
2802
Hayes Wang70090422011-07-06 15:58:06 +08002803static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002804{
2805 static const struct phy_reg phy_reg_init[] = {
2806 /* Enable Delay cap */
2807 { 0x1f, 0x0005 },
2808 { 0x05, 0x8b80 },
2809 { 0x06, 0xc896 },
2810 { 0x1f, 0x0000 },
2811
2812 /* Channel estimation fine tune */
2813 { 0x1f, 0x0001 },
2814 { 0x0b, 0x6c20 },
2815 { 0x07, 0x2872 },
2816 { 0x1c, 0xefff },
2817 { 0x1f, 0x0003 },
2818 { 0x14, 0x6420 },
2819 { 0x1f, 0x0000 },
2820
2821 /* Update PFM & 10M TX idle timer */
2822 { 0x1f, 0x0007 },
2823 { 0x1e, 0x002f },
2824 { 0x15, 0x1919 },
2825 { 0x1f, 0x0000 },
2826
2827 { 0x1f, 0x0007 },
2828 { 0x1e, 0x00ac },
2829 { 0x18, 0x0006 },
2830 { 0x1f, 0x0000 }
2831 };
2832
Francois Romieu15ecd032011-04-27 13:52:22 -07002833 rtl_apply_firmware(tp);
2834
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002835 rtl_writephy_batch(tp, phy_reg_init);
hayeswang01dc7fe2011-03-21 01:50:28 +00002836
2837 /* DCO enable for 10M IDLE Power */
2838 rtl_writephy(tp, 0x1f, 0x0007);
2839 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002840 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002841 rtl_writephy(tp, 0x1f, 0x0000);
2842
2843 /* For impedance matching */
2844 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002845 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002846 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002847
2848 /* PHY auto speed down */
2849 rtl_writephy(tp, 0x1f, 0x0007);
2850 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002851 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002852 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002853 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002854
2855 rtl_writephy(tp, 0x1f, 0x0005);
2856 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002857 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002858 rtl_writephy(tp, 0x1f, 0x0000);
2859
2860 rtl_writephy(tp, 0x1f, 0x0005);
2861 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002862 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002863 rtl_writephy(tp, 0x1f, 0x0007);
2864 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002865 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00002866 rtl_writephy(tp, 0x1f, 0x0006);
2867 rtl_writephy(tp, 0x00, 0x5a00);
2868 rtl_writephy(tp, 0x1f, 0x0000);
2869 rtl_writephy(tp, 0x0d, 0x0007);
2870 rtl_writephy(tp, 0x0e, 0x003c);
2871 rtl_writephy(tp, 0x0d, 0x4007);
2872 rtl_writephy(tp, 0x0e, 0x0000);
2873 rtl_writephy(tp, 0x0d, 0x0000);
2874}
2875
françois romieu9ecb9aa2012-12-07 11:20:21 +00002876static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
2877{
2878 const u16 w[] = {
2879 addr[0] | (addr[1] << 8),
2880 addr[2] | (addr[3] << 8),
2881 addr[4] | (addr[5] << 8)
2882 };
françois romieu9ecb9aa2012-12-07 11:20:21 +00002883
Heiner Kallweit3aa4c492019-05-02 20:46:52 +02002884 rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, w[0] | (w[1] << 16));
2885 rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, w[2]);
2886 rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, w[0] << 16);
2887 rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, w[1] | (w[2] << 16));
françois romieu9ecb9aa2012-12-07 11:20:21 +00002888}
2889
Hayes Wang70090422011-07-06 15:58:06 +08002890static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2891{
2892 static const struct phy_reg phy_reg_init[] = {
2893 /* Enable Delay cap */
2894 { 0x1f, 0x0004 },
2895 { 0x1f, 0x0007 },
2896 { 0x1e, 0x00ac },
2897 { 0x18, 0x0006 },
2898 { 0x1f, 0x0002 },
2899 { 0x1f, 0x0000 },
2900 { 0x1f, 0x0000 },
2901
2902 /* Channel estimation fine tune */
2903 { 0x1f, 0x0003 },
2904 { 0x09, 0xa20f },
2905 { 0x1f, 0x0000 },
2906 { 0x1f, 0x0000 },
2907
2908 /* Green Setting */
2909 { 0x1f, 0x0005 },
2910 { 0x05, 0x8b5b },
2911 { 0x06, 0x9222 },
2912 { 0x05, 0x8b6d },
2913 { 0x06, 0x8000 },
2914 { 0x05, 0x8b76 },
2915 { 0x06, 0x8000 },
2916 { 0x1f, 0x0000 }
2917 };
2918
2919 rtl_apply_firmware(tp);
2920
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002921 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang70090422011-07-06 15:58:06 +08002922
2923 /* For 4-corner performance improve */
2924 rtl_writephy(tp, 0x1f, 0x0005);
2925 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002926 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002927 rtl_writephy(tp, 0x1f, 0x0000);
2928
2929 /* PHY auto speed down */
2930 rtl_writephy(tp, 0x1f, 0x0004);
2931 rtl_writephy(tp, 0x1f, 0x0007);
2932 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002933 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002934 rtl_writephy(tp, 0x1f, 0x0002);
2935 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002936 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002937
2938 /* improve 10M EEE waveform */
2939 rtl_writephy(tp, 0x1f, 0x0005);
2940 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002941 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002942 rtl_writephy(tp, 0x1f, 0x0000);
2943
2944 /* Improve 2-pair detection performance */
2945 rtl_writephy(tp, 0x1f, 0x0005);
2946 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002947 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002948 rtl_writephy(tp, 0x1f, 0x0000);
2949
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002950 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01002951 rtl_enable_eee(tp);
Hayes Wang70090422011-07-06 15:58:06 +08002952
2953 /* Green feature */
2954 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01002955 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
2956 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002957 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01002958 rtl_writephy(tp, 0x1f, 0x0005);
2959 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
2960 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00002961
françois romieu9ecb9aa2012-12-07 11:20:21 +00002962 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
2963 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08002964}
2965
Hayes Wang5f886e02012-03-30 14:33:03 +08002966static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
2967{
2968 /* For 4-corner performance improve */
2969 rtl_writephy(tp, 0x1f, 0x0005);
2970 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002971 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002972 rtl_writephy(tp, 0x1f, 0x0000);
2973
2974 /* PHY auto speed down */
2975 rtl_writephy(tp, 0x1f, 0x0007);
2976 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002977 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002978 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002979 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002980
2981 /* Improve 10M EEE waveform */
2982 rtl_writephy(tp, 0x1f, 0x0005);
2983 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002984 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002985 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002986
2987 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01002988 rtl_enable_eee(tp);
Hayes Wang5f886e02012-03-30 14:33:03 +08002989}
2990
Hayes Wangc2218922011-09-06 16:55:18 +08002991static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2992{
2993 static const struct phy_reg phy_reg_init[] = {
2994 /* Channel estimation fine tune */
2995 { 0x1f, 0x0003 },
2996 { 0x09, 0xa20f },
2997 { 0x1f, 0x0000 },
2998
2999 /* Modify green table for giga & fnet */
3000 { 0x1f, 0x0005 },
3001 { 0x05, 0x8b55 },
3002 { 0x06, 0x0000 },
3003 { 0x05, 0x8b5e },
3004 { 0x06, 0x0000 },
3005 { 0x05, 0x8b67 },
3006 { 0x06, 0x0000 },
3007 { 0x05, 0x8b70 },
3008 { 0x06, 0x0000 },
3009 { 0x1f, 0x0000 },
3010 { 0x1f, 0x0007 },
3011 { 0x1e, 0x0078 },
3012 { 0x17, 0x0000 },
3013 { 0x19, 0x00fb },
3014 { 0x1f, 0x0000 },
3015
3016 /* Modify green table for 10M */
3017 { 0x1f, 0x0005 },
3018 { 0x05, 0x8b79 },
3019 { 0x06, 0xaa00 },
3020 { 0x1f, 0x0000 },
3021
3022 /* Disable hiimpedance detection (RTCT) */
3023 { 0x1f, 0x0003 },
3024 { 0x01, 0x328a },
3025 { 0x1f, 0x0000 }
3026 };
3027
3028 rtl_apply_firmware(tp);
3029
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003030 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangc2218922011-09-06 16:55:18 +08003031
Hayes Wang5f886e02012-03-30 14:33:03 +08003032 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003033
3034 /* Improve 2-pair detection performance */
3035 rtl_writephy(tp, 0x1f, 0x0005);
3036 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003037 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003038 rtl_writephy(tp, 0x1f, 0x0000);
3039}
3040
3041static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3042{
3043 rtl_apply_firmware(tp);
3044
Hayes Wang5f886e02012-03-30 14:33:03 +08003045 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003046}
3047
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003048static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3049{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003050 static const struct phy_reg phy_reg_init[] = {
3051 /* Channel estimation fine tune */
3052 { 0x1f, 0x0003 },
3053 { 0x09, 0xa20f },
3054 { 0x1f, 0x0000 },
3055
3056 /* Modify green table for giga & fnet */
3057 { 0x1f, 0x0005 },
3058 { 0x05, 0x8b55 },
3059 { 0x06, 0x0000 },
3060 { 0x05, 0x8b5e },
3061 { 0x06, 0x0000 },
3062 { 0x05, 0x8b67 },
3063 { 0x06, 0x0000 },
3064 { 0x05, 0x8b70 },
3065 { 0x06, 0x0000 },
3066 { 0x1f, 0x0000 },
3067 { 0x1f, 0x0007 },
3068 { 0x1e, 0x0078 },
3069 { 0x17, 0x0000 },
3070 { 0x19, 0x00aa },
3071 { 0x1f, 0x0000 },
3072
3073 /* Modify green table for 10M */
3074 { 0x1f, 0x0005 },
3075 { 0x05, 0x8b79 },
3076 { 0x06, 0xaa00 },
3077 { 0x1f, 0x0000 },
3078
3079 /* Disable hiimpedance detection (RTCT) */
3080 { 0x1f, 0x0003 },
3081 { 0x01, 0x328a },
3082 { 0x1f, 0x0000 }
3083 };
3084
3085
3086 rtl_apply_firmware(tp);
3087
3088 rtl8168f_hw_phy_config(tp);
3089
3090 /* Improve 2-pair detection performance */
3091 rtl_writephy(tp, 0x1f, 0x0005);
3092 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003093 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003094 rtl_writephy(tp, 0x1f, 0x0000);
3095
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003096 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003097
3098 /* Modify green table for giga */
3099 rtl_writephy(tp, 0x1f, 0x0005);
3100 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003101 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003102 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003103 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003104 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003105 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003106 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003107 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003108 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003109 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003110 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003111 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003112 rtl_writephy(tp, 0x05, 0x8a88);
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, 0x1f, 0x0000);
3115
3116 /* uc same-seed solution */
3117 rtl_writephy(tp, 0x1f, 0x0005);
3118 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003119 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003120 rtl_writephy(tp, 0x1f, 0x0000);
3121
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003122 /* Green feature */
3123 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003124 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3125 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003126 rtl_writephy(tp, 0x1f, 0x0000);
3127}
3128
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003129static void rtl8168g_disable_aldps(struct rtl8169_private *tp)
3130{
Heiner Kallweita2928d22019-06-02 10:53:49 +02003131 phy_modify_paged(tp->phydev, 0x0a43, 0x10, BIT(2), 0);
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003132}
3133
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003134static void rtl8168g_phy_adjust_10m_aldps(struct rtl8169_private *tp)
3135{
3136 struct phy_device *phydev = tp->phydev;
3137
Heiner Kallweita2928d22019-06-02 10:53:49 +02003138 phy_modify_paged(phydev, 0x0bcc, 0x14, BIT(8), 0);
3139 phy_modify_paged(phydev, 0x0a44, 0x11, 0, BIT(7) | BIT(6));
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003140 phy_write(phydev, 0x1f, 0x0a43);
3141 phy_write(phydev, 0x13, 0x8084);
3142 phy_clear_bits(phydev, 0x14, BIT(14) | BIT(13));
3143 phy_set_bits(phydev, 0x10, BIT(12) | BIT(1) | BIT(0));
3144
3145 phy_write(phydev, 0x1f, 0x0000);
3146}
3147
Hayes Wangc5583862012-07-02 17:23:22 +08003148static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3149{
Heiner Kallweita2928d22019-06-02 10:53:49 +02003150 int ret;
3151
Hayes Wangc5583862012-07-02 17:23:22 +08003152 rtl_apply_firmware(tp);
3153
Heiner Kallweita2928d22019-06-02 10:53:49 +02003154 ret = phy_read_paged(tp->phydev, 0x0a46, 0x10);
3155 if (ret & BIT(8))
3156 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, BIT(15), 0);
3157 else
3158 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, 0, BIT(15));
Hayes Wangc5583862012-07-02 17:23:22 +08003159
Heiner Kallweita2928d22019-06-02 10:53:49 +02003160 ret = phy_read_paged(tp->phydev, 0x0a46, 0x13);
3161 if (ret & BIT(8))
Thomas Voegtle1a03bb52019-07-20 19:01:22 +02003162 phy_modify_paged(tp->phydev, 0x0c41, 0x15, 0, BIT(1));
Heiner Kallweita2928d22019-06-02 10:53:49 +02003163 else
Thomas Voegtle1a03bb52019-07-20 19:01:22 +02003164 phy_modify_paged(tp->phydev, 0x0c41, 0x15, BIT(1), 0);
Hayes Wangc5583862012-07-02 17:23:22 +08003165
hayeswang41f44d12013-04-01 22:23:36 +00003166 /* Enable PHY auto speed down */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003167 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
Hayes Wangc5583862012-07-02 17:23:22 +08003168
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003169 rtl8168g_phy_adjust_10m_aldps(tp);
hayeswangfe7524c2013-04-01 22:23:37 +00003170
hayeswang41f44d12013-04-01 22:23:36 +00003171 /* EEE auto-fallback function */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003172 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
Hayes Wangc5583862012-07-02 17:23:22 +08003173
hayeswang41f44d12013-04-01 22:23:36 +00003174 /* Enable UC LPF tune function */
3175 rtl_writephy(tp, 0x1f, 0x0a43);
3176 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003177 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003178
Heiner Kallweita2928d22019-06-02 10:53:49 +02003179 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
hayeswang41f44d12013-04-01 22:23:36 +00003180
hayeswangfe7524c2013-04-01 22:23:37 +00003181 /* Improve SWR Efficiency */
3182 rtl_writephy(tp, 0x1f, 0x0bcd);
3183 rtl_writephy(tp, 0x14, 0x5065);
3184 rtl_writephy(tp, 0x14, 0xd065);
3185 rtl_writephy(tp, 0x1f, 0x0bc8);
3186 rtl_writephy(tp, 0x11, 0x5655);
3187 rtl_writephy(tp, 0x1f, 0x0bcd);
3188 rtl_writephy(tp, 0x14, 0x1065);
3189 rtl_writephy(tp, 0x14, 0x9065);
3190 rtl_writephy(tp, 0x14, 0x1065);
Heiner Kallweita2928d22019-06-02 10:53:49 +02003191 rtl_writephy(tp, 0x1f, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003192
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003193 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003194 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003195 rtl_enable_eee(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08003196}
3197
hayeswang57538c42013-04-01 22:23:40 +00003198static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3199{
3200 rtl_apply_firmware(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003201 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003202 rtl_enable_eee(tp);
hayeswang57538c42013-04-01 22:23:40 +00003203}
3204
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003205static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3206{
3207 u16 dout_tapbin;
3208 u32 data;
3209
3210 rtl_apply_firmware(tp);
3211
3212 /* CHN EST parameters adjust - giga master */
3213 rtl_writephy(tp, 0x1f, 0x0a43);
3214 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003215 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003216 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003217 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003218 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003219 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003220 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003221 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003222 rtl_writephy(tp, 0x1f, 0x0000);
3223
3224 /* CHN EST parameters adjust - giga slave */
3225 rtl_writephy(tp, 0x1f, 0x0a43);
3226 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003227 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003228 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003229 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003230 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003231 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003232 rtl_writephy(tp, 0x1f, 0x0000);
3233
3234 /* CHN EST parameters adjust - fnet */
3235 rtl_writephy(tp, 0x1f, 0x0a43);
3236 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003237 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003238 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003239 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003240 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003241 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003242 rtl_writephy(tp, 0x1f, 0x0000);
3243
3244 /* enable R-tune & PGA-retune function */
3245 dout_tapbin = 0;
3246 rtl_writephy(tp, 0x1f, 0x0a46);
3247 data = rtl_readphy(tp, 0x13);
3248 data &= 3;
3249 data <<= 2;
3250 dout_tapbin |= data;
3251 data = rtl_readphy(tp, 0x12);
3252 data &= 0xc000;
3253 data >>= 14;
3254 dout_tapbin |= data;
3255 dout_tapbin = ~(dout_tapbin^0x08);
3256 dout_tapbin <<= 12;
3257 dout_tapbin &= 0xf000;
3258 rtl_writephy(tp, 0x1f, 0x0a43);
3259 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003260 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003261 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003262 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003263 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003264 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003265 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003266 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003267
3268 rtl_writephy(tp, 0x1f, 0x0a43);
3269 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003270 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003271 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003272 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003273 rtl_writephy(tp, 0x1f, 0x0000);
3274
3275 /* enable GPHY 10M */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003276 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003277
3278 /* SAR ADC performance */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003279 phy_modify_paged(tp->phydev, 0x0bca, 0x17, BIT(12) | BIT(13), BIT(14));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003280
3281 rtl_writephy(tp, 0x1f, 0x0a43);
3282 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003283 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003284 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003285 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003286 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003287 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003288 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003289 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003290 rtl_writephy(tp, 0x13, 0x805f);
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, 0x8067);
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, 0x806f);
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, 0x1f, 0x0000);
3297
3298 /* disable phy pfm mode */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003299 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003300
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003301 rtl8168g_disable_aldps(tp);
Heiner Kallweitb6cef262019-08-15 14:21:30 +02003302 rtl8168h_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003303 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003304}
3305
3306static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3307{
3308 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3309 u16 rlen;
3310 u32 data;
3311
3312 rtl_apply_firmware(tp);
3313
3314 /* CHIN EST parameter update */
3315 rtl_writephy(tp, 0x1f, 0x0a43);
3316 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003317 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003318 rtl_writephy(tp, 0x1f, 0x0000);
3319
3320 /* enable R-tune & PGA-retune function */
3321 rtl_writephy(tp, 0x1f, 0x0a43);
3322 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003323 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003324 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003325 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003326 rtl_writephy(tp, 0x1f, 0x0000);
3327
3328 /* enable GPHY 10M */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003329 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003330
3331 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3332 data = r8168_mac_ocp_read(tp, 0xdd02);
3333 ioffset_p3 = ((data & 0x80)>>7);
3334 ioffset_p3 <<= 3;
3335
3336 data = r8168_mac_ocp_read(tp, 0xdd00);
3337 ioffset_p3 |= ((data & (0xe000))>>13);
3338 ioffset_p2 = ((data & (0x1e00))>>9);
3339 ioffset_p1 = ((data & (0x01e0))>>5);
3340 ioffset_p0 = ((data & 0x0010)>>4);
3341 ioffset_p0 <<= 3;
3342 ioffset_p0 |= (data & (0x07));
3343 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3344
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003345 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003346 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003347 rtl_writephy(tp, 0x1f, 0x0bcf);
3348 rtl_writephy(tp, 0x16, data);
3349 rtl_writephy(tp, 0x1f, 0x0000);
3350 }
3351
3352 /* Modify rlen (TX LPF corner frequency) level */
3353 rtl_writephy(tp, 0x1f, 0x0bcd);
3354 data = rtl_readphy(tp, 0x16);
3355 data &= 0x000f;
3356 rlen = 0;
3357 if (data > 3)
3358 rlen = data - 3;
3359 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3360 rtl_writephy(tp, 0x17, data);
3361 rtl_writephy(tp, 0x1f, 0x0bcd);
3362 rtl_writephy(tp, 0x1f, 0x0000);
3363
3364 /* disable phy pfm mode */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003365 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003366
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003367 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003368 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003369 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003370}
3371
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003372static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3373{
3374 /* Enable PHY auto speed down */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003375 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003376
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003377 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003378
3379 /* Enable EEE auto-fallback function */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003380 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003381
3382 /* Enable UC LPF tune function */
3383 rtl_writephy(tp, 0x1f, 0x0a43);
3384 rtl_writephy(tp, 0x13, 0x8012);
3385 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3386 rtl_writephy(tp, 0x1f, 0x0000);
3387
3388 /* set rg_sel_sdm_rate */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003389 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003390
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003391 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003392 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003393 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003394}
3395
3396static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3397{
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003398 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003399
3400 /* Enable UC LPF tune function */
3401 rtl_writephy(tp, 0x1f, 0x0a43);
3402 rtl_writephy(tp, 0x13, 0x8012);
3403 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3404 rtl_writephy(tp, 0x1f, 0x0000);
3405
3406 /* Set rg_sel_sdm_rate */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003407 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003408
3409 /* Channel estimation parameters */
3410 rtl_writephy(tp, 0x1f, 0x0a43);
3411 rtl_writephy(tp, 0x13, 0x80f3);
3412 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3413 rtl_writephy(tp, 0x13, 0x80f0);
3414 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3415 rtl_writephy(tp, 0x13, 0x80ef);
3416 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3417 rtl_writephy(tp, 0x13, 0x80f6);
3418 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3419 rtl_writephy(tp, 0x13, 0x80ec);
3420 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3421 rtl_writephy(tp, 0x13, 0x80ed);
3422 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3423 rtl_writephy(tp, 0x13, 0x80f2);
3424 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3425 rtl_writephy(tp, 0x13, 0x80f4);
3426 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3427 rtl_writephy(tp, 0x1f, 0x0a43);
3428 rtl_writephy(tp, 0x13, 0x8110);
3429 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3430 rtl_writephy(tp, 0x13, 0x810f);
3431 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3432 rtl_writephy(tp, 0x13, 0x8111);
3433 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3434 rtl_writephy(tp, 0x13, 0x8113);
3435 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3436 rtl_writephy(tp, 0x13, 0x8115);
3437 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3438 rtl_writephy(tp, 0x13, 0x810e);
3439 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3440 rtl_writephy(tp, 0x13, 0x810c);
3441 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3442 rtl_writephy(tp, 0x13, 0x810b);
3443 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3444 rtl_writephy(tp, 0x1f, 0x0a43);
3445 rtl_writephy(tp, 0x13, 0x80d1);
3446 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3447 rtl_writephy(tp, 0x13, 0x80cd);
3448 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3449 rtl_writephy(tp, 0x13, 0x80d3);
3450 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3451 rtl_writephy(tp, 0x13, 0x80d5);
3452 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3453 rtl_writephy(tp, 0x13, 0x80d7);
3454 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3455
3456 /* Force PWM-mode */
3457 rtl_writephy(tp, 0x1f, 0x0bcd);
3458 rtl_writephy(tp, 0x14, 0x5065);
3459 rtl_writephy(tp, 0x14, 0xd065);
3460 rtl_writephy(tp, 0x1f, 0x0bc8);
3461 rtl_writephy(tp, 0x12, 0x00ed);
3462 rtl_writephy(tp, 0x1f, 0x0bcd);
3463 rtl_writephy(tp, 0x14, 0x1065);
3464 rtl_writephy(tp, 0x14, 0x9065);
3465 rtl_writephy(tp, 0x14, 0x1065);
3466 rtl_writephy(tp, 0x1f, 0x0000);
3467
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003468 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003469 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003470 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003471}
3472
françois romieu4da19632011-01-03 15:07:55 +00003473static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003474{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003475 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003476 { 0x1f, 0x0003 },
3477 { 0x08, 0x441d },
3478 { 0x01, 0x9100 },
3479 { 0x1f, 0x0000 }
3480 };
3481
françois romieu4da19632011-01-03 15:07:55 +00003482 rtl_writephy(tp, 0x1f, 0x0000);
3483 rtl_patchphy(tp, 0x11, 1 << 12);
3484 rtl_patchphy(tp, 0x19, 1 << 13);
3485 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003486
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003487 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003488}
3489
Hayes Wang5a5e4442011-02-22 17:26:21 +08003490static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3491{
3492 static const struct phy_reg phy_reg_init[] = {
3493 { 0x1f, 0x0005 },
3494 { 0x1a, 0x0000 },
3495 { 0x1f, 0x0000 },
3496
3497 { 0x1f, 0x0004 },
3498 { 0x1c, 0x0000 },
3499 { 0x1f, 0x0000 },
3500
3501 { 0x1f, 0x0001 },
3502 { 0x15, 0x7701 },
3503 { 0x1f, 0x0000 }
3504 };
3505
3506 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003507 rtl_writephy(tp, 0x1f, 0x0000);
3508 rtl_writephy(tp, 0x18, 0x0310);
3509 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003510
François Romieu953a12c2011-04-24 17:38:48 +02003511 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003512
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003513 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003514}
3515
Hayes Wang7e18dca2012-03-30 14:33:02 +08003516static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3517{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003518 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003519 rtl_writephy(tp, 0x1f, 0x0000);
3520 rtl_writephy(tp, 0x18, 0x0310);
3521 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003522
3523 rtl_apply_firmware(tp);
3524
3525 /* EEE setting */
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003526 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003527 rtl_writephy(tp, 0x1f, 0x0004);
3528 rtl_writephy(tp, 0x10, 0x401f);
3529 rtl_writephy(tp, 0x19, 0x7030);
3530 rtl_writephy(tp, 0x1f, 0x0000);
3531}
3532
Hayes Wang5598bfe2012-07-02 17:23:21 +08003533static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3534{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003535 static const struct phy_reg phy_reg_init[] = {
3536 { 0x1f, 0x0004 },
3537 { 0x10, 0xc07f },
3538 { 0x19, 0x7030 },
3539 { 0x1f, 0x0000 }
3540 };
3541
3542 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003543 rtl_writephy(tp, 0x1f, 0x0000);
3544 rtl_writephy(tp, 0x18, 0x0310);
3545 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003546
3547 rtl_apply_firmware(tp);
3548
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003549 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003550 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003551
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003552 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003553}
3554
Francois Romieu5615d9f2007-08-17 17:50:46 +02003555static void rtl_hw_phy_config(struct net_device *dev)
3556{
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003557 static const rtl_generic_fct phy_configs[] = {
3558 /* PCI devices. */
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003559 [RTL_GIGA_MAC_VER_02] = rtl8169s_hw_phy_config,
3560 [RTL_GIGA_MAC_VER_03] = rtl8169s_hw_phy_config,
3561 [RTL_GIGA_MAC_VER_04] = rtl8169sb_hw_phy_config,
3562 [RTL_GIGA_MAC_VER_05] = rtl8169scd_hw_phy_config,
3563 [RTL_GIGA_MAC_VER_06] = rtl8169sce_hw_phy_config,
3564 /* PCI-E devices. */
3565 [RTL_GIGA_MAC_VER_07] = rtl8102e_hw_phy_config,
3566 [RTL_GIGA_MAC_VER_08] = rtl8102e_hw_phy_config,
3567 [RTL_GIGA_MAC_VER_09] = rtl8102e_hw_phy_config,
3568 [RTL_GIGA_MAC_VER_10] = NULL,
3569 [RTL_GIGA_MAC_VER_11] = rtl8168bb_hw_phy_config,
3570 [RTL_GIGA_MAC_VER_12] = rtl8168bef_hw_phy_config,
3571 [RTL_GIGA_MAC_VER_13] = NULL,
3572 [RTL_GIGA_MAC_VER_14] = NULL,
3573 [RTL_GIGA_MAC_VER_15] = NULL,
3574 [RTL_GIGA_MAC_VER_16] = NULL,
3575 [RTL_GIGA_MAC_VER_17] = rtl8168bef_hw_phy_config,
3576 [RTL_GIGA_MAC_VER_18] = rtl8168cp_1_hw_phy_config,
3577 [RTL_GIGA_MAC_VER_19] = rtl8168c_1_hw_phy_config,
3578 [RTL_GIGA_MAC_VER_20] = rtl8168c_2_hw_phy_config,
3579 [RTL_GIGA_MAC_VER_21] = rtl8168c_3_hw_phy_config,
3580 [RTL_GIGA_MAC_VER_22] = rtl8168c_4_hw_phy_config,
3581 [RTL_GIGA_MAC_VER_23] = rtl8168cp_2_hw_phy_config,
3582 [RTL_GIGA_MAC_VER_24] = rtl8168cp_2_hw_phy_config,
3583 [RTL_GIGA_MAC_VER_25] = rtl8168d_1_hw_phy_config,
3584 [RTL_GIGA_MAC_VER_26] = rtl8168d_2_hw_phy_config,
3585 [RTL_GIGA_MAC_VER_27] = rtl8168d_3_hw_phy_config,
3586 [RTL_GIGA_MAC_VER_28] = rtl8168d_4_hw_phy_config,
3587 [RTL_GIGA_MAC_VER_29] = rtl8105e_hw_phy_config,
3588 [RTL_GIGA_MAC_VER_30] = rtl8105e_hw_phy_config,
3589 [RTL_GIGA_MAC_VER_31] = NULL,
3590 [RTL_GIGA_MAC_VER_32] = rtl8168e_1_hw_phy_config,
3591 [RTL_GIGA_MAC_VER_33] = rtl8168e_1_hw_phy_config,
3592 [RTL_GIGA_MAC_VER_34] = rtl8168e_2_hw_phy_config,
3593 [RTL_GIGA_MAC_VER_35] = rtl8168f_1_hw_phy_config,
3594 [RTL_GIGA_MAC_VER_36] = rtl8168f_2_hw_phy_config,
3595 [RTL_GIGA_MAC_VER_37] = rtl8402_hw_phy_config,
3596 [RTL_GIGA_MAC_VER_38] = rtl8411_hw_phy_config,
3597 [RTL_GIGA_MAC_VER_39] = rtl8106e_hw_phy_config,
3598 [RTL_GIGA_MAC_VER_40] = rtl8168g_1_hw_phy_config,
3599 [RTL_GIGA_MAC_VER_41] = NULL,
3600 [RTL_GIGA_MAC_VER_42] = rtl8168g_2_hw_phy_config,
3601 [RTL_GIGA_MAC_VER_43] = rtl8168g_2_hw_phy_config,
3602 [RTL_GIGA_MAC_VER_44] = rtl8168g_2_hw_phy_config,
3603 [RTL_GIGA_MAC_VER_45] = rtl8168h_1_hw_phy_config,
3604 [RTL_GIGA_MAC_VER_46] = rtl8168h_2_hw_phy_config,
3605 [RTL_GIGA_MAC_VER_47] = rtl8168h_1_hw_phy_config,
3606 [RTL_GIGA_MAC_VER_48] = rtl8168h_2_hw_phy_config,
3607 [RTL_GIGA_MAC_VER_49] = rtl8168ep_1_hw_phy_config,
3608 [RTL_GIGA_MAC_VER_50] = rtl8168ep_2_hw_phy_config,
3609 [RTL_GIGA_MAC_VER_51] = rtl8168ep_2_hw_phy_config,
3610 };
Francois Romieu5615d9f2007-08-17 17:50:46 +02003611 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003612
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003613 if (phy_configs[tp->mac_version])
3614 phy_configs[tp->mac_version](tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003615}
3616
Francois Romieuda78dbf2012-01-26 14:18:23 +01003617static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3618{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003619 if (!test_and_set_bit(flag, tp->wk.flags))
3620 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003621}
3622
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003623static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
Francois Romieu5615d9f2007-08-17 17:50:46 +02003625 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003626
Marcus Sundberg773328942008-07-10 21:28:08 +02003627 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit7a67e112018-09-19 22:00:24 +02003628 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3629 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Heiner Kallweit49d17512018-06-28 20:36:15 +02003630 netif_dbg(tp, drv, dev,
3631 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003632 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02003633 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003634
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003635 /* We may have called phy_speed_down before */
Heiner Kallweit703732f2019-01-19 22:07:05 +01003636 phy_speed_up(tp->phydev);
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003637
Heiner Kallweit703732f2019-01-19 22:07:05 +01003638 genphy_soft_reset(tp->phydev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003639}
3640
Francois Romieu773d2022007-01-31 23:47:43 +01003641static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3642{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003643 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003644
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01003645 rtl_unlock_config_regs(tp);
françois romieu908ba2bf2010-04-26 11:42:58 +00003646
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003647 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
3648 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00003649
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003650 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
3651 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00003652
françois romieu9ecb9aa2012-12-07 11:20:21 +00003653 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3654 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00003655
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01003656 rtl_lock_config_regs(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003657
Francois Romieuda78dbf2012-01-26 14:18:23 +01003658 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003659}
3660
3661static int rtl_set_mac_address(struct net_device *dev, void *p)
3662{
3663 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01003664 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01003665 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01003666
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01003667 ret = eth_mac_addr(dev, p);
3668 if (ret)
3669 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01003670
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08003671 pm_runtime_get_noresume(d);
3672
3673 if (pm_runtime_active(d))
3674 rtl_rar_set(tp, dev->dev_addr);
3675
3676 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01003677
3678 return 0;
3679}
3680
Heiner Kallweite3972862018-06-29 08:07:04 +02003681static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003682{
Heiner Kallweit703732f2019-01-19 22:07:05 +01003683 struct rtl8169_private *tp = netdev_priv(dev);
3684
Heiner Kallweit69b3c592018-07-17 22:51:53 +02003685 if (!netif_running(dev))
3686 return -ENODEV;
Heiner Kallweite3972862018-06-29 08:07:04 +02003687
Heiner Kallweit703732f2019-01-19 22:07:05 +01003688 return phy_mii_ioctl(tp->phydev, ifr, cmd);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003689}
3690
David S. Miller1805b2f2011-10-24 18:18:09 -04003691static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3692{
David S. Miller1805b2f2011-10-24 18:18:09 -04003693 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00003694 case RTL_GIGA_MAC_VER_25:
3695 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003696 case RTL_GIGA_MAC_VER_29:
3697 case RTL_GIGA_MAC_VER_30:
3698 case RTL_GIGA_MAC_VER_32:
3699 case RTL_GIGA_MAC_VER_33:
3700 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02003701 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003702 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04003703 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3704 break;
3705 default:
3706 break;
3707 }
3708}
3709
Heiner Kallweit25e94112019-05-29 20:52:03 +02003710static void rtl_pll_power_down(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00003711{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01003712 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003713 return;
3714
hayeswang01dc7fe2011-03-21 01:50:28 +00003715 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3716 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02003717 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00003718
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01003719 if (device_may_wakeup(tp_to_dev(tp))) {
3720 phy_speed_down(tp->phydev, false);
3721 rtl_wol_suspend_quirk(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003722 return;
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01003723 }
françois romieu065c27c2011-01-03 15:08:12 +00003724
françois romieu065c27c2011-01-03 15:08:12 +00003725 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02003726 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003727 case RTL_GIGA_MAC_VER_37:
3728 case RTL_GIGA_MAC_VER_39:
3729 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08003730 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003731 case RTL_GIGA_MAC_VER_45:
3732 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003733 case RTL_GIGA_MAC_VER_47:
3734 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003735 case RTL_GIGA_MAC_VER_50:
3736 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003737 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00003738 break;
hayeswangbeb330a2013-04-01 22:23:39 +00003739 case RTL_GIGA_MAC_VER_40:
3740 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003741 case RTL_GIGA_MAC_VER_49:
Heiner Kallweite719b3e2019-04-28 11:11:47 +02003742 rtl_eri_clear_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003743 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00003744 break;
Heiner Kallweit76719ee2019-05-25 20:45:04 +02003745 default:
3746 break;
françois romieu065c27c2011-01-03 15:08:12 +00003747 }
3748}
3749
Heiner Kallweit25e94112019-05-29 20:52:03 +02003750static void rtl_pll_power_up(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00003751{
françois romieu065c27c2011-01-03 15:08:12 +00003752 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02003753 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003754 case RTL_GIGA_MAC_VER_37:
3755 case RTL_GIGA_MAC_VER_39:
3756 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003757 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00003758 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08003759 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003760 case RTL_GIGA_MAC_VER_45:
3761 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003762 case RTL_GIGA_MAC_VER_47:
3763 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003764 case RTL_GIGA_MAC_VER_50:
3765 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003766 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003767 break;
hayeswangbeb330a2013-04-01 22:23:39 +00003768 case RTL_GIGA_MAC_VER_40:
3769 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003770 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003771 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02003772 rtl_eri_set_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
hayeswangbeb330a2013-04-01 22:23:39 +00003773 break;
Heiner Kallweit76719ee2019-05-25 20:45:04 +02003774 default:
3775 break;
françois romieu065c27c2011-01-03 15:08:12 +00003776 }
3777
Heiner Kallweit703732f2019-01-19 22:07:05 +01003778 phy_resume(tp->phydev);
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02003779 /* give MAC/PHY some time to resume */
3780 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00003781}
3782
Hayes Wange542a222011-07-06 15:58:04 +08003783static void rtl_init_rxcfg(struct rtl8169_private *tp)
3784{
Hayes Wange542a222011-07-06 15:58:04 +08003785 switch (tp->mac_version) {
Heiner Kallweite9588eb2019-05-25 21:14:39 +02003786 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweit2a718832018-05-02 21:39:49 +02003787 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003788 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003789 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02003790 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
Maciej S. Szmigiero511cfd52018-10-11 16:02:10 +02003791 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
3792 case RTL_GIGA_MAC_VER_38:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003793 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003794 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02003795 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003796 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00003797 break;
Hayes Wange542a222011-07-06 15:58:04 +08003798 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003799 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003800 break;
3801 }
3802}
3803
Hayes Wang92fc43b2011-07-06 15:58:03 +08003804static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3805{
Timo Teräs9fba0812013-01-15 21:01:24 +00003806 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003807}
3808
Francois Romieud58d46b2011-05-03 16:38:29 +02003809static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3810{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003811 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3812 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01003813 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003814}
3815
3816static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3817{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003818 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3819 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003820 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003821}
3822
3823static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3824{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003825 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02003826}
3827
3828static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3829{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003830 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02003831}
3832
3833static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3834{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003835 RTL_W8(tp, MaxTxPacketSize, 0x3f);
3836 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3837 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01003838 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003839}
3840
3841static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3842{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003843 RTL_W8(tp, MaxTxPacketSize, 0x0c);
3844 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3845 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003846 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003847}
3848
3849static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3850{
Heiner Kallweitcb732002018-03-20 07:45:35 +01003851 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01003852 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02003853}
3854
3855static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3856{
Heiner Kallweitcb732002018-03-20 07:45:35 +01003857 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003858 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02003859}
3860
3861static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3862{
Francois Romieud58d46b2011-05-03 16:38:29 +02003863 r8168b_0_hw_jumbo_enable(tp);
3864
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003865 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02003866}
3867
3868static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3869{
Francois Romieud58d46b2011-05-03 16:38:29 +02003870 r8168b_0_hw_jumbo_disable(tp);
3871
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003872 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02003873}
3874
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003875static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02003876{
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003877 rtl_unlock_config_regs(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003878 switch (tp->mac_version) {
3879 case RTL_GIGA_MAC_VER_11:
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003880 r8168b_0_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003881 break;
3882 case RTL_GIGA_MAC_VER_12:
3883 case RTL_GIGA_MAC_VER_17:
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003884 r8168b_1_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003885 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003886 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
3887 r8168c_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003888 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003889 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
3890 r8168dp_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003891 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003892 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
3893 r8168e_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003894 break;
Francois Romieud58d46b2011-05-03 16:38:29 +02003895 default:
Francois Romieud58d46b2011-05-03 16:38:29 +02003896 break;
3897 }
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003898 rtl_lock_config_regs(tp);
3899}
3900
3901static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3902{
3903 rtl_unlock_config_regs(tp);
3904 switch (tp->mac_version) {
3905 case RTL_GIGA_MAC_VER_11:
3906 r8168b_0_hw_jumbo_disable(tp);
3907 break;
3908 case RTL_GIGA_MAC_VER_12:
3909 case RTL_GIGA_MAC_VER_17:
3910 r8168b_1_hw_jumbo_disable(tp);
3911 break;
3912 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
3913 r8168c_hw_jumbo_disable(tp);
3914 break;
3915 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
3916 r8168dp_hw_jumbo_disable(tp);
3917 break;
3918 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
3919 r8168e_hw_jumbo_disable(tp);
3920 break;
3921 default:
3922 break;
3923 }
3924 rtl_lock_config_regs(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003925}
3926
Francois Romieuffc46952012-07-06 14:19:23 +02003927DECLARE_RTL_COND(rtl_chipcmd_cond)
3928{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003929 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02003930}
3931
Francois Romieu6f43adc2011-04-29 15:05:51 +02003932static void rtl_hw_reset(struct rtl8169_private *tp)
3933{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003934 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003935
Francois Romieuffc46952012-07-06 14:19:23 +02003936 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003937}
3938
Heiner Kallweit254764e2019-01-22 22:23:41 +01003939static void rtl_request_firmware(struct rtl8169_private *tp)
Francois Romieub6ffd972011-06-17 17:00:05 +02003940{
3941 struct rtl_fw *rtl_fw;
Francois Romieub6ffd972011-06-17 17:00:05 +02003942
Heiner Kallweit254764e2019-01-22 22:23:41 +01003943 /* firmware loaded already or no firmware available */
3944 if (tp->rtl_fw || !tp->fw_name)
3945 return;
Francois Romieub6ffd972011-06-17 17:00:05 +02003946
3947 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
Heiner Kallweit47ad5932019-06-03 21:26:31 +02003948 if (!rtl_fw) {
3949 netif_warn(tp, ifup, tp->dev, "Unable to load firmware, out of memory\n");
3950 return;
3951 }
Francois Romieub6ffd972011-06-17 17:00:05 +02003952
Heiner Kallweitce8843a2019-05-29 21:15:06 +02003953 rtl_fw->phy_write = rtl_writephy;
3954 rtl_fw->phy_read = rtl_readphy;
3955 rtl_fw->mac_mcu_write = mac_mcu_write;
3956 rtl_fw->mac_mcu_read = mac_mcu_read;
Heiner Kallweit4edb00f2019-06-03 21:25:43 +02003957 rtl_fw->fw_name = tp->fw_name;
3958 rtl_fw->dev = tp_to_dev(tp);
Heiner Kallweitce8843a2019-05-29 21:15:06 +02003959
Heiner Kallweit47ad5932019-06-03 21:26:31 +02003960 if (rtl_fw_request_firmware(rtl_fw))
3961 kfree(rtl_fw);
3962 else
3963 tp->rtl_fw = rtl_fw;
François Romieu953a12c2011-04-24 17:38:48 +02003964}
3965
Hayes Wang92fc43b2011-07-06 15:58:03 +08003966static void rtl_rx_close(struct rtl8169_private *tp)
3967{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003968 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003969}
3970
Francois Romieuffc46952012-07-06 14:19:23 +02003971DECLARE_RTL_COND(rtl_npq_cond)
3972{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003973 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02003974}
3975
3976DECLARE_RTL_COND(rtl_txcfg_empty_cond)
3977{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003978 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02003979}
3980
françois romieue6de30d2011-01-03 15:08:37 +00003981static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
3983 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00003984 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Hayes Wang92fc43b2011-07-06 15:58:03 +08003986 rtl_rx_close(tp);
3987
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02003988 switch (tp->mac_version) {
3989 case RTL_GIGA_MAC_VER_27:
3990 case RTL_GIGA_MAC_VER_28:
3991 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02003992 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02003993 break;
3994 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
3995 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003996 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02003997 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02003998 break;
3999 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004000 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004001 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004002 break;
françois romieue6de30d2011-01-03 15:08:37 +00004003 }
4004
Hayes Wang92fc43b2011-07-06 15:58:03 +08004005 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004008static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004009{
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004010 u32 val = TX_DMA_BURST << TxDMAShift |
4011 InterFrameGap << TxInterFrameGapShift;
4012
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02004013 if (rtl_is_8168evl_up(tp))
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004014 val |= TXCFG_AUTO_FIFO;
4015
4016 RTL_W32(tp, TxConfig, val);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004017}
4018
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004019static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004021 /* Low hurts. Let's disable the filtering. */
4022 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004023}
4024
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004025static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004026{
4027 /*
4028 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4029 * register to be written before TxDescAddrLow to work.
4030 * Switching from MMIO to I/O access fixes the issue as well.
4031 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004032 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4033 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4034 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4035 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004036}
4037
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004038static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004039{
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004040 u32 val;
Francois Romieu6dccd162007-02-13 23:38:05 +01004041
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004042 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4043 val = 0x000fff00;
4044 else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
4045 val = 0x00ffff00;
4046 else
4047 return;
4048
4049 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
4050 val |= 0xff;
4051
4052 RTL_W32(tp, 0x7c, val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004053}
4054
Francois Romieue6b763e2012-03-08 09:35:39 +01004055static void rtl_set_rx_mode(struct net_device *dev)
4056{
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004057 u32 rx_mode = AcceptBroadcast | AcceptMyPhys | AcceptMulticast;
4058 /* Multicast hash filter */
4059 u32 mc_filter[2] = { 0xffffffff, 0xffffffff };
Francois Romieue6b763e2012-03-08 09:35:39 +01004060 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004061 u32 tmp;
Francois Romieue6b763e2012-03-08 09:35:39 +01004062
4063 if (dev->flags & IFF_PROMISC) {
4064 /* Unconditionally log net taps. */
4065 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004066 rx_mode |= AcceptAllPhys;
4067 } else if (netdev_mc_count(dev) > MC_FILTER_LIMIT ||
4068 dev->flags & IFF_ALLMULTI ||
4069 tp->mac_version == RTL_GIGA_MAC_VER_35) {
4070 /* accept all multicasts */
4071 } else if (netdev_mc_empty(dev)) {
4072 rx_mode &= ~AcceptMulticast;
Francois Romieue6b763e2012-03-08 09:35:39 +01004073 } else {
4074 struct netdev_hw_addr *ha;
4075
Francois Romieue6b763e2012-03-08 09:35:39 +01004076 mc_filter[1] = mc_filter[0] = 0;
4077 netdev_for_each_mc_addr(ha, dev) {
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004078 u32 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4079 mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31);
4080 }
4081
4082 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4083 tmp = mc_filter[0];
4084 mc_filter[0] = swab32(mc_filter[1]);
4085 mc_filter[1] = swab32(tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004086 }
4087 }
4088
4089 if (dev->features & NETIF_F_RXALL)
4090 rx_mode |= (AcceptErr | AcceptRunt);
4091
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004092 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4093 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004094
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004095 tmp = RTL_R32(tp, RxConfig);
4096 RTL_W32(tp, RxConfig, (tmp & ~RX_CONFIG_ACCEPT_MASK) | rx_mode);
Francois Romieue6b763e2012-03-08 09:35:39 +01004097}
4098
Francois Romieuffc46952012-07-06 14:19:23 +02004099DECLARE_RTL_COND(rtl_csiar_cond)
4100{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004101 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02004102}
4103
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004104static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004105{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004106 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4107
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004108 RTL_W32(tp, CSIDR, value);
4109 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004110 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004111
Francois Romieuffc46952012-07-06 14:19:23 +02004112 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004113}
4114
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004115static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004116{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004117 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4118
4119 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4120 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004121
Francois Romieuffc46952012-07-06 14:19:23 +02004122 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004123 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004124}
4125
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004126static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004127{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004128 struct pci_dev *pdev = tp->pci_dev;
4129 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004130
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004131 /* According to Realtek the value at config space address 0x070f
4132 * controls the L0s/L1 entrance latency. We try standard ECAM access
4133 * first and if it fails fall back to CSI.
4134 */
4135 if (pdev->cfg_size > 0x070f &&
4136 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4137 return;
4138
4139 netdev_notice_once(tp->dev,
4140 "No native access to PCI extended config space, falling back to CSI\n");
4141 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4142 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004143}
4144
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004145static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08004146{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004147 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02004148}
4149
4150struct ephy_info {
4151 unsigned int offset;
4152 u16 mask;
4153 u16 bits;
4154};
4155
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004156static void __rtl_ephy_init(struct rtl8169_private *tp,
4157 const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004158{
4159 u16 w;
4160
4161 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004162 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4163 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004164 e++;
4165 }
4166}
4167
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004168#define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
4169
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004170static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02004171{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004172 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004173 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004174}
4175
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004176static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004177{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004178 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004179 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004180}
4181
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004182static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
hayeswangb51ecea2014-07-09 14:52:51 +08004183{
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004184 /* work around an issue when PCI reset occurs during L2/L3 state */
4185 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
hayeswangb51ecea2014-07-09 14:52:51 +08004186}
4187
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004188static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4189{
Heiner Kallweit62b1b3b2019-06-18 23:14:50 +02004190 /* Don't enable ASPM in the chip if OS can't control ASPM */
4191 if (enable && tp->aspm_manageable) {
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004192 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
Kai-Heng Feng94235462018-09-12 14:58:20 +08004193 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004194 } else {
4195 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4196 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4197 }
Kai-Heng Feng94235462018-09-12 14:58:20 +08004198
4199 udelay(10);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004200}
4201
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004202static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
4203 u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
4204{
4205 /* Usage of dynamic vs. static FIFO is controlled by bit
4206 * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
4207 */
4208 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
4209 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
4210}
4211
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004212static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
4213 u8 low, u8 high)
4214{
4215 /* FIFO thresholds for pause flow control */
4216 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
4217 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
4218}
4219
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004220static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004221{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004222 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004223
françois romieufaf1e782013-02-27 13:01:57 +00004224 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004225 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00004226 PCI_EXP_DEVCTL_NOSNOOP_EN);
4227 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004228}
4229
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004230static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004231{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004232 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004233
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004234 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004235}
4236
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004237static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004238{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004239 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02004240
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004241 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004242
françois romieufaf1e782013-02-27 13:01:57 +00004243 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004244 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02004245
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004246 rtl_disable_clock_request(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004247}
4248
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004249static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004250{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004251 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004252 { 0x01, 0, 0x0001 },
4253 { 0x02, 0x0800, 0x1000 },
4254 { 0x03, 0, 0x0042 },
4255 { 0x06, 0x0080, 0x0000 },
4256 { 0x07, 0, 0x2000 }
4257 };
4258
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004259 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004260
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004261 rtl_ephy_init(tp, e_info_8168cp);
Francois Romieub726e492008-06-28 12:22:59 +02004262
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004263 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004264}
4265
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004266static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004267{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004268 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004269
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004270 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02004271
françois romieufaf1e782013-02-27 13:01:57 +00004272 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004273 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02004274}
4275
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004276static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004277{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004278 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004279
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004280 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004281
4282 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004283 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004284
françois romieufaf1e782013-02-27 13:01:57 +00004285 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004286 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004287}
4288
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004289static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004290{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004291 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004292 { 0x02, 0x0800, 0x1000 },
4293 { 0x03, 0, 0x0002 },
4294 { 0x06, 0x0080, 0x0000 }
4295 };
4296
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004297 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004298
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004299 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02004300
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004301 rtl_ephy_init(tp, e_info_8168c_1);
Francois Romieub726e492008-06-28 12:22:59 +02004302
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004303 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004304}
4305
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004306static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004307{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004308 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004309 { 0x01, 0, 0x0001 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004310 { 0x03, 0x0400, 0x0020 }
Francois Romieub726e492008-06-28 12:22:59 +02004311 };
4312
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004313 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004314
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004315 rtl_ephy_init(tp, e_info_8168c_2);
Francois Romieub726e492008-06-28 12:22:59 +02004316
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004317 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004318}
4319
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004320static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004321{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004322 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004323}
4324
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004325static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004326{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004327 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004328
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004329 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004330}
4331
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004332static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004333{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004334 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004335
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004336 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004337
françois romieufaf1e782013-02-27 13:01:57 +00004338 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004339 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02004340}
4341
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004342static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004343{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004344 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004345
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);
hayeswang4804b3b2011-03-21 01:50:29 +00004348
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004349 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004350}
4351
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004352static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004353{
4354 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004355 { 0x0b, 0x0000, 0x0048 },
4356 { 0x19, 0x0020, 0x0050 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004357 { 0x0c, 0x0100, 0x0020 },
4358 { 0x10, 0x0004, 0x0000 },
françois romieue6de30d2011-01-03 15:08:37 +00004359 };
françois romieue6de30d2011-01-03 15:08:37 +00004360
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004361 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004362
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004363 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00004364
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004365 rtl_ephy_init(tp, e_info_8168d_4);
françois romieue6de30d2011-01-03 15:08:37 +00004366
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004367 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004368}
4369
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004370static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004371{
Hayes Wang70090422011-07-06 15:58:06 +08004372 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004373 { 0x00, 0x0200, 0x0100 },
4374 { 0x00, 0x0000, 0x0004 },
4375 { 0x06, 0x0002, 0x0001 },
4376 { 0x06, 0x0000, 0x0030 },
4377 { 0x07, 0x0000, 0x2000 },
4378 { 0x00, 0x0000, 0x0020 },
4379 { 0x03, 0x5800, 0x2000 },
4380 { 0x03, 0x0000, 0x0001 },
4381 { 0x01, 0x0800, 0x1000 },
4382 { 0x07, 0x0000, 0x4000 },
4383 { 0x1e, 0x0000, 0x2000 },
4384 { 0x19, 0xffff, 0xfe6c },
4385 { 0x0a, 0x0000, 0x0040 }
4386 };
4387
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004388 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004389
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004390 rtl_ephy_init(tp, e_info_8168e_1);
hayeswang01dc7fe2011-03-21 01:50:28 +00004391
françois romieufaf1e782013-02-27 13:01:57 +00004392 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004393 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00004394
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004395 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004396
4397 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004398 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4399 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004400
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004401 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004402}
4403
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004404static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004405{
4406 static const struct ephy_info e_info_8168e_2[] = {
4407 { 0x09, 0x0000, 0x0080 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004408 { 0x19, 0x0000, 0x0224 },
4409 { 0x00, 0x0000, 0x0004 },
4410 { 0x0c, 0x3df0, 0x0200 },
Hayes Wang70090422011-07-06 15:58:06 +08004411 };
4412
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004413 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004414
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004415 rtl_ephy_init(tp, e_info_8168e_2);
Hayes Wang70090422011-07-06 15:58:06 +08004416
françois romieufaf1e782013-02-27 13:01:57 +00004417 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004418 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08004419
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004420 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4421 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004422 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004423 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4424 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004425 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004426 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang70090422011-07-06 15:58:06 +08004427
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004428 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004429
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004430 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08004431
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01004432 rtl8168_config_eee_mac(tp);
4433
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004434 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4435 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4436 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02004437
4438 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08004439}
4440
Hayes Wang5f886e02012-03-30 14:33:03 +08004441static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004442{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004443 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004444
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004445 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08004446
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004447 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4448 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004449 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004450 rtl_reset_packet_filter(tp);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004451 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
4452 rtl_eri_set_bits(tp, 0x1d0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004453 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4454 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
Hayes Wangc2218922011-09-06 16:55:18 +08004455
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004456 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004457
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004458 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
4459 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4460 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4461 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01004462
4463 rtl8168_config_eee_mac(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004464}
4465
Hayes Wang5f886e02012-03-30 14:33:03 +08004466static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4467{
Hayes Wang5f886e02012-03-30 14:33:03 +08004468 static const struct ephy_info e_info_8168f_1[] = {
4469 { 0x06, 0x00c0, 0x0020 },
4470 { 0x08, 0x0001, 0x0002 },
4471 { 0x09, 0x0000, 0x0080 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004472 { 0x19, 0x0000, 0x0224 },
4473 { 0x00, 0x0000, 0x0004 },
4474 { 0x0c, 0x3df0, 0x0200 },
Hayes Wang5f886e02012-03-30 14:33:03 +08004475 };
4476
4477 rtl_hw_start_8168f(tp);
4478
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004479 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wang5f886e02012-03-30 14:33:03 +08004480
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004481 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang5f886e02012-03-30 14:33:03 +08004482}
4483
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004484static void rtl_hw_start_8411(struct rtl8169_private *tp)
4485{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004486 static const struct ephy_info e_info_8168f_1[] = {
4487 { 0x06, 0x00c0, 0x0020 },
4488 { 0x0f, 0xffff, 0x5200 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004489 { 0x19, 0x0000, 0x0224 },
4490 { 0x00, 0x0000, 0x0004 },
4491 { 0x0c, 0x3df0, 0x0200 },
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004492 };
4493
4494 rtl_hw_start_8168f(tp);
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004495 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004496
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004497 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004498
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004499 rtl_eri_set_bits(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004500}
4501
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004502static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08004503{
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004504 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004505 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Hayes Wangc5583862012-07-02 17:23:22 +08004506
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004507 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08004508
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004509 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08004510
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004511 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004512 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
Hayes Wangc5583862012-07-02 17:23:22 +08004513
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004514 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08004515
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004516 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4517 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004518
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004519 rtl8168_config_eee_mac(tp);
4520
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004521 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004522 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
hayeswangb51ecea2014-07-09 14:52:51 +08004523
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004524 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08004525}
4526
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004527static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
4528{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004529 static const struct ephy_info e_info_8168g_1[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004530 { 0x00, 0x0008, 0x0000 },
4531 { 0x0c, 0x3ff0, 0x0820 },
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004532 { 0x1e, 0x0000, 0x0001 },
4533 { 0x19, 0x8000, 0x0000 }
4534 };
4535
4536 rtl_hw_start_8168g(tp);
4537
4538 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004539 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004540 rtl_ephy_init(tp, e_info_8168g_1);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004541 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004542}
4543
hayeswang57538c42013-04-01 22:23:40 +00004544static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
4545{
hayeswang57538c42013-04-01 22:23:40 +00004546 static const struct ephy_info e_info_8168g_2[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004547 { 0x00, 0x0008, 0x0000 },
4548 { 0x0c, 0x3ff0, 0x0820 },
4549 { 0x19, 0xffff, 0x7c00 },
4550 { 0x1e, 0xffff, 0x20eb },
4551 { 0x0d, 0xffff, 0x1666 },
4552 { 0x00, 0xffff, 0x10a3 },
4553 { 0x06, 0xffff, 0xf050 },
4554 { 0x04, 0x0000, 0x0010 },
4555 { 0x1d, 0x4000, 0x0000 },
hayeswang57538c42013-04-01 22:23:40 +00004556 };
4557
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004558 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00004559
4560 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004561 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4562 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004563 rtl_ephy_init(tp, e_info_8168g_2);
hayeswang57538c42013-04-01 22:23:40 +00004564}
4565
hayeswang45dd95c2013-07-08 17:09:01 +08004566static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
4567{
hayeswang45dd95c2013-07-08 17:09:01 +08004568 static const struct ephy_info e_info_8411_2[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004569 { 0x00, 0x0008, 0x0000 },
4570 { 0x0c, 0x37d0, 0x0820 },
4571 { 0x1e, 0x0000, 0x0001 },
4572 { 0x19, 0x8021, 0x0000 },
4573 { 0x1e, 0x0000, 0x2000 },
4574 { 0x0d, 0x0100, 0x0200 },
4575 { 0x00, 0x0000, 0x0080 },
4576 { 0x06, 0x0000, 0x0010 },
4577 { 0x04, 0x0000, 0x0010 },
4578 { 0x1d, 0x0000, 0x4000 },
hayeswang45dd95c2013-07-08 17:09:01 +08004579 };
4580
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004581 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08004582
4583 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004584 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004585 rtl_ephy_init(tp, e_info_8411_2);
Heiner Kallweitfe4e8db02019-07-13 13:45:47 +02004586
4587 /* The following Realtek-provided magic fixes an issue with the RX unit
4588 * getting confused after the PHY having been powered-down.
4589 */
4590 r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
4591 r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
4592 r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
4593 r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
4594 r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
4595 r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
4596 r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
4597 r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
4598 mdelay(3);
4599 r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
4600
4601 r8168_mac_ocp_write(tp, 0xF800, 0xE008);
4602 r8168_mac_ocp_write(tp, 0xF802, 0xE00A);
4603 r8168_mac_ocp_write(tp, 0xF804, 0xE00C);
4604 r8168_mac_ocp_write(tp, 0xF806, 0xE00E);
4605 r8168_mac_ocp_write(tp, 0xF808, 0xE027);
4606 r8168_mac_ocp_write(tp, 0xF80A, 0xE04F);
4607 r8168_mac_ocp_write(tp, 0xF80C, 0xE05E);
4608 r8168_mac_ocp_write(tp, 0xF80E, 0xE065);
4609 r8168_mac_ocp_write(tp, 0xF810, 0xC602);
4610 r8168_mac_ocp_write(tp, 0xF812, 0xBE00);
4611 r8168_mac_ocp_write(tp, 0xF814, 0x0000);
4612 r8168_mac_ocp_write(tp, 0xF816, 0xC502);
4613 r8168_mac_ocp_write(tp, 0xF818, 0xBD00);
4614 r8168_mac_ocp_write(tp, 0xF81A, 0x074C);
4615 r8168_mac_ocp_write(tp, 0xF81C, 0xC302);
4616 r8168_mac_ocp_write(tp, 0xF81E, 0xBB00);
4617 r8168_mac_ocp_write(tp, 0xF820, 0x080A);
4618 r8168_mac_ocp_write(tp, 0xF822, 0x6420);
4619 r8168_mac_ocp_write(tp, 0xF824, 0x48C2);
4620 r8168_mac_ocp_write(tp, 0xF826, 0x8C20);
4621 r8168_mac_ocp_write(tp, 0xF828, 0xC516);
4622 r8168_mac_ocp_write(tp, 0xF82A, 0x64A4);
4623 r8168_mac_ocp_write(tp, 0xF82C, 0x49C0);
4624 r8168_mac_ocp_write(tp, 0xF82E, 0xF009);
4625 r8168_mac_ocp_write(tp, 0xF830, 0x74A2);
4626 r8168_mac_ocp_write(tp, 0xF832, 0x8CA5);
4627 r8168_mac_ocp_write(tp, 0xF834, 0x74A0);
4628 r8168_mac_ocp_write(tp, 0xF836, 0xC50E);
4629 r8168_mac_ocp_write(tp, 0xF838, 0x9CA2);
4630 r8168_mac_ocp_write(tp, 0xF83A, 0x1C11);
4631 r8168_mac_ocp_write(tp, 0xF83C, 0x9CA0);
4632 r8168_mac_ocp_write(tp, 0xF83E, 0xE006);
4633 r8168_mac_ocp_write(tp, 0xF840, 0x74F8);
4634 r8168_mac_ocp_write(tp, 0xF842, 0x48C4);
4635 r8168_mac_ocp_write(tp, 0xF844, 0x8CF8);
4636 r8168_mac_ocp_write(tp, 0xF846, 0xC404);
4637 r8168_mac_ocp_write(tp, 0xF848, 0xBC00);
4638 r8168_mac_ocp_write(tp, 0xF84A, 0xC403);
4639 r8168_mac_ocp_write(tp, 0xF84C, 0xBC00);
4640 r8168_mac_ocp_write(tp, 0xF84E, 0x0BF2);
4641 r8168_mac_ocp_write(tp, 0xF850, 0x0C0A);
4642 r8168_mac_ocp_write(tp, 0xF852, 0xE434);
4643 r8168_mac_ocp_write(tp, 0xF854, 0xD3C0);
4644 r8168_mac_ocp_write(tp, 0xF856, 0x49D9);
4645 r8168_mac_ocp_write(tp, 0xF858, 0xF01F);
4646 r8168_mac_ocp_write(tp, 0xF85A, 0xC526);
4647 r8168_mac_ocp_write(tp, 0xF85C, 0x64A5);
4648 r8168_mac_ocp_write(tp, 0xF85E, 0x1400);
4649 r8168_mac_ocp_write(tp, 0xF860, 0xF007);
4650 r8168_mac_ocp_write(tp, 0xF862, 0x0C01);
4651 r8168_mac_ocp_write(tp, 0xF864, 0x8CA5);
4652 r8168_mac_ocp_write(tp, 0xF866, 0x1C15);
4653 r8168_mac_ocp_write(tp, 0xF868, 0xC51B);
4654 r8168_mac_ocp_write(tp, 0xF86A, 0x9CA0);
4655 r8168_mac_ocp_write(tp, 0xF86C, 0xE013);
4656 r8168_mac_ocp_write(tp, 0xF86E, 0xC519);
4657 r8168_mac_ocp_write(tp, 0xF870, 0x74A0);
4658 r8168_mac_ocp_write(tp, 0xF872, 0x48C4);
4659 r8168_mac_ocp_write(tp, 0xF874, 0x8CA0);
4660 r8168_mac_ocp_write(tp, 0xF876, 0xC516);
4661 r8168_mac_ocp_write(tp, 0xF878, 0x74A4);
4662 r8168_mac_ocp_write(tp, 0xF87A, 0x48C8);
4663 r8168_mac_ocp_write(tp, 0xF87C, 0x48CA);
4664 r8168_mac_ocp_write(tp, 0xF87E, 0x9CA4);
4665 r8168_mac_ocp_write(tp, 0xF880, 0xC512);
4666 r8168_mac_ocp_write(tp, 0xF882, 0x1B00);
4667 r8168_mac_ocp_write(tp, 0xF884, 0x9BA0);
4668 r8168_mac_ocp_write(tp, 0xF886, 0x1B1C);
4669 r8168_mac_ocp_write(tp, 0xF888, 0x483F);
4670 r8168_mac_ocp_write(tp, 0xF88A, 0x9BA2);
4671 r8168_mac_ocp_write(tp, 0xF88C, 0x1B04);
4672 r8168_mac_ocp_write(tp, 0xF88E, 0xC508);
4673 r8168_mac_ocp_write(tp, 0xF890, 0x9BA0);
4674 r8168_mac_ocp_write(tp, 0xF892, 0xC505);
4675 r8168_mac_ocp_write(tp, 0xF894, 0xBD00);
4676 r8168_mac_ocp_write(tp, 0xF896, 0xC502);
4677 r8168_mac_ocp_write(tp, 0xF898, 0xBD00);
4678 r8168_mac_ocp_write(tp, 0xF89A, 0x0300);
4679 r8168_mac_ocp_write(tp, 0xF89C, 0x051E);
4680 r8168_mac_ocp_write(tp, 0xF89E, 0xE434);
4681 r8168_mac_ocp_write(tp, 0xF8A0, 0xE018);
4682 r8168_mac_ocp_write(tp, 0xF8A2, 0xE092);
4683 r8168_mac_ocp_write(tp, 0xF8A4, 0xDE20);
4684 r8168_mac_ocp_write(tp, 0xF8A6, 0xD3C0);
4685 r8168_mac_ocp_write(tp, 0xF8A8, 0xC50F);
4686 r8168_mac_ocp_write(tp, 0xF8AA, 0x76A4);
4687 r8168_mac_ocp_write(tp, 0xF8AC, 0x49E3);
4688 r8168_mac_ocp_write(tp, 0xF8AE, 0xF007);
4689 r8168_mac_ocp_write(tp, 0xF8B0, 0x49C0);
4690 r8168_mac_ocp_write(tp, 0xF8B2, 0xF103);
4691 r8168_mac_ocp_write(tp, 0xF8B4, 0xC607);
4692 r8168_mac_ocp_write(tp, 0xF8B6, 0xBE00);
4693 r8168_mac_ocp_write(tp, 0xF8B8, 0xC606);
4694 r8168_mac_ocp_write(tp, 0xF8BA, 0xBE00);
4695 r8168_mac_ocp_write(tp, 0xF8BC, 0xC602);
4696 r8168_mac_ocp_write(tp, 0xF8BE, 0xBE00);
4697 r8168_mac_ocp_write(tp, 0xF8C0, 0x0C4C);
4698 r8168_mac_ocp_write(tp, 0xF8C2, 0x0C28);
4699 r8168_mac_ocp_write(tp, 0xF8C4, 0x0C2C);
4700 r8168_mac_ocp_write(tp, 0xF8C6, 0xDC00);
4701 r8168_mac_ocp_write(tp, 0xF8C8, 0xC707);
4702 r8168_mac_ocp_write(tp, 0xF8CA, 0x1D00);
4703 r8168_mac_ocp_write(tp, 0xF8CC, 0x8DE2);
4704 r8168_mac_ocp_write(tp, 0xF8CE, 0x48C1);
4705 r8168_mac_ocp_write(tp, 0xF8D0, 0xC502);
4706 r8168_mac_ocp_write(tp, 0xF8D2, 0xBD00);
4707 r8168_mac_ocp_write(tp, 0xF8D4, 0x00AA);
4708 r8168_mac_ocp_write(tp, 0xF8D6, 0xE0C0);
4709 r8168_mac_ocp_write(tp, 0xF8D8, 0xC502);
4710 r8168_mac_ocp_write(tp, 0xF8DA, 0xBD00);
4711 r8168_mac_ocp_write(tp, 0xF8DC, 0x0132);
4712
4713 r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
4714
4715 r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
4716 r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
4717 r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
4718 r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
4719 r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
4720 r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
4721 r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
4722
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004723 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08004724}
4725
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004726static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
4727{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004728 static const struct ephy_info e_info_8168h_1[] = {
4729 { 0x1e, 0x0800, 0x0001 },
4730 { 0x1d, 0x0000, 0x0800 },
4731 { 0x05, 0xffff, 0x2089 },
4732 { 0x06, 0xffff, 0x5881 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004733 { 0x04, 0xffff, 0x854a },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004734 { 0x01, 0xffff, 0x068b }
4735 };
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004736 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004737
4738 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004739 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004740 rtl_ephy_init(tp, e_info_8168h_1);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004741
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004742 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004743 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004744
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004745 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004746
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004747 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004748
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004749 rtl_reset_packet_filter(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004750
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004751 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_1111, BIT(4));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004752
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004753 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004754
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004755 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004756
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004757 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004758
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004759 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4760 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004761
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004762 rtl8168_config_eee_mac(tp);
4763
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004764 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4765 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004766
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004767 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004768
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004769 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004770
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004771 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004772
4773 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08004774 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004775 rtl_writephy(tp, 0x1f, 0x0000);
4776 if (rg_saw_cnt > 0) {
4777 u16 sw_cnt_1ms_ini;
4778
4779 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
4780 sw_cnt_1ms_ini &= 0x0fff;
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004781 r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004782 }
4783
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004784 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
4785 r8168_mac_ocp_modify(tp, 0xe052, 0x6000, 0x8008);
4786 r8168_mac_ocp_modify(tp, 0xe0d6, 0x01ff, 0x017f);
4787 r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004788
4789 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
4790 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
4791 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
4792 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004793
4794 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004795}
4796
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004797static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
4798{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08004799 rtl8168ep_stop_cmac(tp);
4800
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004801 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004802 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004803
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004804 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004805
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004806 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004807
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004808 rtl_reset_packet_filter(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004809
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004810 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f80);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004811
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004812 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004813
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004814 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004815
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004816 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4817 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004818
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004819 rtl8168_config_eee_mac(tp);
4820
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004821 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004822
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004823 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004824
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004825 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004826}
4827
4828static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
4829{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004830 static const struct ephy_info e_info_8168ep_1[] = {
4831 { 0x00, 0xffff, 0x10ab },
4832 { 0x06, 0xffff, 0xf030 },
4833 { 0x08, 0xffff, 0x2006 },
4834 { 0x0d, 0xffff, 0x1666 },
4835 { 0x0c, 0x3ff0, 0x0000 }
4836 };
4837
4838 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004839 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004840 rtl_ephy_init(tp, e_info_8168ep_1);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004841
4842 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004843
4844 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004845}
4846
4847static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
4848{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004849 static const struct ephy_info e_info_8168ep_2[] = {
4850 { 0x00, 0xffff, 0x10a3 },
4851 { 0x19, 0xffff, 0xfc00 },
4852 { 0x1e, 0xffff, 0x20ea }
4853 };
4854
4855 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004856 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004857 rtl_ephy_init(tp, e_info_8168ep_2);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004858
4859 rtl_hw_start_8168ep(tp);
4860
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004861 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4862 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004863
4864 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004865}
4866
4867static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
4868{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004869 static const struct ephy_info e_info_8168ep_3[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004870 { 0x00, 0x0000, 0x0080 },
4871 { 0x0d, 0x0100, 0x0200 },
4872 { 0x19, 0x8021, 0x0000 },
4873 { 0x1e, 0x0000, 0x2000 },
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004874 };
4875
4876 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004877 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004878 rtl_ephy_init(tp, e_info_8168ep_3);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004879
4880 rtl_hw_start_8168ep(tp);
4881
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004882 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4883 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004884
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004885 r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x0271);
4886 r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
4887 r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004888
4889 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004890}
4891
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004892static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004893{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004894 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004895 { 0x01, 0, 0x6e65 },
4896 { 0x02, 0, 0x091f },
4897 { 0x03, 0, 0xc2f9 },
4898 { 0x06, 0, 0xafb5 },
4899 { 0x07, 0, 0x0e00 },
4900 { 0x19, 0, 0xec80 },
4901 { 0x01, 0, 0x2e65 },
4902 { 0x01, 0, 0x6e65 }
4903 };
4904 u8 cfg1;
4905
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004906 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004907
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004908 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004909
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004910 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004911
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004912 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02004913 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004914 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004915
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004916 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004917 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004918 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004919
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004920 rtl_ephy_init(tp, e_info_8102e_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004921}
4922
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004923static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004924{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004925 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004926
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004927 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004928
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004929 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
4930 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004931}
4932
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004933static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004934{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004935 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004936
Francois Romieufdf6fc02012-07-06 22:40:38 +02004937 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004938}
4939
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004940static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08004941{
4942 static const struct ephy_info e_info_8105e_1[] = {
4943 { 0x07, 0, 0x4000 },
4944 { 0x19, 0, 0x0200 },
4945 { 0x19, 0, 0x0020 },
4946 { 0x1e, 0, 0x2000 },
4947 { 0x03, 0, 0x0001 },
4948 { 0x19, 0, 0x0100 },
4949 { 0x19, 0, 0x0004 },
4950 { 0x0a, 0, 0x0020 }
4951 };
4952
Francois Romieucecb5fd2011-04-01 10:21:07 +02004953 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004954 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004955
Francois Romieucecb5fd2011-04-01 10:21:07 +02004956 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004957 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004958
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004959 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
4960 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004961
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004962 rtl_ephy_init(tp, e_info_8105e_1);
hayeswangb51ecea2014-07-09 14:52:51 +08004963
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004964 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004965}
4966
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004967static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08004968{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004969 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02004970 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004971}
4972
Hayes Wang7e18dca2012-03-30 14:33:02 +08004973static void rtl_hw_start_8402(struct rtl8169_private *tp)
4974{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004975 static const struct ephy_info e_info_8402[] = {
4976 { 0x19, 0xffff, 0xff64 },
4977 { 0x1e, 0, 0x4000 }
4978 };
4979
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004980 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004981
4982 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004983 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004984
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004985 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004986
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004987 rtl_ephy_init(tp, e_info_8402);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004988
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004989 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004990
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004991 rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004992 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004993 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4994 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4995 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00);
hayeswangb51ecea2014-07-09 14:52:51 +08004996
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004997 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004998}
4999
Hayes Wang5598bfe2012-07-02 17:23:21 +08005000static void rtl_hw_start_8106(struct rtl8169_private *tp)
5001{
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005002 rtl_hw_aspm_clkreq_enable(tp, false);
5003
Hayes Wang5598bfe2012-07-02 17:23:21 +08005004 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005005 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005006
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005007 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5008 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5009 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005010
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005011 rtl_pcie_state_l2l3_disable(tp);
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005012 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005013}
5014
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005015static void rtl_hw_config(struct rtl8169_private *tp)
5016{
5017 static const rtl_generic_fct hw_configs[] = {
5018 [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
5019 [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
5020 [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
5021 [RTL_GIGA_MAC_VER_10] = NULL,
5022 [RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168bb,
5023 [RTL_GIGA_MAC_VER_12] = rtl_hw_start_8168bef,
5024 [RTL_GIGA_MAC_VER_13] = NULL,
5025 [RTL_GIGA_MAC_VER_14] = NULL,
5026 [RTL_GIGA_MAC_VER_15] = NULL,
5027 [RTL_GIGA_MAC_VER_16] = NULL,
5028 [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168bef,
5029 [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
5030 [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
5031 [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
5032 [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_3,
5033 [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
5034 [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
5035 [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
5036 [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
5037 [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
5038 [RTL_GIGA_MAC_VER_27] = rtl_hw_start_8168d,
5039 [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
5040 [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
5041 [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
5042 [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168dp,
5043 [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
5044 [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
5045 [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
5046 [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
5047 [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
5048 [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
5049 [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
5050 [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
5051 [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
5052 [RTL_GIGA_MAC_VER_41] = rtl_hw_start_8168g_1,
5053 [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
5054 [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
5055 [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
5056 [RTL_GIGA_MAC_VER_45] = rtl_hw_start_8168h_1,
5057 [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
5058 [RTL_GIGA_MAC_VER_47] = rtl_hw_start_8168h_1,
5059 [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
5060 [RTL_GIGA_MAC_VER_49] = rtl_hw_start_8168ep_1,
5061 [RTL_GIGA_MAC_VER_50] = rtl_hw_start_8168ep_2,
5062 [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
5063 };
5064
5065 if (hw_configs[tp->mac_version])
5066 hw_configs[tp->mac_version](tp);
5067}
5068
5069static void rtl_hw_start_8168(struct rtl8169_private *tp)
5070{
Francois Romieucecb5fd2011-04-01 10:21:07 +02005071 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005072 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005073 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005074 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005075
Heiner Kallweit272b2262019-06-14 07:55:21 +02005076 if (rtl_is_8168evl_up(tp))
5077 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5078 else
5079 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005080
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005081 rtl_hw_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082}
5083
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005084static void rtl_hw_start_8169(struct rtl8169_private *tp)
5085{
5086 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
5087 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
5088
5089 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
5090
5091 tp->cp_cmd |= PCIMulRW;
5092
5093 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5094 tp->mac_version == RTL_GIGA_MAC_VER_03) {
5095 netif_dbg(tp, drv, tp->dev,
5096 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
5097 tp->cp_cmd |= (1 << 14);
5098 }
5099
5100 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5101
5102 rtl8169_set_magic_reg(tp, tp->mac_version);
5103
5104 RTL_W32(tp, RxMissed, 0);
5105}
5106
5107static void rtl_hw_start(struct rtl8169_private *tp)
5108{
5109 rtl_unlock_config_regs(tp);
5110
5111 tp->cp_cmd &= CPCMD_MASK;
5112 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5113
5114 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
5115 rtl_hw_start_8169(tp);
5116 else
5117 rtl_hw_start_8168(tp);
5118
5119 rtl_set_rx_max_size(tp);
5120 rtl_set_rx_tx_desc_registers(tp);
5121 rtl_lock_config_regs(tp);
5122
5123 /* disable interrupt coalescing */
5124 RTL_W16(tp, IntrMitigate, 0x0000);
5125 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5126 RTL_R8(tp, IntrMask);
5127 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5128 rtl_init_rxcfg(tp);
5129 rtl_set_tx_config_registers(tp);
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005130 rtl_set_rx_mode(tp->dev);
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005131 rtl_irq_enable(tp);
5132}
5133
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5135{
Francois Romieud58d46b2011-05-03 16:38:29 +02005136 struct rtl8169_private *tp = netdev_priv(dev);
5137
Francois Romieud58d46b2011-05-03 16:38:29 +02005138 if (new_mtu > ETH_DATA_LEN)
5139 rtl_hw_jumbo_enable(tp);
5140 else
5141 rtl_hw_jumbo_disable(tp);
5142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005144 netdev_update_features(dev);
5145
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147}
5148
5149static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5150{
Al Viro95e09182007-12-22 18:55:39 +00005151 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5153}
5154
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005155static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156{
5157 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5158
Alexander Duycka0750132014-12-11 15:02:17 -08005159 /* Force memory writes to complete before releasing descriptor */
5160 dma_wmb();
5161
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005162 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163}
5164
Heiner Kallweit32879f02019-08-07 21:38:22 +02005165static struct page *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5166 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005167{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005168 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02005169 int node = dev_to_node(d);
Heiner Kallweit32879f02019-08-07 21:38:22 +02005170 dma_addr_t mapping;
5171 struct page *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172
Heiner Kallweit32879f02019-08-07 21:38:22 +02005173 data = alloc_pages_node(node, GFP_KERNEL, get_order(R8169_RX_BUF_SIZE));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005174 if (!data)
5175 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005176
Heiner Kallweit32879f02019-08-07 21:38:22 +02005177 mapping = dma_map_page(d, data, 0, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005178 if (unlikely(dma_mapping_error(d, mapping))) {
5179 if (net_ratelimit())
5180 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Heiner Kallweit32879f02019-08-07 21:38:22 +02005181 __free_pages(data, get_order(R8169_RX_BUF_SIZE));
5182 return NULL;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
Heiner Kallweitd731af72018-04-17 23:26:41 +02005185 desc->addr = cpu_to_le64(mapping);
5186 rtl8169_mark_to_asic(desc);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005187
Heiner Kallweit32879f02019-08-07 21:38:22 +02005188 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189}
5190
5191static void rtl8169_rx_clear(struct rtl8169_private *tp)
5192{
Francois Romieu07d3f512007-02-21 22:40:46 +01005193 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
Heiner Kallweiteb2e7f02019-08-09 22:59:07 +02005195 for (i = 0; i < NUM_RX_DESC && tp->Rx_databuff[i]; i++) {
5196 dma_unmap_page(tp_to_dev(tp),
5197 le64_to_cpu(tp->RxDescArray[i].addr),
5198 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5199 __free_pages(tp->Rx_databuff[i], get_order(R8169_RX_BUF_SIZE));
5200 tp->Rx_databuff[i] = NULL;
5201 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 }
5203}
5204
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005205static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005207 desc->opts1 |= cpu_to_le32(RingEnd);
5208}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005209
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005210static int rtl8169_rx_fill(struct rtl8169_private *tp)
5211{
5212 unsigned int i;
5213
5214 for (i = 0; i < NUM_RX_DESC; i++) {
Heiner Kallweit32879f02019-08-07 21:38:22 +02005215 struct page *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005216
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005217 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005218 if (!data) {
5219 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005220 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005221 }
5222 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005225 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5226 return 0;
5227
5228err_out:
5229 rtl8169_rx_clear(tp);
5230 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231}
5232
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005233static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 rtl8169_init_ring_indexes(tp);
5236
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005237 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5238 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005240 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241}
5242
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005243static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 struct TxDesc *desc)
5245{
5246 unsigned int len = tx_skb->len;
5247
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005248 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5249
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 desc->opts1 = 0x00;
5251 desc->opts2 = 0x00;
5252 desc->addr = 0x00;
5253 tx_skb->len = 0;
5254}
5255
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005256static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5257 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258{
5259 unsigned int i;
5260
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005261 for (i = 0; i < n; i++) {
5262 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 struct ring_info *tx_skb = tp->tx_skb + entry;
5264 unsigned int len = tx_skb->len;
5265
5266 if (len) {
5267 struct sk_buff *skb = tx_skb->skb;
5268
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005269 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 tp->TxDescArray + entry);
5271 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07005272 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 tx_skb->skb = NULL;
5274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 }
5276 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005277}
5278
5279static void rtl8169_tx_clear(struct rtl8169_private *tp)
5280{
5281 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 tp->cur_tx = tp->dirty_tx = 0;
Florian Westphald92060b2018-10-20 12:25:27 +02005283 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284}
5285
Francois Romieu4422bcd2012-01-26 11:23:32 +01005286static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287{
David Howellsc4028952006-11-22 14:57:56 +00005288 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005289 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Francois Romieuda78dbf2012-01-26 14:18:23 +01005291 napi_disable(&tp->napi);
5292 netif_stop_queue(dev);
Paul E. McKenney16f11502018-11-05 17:07:39 -08005293 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294
françois romieuc7c2c392011-12-04 20:30:52 +00005295 rtl8169_hw_reset(tp);
5296
Francois Romieu56de4142011-03-15 17:29:31 +01005297 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005298 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01005299
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005301 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
Francois Romieuda78dbf2012-01-26 14:18:23 +01005303 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005304 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005305 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306}
5307
5308static void rtl8169_tx_timeout(struct net_device *dev)
5309{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005310 struct rtl8169_private *tp = netdev_priv(dev);
5311
5312 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313}
5314
Heiner Kallweit734c1402018-11-22 21:56:48 +01005315static __le32 rtl8169_get_txd_opts1(u32 opts0, u32 len, unsigned int entry)
5316{
5317 u32 status = opts0 | len;
5318
5319 if (entry == NUM_TX_DESC - 1)
5320 status |= RingEnd;
5321
5322 return cpu_to_le32(status);
5323}
5324
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005326 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327{
5328 struct skb_shared_info *info = skb_shinfo(skb);
5329 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005330 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005331 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
5333 entry = tp->cur_tx;
5334 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005335 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005337 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 void *addr;
5339
5340 entry = (entry + 1) % NUM_TX_DESC;
5341
5342 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005343 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005344 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005345 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005346 if (unlikely(dma_mapping_error(d, mapping))) {
5347 if (net_ratelimit())
5348 netif_err(tp, drv, tp->dev,
5349 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005350 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
Heiner Kallweit734c1402018-11-22 21:56:48 +01005353 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005354 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 txd->addr = cpu_to_le64(mapping);
5356
5357 tp->tx_skb[entry].len = len;
5358 }
5359
5360 if (cur_frag) {
5361 tp->tx_skb[entry].skb = skb;
5362 txd->opts1 |= cpu_to_le32(LastFrag);
5363 }
5364
5365 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005366
5367err_out:
5368 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5369 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370}
5371
françois romieub423e9a2013-05-18 01:24:46 +00005372static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5373{
5374 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5375}
5376
hayeswange9746042014-07-11 16:25:58 +08005377/* msdn_giant_send_check()
5378 * According to the document of microsoft, the TCP Pseudo Header excludes the
5379 * packet length for IPv6 TCP large packets.
5380 */
5381static int msdn_giant_send_check(struct sk_buff *skb)
5382{
5383 const struct ipv6hdr *ipv6h;
5384 struct tcphdr *th;
5385 int ret;
5386
5387 ret = skb_cow_head(skb, 0);
5388 if (ret)
5389 return ret;
5390
5391 ipv6h = ipv6_hdr(skb);
5392 th = tcp_hdr(skb);
5393
5394 th->check = 0;
5395 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
5396
5397 return ret;
5398}
5399
Heiner Kallweit87945b62019-05-31 19:55:11 +02005400static void rtl8169_tso_csum_v1(struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
Michał Mirosław350fb322011-04-08 06:35:56 +00005402 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
Francois Romieu2b7b4312011-04-18 22:53:24 -07005404 if (mss) {
5405 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08005406 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5407 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5408 const struct iphdr *ip = ip_hdr(skb);
5409
5410 if (ip->protocol == IPPROTO_TCP)
5411 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5412 else if (ip->protocol == IPPROTO_UDP)
5413 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5414 else
5415 WARN_ON_ONCE(1);
5416 }
hayeswang5888d3f2014-07-11 16:25:56 +08005417}
5418
5419static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5420 struct sk_buff *skb, u32 *opts)
5421{
hayeswangbdfa4ed2014-07-11 16:25:57 +08005422 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08005423 u32 mss = skb_shinfo(skb)->gso_size;
5424
5425 if (mss) {
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005426 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005427 case htons(ETH_P_IP):
5428 opts[0] |= TD1_GTSENV4;
5429 break;
5430
5431 case htons(ETH_P_IPV6):
5432 if (msdn_giant_send_check(skb))
5433 return false;
5434
5435 opts[0] |= TD1_GTSENV6;
5436 break;
5437
5438 default:
5439 WARN_ON_ONCE(1);
5440 break;
5441 }
5442
hayeswangbdfa4ed2014-07-11 16:25:57 +08005443 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08005444 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005445 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08005446 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005448 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005449 case htons(ETH_P_IP):
5450 opts[1] |= TD1_IPv4_CS;
5451 ip_protocol = ip_hdr(skb)->protocol;
5452 break;
5453
5454 case htons(ETH_P_IPV6):
5455 opts[1] |= TD1_IPv6_CS;
5456 ip_protocol = ipv6_hdr(skb)->nexthdr;
5457 break;
5458
5459 default:
5460 ip_protocol = IPPROTO_RAW;
5461 break;
5462 }
5463
5464 if (ip_protocol == IPPROTO_TCP)
5465 opts[1] |= TD1_TCP_CS;
5466 else if (ip_protocol == IPPROTO_UDP)
5467 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005468 else
5469 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08005470
5471 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00005472 } else {
5473 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08005474 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 }
hayeswang5888d3f2014-07-11 16:25:56 +08005476
françois romieub423e9a2013-05-18 01:24:46 +00005477 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
Heiner Kallweit76085c92018-11-22 22:03:08 +01005480static bool rtl_tx_slots_avail(struct rtl8169_private *tp,
5481 unsigned int nr_frags)
5482{
5483 unsigned int slots_avail = tp->dirty_tx + NUM_TX_DESC - tp->cur_tx;
5484
5485 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
5486 return slots_avail > nr_frags;
5487}
5488
Heiner Kallweit87945b62019-05-31 19:55:11 +02005489/* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
5490static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
5491{
5492 switch (tp->mac_version) {
5493 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
5494 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
5495 return false;
5496 default:
5497 return true;
5498 }
5499}
5500
Stephen Hemminger613573252009-08-31 19:50:58 +00005501static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5502 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503{
5504 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005505 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005507 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005509 u32 opts[2], len;
Heiner Kallweitef143582019-07-28 11:25:19 +02005510 bool stop_queue;
5511 bool door_bell;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005512 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005513
Heiner Kallweit76085c92018-11-22 22:03:08 +01005514 if (unlikely(!rtl_tx_slots_avail(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005515 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005516 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 }
5518
5519 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005520 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
Heiner Kallweit355f9482019-06-06 07:49:17 +02005522 opts[1] = rtl8169_tx_vlan_tag(skb);
françois romieub423e9a2013-05-18 01:24:46 +00005523 opts[0] = DescOwn;
5524
Heiner Kallweit87945b62019-05-31 19:55:11 +02005525 if (rtl_chip_supports_csum_v2(tp)) {
Heiner Kallweit96ea7722019-07-26 21:50:34 +02005526 if (!rtl8169_tso_csum_v2(tp, skb, opts))
5527 goto err_dma_0;
Heiner Kallweit87945b62019-05-31 19:55:11 +02005528 } else {
5529 rtl8169_tso_csum_v1(skb, opts);
hayeswange9746042014-07-11 16:25:58 +08005530 }
françois romieub423e9a2013-05-18 01:24:46 +00005531
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005532 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005533 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005534 if (unlikely(dma_mapping_error(d, mapping))) {
5535 if (net_ratelimit())
5536 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005537 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
5540 tp->tx_skb[entry].len = len;
5541 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
Francois Romieu2b7b4312011-04-18 22:53:24 -07005543 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005544 if (frags < 0)
5545 goto err_dma_1;
5546 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005547 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005548 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005549 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005550 tp->tx_skb[entry].skb = skb;
5551 }
5552
Francois Romieu2b7b4312011-04-18 22:53:24 -07005553 txd->opts2 = cpu_to_le32(opts[1]);
5554
Richard Cochran5047fb52012-03-10 07:29:42 +00005555 skb_tx_timestamp(skb);
5556
Alexander Duycka0750132014-12-11 15:02:17 -08005557 /* Force memory writes to complete before releasing descriptor */
5558 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
Heiner Kallweitef143582019-07-28 11:25:19 +02005560 door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more());
5561
Heiner Kallweit734c1402018-11-22 21:56:48 +01005562 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
Alexander Duycka0750132014-12-11 15:02:17 -08005564 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00005565 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Alexander Duycka0750132014-12-11 15:02:17 -08005567 tp->cur_tx += frags + 1;
5568
Heiner Kallweitef143582019-07-28 11:25:19 +02005569 stop_queue = !rtl_tx_slots_avail(tp, MAX_SKB_FRAGS);
5570 if (unlikely(stop_queue)) {
Heiner Kallweit0255d592019-02-10 15:28:04 +01005571 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5572 * not miss a ring update when it notices a stopped queue.
5573 */
5574 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 netif_stop_queue(dev);
Heiner Kallweit4773f9b2019-08-12 20:47:40 +02005576 door_bell = true;
Heiner Kallweitef143582019-07-28 11:25:19 +02005577 }
5578
5579 if (door_bell)
5580 RTL_W8(tp, TxPoll, NPQ);
5581
5582 if (unlikely(stop_queue)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005583 /* Sync with rtl_tx:
5584 * - publish queue status and cur_tx ring index (write barrier)
5585 * - refresh dirty_tx ring index (read barrier).
5586 * May the current thread have a pessimistic view of the ring
5587 * status and forget to wake up queue, a racing rtl_tx thread
5588 * can't.
5589 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005590 smp_mb();
Heiner Kallweit76085c92018-11-22 22:03:08 +01005591 if (rtl_tx_slots_avail(tp, MAX_SKB_FRAGS))
Heiner Kallweit601ed4d2019-03-21 21:41:48 +01005592 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 }
5594
Stephen Hemminger613573252009-08-31 19:50:58 +00005595 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005597err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005598 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005599err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07005600 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005601 dev->stats.tx_dropped++;
5602 return NETDEV_TX_OK;
5603
5604err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005606 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005607 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608}
5609
Heiner Kallweite64e0c82019-07-26 21:49:22 +02005610static netdev_features_t rtl8169_features_check(struct sk_buff *skb,
5611 struct net_device *dev,
5612 netdev_features_t features)
5613{
5614 int transport_offset = skb_transport_offset(skb);
5615 struct rtl8169_private *tp = netdev_priv(dev);
5616
5617 if (skb_is_gso(skb)) {
5618 if (transport_offset > GTTCPHO_MAX &&
5619 rtl_chip_supports_csum_v2(tp))
5620 features &= ~NETIF_F_ALL_TSO;
5621 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5622 if (skb->len < ETH_ZLEN) {
5623 switch (tp->mac_version) {
5624 case RTL_GIGA_MAC_VER_11:
5625 case RTL_GIGA_MAC_VER_12:
5626 case RTL_GIGA_MAC_VER_17:
5627 case RTL_GIGA_MAC_VER_34:
5628 features &= ~NETIF_F_CSUM_MASK;
5629 break;
5630 default:
5631 break;
5632 }
5633 }
5634
5635 if (transport_offset > TCPHO_MAX &&
5636 rtl_chip_supports_csum_v2(tp))
5637 features &= ~NETIF_F_CSUM_MASK;
5638 }
5639
5640 return vlan_features_check(skb, features);
5641}
5642
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643static void rtl8169_pcierr_interrupt(struct net_device *dev)
5644{
5645 struct rtl8169_private *tp = netdev_priv(dev);
5646 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 u16 pci_status, pci_cmd;
5648
5649 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5650 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5651
Joe Perchesbf82c182010-02-09 11:49:50 +00005652 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5653 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
5655 /*
5656 * The recovery sequence below admits a very elaborated explanation:
5657 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005658 * - I did not see what else could be done;
5659 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 *
5661 * Feel free to adjust to your needs.
5662 */
Francois Romieua27993f2006-12-18 00:04:19 +01005663 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005664 pci_cmd &= ~PCI_COMMAND_PARITY;
5665 else
5666 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5667
5668 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
5670 pci_write_config_word(pdev, PCI_STATUS,
5671 pci_status & (PCI_STATUS_DETECTED_PARITY |
5672 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5673 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5674
Francois Romieu98ddf982012-01-31 10:47:34 +01005675 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676}
5677
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005678static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
5679 int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680{
Florian Westphald92060b2018-10-20 12:25:27 +02005681 unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 dirty_tx = tp->dirty_tx;
5684 smp_rmb();
5685 tx_left = tp->cur_tx - dirty_tx;
5686
5687 while (tx_left > 0) {
5688 unsigned int entry = dirty_tx % NUM_TX_DESC;
5689 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 u32 status;
5691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5693 if (status & DescOwn)
5694 break;
5695
Alexander Duycka0750132014-12-11 15:02:17 -08005696 /* This barrier is needed to keep us from reading
5697 * any other fields out of the Tx descriptor until
5698 * we know the status of DescOwn
5699 */
5700 dma_rmb();
5701
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005702 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005703 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 if (status & LastFrag) {
Florian Westphald92060b2018-10-20 12:25:27 +02005705 pkts_compl++;
5706 bytes_compl += tx_skb->skb->len;
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005707 napi_consume_skb(tx_skb->skb, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 tx_skb->skb = NULL;
5709 }
5710 dirty_tx++;
5711 tx_left--;
5712 }
5713
5714 if (tp->dirty_tx != dirty_tx) {
Florian Westphald92060b2018-10-20 12:25:27 +02005715 netdev_completed_queue(dev, pkts_compl, bytes_compl);
5716
5717 u64_stats_update_begin(&tp->tx_stats.syncp);
5718 tp->tx_stats.packets += pkts_compl;
5719 tp->tx_stats.bytes += bytes_compl;
5720 u64_stats_update_end(&tp->tx_stats.syncp);
5721
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005723 /* Sync with rtl8169_start_xmit:
5724 * - publish dirty_tx ring index (write barrier)
5725 * - refresh cur_tx ring index and queue status (read barrier)
5726 * May the current thread miss the stopped queue condition,
5727 * a racing xmit thread can only have a right view of the
5728 * ring status.
5729 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005730 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 if (netif_queue_stopped(dev) &&
Heiner Kallweit76085c92018-11-22 22:03:08 +01005732 rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 netif_wake_queue(dev);
5734 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005735 /*
5736 * 8168 hack: TxPoll requests are lost when the Tx packets are
5737 * too close. Let's kick an extra TxPoll request when a burst
5738 * of start_xmit activity is detected (if it is not detected,
5739 * it is slow enough). -- FR
5740 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005741 if (tp->cur_tx != dirty_tx)
5742 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 }
5744}
5745
Francois Romieu126fa4b2005-05-12 20:09:17 -04005746static inline int rtl8169_fragmented_frame(u32 status)
5747{
5748 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5749}
5750
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005751static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 u32 status = opts1 & RxProtoMask;
5754
5755 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005756 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 skb->ip_summed = CHECKSUM_UNNECESSARY;
5758 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005759 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760}
5761
Francois Romieuda78dbf2012-01-26 14:18:23 +01005762static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763{
5764 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005765 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
Timo Teräs9fba0812013-01-15 21:01:24 +00005769 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 unsigned int entry = cur_rx % NUM_RX_DESC;
Heiner Kallweit32879f02019-08-07 21:38:22 +02005771 const void *rx_buf = page_address(tp->Rx_databuff[entry]);
Francois Romieu126fa4b2005-05-12 20:09:17 -04005772 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 u32 status;
5774
Heiner Kallweit62028062018-04-17 23:30:29 +02005775 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 if (status & DescOwn)
5777 break;
Alexander Duycka0750132014-12-11 15:02:17 -08005778
5779 /* This barrier is needed to keep us from reading
5780 * any other fields out of the Rx descriptor until
5781 * we know the status of DescOwn
5782 */
5783 dma_rmb();
5784
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005785 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005786 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5787 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005788 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005790 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005792 dev->stats.rx_crc_errors++;
Heiner Kallweite9588eb2019-05-25 21:14:39 +02005793 if (status & (RxRUNT | RxCRC) && !(status & RxRWT) &&
5794 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00005795 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02005796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 } else {
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005798 unsigned int pkt_size;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005799 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005800
5801process_pkt:
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005802 pkt_size = status & GENMASK(13, 0);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005803 if (likely(!(dev->features & NETIF_F_RXFCS)))
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005804 pkt_size -= ETH_FCS_LEN;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005805 /*
5806 * The driver does not support incoming fragmented
5807 * frames. They are seen as a symptom of over-mtu
5808 * sized frames.
5809 */
5810 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005811 dev->stats.rx_dropped++;
5812 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00005813 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005814 }
5815
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005816 skb = napi_alloc_skb(&tp->napi, pkt_size);
5817 if (unlikely(!skb)) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005818 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00005819 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 }
5821
Heiner Kallweit3c95e502019-08-26 22:52:36 +02005822 dma_sync_single_for_cpu(tp_to_dev(tp),
5823 le64_to_cpu(desc->addr),
5824 pkt_size, DMA_FROM_DEVICE);
Heiner Kallweit32879f02019-08-07 21:38:22 +02005825 prefetch(rx_buf);
5826 skb_copy_to_linear_data(skb, rx_buf, pkt_size);
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005827 skb->tail += pkt_size;
5828 skb->len = pkt_size;
5829
Heiner Kallweitd4ed7462019-08-23 20:07:26 +02005830 dma_sync_single_for_device(tp_to_dev(tp),
5831 le64_to_cpu(desc->addr),
5832 pkt_size, DMA_FROM_DEVICE);
5833
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005834 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 skb->protocol = eth_type_trans(skb, dev);
5836
Francois Romieu7a8fc772011-03-01 17:18:33 +01005837 rtl8169_rx_vlan_tag(desc, skb);
5838
françois romieu39174292015-11-11 23:35:18 +01005839 if (skb->pkt_type == PACKET_MULTICAST)
5840 dev->stats.multicast++;
5841
Heiner Kallweit448a2412019-04-03 19:54:12 +02005842 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
Junchang Wang8027aa22012-03-04 23:30:32 +01005844 u64_stats_update_begin(&tp->rx_stats.syncp);
5845 tp->rx_stats.packets++;
5846 tp->rx_stats.bytes += pkt_size;
5847 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 }
françois romieuce11ff52013-01-24 13:30:06 +00005849release_descriptor:
5850 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005851 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 }
5853
5854 count = cur_rx - tp->cur_rx;
5855 tp->cur_rx = cur_rx;
5856
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 return count;
5858}
5859
Francois Romieu07d3f512007-02-21 22:40:46 +01005860static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02005862 struct rtl8169_private *tp = dev_instance;
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02005863 u32 status = rtl_get_events(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02005865 if (!tp->irq_enabled || (status & 0xffff) == 0xffff ||
5866 !(status & tp->irq_mask))
Heiner Kallweit05bbe552018-08-10 22:38:29 +02005867 return IRQ_NONE;
françois romieu811fd302011-12-04 20:30:45 +00005868
Heiner Kallweit38caff52018-10-18 22:19:28 +02005869 if (unlikely(status & SYSErr)) {
5870 rtl8169_pcierr_interrupt(tp->dev);
5871 goto out;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005872 }
5873
Heiner Kallweit703732f2019-01-19 22:07:05 +01005874 if (status & LinkChg)
5875 phy_mac_interrupt(tp->phydev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005876
Heiner Kallweit38caff52018-10-18 22:19:28 +02005877 if (unlikely(status & RxFIFOOver &&
5878 tp->mac_version == RTL_GIGA_MAC_VER_11)) {
5879 netif_stop_queue(tp->dev);
5880 /* XXX - Hack alert. See rtl_task(). */
5881 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
5882 }
5883
Heiner Kallweite62b2fd2019-04-14 11:48:39 +02005884 rtl_irq_disable(tp);
5885 napi_schedule_irqoff(&tp->napi);
Heiner Kallweit38caff52018-10-18 22:19:28 +02005886out:
5887 rtl_ack_events(tp, status);
5888
5889 return IRQ_HANDLED;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005890}
5891
Francois Romieu4422bcd2012-01-26 11:23:32 +01005892static void rtl_task(struct work_struct *work)
5893{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005894 static const struct {
5895 int bitnr;
5896 void (*action)(struct rtl8169_private *);
5897 } rtl_work[] = {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005898 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01005899 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005900 struct rtl8169_private *tp =
5901 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005902 struct net_device *dev = tp->dev;
5903 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005904
Francois Romieuda78dbf2012-01-26 14:18:23 +01005905 rtl_lock_work(tp);
5906
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005907 if (!netif_running(dev) ||
5908 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005909 goto out_unlock;
5910
5911 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5912 bool pending;
5913
Francois Romieuda78dbf2012-01-26 14:18:23 +01005914 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005915 if (pending)
5916 rtl_work[i].action(tp);
5917 }
5918
5919out_unlock:
5920 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005921}
5922
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005923static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005925 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5926 struct net_device *dev = tp->dev;
Heiner Kallweit6b839b62018-10-18 19:56:01 +02005927 int work_done;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005928
Heiner Kallweit6b839b62018-10-18 19:56:01 +02005929 work_done = rtl_rx(dev, tp, (u32) budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005930
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005931 rtl_tx(dev, tp, budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005932
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005933 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08005934 napi_complete_done(napi, work_done);
Heiner Kallweitfe716f82018-11-19 22:31:32 +01005935 rtl_irq_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 }
5937
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005938 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005941static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02005942{
5943 struct rtl8169_private *tp = netdev_priv(dev);
5944
5945 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5946 return;
5947
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005948 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
5949 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02005950}
5951
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005952static void r8169_phylink_handler(struct net_device *ndev)
5953{
5954 struct rtl8169_private *tp = netdev_priv(ndev);
5955
5956 if (netif_carrier_ok(ndev)) {
5957 rtl_link_chg_patch(tp);
5958 pm_request_resume(&tp->pci_dev->dev);
5959 } else {
5960 pm_runtime_idle(&tp->pci_dev->dev);
5961 }
5962
5963 if (net_ratelimit())
Heiner Kallweit703732f2019-01-19 22:07:05 +01005964 phy_print_status(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005965}
5966
5967static int r8169_phy_connect(struct rtl8169_private *tp)
5968{
Heiner Kallweit703732f2019-01-19 22:07:05 +01005969 struct phy_device *phydev = tp->phydev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005970 phy_interface_t phy_mode;
5971 int ret;
5972
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02005973 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005974 PHY_INTERFACE_MODE_MII;
5975
5976 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
5977 phy_mode);
5978 if (ret)
5979 return ret;
5980
Heiner Kallweit66058b12019-07-27 12:32:28 +02005981 if (!tp->supports_gmii)
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005982 phy_set_max_speed(phydev, SPEED_100);
5983
Heiner Kallweit9cf9b842019-05-04 12:01:03 +02005984 phy_support_asym_pause(phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005985
5986 phy_attached_info(phydev);
5987
5988 return 0;
5989}
5990
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991static void rtl8169_down(struct net_device *dev)
5992{
5993 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
Heiner Kallweit703732f2019-01-19 22:07:05 +01005995 phy_stop(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005996
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005997 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005998 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Hayes Wang92fc43b2011-07-06 15:58:03 +08006000 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006001 /*
6002 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006003 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6004 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006005 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006006 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenney16f11502018-11-05 17:07:39 -08006009 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 rtl8169_tx_clear(tp);
6012
6013 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006014
6015 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016}
6017
6018static int rtl8169_close(struct net_device *dev)
6019{
6020 struct rtl8169_private *tp = netdev_priv(dev);
6021 struct pci_dev *pdev = tp->pci_dev;
6022
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006023 pm_runtime_get_sync(&pdev->dev);
6024
Francois Romieucecb5fd2011-04-01 10:21:07 +02006025 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006026 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006027
Francois Romieuda78dbf2012-01-26 14:18:23 +01006028 rtl_lock_work(tp);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006029 /* Clear all task flags */
6030 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006031
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006033 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Lekensteyn4ea72442013-07-22 09:53:30 +02006035 cancel_work_sync(&tp->wk.work);
6036
Heiner Kallweit703732f2019-01-19 22:07:05 +01006037 phy_disconnect(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006038
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006039 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006041 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6042 tp->RxPhyAddr);
6043 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6044 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 tp->TxDescArray = NULL;
6046 tp->RxDescArray = NULL;
6047
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006048 pm_runtime_put_sync(&pdev->dev);
6049
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 return 0;
6051}
6052
Francois Romieudc1c00c2012-03-08 10:06:18 +01006053#ifdef CONFIG_NET_POLL_CONTROLLER
6054static void rtl8169_netpoll(struct net_device *dev)
6055{
6056 struct rtl8169_private *tp = netdev_priv(dev);
6057
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03006058 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01006059}
6060#endif
6061
Francois Romieudf43ac72012-03-08 09:48:40 +01006062static int rtl_open(struct net_device *dev)
6063{
6064 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006065 struct pci_dev *pdev = tp->pci_dev;
6066 int retval = -ENOMEM;
6067
6068 pm_runtime_get_sync(&pdev->dev);
6069
6070 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006071 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006072 * dma_alloc_coherent provides more.
6073 */
6074 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6075 &tp->TxPhyAddr, GFP_KERNEL);
6076 if (!tp->TxDescArray)
6077 goto err_pm_runtime_put;
6078
6079 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6080 &tp->RxPhyAddr, GFP_KERNEL);
6081 if (!tp->RxDescArray)
6082 goto err_free_tx_0;
6083
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006084 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006085 if (retval < 0)
6086 goto err_free_rx_1;
6087
Francois Romieudf43ac72012-03-08 09:48:40 +01006088 rtl_request_firmware(tp);
6089
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006090 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006091 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01006092 if (retval < 0)
6093 goto err_release_fw_2;
6094
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006095 retval = r8169_phy_connect(tp);
6096 if (retval)
6097 goto err_free_irq;
6098
Francois Romieudf43ac72012-03-08 09:48:40 +01006099 rtl_lock_work(tp);
6100
6101 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6102
6103 napi_enable(&tp->napi);
6104
6105 rtl8169_init_phy(dev, tp);
6106
Francois Romieudf43ac72012-03-08 09:48:40 +01006107 rtl_pll_power_up(tp);
6108
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006109 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006110
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006111 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006112 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6113
Heiner Kallweit703732f2019-01-19 22:07:05 +01006114 phy_start(tp->phydev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006115 netif_start_queue(dev);
6116
6117 rtl_unlock_work(tp);
6118
Heiner Kallweita92a0842018-01-08 21:39:13 +01006119 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006120out:
6121 return retval;
6122
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006123err_free_irq:
6124 pci_free_irq(pdev, 0, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006125err_release_fw_2:
6126 rtl_release_firmware(tp);
6127 rtl8169_rx_clear(tp);
6128err_free_rx_1:
6129 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6130 tp->RxPhyAddr);
6131 tp->RxDescArray = NULL;
6132err_free_tx_0:
6133 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6134 tp->TxPhyAddr);
6135 tp->TxDescArray = NULL;
6136err_pm_runtime_put:
6137 pm_runtime_put_noidle(&pdev->dev);
6138 goto out;
6139}
6140
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006141static void
Junchang Wang8027aa22012-03-04 23:30:32 +01006142rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143{
6144 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006145 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02006146 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01006147 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006149 pm_runtime_get_noresume(&pdev->dev);
6150
6151 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006152 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006153
Junchang Wang8027aa22012-03-04 23:30:32 +01006154 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006155 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006156 stats->rx_packets = tp->rx_stats.packets;
6157 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006158 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006159
Junchang Wang8027aa22012-03-04 23:30:32 +01006160 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006161 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006162 stats->tx_packets = tp->tx_stats.packets;
6163 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006164 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006165
6166 stats->rx_dropped = dev->stats.rx_dropped;
6167 stats->tx_dropped = dev->stats.tx_dropped;
6168 stats->rx_length_errors = dev->stats.rx_length_errors;
6169 stats->rx_errors = dev->stats.rx_errors;
6170 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6171 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6172 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02006173 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01006174
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006175 /*
Corentin Musarded72a9b2019-07-24 14:34:43 +02006176 * Fetch additional counter values missing in stats collected by driver
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006177 * from tally counters.
6178 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006179 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006180 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006181
6182 /*
6183 * Subtract values fetched during initalization.
6184 * See rtl8169_init_counter_offsets for a description why we do that.
6185 */
Corinna Vinschen42020322015-09-10 10:47:35 +02006186 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006187 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02006188 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006189 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02006190 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006191 le16_to_cpu(tp->tc_offset.tx_aborted);
6192
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006193 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194}
6195
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006196static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006197{
françois romieu065c27c2011-01-03 15:08:12 +00006198 struct rtl8169_private *tp = netdev_priv(dev);
6199
Francois Romieu5d06a992006-02-23 00:47:58 +01006200 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006201 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006202
Heiner Kallweit703732f2019-01-19 22:07:05 +01006203 phy_stop(tp->phydev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006204 netif_device_detach(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006205
6206 rtl_lock_work(tp);
6207 napi_disable(&tp->napi);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006208 /* Clear all task flags */
6209 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6210
Francois Romieuda78dbf2012-01-26 14:18:23 +01006211 rtl_unlock_work(tp);
6212
6213 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006214}
Francois Romieu5d06a992006-02-23 00:47:58 +01006215
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006216#ifdef CONFIG_PM
6217
6218static int rtl8169_suspend(struct device *device)
6219{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006220 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006221 struct rtl8169_private *tp = netdev_priv(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006222
6223 rtl8169_net_suspend(dev);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006224 clk_disable_unprepare(tp->clk);
Francois Romieu1371fa62007-04-02 23:01:11 +02006225
Francois Romieu5d06a992006-02-23 00:47:58 +01006226 return 0;
6227}
6228
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006229static void __rtl8169_resume(struct net_device *dev)
6230{
françois romieu065c27c2011-01-03 15:08:12 +00006231 struct rtl8169_private *tp = netdev_priv(dev);
6232
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006233 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006234
6235 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02006236 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00006237
Heiner Kallweit703732f2019-01-19 22:07:05 +01006238 phy_start(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006239
Artem Savkovcff4c162012-04-03 10:29:11 +00006240 rtl_lock_work(tp);
6241 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006242 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Heiner Kallweit58ba5662019-01-19 22:06:25 +01006243 rtl_reset_work(tp);
Artem Savkovcff4c162012-04-03 10:29:11 +00006244 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006245}
6246
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006247static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006248{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006249 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006250 struct rtl8169_private *tp = netdev_priv(dev);
6251
Heiner Kallweit59715172019-05-29 07:44:01 +02006252 rtl_rar_set(tp, dev->dev_addr);
6253
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006254 clk_prepare_enable(tp->clk);
Francois Romieu5d06a992006-02-23 00:47:58 +01006255
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006256 if (netif_running(dev))
6257 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006258
Francois Romieu5d06a992006-02-23 00:47:58 +01006259 return 0;
6260}
6261
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006262static int rtl8169_runtime_suspend(struct device *device)
6263{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006264 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006265 struct rtl8169_private *tp = netdev_priv(dev);
6266
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006267 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006268 return 0;
6269
Francois Romieuda78dbf2012-01-26 14:18:23 +01006270 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006271 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006272 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006273
6274 rtl8169_net_suspend(dev);
6275
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006276 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006277 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006278 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006279
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006280 return 0;
6281}
6282
6283static int rtl8169_runtime_resume(struct device *device)
6284{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006285 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006286 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit59715172019-05-29 07:44:01 +02006287
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08006288 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006289
6290 if (!tp->TxDescArray)
6291 return 0;
6292
Francois Romieuda78dbf2012-01-26 14:18:23 +01006293 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006294 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006295 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006296
6297 __rtl8169_resume(dev);
6298
6299 return 0;
6300}
6301
6302static int rtl8169_runtime_idle(struct device *device)
6303{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006304 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006305
Heiner Kallweita92a0842018-01-08 21:39:13 +01006306 if (!netif_running(dev) || !netif_carrier_ok(dev))
6307 pm_schedule_suspend(device, 10000);
6308
6309 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006310}
6311
Alexey Dobriyan47145212009-12-14 18:00:08 -08006312static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006313 .suspend = rtl8169_suspend,
6314 .resume = rtl8169_resume,
6315 .freeze = rtl8169_suspend,
6316 .thaw = rtl8169_resume,
6317 .poweroff = rtl8169_suspend,
6318 .restore = rtl8169_resume,
6319 .runtime_suspend = rtl8169_runtime_suspend,
6320 .runtime_resume = rtl8169_runtime_resume,
6321 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006322};
6323
6324#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6325
6326#else /* !CONFIG_PM */
6327
6328#define RTL8169_PM_OPS NULL
6329
6330#endif /* !CONFIG_PM */
6331
David S. Miller1805b2f2011-10-24 18:18:09 -04006332static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6333{
David S. Miller1805b2f2011-10-24 18:18:09 -04006334 /* WoL fails with 8168b when the receiver is disabled. */
6335 switch (tp->mac_version) {
6336 case RTL_GIGA_MAC_VER_11:
6337 case RTL_GIGA_MAC_VER_12:
6338 case RTL_GIGA_MAC_VER_17:
6339 pci_clear_master(tp->pci_dev);
6340
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006341 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006342 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006343 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04006344 break;
6345 default:
6346 break;
6347 }
6348}
6349
Francois Romieu1765f952008-09-13 17:21:40 +02006350static void rtl_shutdown(struct pci_dev *pdev)
6351{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006352 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006353 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006354
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006355 rtl8169_net_suspend(dev);
6356
Francois Romieucecb5fd2011-04-01 10:21:07 +02006357 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006358 rtl_rar_set(tp, dev->perm_addr);
6359
Hayes Wang92fc43b2011-07-06 15:58:03 +08006360 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006361
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006362 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02006363 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006364 rtl_wol_suspend_quirk(tp);
6365 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006366 }
6367
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006368 pci_wake_from_d3(pdev, true);
6369 pci_set_power_state(pdev, PCI_D3hot);
6370 }
6371}
Francois Romieu5d06a992006-02-23 00:47:58 +01006372
Bill Pembertonbaf63292012-12-03 09:23:28 -05006373static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006374{
6375 struct net_device *dev = pci_get_drvdata(pdev);
6376 struct rtl8169_private *tp = netdev_priv(dev);
6377
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006378 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01006379 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01006380
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006381 netif_napi_del(&tp->napi);
6382
Francois Romieue27566e2012-03-08 09:54:01 +01006383 unregister_netdev(dev);
Heiner Kallweit703732f2019-01-19 22:07:05 +01006384 mdiobus_unregister(tp->phydev->mdio.bus);
Francois Romieue27566e2012-03-08 09:54:01 +01006385
6386 rtl_release_firmware(tp);
6387
6388 if (pci_dev_run_wake(pdev))
6389 pm_runtime_get_noresume(&pdev->dev);
6390
6391 /* restore original MAC address */
6392 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01006393}
6394
Francois Romieufa9c3852012-03-08 10:01:50 +01006395static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006396 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006397 .ndo_stop = rtl8169_close,
6398 .ndo_get_stats64 = rtl8169_get_stats64,
6399 .ndo_start_xmit = rtl8169_start_xmit,
Heiner Kallweite64e0c82019-07-26 21:49:22 +02006400 .ndo_features_check = rtl8169_features_check,
Francois Romieufa9c3852012-03-08 10:01:50 +01006401 .ndo_tx_timeout = rtl8169_tx_timeout,
6402 .ndo_validate_addr = eth_validate_addr,
6403 .ndo_change_mtu = rtl8169_change_mtu,
6404 .ndo_fix_features = rtl8169_fix_features,
6405 .ndo_set_features = rtl8169_set_features,
6406 .ndo_set_mac_address = rtl_set_mac_address,
6407 .ndo_do_ioctl = rtl8169_ioctl,
6408 .ndo_set_rx_mode = rtl_set_rx_mode,
6409#ifdef CONFIG_NET_POLL_CONTROLLER
6410 .ndo_poll_controller = rtl8169_netpoll,
6411#endif
6412
6413};
6414
Heiner Kallweitec9a4082019-06-10 18:21:50 +02006415static void rtl_set_irq_mask(struct rtl8169_private *tp)
6416{
6417 tp->irq_mask = RTL_EVENT_NAPI | LinkChg;
6418
6419 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6420 tp->irq_mask |= SYSErr | RxOverflow | RxFIFOOver;
6421 else if (tp->mac_version == RTL_GIGA_MAC_VER_11)
6422 /* special workaround needed */
6423 tp->irq_mask |= RxFIFOOver;
6424 else
6425 tp->irq_mask |= RxOverflow;
6426}
6427
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006428static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01006429{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006430 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006431
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006432 switch (tp->mac_version) {
6433 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006434 rtl_unlock_config_regs(tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006435 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006436 rtl_lock_config_regs(tp);
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006437 /* fall through */
6438 case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_24:
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006439 flags = PCI_IRQ_LEGACY;
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006440 break;
6441 default:
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006442 flags = PCI_IRQ_ALL_TYPES;
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006443 break;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006444 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006445
6446 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01006447}
6448
Thierry Reding04c77882019-02-06 13:30:17 +01006449static void rtl_read_mac_address(struct rtl8169_private *tp,
6450 u8 mac_addr[ETH_ALEN])
6451{
6452 /* Get MAC address */
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02006453 if (rtl_is_8168evl_up(tp) && tp->mac_version != RTL_GIGA_MAC_VER_34) {
6454 u32 value = rtl_eri_read(tp, 0xe0);
6455
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006456 mac_addr[0] = (value >> 0) & 0xff;
6457 mac_addr[1] = (value >> 8) & 0xff;
6458 mac_addr[2] = (value >> 16) & 0xff;
6459 mac_addr[3] = (value >> 24) & 0xff;
6460
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02006461 value = rtl_eri_read(tp, 0xe4);
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006462 mac_addr[4] = (value >> 0) & 0xff;
6463 mac_addr[5] = (value >> 8) & 0xff;
Thierry Reding04c77882019-02-06 13:30:17 +01006464 }
6465}
6466
Hayes Wangc5583862012-07-02 17:23:22 +08006467DECLARE_RTL_COND(rtl_link_list_ready_cond)
6468{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006469 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08006470}
6471
6472DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6473{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006474 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08006475}
6476
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006477static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
6478{
6479 struct rtl8169_private *tp = mii_bus->priv;
6480
6481 if (phyaddr > 0)
6482 return -ENODEV;
6483
6484 return rtl_readphy(tp, phyreg);
6485}
6486
6487static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
6488 int phyreg, u16 val)
6489{
6490 struct rtl8169_private *tp = mii_bus->priv;
6491
6492 if (phyaddr > 0)
6493 return -ENODEV;
6494
6495 rtl_writephy(tp, phyreg, val);
6496
6497 return 0;
6498}
6499
6500static int r8169_mdio_register(struct rtl8169_private *tp)
6501{
6502 struct pci_dev *pdev = tp->pci_dev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006503 struct mii_bus *new_bus;
6504 int ret;
6505
6506 new_bus = devm_mdiobus_alloc(&pdev->dev);
6507 if (!new_bus)
6508 return -ENOMEM;
6509
6510 new_bus->name = "r8169";
6511 new_bus->priv = tp;
6512 new_bus->parent = &pdev->dev;
6513 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
Heiner Kallweita1950162019-04-24 21:13:25 +02006514 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x", pci_dev_id(pdev));
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006515
6516 new_bus->read = r8169_mdio_read_reg;
6517 new_bus->write = r8169_mdio_write_reg;
6518
6519 ret = mdiobus_register(new_bus);
6520 if (ret)
6521 return ret;
6522
Heiner Kallweit703732f2019-01-19 22:07:05 +01006523 tp->phydev = mdiobus_get_phy(new_bus, 0);
6524 if (!tp->phydev) {
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006525 mdiobus_unregister(new_bus);
6526 return -ENODEV;
6527 }
6528
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02006529 /* PHY will be woken up in rtl_open() */
Heiner Kallweit703732f2019-01-19 22:07:05 +01006530 phy_suspend(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006531
6532 return 0;
6533}
6534
Bill Pembertonbaf63292012-12-03 09:23:28 -05006535static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006536{
Hayes Wangc5583862012-07-02 17:23:22 +08006537 tp->ocp_base = OCP_STD_PHY_BASE;
6538
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006539 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006540
6541 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6542 return;
6543
6544 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6545 return;
6546
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006547 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08006548 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006549 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08006550
Heiner Kallweitef712ed2019-08-04 09:47:51 +02006551 r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
Hayes Wangc5583862012-07-02 17:23:22 +08006552
6553 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6554 return;
6555
Heiner Kallweitef712ed2019-08-04 09:47:51 +02006556 r8168_mac_ocp_modify(tp, 0xe8de, 0, BIT(15));
Hayes Wangc5583862012-07-02 17:23:22 +08006557
Heiner Kallweit7160be22019-05-25 20:44:01 +02006558 rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
Hayes Wangc5583862012-07-02 17:23:22 +08006559}
6560
Bill Pembertonbaf63292012-12-03 09:23:28 -05006561static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006562{
6563 switch (tp->mac_version) {
Heiner Kallweit29ec0482019-05-25 20:43:25 +02006564 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
6565 rtl8168ep_stop_cmac(tp);
6566 /* fall through */
Heiner Kallweit2a718832018-05-02 21:39:49 +02006567 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006568 rtl_hw_init_8168g(tp);
6569 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006570 default:
6571 break;
6572 }
6573}
6574
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006575static int rtl_jumbo_max(struct rtl8169_private *tp)
6576{
6577 /* Non-GBit versions don't support jumbo frames */
6578 if (!tp->supports_gmii)
6579 return JUMBO_1K;
6580
6581 switch (tp->mac_version) {
6582 /* RTL8169 */
Heiner Kallweite9588eb2019-05-25 21:14:39 +02006583 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006584 return JUMBO_7K;
6585 /* RTL8168b */
6586 case RTL_GIGA_MAC_VER_11:
6587 case RTL_GIGA_MAC_VER_12:
6588 case RTL_GIGA_MAC_VER_17:
6589 return JUMBO_4K;
6590 /* RTL8168c */
6591 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
6592 return JUMBO_6K;
6593 default:
6594 return JUMBO_9K;
6595 }
6596}
6597
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006598static void rtl_disable_clk(void *data)
6599{
6600 clk_disable_unprepare(data);
6601}
6602
Heiner Kallweitb779dae2019-01-19 22:07:34 +01006603static int rtl_get_ether_clk(struct rtl8169_private *tp)
6604{
6605 struct device *d = tp_to_dev(tp);
6606 struct clk *clk;
6607 int rc;
6608
6609 clk = devm_clk_get(d, "ether_clk");
6610 if (IS_ERR(clk)) {
6611 rc = PTR_ERR(clk);
6612 if (rc == -ENOENT)
6613 /* clk-core allows NULL (for suspend / resume) */
6614 rc = 0;
6615 else if (rc != -EPROBE_DEFER)
6616 dev_err(d, "failed to get clk: %d\n", rc);
6617 } else {
6618 tp->clk = clk;
6619 rc = clk_prepare_enable(clk);
6620 if (rc)
6621 dev_err(d, "failed to enable clk: %d\n", rc);
6622 else
6623 rc = devm_add_action_or_reset(d, rtl_disable_clk, clk);
6624 }
6625
6626 return rc;
6627}
6628
Heiner Kallweitc782e202019-07-02 20:46:09 +02006629static void rtl_init_mac_address(struct rtl8169_private *tp)
6630{
6631 struct net_device *dev = tp->dev;
6632 u8 *mac_addr = dev->dev_addr;
6633 int rc, i;
6634
6635 rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr);
6636 if (!rc)
6637 goto done;
6638
6639 rtl_read_mac_address(tp, mac_addr);
6640 if (is_valid_ether_addr(mac_addr))
6641 goto done;
6642
6643 for (i = 0; i < ETH_ALEN; i++)
6644 mac_addr[i] = RTL_R8(tp, MAC0 + i);
6645 if (is_valid_ether_addr(mac_addr))
6646 goto done;
6647
6648 eth_hw_addr_random(dev);
6649 dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n");
6650done:
6651 rtl_rar_set(tp, mac_addr);
6652}
6653
hayeswang929a0312014-09-16 11:40:47 +08006654static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01006655{
Francois Romieu3b6cf252012-03-08 09:59:04 +01006656 struct rtl8169_private *tp;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006657 struct net_device *dev;
Heiner Kallweitc782e202019-07-02 20:46:09 +02006658 int chipset, region;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006659 int jumbo_max, rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006660
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006661 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
6662 if (!dev)
6663 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006664
6665 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006666 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006667 tp = netdev_priv(dev);
6668 tp->dev = dev;
6669 tp->pci_dev = pdev;
6670 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Heiner Kallweit145a40e2019-06-10 18:25:29 +02006671 tp->supports_gmii = ent->driver_data == RTL_CFG_NO_GBIT ? 0 : 1;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006672
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006673 /* Get the *optional* external "ether_clk" used on some boards */
Heiner Kallweitb779dae2019-01-19 22:07:34 +01006674 rc = rtl_get_ether_clk(tp);
6675 if (rc)
6676 return rc;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006677
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +02006678 /* Disable ASPM completely as that cause random device stop working
6679 * problems as well as full system hangs for some PCIe devices users.
6680 */
Heiner Kallweit62b1b3b2019-06-18 23:14:50 +02006681 rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
6682 PCIE_LINK_STATE_L1);
6683 tp->aspm_manageable = !rc;
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +02006684
Francois Romieu3b6cf252012-03-08 09:59:04 +01006685 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006686 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006687 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006688 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006689 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006690 }
6691
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006692 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02006693 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01006694
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02006695 /* use first MMIO region */
6696 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
6697 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006698 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006699 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006700 }
6701
6702 /* check for weird/broken PCI region reporting */
6703 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006704 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006705 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006706 }
6707
Andy Shevchenko93a00d42018-03-01 13:27:35 +02006708 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006709 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006710 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006711 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006712 }
6713
Andy Shevchenko93a00d42018-03-01 13:27:35 +02006714 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01006715
Francois Romieu3b6cf252012-03-08 09:59:04 +01006716 /* Identify chip attached to board */
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01006717 rtl8169_get_mac_version(tp);
6718 if (tp->mac_version == RTL_GIGA_MAC_NONE)
6719 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006720
Heiner Kallweit0ae09742018-04-28 22:19:26 +02006721 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006722
Heiner Kallweit10b63e82019-01-20 11:45:20 +01006723 if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
Heiner Kallweit3c18cbe2019-06-27 23:12:39 +02006724 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)))
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006725 dev->features |= NETIF_F_HIGHDMA;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006726
Francois Romieu3b6cf252012-03-08 09:59:04 +01006727 rtl_init_rxcfg(tp);
6728
Heiner Kallweitde20e122018-09-25 07:58:00 +02006729 rtl8169_irq_mask_and_ack(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006730
Hayes Wangc5583862012-07-02 17:23:22 +08006731 rtl_hw_initialize(tp);
6732
Francois Romieu3b6cf252012-03-08 09:59:04 +01006733 rtl_hw_reset(tp);
6734
Francois Romieu3b6cf252012-03-08 09:59:04 +01006735 pci_set_master(pdev);
6736
Francois Romieu3b6cf252012-03-08 09:59:04 +01006737 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006738
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006739 rc = rtl_alloc_irq(tp);
6740 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006741 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006742 return rc;
6743 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01006744
Francois Romieu3b6cf252012-03-08 09:59:04 +01006745 mutex_init(&tp->wk.mutex);
Heiner Kallweit5c41e782019-01-19 22:03:49 +01006746 INIT_WORK(&tp->wk.work, rtl_task);
Kyle McMartin340fea32014-02-24 20:12:28 -05006747 u64_stats_init(&tp->rx_stats.syncp);
6748 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006749
Heiner Kallweitc782e202019-07-02 20:46:09 +02006750 rtl_init_mac_address(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006751
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00006752 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006753
Heiner Kallweit37621492018-04-17 23:20:03 +02006754 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006755
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006756 dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6757 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6758 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006759 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00006760 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6761 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006762 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6763 NETIF_F_HIGHDMA;
Heiner Kallweit2d0ec542018-07-02 22:49:35 +02006764 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006765
hayeswang929a0312014-09-16 11:40:47 +08006766 tp->cp_cmd |= RxChkSum | RxVlan;
6767
6768 /*
6769 * Pretend we are using VLANs; This bypasses a nasty bug where
6770 * Interrupts stop flowing on high load on 8110SCd controllers.
6771 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01006772 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08006773 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00006774 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006775
Heiner Kallweit0170d592019-07-26 21:48:32 +02006776 if (rtl_chip_supports_csum_v2(tp)) {
hayeswange9746042014-07-11 16:25:58 +08006777 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006778 dev->features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweit0170d592019-07-26 21:48:32 +02006779 dev->gso_max_size = RTL_GSO_MAX_SIZE_V2;
6780 dev->gso_max_segs = RTL_GSO_MAX_SEGS_V2;
6781 } else {
6782 dev->gso_max_size = RTL_GSO_MAX_SIZE_V1;
6783 dev->gso_max_segs = RTL_GSO_MAX_SEGS_V1;
6784 }
hayeswang5888d3f2014-07-11 16:25:56 +08006785
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006786 /* RTL8168e-vl has a HW issue with TSO */
6787 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
Holger Hoffstättea7eb6a42019-08-09 00:02:40 +02006788 dev->vlan_features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
6789 dev->hw_features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
6790 dev->features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006791 }
6792
Francois Romieu3b6cf252012-03-08 09:59:04 +01006793 dev->hw_features |= NETIF_F_RXALL;
6794 dev->hw_features |= NETIF_F_RXFCS;
6795
Jarod Wilsonc7315a92016-10-17 15:54:09 -04006796 /* MTU range: 60 - hw-specific max */
6797 dev->min_mtu = ETH_ZLEN;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006798 jumbo_max = rtl_jumbo_max(tp);
6799 dev->max_mtu = jumbo_max;
Jarod Wilsonc7315a92016-10-17 15:54:09 -04006800
Heiner Kallweitec9a4082019-06-10 18:21:50 +02006801 rtl_set_irq_mask(tp);
Heiner Kallweit9fa0a8e2019-06-10 18:24:25 +02006802
Heiner Kallweit254764e2019-01-22 22:23:41 +01006803 tp->fw_name = rtl_chip_infos[chipset].fw_name;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006804
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006805 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
6806 &tp->counters_phys_addr,
6807 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01006808 if (!tp->counters)
6809 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02006810
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02006811 pci_set_drvdata(pdev, dev);
6812
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006813 rc = r8169_mdio_register(tp);
6814 if (rc)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01006815 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006816
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006817 /* chip gets powered up in rtl_open() */
6818 rtl_pll_power_down(tp);
6819
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006820 rc = register_netdev(dev);
6821 if (rc)
6822 goto err_mdio_unregister;
6823
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01006824 netif_info(tp, probe, dev, "%s, %pM, XID %03x, IRQ %d\n",
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02006825 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01006826 (RTL_R32(tp, TxConfig) >> 20) & 0xfcf,
Heiner Kallweit29274992018-02-28 20:43:38 +01006827 pci_irq_vector(pdev, 0));
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006828
6829 if (jumbo_max > JUMBO_1K)
6830 netif_info(tp, probe, dev,
6831 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
6832 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
6833 "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01006834
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006835 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01006836 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006837
Heiner Kallweita92a0842018-01-08 21:39:13 +01006838 if (pci_dev_run_wake(pdev))
6839 pm_runtime_put_sync(&pdev->dev);
6840
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006841 return 0;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006842
6843err_mdio_unregister:
Heiner Kallweit703732f2019-01-19 22:07:05 +01006844 mdiobus_unregister(tp->phydev->mdio.bus);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006845 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006846}
6847
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848static struct pci_driver rtl8169_pci_driver = {
6849 .name = MODULENAME,
6850 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006851 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05006852 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02006853 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006854 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855};
6856
Devendra Naga3eeb7da2012-10-26 09:27:42 +00006857module_pci_driver(rtl8169_pci_driver);