blob: bf00c3d8f4c8edc3bb5856a283d4c33e72b0aaf1 [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 &&
733 tp->mac_version != RTL_GIGA_MAC_VER_39;
734}
735
Heiner Kallweit2e779dd2019-08-15 14:14:18 +0200736static bool rtl_supports_eee(struct rtl8169_private *tp)
737{
738 return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
739 tp->mac_version != RTL_GIGA_MAC_VER_37 &&
740 tp->mac_version != RTL_GIGA_MAC_VER_39;
741}
742
Francois Romieuffc46952012-07-06 14:19:23 +0200743struct rtl_cond {
744 bool (*check)(struct rtl8169_private *);
745 const char *msg;
746};
747
748static void rtl_udelay(unsigned int d)
749{
750 udelay(d);
751}
752
753static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
754 void (*delay)(unsigned int), unsigned int d, int n,
755 bool high)
756{
757 int i;
758
759 for (i = 0; i < n; i++) {
Francois Romieuffc46952012-07-06 14:19:23 +0200760 if (c->check(tp) == high)
761 return true;
Heiner Kallweitd1f50502019-05-04 15:20:38 +0200762 delay(d);
Francois Romieuffc46952012-07-06 14:19:23 +0200763 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200764 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
765 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200766 return false;
767}
768
769static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
770 const struct rtl_cond *c,
771 unsigned int d, int n)
772{
773 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
774}
775
776static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
777 const struct rtl_cond *c,
778 unsigned int d, int n)
779{
780 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
781}
782
783static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
784 const struct rtl_cond *c,
785 unsigned int d, int n)
786{
787 return rtl_loop_wait(tp, c, msleep, d, n, true);
788}
789
790static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
791 const struct rtl_cond *c,
792 unsigned int d, int n)
793{
794 return rtl_loop_wait(tp, c, msleep, d, n, false);
795}
796
797#define DECLARE_RTL_COND(name) \
798static bool name ## _check(struct rtl8169_private *); \
799 \
800static const struct rtl_cond name = { \
801 .check = name ## _check, \
802 .msg = #name \
803}; \
804 \
805static bool name ## _check(struct rtl8169_private *tp)
806
Hayes Wangc5583862012-07-02 17:23:22 +0800807static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
808{
809 if (reg & 0xffff0001) {
810 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
811 return true;
812 }
813 return false;
814}
815
816DECLARE_RTL_COND(rtl_ocp_gphy_cond)
817{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200818 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800819}
820
821static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
822{
Hayes Wangc5583862012-07-02 17:23:22 +0800823 if (rtl_ocp_reg_failure(tp, reg))
824 return;
825
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200826 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800827
828 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
829}
830
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200831static int r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
Hayes Wangc5583862012-07-02 17:23:22 +0800832{
Hayes Wangc5583862012-07-02 17:23:22 +0800833 if (rtl_ocp_reg_failure(tp, reg))
834 return 0;
835
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200836 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800837
838 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200839 (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;
Hayes Wangc5583862012-07-02 17:23:22 +0800840}
841
Hayes Wangc5583862012-07-02 17:23:22 +0800842static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
843{
Hayes Wangc5583862012-07-02 17:23:22 +0800844 if (rtl_ocp_reg_failure(tp, reg))
845 return;
846
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200847 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800848}
849
850static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
851{
Hayes Wangc5583862012-07-02 17:23:22 +0800852 if (rtl_ocp_reg_failure(tp, reg))
853 return 0;
854
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200855 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800856
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200857 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800858}
859
Heiner Kallweitef712ed2019-08-04 09:47:51 +0200860static void r8168_mac_ocp_modify(struct rtl8169_private *tp, u32 reg, u16 mask,
861 u16 set)
862{
863 u16 data = r8168_mac_ocp_read(tp, reg);
864
865 r8168_mac_ocp_write(tp, reg, (data & ~mask) | set);
866}
867
Hayes Wangc5583862012-07-02 17:23:22 +0800868#define OCP_STD_PHY_BASE 0xa400
869
870static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
871{
872 if (reg == 0x1f) {
873 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
874 return;
875 }
876
877 if (tp->ocp_base != OCP_STD_PHY_BASE)
878 reg -= 0x10;
879
880 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
881}
882
883static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
884{
885 if (tp->ocp_base != OCP_STD_PHY_BASE)
886 reg -= 0x10;
887
888 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
889}
890
hayeswangeee37862013-04-01 22:23:38 +0000891static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
892{
893 if (reg == 0x1f) {
894 tp->ocp_base = value << 4;
895 return;
896 }
897
898 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
899}
900
901static int mac_mcu_read(struct rtl8169_private *tp, int reg)
902{
903 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
904}
905
Francois Romieuffc46952012-07-06 14:19:23 +0200906DECLARE_RTL_COND(rtl_phyar_cond)
907{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200908 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200909}
910
Francois Romieu24192212012-07-06 20:19:42 +0200911static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200913 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Francois Romieuffc46952012-07-06 14:19:23 +0200915 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700916 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700917 * According to hardware specs a 20us delay is required after write
918 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700919 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700920 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Francois Romieu24192212012-07-06 20:19:42 +0200923static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Francois Romieuffc46952012-07-06 14:19:23 +0200925 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200927 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Francois Romieuffc46952012-07-06 14:19:23 +0200929 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200930 RTL_R32(tp, PHYAR) & 0xffff : -ETIMEDOUT;
Francois Romieuffc46952012-07-06 14:19:23 +0200931
Timo Teräs81a95f02010-06-09 17:31:48 -0700932 /*
933 * According to hardware specs a 20us delay is required after read
934 * complete indication, but before sending next command.
935 */
936 udelay(20);
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return value;
939}
940
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800941DECLARE_RTL_COND(rtl_ocpar_cond)
942{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200943 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800944}
945
Francois Romieu24192212012-07-06 20:19:42 +0200946static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000947{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200948 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
949 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
950 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000951
Francois Romieuffc46952012-07-06 14:19:23 +0200952 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +0000953}
954
Francois Romieu24192212012-07-06 20:19:42 +0200955static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000956{
Francois Romieu24192212012-07-06 20:19:42 +0200957 r8168dp_1_mdio_access(tp, reg,
958 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000959}
960
Francois Romieu24192212012-07-06 20:19:42 +0200961static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000962{
Francois Romieu24192212012-07-06 20:19:42 +0200963 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +0000964
965 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200966 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
967 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000968
Francois Romieuffc46952012-07-06 14:19:23 +0200969 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200970 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : -ETIMEDOUT;
françois romieuc0e45c12011-01-03 15:08:04 +0000971}
972
françois romieue6de30d2011-01-03 15:08:37 +0000973#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
974
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200975static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000976{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200977 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000978}
979
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200980static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000981{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200982 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000983}
984
Francois Romieu24192212012-07-06 20:19:42 +0200985static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +0000986{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200987 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000988
Francois Romieu24192212012-07-06 20:19:42 +0200989 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +0000990
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200991 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000992}
993
Francois Romieu24192212012-07-06 20:19:42 +0200994static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +0000995{
996 int value;
997
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200998 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000999
Francois Romieu24192212012-07-06 20:19:42 +02001000 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001001
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001002 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001003
1004 return value;
1005}
1006
Heiner Kallweitce8843a2019-05-29 21:15:06 +02001007static void rtl_writephy(struct rtl8169_private *tp, int location, int val)
Francois Romieudacf8152008-08-02 20:44:13 +02001008{
Heiner Kallweit5f950522019-05-31 19:53:28 +02001009 switch (tp->mac_version) {
1010 case RTL_GIGA_MAC_VER_27:
1011 r8168dp_1_mdio_write(tp, location, val);
1012 break;
1013 case RTL_GIGA_MAC_VER_28:
1014 case RTL_GIGA_MAC_VER_31:
1015 r8168dp_2_mdio_write(tp, location, val);
1016 break;
1017 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1018 r8168g_mdio_write(tp, location, val);
1019 break;
1020 default:
1021 r8169_mdio_write(tp, location, val);
1022 break;
1023 }
Francois Romieudacf8152008-08-02 20:44:13 +02001024}
1025
françois romieu4da19632011-01-03 15:07:55 +00001026static int rtl_readphy(struct rtl8169_private *tp, int location)
1027{
Heiner Kallweit5f950522019-05-31 19:53:28 +02001028 switch (tp->mac_version) {
1029 case RTL_GIGA_MAC_VER_27:
1030 return r8168dp_1_mdio_read(tp, location);
1031 case RTL_GIGA_MAC_VER_28:
1032 case RTL_GIGA_MAC_VER_31:
1033 return r8168dp_2_mdio_read(tp, location);
1034 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1035 return r8168g_mdio_read(tp, location);
1036 default:
1037 return r8169_mdio_read(tp, location);
1038 }
françois romieu4da19632011-01-03 15:07:55 +00001039}
1040
1041static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1042{
1043 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1044}
1045
Chun-Hao Lin76564422014-10-01 23:17:17 +08001046static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001047{
1048 int val;
1049
françois romieu4da19632011-01-03 15:07:55 +00001050 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001051 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001052}
1053
Francois Romieuffc46952012-07-06 14:19:23 +02001054DECLARE_RTL_COND(rtl_ephyar_cond)
1055{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001056 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001057}
1058
Francois Romieufdf6fc02012-07-06 22:40:38 +02001059static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001060{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001061 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001062 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1063
Francois Romieuffc46952012-07-06 14:19:23 +02001064 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1065
1066 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001067}
1068
Francois Romieufdf6fc02012-07-06 22:40:38 +02001069static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001070{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001071 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001072
Francois Romieuffc46952012-07-06 14:19:23 +02001073 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001074 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001075}
1076
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001077DECLARE_RTL_COND(rtl_eriar_cond)
1078{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001079 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001080}
1081
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001082static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1083 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001084{
Hayes Wang133ac402011-07-06 15:58:05 +08001085 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001086 RTL_W32(tp, ERIDR, val);
1087 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001088
Francois Romieuffc46952012-07-06 14:19:23 +02001089 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001090}
1091
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001092static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1093 u32 val)
1094{
1095 _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
1096}
1097
1098static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001099{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001100 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001101
Francois Romieuffc46952012-07-06 14:19:23 +02001102 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001103 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001104}
1105
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001106static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
1107{
1108 return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
1109}
1110
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001111static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001112 u32 m)
Hayes Wang133ac402011-07-06 15:58:05 +08001113{
1114 u32 val;
1115
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001116 val = rtl_eri_read(tp, addr);
1117 rtl_eri_write(tp, addr, mask, (val & ~m) | p);
Hayes Wang133ac402011-07-06 15:58:05 +08001118}
1119
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001120static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 mask,
1121 u32 p)
1122{
1123 rtl_w0w1_eri(tp, addr, mask, p, 0);
1124}
1125
1126static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 mask,
1127 u32 m)
1128{
1129 rtl_w0w1_eri(tp, addr, mask, 0, m);
1130}
1131
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001132static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1133{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001134 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001135 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001136 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001137}
1138
1139static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1140{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001141 return _rtl_eri_read(tp, reg, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001142}
1143
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001144static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1145 u32 data)
1146{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001147 RTL_W32(tp, OCPDR, data);
1148 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001149 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1150}
1151
1152static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1153 u32 data)
1154{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001155 _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1156 data, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001157}
1158
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001159static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001160{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001161 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001162
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001163 r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001164}
1165
1166#define OOB_CMD_RESET 0x00
1167#define OOB_CMD_DRIVER_START 0x05
1168#define OOB_CMD_DRIVER_STOP 0x06
1169
1170static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1171{
1172 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1173}
1174
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001175DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001176{
1177 u16 reg;
1178
1179 reg = rtl8168_get_ocp_reg(tp);
1180
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001181 return r8168dp_ocp_read(tp, 0x0f, reg) & 0x00000800;
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001182}
1183
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001184DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1185{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001186 return r8168ep_ocp_read(tp, 0x0f, 0x124) & 0x00000001;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001187}
1188
1189DECLARE_RTL_COND(rtl_ocp_tx_cond)
1190{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001191 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001192}
1193
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001194static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1195{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001196 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001197 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001198 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1199 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001200}
1201
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001202static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001203{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001204 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1205 rtl_msleep_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001206}
1207
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001208static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1209{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001210 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1211 r8168ep_ocp_write(tp, 0x01, 0x30,
1212 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001213 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1214}
1215
1216static void rtl8168_driver_start(struct rtl8169_private *tp)
1217{
1218 switch (tp->mac_version) {
1219 case RTL_GIGA_MAC_VER_27:
1220 case RTL_GIGA_MAC_VER_28:
1221 case RTL_GIGA_MAC_VER_31:
1222 rtl8168dp_driver_start(tp);
1223 break;
1224 case RTL_GIGA_MAC_VER_49:
1225 case RTL_GIGA_MAC_VER_50:
1226 case RTL_GIGA_MAC_VER_51:
1227 rtl8168ep_driver_start(tp);
1228 break;
1229 default:
1230 BUG();
1231 break;
1232 }
1233}
1234
1235static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1236{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001237 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1238 rtl_msleep_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001239}
1240
1241static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1242{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001243 rtl8168ep_stop_cmac(tp);
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001244 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1245 r8168ep_ocp_write(tp, 0x01, 0x30,
1246 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001247 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1248}
1249
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001250static void rtl8168_driver_stop(struct rtl8169_private *tp)
1251{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001252 switch (tp->mac_version) {
1253 case RTL_GIGA_MAC_VER_27:
1254 case RTL_GIGA_MAC_VER_28:
1255 case RTL_GIGA_MAC_VER_31:
1256 rtl8168dp_driver_stop(tp);
1257 break;
1258 case RTL_GIGA_MAC_VER_49:
1259 case RTL_GIGA_MAC_VER_50:
1260 case RTL_GIGA_MAC_VER_51:
1261 rtl8168ep_driver_stop(tp);
1262 break;
1263 default:
1264 BUG();
1265 break;
1266 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001267}
1268
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001269static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001270{
1271 u16 reg = rtl8168_get_ocp_reg(tp);
1272
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001273 return !!(r8168dp_ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001274}
1275
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001276static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001277{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001278 return !!(r8168ep_ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001279}
1280
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001281static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001282{
1283 switch (tp->mac_version) {
1284 case RTL_GIGA_MAC_VER_27:
1285 case RTL_GIGA_MAC_VER_28:
1286 case RTL_GIGA_MAC_VER_31:
1287 return r8168dp_check_dash(tp);
1288 case RTL_GIGA_MAC_VER_49:
1289 case RTL_GIGA_MAC_VER_50:
1290 case RTL_GIGA_MAC_VER_51:
1291 return r8168ep_check_dash(tp);
1292 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001293 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001294 }
1295}
1296
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001297static void rtl_reset_packet_filter(struct rtl8169_private *tp)
1298{
1299 rtl_eri_clear_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1300 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1301}
1302
Francois Romieuffc46952012-07-06 14:19:23 +02001303DECLARE_RTL_COND(rtl_efusear_cond)
1304{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001305 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001306}
1307
Francois Romieufdf6fc02012-07-06 22:40:38 +02001308static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001309{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001310 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001311
Francois Romieuffc46952012-07-06 14:19:23 +02001312 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001313 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001314}
1315
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02001316static u32 rtl_get_events(struct rtl8169_private *tp)
1317{
1318 return RTL_R16(tp, IntrStatus);
1319}
1320
1321static void rtl_ack_events(struct rtl8169_private *tp, u32 bits)
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001322{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001323 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001324}
1325
1326static void rtl_irq_disable(struct rtl8169_private *tp)
1327{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001328 RTL_W16(tp, IntrMask, 0);
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001329 tp->irq_enabled = 0;
Francois Romieu3e990ff2012-01-26 12:50:01 +01001330}
1331
Francois Romieuda78dbf2012-01-26 14:18:23 +01001332#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1333#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1334#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1335
Heiner Kallweitfe716f82018-11-19 22:31:32 +01001336static void rtl_irq_enable(struct rtl8169_private *tp)
Francois Romieuda78dbf2012-01-26 14:18:23 +01001337{
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001338 tp->irq_enabled = 1;
Heiner Kallweit559c3c02018-11-19 22:34:17 +01001339 RTL_W16(tp, IntrMask, tp->irq_mask);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001340}
1341
françois romieu811fd302011-12-04 20:30:45 +00001342static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001344 rtl_irq_disable(tp);
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02001345 rtl_ack_events(tp, 0xffffffff);
Heiner Kallweitde20e122018-09-25 07:58:00 +02001346 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001347 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Hayes Wang70090422011-07-06 15:58:06 +08001350static void rtl_link_chg_patch(struct rtl8169_private *tp)
1351{
Hayes Wang70090422011-07-06 15:58:06 +08001352 struct net_device *dev = tp->dev;
Heiner Kallweit703732f2019-01-19 22:07:05 +01001353 struct phy_device *phydev = tp->phydev;
Hayes Wang70090422011-07-06 15:58:06 +08001354
1355 if (!netif_running(dev))
1356 return;
1357
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001358 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1359 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001360 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001361 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1362 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001363 } else if (phydev->speed == SPEED_100) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001364 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1365 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wang70090422011-07-06 15:58:06 +08001366 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001367 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1368 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wang70090422011-07-06 15:58:06 +08001369 }
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001370 rtl_reset_packet_filter(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08001371 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1372 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001373 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001374 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1375 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wangc2218922011-09-06 16:55:18 +08001376 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001377 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1378 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wangc2218922011-09-06 16:55:18 +08001379 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001380 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001381 if (phydev->speed == SPEED_10) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001382 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
1383 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001384 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001385 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001386 }
Hayes Wang70090422011-07-06 15:58:06 +08001387 }
1388}
1389
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001390#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1391
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001392static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1393{
1394 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001395
Francois Romieuda78dbf2012-01-26 14:18:23 +01001396 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001397 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001398 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001399 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001400}
1401
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001402static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001403{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001404 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001405 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001406 u32 opt;
1407 u16 reg;
1408 u8 mask;
1409 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001410 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001411 { WAKE_UCAST, Config5, UWF },
1412 { WAKE_BCAST, Config5, BWF },
1413 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001414 { WAKE_ANY, Config5, LanWake },
1415 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001416 };
Francois Romieu851e6022012-04-17 11:10:11 +02001417 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001418
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001419 rtl_unlock_config_regs(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001420
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02001421 if (rtl_is_8168evl_up(tp)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001422 tmp = ARRAY_SIZE(cfg) - 1;
1423 if (wolopts & WAKE_MAGIC)
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001424 rtl_eri_set_bits(tp, 0x0dc, ERIAR_MASK_0100,
1425 MagicPacket_v2);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001426 else
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001427 rtl_eri_clear_bits(tp, 0x0dc, ERIAR_MASK_0100,
1428 MagicPacket_v2);
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02001429 } else {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001430 tmp = ARRAY_SIZE(cfg);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001431 }
1432
1433 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001434 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001435 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001436 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001437 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001438 }
1439
Francois Romieu851e6022012-04-17 11:10:11 +02001440 switch (tp->mac_version) {
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001441 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001442 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001443 if (wolopts)
1444 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001445 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001446 break;
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001447 case RTL_GIGA_MAC_VER_34:
1448 case RTL_GIGA_MAC_VER_37:
1449 case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001450 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001451 if (wolopts)
1452 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001453 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001454 break;
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001455 default:
1456 break;
Francois Romieu851e6022012-04-17 11:10:11 +02001457 }
1458
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001459 rtl_lock_config_regs(tp);
Heiner Kallweit3bd82642018-12-30 13:16:12 +01001460
1461 device_set_wakeup_enable(tp_to_dev(tp), wolopts);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001462}
1463
1464static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1465{
1466 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001467 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001468
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001469 if (wol->wolopts & ~WAKE_ANY)
1470 return -EINVAL;
1471
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001472 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001473
Francois Romieuda78dbf2012-01-26 14:18:23 +01001474 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001475
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001476 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001477
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001478 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001479 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001480
1481 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001482
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001483 pm_runtime_put_noidle(d);
1484
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001485 return 0;
1486}
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488static void rtl8169_get_drvinfo(struct net_device *dev,
1489 struct ethtool_drvinfo *info)
1490{
1491 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001492 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Rick Jones68aad782011-11-07 13:29:27 +00001494 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001495 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001496 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Heiner Kallweit254764e2019-01-22 22:23:41 +01001497 if (rtl_fw)
Rick Jones8ac72d12011-11-22 14:06:26 +00001498 strlcpy(info->fw_version, rtl_fw->version,
1499 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502static int rtl8169_get_regs_len(struct net_device *dev)
1503{
1504 return R8169_REGS_SIZE;
1505}
1506
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001507static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1508 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Francois Romieud58d46b2011-05-03 16:38:29 +02001510 struct rtl8169_private *tp = netdev_priv(dev);
1511
Francois Romieu2b7b4312011-04-18 22:53:24 -07001512 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001513 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Francois Romieud58d46b2011-05-03 16:38:29 +02001515 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001516 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001517 features &= ~NETIF_F_IP_CSUM;
1518
Michał Mirosław350fb322011-04-08 06:35:56 +00001519 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
Heiner Kallweita3984572018-04-28 22:19:15 +02001522static int rtl8169_set_features(struct net_device *dev,
1523 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001526 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Heiner Kallweita3984572018-04-28 22:19:15 +02001528 rtl_lock_work(tp);
1529
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001530 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001531 if (features & NETIF_F_RXALL)
1532 rx_config |= (AcceptErr | AcceptRunt);
1533 else
1534 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001536 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001537
hayeswang929a0312014-09-16 11:40:47 +08001538 if (features & NETIF_F_RXCSUM)
1539 tp->cp_cmd |= RxChkSum;
1540 else
1541 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001542
hayeswang929a0312014-09-16 11:40:47 +08001543 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1544 tp->cp_cmd |= RxVlan;
1545 else
1546 tp->cp_cmd &= ~RxVlan;
1547
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001548 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1549 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Francois Romieuda78dbf2012-01-26 14:18:23 +01001551 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 return 0;
1554}
1555
Kirill Smelkov810f4892012-11-10 21:11:02 +04001556static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001558 return (skb_vlan_tag_present(skb)) ?
Heiner Kallweit7424edb2019-07-02 07:59:17 +02001559 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Francois Romieu7a8fc772011-03-01 17:18:33 +01001562static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Francois Romieu7a8fc772011-03-01 17:18:33 +01001566 if (opts2 & RxVlanTag)
Heiner Kallweit7424edb2019-07-02 07:59:17 +02001567 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1571 void *p)
1572{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001573 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001574 u32 __iomem *data = tp->mmio_addr;
1575 u32 *dw = p;
1576 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Francois Romieuda78dbf2012-01-26 14:18:23 +01001578 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001579 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1580 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001581 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001584static u32 rtl8169_get_msglevel(struct net_device *dev)
1585{
1586 struct rtl8169_private *tp = netdev_priv(dev);
1587
1588 return tp->msg_enable;
1589}
1590
1591static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1592{
1593 struct rtl8169_private *tp = netdev_priv(dev);
1594
1595 tp->msg_enable = value;
1596}
1597
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001598static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1599 "tx_packets",
1600 "rx_packets",
1601 "tx_errors",
1602 "rx_errors",
1603 "rx_missed",
1604 "align_errors",
1605 "tx_single_collisions",
1606 "tx_multi_collisions",
1607 "unicast",
1608 "broadcast",
1609 "multicast",
1610 "tx_aborted",
1611 "tx_underrun",
1612};
1613
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001614static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001615{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001616 switch (sset) {
1617 case ETH_SS_STATS:
1618 return ARRAY_SIZE(rtl8169_gstrings);
1619 default:
1620 return -EOPNOTSUPP;
1621 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001622}
1623
Corinna Vinschen42020322015-09-10 10:47:35 +02001624DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001625{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001626 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001627}
1628
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001629static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001630{
Corinna Vinschen42020322015-09-10 10:47:35 +02001631 dma_addr_t paddr = tp->counters_phys_addr;
1632 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001633
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001634 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1635 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001636 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001637 RTL_W32(tp, CounterAddrLow, cmd);
1638 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001639
Francois Romieua78e9362018-01-26 01:53:26 +01001640 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001641}
1642
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001643static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001644{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001645 /*
1646 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1647 * tally counters.
1648 */
1649 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1650 return true;
1651
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001652 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001653}
1654
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001655static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001656{
Heiner Kallweit10262b02019-01-06 20:44:00 +01001657 u8 val = RTL_R8(tp, ChipCmd);
1658
Ivan Vecera355423d2009-02-06 21:49:57 -08001659 /*
1660 * Some chips are unable to dump tally counters when the receiver
Heiner Kallweit10262b02019-01-06 20:44:00 +01001661 * is disabled. If 0xff chip may be in a PCI power-save state.
Ivan Vecera355423d2009-02-06 21:49:57 -08001662 */
Heiner Kallweit10262b02019-01-06 20:44:00 +01001663 if (!(val & CmdRxEnb) || val == 0xff)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001664 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001665
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001666 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001667}
1668
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001669static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001670{
Corinna Vinschen42020322015-09-10 10:47:35 +02001671 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001672 bool ret = false;
1673
1674 /*
1675 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1676 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1677 * reset by a power cycle, while the counter values collected by the
1678 * driver are reset at every driver unload/load cycle.
1679 *
1680 * To make sure the HW values returned by @get_stats64 match the SW
1681 * values, we collect the initial values at first open(*) and use them
1682 * as offsets to normalize the values returned by @get_stats64.
1683 *
1684 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1685 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1686 * set at open time by rtl_hw_start.
1687 */
1688
1689 if (tp->tc_offset.inited)
1690 return true;
1691
1692 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001693 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001694 ret = true;
1695
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001696 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001697 ret = true;
1698
Corinna Vinschen42020322015-09-10 10:47:35 +02001699 tp->tc_offset.tx_errors = counters->tx_errors;
1700 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1701 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001702 tp->tc_offset.inited = true;
1703
1704 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001705}
1706
Ivan Vecera355423d2009-02-06 21:49:57 -08001707static void rtl8169_get_ethtool_stats(struct net_device *dev,
1708 struct ethtool_stats *stats, u64 *data)
1709{
1710 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001711 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02001712 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08001713
1714 ASSERT_RTNL();
1715
Chun-Hao Line0636232016-07-29 16:37:55 +08001716 pm_runtime_get_noresume(d);
1717
1718 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001719 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08001720
1721 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08001722
Corinna Vinschen42020322015-09-10 10:47:35 +02001723 data[0] = le64_to_cpu(counters->tx_packets);
1724 data[1] = le64_to_cpu(counters->rx_packets);
1725 data[2] = le64_to_cpu(counters->tx_errors);
1726 data[3] = le32_to_cpu(counters->rx_errors);
1727 data[4] = le16_to_cpu(counters->rx_missed);
1728 data[5] = le16_to_cpu(counters->align_errors);
1729 data[6] = le32_to_cpu(counters->tx_one_collision);
1730 data[7] = le32_to_cpu(counters->tx_multi_collision);
1731 data[8] = le64_to_cpu(counters->rx_unicast);
1732 data[9] = le64_to_cpu(counters->rx_broadcast);
1733 data[10] = le32_to_cpu(counters->rx_multicast);
1734 data[11] = le16_to_cpu(counters->tx_aborted);
1735 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08001736}
1737
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001738static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1739{
1740 switch(stringset) {
1741 case ETH_SS_STATS:
1742 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1743 break;
1744 }
1745}
1746
Francois Romieu50970832017-10-27 13:24:49 +03001747/*
1748 * Interrupt coalescing
1749 *
1750 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1751 * > 8169, 8168 and 810x line of chipsets
1752 *
1753 * 8169, 8168, and 8136(810x) serial chipsets support it.
1754 *
1755 * > 2 - the Tx timer unit at gigabit speed
1756 *
1757 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1758 * (0xe0) bit 1 and bit 0.
1759 *
1760 * For 8169
1761 * bit[1:0] \ speed 1000M 100M 10M
1762 * 0 0 320ns 2.56us 40.96us
1763 * 0 1 2.56us 20.48us 327.7us
1764 * 1 0 5.12us 40.96us 655.4us
1765 * 1 1 10.24us 81.92us 1.31ms
1766 *
1767 * For the other
1768 * bit[1:0] \ speed 1000M 100M 10M
1769 * 0 0 5us 2.56us 40.96us
1770 * 0 1 40us 20.48us 327.7us
1771 * 1 0 80us 40.96us 655.4us
1772 * 1 1 160us 81.92us 1.31ms
1773 */
1774
1775/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1776struct rtl_coalesce_scale {
1777 /* Rx / Tx */
1778 u32 nsecs[2];
1779};
1780
1781/* rx/tx scale factors for all CPlusCmd[0:1] cases */
1782struct rtl_coalesce_info {
1783 u32 speed;
1784 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1785};
1786
1787/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1788#define rxtx_x1822(r, t) { \
1789 {{(r), (t)}}, \
1790 {{(r)*8, (t)*8}}, \
1791 {{(r)*8*2, (t)*8*2}}, \
1792 {{(r)*8*2*2, (t)*8*2*2}}, \
1793}
1794static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1795 /* speed delays: rx00 tx00 */
1796 { SPEED_10, rxtx_x1822(40960, 40960) },
1797 { SPEED_100, rxtx_x1822( 2560, 2560) },
1798 { SPEED_1000, rxtx_x1822( 320, 320) },
1799 { 0 },
1800};
1801
1802static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1803 /* speed delays: rx00 tx00 */
1804 { SPEED_10, rxtx_x1822(40960, 40960) },
1805 { SPEED_100, rxtx_x1822( 2560, 2560) },
1806 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1807 { 0 },
1808};
1809#undef rxtx_x1822
1810
1811/* get rx/tx scale vector corresponding to current speed */
1812static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1813{
1814 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001815 const struct rtl_coalesce_info *ci;
Francois Romieu50970832017-10-27 13:24:49 +03001816
Heiner Kallweit20023d32019-06-11 21:09:19 +02001817 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
1818 ci = rtl_coalesce_info_8169;
1819 else
1820 ci = rtl_coalesce_info_8168_8136;
Francois Romieu50970832017-10-27 13:24:49 +03001821
Heiner Kallweit20023d32019-06-11 21:09:19 +02001822 for (; ci->speed; ci++) {
1823 if (tp->phydev->speed == ci->speed)
Francois Romieu50970832017-10-27 13:24:49 +03001824 return ci;
Francois Romieu50970832017-10-27 13:24:49 +03001825 }
1826
1827 return ERR_PTR(-ELNRNG);
1828}
1829
1830static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1831{
1832 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001833 const struct rtl_coalesce_info *ci;
1834 const struct rtl_coalesce_scale *scale;
1835 struct {
1836 u32 *max_frames;
1837 u32 *usecs;
1838 } coal_settings [] = {
1839 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1840 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1841 }, *p = coal_settings;
1842 int i;
1843 u16 w;
1844
1845 memset(ec, 0, sizeof(*ec));
1846
1847 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1848 ci = rtl_coalesce_info(dev);
1849 if (IS_ERR(ci))
1850 return PTR_ERR(ci);
1851
Heiner Kallweit0ae09742018-04-28 22:19:26 +02001852 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03001853
1854 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001855 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03001856 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1857 w >>= RTL_COALESCE_SHIFT;
1858 *p->usecs = w & RTL_COALESCE_MASK;
1859 }
1860
1861 for (i = 0; i < 2; i++) {
1862 p = coal_settings + i;
1863 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1864
1865 /*
1866 * ethtool_coalesce says it is illegal to set both usecs and
1867 * max_frames to 0.
1868 */
1869 if (!*p->usecs && !*p->max_frames)
1870 *p->max_frames = 1;
1871 }
1872
1873 return 0;
1874}
1875
1876/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1877static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1878 struct net_device *dev, u32 nsec, u16 *cp01)
1879{
1880 const struct rtl_coalesce_info *ci;
1881 u16 i;
1882
1883 ci = rtl_coalesce_info(dev);
1884 if (IS_ERR(ci))
1885 return ERR_CAST(ci);
1886
1887 for (i = 0; i < 4; i++) {
1888 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1889 ci->scalev[i].nsecs[1]);
1890 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1891 *cp01 = i;
1892 return &ci->scalev[i];
1893 }
1894 }
1895
1896 return ERR_PTR(-EINVAL);
1897}
1898
1899static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1900{
1901 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001902 const struct rtl_coalesce_scale *scale;
1903 struct {
1904 u32 frames;
1905 u32 usecs;
1906 } coal_settings [] = {
1907 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1908 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1909 }, *p = coal_settings;
1910 u16 w = 0, cp01;
1911 int i;
1912
1913 scale = rtl_coalesce_choose_scale(dev,
1914 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1915 if (IS_ERR(scale))
1916 return PTR_ERR(scale);
1917
1918 for (i = 0; i < 2; i++, p++) {
1919 u32 units;
1920
1921 /*
1922 * accept max_frames=1 we returned in rtl_get_coalesce.
1923 * accept it not only when usecs=0 because of e.g. the following scenario:
1924 *
1925 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
1926 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
1927 * - then user does `ethtool -C eth0 rx-usecs 100`
1928 *
1929 * since ethtool sends to kernel whole ethtool_coalesce
1930 * settings, if we do not handle rx_usecs=!0, rx_frames=1
1931 * we'll reject it below in `frames % 4 != 0`.
1932 */
1933 if (p->frames == 1) {
1934 p->frames = 0;
1935 }
1936
1937 units = p->usecs * 1000 / scale->nsecs[i];
1938 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
1939 return -EINVAL;
1940
1941 w <<= RTL_COALESCE_SHIFT;
1942 w |= units;
1943 w <<= RTL_COALESCE_SHIFT;
1944 w |= p->frames >> 2;
1945 }
1946
1947 rtl_lock_work(tp);
1948
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001949 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03001950
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02001951 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001952 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1953 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03001954
1955 rtl_unlock_work(tp);
1956
1957 return 0;
1958}
1959
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001960static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
1961{
1962 struct rtl8169_private *tp = netdev_priv(dev);
1963 struct device *d = tp_to_dev(tp);
1964 int ret;
1965
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001966 if (!rtl_supports_eee(tp))
1967 return -EOPNOTSUPP;
1968
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001969 pm_runtime_get_noresume(d);
1970
1971 if (!pm_runtime_active(d)) {
1972 ret = -EOPNOTSUPP;
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001973 } else {
1974 ret = phy_ethtool_get_eee(tp->phydev, data);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001975 }
1976
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001977 pm_runtime_put_noidle(d);
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001978
1979 return ret;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001980}
1981
1982static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
1983{
1984 struct rtl8169_private *tp = netdev_priv(dev);
1985 struct device *d = tp_to_dev(tp);
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001986 int ret;
1987
1988 if (!rtl_supports_eee(tp))
1989 return -EOPNOTSUPP;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001990
1991 pm_runtime_get_noresume(d);
1992
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02001993 if (!pm_runtime_active(d)) {
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001994 ret = -EOPNOTSUPP;
1995 goto out;
1996 }
1997
1998 if (dev->phydev->autoneg == AUTONEG_DISABLE ||
1999 dev->phydev->duplex != DUPLEX_FULL) {
2000 ret = -EPROTONOSUPPORT;
2001 goto out;
2002 }
2003
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002004 ret = phy_ethtool_set_eee(tp->phydev, data);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002005out:
2006 pm_runtime_put_noidle(d);
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002007 return ret;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002008}
2009
Jeff Garzik7282d492006-09-13 14:30:00 -04002010static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 .get_drvinfo = rtl8169_get_drvinfo,
2012 .get_regs_len = rtl8169_get_regs_len,
2013 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002014 .get_coalesce = rtl_get_coalesce,
2015 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002016 .get_msglevel = rtl8169_get_msglevel,
2017 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002019 .get_wol = rtl8169_get_wol,
2020 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002021 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002022 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002023 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002024 .get_ts_info = ethtool_op_get_ts_info,
Heiner Kallweitdd849572018-07-17 22:51:48 +02002025 .nway_reset = phy_ethtool_nway_reset,
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002026 .get_eee = rtl8169_get_eee,
2027 .set_eee = rtl8169_set_eee,
Heiner Kallweit45772432018-07-17 22:51:44 +02002028 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2029 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030};
2031
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002032static void rtl_enable_eee(struct rtl8169_private *tp)
2033{
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002034 struct phy_device *phydev = tp->phydev;
2035 int supported = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002036
2037 if (supported > 0)
Heiner Kallweit2e779dd2019-08-15 14:14:18 +02002038 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, supported);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002039}
2040
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002041static void rtl8169_get_mac_version(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Francois Romieu0e485152007-02-20 00:00:26 +01002043 /*
2044 * The driver currently handles the 8168Bf and the 8168Be identically
2045 * but they can be identified more specifically through the test below
2046 * if needed:
2047 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002048 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002049 *
2050 * Same thing for the 8101Eb and the 8101Ec:
2051 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002052 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002053 */
Francois Romieu37441002011-06-17 22:58:54 +02002054 static const struct rtl_mac_info {
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002055 u16 mask;
2056 u16 val;
2057 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002059 /* 8168EP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002060 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
2061 { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
2062 { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002063
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002064 /* 8168H family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002065 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
2066 { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002067
Hayes Wangc5583862012-07-02 17:23:22 +08002068 /* 8168G family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002069 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
2070 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
2071 { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
2072 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
Hayes Wangc5583862012-07-02 17:23:22 +08002073
Hayes Wangc2218922011-09-06 16:55:18 +08002074 /* 8168F family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002075 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
2076 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
2077 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
Hayes Wangc2218922011-09-06 16:55:18 +08002078
hayeswang01dc7fe2011-03-21 01:50:28 +00002079 /* 8168E family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002080 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
2081 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
2082 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002083
Francois Romieu5b538df2008-07-20 16:22:45 +02002084 /* 8168D family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002085 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
2086 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002087
françois romieue6de30d2011-01-03 15:08:37 +00002088 /* 8168DP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002089 { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
2090 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
2091 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002092
Francois Romieuef808d52008-06-29 13:10:54 +02002093 /* 8168C family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002094 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
2095 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
2096 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
2097 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
2098 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
2099 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
2100 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002101
2102 /* 8168B family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002103 { 0x7cf, 0x380, RTL_GIGA_MAC_VER_12 },
2104 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
2105 { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002106
2107 /* 8101 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002108 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
2109 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
2110 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
2111 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
2112 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
2113 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
2114 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
2115 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
2116 { 0x7cf, 0x340, RTL_GIGA_MAC_VER_13 },
2117 { 0x7cf, 0x343, RTL_GIGA_MAC_VER_10 },
2118 { 0x7cf, 0x342, RTL_GIGA_MAC_VER_16 },
2119 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
2120 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
2121 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_16 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002122 /* FIXME: where did these entries come from ? -- FR */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002123 { 0xfc8, 0x388, RTL_GIGA_MAC_VER_15 },
2124 { 0xfc8, 0x308, RTL_GIGA_MAC_VER_14 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002125
2126 /* 8110 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002127 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
2128 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
2129 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
2130 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
2131 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002132
Jean Delvaref21b75e2009-05-26 20:54:48 -07002133 /* Catch-all */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002134 { 0x000, 0x000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002135 };
2136 const struct rtl_mac_info *p = mac_info;
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002137 u16 reg = RTL_R32(tp, TxConfig) >> 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002139 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 p++;
2141 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002142
2143 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002144 dev_err(tp_to_dev(tp), "unknown chip XID %03x\n", reg & 0xfcf);
Heiner Kallweit45f19962018-11-22 22:00:10 +01002145 } else if (!tp->supports_gmii) {
2146 if (tp->mac_version == RTL_GIGA_MAC_VER_42)
2147 tp->mac_version = RTL_GIGA_MAC_VER_43;
2148 else if (tp->mac_version == RTL_GIGA_MAC_VER_45)
2149 tp->mac_version = RTL_GIGA_MAC_VER_47;
2150 else if (tp->mac_version == RTL_GIGA_MAC_VER_46)
2151 tp->mac_version = RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
Francois Romieu867763c2007-08-17 18:21:58 +02002155struct phy_reg {
2156 u16 reg;
2157 u16 val;
2158};
2159
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002160static void __rtl_writephy_batch(struct rtl8169_private *tp,
2161 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002162{
2163 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002164 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002165 regs++;
2166 }
2167}
2168
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002169#define rtl_writephy_batch(tp, a) __rtl_writephy_batch(tp, a, ARRAY_SIZE(a))
2170
françois romieuf1e02ed2011-01-13 13:07:53 +00002171static void rtl_release_firmware(struct rtl8169_private *tp)
2172{
Heiner Kallweit254764e2019-01-22 22:23:41 +01002173 if (tp->rtl_fw) {
Heiner Kallweit47ad5932019-06-03 21:26:31 +02002174 rtl_fw_release_firmware(tp->rtl_fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02002175 kfree(tp->rtl_fw);
Heiner Kallweit254764e2019-01-22 22:23:41 +01002176 tp->rtl_fw = NULL;
Francois Romieub6ffd972011-06-17 17:00:05 +02002177 }
françois romieuf1e02ed2011-01-13 13:07:53 +00002178}
2179
François Romieu953a12c2011-04-24 17:38:48 +02002180static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002181{
Heiner Kallweitce8843a2019-05-29 21:15:06 +02002182 /* TODO: release firmware if rtl_fw_write_firmware signals failure. */
Heiner Kallweit254764e2019-01-22 22:23:41 +01002183 if (tp->rtl_fw)
Heiner Kallweitce8843a2019-05-29 21:15:06 +02002184 rtl_fw_write_firmware(tp, tp->rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002185}
2186
2187static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2188{
2189 if (rtl_readphy(tp, reg) != val)
2190 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2191 else
2192 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002193}
2194
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002195static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
2196{
Heiner Kallweitf4528252019-05-04 17:13:09 +02002197 /* Adjust EEE LED frequency */
2198 if (tp->mac_version != RTL_GIGA_MAC_VER_38)
2199 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
2200
Heiner Kallweite719b3e2019-04-28 11:11:47 +02002201 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_1111, 0x0003);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002202}
2203
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002204static void rtl8168f_config_eee_phy(struct rtl8169_private *tp)
2205{
2206 struct phy_device *phydev = tp->phydev;
2207
2208 phy_write(phydev, 0x1f, 0x0007);
2209 phy_write(phydev, 0x1e, 0x0020);
2210 phy_set_bits(phydev, 0x15, BIT(8));
2211
2212 phy_write(phydev, 0x1f, 0x0005);
2213 phy_write(phydev, 0x05, 0x8b85);
2214 phy_set_bits(phydev, 0x06, BIT(13));
2215
2216 phy_write(phydev, 0x1f, 0x0000);
2217}
2218
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002219static void rtl8168g_config_eee_phy(struct rtl8169_private *tp)
2220{
Heiner Kallweita2928d22019-06-02 10:53:49 +02002221 phy_modify_paged(tp->phydev, 0x0a43, 0x11, 0, BIT(4));
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002222}
2223
Heiner Kallweitb6cef262019-08-15 14:21:30 +02002224static void rtl8168h_config_eee_phy(struct rtl8169_private *tp)
2225{
2226 struct phy_device *phydev = tp->phydev;
2227
2228 rtl8168g_config_eee_phy(tp);
2229
2230 phy_modify_paged(phydev, 0xa4a, 0x11, 0x0000, 0x0200);
2231 phy_modify_paged(phydev, 0xa42, 0x14, 0x0000, 0x0080);
2232}
2233
françois romieu4da19632011-01-03 15:07:55 +00002234static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002236 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002237 { 0x1f, 0x0001 },
2238 { 0x06, 0x006e },
2239 { 0x08, 0x0708 },
2240 { 0x15, 0x4000 },
2241 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
françois romieu0b9b5712009-08-10 19:44:56 +00002243 { 0x1f, 0x0001 },
2244 { 0x03, 0x00a1 },
2245 { 0x02, 0x0008 },
2246 { 0x01, 0x0120 },
2247 { 0x00, 0x1000 },
2248 { 0x04, 0x0800 },
2249 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
françois romieu0b9b5712009-08-10 19:44:56 +00002251 { 0x03, 0xff41 },
2252 { 0x02, 0xdf60 },
2253 { 0x01, 0x0140 },
2254 { 0x00, 0x0077 },
2255 { 0x04, 0x7800 },
2256 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
françois romieu0b9b5712009-08-10 19:44:56 +00002258 { 0x03, 0x802f },
2259 { 0x02, 0x4f02 },
2260 { 0x01, 0x0409 },
2261 { 0x00, 0xf0f9 },
2262 { 0x04, 0x9800 },
2263 { 0x04, 0x9000 },
2264
2265 { 0x03, 0xdf01 },
2266 { 0x02, 0xdf20 },
2267 { 0x01, 0xff95 },
2268 { 0x00, 0xba00 },
2269 { 0x04, 0xa800 },
2270 { 0x04, 0xa000 },
2271
2272 { 0x03, 0xff41 },
2273 { 0x02, 0xdf20 },
2274 { 0x01, 0x0140 },
2275 { 0x00, 0x00bb },
2276 { 0x04, 0xb800 },
2277 { 0x04, 0xb000 },
2278
2279 { 0x03, 0xdf41 },
2280 { 0x02, 0xdc60 },
2281 { 0x01, 0x6340 },
2282 { 0x00, 0x007d },
2283 { 0x04, 0xd800 },
2284 { 0x04, 0xd000 },
2285
2286 { 0x03, 0xdf01 },
2287 { 0x02, 0xdf20 },
2288 { 0x01, 0x100a },
2289 { 0x00, 0xa0ff },
2290 { 0x04, 0xf800 },
2291 { 0x04, 0xf000 },
2292
2293 { 0x1f, 0x0000 },
2294 { 0x0b, 0x0000 },
2295 { 0x00, 0x9200 }
2296 };
2297
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002298 rtl_writephy_batch(tp, phy_reg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
françois romieu4da19632011-01-03 15:07:55 +00002301static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002302{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002303 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002304 { 0x1f, 0x0002 },
2305 { 0x01, 0x90d0 },
2306 { 0x1f, 0x0000 }
2307 };
2308
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002309 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002310}
2311
françois romieu4da19632011-01-03 15:07:55 +00002312static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002313{
2314 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002315
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002316 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2317 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002318 return;
2319
françois romieu4da19632011-01-03 15:07:55 +00002320 rtl_writephy(tp, 0x1f, 0x0001);
2321 rtl_writephy(tp, 0x10, 0xf01b);
2322 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002323}
2324
françois romieu4da19632011-01-03 15:07:55 +00002325static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002326{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002327 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002328 { 0x1f, 0x0001 },
2329 { 0x04, 0x0000 },
2330 { 0x03, 0x00a1 },
2331 { 0x02, 0x0008 },
2332 { 0x01, 0x0120 },
2333 { 0x00, 0x1000 },
2334 { 0x04, 0x0800 },
2335 { 0x04, 0x9000 },
2336 { 0x03, 0x802f },
2337 { 0x02, 0x4f02 },
2338 { 0x01, 0x0409 },
2339 { 0x00, 0xf099 },
2340 { 0x04, 0x9800 },
2341 { 0x04, 0xa000 },
2342 { 0x03, 0xdf01 },
2343 { 0x02, 0xdf20 },
2344 { 0x01, 0xff95 },
2345 { 0x00, 0xba00 },
2346 { 0x04, 0xa800 },
2347 { 0x04, 0xf000 },
2348 { 0x03, 0xdf01 },
2349 { 0x02, 0xdf20 },
2350 { 0x01, 0x101a },
2351 { 0x00, 0xa0ff },
2352 { 0x04, 0xf800 },
2353 { 0x04, 0x0000 },
2354 { 0x1f, 0x0000 },
2355
2356 { 0x1f, 0x0001 },
2357 { 0x10, 0xf41b },
2358 { 0x14, 0xfb54 },
2359 { 0x18, 0xf5c7 },
2360 { 0x1f, 0x0000 },
2361
2362 { 0x1f, 0x0001 },
2363 { 0x17, 0x0cc0 },
2364 { 0x1f, 0x0000 }
2365 };
2366
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002367 rtl_writephy_batch(tp, phy_reg_init);
françois romieu2e9558562009-08-10 19:44:19 +00002368
françois romieu4da19632011-01-03 15:07:55 +00002369 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002370}
2371
françois romieu4da19632011-01-03 15:07:55 +00002372static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002373{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002374 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002375 { 0x1f, 0x0001 },
2376 { 0x04, 0x0000 },
2377 { 0x03, 0x00a1 },
2378 { 0x02, 0x0008 },
2379 { 0x01, 0x0120 },
2380 { 0x00, 0x1000 },
2381 { 0x04, 0x0800 },
2382 { 0x04, 0x9000 },
2383 { 0x03, 0x802f },
2384 { 0x02, 0x4f02 },
2385 { 0x01, 0x0409 },
2386 { 0x00, 0xf099 },
2387 { 0x04, 0x9800 },
2388 { 0x04, 0xa000 },
2389 { 0x03, 0xdf01 },
2390 { 0x02, 0xdf20 },
2391 { 0x01, 0xff95 },
2392 { 0x00, 0xba00 },
2393 { 0x04, 0xa800 },
2394 { 0x04, 0xf000 },
2395 { 0x03, 0xdf01 },
2396 { 0x02, 0xdf20 },
2397 { 0x01, 0x101a },
2398 { 0x00, 0xa0ff },
2399 { 0x04, 0xf800 },
2400 { 0x04, 0x0000 },
2401 { 0x1f, 0x0000 },
2402
2403 { 0x1f, 0x0001 },
2404 { 0x0b, 0x8480 },
2405 { 0x1f, 0x0000 },
2406
2407 { 0x1f, 0x0001 },
2408 { 0x18, 0x67c7 },
2409 { 0x04, 0x2000 },
2410 { 0x03, 0x002f },
2411 { 0x02, 0x4360 },
2412 { 0x01, 0x0109 },
2413 { 0x00, 0x3022 },
2414 { 0x04, 0x2800 },
2415 { 0x1f, 0x0000 },
2416
2417 { 0x1f, 0x0001 },
2418 { 0x17, 0x0cc0 },
2419 { 0x1f, 0x0000 }
2420 };
2421
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002422 rtl_writephy_batch(tp, phy_reg_init);
françois romieu8c7006a2009-08-10 19:43:29 +00002423}
2424
françois romieu4da19632011-01-03 15:07:55 +00002425static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002426{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002427 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002428 { 0x10, 0xf41b },
2429 { 0x1f, 0x0000 }
2430 };
2431
françois romieu4da19632011-01-03 15:07:55 +00002432 rtl_writephy(tp, 0x1f, 0x0001);
2433 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002434
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002435 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002436}
2437
françois romieu4da19632011-01-03 15:07:55 +00002438static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002439{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002440 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002441 { 0x1f, 0x0001 },
2442 { 0x10, 0xf41b },
2443 { 0x1f, 0x0000 }
2444 };
2445
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002446 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002447}
2448
françois romieu4da19632011-01-03 15:07:55 +00002449static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002450{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002451 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002452 { 0x1f, 0x0000 },
2453 { 0x1d, 0x0f00 },
2454 { 0x1f, 0x0002 },
2455 { 0x0c, 0x1ec8 },
2456 { 0x1f, 0x0000 }
2457 };
2458
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002459 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu867763c2007-08-17 18:21:58 +02002460}
2461
françois romieu4da19632011-01-03 15:07:55 +00002462static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002463{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002464 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002465 { 0x1f, 0x0001 },
2466 { 0x1d, 0x3d98 },
2467 { 0x1f, 0x0000 }
2468 };
2469
françois romieu4da19632011-01-03 15:07:55 +00002470 rtl_writephy(tp, 0x1f, 0x0000);
2471 rtl_patchphy(tp, 0x14, 1 << 5);
2472 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002473
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002474 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuef3386f2008-06-29 12:24:30 +02002475}
2476
françois romieu4da19632011-01-03 15:07:55 +00002477static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002478{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002479 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002480 { 0x1f, 0x0001 },
2481 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002482 { 0x1f, 0x0002 },
2483 { 0x00, 0x88d4 },
2484 { 0x01, 0x82b1 },
2485 { 0x03, 0x7002 },
2486 { 0x08, 0x9e30 },
2487 { 0x09, 0x01f0 },
2488 { 0x0a, 0x5500 },
2489 { 0x0c, 0x00c8 },
2490 { 0x1f, 0x0003 },
2491 { 0x12, 0xc096 },
2492 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002493 { 0x1f, 0x0000 },
2494 { 0x1f, 0x0000 },
2495 { 0x09, 0x2000 },
2496 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002497 };
2498
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002499 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002500
françois romieu4da19632011-01-03 15:07:55 +00002501 rtl_patchphy(tp, 0x14, 1 << 5);
2502 rtl_patchphy(tp, 0x0d, 1 << 5);
2503 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002504}
2505
françois romieu4da19632011-01-03 15:07:55 +00002506static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002507{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002508 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002509 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002510 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002511 { 0x03, 0x802f },
2512 { 0x02, 0x4f02 },
2513 { 0x01, 0x0409 },
2514 { 0x00, 0xf099 },
2515 { 0x04, 0x9800 },
2516 { 0x04, 0x9000 },
2517 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002518 { 0x1f, 0x0002 },
2519 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002520 { 0x06, 0x0761 },
2521 { 0x1f, 0x0003 },
2522 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002523 { 0x1f, 0x0000 }
2524 };
2525
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002526 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002527
françois romieu4da19632011-01-03 15:07:55 +00002528 rtl_patchphy(tp, 0x16, 1 << 0);
2529 rtl_patchphy(tp, 0x14, 1 << 5);
2530 rtl_patchphy(tp, 0x0d, 1 << 5);
2531 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002532}
2533
françois romieu4da19632011-01-03 15:07:55 +00002534static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002535{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002536 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002537 { 0x1f, 0x0001 },
2538 { 0x12, 0x2300 },
2539 { 0x1d, 0x3d98 },
2540 { 0x1f, 0x0002 },
2541 { 0x0c, 0x7eb8 },
2542 { 0x06, 0x5461 },
2543 { 0x1f, 0x0003 },
2544 { 0x16, 0x0f0a },
2545 { 0x1f, 0x0000 }
2546 };
2547
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002548 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu197ff762008-06-28 13:16:02 +02002549
françois romieu4da19632011-01-03 15:07:55 +00002550 rtl_patchphy(tp, 0x16, 1 << 0);
2551 rtl_patchphy(tp, 0x14, 1 << 5);
2552 rtl_patchphy(tp, 0x0d, 1 << 5);
2553 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002554}
2555
françois romieu4da19632011-01-03 15:07:55 +00002556static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002557{
françois romieu4da19632011-01-03 15:07:55 +00002558 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002559}
2560
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002561static const struct phy_reg rtl8168d_1_phy_reg_init_0[] = {
2562 /* Channel Estimation */
2563 { 0x1f, 0x0001 },
2564 { 0x06, 0x4064 },
2565 { 0x07, 0x2863 },
2566 { 0x08, 0x059c },
2567 { 0x09, 0x26b4 },
2568 { 0x0a, 0x6a19 },
2569 { 0x0b, 0xdcc8 },
2570 { 0x10, 0xf06d },
2571 { 0x14, 0x7f68 },
2572 { 0x18, 0x7fd9 },
2573 { 0x1c, 0xf0ff },
2574 { 0x1d, 0x3d9c },
2575 { 0x1f, 0x0003 },
2576 { 0x12, 0xf49f },
2577 { 0x13, 0x070b },
2578 { 0x1a, 0x05ad },
2579 { 0x14, 0x94c0 },
2580
2581 /*
2582 * Tx Error Issue
2583 * Enhance line driver power
2584 */
2585 { 0x1f, 0x0002 },
2586 { 0x06, 0x5561 },
2587 { 0x1f, 0x0005 },
2588 { 0x05, 0x8332 },
2589 { 0x06, 0x5561 },
2590
2591 /*
2592 * Can not link to 1Gbps with bad cable
2593 * Decrease SNR threshold form 21.07dB to 19.04dB
2594 */
2595 { 0x1f, 0x0001 },
2596 { 0x17, 0x0cc0 },
2597
2598 { 0x1f, 0x0000 },
2599 { 0x0d, 0xf880 }
2600};
2601
2602static const struct phy_reg rtl8168d_1_phy_reg_init_1[] = {
2603 { 0x1f, 0x0002 },
2604 { 0x05, 0x669a },
2605 { 0x1f, 0x0005 },
2606 { 0x05, 0x8330 },
2607 { 0x06, 0x669a },
2608 { 0x1f, 0x0002 }
2609};
2610
françois romieubca03d52011-01-03 15:07:31 +00002611static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002612{
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002613 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
Francois Romieu5b538df2008-07-20 16:22:45 +02002614
françois romieubca03d52011-01-03 15:07:31 +00002615 /*
2616 * Rx Error Issue
2617 * Fine Tune Switching regulator parameter
2618 */
françois romieu4da19632011-01-03 15:07:55 +00002619 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002620 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2621 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002622
Francois Romieufdf6fc02012-07-06 22:40:38 +02002623 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
françois romieudaf9df62009-10-07 12:44:20 +00002624 int val;
2625
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002626 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
françois romieudaf9df62009-10-07 12:44:20 +00002627
françois romieu4da19632011-01-03 15:07:55 +00002628 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002629
2630 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002631 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002632 0x0065, 0x0066, 0x0067, 0x0068,
2633 0x0069, 0x006a, 0x006b, 0x006c
2634 };
2635 int i;
2636
françois romieu4da19632011-01-03 15:07:55 +00002637 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002638
2639 val &= 0xff00;
2640 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002641 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002642 }
2643 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002644 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002645 { 0x1f, 0x0002 },
2646 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002647 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002648 { 0x05, 0x8330 },
2649 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002650 };
2651
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002652 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02002653 }
2654
françois romieubca03d52011-01-03 15:07:31 +00002655 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002656 rtl_writephy(tp, 0x1f, 0x0002);
2657 rtl_patchphy(tp, 0x0d, 0x0300);
2658 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002659
françois romieubca03d52011-01-03 15:07:31 +00002660 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002661 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002662 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2663 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002664
françois romieu4da19632011-01-03 15:07:55 +00002665 rtl_writephy(tp, 0x1f, 0x0005);
2666 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002667
2668 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002669
françois romieu4da19632011-01-03 15:07:55 +00002670 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002671}
2672
françois romieubca03d52011-01-03 15:07:31 +00002673static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002674{
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002675 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
françois romieudaf9df62009-10-07 12:44:20 +00002676
Francois Romieufdf6fc02012-07-06 22:40:38 +02002677 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
françois romieudaf9df62009-10-07 12:44:20 +00002678 int val;
2679
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002680 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
françois romieudaf9df62009-10-07 12:44:20 +00002681
françois romieu4da19632011-01-03 15:07:55 +00002682 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002683 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002684 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002685 0x0065, 0x0066, 0x0067, 0x0068,
2686 0x0069, 0x006a, 0x006b, 0x006c
2687 };
2688 int i;
2689
françois romieu4da19632011-01-03 15:07:55 +00002690 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002691
2692 val &= 0xff00;
2693 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002694 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002695 }
2696 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002697 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002698 { 0x1f, 0x0002 },
2699 { 0x05, 0x2642 },
2700 { 0x1f, 0x0005 },
2701 { 0x05, 0x8330 },
2702 { 0x06, 0x2642 }
2703 };
2704
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002705 rtl_writephy_batch(tp, phy_reg_init);
françois romieudaf9df62009-10-07 12:44:20 +00002706 }
2707
françois romieubca03d52011-01-03 15:07:31 +00002708 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002709 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002710 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2711 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002712
françois romieubca03d52011-01-03 15:07:31 +00002713 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002714 rtl_writephy(tp, 0x1f, 0x0002);
2715 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002716
françois romieu4da19632011-01-03 15:07:55 +00002717 rtl_writephy(tp, 0x1f, 0x0005);
2718 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002719
2720 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002721
françois romieu4da19632011-01-03 15:07:55 +00002722 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002723}
2724
françois romieu4da19632011-01-03 15:07:55 +00002725static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002726{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002727 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002728 { 0x1f, 0x0002 },
2729 { 0x10, 0x0008 },
2730 { 0x0d, 0x006c },
2731
2732 { 0x1f, 0x0000 },
2733 { 0x0d, 0xf880 },
2734
2735 { 0x1f, 0x0001 },
2736 { 0x17, 0x0cc0 },
2737
2738 { 0x1f, 0x0001 },
2739 { 0x0b, 0xa4d8 },
2740 { 0x09, 0x281c },
2741 { 0x07, 0x2883 },
2742 { 0x0a, 0x6b35 },
2743 { 0x1d, 0x3da4 },
2744 { 0x1c, 0xeffd },
2745 { 0x14, 0x7f52 },
2746 { 0x18, 0x7fc6 },
2747 { 0x08, 0x0601 },
2748 { 0x06, 0x4063 },
2749 { 0x10, 0xf074 },
2750 { 0x1f, 0x0003 },
2751 { 0x13, 0x0789 },
2752 { 0x12, 0xf4bd },
2753 { 0x1a, 0x04fd },
2754 { 0x14, 0x84b0 },
2755 { 0x1f, 0x0000 },
2756 { 0x00, 0x9200 },
2757
2758 { 0x1f, 0x0005 },
2759 { 0x01, 0x0340 },
2760 { 0x1f, 0x0001 },
2761 { 0x04, 0x4000 },
2762 { 0x03, 0x1d21 },
2763 { 0x02, 0x0c32 },
2764 { 0x01, 0x0200 },
2765 { 0x00, 0x5554 },
2766 { 0x04, 0x4800 },
2767 { 0x04, 0x4000 },
2768 { 0x04, 0xf000 },
2769 { 0x03, 0xdf01 },
2770 { 0x02, 0xdf20 },
2771 { 0x01, 0x101a },
2772 { 0x00, 0xa0ff },
2773 { 0x04, 0xf800 },
2774 { 0x04, 0xf000 },
2775 { 0x1f, 0x0000 },
2776
2777 { 0x1f, 0x0007 },
2778 { 0x1e, 0x0023 },
2779 { 0x16, 0x0000 },
2780 { 0x1f, 0x0000 }
2781 };
2782
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002783 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02002784}
2785
françois romieue6de30d2011-01-03 15:08:37 +00002786static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2787{
2788 static const struct phy_reg phy_reg_init[] = {
2789 { 0x1f, 0x0001 },
2790 { 0x17, 0x0cc0 },
2791
2792 { 0x1f, 0x0007 },
2793 { 0x1e, 0x002d },
2794 { 0x18, 0x0040 },
2795 { 0x1f, 0x0000 }
2796 };
2797
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002798 rtl_writephy_batch(tp, phy_reg_init);
françois romieue6de30d2011-01-03 15:08:37 +00002799 rtl_patchphy(tp, 0x0d, 1 << 5);
2800}
2801
Hayes Wang70090422011-07-06 15:58:06 +08002802static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002803{
2804 static const struct phy_reg phy_reg_init[] = {
2805 /* Enable Delay cap */
2806 { 0x1f, 0x0005 },
2807 { 0x05, 0x8b80 },
2808 { 0x06, 0xc896 },
2809 { 0x1f, 0x0000 },
2810
2811 /* Channel estimation fine tune */
2812 { 0x1f, 0x0001 },
2813 { 0x0b, 0x6c20 },
2814 { 0x07, 0x2872 },
2815 { 0x1c, 0xefff },
2816 { 0x1f, 0x0003 },
2817 { 0x14, 0x6420 },
2818 { 0x1f, 0x0000 },
2819
2820 /* Update PFM & 10M TX idle timer */
2821 { 0x1f, 0x0007 },
2822 { 0x1e, 0x002f },
2823 { 0x15, 0x1919 },
2824 { 0x1f, 0x0000 },
2825
2826 { 0x1f, 0x0007 },
2827 { 0x1e, 0x00ac },
2828 { 0x18, 0x0006 },
2829 { 0x1f, 0x0000 }
2830 };
2831
Francois Romieu15ecd032011-04-27 13:52:22 -07002832 rtl_apply_firmware(tp);
2833
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002834 rtl_writephy_batch(tp, phy_reg_init);
hayeswang01dc7fe2011-03-21 01:50:28 +00002835
2836 /* DCO enable for 10M IDLE Power */
2837 rtl_writephy(tp, 0x1f, 0x0007);
2838 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002839 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002840 rtl_writephy(tp, 0x1f, 0x0000);
2841
2842 /* For impedance matching */
2843 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002844 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002845 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002846
2847 /* PHY auto speed down */
2848 rtl_writephy(tp, 0x1f, 0x0007);
2849 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002850 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002851 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002852 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002853
2854 rtl_writephy(tp, 0x1f, 0x0005);
2855 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002856 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002857 rtl_writephy(tp, 0x1f, 0x0000);
2858
2859 rtl_writephy(tp, 0x1f, 0x0005);
2860 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002861 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002862 rtl_writephy(tp, 0x1f, 0x0007);
2863 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002864 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00002865 rtl_writephy(tp, 0x1f, 0x0006);
2866 rtl_writephy(tp, 0x00, 0x5a00);
2867 rtl_writephy(tp, 0x1f, 0x0000);
2868 rtl_writephy(tp, 0x0d, 0x0007);
2869 rtl_writephy(tp, 0x0e, 0x003c);
2870 rtl_writephy(tp, 0x0d, 0x4007);
2871 rtl_writephy(tp, 0x0e, 0x0000);
2872 rtl_writephy(tp, 0x0d, 0x0000);
2873}
2874
françois romieu9ecb9aa2012-12-07 11:20:21 +00002875static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
2876{
2877 const u16 w[] = {
2878 addr[0] | (addr[1] << 8),
2879 addr[2] | (addr[3] << 8),
2880 addr[4] | (addr[5] << 8)
2881 };
françois romieu9ecb9aa2012-12-07 11:20:21 +00002882
Heiner Kallweit3aa4c492019-05-02 20:46:52 +02002883 rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, w[0] | (w[1] << 16));
2884 rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, w[2]);
2885 rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, w[0] << 16);
2886 rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, w[1] | (w[2] << 16));
françois romieu9ecb9aa2012-12-07 11:20:21 +00002887}
2888
Hayes Wang70090422011-07-06 15:58:06 +08002889static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2890{
2891 static const struct phy_reg phy_reg_init[] = {
2892 /* Enable Delay cap */
2893 { 0x1f, 0x0004 },
2894 { 0x1f, 0x0007 },
2895 { 0x1e, 0x00ac },
2896 { 0x18, 0x0006 },
2897 { 0x1f, 0x0002 },
2898 { 0x1f, 0x0000 },
2899 { 0x1f, 0x0000 },
2900
2901 /* Channel estimation fine tune */
2902 { 0x1f, 0x0003 },
2903 { 0x09, 0xa20f },
2904 { 0x1f, 0x0000 },
2905 { 0x1f, 0x0000 },
2906
2907 /* Green Setting */
2908 { 0x1f, 0x0005 },
2909 { 0x05, 0x8b5b },
2910 { 0x06, 0x9222 },
2911 { 0x05, 0x8b6d },
2912 { 0x06, 0x8000 },
2913 { 0x05, 0x8b76 },
2914 { 0x06, 0x8000 },
2915 { 0x1f, 0x0000 }
2916 };
2917
2918 rtl_apply_firmware(tp);
2919
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002920 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang70090422011-07-06 15:58:06 +08002921
2922 /* For 4-corner performance improve */
2923 rtl_writephy(tp, 0x1f, 0x0005);
2924 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002925 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002926 rtl_writephy(tp, 0x1f, 0x0000);
2927
2928 /* PHY auto speed down */
2929 rtl_writephy(tp, 0x1f, 0x0004);
2930 rtl_writephy(tp, 0x1f, 0x0007);
2931 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002932 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002933 rtl_writephy(tp, 0x1f, 0x0002);
2934 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002935 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002936
2937 /* improve 10M EEE waveform */
2938 rtl_writephy(tp, 0x1f, 0x0005);
2939 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002940 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002941 rtl_writephy(tp, 0x1f, 0x0000);
2942
2943 /* Improve 2-pair detection performance */
2944 rtl_writephy(tp, 0x1f, 0x0005);
2945 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002946 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002947 rtl_writephy(tp, 0x1f, 0x0000);
2948
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002949 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01002950 rtl_enable_eee(tp);
Hayes Wang70090422011-07-06 15:58:06 +08002951
2952 /* Green feature */
2953 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01002954 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
2955 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08002956 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01002957 rtl_writephy(tp, 0x1f, 0x0005);
2958 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
2959 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00002960
françois romieu9ecb9aa2012-12-07 11:20:21 +00002961 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
2962 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08002963}
2964
Hayes Wang5f886e02012-03-30 14:33:03 +08002965static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
2966{
2967 /* For 4-corner performance improve */
2968 rtl_writephy(tp, 0x1f, 0x0005);
2969 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002970 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002971 rtl_writephy(tp, 0x1f, 0x0000);
2972
2973 /* PHY auto speed down */
2974 rtl_writephy(tp, 0x1f, 0x0007);
2975 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002976 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002977 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002978 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002979
2980 /* Improve 10M EEE waveform */
2981 rtl_writephy(tp, 0x1f, 0x0005);
2982 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002983 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08002984 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002985
2986 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01002987 rtl_enable_eee(tp);
Hayes Wang5f886e02012-03-30 14:33:03 +08002988}
2989
Hayes Wangc2218922011-09-06 16:55:18 +08002990static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
2991{
2992 static const struct phy_reg phy_reg_init[] = {
2993 /* Channel estimation fine tune */
2994 { 0x1f, 0x0003 },
2995 { 0x09, 0xa20f },
2996 { 0x1f, 0x0000 },
2997
2998 /* Modify green table for giga & fnet */
2999 { 0x1f, 0x0005 },
3000 { 0x05, 0x8b55 },
3001 { 0x06, 0x0000 },
3002 { 0x05, 0x8b5e },
3003 { 0x06, 0x0000 },
3004 { 0x05, 0x8b67 },
3005 { 0x06, 0x0000 },
3006 { 0x05, 0x8b70 },
3007 { 0x06, 0x0000 },
3008 { 0x1f, 0x0000 },
3009 { 0x1f, 0x0007 },
3010 { 0x1e, 0x0078 },
3011 { 0x17, 0x0000 },
3012 { 0x19, 0x00fb },
3013 { 0x1f, 0x0000 },
3014
3015 /* Modify green table for 10M */
3016 { 0x1f, 0x0005 },
3017 { 0x05, 0x8b79 },
3018 { 0x06, 0xaa00 },
3019 { 0x1f, 0x0000 },
3020
3021 /* Disable hiimpedance detection (RTCT) */
3022 { 0x1f, 0x0003 },
3023 { 0x01, 0x328a },
3024 { 0x1f, 0x0000 }
3025 };
3026
3027 rtl_apply_firmware(tp);
3028
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003029 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangc2218922011-09-06 16:55:18 +08003030
Hayes Wang5f886e02012-03-30 14:33:03 +08003031 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003032
3033 /* Improve 2-pair detection performance */
3034 rtl_writephy(tp, 0x1f, 0x0005);
3035 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003036 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003037 rtl_writephy(tp, 0x1f, 0x0000);
3038}
3039
3040static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3041{
3042 rtl_apply_firmware(tp);
3043
Hayes Wang5f886e02012-03-30 14:33:03 +08003044 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003045}
3046
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003047static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3048{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003049 static const struct phy_reg phy_reg_init[] = {
3050 /* Channel estimation fine tune */
3051 { 0x1f, 0x0003 },
3052 { 0x09, 0xa20f },
3053 { 0x1f, 0x0000 },
3054
3055 /* Modify green table for giga & fnet */
3056 { 0x1f, 0x0005 },
3057 { 0x05, 0x8b55 },
3058 { 0x06, 0x0000 },
3059 { 0x05, 0x8b5e },
3060 { 0x06, 0x0000 },
3061 { 0x05, 0x8b67 },
3062 { 0x06, 0x0000 },
3063 { 0x05, 0x8b70 },
3064 { 0x06, 0x0000 },
3065 { 0x1f, 0x0000 },
3066 { 0x1f, 0x0007 },
3067 { 0x1e, 0x0078 },
3068 { 0x17, 0x0000 },
3069 { 0x19, 0x00aa },
3070 { 0x1f, 0x0000 },
3071
3072 /* Modify green table for 10M */
3073 { 0x1f, 0x0005 },
3074 { 0x05, 0x8b79 },
3075 { 0x06, 0xaa00 },
3076 { 0x1f, 0x0000 },
3077
3078 /* Disable hiimpedance detection (RTCT) */
3079 { 0x1f, 0x0003 },
3080 { 0x01, 0x328a },
3081 { 0x1f, 0x0000 }
3082 };
3083
3084
3085 rtl_apply_firmware(tp);
3086
3087 rtl8168f_hw_phy_config(tp);
3088
3089 /* Improve 2-pair detection performance */
3090 rtl_writephy(tp, 0x1f, 0x0005);
3091 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003092 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003093 rtl_writephy(tp, 0x1f, 0x0000);
3094
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003095 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003096
3097 /* Modify green table for giga */
3098 rtl_writephy(tp, 0x1f, 0x0005);
3099 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003100 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003101 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003102 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003103 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003104 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003105 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003106 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003107 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003108 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003109 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003110 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003111 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003112 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003113 rtl_writephy(tp, 0x1f, 0x0000);
3114
3115 /* uc same-seed solution */
3116 rtl_writephy(tp, 0x1f, 0x0005);
3117 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003118 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003119 rtl_writephy(tp, 0x1f, 0x0000);
3120
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003121 /* Green feature */
3122 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003123 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3124 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003125 rtl_writephy(tp, 0x1f, 0x0000);
3126}
3127
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003128static void rtl8168g_disable_aldps(struct rtl8169_private *tp)
3129{
Heiner Kallweita2928d22019-06-02 10:53:49 +02003130 phy_modify_paged(tp->phydev, 0x0a43, 0x10, BIT(2), 0);
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003131}
3132
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003133static void rtl8168g_phy_adjust_10m_aldps(struct rtl8169_private *tp)
3134{
3135 struct phy_device *phydev = tp->phydev;
3136
Heiner Kallweita2928d22019-06-02 10:53:49 +02003137 phy_modify_paged(phydev, 0x0bcc, 0x14, BIT(8), 0);
3138 phy_modify_paged(phydev, 0x0a44, 0x11, 0, BIT(7) | BIT(6));
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003139 phy_write(phydev, 0x1f, 0x0a43);
3140 phy_write(phydev, 0x13, 0x8084);
3141 phy_clear_bits(phydev, 0x14, BIT(14) | BIT(13));
3142 phy_set_bits(phydev, 0x10, BIT(12) | BIT(1) | BIT(0));
3143
3144 phy_write(phydev, 0x1f, 0x0000);
3145}
3146
Hayes Wangc5583862012-07-02 17:23:22 +08003147static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3148{
Heiner Kallweita2928d22019-06-02 10:53:49 +02003149 int ret;
3150
Hayes Wangc5583862012-07-02 17:23:22 +08003151 rtl_apply_firmware(tp);
3152
Heiner Kallweita2928d22019-06-02 10:53:49 +02003153 ret = phy_read_paged(tp->phydev, 0x0a46, 0x10);
3154 if (ret & BIT(8))
3155 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, BIT(15), 0);
3156 else
3157 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, 0, BIT(15));
Hayes Wangc5583862012-07-02 17:23:22 +08003158
Heiner Kallweita2928d22019-06-02 10:53:49 +02003159 ret = phy_read_paged(tp->phydev, 0x0a46, 0x13);
3160 if (ret & BIT(8))
Thomas Voegtle1a03bb52019-07-20 19:01:22 +02003161 phy_modify_paged(tp->phydev, 0x0c41, 0x15, 0, BIT(1));
Heiner Kallweita2928d22019-06-02 10:53:49 +02003162 else
Thomas Voegtle1a03bb52019-07-20 19:01:22 +02003163 phy_modify_paged(tp->phydev, 0x0c41, 0x15, BIT(1), 0);
Hayes Wangc5583862012-07-02 17:23:22 +08003164
hayeswang41f44d12013-04-01 22:23:36 +00003165 /* Enable PHY auto speed down */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003166 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
Hayes Wangc5583862012-07-02 17:23:22 +08003167
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003168 rtl8168g_phy_adjust_10m_aldps(tp);
hayeswangfe7524c2013-04-01 22:23:37 +00003169
hayeswang41f44d12013-04-01 22:23:36 +00003170 /* EEE auto-fallback function */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003171 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
Hayes Wangc5583862012-07-02 17:23:22 +08003172
hayeswang41f44d12013-04-01 22:23:36 +00003173 /* Enable UC LPF tune function */
3174 rtl_writephy(tp, 0x1f, 0x0a43);
3175 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003176 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003177
Heiner Kallweita2928d22019-06-02 10:53:49 +02003178 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
hayeswang41f44d12013-04-01 22:23:36 +00003179
hayeswangfe7524c2013-04-01 22:23:37 +00003180 /* Improve SWR Efficiency */
3181 rtl_writephy(tp, 0x1f, 0x0bcd);
3182 rtl_writephy(tp, 0x14, 0x5065);
3183 rtl_writephy(tp, 0x14, 0xd065);
3184 rtl_writephy(tp, 0x1f, 0x0bc8);
3185 rtl_writephy(tp, 0x11, 0x5655);
3186 rtl_writephy(tp, 0x1f, 0x0bcd);
3187 rtl_writephy(tp, 0x14, 0x1065);
3188 rtl_writephy(tp, 0x14, 0x9065);
3189 rtl_writephy(tp, 0x14, 0x1065);
Heiner Kallweita2928d22019-06-02 10:53:49 +02003190 rtl_writephy(tp, 0x1f, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003191
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003192 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003193 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003194 rtl_enable_eee(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08003195}
3196
hayeswang57538c42013-04-01 22:23:40 +00003197static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3198{
3199 rtl_apply_firmware(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003200 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003201 rtl_enable_eee(tp);
hayeswang57538c42013-04-01 22:23:40 +00003202}
3203
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003204static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3205{
3206 u16 dout_tapbin;
3207 u32 data;
3208
3209 rtl_apply_firmware(tp);
3210
3211 /* CHN EST parameters adjust - giga master */
3212 rtl_writephy(tp, 0x1f, 0x0a43);
3213 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003214 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003215 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003216 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003217 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003218 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003219 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003220 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003221 rtl_writephy(tp, 0x1f, 0x0000);
3222
3223 /* CHN EST parameters adjust - giga slave */
3224 rtl_writephy(tp, 0x1f, 0x0a43);
3225 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003226 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003227 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003228 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003229 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003230 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003231 rtl_writephy(tp, 0x1f, 0x0000);
3232
3233 /* CHN EST parameters adjust - fnet */
3234 rtl_writephy(tp, 0x1f, 0x0a43);
3235 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003236 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003237 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003238 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003239 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003240 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003241 rtl_writephy(tp, 0x1f, 0x0000);
3242
3243 /* enable R-tune & PGA-retune function */
3244 dout_tapbin = 0;
3245 rtl_writephy(tp, 0x1f, 0x0a46);
3246 data = rtl_readphy(tp, 0x13);
3247 data &= 3;
3248 data <<= 2;
3249 dout_tapbin |= data;
3250 data = rtl_readphy(tp, 0x12);
3251 data &= 0xc000;
3252 data >>= 14;
3253 dout_tapbin |= data;
3254 dout_tapbin = ~(dout_tapbin^0x08);
3255 dout_tapbin <<= 12;
3256 dout_tapbin &= 0xf000;
3257 rtl_writephy(tp, 0x1f, 0x0a43);
3258 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003259 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003260 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003261 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003262 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003263 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003264 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003265 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003266
3267 rtl_writephy(tp, 0x1f, 0x0a43);
3268 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003269 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003270 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003271 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003272 rtl_writephy(tp, 0x1f, 0x0000);
3273
3274 /* enable GPHY 10M */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003275 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003276
3277 /* SAR ADC performance */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003278 phy_modify_paged(tp->phydev, 0x0bca, 0x17, BIT(12) | BIT(13), BIT(14));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003279
3280 rtl_writephy(tp, 0x1f, 0x0a43);
3281 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003282 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003283 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003284 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003285 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003286 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003287 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003288 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003289 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003290 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003291 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003292 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003293 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003294 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003295 rtl_writephy(tp, 0x1f, 0x0000);
3296
3297 /* disable phy pfm mode */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003298 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003299
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003300 rtl8168g_disable_aldps(tp);
Heiner Kallweitb6cef262019-08-15 14:21:30 +02003301 rtl8168h_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003302 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003303}
3304
3305static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3306{
3307 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3308 u16 rlen;
3309 u32 data;
3310
3311 rtl_apply_firmware(tp);
3312
3313 /* CHIN EST parameter update */
3314 rtl_writephy(tp, 0x1f, 0x0a43);
3315 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003316 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003317 rtl_writephy(tp, 0x1f, 0x0000);
3318
3319 /* enable R-tune & PGA-retune function */
3320 rtl_writephy(tp, 0x1f, 0x0a43);
3321 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003322 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003323 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003324 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003325 rtl_writephy(tp, 0x1f, 0x0000);
3326
3327 /* enable GPHY 10M */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003328 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003329
3330 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3331 data = r8168_mac_ocp_read(tp, 0xdd02);
3332 ioffset_p3 = ((data & 0x80)>>7);
3333 ioffset_p3 <<= 3;
3334
3335 data = r8168_mac_ocp_read(tp, 0xdd00);
3336 ioffset_p3 |= ((data & (0xe000))>>13);
3337 ioffset_p2 = ((data & (0x1e00))>>9);
3338 ioffset_p1 = ((data & (0x01e0))>>5);
3339 ioffset_p0 = ((data & 0x0010)>>4);
3340 ioffset_p0 <<= 3;
3341 ioffset_p0 |= (data & (0x07));
3342 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3343
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003344 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003345 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003346 rtl_writephy(tp, 0x1f, 0x0bcf);
3347 rtl_writephy(tp, 0x16, data);
3348 rtl_writephy(tp, 0x1f, 0x0000);
3349 }
3350
3351 /* Modify rlen (TX LPF corner frequency) level */
3352 rtl_writephy(tp, 0x1f, 0x0bcd);
3353 data = rtl_readphy(tp, 0x16);
3354 data &= 0x000f;
3355 rlen = 0;
3356 if (data > 3)
3357 rlen = data - 3;
3358 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3359 rtl_writephy(tp, 0x17, data);
3360 rtl_writephy(tp, 0x1f, 0x0bcd);
3361 rtl_writephy(tp, 0x1f, 0x0000);
3362
3363 /* disable phy pfm mode */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003364 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003365
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003366 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003367 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003368 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003369}
3370
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003371static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3372{
3373 /* Enable PHY auto speed down */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003374 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003375
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003376 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003377
3378 /* Enable EEE auto-fallback function */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003379 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003380
3381 /* Enable UC LPF tune function */
3382 rtl_writephy(tp, 0x1f, 0x0a43);
3383 rtl_writephy(tp, 0x13, 0x8012);
3384 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3385 rtl_writephy(tp, 0x1f, 0x0000);
3386
3387 /* set rg_sel_sdm_rate */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003388 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003389
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003390 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003391 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003392 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003393}
3394
3395static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3396{
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003397 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003398
3399 /* Enable UC LPF tune function */
3400 rtl_writephy(tp, 0x1f, 0x0a43);
3401 rtl_writephy(tp, 0x13, 0x8012);
3402 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3403 rtl_writephy(tp, 0x1f, 0x0000);
3404
3405 /* Set rg_sel_sdm_rate */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003406 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003407
3408 /* Channel estimation parameters */
3409 rtl_writephy(tp, 0x1f, 0x0a43);
3410 rtl_writephy(tp, 0x13, 0x80f3);
3411 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3412 rtl_writephy(tp, 0x13, 0x80f0);
3413 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3414 rtl_writephy(tp, 0x13, 0x80ef);
3415 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3416 rtl_writephy(tp, 0x13, 0x80f6);
3417 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3418 rtl_writephy(tp, 0x13, 0x80ec);
3419 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3420 rtl_writephy(tp, 0x13, 0x80ed);
3421 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3422 rtl_writephy(tp, 0x13, 0x80f2);
3423 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3424 rtl_writephy(tp, 0x13, 0x80f4);
3425 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3426 rtl_writephy(tp, 0x1f, 0x0a43);
3427 rtl_writephy(tp, 0x13, 0x8110);
3428 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3429 rtl_writephy(tp, 0x13, 0x810f);
3430 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3431 rtl_writephy(tp, 0x13, 0x8111);
3432 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3433 rtl_writephy(tp, 0x13, 0x8113);
3434 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3435 rtl_writephy(tp, 0x13, 0x8115);
3436 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3437 rtl_writephy(tp, 0x13, 0x810e);
3438 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3439 rtl_writephy(tp, 0x13, 0x810c);
3440 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3441 rtl_writephy(tp, 0x13, 0x810b);
3442 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3443 rtl_writephy(tp, 0x1f, 0x0a43);
3444 rtl_writephy(tp, 0x13, 0x80d1);
3445 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3446 rtl_writephy(tp, 0x13, 0x80cd);
3447 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3448 rtl_writephy(tp, 0x13, 0x80d3);
3449 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3450 rtl_writephy(tp, 0x13, 0x80d5);
3451 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3452 rtl_writephy(tp, 0x13, 0x80d7);
3453 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3454
3455 /* Force PWM-mode */
3456 rtl_writephy(tp, 0x1f, 0x0bcd);
3457 rtl_writephy(tp, 0x14, 0x5065);
3458 rtl_writephy(tp, 0x14, 0xd065);
3459 rtl_writephy(tp, 0x1f, 0x0bc8);
3460 rtl_writephy(tp, 0x12, 0x00ed);
3461 rtl_writephy(tp, 0x1f, 0x0bcd);
3462 rtl_writephy(tp, 0x14, 0x1065);
3463 rtl_writephy(tp, 0x14, 0x9065);
3464 rtl_writephy(tp, 0x14, 0x1065);
3465 rtl_writephy(tp, 0x1f, 0x0000);
3466
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003467 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003468 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003469 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003470}
3471
françois romieu4da19632011-01-03 15:07:55 +00003472static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003473{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003474 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003475 { 0x1f, 0x0003 },
3476 { 0x08, 0x441d },
3477 { 0x01, 0x9100 },
3478 { 0x1f, 0x0000 }
3479 };
3480
françois romieu4da19632011-01-03 15:07:55 +00003481 rtl_writephy(tp, 0x1f, 0x0000);
3482 rtl_patchphy(tp, 0x11, 1 << 12);
3483 rtl_patchphy(tp, 0x19, 1 << 13);
3484 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003485
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003486 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003487}
3488
Hayes Wang5a5e4442011-02-22 17:26:21 +08003489static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3490{
3491 static const struct phy_reg phy_reg_init[] = {
3492 { 0x1f, 0x0005 },
3493 { 0x1a, 0x0000 },
3494 { 0x1f, 0x0000 },
3495
3496 { 0x1f, 0x0004 },
3497 { 0x1c, 0x0000 },
3498 { 0x1f, 0x0000 },
3499
3500 { 0x1f, 0x0001 },
3501 { 0x15, 0x7701 },
3502 { 0x1f, 0x0000 }
3503 };
3504
3505 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003506 rtl_writephy(tp, 0x1f, 0x0000);
3507 rtl_writephy(tp, 0x18, 0x0310);
3508 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003509
François Romieu953a12c2011-04-24 17:38:48 +02003510 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003511
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003512 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003513}
3514
Hayes Wang7e18dca2012-03-30 14:33:02 +08003515static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3516{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003517 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003518 rtl_writephy(tp, 0x1f, 0x0000);
3519 rtl_writephy(tp, 0x18, 0x0310);
3520 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003521
3522 rtl_apply_firmware(tp);
3523
3524 /* EEE setting */
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003525 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003526 rtl_writephy(tp, 0x1f, 0x0004);
3527 rtl_writephy(tp, 0x10, 0x401f);
3528 rtl_writephy(tp, 0x19, 0x7030);
3529 rtl_writephy(tp, 0x1f, 0x0000);
3530}
3531
Hayes Wang5598bfe2012-07-02 17:23:21 +08003532static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3533{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003534 static const struct phy_reg phy_reg_init[] = {
3535 { 0x1f, 0x0004 },
3536 { 0x10, 0xc07f },
3537 { 0x19, 0x7030 },
3538 { 0x1f, 0x0000 }
3539 };
3540
3541 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003542 rtl_writephy(tp, 0x1f, 0x0000);
3543 rtl_writephy(tp, 0x18, 0x0310);
3544 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003545
3546 rtl_apply_firmware(tp);
3547
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003548 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003549 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003550
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003551 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003552}
3553
Francois Romieu5615d9f2007-08-17 17:50:46 +02003554static void rtl_hw_phy_config(struct net_device *dev)
3555{
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003556 static const rtl_generic_fct phy_configs[] = {
3557 /* PCI devices. */
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003558 [RTL_GIGA_MAC_VER_02] = rtl8169s_hw_phy_config,
3559 [RTL_GIGA_MAC_VER_03] = rtl8169s_hw_phy_config,
3560 [RTL_GIGA_MAC_VER_04] = rtl8169sb_hw_phy_config,
3561 [RTL_GIGA_MAC_VER_05] = rtl8169scd_hw_phy_config,
3562 [RTL_GIGA_MAC_VER_06] = rtl8169sce_hw_phy_config,
3563 /* PCI-E devices. */
3564 [RTL_GIGA_MAC_VER_07] = rtl8102e_hw_phy_config,
3565 [RTL_GIGA_MAC_VER_08] = rtl8102e_hw_phy_config,
3566 [RTL_GIGA_MAC_VER_09] = rtl8102e_hw_phy_config,
3567 [RTL_GIGA_MAC_VER_10] = NULL,
3568 [RTL_GIGA_MAC_VER_11] = rtl8168bb_hw_phy_config,
3569 [RTL_GIGA_MAC_VER_12] = rtl8168bef_hw_phy_config,
3570 [RTL_GIGA_MAC_VER_13] = NULL,
3571 [RTL_GIGA_MAC_VER_14] = NULL,
3572 [RTL_GIGA_MAC_VER_15] = NULL,
3573 [RTL_GIGA_MAC_VER_16] = NULL,
3574 [RTL_GIGA_MAC_VER_17] = rtl8168bef_hw_phy_config,
3575 [RTL_GIGA_MAC_VER_18] = rtl8168cp_1_hw_phy_config,
3576 [RTL_GIGA_MAC_VER_19] = rtl8168c_1_hw_phy_config,
3577 [RTL_GIGA_MAC_VER_20] = rtl8168c_2_hw_phy_config,
3578 [RTL_GIGA_MAC_VER_21] = rtl8168c_3_hw_phy_config,
3579 [RTL_GIGA_MAC_VER_22] = rtl8168c_4_hw_phy_config,
3580 [RTL_GIGA_MAC_VER_23] = rtl8168cp_2_hw_phy_config,
3581 [RTL_GIGA_MAC_VER_24] = rtl8168cp_2_hw_phy_config,
3582 [RTL_GIGA_MAC_VER_25] = rtl8168d_1_hw_phy_config,
3583 [RTL_GIGA_MAC_VER_26] = rtl8168d_2_hw_phy_config,
3584 [RTL_GIGA_MAC_VER_27] = rtl8168d_3_hw_phy_config,
3585 [RTL_GIGA_MAC_VER_28] = rtl8168d_4_hw_phy_config,
3586 [RTL_GIGA_MAC_VER_29] = rtl8105e_hw_phy_config,
3587 [RTL_GIGA_MAC_VER_30] = rtl8105e_hw_phy_config,
3588 [RTL_GIGA_MAC_VER_31] = NULL,
3589 [RTL_GIGA_MAC_VER_32] = rtl8168e_1_hw_phy_config,
3590 [RTL_GIGA_MAC_VER_33] = rtl8168e_1_hw_phy_config,
3591 [RTL_GIGA_MAC_VER_34] = rtl8168e_2_hw_phy_config,
3592 [RTL_GIGA_MAC_VER_35] = rtl8168f_1_hw_phy_config,
3593 [RTL_GIGA_MAC_VER_36] = rtl8168f_2_hw_phy_config,
3594 [RTL_GIGA_MAC_VER_37] = rtl8402_hw_phy_config,
3595 [RTL_GIGA_MAC_VER_38] = rtl8411_hw_phy_config,
3596 [RTL_GIGA_MAC_VER_39] = rtl8106e_hw_phy_config,
3597 [RTL_GIGA_MAC_VER_40] = rtl8168g_1_hw_phy_config,
3598 [RTL_GIGA_MAC_VER_41] = NULL,
3599 [RTL_GIGA_MAC_VER_42] = rtl8168g_2_hw_phy_config,
3600 [RTL_GIGA_MAC_VER_43] = rtl8168g_2_hw_phy_config,
3601 [RTL_GIGA_MAC_VER_44] = rtl8168g_2_hw_phy_config,
3602 [RTL_GIGA_MAC_VER_45] = rtl8168h_1_hw_phy_config,
3603 [RTL_GIGA_MAC_VER_46] = rtl8168h_2_hw_phy_config,
3604 [RTL_GIGA_MAC_VER_47] = rtl8168h_1_hw_phy_config,
3605 [RTL_GIGA_MAC_VER_48] = rtl8168h_2_hw_phy_config,
3606 [RTL_GIGA_MAC_VER_49] = rtl8168ep_1_hw_phy_config,
3607 [RTL_GIGA_MAC_VER_50] = rtl8168ep_2_hw_phy_config,
3608 [RTL_GIGA_MAC_VER_51] = rtl8168ep_2_hw_phy_config,
3609 };
Francois Romieu5615d9f2007-08-17 17:50:46 +02003610 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003611
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003612 if (phy_configs[tp->mac_version])
3613 phy_configs[tp->mac_version](tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003614}
3615
Francois Romieuda78dbf2012-01-26 14:18:23 +01003616static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3617{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003618 if (!test_and_set_bit(flag, tp->wk.flags))
3619 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003620}
3621
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003622static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
Francois Romieu5615d9f2007-08-17 17:50:46 +02003624 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003625
Marcus Sundberg773328942008-07-10 21:28:08 +02003626 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit7a67e112018-09-19 22:00:24 +02003627 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3628 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Heiner Kallweit49d17512018-06-28 20:36:15 +02003629 netif_dbg(tp, drv, dev,
3630 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003631 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02003632 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003633
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003634 /* We may have called phy_speed_down before */
Heiner Kallweit703732f2019-01-19 22:07:05 +01003635 phy_speed_up(tp->phydev);
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003636
Heiner Kallweit703732f2019-01-19 22:07:05 +01003637 genphy_soft_reset(tp->phydev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003638}
3639
Francois Romieu773d2022007-01-31 23:47:43 +01003640static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3641{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003642 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003643
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01003644 rtl_unlock_config_regs(tp);
françois romieu908ba2bf2010-04-26 11:42:58 +00003645
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003646 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
3647 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00003648
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003649 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
3650 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00003651
françois romieu9ecb9aa2012-12-07 11:20:21 +00003652 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3653 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00003654
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01003655 rtl_lock_config_regs(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003656
Francois Romieuda78dbf2012-01-26 14:18:23 +01003657 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003658}
3659
3660static int rtl_set_mac_address(struct net_device *dev, void *p)
3661{
3662 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01003663 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01003664 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01003665
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01003666 ret = eth_mac_addr(dev, p);
3667 if (ret)
3668 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01003669
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08003670 pm_runtime_get_noresume(d);
3671
3672 if (pm_runtime_active(d))
3673 rtl_rar_set(tp, dev->dev_addr);
3674
3675 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01003676
3677 return 0;
3678}
3679
Heiner Kallweite3972862018-06-29 08:07:04 +02003680static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003681{
Heiner Kallweit703732f2019-01-19 22:07:05 +01003682 struct rtl8169_private *tp = netdev_priv(dev);
3683
Heiner Kallweit69b3c592018-07-17 22:51:53 +02003684 if (!netif_running(dev))
3685 return -ENODEV;
Heiner Kallweite3972862018-06-29 08:07:04 +02003686
Heiner Kallweit703732f2019-01-19 22:07:05 +01003687 return phy_mii_ioctl(tp->phydev, ifr, cmd);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003688}
3689
David S. Miller1805b2f2011-10-24 18:18:09 -04003690static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3691{
David S. Miller1805b2f2011-10-24 18:18:09 -04003692 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00003693 case RTL_GIGA_MAC_VER_25:
3694 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003695 case RTL_GIGA_MAC_VER_29:
3696 case RTL_GIGA_MAC_VER_30:
3697 case RTL_GIGA_MAC_VER_32:
3698 case RTL_GIGA_MAC_VER_33:
3699 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02003700 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003701 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04003702 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3703 break;
3704 default:
3705 break;
3706 }
3707}
3708
Heiner Kallweit25e94112019-05-29 20:52:03 +02003709static void rtl_pll_power_down(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00003710{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01003711 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003712 return;
3713
hayeswang01dc7fe2011-03-21 01:50:28 +00003714 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3715 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02003716 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00003717
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01003718 if (device_may_wakeup(tp_to_dev(tp))) {
3719 phy_speed_down(tp->phydev, false);
3720 rtl_wol_suspend_quirk(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003721 return;
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01003722 }
françois romieu065c27c2011-01-03 15:08:12 +00003723
françois romieu065c27c2011-01-03 15:08:12 +00003724 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02003725 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003726 case RTL_GIGA_MAC_VER_37:
3727 case RTL_GIGA_MAC_VER_39:
3728 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08003729 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003730 case RTL_GIGA_MAC_VER_45:
3731 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003732 case RTL_GIGA_MAC_VER_47:
3733 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003734 case RTL_GIGA_MAC_VER_50:
3735 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003736 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00003737 break;
hayeswangbeb330a2013-04-01 22:23:39 +00003738 case RTL_GIGA_MAC_VER_40:
3739 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003740 case RTL_GIGA_MAC_VER_49:
Heiner Kallweite719b3e2019-04-28 11:11:47 +02003741 rtl_eri_clear_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003742 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00003743 break;
Heiner Kallweit76719ee2019-05-25 20:45:04 +02003744 default:
3745 break;
françois romieu065c27c2011-01-03 15:08:12 +00003746 }
3747}
3748
Heiner Kallweit25e94112019-05-29 20:52:03 +02003749static void rtl_pll_power_up(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00003750{
françois romieu065c27c2011-01-03 15:08:12 +00003751 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02003752 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003753 case RTL_GIGA_MAC_VER_37:
3754 case RTL_GIGA_MAC_VER_39:
3755 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003756 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00003757 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08003758 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003759 case RTL_GIGA_MAC_VER_45:
3760 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003761 case RTL_GIGA_MAC_VER_47:
3762 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003763 case RTL_GIGA_MAC_VER_50:
3764 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003765 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003766 break;
hayeswangbeb330a2013-04-01 22:23:39 +00003767 case RTL_GIGA_MAC_VER_40:
3768 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003769 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003770 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02003771 rtl_eri_set_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
hayeswangbeb330a2013-04-01 22:23:39 +00003772 break;
Heiner Kallweit76719ee2019-05-25 20:45:04 +02003773 default:
3774 break;
françois romieu065c27c2011-01-03 15:08:12 +00003775 }
3776
Heiner Kallweit703732f2019-01-19 22:07:05 +01003777 phy_resume(tp->phydev);
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02003778 /* give MAC/PHY some time to resume */
3779 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00003780}
3781
Hayes Wange542a222011-07-06 15:58:04 +08003782static void rtl_init_rxcfg(struct rtl8169_private *tp)
3783{
Hayes Wange542a222011-07-06 15:58:04 +08003784 switch (tp->mac_version) {
Heiner Kallweite9588eb2019-05-25 21:14:39 +02003785 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweit2a718832018-05-02 21:39:49 +02003786 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003787 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003788 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02003789 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
Maciej S. Szmigiero511cfd52018-10-11 16:02:10 +02003790 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
3791 case RTL_GIGA_MAC_VER_38:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003792 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003793 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02003794 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003795 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00003796 break;
Hayes Wange542a222011-07-06 15:58:04 +08003797 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003798 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003799 break;
3800 }
3801}
3802
Hayes Wang92fc43b2011-07-06 15:58:03 +08003803static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3804{
Timo Teräs9fba0812013-01-15 21:01:24 +00003805 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003806}
3807
Francois Romieud58d46b2011-05-03 16:38:29 +02003808static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3809{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003810 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3811 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01003812 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003813}
3814
3815static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3816{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003817 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3818 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003819 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003820}
3821
3822static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3823{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003824 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02003825}
3826
3827static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3828{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003829 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02003830}
3831
3832static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3833{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003834 RTL_W8(tp, MaxTxPacketSize, 0x3f);
3835 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3836 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01003837 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003838}
3839
3840static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3841{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003842 RTL_W8(tp, MaxTxPacketSize, 0x0c);
3843 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3844 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003845 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003846}
3847
3848static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3849{
Heiner Kallweitcb732002018-03-20 07:45:35 +01003850 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01003851 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02003852}
3853
3854static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3855{
Heiner Kallweitcb732002018-03-20 07:45:35 +01003856 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003857 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02003858}
3859
3860static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3861{
Francois Romieud58d46b2011-05-03 16:38:29 +02003862 r8168b_0_hw_jumbo_enable(tp);
3863
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003864 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02003865}
3866
3867static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3868{
Francois Romieud58d46b2011-05-03 16:38:29 +02003869 r8168b_0_hw_jumbo_disable(tp);
3870
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003871 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02003872}
3873
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003874static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02003875{
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003876 rtl_unlock_config_regs(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003877 switch (tp->mac_version) {
3878 case RTL_GIGA_MAC_VER_11:
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003879 r8168b_0_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003880 break;
3881 case RTL_GIGA_MAC_VER_12:
3882 case RTL_GIGA_MAC_VER_17:
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003883 r8168b_1_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003884 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003885 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
3886 r8168c_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003887 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003888 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
3889 r8168dp_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003890 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003891 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
3892 r8168e_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003893 break;
Francois Romieud58d46b2011-05-03 16:38:29 +02003894 default:
Francois Romieud58d46b2011-05-03 16:38:29 +02003895 break;
3896 }
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003897 rtl_lock_config_regs(tp);
3898}
3899
3900static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3901{
3902 rtl_unlock_config_regs(tp);
3903 switch (tp->mac_version) {
3904 case RTL_GIGA_MAC_VER_11:
3905 r8168b_0_hw_jumbo_disable(tp);
3906 break;
3907 case RTL_GIGA_MAC_VER_12:
3908 case RTL_GIGA_MAC_VER_17:
3909 r8168b_1_hw_jumbo_disable(tp);
3910 break;
3911 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
3912 r8168c_hw_jumbo_disable(tp);
3913 break;
3914 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
3915 r8168dp_hw_jumbo_disable(tp);
3916 break;
3917 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
3918 r8168e_hw_jumbo_disable(tp);
3919 break;
3920 default:
3921 break;
3922 }
3923 rtl_lock_config_regs(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003924}
3925
Francois Romieuffc46952012-07-06 14:19:23 +02003926DECLARE_RTL_COND(rtl_chipcmd_cond)
3927{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003928 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02003929}
3930
Francois Romieu6f43adc2011-04-29 15:05:51 +02003931static void rtl_hw_reset(struct rtl8169_private *tp)
3932{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003933 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003934
Francois Romieuffc46952012-07-06 14:19:23 +02003935 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003936}
3937
Heiner Kallweit254764e2019-01-22 22:23:41 +01003938static void rtl_request_firmware(struct rtl8169_private *tp)
Francois Romieub6ffd972011-06-17 17:00:05 +02003939{
3940 struct rtl_fw *rtl_fw;
Francois Romieub6ffd972011-06-17 17:00:05 +02003941
Heiner Kallweit254764e2019-01-22 22:23:41 +01003942 /* firmware loaded already or no firmware available */
3943 if (tp->rtl_fw || !tp->fw_name)
3944 return;
Francois Romieub6ffd972011-06-17 17:00:05 +02003945
3946 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
Heiner Kallweit47ad5932019-06-03 21:26:31 +02003947 if (!rtl_fw) {
3948 netif_warn(tp, ifup, tp->dev, "Unable to load firmware, out of memory\n");
3949 return;
3950 }
Francois Romieub6ffd972011-06-17 17:00:05 +02003951
Heiner Kallweitce8843a2019-05-29 21:15:06 +02003952 rtl_fw->phy_write = rtl_writephy;
3953 rtl_fw->phy_read = rtl_readphy;
3954 rtl_fw->mac_mcu_write = mac_mcu_write;
3955 rtl_fw->mac_mcu_read = mac_mcu_read;
Heiner Kallweit4edb00f2019-06-03 21:25:43 +02003956 rtl_fw->fw_name = tp->fw_name;
3957 rtl_fw->dev = tp_to_dev(tp);
Heiner Kallweitce8843a2019-05-29 21:15:06 +02003958
Heiner Kallweit47ad5932019-06-03 21:26:31 +02003959 if (rtl_fw_request_firmware(rtl_fw))
3960 kfree(rtl_fw);
3961 else
3962 tp->rtl_fw = rtl_fw;
François Romieu953a12c2011-04-24 17:38:48 +02003963}
3964
Hayes Wang92fc43b2011-07-06 15:58:03 +08003965static void rtl_rx_close(struct rtl8169_private *tp)
3966{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003967 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003968}
3969
Francois Romieuffc46952012-07-06 14:19:23 +02003970DECLARE_RTL_COND(rtl_npq_cond)
3971{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003972 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02003973}
3974
3975DECLARE_RTL_COND(rtl_txcfg_empty_cond)
3976{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003977 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02003978}
3979
françois romieue6de30d2011-01-03 15:08:37 +00003980static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981{
3982 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00003983 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Hayes Wang92fc43b2011-07-06 15:58:03 +08003985 rtl_rx_close(tp);
3986
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02003987 switch (tp->mac_version) {
3988 case RTL_GIGA_MAC_VER_27:
3989 case RTL_GIGA_MAC_VER_28:
3990 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02003991 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02003992 break;
3993 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
3994 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003995 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02003996 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02003997 break;
3998 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003999 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004000 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004001 break;
françois romieue6de30d2011-01-03 15:08:37 +00004002 }
4003
Hayes Wang92fc43b2011-07-06 15:58:03 +08004004 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005}
4006
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004007static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004008{
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004009 u32 val = TX_DMA_BURST << TxDMAShift |
4010 InterFrameGap << TxInterFrameGapShift;
4011
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02004012 if (rtl_is_8168evl_up(tp))
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004013 val |= TXCFG_AUTO_FIFO;
4014
4015 RTL_W32(tp, TxConfig, val);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004016}
4017
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004018static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004020 /* Low hurts. Let's disable the filtering. */
4021 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004022}
4023
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004024static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004025{
4026 /*
4027 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4028 * register to be written before TxDescAddrLow to work.
4029 * Switching from MMIO to I/O access fixes the issue as well.
4030 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004031 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4032 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4033 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4034 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004035}
4036
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004037static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004038{
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004039 u32 val;
Francois Romieu6dccd162007-02-13 23:38:05 +01004040
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004041 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4042 val = 0x000fff00;
4043 else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
4044 val = 0x00ffff00;
4045 else
4046 return;
4047
4048 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
4049 val |= 0xff;
4050
4051 RTL_W32(tp, 0x7c, val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004052}
4053
Francois Romieue6b763e2012-03-08 09:35:39 +01004054static void rtl_set_rx_mode(struct net_device *dev)
4055{
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004056 u32 rx_mode = AcceptBroadcast | AcceptMyPhys | AcceptMulticast;
4057 /* Multicast hash filter */
4058 u32 mc_filter[2] = { 0xffffffff, 0xffffffff };
Francois Romieue6b763e2012-03-08 09:35:39 +01004059 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004060 u32 tmp;
Francois Romieue6b763e2012-03-08 09:35:39 +01004061
4062 if (dev->flags & IFF_PROMISC) {
4063 /* Unconditionally log net taps. */
4064 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004065 rx_mode |= AcceptAllPhys;
4066 } else if (netdev_mc_count(dev) > MC_FILTER_LIMIT ||
4067 dev->flags & IFF_ALLMULTI ||
4068 tp->mac_version == RTL_GIGA_MAC_VER_35) {
4069 /* accept all multicasts */
4070 } else if (netdev_mc_empty(dev)) {
4071 rx_mode &= ~AcceptMulticast;
Francois Romieue6b763e2012-03-08 09:35:39 +01004072 } else {
4073 struct netdev_hw_addr *ha;
4074
Francois Romieue6b763e2012-03-08 09:35:39 +01004075 mc_filter[1] = mc_filter[0] = 0;
4076 netdev_for_each_mc_addr(ha, dev) {
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004077 u32 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4078 mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31);
4079 }
4080
4081 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4082 tmp = mc_filter[0];
4083 mc_filter[0] = swab32(mc_filter[1]);
4084 mc_filter[1] = swab32(tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004085 }
4086 }
4087
4088 if (dev->features & NETIF_F_RXALL)
4089 rx_mode |= (AcceptErr | AcceptRunt);
4090
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004091 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4092 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004093
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004094 tmp = RTL_R32(tp, RxConfig);
4095 RTL_W32(tp, RxConfig, (tmp & ~RX_CONFIG_ACCEPT_MASK) | rx_mode);
Francois Romieue6b763e2012-03-08 09:35:39 +01004096}
4097
Francois Romieuffc46952012-07-06 14:19:23 +02004098DECLARE_RTL_COND(rtl_csiar_cond)
4099{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004100 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02004101}
4102
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004103static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004104{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004105 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4106
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004107 RTL_W32(tp, CSIDR, value);
4108 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004109 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004110
Francois Romieuffc46952012-07-06 14:19:23 +02004111 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004112}
4113
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004114static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004115{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004116 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4117
4118 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4119 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004120
Francois Romieuffc46952012-07-06 14:19:23 +02004121 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004122 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004123}
4124
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004125static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004126{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004127 struct pci_dev *pdev = tp->pci_dev;
4128 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004129
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004130 /* According to Realtek the value at config space address 0x070f
4131 * controls the L0s/L1 entrance latency. We try standard ECAM access
4132 * first and if it fails fall back to CSI.
4133 */
4134 if (pdev->cfg_size > 0x070f &&
4135 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4136 return;
4137
4138 netdev_notice_once(tp->dev,
4139 "No native access to PCI extended config space, falling back to CSI\n");
4140 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4141 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004142}
4143
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004144static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08004145{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004146 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02004147}
4148
4149struct ephy_info {
4150 unsigned int offset;
4151 u16 mask;
4152 u16 bits;
4153};
4154
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004155static void __rtl_ephy_init(struct rtl8169_private *tp,
4156 const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004157{
4158 u16 w;
4159
4160 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004161 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4162 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004163 e++;
4164 }
4165}
4166
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004167#define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
4168
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004169static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02004170{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004171 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004172 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004173}
4174
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004175static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004176{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004177 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004178 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004179}
4180
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004181static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
hayeswangb51ecea2014-07-09 14:52:51 +08004182{
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004183 /* work around an issue when PCI reset occurs during L2/L3 state */
4184 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
hayeswangb51ecea2014-07-09 14:52:51 +08004185}
4186
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004187static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4188{
Heiner Kallweit62b1b3b2019-06-18 23:14:50 +02004189 /* Don't enable ASPM in the chip if OS can't control ASPM */
4190 if (enable && tp->aspm_manageable) {
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004191 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
Kai-Heng Feng94235462018-09-12 14:58:20 +08004192 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004193 } else {
4194 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4195 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4196 }
Kai-Heng Feng94235462018-09-12 14:58:20 +08004197
4198 udelay(10);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004199}
4200
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004201static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
4202 u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
4203{
4204 /* Usage of dynamic vs. static FIFO is controlled by bit
4205 * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
4206 */
4207 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
4208 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
4209}
4210
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004211static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
4212 u8 low, u8 high)
4213{
4214 /* FIFO thresholds for pause flow control */
4215 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
4216 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
4217}
4218
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004219static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004220{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004221 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004222
françois romieufaf1e782013-02-27 13:01:57 +00004223 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004224 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00004225 PCI_EXP_DEVCTL_NOSNOOP_EN);
4226 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004227}
4228
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004229static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004230{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004231 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004232
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004233 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004234}
4235
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004236static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004237{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004238 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02004239
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004240 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004241
françois romieufaf1e782013-02-27 13:01:57 +00004242 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004243 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02004244
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004245 rtl_disable_clock_request(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004246}
4247
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004248static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004249{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004250 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004251 { 0x01, 0, 0x0001 },
4252 { 0x02, 0x0800, 0x1000 },
4253 { 0x03, 0, 0x0042 },
4254 { 0x06, 0x0080, 0x0000 },
4255 { 0x07, 0, 0x2000 }
4256 };
4257
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004258 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004259
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004260 rtl_ephy_init(tp, e_info_8168cp);
Francois Romieub726e492008-06-28 12:22:59 +02004261
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004262 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004263}
4264
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004265static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004266{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004267 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004268
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004269 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02004270
françois romieufaf1e782013-02-27 13:01:57 +00004271 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004272 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02004273}
4274
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004275static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004276{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004277 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004278
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004279 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004280
4281 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004282 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004283
françois romieufaf1e782013-02-27 13:01:57 +00004284 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004285 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004286}
4287
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004288static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004289{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004290 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004291 { 0x02, 0x0800, 0x1000 },
4292 { 0x03, 0, 0x0002 },
4293 { 0x06, 0x0080, 0x0000 }
4294 };
4295
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004296 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004297
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004298 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02004299
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004300 rtl_ephy_init(tp, e_info_8168c_1);
Francois Romieub726e492008-06-28 12:22:59 +02004301
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004302 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004303}
4304
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004305static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004306{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004307 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004308 { 0x01, 0, 0x0001 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004309 { 0x03, 0x0400, 0x0020 }
Francois Romieub726e492008-06-28 12:22:59 +02004310 };
4311
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004312 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004313
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004314 rtl_ephy_init(tp, e_info_8168c_2);
Francois Romieub726e492008-06-28 12:22:59 +02004315
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004316 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004317}
4318
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004319static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004320{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004321 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004322}
4323
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004324static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004325{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004326 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004327
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004328 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004329}
4330
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004331static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004332{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004333 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004334
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004335 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004336
françois romieufaf1e782013-02-27 13:01:57 +00004337 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004338 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02004339}
4340
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004341static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004342{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004343 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004344
françois romieufaf1e782013-02-27 13:01:57 +00004345 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004346 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00004347
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004348 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004349}
4350
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004351static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004352{
4353 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004354 { 0x0b, 0x0000, 0x0048 },
4355 { 0x19, 0x0020, 0x0050 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004356 { 0x0c, 0x0100, 0x0020 },
4357 { 0x10, 0x0004, 0x0000 },
françois romieue6de30d2011-01-03 15:08:37 +00004358 };
françois romieue6de30d2011-01-03 15:08:37 +00004359
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004360 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004361
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004362 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00004363
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004364 rtl_ephy_init(tp, e_info_8168d_4);
françois romieue6de30d2011-01-03 15:08:37 +00004365
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004366 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004367}
4368
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004369static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004370{
Hayes Wang70090422011-07-06 15:58:06 +08004371 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004372 { 0x00, 0x0200, 0x0100 },
4373 { 0x00, 0x0000, 0x0004 },
4374 { 0x06, 0x0002, 0x0001 },
4375 { 0x06, 0x0000, 0x0030 },
4376 { 0x07, 0x0000, 0x2000 },
4377 { 0x00, 0x0000, 0x0020 },
4378 { 0x03, 0x5800, 0x2000 },
4379 { 0x03, 0x0000, 0x0001 },
4380 { 0x01, 0x0800, 0x1000 },
4381 { 0x07, 0x0000, 0x4000 },
4382 { 0x1e, 0x0000, 0x2000 },
4383 { 0x19, 0xffff, 0xfe6c },
4384 { 0x0a, 0x0000, 0x0040 }
4385 };
4386
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004387 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004388
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004389 rtl_ephy_init(tp, e_info_8168e_1);
hayeswang01dc7fe2011-03-21 01:50:28 +00004390
françois romieufaf1e782013-02-27 13:01:57 +00004391 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004392 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00004393
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004394 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004395
4396 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004397 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4398 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004399
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004400 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004401}
4402
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004403static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004404{
4405 static const struct ephy_info e_info_8168e_2[] = {
4406 { 0x09, 0x0000, 0x0080 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004407 { 0x19, 0x0000, 0x0224 },
4408 { 0x00, 0x0000, 0x0004 },
4409 { 0x0c, 0x3df0, 0x0200 },
Hayes Wang70090422011-07-06 15:58:06 +08004410 };
4411
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004412 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004413
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004414 rtl_ephy_init(tp, e_info_8168e_2);
Hayes Wang70090422011-07-06 15:58:06 +08004415
françois romieufaf1e782013-02-27 13:01:57 +00004416 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004417 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08004418
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004419 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4420 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004421 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004422 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4423 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004424 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004425 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang70090422011-07-06 15:58:06 +08004426
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004427 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004428
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004429 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08004430
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01004431 rtl8168_config_eee_mac(tp);
4432
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004433 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4434 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4435 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02004436
4437 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08004438}
4439
Hayes Wang5f886e02012-03-30 14:33:03 +08004440static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004441{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004442 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004443
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004444 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08004445
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004446 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4447 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004448 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004449 rtl_reset_packet_filter(tp);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004450 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
4451 rtl_eri_set_bits(tp, 0x1d0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004452 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4453 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
Hayes Wangc2218922011-09-06 16:55:18 +08004454
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004455 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004456
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004457 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
4458 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4459 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4460 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01004461
4462 rtl8168_config_eee_mac(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004463}
4464
Hayes Wang5f886e02012-03-30 14:33:03 +08004465static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4466{
Hayes Wang5f886e02012-03-30 14:33:03 +08004467 static const struct ephy_info e_info_8168f_1[] = {
4468 { 0x06, 0x00c0, 0x0020 },
4469 { 0x08, 0x0001, 0x0002 },
4470 { 0x09, 0x0000, 0x0080 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004471 { 0x19, 0x0000, 0x0224 },
4472 { 0x00, 0x0000, 0x0004 },
4473 { 0x0c, 0x3df0, 0x0200 },
Hayes Wang5f886e02012-03-30 14:33:03 +08004474 };
4475
4476 rtl_hw_start_8168f(tp);
4477
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004478 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wang5f886e02012-03-30 14:33:03 +08004479
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004480 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang5f886e02012-03-30 14:33:03 +08004481}
4482
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004483static void rtl_hw_start_8411(struct rtl8169_private *tp)
4484{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004485 static const struct ephy_info e_info_8168f_1[] = {
4486 { 0x06, 0x00c0, 0x0020 },
4487 { 0x0f, 0xffff, 0x5200 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004488 { 0x19, 0x0000, 0x0224 },
4489 { 0x00, 0x0000, 0x0004 },
4490 { 0x0c, 0x3df0, 0x0200 },
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004491 };
4492
4493 rtl_hw_start_8168f(tp);
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004494 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004495
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004496 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004497
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004498 rtl_eri_set_bits(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004499}
4500
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004501static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08004502{
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004503 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004504 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Hayes Wangc5583862012-07-02 17:23:22 +08004505
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004506 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08004507
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004508 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08004509
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004510 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004511 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
Hayes Wangc5583862012-07-02 17:23:22 +08004512
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004513 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08004514
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004515 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4516 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004517
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004518 rtl8168_config_eee_mac(tp);
4519
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004520 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004521 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
hayeswangb51ecea2014-07-09 14:52:51 +08004522
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004523 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08004524}
4525
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004526static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
4527{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004528 static const struct ephy_info e_info_8168g_1[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004529 { 0x00, 0x0008, 0x0000 },
4530 { 0x0c, 0x3ff0, 0x0820 },
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004531 { 0x1e, 0x0000, 0x0001 },
4532 { 0x19, 0x8000, 0x0000 }
4533 };
4534
4535 rtl_hw_start_8168g(tp);
4536
4537 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004538 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004539 rtl_ephy_init(tp, e_info_8168g_1);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004540 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004541}
4542
hayeswang57538c42013-04-01 22:23:40 +00004543static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
4544{
hayeswang57538c42013-04-01 22:23:40 +00004545 static const struct ephy_info e_info_8168g_2[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004546 { 0x00, 0x0008, 0x0000 },
4547 { 0x0c, 0x3ff0, 0x0820 },
4548 { 0x19, 0xffff, 0x7c00 },
4549 { 0x1e, 0xffff, 0x20eb },
4550 { 0x0d, 0xffff, 0x1666 },
4551 { 0x00, 0xffff, 0x10a3 },
4552 { 0x06, 0xffff, 0xf050 },
4553 { 0x04, 0x0000, 0x0010 },
4554 { 0x1d, 0x4000, 0x0000 },
hayeswang57538c42013-04-01 22:23:40 +00004555 };
4556
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004557 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00004558
4559 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004560 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4561 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004562 rtl_ephy_init(tp, e_info_8168g_2);
hayeswang57538c42013-04-01 22:23:40 +00004563}
4564
hayeswang45dd95c2013-07-08 17:09:01 +08004565static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
4566{
hayeswang45dd95c2013-07-08 17:09:01 +08004567 static const struct ephy_info e_info_8411_2[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004568 { 0x00, 0x0008, 0x0000 },
4569 { 0x0c, 0x37d0, 0x0820 },
4570 { 0x1e, 0x0000, 0x0001 },
4571 { 0x19, 0x8021, 0x0000 },
4572 { 0x1e, 0x0000, 0x2000 },
4573 { 0x0d, 0x0100, 0x0200 },
4574 { 0x00, 0x0000, 0x0080 },
4575 { 0x06, 0x0000, 0x0010 },
4576 { 0x04, 0x0000, 0x0010 },
4577 { 0x1d, 0x0000, 0x4000 },
hayeswang45dd95c2013-07-08 17:09:01 +08004578 };
4579
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004580 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08004581
4582 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004583 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004584 rtl_ephy_init(tp, e_info_8411_2);
Heiner Kallweitfe4e8db02019-07-13 13:45:47 +02004585
4586 /* The following Realtek-provided magic fixes an issue with the RX unit
4587 * getting confused after the PHY having been powered-down.
4588 */
4589 r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
4590 r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
4591 r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
4592 r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
4593 r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
4594 r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
4595 r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
4596 r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
4597 mdelay(3);
4598 r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
4599
4600 r8168_mac_ocp_write(tp, 0xF800, 0xE008);
4601 r8168_mac_ocp_write(tp, 0xF802, 0xE00A);
4602 r8168_mac_ocp_write(tp, 0xF804, 0xE00C);
4603 r8168_mac_ocp_write(tp, 0xF806, 0xE00E);
4604 r8168_mac_ocp_write(tp, 0xF808, 0xE027);
4605 r8168_mac_ocp_write(tp, 0xF80A, 0xE04F);
4606 r8168_mac_ocp_write(tp, 0xF80C, 0xE05E);
4607 r8168_mac_ocp_write(tp, 0xF80E, 0xE065);
4608 r8168_mac_ocp_write(tp, 0xF810, 0xC602);
4609 r8168_mac_ocp_write(tp, 0xF812, 0xBE00);
4610 r8168_mac_ocp_write(tp, 0xF814, 0x0000);
4611 r8168_mac_ocp_write(tp, 0xF816, 0xC502);
4612 r8168_mac_ocp_write(tp, 0xF818, 0xBD00);
4613 r8168_mac_ocp_write(tp, 0xF81A, 0x074C);
4614 r8168_mac_ocp_write(tp, 0xF81C, 0xC302);
4615 r8168_mac_ocp_write(tp, 0xF81E, 0xBB00);
4616 r8168_mac_ocp_write(tp, 0xF820, 0x080A);
4617 r8168_mac_ocp_write(tp, 0xF822, 0x6420);
4618 r8168_mac_ocp_write(tp, 0xF824, 0x48C2);
4619 r8168_mac_ocp_write(tp, 0xF826, 0x8C20);
4620 r8168_mac_ocp_write(tp, 0xF828, 0xC516);
4621 r8168_mac_ocp_write(tp, 0xF82A, 0x64A4);
4622 r8168_mac_ocp_write(tp, 0xF82C, 0x49C0);
4623 r8168_mac_ocp_write(tp, 0xF82E, 0xF009);
4624 r8168_mac_ocp_write(tp, 0xF830, 0x74A2);
4625 r8168_mac_ocp_write(tp, 0xF832, 0x8CA5);
4626 r8168_mac_ocp_write(tp, 0xF834, 0x74A0);
4627 r8168_mac_ocp_write(tp, 0xF836, 0xC50E);
4628 r8168_mac_ocp_write(tp, 0xF838, 0x9CA2);
4629 r8168_mac_ocp_write(tp, 0xF83A, 0x1C11);
4630 r8168_mac_ocp_write(tp, 0xF83C, 0x9CA0);
4631 r8168_mac_ocp_write(tp, 0xF83E, 0xE006);
4632 r8168_mac_ocp_write(tp, 0xF840, 0x74F8);
4633 r8168_mac_ocp_write(tp, 0xF842, 0x48C4);
4634 r8168_mac_ocp_write(tp, 0xF844, 0x8CF8);
4635 r8168_mac_ocp_write(tp, 0xF846, 0xC404);
4636 r8168_mac_ocp_write(tp, 0xF848, 0xBC00);
4637 r8168_mac_ocp_write(tp, 0xF84A, 0xC403);
4638 r8168_mac_ocp_write(tp, 0xF84C, 0xBC00);
4639 r8168_mac_ocp_write(tp, 0xF84E, 0x0BF2);
4640 r8168_mac_ocp_write(tp, 0xF850, 0x0C0A);
4641 r8168_mac_ocp_write(tp, 0xF852, 0xE434);
4642 r8168_mac_ocp_write(tp, 0xF854, 0xD3C0);
4643 r8168_mac_ocp_write(tp, 0xF856, 0x49D9);
4644 r8168_mac_ocp_write(tp, 0xF858, 0xF01F);
4645 r8168_mac_ocp_write(tp, 0xF85A, 0xC526);
4646 r8168_mac_ocp_write(tp, 0xF85C, 0x64A5);
4647 r8168_mac_ocp_write(tp, 0xF85E, 0x1400);
4648 r8168_mac_ocp_write(tp, 0xF860, 0xF007);
4649 r8168_mac_ocp_write(tp, 0xF862, 0x0C01);
4650 r8168_mac_ocp_write(tp, 0xF864, 0x8CA5);
4651 r8168_mac_ocp_write(tp, 0xF866, 0x1C15);
4652 r8168_mac_ocp_write(tp, 0xF868, 0xC51B);
4653 r8168_mac_ocp_write(tp, 0xF86A, 0x9CA0);
4654 r8168_mac_ocp_write(tp, 0xF86C, 0xE013);
4655 r8168_mac_ocp_write(tp, 0xF86E, 0xC519);
4656 r8168_mac_ocp_write(tp, 0xF870, 0x74A0);
4657 r8168_mac_ocp_write(tp, 0xF872, 0x48C4);
4658 r8168_mac_ocp_write(tp, 0xF874, 0x8CA0);
4659 r8168_mac_ocp_write(tp, 0xF876, 0xC516);
4660 r8168_mac_ocp_write(tp, 0xF878, 0x74A4);
4661 r8168_mac_ocp_write(tp, 0xF87A, 0x48C8);
4662 r8168_mac_ocp_write(tp, 0xF87C, 0x48CA);
4663 r8168_mac_ocp_write(tp, 0xF87E, 0x9CA4);
4664 r8168_mac_ocp_write(tp, 0xF880, 0xC512);
4665 r8168_mac_ocp_write(tp, 0xF882, 0x1B00);
4666 r8168_mac_ocp_write(tp, 0xF884, 0x9BA0);
4667 r8168_mac_ocp_write(tp, 0xF886, 0x1B1C);
4668 r8168_mac_ocp_write(tp, 0xF888, 0x483F);
4669 r8168_mac_ocp_write(tp, 0xF88A, 0x9BA2);
4670 r8168_mac_ocp_write(tp, 0xF88C, 0x1B04);
4671 r8168_mac_ocp_write(tp, 0xF88E, 0xC508);
4672 r8168_mac_ocp_write(tp, 0xF890, 0x9BA0);
4673 r8168_mac_ocp_write(tp, 0xF892, 0xC505);
4674 r8168_mac_ocp_write(tp, 0xF894, 0xBD00);
4675 r8168_mac_ocp_write(tp, 0xF896, 0xC502);
4676 r8168_mac_ocp_write(tp, 0xF898, 0xBD00);
4677 r8168_mac_ocp_write(tp, 0xF89A, 0x0300);
4678 r8168_mac_ocp_write(tp, 0xF89C, 0x051E);
4679 r8168_mac_ocp_write(tp, 0xF89E, 0xE434);
4680 r8168_mac_ocp_write(tp, 0xF8A0, 0xE018);
4681 r8168_mac_ocp_write(tp, 0xF8A2, 0xE092);
4682 r8168_mac_ocp_write(tp, 0xF8A4, 0xDE20);
4683 r8168_mac_ocp_write(tp, 0xF8A6, 0xD3C0);
4684 r8168_mac_ocp_write(tp, 0xF8A8, 0xC50F);
4685 r8168_mac_ocp_write(tp, 0xF8AA, 0x76A4);
4686 r8168_mac_ocp_write(tp, 0xF8AC, 0x49E3);
4687 r8168_mac_ocp_write(tp, 0xF8AE, 0xF007);
4688 r8168_mac_ocp_write(tp, 0xF8B0, 0x49C0);
4689 r8168_mac_ocp_write(tp, 0xF8B2, 0xF103);
4690 r8168_mac_ocp_write(tp, 0xF8B4, 0xC607);
4691 r8168_mac_ocp_write(tp, 0xF8B6, 0xBE00);
4692 r8168_mac_ocp_write(tp, 0xF8B8, 0xC606);
4693 r8168_mac_ocp_write(tp, 0xF8BA, 0xBE00);
4694 r8168_mac_ocp_write(tp, 0xF8BC, 0xC602);
4695 r8168_mac_ocp_write(tp, 0xF8BE, 0xBE00);
4696 r8168_mac_ocp_write(tp, 0xF8C0, 0x0C4C);
4697 r8168_mac_ocp_write(tp, 0xF8C2, 0x0C28);
4698 r8168_mac_ocp_write(tp, 0xF8C4, 0x0C2C);
4699 r8168_mac_ocp_write(tp, 0xF8C6, 0xDC00);
4700 r8168_mac_ocp_write(tp, 0xF8C8, 0xC707);
4701 r8168_mac_ocp_write(tp, 0xF8CA, 0x1D00);
4702 r8168_mac_ocp_write(tp, 0xF8CC, 0x8DE2);
4703 r8168_mac_ocp_write(tp, 0xF8CE, 0x48C1);
4704 r8168_mac_ocp_write(tp, 0xF8D0, 0xC502);
4705 r8168_mac_ocp_write(tp, 0xF8D2, 0xBD00);
4706 r8168_mac_ocp_write(tp, 0xF8D4, 0x00AA);
4707 r8168_mac_ocp_write(tp, 0xF8D6, 0xE0C0);
4708 r8168_mac_ocp_write(tp, 0xF8D8, 0xC502);
4709 r8168_mac_ocp_write(tp, 0xF8DA, 0xBD00);
4710 r8168_mac_ocp_write(tp, 0xF8DC, 0x0132);
4711
4712 r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
4713
4714 r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
4715 r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
4716 r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
4717 r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
4718 r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
4719 r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
4720 r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
4721
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004722 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08004723}
4724
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004725static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
4726{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004727 static const struct ephy_info e_info_8168h_1[] = {
4728 { 0x1e, 0x0800, 0x0001 },
4729 { 0x1d, 0x0000, 0x0800 },
4730 { 0x05, 0xffff, 0x2089 },
4731 { 0x06, 0xffff, 0x5881 },
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004732 { 0x04, 0xffff, 0x854a },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004733 { 0x01, 0xffff, 0x068b }
4734 };
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004735 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004736
4737 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004738 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004739 rtl_ephy_init(tp, e_info_8168h_1);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004740
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004741 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004742 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004743
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004744 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004745
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004746 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004747
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004748 rtl_reset_packet_filter(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004749
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004750 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_1111, BIT(4));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004751
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004752 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004753
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004754 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004755
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004756 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004757
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004758 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4759 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004760
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004761 rtl8168_config_eee_mac(tp);
4762
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004763 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4764 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004765
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004766 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004767
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004768 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004769
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004770 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004771
4772 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08004773 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004774 rtl_writephy(tp, 0x1f, 0x0000);
4775 if (rg_saw_cnt > 0) {
4776 u16 sw_cnt_1ms_ini;
4777
4778 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
4779 sw_cnt_1ms_ini &= 0x0fff;
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004780 r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004781 }
4782
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004783 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
4784 r8168_mac_ocp_modify(tp, 0xe052, 0x6000, 0x8008);
4785 r8168_mac_ocp_modify(tp, 0xe0d6, 0x01ff, 0x017f);
4786 r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004787
4788 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
4789 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
4790 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
4791 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004792
4793 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004794}
4795
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004796static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
4797{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08004798 rtl8168ep_stop_cmac(tp);
4799
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004800 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004801 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004802
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004803 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004804
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004805 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004806
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004807 rtl_reset_packet_filter(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004808
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004809 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f80);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004810
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004811 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004812
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004813 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004814
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004815 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4816 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004817
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004818 rtl8168_config_eee_mac(tp);
4819
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004820 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004821
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004822 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004823
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004824 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004825}
4826
4827static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
4828{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004829 static const struct ephy_info e_info_8168ep_1[] = {
4830 { 0x00, 0xffff, 0x10ab },
4831 { 0x06, 0xffff, 0xf030 },
4832 { 0x08, 0xffff, 0x2006 },
4833 { 0x0d, 0xffff, 0x1666 },
4834 { 0x0c, 0x3ff0, 0x0000 }
4835 };
4836
4837 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004838 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004839 rtl_ephy_init(tp, e_info_8168ep_1);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004840
4841 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004842
4843 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004844}
4845
4846static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
4847{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004848 static const struct ephy_info e_info_8168ep_2[] = {
4849 { 0x00, 0xffff, 0x10a3 },
4850 { 0x19, 0xffff, 0xfc00 },
4851 { 0x1e, 0xffff, 0x20ea }
4852 };
4853
4854 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004855 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004856 rtl_ephy_init(tp, e_info_8168ep_2);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004857
4858 rtl_hw_start_8168ep(tp);
4859
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004860 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4861 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004862
4863 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004864}
4865
4866static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
4867{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004868 static const struct ephy_info e_info_8168ep_3[] = {
Heiner Kallweita7a92cf2019-08-04 09:52:33 +02004869 { 0x00, 0x0000, 0x0080 },
4870 { 0x0d, 0x0100, 0x0200 },
4871 { 0x19, 0x8021, 0x0000 },
4872 { 0x1e, 0x0000, 0x2000 },
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004873 };
4874
4875 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004876 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004877 rtl_ephy_init(tp, e_info_8168ep_3);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004878
4879 rtl_hw_start_8168ep(tp);
4880
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004881 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4882 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004883
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004884 r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x0271);
4885 r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
4886 r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004887
4888 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004889}
4890
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004891static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004892{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004893 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004894 { 0x01, 0, 0x6e65 },
4895 { 0x02, 0, 0x091f },
4896 { 0x03, 0, 0xc2f9 },
4897 { 0x06, 0, 0xafb5 },
4898 { 0x07, 0, 0x0e00 },
4899 { 0x19, 0, 0xec80 },
4900 { 0x01, 0, 0x2e65 },
4901 { 0x01, 0, 0x6e65 }
4902 };
4903 u8 cfg1;
4904
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004905 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004906
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004907 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004908
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004909 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004910
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004911 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02004912 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004913 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004914
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004915 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004916 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004917 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004918
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004919 rtl_ephy_init(tp, e_info_8102e_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004920}
4921
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004922static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004923{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004924 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004925
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004926 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004927
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004928 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
4929 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004930}
4931
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004932static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004933{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004934 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004935
Francois Romieufdf6fc02012-07-06 22:40:38 +02004936 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004937}
4938
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004939static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08004940{
4941 static const struct ephy_info e_info_8105e_1[] = {
4942 { 0x07, 0, 0x4000 },
4943 { 0x19, 0, 0x0200 },
4944 { 0x19, 0, 0x0020 },
4945 { 0x1e, 0, 0x2000 },
4946 { 0x03, 0, 0x0001 },
4947 { 0x19, 0, 0x0100 },
4948 { 0x19, 0, 0x0004 },
4949 { 0x0a, 0, 0x0020 }
4950 };
4951
Francois Romieucecb5fd2011-04-01 10:21:07 +02004952 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004953 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004954
Francois Romieucecb5fd2011-04-01 10:21:07 +02004955 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004956 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004957
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004958 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
4959 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004960
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004961 rtl_ephy_init(tp, e_info_8105e_1);
hayeswangb51ecea2014-07-09 14:52:51 +08004962
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004963 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004964}
4965
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004966static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08004967{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004968 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02004969 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004970}
4971
Hayes Wang7e18dca2012-03-30 14:33:02 +08004972static void rtl_hw_start_8402(struct rtl8169_private *tp)
4973{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004974 static const struct ephy_info e_info_8402[] = {
4975 { 0x19, 0xffff, 0xff64 },
4976 { 0x1e, 0, 0x4000 }
4977 };
4978
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004979 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004980
4981 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004982 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004983
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004984 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004985
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004986 rtl_ephy_init(tp, e_info_8402);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004987
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004988 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004989
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004990 rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004991 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004992 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4993 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4994 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00);
hayeswangb51ecea2014-07-09 14:52:51 +08004995
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004996 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004997}
4998
Hayes Wang5598bfe2012-07-02 17:23:21 +08004999static void rtl_hw_start_8106(struct rtl8169_private *tp)
5000{
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005001 rtl_hw_aspm_clkreq_enable(tp, false);
5002
Hayes Wang5598bfe2012-07-02 17:23:21 +08005003 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005004 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005005
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005006 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5007 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5008 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005009
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005010 rtl_pcie_state_l2l3_disable(tp);
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005011 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005012}
5013
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005014static void rtl_hw_config(struct rtl8169_private *tp)
5015{
5016 static const rtl_generic_fct hw_configs[] = {
5017 [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
5018 [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
5019 [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
5020 [RTL_GIGA_MAC_VER_10] = NULL,
5021 [RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168bb,
5022 [RTL_GIGA_MAC_VER_12] = rtl_hw_start_8168bef,
5023 [RTL_GIGA_MAC_VER_13] = NULL,
5024 [RTL_GIGA_MAC_VER_14] = NULL,
5025 [RTL_GIGA_MAC_VER_15] = NULL,
5026 [RTL_GIGA_MAC_VER_16] = NULL,
5027 [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168bef,
5028 [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
5029 [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
5030 [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
5031 [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_3,
5032 [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
5033 [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
5034 [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
5035 [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
5036 [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
5037 [RTL_GIGA_MAC_VER_27] = rtl_hw_start_8168d,
5038 [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
5039 [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
5040 [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
5041 [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168dp,
5042 [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
5043 [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
5044 [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
5045 [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
5046 [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
5047 [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
5048 [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
5049 [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
5050 [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
5051 [RTL_GIGA_MAC_VER_41] = rtl_hw_start_8168g_1,
5052 [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
5053 [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
5054 [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
5055 [RTL_GIGA_MAC_VER_45] = rtl_hw_start_8168h_1,
5056 [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
5057 [RTL_GIGA_MAC_VER_47] = rtl_hw_start_8168h_1,
5058 [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
5059 [RTL_GIGA_MAC_VER_49] = rtl_hw_start_8168ep_1,
5060 [RTL_GIGA_MAC_VER_50] = rtl_hw_start_8168ep_2,
5061 [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
5062 };
5063
5064 if (hw_configs[tp->mac_version])
5065 hw_configs[tp->mac_version](tp);
5066}
5067
5068static void rtl_hw_start_8168(struct rtl8169_private *tp)
5069{
Francois Romieucecb5fd2011-04-01 10:21:07 +02005070 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005071 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005072 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005073 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005074
Heiner Kallweit272b2262019-06-14 07:55:21 +02005075 if (rtl_is_8168evl_up(tp))
5076 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5077 else
5078 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005079
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005080 rtl_hw_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081}
5082
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005083static void rtl_hw_start_8169(struct rtl8169_private *tp)
5084{
5085 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
5086 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
5087
5088 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
5089
5090 tp->cp_cmd |= PCIMulRW;
5091
5092 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5093 tp->mac_version == RTL_GIGA_MAC_VER_03) {
5094 netif_dbg(tp, drv, tp->dev,
5095 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
5096 tp->cp_cmd |= (1 << 14);
5097 }
5098
5099 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5100
5101 rtl8169_set_magic_reg(tp, tp->mac_version);
5102
5103 RTL_W32(tp, RxMissed, 0);
5104}
5105
5106static void rtl_hw_start(struct rtl8169_private *tp)
5107{
5108 rtl_unlock_config_regs(tp);
5109
5110 tp->cp_cmd &= CPCMD_MASK;
5111 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5112
5113 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
5114 rtl_hw_start_8169(tp);
5115 else
5116 rtl_hw_start_8168(tp);
5117
5118 rtl_set_rx_max_size(tp);
5119 rtl_set_rx_tx_desc_registers(tp);
5120 rtl_lock_config_regs(tp);
5121
5122 /* disable interrupt coalescing */
5123 RTL_W16(tp, IntrMitigate, 0x0000);
5124 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5125 RTL_R8(tp, IntrMask);
5126 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5127 rtl_init_rxcfg(tp);
5128 rtl_set_tx_config_registers(tp);
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005129 rtl_set_rx_mode(tp->dev);
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005130 rtl_irq_enable(tp);
5131}
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5134{
Francois Romieud58d46b2011-05-03 16:38:29 +02005135 struct rtl8169_private *tp = netdev_priv(dev);
5136
Francois Romieud58d46b2011-05-03 16:38:29 +02005137 if (new_mtu > ETH_DATA_LEN)
5138 rtl_hw_jumbo_enable(tp);
5139 else
5140 rtl_hw_jumbo_disable(tp);
5141
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005143 netdev_update_features(dev);
5144
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146}
5147
5148static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5149{
Al Viro95e09182007-12-22 18:55:39 +00005150 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5152}
5153
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005154static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155{
5156 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5157
Alexander Duycka0750132014-12-11 15:02:17 -08005158 /* Force memory writes to complete before releasing descriptor */
5159 dma_wmb();
5160
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005161 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162}
5163
Heiner Kallweit32879f02019-08-07 21:38:22 +02005164static struct page *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5165 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005166{
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005167 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02005168 int node = dev_to_node(d);
Heiner Kallweit32879f02019-08-07 21:38:22 +02005169 dma_addr_t mapping;
5170 struct page *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
Heiner Kallweit32879f02019-08-07 21:38:22 +02005172 data = alloc_pages_node(node, GFP_KERNEL, get_order(R8169_RX_BUF_SIZE));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005173 if (!data)
5174 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005175
Heiner Kallweit32879f02019-08-07 21:38:22 +02005176 mapping = dma_map_page(d, data, 0, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005177 if (unlikely(dma_mapping_error(d, mapping))) {
5178 if (net_ratelimit())
5179 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Heiner Kallweit32879f02019-08-07 21:38:22 +02005180 __free_pages(data, get_order(R8169_RX_BUF_SIZE));
5181 return NULL;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
Heiner Kallweitd731af72018-04-17 23:26:41 +02005184 desc->addr = cpu_to_le64(mapping);
5185 rtl8169_mark_to_asic(desc);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005186
Heiner Kallweit32879f02019-08-07 21:38:22 +02005187 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188}
5189
5190static void rtl8169_rx_clear(struct rtl8169_private *tp)
5191{
Francois Romieu07d3f512007-02-21 22:40:46 +01005192 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
Heiner Kallweiteb2e7f02019-08-09 22:59:07 +02005194 for (i = 0; i < NUM_RX_DESC && tp->Rx_databuff[i]; i++) {
5195 dma_unmap_page(tp_to_dev(tp),
5196 le64_to_cpu(tp->RxDescArray[i].addr),
5197 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5198 __free_pages(tp->Rx_databuff[i], get_order(R8169_RX_BUF_SIZE));
5199 tp->Rx_databuff[i] = NULL;
5200 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 }
5202}
5203
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005204static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005206 desc->opts1 |= cpu_to_le32(RingEnd);
5207}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005208
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005209static int rtl8169_rx_fill(struct rtl8169_private *tp)
5210{
5211 unsigned int i;
5212
5213 for (i = 0; i < NUM_RX_DESC; i++) {
Heiner Kallweit32879f02019-08-07 21:38:22 +02005214 struct page *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005215
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005216 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005217 if (!data) {
5218 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005219 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005220 }
5221 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005224 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5225 return 0;
5226
5227err_out:
5228 rtl8169_rx_clear(tp);
5229 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230}
5231
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005232static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 rtl8169_init_ring_indexes(tp);
5235
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005236 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5237 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005239 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240}
5241
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005242static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 struct TxDesc *desc)
5244{
5245 unsigned int len = tx_skb->len;
5246
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005247 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5248
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 desc->opts1 = 0x00;
5250 desc->opts2 = 0x00;
5251 desc->addr = 0x00;
5252 tx_skb->len = 0;
5253}
5254
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005255static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5256 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
5258 unsigned int i;
5259
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005260 for (i = 0; i < n; i++) {
5261 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 struct ring_info *tx_skb = tp->tx_skb + entry;
5263 unsigned int len = tx_skb->len;
5264
5265 if (len) {
5266 struct sk_buff *skb = tx_skb->skb;
5267
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005268 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 tp->TxDescArray + entry);
5270 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07005271 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 tx_skb->skb = NULL;
5273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 }
5275 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005276}
5277
5278static void rtl8169_tx_clear(struct rtl8169_private *tp)
5279{
5280 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 tp->cur_tx = tp->dirty_tx = 0;
Florian Westphald92060b2018-10-20 12:25:27 +02005282 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283}
5284
Francois Romieu4422bcd2012-01-26 11:23:32 +01005285static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286{
David Howellsc4028952006-11-22 14:57:56 +00005287 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005288 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
Francois Romieuda78dbf2012-01-26 14:18:23 +01005290 napi_disable(&tp->napi);
5291 netif_stop_queue(dev);
Paul E. McKenney16f11502018-11-05 17:07:39 -08005292 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
françois romieuc7c2c392011-12-04 20:30:52 +00005294 rtl8169_hw_reset(tp);
5295
Francois Romieu56de4142011-03-15 17:29:31 +01005296 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005297 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01005298
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005300 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
Francois Romieuda78dbf2012-01-26 14:18:23 +01005302 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005303 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005304 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305}
5306
5307static void rtl8169_tx_timeout(struct net_device *dev)
5308{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005309 struct rtl8169_private *tp = netdev_priv(dev);
5310
5311 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312}
5313
Heiner Kallweit734c1402018-11-22 21:56:48 +01005314static __le32 rtl8169_get_txd_opts1(u32 opts0, u32 len, unsigned int entry)
5315{
5316 u32 status = opts0 | len;
5317
5318 if (entry == NUM_TX_DESC - 1)
5319 status |= RingEnd;
5320
5321 return cpu_to_le32(status);
5322}
5323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005325 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326{
5327 struct skb_shared_info *info = skb_shinfo(skb);
5328 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005329 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005330 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
5332 entry = tp->cur_tx;
5333 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005334 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005336 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 void *addr;
5338
5339 entry = (entry + 1) % NUM_TX_DESC;
5340
5341 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005342 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005343 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005344 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005345 if (unlikely(dma_mapping_error(d, mapping))) {
5346 if (net_ratelimit())
5347 netif_err(tp, drv, tp->dev,
5348 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005349 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
Heiner Kallweit734c1402018-11-22 21:56:48 +01005352 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005353 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 txd->addr = cpu_to_le64(mapping);
5355
5356 tp->tx_skb[entry].len = len;
5357 }
5358
5359 if (cur_frag) {
5360 tp->tx_skb[entry].skb = skb;
5361 txd->opts1 |= cpu_to_le32(LastFrag);
5362 }
5363
5364 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005365
5366err_out:
5367 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5368 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369}
5370
françois romieub423e9a2013-05-18 01:24:46 +00005371static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5372{
5373 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5374}
5375
hayeswange9746042014-07-11 16:25:58 +08005376/* msdn_giant_send_check()
5377 * According to the document of microsoft, the TCP Pseudo Header excludes the
5378 * packet length for IPv6 TCP large packets.
5379 */
5380static int msdn_giant_send_check(struct sk_buff *skb)
5381{
5382 const struct ipv6hdr *ipv6h;
5383 struct tcphdr *th;
5384 int ret;
5385
5386 ret = skb_cow_head(skb, 0);
5387 if (ret)
5388 return ret;
5389
5390 ipv6h = ipv6_hdr(skb);
5391 th = tcp_hdr(skb);
5392
5393 th->check = 0;
5394 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
5395
5396 return ret;
5397}
5398
Heiner Kallweit87945b62019-05-31 19:55:11 +02005399static void rtl8169_tso_csum_v1(struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400{
Michał Mirosław350fb322011-04-08 06:35:56 +00005401 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
Francois Romieu2b7b4312011-04-18 22:53:24 -07005403 if (mss) {
5404 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08005405 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5406 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5407 const struct iphdr *ip = ip_hdr(skb);
5408
5409 if (ip->protocol == IPPROTO_TCP)
5410 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5411 else if (ip->protocol == IPPROTO_UDP)
5412 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5413 else
5414 WARN_ON_ONCE(1);
5415 }
hayeswang5888d3f2014-07-11 16:25:56 +08005416}
5417
5418static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5419 struct sk_buff *skb, u32 *opts)
5420{
hayeswangbdfa4ed2014-07-11 16:25:57 +08005421 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08005422 u32 mss = skb_shinfo(skb)->gso_size;
5423
5424 if (mss) {
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005425 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005426 case htons(ETH_P_IP):
5427 opts[0] |= TD1_GTSENV4;
5428 break;
5429
5430 case htons(ETH_P_IPV6):
5431 if (msdn_giant_send_check(skb))
5432 return false;
5433
5434 opts[0] |= TD1_GTSENV6;
5435 break;
5436
5437 default:
5438 WARN_ON_ONCE(1);
5439 break;
5440 }
5441
hayeswangbdfa4ed2014-07-11 16:25:57 +08005442 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08005443 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005444 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08005445 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005447 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005448 case htons(ETH_P_IP):
5449 opts[1] |= TD1_IPv4_CS;
5450 ip_protocol = ip_hdr(skb)->protocol;
5451 break;
5452
5453 case htons(ETH_P_IPV6):
5454 opts[1] |= TD1_IPv6_CS;
5455 ip_protocol = ipv6_hdr(skb)->nexthdr;
5456 break;
5457
5458 default:
5459 ip_protocol = IPPROTO_RAW;
5460 break;
5461 }
5462
5463 if (ip_protocol == IPPROTO_TCP)
5464 opts[1] |= TD1_TCP_CS;
5465 else if (ip_protocol == IPPROTO_UDP)
5466 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005467 else
5468 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08005469
5470 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00005471 } else {
5472 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08005473 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 }
hayeswang5888d3f2014-07-11 16:25:56 +08005475
françois romieub423e9a2013-05-18 01:24:46 +00005476 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477}
5478
Heiner Kallweit76085c92018-11-22 22:03:08 +01005479static bool rtl_tx_slots_avail(struct rtl8169_private *tp,
5480 unsigned int nr_frags)
5481{
5482 unsigned int slots_avail = tp->dirty_tx + NUM_TX_DESC - tp->cur_tx;
5483
5484 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
5485 return slots_avail > nr_frags;
5486}
5487
Heiner Kallweit87945b62019-05-31 19:55:11 +02005488/* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
5489static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
5490{
5491 switch (tp->mac_version) {
5492 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
5493 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
5494 return false;
5495 default:
5496 return true;
5497 }
5498}
5499
Stephen Hemminger613573252009-08-31 19:50:58 +00005500static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5501 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502{
5503 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005504 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005506 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005508 u32 opts[2], len;
Heiner Kallweitef143582019-07-28 11:25:19 +02005509 bool stop_queue;
5510 bool door_bell;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005511 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005512
Heiner Kallweit76085c92018-11-22 22:03:08 +01005513 if (unlikely(!rtl_tx_slots_avail(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005514 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005515 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 }
5517
5518 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005519 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Heiner Kallweit355f9482019-06-06 07:49:17 +02005521 opts[1] = rtl8169_tx_vlan_tag(skb);
françois romieub423e9a2013-05-18 01:24:46 +00005522 opts[0] = DescOwn;
5523
Heiner Kallweit87945b62019-05-31 19:55:11 +02005524 if (rtl_chip_supports_csum_v2(tp)) {
Heiner Kallweit96ea7722019-07-26 21:50:34 +02005525 if (!rtl8169_tso_csum_v2(tp, skb, opts))
5526 goto err_dma_0;
Heiner Kallweit87945b62019-05-31 19:55:11 +02005527 } else {
5528 rtl8169_tso_csum_v1(skb, opts);
hayeswange9746042014-07-11 16:25:58 +08005529 }
françois romieub423e9a2013-05-18 01:24:46 +00005530
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005531 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005532 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005533 if (unlikely(dma_mapping_error(d, mapping))) {
5534 if (net_ratelimit())
5535 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005536 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
5539 tp->tx_skb[entry].len = len;
5540 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541
Francois Romieu2b7b4312011-04-18 22:53:24 -07005542 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005543 if (frags < 0)
5544 goto err_dma_1;
5545 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005546 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005547 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005548 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005549 tp->tx_skb[entry].skb = skb;
5550 }
5551
Francois Romieu2b7b4312011-04-18 22:53:24 -07005552 txd->opts2 = cpu_to_le32(opts[1]);
5553
Richard Cochran5047fb52012-03-10 07:29:42 +00005554 skb_tx_timestamp(skb);
5555
Alexander Duycka0750132014-12-11 15:02:17 -08005556 /* Force memory writes to complete before releasing descriptor */
5557 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
Heiner Kallweitef143582019-07-28 11:25:19 +02005559 door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more());
5560
Heiner Kallweit734c1402018-11-22 21:56:48 +01005561 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562
Alexander Duycka0750132014-12-11 15:02:17 -08005563 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00005564 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Alexander Duycka0750132014-12-11 15:02:17 -08005566 tp->cur_tx += frags + 1;
5567
Heiner Kallweitef143582019-07-28 11:25:19 +02005568 stop_queue = !rtl_tx_slots_avail(tp, MAX_SKB_FRAGS);
5569 if (unlikely(stop_queue)) {
Heiner Kallweit0255d592019-02-10 15:28:04 +01005570 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5571 * not miss a ring update when it notices a stopped queue.
5572 */
5573 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 netif_stop_queue(dev);
Heiner Kallweit4773f9b2019-08-12 20:47:40 +02005575 door_bell = true;
Heiner Kallweitef143582019-07-28 11:25:19 +02005576 }
5577
5578 if (door_bell)
5579 RTL_W8(tp, TxPoll, NPQ);
5580
5581 if (unlikely(stop_queue)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005582 /* Sync with rtl_tx:
5583 * - publish queue status and cur_tx ring index (write barrier)
5584 * - refresh dirty_tx ring index (read barrier).
5585 * May the current thread have a pessimistic view of the ring
5586 * status and forget to wake up queue, a racing rtl_tx thread
5587 * can't.
5588 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005589 smp_mb();
Heiner Kallweit76085c92018-11-22 22:03:08 +01005590 if (rtl_tx_slots_avail(tp, MAX_SKB_FRAGS))
Heiner Kallweit601ed4d2019-03-21 21:41:48 +01005591 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 }
5593
Stephen Hemminger613573252009-08-31 19:50:58 +00005594 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005596err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005597 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005598err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07005599 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005600 dev->stats.tx_dropped++;
5601 return NETDEV_TX_OK;
5602
5603err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005605 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005606 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607}
5608
Heiner Kallweite64e0c82019-07-26 21:49:22 +02005609static netdev_features_t rtl8169_features_check(struct sk_buff *skb,
5610 struct net_device *dev,
5611 netdev_features_t features)
5612{
5613 int transport_offset = skb_transport_offset(skb);
5614 struct rtl8169_private *tp = netdev_priv(dev);
5615
5616 if (skb_is_gso(skb)) {
5617 if (transport_offset > GTTCPHO_MAX &&
5618 rtl_chip_supports_csum_v2(tp))
5619 features &= ~NETIF_F_ALL_TSO;
5620 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5621 if (skb->len < ETH_ZLEN) {
5622 switch (tp->mac_version) {
5623 case RTL_GIGA_MAC_VER_11:
5624 case RTL_GIGA_MAC_VER_12:
5625 case RTL_GIGA_MAC_VER_17:
5626 case RTL_GIGA_MAC_VER_34:
5627 features &= ~NETIF_F_CSUM_MASK;
5628 break;
5629 default:
5630 break;
5631 }
5632 }
5633
5634 if (transport_offset > TCPHO_MAX &&
5635 rtl_chip_supports_csum_v2(tp))
5636 features &= ~NETIF_F_CSUM_MASK;
5637 }
5638
5639 return vlan_features_check(skb, features);
5640}
5641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642static void rtl8169_pcierr_interrupt(struct net_device *dev)
5643{
5644 struct rtl8169_private *tp = netdev_priv(dev);
5645 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 u16 pci_status, pci_cmd;
5647
5648 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5649 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5650
Joe Perchesbf82c182010-02-09 11:49:50 +00005651 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5652 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
5654 /*
5655 * The recovery sequence below admits a very elaborated explanation:
5656 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005657 * - I did not see what else could be done;
5658 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 *
5660 * Feel free to adjust to your needs.
5661 */
Francois Romieua27993f2006-12-18 00:04:19 +01005662 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005663 pci_cmd &= ~PCI_COMMAND_PARITY;
5664 else
5665 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5666
5667 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
5669 pci_write_config_word(pdev, PCI_STATUS,
5670 pci_status & (PCI_STATUS_DETECTED_PARITY |
5671 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5672 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5673
Francois Romieu98ddf982012-01-31 10:47:34 +01005674 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675}
5676
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005677static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
5678 int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679{
Florian Westphald92060b2018-10-20 12:25:27 +02005680 unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 dirty_tx = tp->dirty_tx;
5683 smp_rmb();
5684 tx_left = tp->cur_tx - dirty_tx;
5685
5686 while (tx_left > 0) {
5687 unsigned int entry = dirty_tx % NUM_TX_DESC;
5688 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 u32 status;
5690
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5692 if (status & DescOwn)
5693 break;
5694
Alexander Duycka0750132014-12-11 15:02:17 -08005695 /* This barrier is needed to keep us from reading
5696 * any other fields out of the Tx descriptor until
5697 * we know the status of DescOwn
5698 */
5699 dma_rmb();
5700
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005701 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005702 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 if (status & LastFrag) {
Florian Westphald92060b2018-10-20 12:25:27 +02005704 pkts_compl++;
5705 bytes_compl += tx_skb->skb->len;
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005706 napi_consume_skb(tx_skb->skb, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 tx_skb->skb = NULL;
5708 }
5709 dirty_tx++;
5710 tx_left--;
5711 }
5712
5713 if (tp->dirty_tx != dirty_tx) {
Florian Westphald92060b2018-10-20 12:25:27 +02005714 netdev_completed_queue(dev, pkts_compl, bytes_compl);
5715
5716 u64_stats_update_begin(&tp->tx_stats.syncp);
5717 tp->tx_stats.packets += pkts_compl;
5718 tp->tx_stats.bytes += bytes_compl;
5719 u64_stats_update_end(&tp->tx_stats.syncp);
5720
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005722 /* Sync with rtl8169_start_xmit:
5723 * - publish dirty_tx ring index (write barrier)
5724 * - refresh cur_tx ring index and queue status (read barrier)
5725 * May the current thread miss the stopped queue condition,
5726 * a racing xmit thread can only have a right view of the
5727 * ring status.
5728 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005729 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 if (netif_queue_stopped(dev) &&
Heiner Kallweit76085c92018-11-22 22:03:08 +01005731 rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 netif_wake_queue(dev);
5733 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005734 /*
5735 * 8168 hack: TxPoll requests are lost when the Tx packets are
5736 * too close. Let's kick an extra TxPoll request when a burst
5737 * of start_xmit activity is detected (if it is not detected,
5738 * it is slow enough). -- FR
5739 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005740 if (tp->cur_tx != dirty_tx)
5741 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 }
5743}
5744
Francois Romieu126fa4b2005-05-12 20:09:17 -04005745static inline int rtl8169_fragmented_frame(u32 status)
5746{
5747 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5748}
5749
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005750static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 u32 status = opts1 & RxProtoMask;
5753
5754 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005755 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 skb->ip_summed = CHECKSUM_UNNECESSARY;
5757 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005758 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759}
5760
Francois Romieuda78dbf2012-01-26 14:18:23 +01005761static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762{
5763 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005764 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
Timo Teräs9fba0812013-01-15 21:01:24 +00005768 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 unsigned int entry = cur_rx % NUM_RX_DESC;
Heiner Kallweit32879f02019-08-07 21:38:22 +02005770 const void *rx_buf = page_address(tp->Rx_databuff[entry]);
Francois Romieu126fa4b2005-05-12 20:09:17 -04005771 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 u32 status;
5773
Heiner Kallweit62028062018-04-17 23:30:29 +02005774 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 if (status & DescOwn)
5776 break;
Alexander Duycka0750132014-12-11 15:02:17 -08005777
5778 /* This barrier is needed to keep us from reading
5779 * any other fields out of the Rx descriptor until
5780 * we know the status of DescOwn
5781 */
5782 dma_rmb();
5783
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005784 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005785 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5786 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005787 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005789 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005791 dev->stats.rx_crc_errors++;
Heiner Kallweite9588eb2019-05-25 21:14:39 +02005792 if (status & (RxRUNT | RxCRC) && !(status & RxRWT) &&
5793 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00005794 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02005795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 } else {
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005797 unsigned int pkt_size;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005798 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005799
5800process_pkt:
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005801 pkt_size = status & GENMASK(13, 0);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005802 if (likely(!(dev->features & NETIF_F_RXFCS)))
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005803 pkt_size -= ETH_FCS_LEN;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005804 /*
5805 * The driver does not support incoming fragmented
5806 * frames. They are seen as a symptom of over-mtu
5807 * sized frames.
5808 */
5809 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005810 dev->stats.rx_dropped++;
5811 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00005812 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005813 }
5814
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005815 skb = napi_alloc_skb(&tp->napi, pkt_size);
5816 if (unlikely(!skb)) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005817 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00005818 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 }
5820
Heiner Kallweit3c95e502019-08-26 22:52:36 +02005821 dma_sync_single_for_cpu(tp_to_dev(tp),
5822 le64_to_cpu(desc->addr),
5823 pkt_size, DMA_FROM_DEVICE);
Heiner Kallweit32879f02019-08-07 21:38:22 +02005824 prefetch(rx_buf);
5825 skb_copy_to_linear_data(skb, rx_buf, pkt_size);
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005826 skb->tail += pkt_size;
5827 skb->len = pkt_size;
5828
Heiner Kallweitd4ed7462019-08-23 20:07:26 +02005829 dma_sync_single_for_device(tp_to_dev(tp),
5830 le64_to_cpu(desc->addr),
5831 pkt_size, DMA_FROM_DEVICE);
5832
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005833 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 skb->protocol = eth_type_trans(skb, dev);
5835
Francois Romieu7a8fc772011-03-01 17:18:33 +01005836 rtl8169_rx_vlan_tag(desc, skb);
5837
françois romieu39174292015-11-11 23:35:18 +01005838 if (skb->pkt_type == PACKET_MULTICAST)
5839 dev->stats.multicast++;
5840
Heiner Kallweit448a2412019-04-03 19:54:12 +02005841 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
Junchang Wang8027aa22012-03-04 23:30:32 +01005843 u64_stats_update_begin(&tp->rx_stats.syncp);
5844 tp->rx_stats.packets++;
5845 tp->rx_stats.bytes += pkt_size;
5846 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 }
françois romieuce11ff52013-01-24 13:30:06 +00005848release_descriptor:
5849 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005850 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 }
5852
5853 count = cur_rx - tp->cur_rx;
5854 tp->cur_rx = cur_rx;
5855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 return count;
5857}
5858
Francois Romieu07d3f512007-02-21 22:40:46 +01005859static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02005861 struct rtl8169_private *tp = dev_instance;
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02005862 u32 status = rtl_get_events(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
Heiner Kallweitc1d532d2019-08-28 22:24:13 +02005864 if (!tp->irq_enabled || (status & 0xffff) == 0xffff ||
5865 !(status & tp->irq_mask))
Heiner Kallweit05bbe552018-08-10 22:38:29 +02005866 return IRQ_NONE;
françois romieu811fd302011-12-04 20:30:45 +00005867
Heiner Kallweit38caff52018-10-18 22:19:28 +02005868 if (unlikely(status & SYSErr)) {
5869 rtl8169_pcierr_interrupt(tp->dev);
5870 goto out;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005871 }
5872
Heiner Kallweit703732f2019-01-19 22:07:05 +01005873 if (status & LinkChg)
5874 phy_mac_interrupt(tp->phydev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005875
Heiner Kallweit38caff52018-10-18 22:19:28 +02005876 if (unlikely(status & RxFIFOOver &&
5877 tp->mac_version == RTL_GIGA_MAC_VER_11)) {
5878 netif_stop_queue(tp->dev);
5879 /* XXX - Hack alert. See rtl_task(). */
5880 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
5881 }
5882
Heiner Kallweite62b2fd2019-04-14 11:48:39 +02005883 rtl_irq_disable(tp);
5884 napi_schedule_irqoff(&tp->napi);
Heiner Kallweit38caff52018-10-18 22:19:28 +02005885out:
5886 rtl_ack_events(tp, status);
5887
5888 return IRQ_HANDLED;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005889}
5890
Francois Romieu4422bcd2012-01-26 11:23:32 +01005891static void rtl_task(struct work_struct *work)
5892{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005893 static const struct {
5894 int bitnr;
5895 void (*action)(struct rtl8169_private *);
5896 } rtl_work[] = {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005897 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01005898 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005899 struct rtl8169_private *tp =
5900 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005901 struct net_device *dev = tp->dev;
5902 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005903
Francois Romieuda78dbf2012-01-26 14:18:23 +01005904 rtl_lock_work(tp);
5905
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005906 if (!netif_running(dev) ||
5907 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005908 goto out_unlock;
5909
5910 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5911 bool pending;
5912
Francois Romieuda78dbf2012-01-26 14:18:23 +01005913 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005914 if (pending)
5915 rtl_work[i].action(tp);
5916 }
5917
5918out_unlock:
5919 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005920}
5921
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005922static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005924 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5925 struct net_device *dev = tp->dev;
Heiner Kallweit6b839b62018-10-18 19:56:01 +02005926 int work_done;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005927
Heiner Kallweit6b839b62018-10-18 19:56:01 +02005928 work_done = rtl_rx(dev, tp, (u32) budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005929
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005930 rtl_tx(dev, tp, budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005931
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005932 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08005933 napi_complete_done(napi, work_done);
Heiner Kallweitfe716f82018-11-19 22:31:32 +01005934 rtl_irq_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 }
5936
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005937 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005940static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02005941{
5942 struct rtl8169_private *tp = netdev_priv(dev);
5943
5944 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5945 return;
5946
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005947 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
5948 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02005949}
5950
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005951static void r8169_phylink_handler(struct net_device *ndev)
5952{
5953 struct rtl8169_private *tp = netdev_priv(ndev);
5954
5955 if (netif_carrier_ok(ndev)) {
5956 rtl_link_chg_patch(tp);
5957 pm_request_resume(&tp->pci_dev->dev);
5958 } else {
5959 pm_runtime_idle(&tp->pci_dev->dev);
5960 }
5961
5962 if (net_ratelimit())
Heiner Kallweit703732f2019-01-19 22:07:05 +01005963 phy_print_status(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005964}
5965
5966static int r8169_phy_connect(struct rtl8169_private *tp)
5967{
Heiner Kallweit703732f2019-01-19 22:07:05 +01005968 struct phy_device *phydev = tp->phydev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005969 phy_interface_t phy_mode;
5970 int ret;
5971
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02005972 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005973 PHY_INTERFACE_MODE_MII;
5974
5975 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
5976 phy_mode);
5977 if (ret)
5978 return ret;
5979
Heiner Kallweit66058b12019-07-27 12:32:28 +02005980 if (!tp->supports_gmii)
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005981 phy_set_max_speed(phydev, SPEED_100);
5982
Heiner Kallweit9cf9b842019-05-04 12:01:03 +02005983 phy_support_asym_pause(phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005984
5985 phy_attached_info(phydev);
5986
5987 return 0;
5988}
5989
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990static void rtl8169_down(struct net_device *dev)
5991{
5992 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
Heiner Kallweit703732f2019-01-19 22:07:05 +01005994 phy_stop(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005995
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005996 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005997 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
Hayes Wang92fc43b2011-07-06 15:58:03 +08005999 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006000 /*
6001 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006002 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6003 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006004 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006005 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenney16f11502018-11-05 17:07:39 -08006008 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 rtl8169_tx_clear(tp);
6011
6012 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006013
6014 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015}
6016
6017static int rtl8169_close(struct net_device *dev)
6018{
6019 struct rtl8169_private *tp = netdev_priv(dev);
6020 struct pci_dev *pdev = tp->pci_dev;
6021
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006022 pm_runtime_get_sync(&pdev->dev);
6023
Francois Romieucecb5fd2011-04-01 10:21:07 +02006024 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006025 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006026
Francois Romieuda78dbf2012-01-26 14:18:23 +01006027 rtl_lock_work(tp);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006028 /* Clear all task flags */
6029 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006030
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006032 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033
Lekensteyn4ea72442013-07-22 09:53:30 +02006034 cancel_work_sync(&tp->wk.work);
6035
Heiner Kallweit703732f2019-01-19 22:07:05 +01006036 phy_disconnect(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006037
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006038 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006040 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6041 tp->RxPhyAddr);
6042 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6043 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 tp->TxDescArray = NULL;
6045 tp->RxDescArray = NULL;
6046
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006047 pm_runtime_put_sync(&pdev->dev);
6048
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 return 0;
6050}
6051
Francois Romieudc1c00c2012-03-08 10:06:18 +01006052#ifdef CONFIG_NET_POLL_CONTROLLER
6053static void rtl8169_netpoll(struct net_device *dev)
6054{
6055 struct rtl8169_private *tp = netdev_priv(dev);
6056
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03006057 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01006058}
6059#endif
6060
Francois Romieudf43ac72012-03-08 09:48:40 +01006061static int rtl_open(struct net_device *dev)
6062{
6063 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006064 struct pci_dev *pdev = tp->pci_dev;
6065 int retval = -ENOMEM;
6066
6067 pm_runtime_get_sync(&pdev->dev);
6068
6069 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006070 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006071 * dma_alloc_coherent provides more.
6072 */
6073 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6074 &tp->TxPhyAddr, GFP_KERNEL);
6075 if (!tp->TxDescArray)
6076 goto err_pm_runtime_put;
6077
6078 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6079 &tp->RxPhyAddr, GFP_KERNEL);
6080 if (!tp->RxDescArray)
6081 goto err_free_tx_0;
6082
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006083 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006084 if (retval < 0)
6085 goto err_free_rx_1;
6086
Francois Romieudf43ac72012-03-08 09:48:40 +01006087 rtl_request_firmware(tp);
6088
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006089 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006090 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01006091 if (retval < 0)
6092 goto err_release_fw_2;
6093
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006094 retval = r8169_phy_connect(tp);
6095 if (retval)
6096 goto err_free_irq;
6097
Francois Romieudf43ac72012-03-08 09:48:40 +01006098 rtl_lock_work(tp);
6099
6100 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6101
6102 napi_enable(&tp->napi);
6103
6104 rtl8169_init_phy(dev, tp);
6105
Francois Romieudf43ac72012-03-08 09:48:40 +01006106 rtl_pll_power_up(tp);
6107
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006108 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006109
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006110 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006111 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6112
Heiner Kallweit703732f2019-01-19 22:07:05 +01006113 phy_start(tp->phydev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006114 netif_start_queue(dev);
6115
6116 rtl_unlock_work(tp);
6117
Heiner Kallweita92a0842018-01-08 21:39:13 +01006118 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006119out:
6120 return retval;
6121
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006122err_free_irq:
6123 pci_free_irq(pdev, 0, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006124err_release_fw_2:
6125 rtl_release_firmware(tp);
6126 rtl8169_rx_clear(tp);
6127err_free_rx_1:
6128 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6129 tp->RxPhyAddr);
6130 tp->RxDescArray = NULL;
6131err_free_tx_0:
6132 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6133 tp->TxPhyAddr);
6134 tp->TxDescArray = NULL;
6135err_pm_runtime_put:
6136 pm_runtime_put_noidle(&pdev->dev);
6137 goto out;
6138}
6139
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006140static void
Junchang Wang8027aa22012-03-04 23:30:32 +01006141rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142{
6143 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006144 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02006145 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01006146 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006148 pm_runtime_get_noresume(&pdev->dev);
6149
6150 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006151 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006152
Junchang Wang8027aa22012-03-04 23:30:32 +01006153 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006154 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006155 stats->rx_packets = tp->rx_stats.packets;
6156 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006157 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006158
Junchang Wang8027aa22012-03-04 23:30:32 +01006159 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006160 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006161 stats->tx_packets = tp->tx_stats.packets;
6162 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006163 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006164
6165 stats->rx_dropped = dev->stats.rx_dropped;
6166 stats->tx_dropped = dev->stats.tx_dropped;
6167 stats->rx_length_errors = dev->stats.rx_length_errors;
6168 stats->rx_errors = dev->stats.rx_errors;
6169 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6170 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6171 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02006172 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01006173
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006174 /*
Corentin Musarded72a9b2019-07-24 14:34:43 +02006175 * Fetch additional counter values missing in stats collected by driver
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006176 * from tally counters.
6177 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006178 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006179 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006180
6181 /*
6182 * Subtract values fetched during initalization.
6183 * See rtl8169_init_counter_offsets for a description why we do that.
6184 */
Corinna Vinschen42020322015-09-10 10:47:35 +02006185 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006186 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02006187 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006188 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02006189 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006190 le16_to_cpu(tp->tc_offset.tx_aborted);
6191
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006192 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193}
6194
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006195static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006196{
françois romieu065c27c2011-01-03 15:08:12 +00006197 struct rtl8169_private *tp = netdev_priv(dev);
6198
Francois Romieu5d06a992006-02-23 00:47:58 +01006199 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006200 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006201
Heiner Kallweit703732f2019-01-19 22:07:05 +01006202 phy_stop(tp->phydev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006203 netif_device_detach(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006204
6205 rtl_lock_work(tp);
6206 napi_disable(&tp->napi);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006207 /* Clear all task flags */
6208 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6209
Francois Romieuda78dbf2012-01-26 14:18:23 +01006210 rtl_unlock_work(tp);
6211
6212 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006213}
Francois Romieu5d06a992006-02-23 00:47:58 +01006214
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006215#ifdef CONFIG_PM
6216
6217static int rtl8169_suspend(struct device *device)
6218{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006219 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006220 struct rtl8169_private *tp = netdev_priv(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006221
6222 rtl8169_net_suspend(dev);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006223 clk_disable_unprepare(tp->clk);
Francois Romieu1371fa62007-04-02 23:01:11 +02006224
Francois Romieu5d06a992006-02-23 00:47:58 +01006225 return 0;
6226}
6227
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006228static void __rtl8169_resume(struct net_device *dev)
6229{
françois romieu065c27c2011-01-03 15:08:12 +00006230 struct rtl8169_private *tp = netdev_priv(dev);
6231
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006232 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006233
6234 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02006235 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00006236
Heiner Kallweit703732f2019-01-19 22:07:05 +01006237 phy_start(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006238
Artem Savkovcff4c162012-04-03 10:29:11 +00006239 rtl_lock_work(tp);
6240 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006241 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Heiner Kallweit58ba5662019-01-19 22:06:25 +01006242 rtl_reset_work(tp);
Artem Savkovcff4c162012-04-03 10:29:11 +00006243 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006244}
6245
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006246static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006247{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006248 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006249 struct rtl8169_private *tp = netdev_priv(dev);
6250
Heiner Kallweit59715172019-05-29 07:44:01 +02006251 rtl_rar_set(tp, dev->dev_addr);
6252
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006253 clk_prepare_enable(tp->clk);
Francois Romieu5d06a992006-02-23 00:47:58 +01006254
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006255 if (netif_running(dev))
6256 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006257
Francois Romieu5d06a992006-02-23 00:47:58 +01006258 return 0;
6259}
6260
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006261static int rtl8169_runtime_suspend(struct device *device)
6262{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006263 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006264 struct rtl8169_private *tp = netdev_priv(dev);
6265
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006266 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006267 return 0;
6268
Francois Romieuda78dbf2012-01-26 14:18:23 +01006269 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006270 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006271 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006272
6273 rtl8169_net_suspend(dev);
6274
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006275 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006276 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006277 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006278
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006279 return 0;
6280}
6281
6282static int rtl8169_runtime_resume(struct device *device)
6283{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006284 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006285 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit59715172019-05-29 07:44:01 +02006286
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08006287 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006288
6289 if (!tp->TxDescArray)
6290 return 0;
6291
Francois Romieuda78dbf2012-01-26 14:18:23 +01006292 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006293 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006294 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006295
6296 __rtl8169_resume(dev);
6297
6298 return 0;
6299}
6300
6301static int rtl8169_runtime_idle(struct device *device)
6302{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006303 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006304
Heiner Kallweita92a0842018-01-08 21:39:13 +01006305 if (!netif_running(dev) || !netif_carrier_ok(dev))
6306 pm_schedule_suspend(device, 10000);
6307
6308 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006309}
6310
Alexey Dobriyan47145212009-12-14 18:00:08 -08006311static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006312 .suspend = rtl8169_suspend,
6313 .resume = rtl8169_resume,
6314 .freeze = rtl8169_suspend,
6315 .thaw = rtl8169_resume,
6316 .poweroff = rtl8169_suspend,
6317 .restore = rtl8169_resume,
6318 .runtime_suspend = rtl8169_runtime_suspend,
6319 .runtime_resume = rtl8169_runtime_resume,
6320 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006321};
6322
6323#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6324
6325#else /* !CONFIG_PM */
6326
6327#define RTL8169_PM_OPS NULL
6328
6329#endif /* !CONFIG_PM */
6330
David S. Miller1805b2f2011-10-24 18:18:09 -04006331static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6332{
David S. Miller1805b2f2011-10-24 18:18:09 -04006333 /* WoL fails with 8168b when the receiver is disabled. */
6334 switch (tp->mac_version) {
6335 case RTL_GIGA_MAC_VER_11:
6336 case RTL_GIGA_MAC_VER_12:
6337 case RTL_GIGA_MAC_VER_17:
6338 pci_clear_master(tp->pci_dev);
6339
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006340 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006341 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006342 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04006343 break;
6344 default:
6345 break;
6346 }
6347}
6348
Francois Romieu1765f952008-09-13 17:21:40 +02006349static void rtl_shutdown(struct pci_dev *pdev)
6350{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006351 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006352 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006353
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006354 rtl8169_net_suspend(dev);
6355
Francois Romieucecb5fd2011-04-01 10:21:07 +02006356 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006357 rtl_rar_set(tp, dev->perm_addr);
6358
Hayes Wang92fc43b2011-07-06 15:58:03 +08006359 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006360
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006361 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02006362 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006363 rtl_wol_suspend_quirk(tp);
6364 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006365 }
6366
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006367 pci_wake_from_d3(pdev, true);
6368 pci_set_power_state(pdev, PCI_D3hot);
6369 }
6370}
Francois Romieu5d06a992006-02-23 00:47:58 +01006371
Bill Pembertonbaf63292012-12-03 09:23:28 -05006372static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006373{
6374 struct net_device *dev = pci_get_drvdata(pdev);
6375 struct rtl8169_private *tp = netdev_priv(dev);
6376
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006377 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01006378 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01006379
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006380 netif_napi_del(&tp->napi);
6381
Francois Romieue27566e2012-03-08 09:54:01 +01006382 unregister_netdev(dev);
Heiner Kallweit703732f2019-01-19 22:07:05 +01006383 mdiobus_unregister(tp->phydev->mdio.bus);
Francois Romieue27566e2012-03-08 09:54:01 +01006384
6385 rtl_release_firmware(tp);
6386
6387 if (pci_dev_run_wake(pdev))
6388 pm_runtime_get_noresume(&pdev->dev);
6389
6390 /* restore original MAC address */
6391 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01006392}
6393
Francois Romieufa9c3852012-03-08 10:01:50 +01006394static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006395 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006396 .ndo_stop = rtl8169_close,
6397 .ndo_get_stats64 = rtl8169_get_stats64,
6398 .ndo_start_xmit = rtl8169_start_xmit,
Heiner Kallweite64e0c82019-07-26 21:49:22 +02006399 .ndo_features_check = rtl8169_features_check,
Francois Romieufa9c3852012-03-08 10:01:50 +01006400 .ndo_tx_timeout = rtl8169_tx_timeout,
6401 .ndo_validate_addr = eth_validate_addr,
6402 .ndo_change_mtu = rtl8169_change_mtu,
6403 .ndo_fix_features = rtl8169_fix_features,
6404 .ndo_set_features = rtl8169_set_features,
6405 .ndo_set_mac_address = rtl_set_mac_address,
6406 .ndo_do_ioctl = rtl8169_ioctl,
6407 .ndo_set_rx_mode = rtl_set_rx_mode,
6408#ifdef CONFIG_NET_POLL_CONTROLLER
6409 .ndo_poll_controller = rtl8169_netpoll,
6410#endif
6411
6412};
6413
Heiner Kallweitec9a4082019-06-10 18:21:50 +02006414static void rtl_set_irq_mask(struct rtl8169_private *tp)
6415{
6416 tp->irq_mask = RTL_EVENT_NAPI | LinkChg;
6417
6418 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6419 tp->irq_mask |= SYSErr | RxOverflow | RxFIFOOver;
6420 else if (tp->mac_version == RTL_GIGA_MAC_VER_11)
6421 /* special workaround needed */
6422 tp->irq_mask |= RxFIFOOver;
6423 else
6424 tp->irq_mask |= RxOverflow;
6425}
6426
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006427static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01006428{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006429 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006430
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006431 switch (tp->mac_version) {
6432 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006433 rtl_unlock_config_regs(tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006434 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006435 rtl_lock_config_regs(tp);
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006436 /* fall through */
6437 case RTL_GIGA_MAC_VER_07 ... RTL_GIGA_MAC_VER_24:
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006438 flags = PCI_IRQ_LEGACY;
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006439 break;
6440 default:
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006441 flags = PCI_IRQ_ALL_TYPES;
Heiner Kallweit003bd5b2019-07-27 12:43:31 +02006442 break;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006443 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006444
6445 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01006446}
6447
Thierry Reding04c77882019-02-06 13:30:17 +01006448static void rtl_read_mac_address(struct rtl8169_private *tp,
6449 u8 mac_addr[ETH_ALEN])
6450{
6451 /* Get MAC address */
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02006452 if (rtl_is_8168evl_up(tp) && tp->mac_version != RTL_GIGA_MAC_VER_34) {
6453 u32 value = rtl_eri_read(tp, 0xe0);
6454
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006455 mac_addr[0] = (value >> 0) & 0xff;
6456 mac_addr[1] = (value >> 8) & 0xff;
6457 mac_addr[2] = (value >> 16) & 0xff;
6458 mac_addr[3] = (value >> 24) & 0xff;
6459
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02006460 value = rtl_eri_read(tp, 0xe4);
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006461 mac_addr[4] = (value >> 0) & 0xff;
6462 mac_addr[5] = (value >> 8) & 0xff;
Thierry Reding04c77882019-02-06 13:30:17 +01006463 }
6464}
6465
Hayes Wangc5583862012-07-02 17:23:22 +08006466DECLARE_RTL_COND(rtl_link_list_ready_cond)
6467{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006468 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08006469}
6470
6471DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6472{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006473 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08006474}
6475
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006476static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
6477{
6478 struct rtl8169_private *tp = mii_bus->priv;
6479
6480 if (phyaddr > 0)
6481 return -ENODEV;
6482
6483 return rtl_readphy(tp, phyreg);
6484}
6485
6486static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
6487 int phyreg, u16 val)
6488{
6489 struct rtl8169_private *tp = mii_bus->priv;
6490
6491 if (phyaddr > 0)
6492 return -ENODEV;
6493
6494 rtl_writephy(tp, phyreg, val);
6495
6496 return 0;
6497}
6498
6499static int r8169_mdio_register(struct rtl8169_private *tp)
6500{
6501 struct pci_dev *pdev = tp->pci_dev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006502 struct mii_bus *new_bus;
6503 int ret;
6504
6505 new_bus = devm_mdiobus_alloc(&pdev->dev);
6506 if (!new_bus)
6507 return -ENOMEM;
6508
6509 new_bus->name = "r8169";
6510 new_bus->priv = tp;
6511 new_bus->parent = &pdev->dev;
6512 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
Heiner Kallweita1950162019-04-24 21:13:25 +02006513 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x", pci_dev_id(pdev));
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006514
6515 new_bus->read = r8169_mdio_read_reg;
6516 new_bus->write = r8169_mdio_write_reg;
6517
6518 ret = mdiobus_register(new_bus);
6519 if (ret)
6520 return ret;
6521
Heiner Kallweit703732f2019-01-19 22:07:05 +01006522 tp->phydev = mdiobus_get_phy(new_bus, 0);
6523 if (!tp->phydev) {
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006524 mdiobus_unregister(new_bus);
6525 return -ENODEV;
6526 }
6527
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02006528 /* PHY will be woken up in rtl_open() */
Heiner Kallweit703732f2019-01-19 22:07:05 +01006529 phy_suspend(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006530
6531 return 0;
6532}
6533
Bill Pembertonbaf63292012-12-03 09:23:28 -05006534static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006535{
Hayes Wangc5583862012-07-02 17:23:22 +08006536 tp->ocp_base = OCP_STD_PHY_BASE;
6537
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006538 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006539
6540 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6541 return;
6542
6543 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6544 return;
6545
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006546 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08006547 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006548 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08006549
Heiner Kallweitef712ed2019-08-04 09:47:51 +02006550 r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
Hayes Wangc5583862012-07-02 17:23:22 +08006551
6552 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6553 return;
6554
Heiner Kallweitef712ed2019-08-04 09:47:51 +02006555 r8168_mac_ocp_modify(tp, 0xe8de, 0, BIT(15));
Hayes Wangc5583862012-07-02 17:23:22 +08006556
Heiner Kallweit7160be22019-05-25 20:44:01 +02006557 rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
Hayes Wangc5583862012-07-02 17:23:22 +08006558}
6559
Bill Pembertonbaf63292012-12-03 09:23:28 -05006560static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006561{
6562 switch (tp->mac_version) {
Heiner Kallweit29ec0482019-05-25 20:43:25 +02006563 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
6564 rtl8168ep_stop_cmac(tp);
6565 /* fall through */
Heiner Kallweit2a718832018-05-02 21:39:49 +02006566 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006567 rtl_hw_init_8168g(tp);
6568 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006569 default:
6570 break;
6571 }
6572}
6573
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006574static int rtl_jumbo_max(struct rtl8169_private *tp)
6575{
6576 /* Non-GBit versions don't support jumbo frames */
6577 if (!tp->supports_gmii)
6578 return JUMBO_1K;
6579
6580 switch (tp->mac_version) {
6581 /* RTL8169 */
Heiner Kallweite9588eb2019-05-25 21:14:39 +02006582 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006583 return JUMBO_7K;
6584 /* RTL8168b */
6585 case RTL_GIGA_MAC_VER_11:
6586 case RTL_GIGA_MAC_VER_12:
6587 case RTL_GIGA_MAC_VER_17:
6588 return JUMBO_4K;
6589 /* RTL8168c */
6590 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
6591 return JUMBO_6K;
6592 default:
6593 return JUMBO_9K;
6594 }
6595}
6596
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006597static void rtl_disable_clk(void *data)
6598{
6599 clk_disable_unprepare(data);
6600}
6601
Heiner Kallweitb779dae2019-01-19 22:07:34 +01006602static int rtl_get_ether_clk(struct rtl8169_private *tp)
6603{
6604 struct device *d = tp_to_dev(tp);
6605 struct clk *clk;
6606 int rc;
6607
6608 clk = devm_clk_get(d, "ether_clk");
6609 if (IS_ERR(clk)) {
6610 rc = PTR_ERR(clk);
6611 if (rc == -ENOENT)
6612 /* clk-core allows NULL (for suspend / resume) */
6613 rc = 0;
6614 else if (rc != -EPROBE_DEFER)
6615 dev_err(d, "failed to get clk: %d\n", rc);
6616 } else {
6617 tp->clk = clk;
6618 rc = clk_prepare_enable(clk);
6619 if (rc)
6620 dev_err(d, "failed to enable clk: %d\n", rc);
6621 else
6622 rc = devm_add_action_or_reset(d, rtl_disable_clk, clk);
6623 }
6624
6625 return rc;
6626}
6627
Heiner Kallweitc782e202019-07-02 20:46:09 +02006628static void rtl_init_mac_address(struct rtl8169_private *tp)
6629{
6630 struct net_device *dev = tp->dev;
6631 u8 *mac_addr = dev->dev_addr;
6632 int rc, i;
6633
6634 rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr);
6635 if (!rc)
6636 goto done;
6637
6638 rtl_read_mac_address(tp, mac_addr);
6639 if (is_valid_ether_addr(mac_addr))
6640 goto done;
6641
6642 for (i = 0; i < ETH_ALEN; i++)
6643 mac_addr[i] = RTL_R8(tp, MAC0 + i);
6644 if (is_valid_ether_addr(mac_addr))
6645 goto done;
6646
6647 eth_hw_addr_random(dev);
6648 dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n");
6649done:
6650 rtl_rar_set(tp, mac_addr);
6651}
6652
hayeswang929a0312014-09-16 11:40:47 +08006653static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01006654{
Francois Romieu3b6cf252012-03-08 09:59:04 +01006655 struct rtl8169_private *tp;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006656 struct net_device *dev;
Heiner Kallweitc782e202019-07-02 20:46:09 +02006657 int chipset, region;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006658 int jumbo_max, rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006659
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006660 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
6661 if (!dev)
6662 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006663
6664 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006665 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006666 tp = netdev_priv(dev);
6667 tp->dev = dev;
6668 tp->pci_dev = pdev;
6669 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Heiner Kallweit145a40e2019-06-10 18:25:29 +02006670 tp->supports_gmii = ent->driver_data == RTL_CFG_NO_GBIT ? 0 : 1;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006671
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006672 /* Get the *optional* external "ether_clk" used on some boards */
Heiner Kallweitb779dae2019-01-19 22:07:34 +01006673 rc = rtl_get_ether_clk(tp);
6674 if (rc)
6675 return rc;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006676
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +02006677 /* Disable ASPM completely as that cause random device stop working
6678 * problems as well as full system hangs for some PCIe devices users.
6679 */
Heiner Kallweit62b1b3b2019-06-18 23:14:50 +02006680 rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
6681 PCIE_LINK_STATE_L1);
6682 tp->aspm_manageable = !rc;
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +02006683
Francois Romieu3b6cf252012-03-08 09:59:04 +01006684 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006685 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006686 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006687 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006688 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006689 }
6690
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006691 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02006692 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01006693
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02006694 /* use first MMIO region */
6695 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
6696 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006697 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006698 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006699 }
6700
6701 /* check for weird/broken PCI region reporting */
6702 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006703 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006704 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006705 }
6706
Andy Shevchenko93a00d42018-03-01 13:27:35 +02006707 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006708 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006709 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006710 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006711 }
6712
Andy Shevchenko93a00d42018-03-01 13:27:35 +02006713 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01006714
Francois Romieu3b6cf252012-03-08 09:59:04 +01006715 /* Identify chip attached to board */
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01006716 rtl8169_get_mac_version(tp);
6717 if (tp->mac_version == RTL_GIGA_MAC_NONE)
6718 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006719
Heiner Kallweit0ae09742018-04-28 22:19:26 +02006720 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006721
Heiner Kallweit10b63e82019-01-20 11:45:20 +01006722 if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
Heiner Kallweit3c18cbe2019-06-27 23:12:39 +02006723 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)))
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006724 dev->features |= NETIF_F_HIGHDMA;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006725
Francois Romieu3b6cf252012-03-08 09:59:04 +01006726 rtl_init_rxcfg(tp);
6727
Heiner Kallweitde20e122018-09-25 07:58:00 +02006728 rtl8169_irq_mask_and_ack(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006729
Hayes Wangc5583862012-07-02 17:23:22 +08006730 rtl_hw_initialize(tp);
6731
Francois Romieu3b6cf252012-03-08 09:59:04 +01006732 rtl_hw_reset(tp);
6733
Francois Romieu3b6cf252012-03-08 09:59:04 +01006734 pci_set_master(pdev);
6735
Francois Romieu3b6cf252012-03-08 09:59:04 +01006736 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006737
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006738 rc = rtl_alloc_irq(tp);
6739 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006740 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006741 return rc;
6742 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01006743
Francois Romieu3b6cf252012-03-08 09:59:04 +01006744 mutex_init(&tp->wk.mutex);
Heiner Kallweit5c41e782019-01-19 22:03:49 +01006745 INIT_WORK(&tp->wk.work, rtl_task);
Kyle McMartin340fea32014-02-24 20:12:28 -05006746 u64_stats_init(&tp->rx_stats.syncp);
6747 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006748
Heiner Kallweitc782e202019-07-02 20:46:09 +02006749 rtl_init_mac_address(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006750
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00006751 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006752
Heiner Kallweit37621492018-04-17 23:20:03 +02006753 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006754
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006755 dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6756 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6757 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006758 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00006759 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6760 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006761 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6762 NETIF_F_HIGHDMA;
Heiner Kallweit2d0ec542018-07-02 22:49:35 +02006763 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006764
hayeswang929a0312014-09-16 11:40:47 +08006765 tp->cp_cmd |= RxChkSum | RxVlan;
6766
6767 /*
6768 * Pretend we are using VLANs; This bypasses a nasty bug where
6769 * Interrupts stop flowing on high load on 8110SCd controllers.
6770 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01006771 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08006772 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00006773 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006774
Heiner Kallweit0170d592019-07-26 21:48:32 +02006775 if (rtl_chip_supports_csum_v2(tp)) {
hayeswange9746042014-07-11 16:25:58 +08006776 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006777 dev->features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweit0170d592019-07-26 21:48:32 +02006778 dev->gso_max_size = RTL_GSO_MAX_SIZE_V2;
6779 dev->gso_max_segs = RTL_GSO_MAX_SEGS_V2;
6780 } else {
6781 dev->gso_max_size = RTL_GSO_MAX_SIZE_V1;
6782 dev->gso_max_segs = RTL_GSO_MAX_SEGS_V1;
6783 }
hayeswang5888d3f2014-07-11 16:25:56 +08006784
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006785 /* RTL8168e-vl has a HW issue with TSO */
6786 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
Holger Hoffstättea7eb6a42019-08-09 00:02:40 +02006787 dev->vlan_features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
6788 dev->hw_features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
6789 dev->features &= ~(NETIF_F_ALL_TSO | NETIF_F_SG);
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006790 }
6791
Francois Romieu3b6cf252012-03-08 09:59:04 +01006792 dev->hw_features |= NETIF_F_RXALL;
6793 dev->hw_features |= NETIF_F_RXFCS;
6794
Jarod Wilsonc7315a92016-10-17 15:54:09 -04006795 /* MTU range: 60 - hw-specific max */
6796 dev->min_mtu = ETH_ZLEN;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006797 jumbo_max = rtl_jumbo_max(tp);
6798 dev->max_mtu = jumbo_max;
Jarod Wilsonc7315a92016-10-17 15:54:09 -04006799
Heiner Kallweitec9a4082019-06-10 18:21:50 +02006800 rtl_set_irq_mask(tp);
Heiner Kallweit9fa0a8e2019-06-10 18:24:25 +02006801
Heiner Kallweit254764e2019-01-22 22:23:41 +01006802 tp->fw_name = rtl_chip_infos[chipset].fw_name;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006803
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006804 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
6805 &tp->counters_phys_addr,
6806 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01006807 if (!tp->counters)
6808 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02006809
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02006810 pci_set_drvdata(pdev, dev);
6811
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006812 rc = r8169_mdio_register(tp);
6813 if (rc)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01006814 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006815
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006816 /* chip gets powered up in rtl_open() */
6817 rtl_pll_power_down(tp);
6818
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006819 rc = register_netdev(dev);
6820 if (rc)
6821 goto err_mdio_unregister;
6822
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01006823 netif_info(tp, probe, dev, "%s, %pM, XID %03x, IRQ %d\n",
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02006824 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01006825 (RTL_R32(tp, TxConfig) >> 20) & 0xfcf,
Heiner Kallweit29274992018-02-28 20:43:38 +01006826 pci_irq_vector(pdev, 0));
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006827
6828 if (jumbo_max > JUMBO_1K)
6829 netif_info(tp, probe, dev,
6830 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
6831 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
6832 "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01006833
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006834 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01006835 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006836
Heiner Kallweita92a0842018-01-08 21:39:13 +01006837 if (pci_dev_run_wake(pdev))
6838 pm_runtime_put_sync(&pdev->dev);
6839
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006840 return 0;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006841
6842err_mdio_unregister:
Heiner Kallweit703732f2019-01-19 22:07:05 +01006843 mdiobus_unregister(tp->phydev->mdio.bus);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006844 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006845}
6846
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847static struct pci_driver rtl8169_pci_driver = {
6848 .name = MODULENAME,
6849 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006850 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05006851 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02006852 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006853 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854};
6855
Devendra Naga3eeb7da2012-10-26 09:27:42 +00006856module_pci_driver(rtl8169_pci_driver);