blob: 8e404186ef87d3f46288b6ae28b2ecd1160da2e9 [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>
françois romieubca03d52011-01-03 15:07:31 +000030#include <linux/firmware.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040031#include <linux/prefetch.h>
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +020032#include <linux/pci-aspm.h>
hayeswange9746042014-07-11 16:25:58 +080033#include <linux/ipv6.h>
34#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MODULENAME "r8169"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
françois romieubca03d52011-01-03 15:07:31 +000038#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000040#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080042#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080043#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
44#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080045#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080046#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080047#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080048#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080049#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000050#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000051#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000052#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080053#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
54#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
55#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
56#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000057
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020058#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070059 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
62 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050063static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Michal Schmidtaee77e42012-09-09 13:55:26 +000065#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
67
68#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020069#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000071#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
73#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +020076#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
77#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
78#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
79#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
80#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
81#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +020084 RTL_GIGA_MAC_VER_01 = 0,
85 RTL_GIGA_MAC_VER_02,
86 RTL_GIGA_MAC_VER_03,
87 RTL_GIGA_MAC_VER_04,
88 RTL_GIGA_MAC_VER_05,
89 RTL_GIGA_MAC_VER_06,
90 RTL_GIGA_MAC_VER_07,
91 RTL_GIGA_MAC_VER_08,
92 RTL_GIGA_MAC_VER_09,
93 RTL_GIGA_MAC_VER_10,
94 RTL_GIGA_MAC_VER_11,
95 RTL_GIGA_MAC_VER_12,
96 RTL_GIGA_MAC_VER_13,
97 RTL_GIGA_MAC_VER_14,
98 RTL_GIGA_MAC_VER_15,
99 RTL_GIGA_MAC_VER_16,
100 RTL_GIGA_MAC_VER_17,
101 RTL_GIGA_MAC_VER_18,
102 RTL_GIGA_MAC_VER_19,
103 RTL_GIGA_MAC_VER_20,
104 RTL_GIGA_MAC_VER_21,
105 RTL_GIGA_MAC_VER_22,
106 RTL_GIGA_MAC_VER_23,
107 RTL_GIGA_MAC_VER_24,
108 RTL_GIGA_MAC_VER_25,
109 RTL_GIGA_MAC_VER_26,
110 RTL_GIGA_MAC_VER_27,
111 RTL_GIGA_MAC_VER_28,
112 RTL_GIGA_MAC_VER_29,
113 RTL_GIGA_MAC_VER_30,
114 RTL_GIGA_MAC_VER_31,
115 RTL_GIGA_MAC_VER_32,
116 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800117 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800118 RTL_GIGA_MAC_VER_35,
119 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800120 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800121 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800122 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800123 RTL_GIGA_MAC_VER_40,
124 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000125 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000126 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800127 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800128 RTL_GIGA_MAC_VER_45,
129 RTL_GIGA_MAC_VER_46,
130 RTL_GIGA_MAC_VER_47,
131 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800132 RTL_GIGA_MAC_VER_49,
133 RTL_GIGA_MAC_VER_50,
134 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200135 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Francois Romieud58d46b2011-05-03 16:38:29 +0200138#define JUMBO_1K ETH_DATA_LEN
139#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
140#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
141#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
142#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
143
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800144static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 const char *name;
Francois Romieu85bffe62011-04-27 08:22:39 +0200146 const char *fw_name;
147} rtl_chip_infos[] = {
148 /* PCI devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200149 [RTL_GIGA_MAC_VER_01] = {"RTL8169" },
150 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
151 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
152 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
153 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
154 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
Francois Romieu85bffe62011-04-27 08:22:39 +0200155 /* PCI-E devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200156 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
157 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
158 [RTL_GIGA_MAC_VER_09] = {"RTL8102e" },
159 [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
160 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
161 [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
162 [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
163 [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
164 [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
165 [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
166 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
167 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
168 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
169 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
170 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
171 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
172 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
173 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
174 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
175 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
176 [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
177 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
178 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
179 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
180 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
181 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
182 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
183 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
184 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
185 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
186 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
187 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
188 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
189 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
190 [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
191 [RTL_GIGA_MAC_VER_42] = {"RTL8168g/8111g", FIRMWARE_8168G_3},
192 [RTL_GIGA_MAC_VER_43] = {"RTL8106e", FIRMWARE_8106E_2},
193 [RTL_GIGA_MAC_VER_44] = {"RTL8411", FIRMWARE_8411_2 },
194 [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
195 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
196 [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
197 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
198 [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
199 [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
200 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Francois Romieubcf0bf92006-07-26 23:14:13 +0200203enum cfg_version {
204 RTL_CFG_0 = 0x00,
205 RTL_CFG_1,
206 RTL_CFG_2
207};
208
Benoit Taine9baa3c32014-08-08 15:56:03 +0200209static const struct pci_device_id rtl8169_pci_tbl[] = {
Kai-Heng Feng36352992019-01-02 14:45:07 +0800210 { PCI_VDEVICE(REALTEK, 0x2502), RTL_CFG_1 },
211 { PCI_VDEVICE(REALTEK, 0x2600), RTL_CFG_1 },
Heiner Kallweit6f0d3082018-11-19 22:35:08 +0100212 { PCI_VDEVICE(REALTEK, 0x8129), RTL_CFG_0 },
213 { PCI_VDEVICE(REALTEK, 0x8136), RTL_CFG_2 },
214 { PCI_VDEVICE(REALTEK, 0x8161), RTL_CFG_1 },
215 { PCI_VDEVICE(REALTEK, 0x8167), RTL_CFG_0 },
216 { PCI_VDEVICE(REALTEK, 0x8168), RTL_CFG_1 },
217 { PCI_VDEVICE(NCUBE, 0x8168), RTL_CFG_1 },
218 { PCI_VDEVICE(REALTEK, 0x8169), RTL_CFG_0 },
219 { PCI_VENDOR_ID_DLINK, 0x4300,
220 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
221 { PCI_VDEVICE(DLINK, 0x4300), RTL_CFG_0 },
222 { PCI_VDEVICE(DLINK, 0x4302), RTL_CFG_0 },
223 { PCI_VDEVICE(AT, 0xc107), RTL_CFG_0 },
224 { PCI_VDEVICE(USR, 0x0116), RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200225 { PCI_VENDOR_ID_LINKSYS, 0x1032,
226 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100227 { 0x0001, 0x8168,
228 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Heiner Kallweit6f0d3082018-11-19 22:35:08 +0100229 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230};
231
232MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
233
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200234static struct {
235 u32 msg_enable;
236} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Francois Romieu07d3f512007-02-21 22:40:46 +0100238enum rtl_registers {
239 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100240 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100241 MAR0 = 8, /* Multicast filter. */
242 CounterAddrLow = 0x10,
243 CounterAddrHigh = 0x14,
244 TxDescStartAddrLow = 0x20,
245 TxDescStartAddrHigh = 0x24,
246 TxHDescStartAddrLow = 0x28,
247 TxHDescStartAddrHigh = 0x2c,
248 FLASH = 0x30,
249 ERSR = 0x36,
250 ChipCmd = 0x37,
251 TxPoll = 0x38,
252 IntrMask = 0x3c,
253 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700254
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800255 TxConfig = 0x40,
256#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
257#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
258
259 RxConfig = 0x44,
260#define RX128_INT_EN (1 << 15) /* 8111c and later */
261#define RX_MULTI_EN (1 << 14) /* 8111c only */
262#define RXCFG_FIFO_SHIFT 13
263 /* No threshold before first PCI xfer */
264#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000265#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800266#define RXCFG_DMA_SHIFT 8
267 /* Unlimited maximum PCI burst. */
268#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700269
Francois Romieu07d3f512007-02-21 22:40:46 +0100270 RxMissed = 0x4c,
271 Cfg9346 = 0x50,
272 Config0 = 0x51,
273 Config1 = 0x52,
274 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200275#define PME_SIGNAL (1 << 5) /* 8168c and later */
276
Francois Romieu07d3f512007-02-21 22:40:46 +0100277 Config3 = 0x54,
278 Config4 = 0x55,
279 Config5 = 0x56,
280 MultiIntr = 0x5c,
281 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100282 PHYstatus = 0x6c,
283 RxMaxSize = 0xda,
284 CPlusCmd = 0xe0,
285 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300286
287#define RTL_COALESCE_MASK 0x0f
288#define RTL_COALESCE_SHIFT 4
289#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
290#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
291
Francois Romieu07d3f512007-02-21 22:40:46 +0100292 RxDescAddrLow = 0xe4,
293 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000294 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
295
296#define NoEarlyTx 0x3f /* Max value : no early transmit. */
297
298 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
299
300#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800301#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000302
Francois Romieu07d3f512007-02-21 22:40:46 +0100303 FuncEvent = 0xf0,
304 FuncEventMask = 0xf4,
305 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800306 IBCR0 = 0xf8,
307 IBCR2 = 0xf9,
308 IBIMR0 = 0xfa,
309 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100310 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
Francois Romieuf162a5d2008-06-01 22:37:49 +0200313enum rtl8168_8101_registers {
314 CSIDR = 0x64,
315 CSIAR = 0x68,
316#define CSIAR_FLAG 0x80000000
317#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200318#define CSIAR_BYTE_ENABLE 0x0000f000
319#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000320 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200321 EPHYAR = 0x80,
322#define EPHYAR_FLAG 0x80000000
323#define EPHYAR_WRITE_CMD 0x80000000
324#define EPHYAR_REG_MASK 0x1f
325#define EPHYAR_REG_SHIFT 16
326#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800327 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800328#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800329#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200330 DBG_REG = 0xd1,
331#define FIX_NAK_1 (1 << 4)
332#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800333 TWSI = 0xd2,
334 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800335#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800336#define TX_EMPTY (1 << 5)
337#define RX_EMPTY (1 << 4)
338#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800339#define EN_NDP (1 << 3)
340#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800341#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000342 EFUSEAR = 0xdc,
343#define EFUSEAR_FLAG 0x80000000
344#define EFUSEAR_WRITE_CMD 0x80000000
345#define EFUSEAR_READ_CMD 0x00000000
346#define EFUSEAR_REG_MASK 0x03ff
347#define EFUSEAR_REG_SHIFT 8
348#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800349 MISC_1 = 0xf2,
350#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200351};
352
françois romieuc0e45c12011-01-03 15:08:04 +0000353enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800354 LED_FREQ = 0x1a,
355 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000356 ERIDR = 0x70,
357 ERIAR = 0x74,
358#define ERIAR_FLAG 0x80000000
359#define ERIAR_WRITE_CMD 0x80000000
360#define ERIAR_READ_CMD 0x00000000
361#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000362#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800363#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
364#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
365#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800366#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800367#define ERIAR_MASK_SHIFT 12
368#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
369#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800370#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800371#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800372#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000373 EPHY_RXER_NUM = 0x7c,
374 OCPDR = 0xb0, /* OCP GPHY access */
375#define OCPDR_WRITE_CMD 0x80000000
376#define OCPDR_READ_CMD 0x00000000
377#define OCPDR_REG_MASK 0x7f
378#define OCPDR_GPHY_REG_SHIFT 16
379#define OCPDR_DATA_MASK 0xffff
380 OCPAR = 0xb4,
381#define OCPAR_FLAG 0x80000000
382#define OCPAR_GPHY_WRITE_CMD 0x8000f060
383#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800384 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000385 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
386 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200387#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800388#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800389#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800390#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800391#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000392};
393
Francois Romieu07d3f512007-02-21 22:40:46 +0100394enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100396 SYSErr = 0x8000,
397 PCSTimeout = 0x4000,
398 SWInt = 0x0100,
399 TxDescUnavail = 0x0080,
400 RxFIFOOver = 0x0040,
401 LinkChg = 0x0020,
402 RxOverflow = 0x0010,
403 TxErr = 0x0008,
404 TxOK = 0x0004,
405 RxErr = 0x0002,
406 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400409 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200410 RxFOVF = (1 << 23),
411 RxRWT = (1 << 22),
412 RxRES = (1 << 21),
413 RxRUNT = (1 << 20),
414 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800417 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100418 CmdReset = 0x10,
419 CmdRxEnb = 0x08,
420 CmdTxEnb = 0x04,
421 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Francois Romieu275391a2007-02-23 23:50:28 +0100423 /* TXPoll register p.5 */
424 HPQ = 0x80, /* Poll cmd on the high prio queue */
425 NPQ = 0x40, /* Poll cmd on the low prio queue */
426 FSWInt = 0x01, /* Forced software interrupt */
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100429 Cfg9346_Lock = 0x00,
430 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100433 AcceptErr = 0x20,
434 AcceptRunt = 0x10,
435 AcceptBroadcast = 0x08,
436 AcceptMulticast = 0x04,
437 AcceptMyPhys = 0x02,
438 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200439#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* TxConfigBits */
442 TxInterFrameGapShift = 24,
443 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
444
Francois Romieu5d06a992006-02-23 00:47:58 +0100445 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200446 LEDS1 = (1 << 7),
447 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200448 Speed_down = (1 << 4),
449 MEMMAP = (1 << 3),
450 IOMAP = (1 << 2),
451 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100452 PMEnable = (1 << 0), /* Power Management Enable */
453
Francois Romieu6dccd162007-02-13 23:38:05 +0100454 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000455 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000456 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100457 PCI_Clock_66MHz = 0x01,
458 PCI_Clock_33MHz = 0x00,
459
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100460 /* Config3 register p.25 */
461 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
462 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200463 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800464 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200465 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100466
Francois Romieud58d46b2011-05-03 16:38:29 +0200467 /* Config4 register */
468 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
469
Francois Romieu5d06a992006-02-23 00:47:58 +0100470 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100471 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
472 MWF = (1 << 5), /* Accept Multicast wakeup frame */
473 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200474 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100475 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100476 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000477 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200480 EnableBist = (1 << 15), // 8168 8101
481 Mac_dbgo_oe = (1 << 14), // 8168 8101
482 Normal_mode = (1 << 13), // unused
483 Force_half_dup = (1 << 12), // 8168 8101
484 Force_rxflow_en = (1 << 11), // 8168 8101
485 Force_txflow_en = (1 << 10), // 8168 8101
486 Cxpl_dbg_sel = (1 << 9), // 8168 8101
487 ASF = (1 << 8), // 8168 8101
488 PktCntrDisable = (1 << 7), // 8168 8101
489 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 RxVlan = (1 << 6),
491 RxChkSum = (1 << 5),
492 PCIDAC = (1 << 4),
493 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200494#define INTT_MASK GENMASK(1, 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100497 TBI_Enable = 0x80,
498 TxFlowCtrl = 0x40,
499 RxFlowCtrl = 0x20,
500 _1000bpsF = 0x10,
501 _100bps = 0x08,
502 _10bps = 0x04,
503 LinkStatus = 0x02,
504 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100507 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200508
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200509 /* ResetCounterCommand */
510 CounterReset = 0x1,
511
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200512 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100513 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800514
515 /* magic enable v2 */
516 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517};
518
Francois Romieu2b7b4312011-04-18 22:53:24 -0700519enum rtl_desc_bit {
520 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
522 RingEnd = (1 << 30), /* End of descriptor ring */
523 FirstFrag = (1 << 29), /* First segment of a packet */
524 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700525};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Francois Romieu2b7b4312011-04-18 22:53:24 -0700527/* Generic case. */
528enum rtl_tx_desc_bit {
529 /* First doubleword. */
530 TD_LSO = (1 << 27), /* Large Send Offload */
531#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Francois Romieu2b7b4312011-04-18 22:53:24 -0700533 /* Second doubleword. */
534 TxVlanTag = (1 << 17), /* Add VLAN tag */
535};
536
537/* 8169, 8168b and 810x except 8102e. */
538enum rtl_tx_desc_bit_0 {
539 /* First doubleword. */
540#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
541 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
542 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
543 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
544};
545
546/* 8102e, 8168c and beyond. */
547enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800548 /* First doubleword. */
549 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800550 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800551#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800552#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800553
Francois Romieu2b7b4312011-04-18 22:53:24 -0700554 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800555#define TCPHO_SHIFT 18
556#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700557#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800558 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
559 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700560 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
561 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
562};
563
Francois Romieu2b7b4312011-04-18 22:53:24 -0700564enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* Rx private */
566 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500567 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569#define RxProtoUDP (PID1)
570#define RxProtoTCP (PID0)
571#define RxProtoIP (PID1 | PID0)
572#define RxProtoMask RxProtoIP
573
574 IPFail = (1 << 16), /* IP checksum failed */
575 UDPFail = (1 << 15), /* UDP/IP checksum failed */
576 TCPFail = (1 << 14), /* TCP/IP checksum failed */
577 RxVlanTag = (1 << 16), /* VLAN tag available */
578};
579
580#define RsvdMask 0x3fffc000
Heiner Kallweit12d42c52018-04-28 22:19:30 +0200581#define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200584 __le32 opts1;
585 __le32 opts2;
586 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587};
588
589struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200590 __le32 opts1;
591 __le32 opts2;
592 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593};
594
595struct ring_info {
596 struct sk_buff *skb;
597 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598};
599
Ivan Vecera355423d2009-02-06 21:49:57 -0800600struct rtl8169_counters {
601 __le64 tx_packets;
602 __le64 rx_packets;
603 __le64 tx_errors;
604 __le32 rx_errors;
605 __le16 rx_missed;
606 __le16 align_errors;
607 __le32 tx_one_collision;
608 __le32 tx_multi_collision;
609 __le64 rx_unicast;
610 __le64 rx_broadcast;
611 __le32 rx_multicast;
612 __le16 tx_aborted;
613 __le16 tx_underun;
614};
615
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200616struct rtl8169_tc_offsets {
617 bool inited;
618 __le64 tx_errors;
619 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200620 __le16 tx_aborted;
621};
622
Francois Romieuda78dbf2012-01-26 14:18:23 +0100623enum rtl_flag {
Kai-Heng Feng6ad56902018-09-11 01:51:43 +0800624 RTL_FLAG_TASK_ENABLED = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100625 RTL_FLAG_TASK_RESET_PENDING,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100626 RTL_FLAG_MAX
627};
628
Junchang Wang8027aa22012-03-04 23:30:32 +0100629struct rtl8169_stats {
630 u64 packets;
631 u64 bytes;
632 struct u64_stats_sync syncp;
633};
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635struct rtl8169_private {
636 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200637 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000638 struct net_device *dev;
Heiner Kallweit703732f2019-01-19 22:07:05 +0100639 struct phy_device *phydev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700640 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200641 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700642 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
644 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100646 struct rtl8169_stats rx_stats;
647 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
649 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
650 dma_addr_t TxPhyAddr;
651 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000652 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100655
Heiner Kallweit559c3c02018-11-19 22:34:17 +0100656 u16 irq_mask;
Francois Romieu50970832017-10-27 13:24:49 +0300657 const struct rtl_coalesce_info *coalesce_info;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +0200658 struct clk *clk;
françois romieuc0e45c12011-01-03 15:08:04 +0000659
660 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200661 void (*write)(struct rtl8169_private *, int, int);
662 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000663 } mdio_ops;
664
Francois Romieud58d46b2011-05-03 16:38:29 +0200665 struct jumbo_ops {
666 void (*enable)(struct rtl8169_private *);
667 void (*disable)(struct rtl8169_private *);
668 } jumbo_ops;
669
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200670 void (*hw_start)(struct rtl8169_private *tp);
hayeswang5888d3f2014-07-11 16:25:56 +0800671 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100672
673 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100674 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
675 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100676 struct work_struct work;
677 } wk;
678
Heiner Kallweitc8248c62019-03-21 21:23:14 +0100679 unsigned irq_enabled:1;
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +0200680 unsigned supports_gmii:1;
Corinna Vinschen42020322015-09-10 10:47:35 +0200681 dma_addr_t counters_phys_addr;
682 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200683 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000684 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000685
Heiner Kallweit254764e2019-01-22 22:23:41 +0100686 const char *fw_name;
Francois Romieub6ffd972011-06-17 17:00:05 +0200687 struct rtl_fw {
688 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200689
690#define RTL_VER_SIZE 32
691
692 char version[RTL_VER_SIZE];
693
694 struct rtl_fw_phy_action {
695 __le32 *code;
696 size_t size;
697 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200698 } *rtl_fw;
Hayes Wangc5583862012-07-02 17:23:22 +0800699
700 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701};
702
Heiner Kallweit1fcd1652019-04-14 10:30:24 +0200703typedef void (*rtl_generic_fct)(struct rtl8169_private *tp);
704
Ralf Baechle979b6c12005-06-13 14:30:40 -0700705MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200707module_param_named(debug, debug.msg_enable, int, 0);
708MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Heiner Kallweit11287b62019-01-07 21:49:09 +0100709MODULE_SOFTDEP("pre: realtek");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710MODULE_LICENSE("GPL");
françois romieubca03d52011-01-03 15:07:31 +0000711MODULE_FIRMWARE(FIRMWARE_8168D_1);
712MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000713MODULE_FIRMWARE(FIRMWARE_8168E_1);
714MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400715MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800716MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800717MODULE_FIRMWARE(FIRMWARE_8168F_1);
718MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800719MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800720MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800721MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800722MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000723MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000724MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000725MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800726MODULE_FIRMWARE(FIRMWARE_8168H_1);
727MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200728MODULE_FIRMWARE(FIRMWARE_8107E_1);
729MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100731static inline struct device *tp_to_dev(struct rtl8169_private *tp)
732{
733 return &tp->pci_dev->dev;
734}
735
Francois Romieuda78dbf2012-01-26 14:18:23 +0100736static void rtl_lock_work(struct rtl8169_private *tp)
737{
738 mutex_lock(&tp->wk.mutex);
739}
740
741static void rtl_unlock_work(struct rtl8169_private *tp)
742{
743 mutex_unlock(&tp->wk.mutex);
744}
745
Heiner Kallweitdf320ed2019-01-19 22:05:48 +0100746static void rtl_lock_config_regs(struct rtl8169_private *tp)
747{
748 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
749}
750
751static void rtl_unlock_config_regs(struct rtl8169_private *tp)
752{
753 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
754}
755
Heiner Kallweitcb732002018-03-20 07:45:35 +0100756static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200757{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100758 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800759 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200760}
761
Francois Romieuffc46952012-07-06 14:19:23 +0200762struct rtl_cond {
763 bool (*check)(struct rtl8169_private *);
764 const char *msg;
765};
766
767static void rtl_udelay(unsigned int d)
768{
769 udelay(d);
770}
771
772static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
773 void (*delay)(unsigned int), unsigned int d, int n,
774 bool high)
775{
776 int i;
777
778 for (i = 0; i < n; i++) {
Francois Romieuffc46952012-07-06 14:19:23 +0200779 if (c->check(tp) == high)
780 return true;
Heiner Kallweitd1f50502019-05-04 15:20:38 +0200781 delay(d);
Francois Romieuffc46952012-07-06 14:19:23 +0200782 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200783 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
784 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200785 return false;
786}
787
788static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
789 const struct rtl_cond *c,
790 unsigned int d, int n)
791{
792 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
793}
794
795static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
796 const struct rtl_cond *c,
797 unsigned int d, int n)
798{
799 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
800}
801
802static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
803 const struct rtl_cond *c,
804 unsigned int d, int n)
805{
806 return rtl_loop_wait(tp, c, msleep, d, n, true);
807}
808
809static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
810 const struct rtl_cond *c,
811 unsigned int d, int n)
812{
813 return rtl_loop_wait(tp, c, msleep, d, n, false);
814}
815
816#define DECLARE_RTL_COND(name) \
817static bool name ## _check(struct rtl8169_private *); \
818 \
819static const struct rtl_cond name = { \
820 .check = name ## _check, \
821 .msg = #name \
822}; \
823 \
824static bool name ## _check(struct rtl8169_private *tp)
825
Hayes Wangc5583862012-07-02 17:23:22 +0800826static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
827{
828 if (reg & 0xffff0001) {
829 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
830 return true;
831 }
832 return false;
833}
834
835DECLARE_RTL_COND(rtl_ocp_gphy_cond)
836{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200837 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800838}
839
840static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
841{
Hayes Wangc5583862012-07-02 17:23:22 +0800842 if (rtl_ocp_reg_failure(tp, reg))
843 return;
844
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200845 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800846
847 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
848}
849
850static u16 r8168_phy_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, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800856
857 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200858 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +0800859}
860
Hayes Wangc5583862012-07-02 17:23:22 +0800861static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
862{
Hayes Wangc5583862012-07-02 17:23:22 +0800863 if (rtl_ocp_reg_failure(tp, reg))
864 return;
865
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200866 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800867}
868
869static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
870{
Hayes Wangc5583862012-07-02 17:23:22 +0800871 if (rtl_ocp_reg_failure(tp, reg))
872 return 0;
873
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200874 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800875
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200876 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800877}
878
879#define OCP_STD_PHY_BASE 0xa400
880
881static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
882{
883 if (reg == 0x1f) {
884 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
885 return;
886 }
887
888 if (tp->ocp_base != OCP_STD_PHY_BASE)
889 reg -= 0x10;
890
891 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
892}
893
894static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
895{
896 if (tp->ocp_base != OCP_STD_PHY_BASE)
897 reg -= 0x10;
898
899 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
900}
901
hayeswangeee37862013-04-01 22:23:38 +0000902static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
903{
904 if (reg == 0x1f) {
905 tp->ocp_base = value << 4;
906 return;
907 }
908
909 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
910}
911
912static int mac_mcu_read(struct rtl8169_private *tp, int reg)
913{
914 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
915}
916
Francois Romieuffc46952012-07-06 14:19:23 +0200917DECLARE_RTL_COND(rtl_phyar_cond)
918{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200919 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200920}
921
Francois Romieu24192212012-07-06 20:19:42 +0200922static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200924 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Francois Romieuffc46952012-07-06 14:19:23 +0200926 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700927 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700928 * According to hardware specs a 20us delay is required after write
929 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700930 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700931 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Francois Romieu24192212012-07-06 20:19:42 +0200934static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Francois Romieuffc46952012-07-06 14:19:23 +0200936 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200938 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Francois Romieuffc46952012-07-06 14:19:23 +0200940 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200941 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +0200942
Timo Teräs81a95f02010-06-09 17:31:48 -0700943 /*
944 * According to hardware specs a 20us delay is required after read
945 * complete indication, but before sending next command.
946 */
947 udelay(20);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return value;
950}
951
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800952DECLARE_RTL_COND(rtl_ocpar_cond)
953{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200954 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800955}
956
Francois Romieu24192212012-07-06 20:19:42 +0200957static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000958{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200959 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
960 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
961 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000962
Francois Romieuffc46952012-07-06 14:19:23 +0200963 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +0000964}
965
Francois Romieu24192212012-07-06 20:19:42 +0200966static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000967{
Francois Romieu24192212012-07-06 20:19:42 +0200968 r8168dp_1_mdio_access(tp, reg,
969 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000970}
971
Francois Romieu24192212012-07-06 20:19:42 +0200972static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000973{
Francois Romieu24192212012-07-06 20:19:42 +0200974 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +0000975
976 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200977 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
978 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000979
Francois Romieuffc46952012-07-06 14:19:23 +0200980 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200981 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +0000982}
983
françois romieue6de30d2011-01-03 15:08:37 +0000984#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
985
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200986static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000987{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200988 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000989}
990
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200991static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000992{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200993 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000994}
995
Francois Romieu24192212012-07-06 20:19:42 +0200996static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +0000997{
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 r8169_mdio_write(tp, reg, value);
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
Francois Romieu24192212012-07-06 20:19:42 +02001005static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001006{
1007 int value;
1008
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001009 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001010
Francois Romieu24192212012-07-06 20:19:42 +02001011 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001012
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001013 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001014
1015 return value;
1016}
1017
françois romieu4da19632011-01-03 15:07:55 +00001018static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001019{
Francois Romieu24192212012-07-06 20:19:42 +02001020 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001021}
1022
françois romieu4da19632011-01-03 15:07:55 +00001023static int rtl_readphy(struct rtl8169_private *tp, int location)
1024{
Francois Romieu24192212012-07-06 20:19:42 +02001025 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001026}
1027
1028static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1029{
1030 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1031}
1032
Chun-Hao Lin76564422014-10-01 23:17:17 +08001033static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001034{
1035 int val;
1036
françois romieu4da19632011-01-03 15:07:55 +00001037 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001038 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001039}
1040
Francois Romieuffc46952012-07-06 14:19:23 +02001041DECLARE_RTL_COND(rtl_ephyar_cond)
1042{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001043 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001044}
1045
Francois Romieufdf6fc02012-07-06 22:40:38 +02001046static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001047{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001048 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001049 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1050
Francois Romieuffc46952012-07-06 14:19:23 +02001051 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1052
1053 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001054}
1055
Francois Romieufdf6fc02012-07-06 22:40:38 +02001056static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001057{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001058 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001059
Francois Romieuffc46952012-07-06 14:19:23 +02001060 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001061 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001062}
1063
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001064DECLARE_RTL_COND(rtl_eriar_cond)
1065{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001066 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001067}
1068
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001069static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1070 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001071{
Hayes Wang133ac402011-07-06 15:58:05 +08001072 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001073 RTL_W32(tp, ERIDR, val);
1074 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001075
Francois Romieuffc46952012-07-06 14:19:23 +02001076 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001077}
1078
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001079static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1080 u32 val)
1081{
1082 _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
1083}
1084
1085static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001086{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001087 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001088
Francois Romieuffc46952012-07-06 14:19:23 +02001089 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001090 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001091}
1092
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001093static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
1094{
1095 return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
1096}
1097
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001098static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001099 u32 m)
Hayes Wang133ac402011-07-06 15:58:05 +08001100{
1101 u32 val;
1102
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001103 val = rtl_eri_read(tp, addr);
1104 rtl_eri_write(tp, addr, mask, (val & ~m) | p);
Hayes Wang133ac402011-07-06 15:58:05 +08001105}
1106
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001107static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 mask,
1108 u32 p)
1109{
1110 rtl_w0w1_eri(tp, addr, mask, p, 0);
1111}
1112
1113static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 mask,
1114 u32 m)
1115{
1116 rtl_w0w1_eri(tp, addr, mask, 0, m);
1117}
1118
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001119static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1120{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001121 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001122 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001123 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001124}
1125
1126static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1127{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001128 return _rtl_eri_read(tp, reg, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001129}
1130
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001131static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1132 u32 data)
1133{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001134 RTL_W32(tp, OCPDR, data);
1135 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001136 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1137}
1138
1139static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1140 u32 data)
1141{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001142 _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1143 data, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001144}
1145
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001146static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001147{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001148 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001149
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001150 r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001151}
1152
1153#define OOB_CMD_RESET 0x00
1154#define OOB_CMD_DRIVER_START 0x05
1155#define OOB_CMD_DRIVER_STOP 0x06
1156
1157static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1158{
1159 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1160}
1161
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001162DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001163{
1164 u16 reg;
1165
1166 reg = rtl8168_get_ocp_reg(tp);
1167
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001168 return r8168dp_ocp_read(tp, 0x0f, reg) & 0x00000800;
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001169}
1170
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001171DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1172{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001173 return r8168ep_ocp_read(tp, 0x0f, 0x124) & 0x00000001;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001174}
1175
1176DECLARE_RTL_COND(rtl_ocp_tx_cond)
1177{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001178 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001179}
1180
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001181static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1182{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001183 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001184 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001185 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1186 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001187}
1188
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001189static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001190{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001191 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1192 rtl_msleep_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001193}
1194
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001195static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1196{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001197 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1198 r8168ep_ocp_write(tp, 0x01, 0x30,
1199 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001200 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1201}
1202
1203static void rtl8168_driver_start(struct rtl8169_private *tp)
1204{
1205 switch (tp->mac_version) {
1206 case RTL_GIGA_MAC_VER_27:
1207 case RTL_GIGA_MAC_VER_28:
1208 case RTL_GIGA_MAC_VER_31:
1209 rtl8168dp_driver_start(tp);
1210 break;
1211 case RTL_GIGA_MAC_VER_49:
1212 case RTL_GIGA_MAC_VER_50:
1213 case RTL_GIGA_MAC_VER_51:
1214 rtl8168ep_driver_start(tp);
1215 break;
1216 default:
1217 BUG();
1218 break;
1219 }
1220}
1221
1222static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1223{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001224 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1225 rtl_msleep_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001226}
1227
1228static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1229{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001230 rtl8168ep_stop_cmac(tp);
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001231 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1232 r8168ep_ocp_write(tp, 0x01, 0x30,
1233 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001234 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1235}
1236
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001237static void rtl8168_driver_stop(struct rtl8169_private *tp)
1238{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001239 switch (tp->mac_version) {
1240 case RTL_GIGA_MAC_VER_27:
1241 case RTL_GIGA_MAC_VER_28:
1242 case RTL_GIGA_MAC_VER_31:
1243 rtl8168dp_driver_stop(tp);
1244 break;
1245 case RTL_GIGA_MAC_VER_49:
1246 case RTL_GIGA_MAC_VER_50:
1247 case RTL_GIGA_MAC_VER_51:
1248 rtl8168ep_driver_stop(tp);
1249 break;
1250 default:
1251 BUG();
1252 break;
1253 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001254}
1255
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001256static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001257{
1258 u16 reg = rtl8168_get_ocp_reg(tp);
1259
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001260 return !!(r8168dp_ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001261}
1262
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001263static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001264{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001265 return !!(r8168ep_ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001266}
1267
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001268static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001269{
1270 switch (tp->mac_version) {
1271 case RTL_GIGA_MAC_VER_27:
1272 case RTL_GIGA_MAC_VER_28:
1273 case RTL_GIGA_MAC_VER_31:
1274 return r8168dp_check_dash(tp);
1275 case RTL_GIGA_MAC_VER_49:
1276 case RTL_GIGA_MAC_VER_50:
1277 case RTL_GIGA_MAC_VER_51:
1278 return r8168ep_check_dash(tp);
1279 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001280 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001281 }
1282}
1283
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001284static void rtl_reset_packet_filter(struct rtl8169_private *tp)
1285{
1286 rtl_eri_clear_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1287 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1288}
1289
Francois Romieuffc46952012-07-06 14:19:23 +02001290DECLARE_RTL_COND(rtl_efusear_cond)
1291{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001292 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001293}
1294
Francois Romieufdf6fc02012-07-06 22:40:38 +02001295static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001296{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001297 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001298
Francois Romieuffc46952012-07-06 14:19:23 +02001299 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001300 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001301}
1302
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001303static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1304{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001305 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001306}
1307
1308static void rtl_irq_disable(struct rtl8169_private *tp)
1309{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001310 RTL_W16(tp, IntrMask, 0);
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001311 tp->irq_enabled = 0;
Francois Romieu3e990ff2012-01-26 12:50:01 +01001312}
1313
Francois Romieuda78dbf2012-01-26 14:18:23 +01001314#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1315#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1316#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1317
Heiner Kallweitfe716f82018-11-19 22:31:32 +01001318static void rtl_irq_enable(struct rtl8169_private *tp)
Francois Romieuda78dbf2012-01-26 14:18:23 +01001319{
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001320 tp->irq_enabled = 1;
Heiner Kallweit559c3c02018-11-19 22:34:17 +01001321 RTL_W16(tp, IntrMask, tp->irq_mask);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001322}
1323
françois romieu811fd302011-12-04 20:30:45 +00001324static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001326 rtl_irq_disable(tp);
Heiner Kallweitde20e122018-09-25 07:58:00 +02001327 rtl_ack_events(tp, 0xffff);
1328 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001329 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Hayes Wang70090422011-07-06 15:58:06 +08001332static void rtl_link_chg_patch(struct rtl8169_private *tp)
1333{
Hayes Wang70090422011-07-06 15:58:06 +08001334 struct net_device *dev = tp->dev;
Heiner Kallweit703732f2019-01-19 22:07:05 +01001335 struct phy_device *phydev = tp->phydev;
Hayes Wang70090422011-07-06 15:58:06 +08001336
1337 if (!netif_running(dev))
1338 return;
1339
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001340 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1341 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001342 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001343 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1344 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001345 } else if (phydev->speed == SPEED_100) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001346 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1347 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wang70090422011-07-06 15:58:06 +08001348 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001349 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1350 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wang70090422011-07-06 15:58:06 +08001351 }
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001352 rtl_reset_packet_filter(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08001353 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1354 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001355 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001356 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1357 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wangc2218922011-09-06 16:55:18 +08001358 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001359 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1360 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wangc2218922011-09-06 16:55:18 +08001361 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001362 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001363 if (phydev->speed == SPEED_10) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001364 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
1365 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001366 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001367 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001368 }
Hayes Wang70090422011-07-06 15:58:06 +08001369 }
1370}
1371
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001372#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1373
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001374static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1375{
1376 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001377
Francois Romieuda78dbf2012-01-26 14:18:23 +01001378 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001379 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001380 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001381 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001382}
1383
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001384static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001385{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001386 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001387 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001388 u32 opt;
1389 u16 reg;
1390 u8 mask;
1391 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001392 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001393 { WAKE_UCAST, Config5, UWF },
1394 { WAKE_BCAST, Config5, BWF },
1395 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001396 { WAKE_ANY, Config5, LanWake },
1397 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001398 };
Francois Romieu851e6022012-04-17 11:10:11 +02001399 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001400
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001401 rtl_unlock_config_regs(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001402
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001403 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001404 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1405 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001406 tmp = ARRAY_SIZE(cfg) - 1;
1407 if (wolopts & WAKE_MAGIC)
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001408 rtl_eri_set_bits(tp, 0x0dc, ERIAR_MASK_0100,
1409 MagicPacket_v2);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001410 else
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001411 rtl_eri_clear_bits(tp, 0x0dc, ERIAR_MASK_0100,
1412 MagicPacket_v2);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001413 break;
1414 default:
1415 tmp = ARRAY_SIZE(cfg);
1416 break;
1417 }
1418
1419 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001420 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001421 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001422 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001423 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001424 }
1425
Francois Romieu851e6022012-04-17 11:10:11 +02001426 switch (tp->mac_version) {
1427 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001428 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001429 if (wolopts)
1430 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001431 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001432 break;
1433 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001434 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001435 if (wolopts)
1436 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001437 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001438 break;
1439 }
1440
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001441 rtl_lock_config_regs(tp);
Heiner Kallweit3bd82642018-12-30 13:16:12 +01001442
1443 device_set_wakeup_enable(tp_to_dev(tp), wolopts);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001444}
1445
1446static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1447{
1448 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001449 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001450
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001451 if (wol->wolopts & ~WAKE_ANY)
1452 return -EINVAL;
1453
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001454 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001455
Francois Romieuda78dbf2012-01-26 14:18:23 +01001456 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001457
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001458 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001459
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001460 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001461 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001462
1463 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001464
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001465 pm_runtime_put_noidle(d);
1466
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001467 return 0;
1468}
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470static void rtl8169_get_drvinfo(struct net_device *dev,
1471 struct ethtool_drvinfo *info)
1472{
1473 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001474 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Rick Jones68aad782011-11-07 13:29:27 +00001476 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001477 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001478 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Heiner Kallweit254764e2019-01-22 22:23:41 +01001479 if (rtl_fw)
Rick Jones8ac72d12011-11-22 14:06:26 +00001480 strlcpy(info->fw_version, rtl_fw->version,
1481 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484static int rtl8169_get_regs_len(struct net_device *dev)
1485{
1486 return R8169_REGS_SIZE;
1487}
1488
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001489static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1490 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Francois Romieud58d46b2011-05-03 16:38:29 +02001492 struct rtl8169_private *tp = netdev_priv(dev);
1493
Francois Romieu2b7b4312011-04-18 22:53:24 -07001494 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001495 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Francois Romieud58d46b2011-05-03 16:38:29 +02001497 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001498 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001499 features &= ~NETIF_F_IP_CSUM;
1500
Michał Mirosław350fb322011-04-08 06:35:56 +00001501 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Heiner Kallweita3984572018-04-28 22:19:15 +02001504static int rtl8169_set_features(struct net_device *dev,
1505 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001508 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Heiner Kallweita3984572018-04-28 22:19:15 +02001510 rtl_lock_work(tp);
1511
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001512 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001513 if (features & NETIF_F_RXALL)
1514 rx_config |= (AcceptErr | AcceptRunt);
1515 else
1516 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001518 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001519
hayeswang929a0312014-09-16 11:40:47 +08001520 if (features & NETIF_F_RXCSUM)
1521 tp->cp_cmd |= RxChkSum;
1522 else
1523 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001524
hayeswang929a0312014-09-16 11:40:47 +08001525 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1526 tp->cp_cmd |= RxVlan;
1527 else
1528 tp->cp_cmd &= ~RxVlan;
1529
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001530 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1531 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Francois Romieuda78dbf2012-01-26 14:18:23 +01001533 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 return 0;
1536}
1537
Kirill Smelkov810f4892012-11-10 21:11:02 +04001538static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001540 return (skb_vlan_tag_present(skb)) ?
1541 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Francois Romieu7a8fc772011-03-01 17:18:33 +01001544static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Francois Romieu7a8fc772011-03-01 17:18:33 +01001548 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001549 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1553 void *p)
1554{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001555 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001556 u32 __iomem *data = tp->mmio_addr;
1557 u32 *dw = p;
1558 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Francois Romieuda78dbf2012-01-26 14:18:23 +01001560 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001561 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1562 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001563 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001566static u32 rtl8169_get_msglevel(struct net_device *dev)
1567{
1568 struct rtl8169_private *tp = netdev_priv(dev);
1569
1570 return tp->msg_enable;
1571}
1572
1573static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1574{
1575 struct rtl8169_private *tp = netdev_priv(dev);
1576
1577 tp->msg_enable = value;
1578}
1579
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001580static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1581 "tx_packets",
1582 "rx_packets",
1583 "tx_errors",
1584 "rx_errors",
1585 "rx_missed",
1586 "align_errors",
1587 "tx_single_collisions",
1588 "tx_multi_collisions",
1589 "unicast",
1590 "broadcast",
1591 "multicast",
1592 "tx_aborted",
1593 "tx_underrun",
1594};
1595
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001596static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001597{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001598 switch (sset) {
1599 case ETH_SS_STATS:
1600 return ARRAY_SIZE(rtl8169_gstrings);
1601 default:
1602 return -EOPNOTSUPP;
1603 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001604}
1605
Corinna Vinschen42020322015-09-10 10:47:35 +02001606DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001607{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001608 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001609}
1610
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001611static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001612{
Corinna Vinschen42020322015-09-10 10:47:35 +02001613 dma_addr_t paddr = tp->counters_phys_addr;
1614 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001615
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001616 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1617 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001618 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001619 RTL_W32(tp, CounterAddrLow, cmd);
1620 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001621
Francois Romieua78e9362018-01-26 01:53:26 +01001622 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001623}
1624
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001625static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001626{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001627 /*
1628 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1629 * tally counters.
1630 */
1631 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1632 return true;
1633
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001634 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001635}
1636
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001637static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001638{
Heiner Kallweit10262b02019-01-06 20:44:00 +01001639 u8 val = RTL_R8(tp, ChipCmd);
1640
Ivan Vecera355423d2009-02-06 21:49:57 -08001641 /*
1642 * Some chips are unable to dump tally counters when the receiver
Heiner Kallweit10262b02019-01-06 20:44:00 +01001643 * is disabled. If 0xff chip may be in a PCI power-save state.
Ivan Vecera355423d2009-02-06 21:49:57 -08001644 */
Heiner Kallweit10262b02019-01-06 20:44:00 +01001645 if (!(val & CmdRxEnb) || val == 0xff)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001646 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001647
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001648 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001649}
1650
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001651static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001652{
Corinna Vinschen42020322015-09-10 10:47:35 +02001653 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001654 bool ret = false;
1655
1656 /*
1657 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1658 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1659 * reset by a power cycle, while the counter values collected by the
1660 * driver are reset at every driver unload/load cycle.
1661 *
1662 * To make sure the HW values returned by @get_stats64 match the SW
1663 * values, we collect the initial values at first open(*) and use them
1664 * as offsets to normalize the values returned by @get_stats64.
1665 *
1666 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1667 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1668 * set at open time by rtl_hw_start.
1669 */
1670
1671 if (tp->tc_offset.inited)
1672 return true;
1673
1674 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001675 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001676 ret = true;
1677
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001678 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001679 ret = true;
1680
Corinna Vinschen42020322015-09-10 10:47:35 +02001681 tp->tc_offset.tx_errors = counters->tx_errors;
1682 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1683 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001684 tp->tc_offset.inited = true;
1685
1686 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001687}
1688
Ivan Vecera355423d2009-02-06 21:49:57 -08001689static void rtl8169_get_ethtool_stats(struct net_device *dev,
1690 struct ethtool_stats *stats, u64 *data)
1691{
1692 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001693 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02001694 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08001695
1696 ASSERT_RTNL();
1697
Chun-Hao Line0636232016-07-29 16:37:55 +08001698 pm_runtime_get_noresume(d);
1699
1700 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001701 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08001702
1703 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08001704
Corinna Vinschen42020322015-09-10 10:47:35 +02001705 data[0] = le64_to_cpu(counters->tx_packets);
1706 data[1] = le64_to_cpu(counters->rx_packets);
1707 data[2] = le64_to_cpu(counters->tx_errors);
1708 data[3] = le32_to_cpu(counters->rx_errors);
1709 data[4] = le16_to_cpu(counters->rx_missed);
1710 data[5] = le16_to_cpu(counters->align_errors);
1711 data[6] = le32_to_cpu(counters->tx_one_collision);
1712 data[7] = le32_to_cpu(counters->tx_multi_collision);
1713 data[8] = le64_to_cpu(counters->rx_unicast);
1714 data[9] = le64_to_cpu(counters->rx_broadcast);
1715 data[10] = le32_to_cpu(counters->rx_multicast);
1716 data[11] = le16_to_cpu(counters->tx_aborted);
1717 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08001718}
1719
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001720static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1721{
1722 switch(stringset) {
1723 case ETH_SS_STATS:
1724 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1725 break;
1726 }
1727}
1728
Francois Romieu50970832017-10-27 13:24:49 +03001729/*
1730 * Interrupt coalescing
1731 *
1732 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1733 * > 8169, 8168 and 810x line of chipsets
1734 *
1735 * 8169, 8168, and 8136(810x) serial chipsets support it.
1736 *
1737 * > 2 - the Tx timer unit at gigabit speed
1738 *
1739 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1740 * (0xe0) bit 1 and bit 0.
1741 *
1742 * For 8169
1743 * bit[1:0] \ speed 1000M 100M 10M
1744 * 0 0 320ns 2.56us 40.96us
1745 * 0 1 2.56us 20.48us 327.7us
1746 * 1 0 5.12us 40.96us 655.4us
1747 * 1 1 10.24us 81.92us 1.31ms
1748 *
1749 * For the other
1750 * bit[1:0] \ speed 1000M 100M 10M
1751 * 0 0 5us 2.56us 40.96us
1752 * 0 1 40us 20.48us 327.7us
1753 * 1 0 80us 40.96us 655.4us
1754 * 1 1 160us 81.92us 1.31ms
1755 */
1756
1757/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1758struct rtl_coalesce_scale {
1759 /* Rx / Tx */
1760 u32 nsecs[2];
1761};
1762
1763/* rx/tx scale factors for all CPlusCmd[0:1] cases */
1764struct rtl_coalesce_info {
1765 u32 speed;
1766 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1767};
1768
1769/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1770#define rxtx_x1822(r, t) { \
1771 {{(r), (t)}}, \
1772 {{(r)*8, (t)*8}}, \
1773 {{(r)*8*2, (t)*8*2}}, \
1774 {{(r)*8*2*2, (t)*8*2*2}}, \
1775}
1776static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1777 /* speed delays: rx00 tx00 */
1778 { SPEED_10, rxtx_x1822(40960, 40960) },
1779 { SPEED_100, rxtx_x1822( 2560, 2560) },
1780 { SPEED_1000, rxtx_x1822( 320, 320) },
1781 { 0 },
1782};
1783
1784static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1785 /* speed delays: rx00 tx00 */
1786 { SPEED_10, rxtx_x1822(40960, 40960) },
1787 { SPEED_100, rxtx_x1822( 2560, 2560) },
1788 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1789 { 0 },
1790};
1791#undef rxtx_x1822
1792
1793/* get rx/tx scale vector corresponding to current speed */
1794static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1795{
1796 struct rtl8169_private *tp = netdev_priv(dev);
1797 struct ethtool_link_ksettings ecmd;
1798 const struct rtl_coalesce_info *ci;
1799 int rc;
1800
Heiner Kallweit45772432018-07-17 22:51:44 +02001801 rc = phy_ethtool_get_link_ksettings(dev, &ecmd);
Francois Romieu50970832017-10-27 13:24:49 +03001802 if (rc < 0)
1803 return ERR_PTR(rc);
1804
1805 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
1806 if (ecmd.base.speed == ci->speed) {
1807 return ci;
1808 }
1809 }
1810
1811 return ERR_PTR(-ELNRNG);
1812}
1813
1814static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1815{
1816 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001817 const struct rtl_coalesce_info *ci;
1818 const struct rtl_coalesce_scale *scale;
1819 struct {
1820 u32 *max_frames;
1821 u32 *usecs;
1822 } coal_settings [] = {
1823 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1824 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1825 }, *p = coal_settings;
1826 int i;
1827 u16 w;
1828
1829 memset(ec, 0, sizeof(*ec));
1830
1831 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1832 ci = rtl_coalesce_info(dev);
1833 if (IS_ERR(ci))
1834 return PTR_ERR(ci);
1835
Heiner Kallweit0ae09742018-04-28 22:19:26 +02001836 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03001837
1838 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001839 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03001840 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1841 w >>= RTL_COALESCE_SHIFT;
1842 *p->usecs = w & RTL_COALESCE_MASK;
1843 }
1844
1845 for (i = 0; i < 2; i++) {
1846 p = coal_settings + i;
1847 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1848
1849 /*
1850 * ethtool_coalesce says it is illegal to set both usecs and
1851 * max_frames to 0.
1852 */
1853 if (!*p->usecs && !*p->max_frames)
1854 *p->max_frames = 1;
1855 }
1856
1857 return 0;
1858}
1859
1860/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1861static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1862 struct net_device *dev, u32 nsec, u16 *cp01)
1863{
1864 const struct rtl_coalesce_info *ci;
1865 u16 i;
1866
1867 ci = rtl_coalesce_info(dev);
1868 if (IS_ERR(ci))
1869 return ERR_CAST(ci);
1870
1871 for (i = 0; i < 4; i++) {
1872 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1873 ci->scalev[i].nsecs[1]);
1874 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1875 *cp01 = i;
1876 return &ci->scalev[i];
1877 }
1878 }
1879
1880 return ERR_PTR(-EINVAL);
1881}
1882
1883static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1884{
1885 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001886 const struct rtl_coalesce_scale *scale;
1887 struct {
1888 u32 frames;
1889 u32 usecs;
1890 } coal_settings [] = {
1891 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1892 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1893 }, *p = coal_settings;
1894 u16 w = 0, cp01;
1895 int i;
1896
1897 scale = rtl_coalesce_choose_scale(dev,
1898 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1899 if (IS_ERR(scale))
1900 return PTR_ERR(scale);
1901
1902 for (i = 0; i < 2; i++, p++) {
1903 u32 units;
1904
1905 /*
1906 * accept max_frames=1 we returned in rtl_get_coalesce.
1907 * accept it not only when usecs=0 because of e.g. the following scenario:
1908 *
1909 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
1910 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
1911 * - then user does `ethtool -C eth0 rx-usecs 100`
1912 *
1913 * since ethtool sends to kernel whole ethtool_coalesce
1914 * settings, if we do not handle rx_usecs=!0, rx_frames=1
1915 * we'll reject it below in `frames % 4 != 0`.
1916 */
1917 if (p->frames == 1) {
1918 p->frames = 0;
1919 }
1920
1921 units = p->usecs * 1000 / scale->nsecs[i];
1922 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
1923 return -EINVAL;
1924
1925 w <<= RTL_COALESCE_SHIFT;
1926 w |= units;
1927 w <<= RTL_COALESCE_SHIFT;
1928 w |= p->frames >> 2;
1929 }
1930
1931 rtl_lock_work(tp);
1932
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001933 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03001934
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02001935 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001936 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1937 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03001938
1939 rtl_unlock_work(tp);
1940
1941 return 0;
1942}
1943
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001944static int rtl_get_eee_supp(struct rtl8169_private *tp)
1945{
1946 struct phy_device *phydev = tp->phydev;
1947 int ret;
1948
1949 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01001950 case RTL_GIGA_MAC_VER_34:
1951 case RTL_GIGA_MAC_VER_35:
1952 case RTL_GIGA_MAC_VER_36:
1953 case RTL_GIGA_MAC_VER_38:
1954 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1955 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001956 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1957 phy_write(phydev, 0x1f, 0x0a5c);
1958 ret = phy_read(phydev, 0x12);
1959 phy_write(phydev, 0x1f, 0x0000);
1960 break;
1961 default:
1962 ret = -EPROTONOSUPPORT;
1963 break;
1964 }
1965
1966 return ret;
1967}
1968
1969static int rtl_get_eee_lpadv(struct rtl8169_private *tp)
1970{
1971 struct phy_device *phydev = tp->phydev;
1972 int ret;
1973
1974 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01001975 case RTL_GIGA_MAC_VER_34:
1976 case RTL_GIGA_MAC_VER_35:
1977 case RTL_GIGA_MAC_VER_36:
1978 case RTL_GIGA_MAC_VER_38:
1979 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1980 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001981 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1982 phy_write(phydev, 0x1f, 0x0a5d);
1983 ret = phy_read(phydev, 0x11);
1984 phy_write(phydev, 0x1f, 0x0000);
1985 break;
1986 default:
1987 ret = -EPROTONOSUPPORT;
1988 break;
1989 }
1990
1991 return ret;
1992}
1993
1994static int rtl_get_eee_adv(struct rtl8169_private *tp)
1995{
1996 struct phy_device *phydev = tp->phydev;
1997 int ret;
1998
1999 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002000 case RTL_GIGA_MAC_VER_34:
2001 case RTL_GIGA_MAC_VER_35:
2002 case RTL_GIGA_MAC_VER_36:
2003 case RTL_GIGA_MAC_VER_38:
2004 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
2005 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002006 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
2007 phy_write(phydev, 0x1f, 0x0a5d);
2008 ret = phy_read(phydev, 0x10);
2009 phy_write(phydev, 0x1f, 0x0000);
2010 break;
2011 default:
2012 ret = -EPROTONOSUPPORT;
2013 break;
2014 }
2015
2016 return ret;
2017}
2018
2019static int rtl_set_eee_adv(struct rtl8169_private *tp, int val)
2020{
2021 struct phy_device *phydev = tp->phydev;
2022 int ret = 0;
2023
2024 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002025 case RTL_GIGA_MAC_VER_34:
2026 case RTL_GIGA_MAC_VER_35:
2027 case RTL_GIGA_MAC_VER_36:
2028 case RTL_GIGA_MAC_VER_38:
2029 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
2030 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002031 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
2032 phy_write(phydev, 0x1f, 0x0a5d);
2033 phy_write(phydev, 0x10, val);
2034 phy_write(phydev, 0x1f, 0x0000);
2035 break;
2036 default:
2037 ret = -EPROTONOSUPPORT;
2038 break;
2039 }
2040
2041 return ret;
2042}
2043
2044static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
2045{
2046 struct rtl8169_private *tp = netdev_priv(dev);
2047 struct device *d = tp_to_dev(tp);
2048 int ret;
2049
2050 pm_runtime_get_noresume(d);
2051
2052 if (!pm_runtime_active(d)) {
2053 ret = -EOPNOTSUPP;
2054 goto out;
2055 }
2056
2057 /* Get Supported EEE */
2058 ret = rtl_get_eee_supp(tp);
2059 if (ret < 0)
2060 goto out;
2061 data->supported = mmd_eee_cap_to_ethtool_sup_t(ret);
2062
2063 /* Get advertisement EEE */
2064 ret = rtl_get_eee_adv(tp);
2065 if (ret < 0)
2066 goto out;
2067 data->advertised = mmd_eee_adv_to_ethtool_adv_t(ret);
2068 data->eee_enabled = !!data->advertised;
2069
2070 /* Get LP advertisement EEE */
2071 ret = rtl_get_eee_lpadv(tp);
2072 if (ret < 0)
2073 goto out;
2074 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(ret);
2075 data->eee_active = !!(data->advertised & data->lp_advertised);
2076out:
2077 pm_runtime_put_noidle(d);
2078 return ret < 0 ? ret : 0;
2079}
2080
2081static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
2082{
2083 struct rtl8169_private *tp = netdev_priv(dev);
2084 struct device *d = tp_to_dev(tp);
2085 int old_adv, adv = 0, cap, ret;
2086
2087 pm_runtime_get_noresume(d);
2088
2089 if (!dev->phydev || !pm_runtime_active(d)) {
2090 ret = -EOPNOTSUPP;
2091 goto out;
2092 }
2093
2094 if (dev->phydev->autoneg == AUTONEG_DISABLE ||
2095 dev->phydev->duplex != DUPLEX_FULL) {
2096 ret = -EPROTONOSUPPORT;
2097 goto out;
2098 }
2099
2100 /* Get Supported EEE */
2101 ret = rtl_get_eee_supp(tp);
2102 if (ret < 0)
2103 goto out;
2104 cap = ret;
2105
2106 ret = rtl_get_eee_adv(tp);
2107 if (ret < 0)
2108 goto out;
2109 old_adv = ret;
2110
2111 if (data->eee_enabled) {
2112 adv = !data->advertised ? cap :
2113 ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
2114 /* Mask prohibited EEE modes */
2115 adv &= ~dev->phydev->eee_broken_modes;
2116 }
2117
2118 if (old_adv != adv) {
2119 ret = rtl_set_eee_adv(tp, adv);
2120 if (ret < 0)
2121 goto out;
2122
2123 /* Restart autonegotiation so the new modes get sent to the
2124 * link partner.
2125 */
2126 ret = phy_restart_aneg(dev->phydev);
2127 }
2128
2129out:
2130 pm_runtime_put_noidle(d);
2131 return ret < 0 ? ret : 0;
2132}
2133
Jeff Garzik7282d492006-09-13 14:30:00 -04002134static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 .get_drvinfo = rtl8169_get_drvinfo,
2136 .get_regs_len = rtl8169_get_regs_len,
2137 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002138 .get_coalesce = rtl_get_coalesce,
2139 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002140 .get_msglevel = rtl8169_get_msglevel,
2141 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002143 .get_wol = rtl8169_get_wol,
2144 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002145 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002146 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002147 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002148 .get_ts_info = ethtool_op_get_ts_info,
Heiner Kallweitdd849572018-07-17 22:51:48 +02002149 .nway_reset = phy_ethtool_nway_reset,
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002150 .get_eee = rtl8169_get_eee,
2151 .set_eee = rtl8169_set_eee,
Heiner Kallweit45772432018-07-17 22:51:44 +02002152 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2153 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154};
2155
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002156static void rtl_enable_eee(struct rtl8169_private *tp)
2157{
2158 int supported = rtl_get_eee_supp(tp);
2159
2160 if (supported > 0)
2161 rtl_set_eee_adv(tp, supported);
2162}
2163
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002164static void rtl8169_get_mac_version(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Francois Romieu0e485152007-02-20 00:00:26 +01002166 /*
2167 * The driver currently handles the 8168Bf and the 8168Be identically
2168 * but they can be identified more specifically through the test below
2169 * if needed:
2170 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002171 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002172 *
2173 * Same thing for the 8101Eb and the 8101Ec:
2174 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002175 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002176 */
Francois Romieu37441002011-06-17 22:58:54 +02002177 static const struct rtl_mac_info {
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002178 u16 mask;
2179 u16 val;
2180 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002182 /* 8168EP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002183 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
2184 { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
2185 { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002186
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002187 /* 8168H family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002188 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
2189 { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002190
Hayes Wangc5583862012-07-02 17:23:22 +08002191 /* 8168G family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002192 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
2193 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
2194 { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
2195 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
Hayes Wangc5583862012-07-02 17:23:22 +08002196
Hayes Wangc2218922011-09-06 16:55:18 +08002197 /* 8168F family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002198 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
2199 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
2200 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
Hayes Wangc2218922011-09-06 16:55:18 +08002201
hayeswang01dc7fe2011-03-21 01:50:28 +00002202 /* 8168E family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002203 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
2204 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
2205 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002206
Francois Romieu5b538df2008-07-20 16:22:45 +02002207 /* 8168D family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002208 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
2209 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002210
françois romieue6de30d2011-01-03 15:08:37 +00002211 /* 8168DP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002212 { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
2213 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
2214 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002215
Francois Romieuef808d52008-06-29 13:10:54 +02002216 /* 8168C family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002217 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
2218 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
2219 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
2220 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
2221 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
2222 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
2223 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002224
2225 /* 8168B family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002226 { 0x7cf, 0x380, RTL_GIGA_MAC_VER_12 },
2227 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
2228 { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002229
2230 /* 8101 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002231 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
2232 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
2233 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
2234 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
2235 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
2236 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
2237 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
2238 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
2239 { 0x7cf, 0x340, RTL_GIGA_MAC_VER_13 },
2240 { 0x7cf, 0x343, RTL_GIGA_MAC_VER_10 },
2241 { 0x7cf, 0x342, RTL_GIGA_MAC_VER_16 },
2242 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
2243 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
2244 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_16 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002245 /* FIXME: where did these entries come from ? -- FR */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002246 { 0xfc8, 0x388, RTL_GIGA_MAC_VER_15 },
2247 { 0xfc8, 0x308, RTL_GIGA_MAC_VER_14 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002248
2249 /* 8110 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002250 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
2251 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
2252 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
2253 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
2254 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
2255 { 0xfc8, 0x000, RTL_GIGA_MAC_VER_01 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002256
Jean Delvaref21b75e2009-05-26 20:54:48 -07002257 /* Catch-all */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002258 { 0x000, 0x000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002259 };
2260 const struct rtl_mac_info *p = mac_info;
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002261 u16 reg = RTL_R32(tp, TxConfig) >> 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002263 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 p++;
2265 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002266
2267 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002268 dev_err(tp_to_dev(tp), "unknown chip XID %03x\n", reg & 0xfcf);
Heiner Kallweit45f19962018-11-22 22:00:10 +01002269 } else if (!tp->supports_gmii) {
2270 if (tp->mac_version == RTL_GIGA_MAC_VER_42)
2271 tp->mac_version = RTL_GIGA_MAC_VER_43;
2272 else if (tp->mac_version == RTL_GIGA_MAC_VER_45)
2273 tp->mac_version = RTL_GIGA_MAC_VER_47;
2274 else if (tp->mac_version == RTL_GIGA_MAC_VER_46)
2275 tp->mac_version = RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
Francois Romieu867763c2007-08-17 18:21:58 +02002279struct phy_reg {
2280 u16 reg;
2281 u16 val;
2282};
2283
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002284static void __rtl_writephy_batch(struct rtl8169_private *tp,
2285 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002286{
2287 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002288 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002289 regs++;
2290 }
2291}
2292
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002293#define rtl_writephy_batch(tp, a) __rtl_writephy_batch(tp, a, ARRAY_SIZE(a))
2294
françois romieubca03d52011-01-03 15:07:31 +00002295#define PHY_READ 0x00000000
2296#define PHY_DATA_OR 0x10000000
2297#define PHY_DATA_AND 0x20000000
2298#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002299#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002300#define PHY_CLEAR_READCOUNT 0x70000000
2301#define PHY_WRITE 0x80000000
2302#define PHY_READCOUNT_EQ_SKIP 0x90000000
2303#define PHY_COMP_EQ_SKIPN 0xa0000000
2304#define PHY_COMP_NEQ_SKIPN 0xb0000000
2305#define PHY_WRITE_PREVIOUS 0xc0000000
2306#define PHY_SKIPN 0xd0000000
2307#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002308
Hayes Wang960aee62011-06-18 11:37:48 +02002309struct fw_info {
2310 u32 magic;
2311 char version[RTL_VER_SIZE];
2312 __le32 fw_start;
2313 __le32 fw_len;
2314 u8 chksum;
2315} __packed;
2316
Francois Romieu1c361ef2011-06-17 17:16:24 +02002317#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2318
2319static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002320{
Francois Romieub6ffd972011-06-17 17:00:05 +02002321 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002322 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002323 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2324 char *version = rtl_fw->version;
2325 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002326
Francois Romieu1c361ef2011-06-17 17:16:24 +02002327 if (fw->size < FW_OPCODE_SIZE)
2328 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002329
2330 if (!fw_info->magic) {
2331 size_t i, size, start;
2332 u8 checksum = 0;
2333
2334 if (fw->size < sizeof(*fw_info))
2335 goto out;
2336
2337 for (i = 0; i < fw->size; i++)
2338 checksum += fw->data[i];
2339 if (checksum != 0)
2340 goto out;
2341
2342 start = le32_to_cpu(fw_info->fw_start);
2343 if (start > fw->size)
2344 goto out;
2345
2346 size = le32_to_cpu(fw_info->fw_len);
2347 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2348 goto out;
2349
2350 memcpy(version, fw_info->version, RTL_VER_SIZE);
2351
2352 pa->code = (__le32 *)(fw->data + start);
2353 pa->size = size;
2354 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002355 if (fw->size % FW_OPCODE_SIZE)
2356 goto out;
2357
Heiner Kallweit254764e2019-01-22 22:23:41 +01002358 strlcpy(version, tp->fw_name, RTL_VER_SIZE);
Francois Romieu1c361ef2011-06-17 17:16:24 +02002359
2360 pa->code = (__le32 *)fw->data;
2361 pa->size = fw->size / FW_OPCODE_SIZE;
2362 }
2363 version[RTL_VER_SIZE - 1] = 0;
2364
2365 rc = true;
2366out:
2367 return rc;
2368}
2369
Francois Romieufd112f22011-06-18 00:10:29 +02002370static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2371 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002372{
Francois Romieufd112f22011-06-18 00:10:29 +02002373 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002374 size_t index;
2375
Francois Romieu1c361ef2011-06-17 17:16:24 +02002376 for (index = 0; index < pa->size; index++) {
2377 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002378 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002379
hayeswang42b82dc2011-01-10 02:07:25 +00002380 switch(action & 0xf0000000) {
2381 case PHY_READ:
2382 case PHY_DATA_OR:
2383 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002384 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002385 case PHY_CLEAR_READCOUNT:
2386 case PHY_WRITE:
2387 case PHY_WRITE_PREVIOUS:
2388 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002389 break;
2390
hayeswang42b82dc2011-01-10 02:07:25 +00002391 case PHY_BJMPN:
2392 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002393 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002394 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002395 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002396 }
2397 break;
2398 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002399 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002400 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002401 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002402 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002403 }
2404 break;
2405 case PHY_COMP_EQ_SKIPN:
2406 case PHY_COMP_NEQ_SKIPN:
2407 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002408 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002409 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002410 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002411 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002412 }
2413 break;
2414
hayeswang42b82dc2011-01-10 02:07:25 +00002415 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002416 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002417 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002418 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002419 }
2420 }
Francois Romieufd112f22011-06-18 00:10:29 +02002421 rc = true;
2422out:
2423 return rc;
2424}
françois romieubca03d52011-01-03 15:07:31 +00002425
Francois Romieufd112f22011-06-18 00:10:29 +02002426static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2427{
2428 struct net_device *dev = tp->dev;
2429 int rc = -EINVAL;
2430
2431 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002432 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002433 goto out;
2434 }
2435
2436 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2437 rc = 0;
2438out:
2439 return rc;
2440}
2441
2442static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2443{
2444 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002445 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002446 u32 predata, count;
2447 size_t index;
2448
2449 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002450 org.write = ops->write;
2451 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002452
Francois Romieu1c361ef2011-06-17 17:16:24 +02002453 for (index = 0; index < pa->size; ) {
2454 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002455 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002456 u32 regno = (action & 0x0fff0000) >> 16;
2457
2458 if (!action)
2459 break;
françois romieubca03d52011-01-03 15:07:31 +00002460
2461 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002462 case PHY_READ:
2463 predata = rtl_readphy(tp, regno);
2464 count++;
2465 index++;
françois romieubca03d52011-01-03 15:07:31 +00002466 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002467 case PHY_DATA_OR:
2468 predata |= data;
2469 index++;
2470 break;
2471 case PHY_DATA_AND:
2472 predata &= data;
2473 index++;
2474 break;
2475 case PHY_BJMPN:
2476 index -= regno;
2477 break;
hayeswangeee37862013-04-01 22:23:38 +00002478 case PHY_MDIO_CHG:
2479 if (data == 0) {
2480 ops->write = org.write;
2481 ops->read = org.read;
2482 } else if (data == 1) {
2483 ops->write = mac_mcu_write;
2484 ops->read = mac_mcu_read;
2485 }
2486
hayeswang42b82dc2011-01-10 02:07:25 +00002487 index++;
2488 break;
2489 case PHY_CLEAR_READCOUNT:
2490 count = 0;
2491 index++;
2492 break;
2493 case PHY_WRITE:
2494 rtl_writephy(tp, regno, data);
2495 index++;
2496 break;
2497 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002498 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002499 break;
2500 case PHY_COMP_EQ_SKIPN:
2501 if (predata == data)
2502 index += regno;
2503 index++;
2504 break;
2505 case PHY_COMP_NEQ_SKIPN:
2506 if (predata != data)
2507 index += regno;
2508 index++;
2509 break;
2510 case PHY_WRITE_PREVIOUS:
2511 rtl_writephy(tp, regno, predata);
2512 index++;
2513 break;
2514 case PHY_SKIPN:
2515 index += regno + 1;
2516 break;
2517 case PHY_DELAY_MS:
2518 mdelay(data);
2519 index++;
2520 break;
2521
françois romieubca03d52011-01-03 15:07:31 +00002522 default:
2523 BUG();
2524 }
2525 }
hayeswangeee37862013-04-01 22:23:38 +00002526
2527 ops->write = org.write;
2528 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002529}
2530
françois romieuf1e02ed2011-01-13 13:07:53 +00002531static void rtl_release_firmware(struct rtl8169_private *tp)
2532{
Heiner Kallweit254764e2019-01-22 22:23:41 +01002533 if (tp->rtl_fw) {
Francois Romieub6ffd972011-06-17 17:00:05 +02002534 release_firmware(tp->rtl_fw->fw);
2535 kfree(tp->rtl_fw);
Heiner Kallweit254764e2019-01-22 22:23:41 +01002536 tp->rtl_fw = NULL;
Francois Romieub6ffd972011-06-17 17:00:05 +02002537 }
françois romieuf1e02ed2011-01-13 13:07:53 +00002538}
2539
François Romieu953a12c2011-04-24 17:38:48 +02002540static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002541{
françois romieuf1e02ed2011-01-13 13:07:53 +00002542 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Heiner Kallweit254764e2019-01-22 22:23:41 +01002543 if (tp->rtl_fw)
2544 rtl_phy_write_fw(tp, tp->rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002545}
2546
2547static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2548{
2549 if (rtl_readphy(tp, reg) != val)
2550 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2551 else
2552 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002553}
2554
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002555static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
2556{
Heiner Kallweitf4528252019-05-04 17:13:09 +02002557 /* Adjust EEE LED frequency */
2558 if (tp->mac_version != RTL_GIGA_MAC_VER_38)
2559 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
2560
Heiner Kallweite719b3e2019-04-28 11:11:47 +02002561 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_1111, 0x0003);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002562}
2563
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002564static void rtl8168f_config_eee_phy(struct rtl8169_private *tp)
2565{
2566 struct phy_device *phydev = tp->phydev;
2567
2568 phy_write(phydev, 0x1f, 0x0007);
2569 phy_write(phydev, 0x1e, 0x0020);
2570 phy_set_bits(phydev, 0x15, BIT(8));
2571
2572 phy_write(phydev, 0x1f, 0x0005);
2573 phy_write(phydev, 0x05, 0x8b85);
2574 phy_set_bits(phydev, 0x06, BIT(13));
2575
2576 phy_write(phydev, 0x1f, 0x0000);
2577}
2578
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002579static void rtl8168g_config_eee_phy(struct rtl8169_private *tp)
2580{
2581 phy_write(tp->phydev, 0x1f, 0x0a43);
2582 phy_set_bits(tp->phydev, 0x11, BIT(4));
2583 phy_write(tp->phydev, 0x1f, 0x0000);
2584}
2585
françois romieu4da19632011-01-03 15:07:55 +00002586static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002588 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002589 { 0x1f, 0x0001 },
2590 { 0x06, 0x006e },
2591 { 0x08, 0x0708 },
2592 { 0x15, 0x4000 },
2593 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
françois romieu0b9b5712009-08-10 19:44:56 +00002595 { 0x1f, 0x0001 },
2596 { 0x03, 0x00a1 },
2597 { 0x02, 0x0008 },
2598 { 0x01, 0x0120 },
2599 { 0x00, 0x1000 },
2600 { 0x04, 0x0800 },
2601 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
françois romieu0b9b5712009-08-10 19:44:56 +00002603 { 0x03, 0xff41 },
2604 { 0x02, 0xdf60 },
2605 { 0x01, 0x0140 },
2606 { 0x00, 0x0077 },
2607 { 0x04, 0x7800 },
2608 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
françois romieu0b9b5712009-08-10 19:44:56 +00002610 { 0x03, 0x802f },
2611 { 0x02, 0x4f02 },
2612 { 0x01, 0x0409 },
2613 { 0x00, 0xf0f9 },
2614 { 0x04, 0x9800 },
2615 { 0x04, 0x9000 },
2616
2617 { 0x03, 0xdf01 },
2618 { 0x02, 0xdf20 },
2619 { 0x01, 0xff95 },
2620 { 0x00, 0xba00 },
2621 { 0x04, 0xa800 },
2622 { 0x04, 0xa000 },
2623
2624 { 0x03, 0xff41 },
2625 { 0x02, 0xdf20 },
2626 { 0x01, 0x0140 },
2627 { 0x00, 0x00bb },
2628 { 0x04, 0xb800 },
2629 { 0x04, 0xb000 },
2630
2631 { 0x03, 0xdf41 },
2632 { 0x02, 0xdc60 },
2633 { 0x01, 0x6340 },
2634 { 0x00, 0x007d },
2635 { 0x04, 0xd800 },
2636 { 0x04, 0xd000 },
2637
2638 { 0x03, 0xdf01 },
2639 { 0x02, 0xdf20 },
2640 { 0x01, 0x100a },
2641 { 0x00, 0xa0ff },
2642 { 0x04, 0xf800 },
2643 { 0x04, 0xf000 },
2644
2645 { 0x1f, 0x0000 },
2646 { 0x0b, 0x0000 },
2647 { 0x00, 0x9200 }
2648 };
2649
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002650 rtl_writephy_batch(tp, phy_reg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
françois romieu4da19632011-01-03 15:07:55 +00002653static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002654{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002655 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002656 { 0x1f, 0x0002 },
2657 { 0x01, 0x90d0 },
2658 { 0x1f, 0x0000 }
2659 };
2660
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002661 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002662}
2663
françois romieu4da19632011-01-03 15:07:55 +00002664static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002665{
2666 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002667
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002668 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2669 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002670 return;
2671
françois romieu4da19632011-01-03 15:07:55 +00002672 rtl_writephy(tp, 0x1f, 0x0001);
2673 rtl_writephy(tp, 0x10, 0xf01b);
2674 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002675}
2676
françois romieu4da19632011-01-03 15:07:55 +00002677static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002678{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002679 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002680 { 0x1f, 0x0001 },
2681 { 0x04, 0x0000 },
2682 { 0x03, 0x00a1 },
2683 { 0x02, 0x0008 },
2684 { 0x01, 0x0120 },
2685 { 0x00, 0x1000 },
2686 { 0x04, 0x0800 },
2687 { 0x04, 0x9000 },
2688 { 0x03, 0x802f },
2689 { 0x02, 0x4f02 },
2690 { 0x01, 0x0409 },
2691 { 0x00, 0xf099 },
2692 { 0x04, 0x9800 },
2693 { 0x04, 0xa000 },
2694 { 0x03, 0xdf01 },
2695 { 0x02, 0xdf20 },
2696 { 0x01, 0xff95 },
2697 { 0x00, 0xba00 },
2698 { 0x04, 0xa800 },
2699 { 0x04, 0xf000 },
2700 { 0x03, 0xdf01 },
2701 { 0x02, 0xdf20 },
2702 { 0x01, 0x101a },
2703 { 0x00, 0xa0ff },
2704 { 0x04, 0xf800 },
2705 { 0x04, 0x0000 },
2706 { 0x1f, 0x0000 },
2707
2708 { 0x1f, 0x0001 },
2709 { 0x10, 0xf41b },
2710 { 0x14, 0xfb54 },
2711 { 0x18, 0xf5c7 },
2712 { 0x1f, 0x0000 },
2713
2714 { 0x1f, 0x0001 },
2715 { 0x17, 0x0cc0 },
2716 { 0x1f, 0x0000 }
2717 };
2718
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002719 rtl_writephy_batch(tp, phy_reg_init);
françois romieu2e9558562009-08-10 19:44:19 +00002720
françois romieu4da19632011-01-03 15:07:55 +00002721 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002722}
2723
françois romieu4da19632011-01-03 15:07:55 +00002724static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002725{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002726 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002727 { 0x1f, 0x0001 },
2728 { 0x04, 0x0000 },
2729 { 0x03, 0x00a1 },
2730 { 0x02, 0x0008 },
2731 { 0x01, 0x0120 },
2732 { 0x00, 0x1000 },
2733 { 0x04, 0x0800 },
2734 { 0x04, 0x9000 },
2735 { 0x03, 0x802f },
2736 { 0x02, 0x4f02 },
2737 { 0x01, 0x0409 },
2738 { 0x00, 0xf099 },
2739 { 0x04, 0x9800 },
2740 { 0x04, 0xa000 },
2741 { 0x03, 0xdf01 },
2742 { 0x02, 0xdf20 },
2743 { 0x01, 0xff95 },
2744 { 0x00, 0xba00 },
2745 { 0x04, 0xa800 },
2746 { 0x04, 0xf000 },
2747 { 0x03, 0xdf01 },
2748 { 0x02, 0xdf20 },
2749 { 0x01, 0x101a },
2750 { 0x00, 0xa0ff },
2751 { 0x04, 0xf800 },
2752 { 0x04, 0x0000 },
2753 { 0x1f, 0x0000 },
2754
2755 { 0x1f, 0x0001 },
2756 { 0x0b, 0x8480 },
2757 { 0x1f, 0x0000 },
2758
2759 { 0x1f, 0x0001 },
2760 { 0x18, 0x67c7 },
2761 { 0x04, 0x2000 },
2762 { 0x03, 0x002f },
2763 { 0x02, 0x4360 },
2764 { 0x01, 0x0109 },
2765 { 0x00, 0x3022 },
2766 { 0x04, 0x2800 },
2767 { 0x1f, 0x0000 },
2768
2769 { 0x1f, 0x0001 },
2770 { 0x17, 0x0cc0 },
2771 { 0x1f, 0x0000 }
2772 };
2773
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002774 rtl_writephy_batch(tp, phy_reg_init);
françois romieu8c7006a2009-08-10 19:43:29 +00002775}
2776
françois romieu4da19632011-01-03 15:07:55 +00002777static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002778{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002779 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002780 { 0x10, 0xf41b },
2781 { 0x1f, 0x0000 }
2782 };
2783
françois romieu4da19632011-01-03 15:07:55 +00002784 rtl_writephy(tp, 0x1f, 0x0001);
2785 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002786
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002787 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002788}
2789
françois romieu4da19632011-01-03 15:07:55 +00002790static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002791{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002792 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002793 { 0x1f, 0x0001 },
2794 { 0x10, 0xf41b },
2795 { 0x1f, 0x0000 }
2796 };
2797
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002798 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002799}
2800
françois romieu4da19632011-01-03 15:07:55 +00002801static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002802{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002803 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002804 { 0x1f, 0x0000 },
2805 { 0x1d, 0x0f00 },
2806 { 0x1f, 0x0002 },
2807 { 0x0c, 0x1ec8 },
2808 { 0x1f, 0x0000 }
2809 };
2810
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002811 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu867763c2007-08-17 18:21:58 +02002812}
2813
françois romieu4da19632011-01-03 15:07:55 +00002814static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002815{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002816 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002817 { 0x1f, 0x0001 },
2818 { 0x1d, 0x3d98 },
2819 { 0x1f, 0x0000 }
2820 };
2821
françois romieu4da19632011-01-03 15:07:55 +00002822 rtl_writephy(tp, 0x1f, 0x0000);
2823 rtl_patchphy(tp, 0x14, 1 << 5);
2824 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002825
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002826 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuef3386f2008-06-29 12:24:30 +02002827}
2828
françois romieu4da19632011-01-03 15:07:55 +00002829static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002830{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002831 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002832 { 0x1f, 0x0001 },
2833 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002834 { 0x1f, 0x0002 },
2835 { 0x00, 0x88d4 },
2836 { 0x01, 0x82b1 },
2837 { 0x03, 0x7002 },
2838 { 0x08, 0x9e30 },
2839 { 0x09, 0x01f0 },
2840 { 0x0a, 0x5500 },
2841 { 0x0c, 0x00c8 },
2842 { 0x1f, 0x0003 },
2843 { 0x12, 0xc096 },
2844 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002845 { 0x1f, 0x0000 },
2846 { 0x1f, 0x0000 },
2847 { 0x09, 0x2000 },
2848 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002849 };
2850
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002851 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002852
françois romieu4da19632011-01-03 15:07:55 +00002853 rtl_patchphy(tp, 0x14, 1 << 5);
2854 rtl_patchphy(tp, 0x0d, 1 << 5);
2855 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002856}
2857
françois romieu4da19632011-01-03 15:07:55 +00002858static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002859{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002860 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002861 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002862 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002863 { 0x03, 0x802f },
2864 { 0x02, 0x4f02 },
2865 { 0x01, 0x0409 },
2866 { 0x00, 0xf099 },
2867 { 0x04, 0x9800 },
2868 { 0x04, 0x9000 },
2869 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002870 { 0x1f, 0x0002 },
2871 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002872 { 0x06, 0x0761 },
2873 { 0x1f, 0x0003 },
2874 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002875 { 0x1f, 0x0000 }
2876 };
2877
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002878 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002879
françois romieu4da19632011-01-03 15:07:55 +00002880 rtl_patchphy(tp, 0x16, 1 << 0);
2881 rtl_patchphy(tp, 0x14, 1 << 5);
2882 rtl_patchphy(tp, 0x0d, 1 << 5);
2883 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002884}
2885
françois romieu4da19632011-01-03 15:07:55 +00002886static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002887{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002888 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002889 { 0x1f, 0x0001 },
2890 { 0x12, 0x2300 },
2891 { 0x1d, 0x3d98 },
2892 { 0x1f, 0x0002 },
2893 { 0x0c, 0x7eb8 },
2894 { 0x06, 0x5461 },
2895 { 0x1f, 0x0003 },
2896 { 0x16, 0x0f0a },
2897 { 0x1f, 0x0000 }
2898 };
2899
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002900 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu197ff762008-06-28 13:16:02 +02002901
françois romieu4da19632011-01-03 15:07:55 +00002902 rtl_patchphy(tp, 0x16, 1 << 0);
2903 rtl_patchphy(tp, 0x14, 1 << 5);
2904 rtl_patchphy(tp, 0x0d, 1 << 5);
2905 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002906}
2907
françois romieu4da19632011-01-03 15:07:55 +00002908static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002909{
françois romieu4da19632011-01-03 15:07:55 +00002910 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002911}
2912
françois romieubca03d52011-01-03 15:07:31 +00002913static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002914{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002915 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002916 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002917 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002918 { 0x06, 0x4064 },
2919 { 0x07, 0x2863 },
2920 { 0x08, 0x059c },
2921 { 0x09, 0x26b4 },
2922 { 0x0a, 0x6a19 },
2923 { 0x0b, 0xdcc8 },
2924 { 0x10, 0xf06d },
2925 { 0x14, 0x7f68 },
2926 { 0x18, 0x7fd9 },
2927 { 0x1c, 0xf0ff },
2928 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002929 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002930 { 0x12, 0xf49f },
2931 { 0x13, 0x070b },
2932 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002933 { 0x14, 0x94c0 },
2934
2935 /*
2936 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002937 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002938 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002939 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002940 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002941 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002942 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002943 { 0x06, 0x5561 },
2944
2945 /*
2946 * Can not link to 1Gbps with bad cable
2947 * Decrease SNR threshold form 21.07dB to 19.04dB
2948 */
2949 { 0x1f, 0x0001 },
2950 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002951
2952 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002953 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002954 };
2955
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002956 rtl_writephy_batch(tp, phy_reg_init_0);
Francois Romieu5b538df2008-07-20 16:22:45 +02002957
françois romieubca03d52011-01-03 15:07:31 +00002958 /*
2959 * Rx Error Issue
2960 * Fine Tune Switching regulator parameter
2961 */
françois romieu4da19632011-01-03 15:07:55 +00002962 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002963 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2964 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002965
Francois Romieufdf6fc02012-07-06 22:40:38 +02002966 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002967 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002968 { 0x1f, 0x0002 },
2969 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002970 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002971 { 0x05, 0x8330 },
2972 { 0x06, 0x669a },
2973 { 0x1f, 0x0002 }
2974 };
2975 int val;
2976
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002977 rtl_writephy_batch(tp, phy_reg_init);
françois romieudaf9df62009-10-07 12:44:20 +00002978
françois romieu4da19632011-01-03 15:07:55 +00002979 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002980
2981 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002982 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002983 0x0065, 0x0066, 0x0067, 0x0068,
2984 0x0069, 0x006a, 0x006b, 0x006c
2985 };
2986 int i;
2987
françois romieu4da19632011-01-03 15:07:55 +00002988 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002989
2990 val &= 0xff00;
2991 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002992 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002993 }
2994 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002995 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002996 { 0x1f, 0x0002 },
2997 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002998 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002999 { 0x05, 0x8330 },
3000 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02003001 };
3002
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003003 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02003004 }
3005
françois romieubca03d52011-01-03 15:07:31 +00003006 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00003007 rtl_writephy(tp, 0x1f, 0x0002);
3008 rtl_patchphy(tp, 0x0d, 0x0300);
3009 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00003010
françois romieubca03d52011-01-03 15:07:31 +00003011 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003012 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003013 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3014 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003015
françois romieu4da19632011-01-03 15:07:55 +00003016 rtl_writephy(tp, 0x1f, 0x0005);
3017 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003018
3019 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00003020
françois romieu4da19632011-01-03 15:07:55 +00003021 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003022}
3023
françois romieubca03d52011-01-03 15:07:31 +00003024static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003025{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003026 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00003027 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00003028 { 0x1f, 0x0001 },
3029 { 0x06, 0x4064 },
3030 { 0x07, 0x2863 },
3031 { 0x08, 0x059c },
3032 { 0x09, 0x26b4 },
3033 { 0x0a, 0x6a19 },
3034 { 0x0b, 0xdcc8 },
3035 { 0x10, 0xf06d },
3036 { 0x14, 0x7f68 },
3037 { 0x18, 0x7fd9 },
3038 { 0x1c, 0xf0ff },
3039 { 0x1d, 0x3d9c },
3040 { 0x1f, 0x0003 },
3041 { 0x12, 0xf49f },
3042 { 0x13, 0x070b },
3043 { 0x1a, 0x05ad },
3044 { 0x14, 0x94c0 },
3045
françois romieubca03d52011-01-03 15:07:31 +00003046 /*
3047 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02003048 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00003049 */
françois romieudaf9df62009-10-07 12:44:20 +00003050 { 0x1f, 0x0002 },
3051 { 0x06, 0x5561 },
3052 { 0x1f, 0x0005 },
3053 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00003054 { 0x06, 0x5561 },
3055
3056 /*
3057 * Can not link to 1Gbps with bad cable
3058 * Decrease SNR threshold form 21.07dB to 19.04dB
3059 */
3060 { 0x1f, 0x0001 },
3061 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00003062
3063 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00003064 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00003065 };
3066
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003067 rtl_writephy_batch(tp, phy_reg_init_0);
françois romieudaf9df62009-10-07 12:44:20 +00003068
Francois Romieufdf6fc02012-07-06 22:40:38 +02003069 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003070 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003071 { 0x1f, 0x0002 },
3072 { 0x05, 0x669a },
3073 { 0x1f, 0x0005 },
3074 { 0x05, 0x8330 },
3075 { 0x06, 0x669a },
3076
3077 { 0x1f, 0x0002 }
3078 };
3079 int val;
3080
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003081 rtl_writephy_batch(tp, phy_reg_init);
françois romieudaf9df62009-10-07 12:44:20 +00003082
françois romieu4da19632011-01-03 15:07:55 +00003083 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00003084 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08003085 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003086 0x0065, 0x0066, 0x0067, 0x0068,
3087 0x0069, 0x006a, 0x006b, 0x006c
3088 };
3089 int i;
3090
françois romieu4da19632011-01-03 15:07:55 +00003091 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00003092
3093 val &= 0xff00;
3094 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00003095 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00003096 }
3097 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003098 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003099 { 0x1f, 0x0002 },
3100 { 0x05, 0x2642 },
3101 { 0x1f, 0x0005 },
3102 { 0x05, 0x8330 },
3103 { 0x06, 0x2642 }
3104 };
3105
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003106 rtl_writephy_batch(tp, phy_reg_init);
françois romieudaf9df62009-10-07 12:44:20 +00003107 }
3108
françois romieubca03d52011-01-03 15:07:31 +00003109 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00003110 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003111 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3112 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00003113
françois romieubca03d52011-01-03 15:07:31 +00003114 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00003115 rtl_writephy(tp, 0x1f, 0x0002);
3116 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00003117
françois romieu4da19632011-01-03 15:07:55 +00003118 rtl_writephy(tp, 0x1f, 0x0005);
3119 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02003120
3121 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00003122
françois romieu4da19632011-01-03 15:07:55 +00003123 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00003124}
3125
françois romieu4da19632011-01-03 15:07:55 +00003126static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003127{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003128 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003129 { 0x1f, 0x0002 },
3130 { 0x10, 0x0008 },
3131 { 0x0d, 0x006c },
3132
3133 { 0x1f, 0x0000 },
3134 { 0x0d, 0xf880 },
3135
3136 { 0x1f, 0x0001 },
3137 { 0x17, 0x0cc0 },
3138
3139 { 0x1f, 0x0001 },
3140 { 0x0b, 0xa4d8 },
3141 { 0x09, 0x281c },
3142 { 0x07, 0x2883 },
3143 { 0x0a, 0x6b35 },
3144 { 0x1d, 0x3da4 },
3145 { 0x1c, 0xeffd },
3146 { 0x14, 0x7f52 },
3147 { 0x18, 0x7fc6 },
3148 { 0x08, 0x0601 },
3149 { 0x06, 0x4063 },
3150 { 0x10, 0xf074 },
3151 { 0x1f, 0x0003 },
3152 { 0x13, 0x0789 },
3153 { 0x12, 0xf4bd },
3154 { 0x1a, 0x04fd },
3155 { 0x14, 0x84b0 },
3156 { 0x1f, 0x0000 },
3157 { 0x00, 0x9200 },
3158
3159 { 0x1f, 0x0005 },
3160 { 0x01, 0x0340 },
3161 { 0x1f, 0x0001 },
3162 { 0x04, 0x4000 },
3163 { 0x03, 0x1d21 },
3164 { 0x02, 0x0c32 },
3165 { 0x01, 0x0200 },
3166 { 0x00, 0x5554 },
3167 { 0x04, 0x4800 },
3168 { 0x04, 0x4000 },
3169 { 0x04, 0xf000 },
3170 { 0x03, 0xdf01 },
3171 { 0x02, 0xdf20 },
3172 { 0x01, 0x101a },
3173 { 0x00, 0xa0ff },
3174 { 0x04, 0xf800 },
3175 { 0x04, 0xf000 },
3176 { 0x1f, 0x0000 },
3177
3178 { 0x1f, 0x0007 },
3179 { 0x1e, 0x0023 },
3180 { 0x16, 0x0000 },
3181 { 0x1f, 0x0000 }
3182 };
3183
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003184 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02003185}
3186
françois romieue6de30d2011-01-03 15:08:37 +00003187static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3188{
3189 static const struct phy_reg phy_reg_init[] = {
3190 { 0x1f, 0x0001 },
3191 { 0x17, 0x0cc0 },
3192
3193 { 0x1f, 0x0007 },
3194 { 0x1e, 0x002d },
3195 { 0x18, 0x0040 },
3196 { 0x1f, 0x0000 }
3197 };
3198
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003199 rtl_writephy_batch(tp, phy_reg_init);
françois romieue6de30d2011-01-03 15:08:37 +00003200 rtl_patchphy(tp, 0x0d, 1 << 5);
3201}
3202
Hayes Wang70090422011-07-06 15:58:06 +08003203static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003204{
3205 static const struct phy_reg phy_reg_init[] = {
3206 /* Enable Delay cap */
3207 { 0x1f, 0x0005 },
3208 { 0x05, 0x8b80 },
3209 { 0x06, 0xc896 },
3210 { 0x1f, 0x0000 },
3211
3212 /* Channel estimation fine tune */
3213 { 0x1f, 0x0001 },
3214 { 0x0b, 0x6c20 },
3215 { 0x07, 0x2872 },
3216 { 0x1c, 0xefff },
3217 { 0x1f, 0x0003 },
3218 { 0x14, 0x6420 },
3219 { 0x1f, 0x0000 },
3220
3221 /* Update PFM & 10M TX idle timer */
3222 { 0x1f, 0x0007 },
3223 { 0x1e, 0x002f },
3224 { 0x15, 0x1919 },
3225 { 0x1f, 0x0000 },
3226
3227 { 0x1f, 0x0007 },
3228 { 0x1e, 0x00ac },
3229 { 0x18, 0x0006 },
3230 { 0x1f, 0x0000 }
3231 };
3232
Francois Romieu15ecd032011-04-27 13:52:22 -07003233 rtl_apply_firmware(tp);
3234
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003235 rtl_writephy_batch(tp, phy_reg_init);
hayeswang01dc7fe2011-03-21 01:50:28 +00003236
3237 /* DCO enable for 10M IDLE Power */
3238 rtl_writephy(tp, 0x1f, 0x0007);
3239 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003240 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003241 rtl_writephy(tp, 0x1f, 0x0000);
3242
3243 /* For impedance matching */
3244 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003245 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003246 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003247
3248 /* PHY auto speed down */
3249 rtl_writephy(tp, 0x1f, 0x0007);
3250 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003251 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003252 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003253 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003254
3255 rtl_writephy(tp, 0x1f, 0x0005);
3256 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003257 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003258 rtl_writephy(tp, 0x1f, 0x0000);
3259
3260 rtl_writephy(tp, 0x1f, 0x0005);
3261 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003262 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003263 rtl_writephy(tp, 0x1f, 0x0007);
3264 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003265 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003266 rtl_writephy(tp, 0x1f, 0x0006);
3267 rtl_writephy(tp, 0x00, 0x5a00);
3268 rtl_writephy(tp, 0x1f, 0x0000);
3269 rtl_writephy(tp, 0x0d, 0x0007);
3270 rtl_writephy(tp, 0x0e, 0x003c);
3271 rtl_writephy(tp, 0x0d, 0x4007);
3272 rtl_writephy(tp, 0x0e, 0x0000);
3273 rtl_writephy(tp, 0x0d, 0x0000);
3274}
3275
françois romieu9ecb9aa2012-12-07 11:20:21 +00003276static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3277{
3278 const u16 w[] = {
3279 addr[0] | (addr[1] << 8),
3280 addr[2] | (addr[3] << 8),
3281 addr[4] | (addr[5] << 8)
3282 };
françois romieu9ecb9aa2012-12-07 11:20:21 +00003283
Heiner Kallweit3aa4c492019-05-02 20:46:52 +02003284 rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, w[0] | (w[1] << 16));
3285 rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, w[2]);
3286 rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, w[0] << 16);
3287 rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, w[1] | (w[2] << 16));
françois romieu9ecb9aa2012-12-07 11:20:21 +00003288}
3289
Hayes Wang70090422011-07-06 15:58:06 +08003290static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3291{
3292 static const struct phy_reg phy_reg_init[] = {
3293 /* Enable Delay cap */
3294 { 0x1f, 0x0004 },
3295 { 0x1f, 0x0007 },
3296 { 0x1e, 0x00ac },
3297 { 0x18, 0x0006 },
3298 { 0x1f, 0x0002 },
3299 { 0x1f, 0x0000 },
3300 { 0x1f, 0x0000 },
3301
3302 /* Channel estimation fine tune */
3303 { 0x1f, 0x0003 },
3304 { 0x09, 0xa20f },
3305 { 0x1f, 0x0000 },
3306 { 0x1f, 0x0000 },
3307
3308 /* Green Setting */
3309 { 0x1f, 0x0005 },
3310 { 0x05, 0x8b5b },
3311 { 0x06, 0x9222 },
3312 { 0x05, 0x8b6d },
3313 { 0x06, 0x8000 },
3314 { 0x05, 0x8b76 },
3315 { 0x06, 0x8000 },
3316 { 0x1f, 0x0000 }
3317 };
3318
3319 rtl_apply_firmware(tp);
3320
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003321 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang70090422011-07-06 15:58:06 +08003322
3323 /* For 4-corner performance improve */
3324 rtl_writephy(tp, 0x1f, 0x0005);
3325 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003326 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003327 rtl_writephy(tp, 0x1f, 0x0000);
3328
3329 /* PHY auto speed down */
3330 rtl_writephy(tp, 0x1f, 0x0004);
3331 rtl_writephy(tp, 0x1f, 0x0007);
3332 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003333 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003334 rtl_writephy(tp, 0x1f, 0x0002);
3335 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003336 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003337
3338 /* improve 10M EEE waveform */
3339 rtl_writephy(tp, 0x1f, 0x0005);
3340 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003341 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003342 rtl_writephy(tp, 0x1f, 0x0000);
3343
3344 /* Improve 2-pair detection performance */
3345 rtl_writephy(tp, 0x1f, 0x0005);
3346 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003347 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003348 rtl_writephy(tp, 0x1f, 0x0000);
3349
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01003350 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01003351 rtl_enable_eee(tp);
Hayes Wang70090422011-07-06 15:58:06 +08003352
3353 /* Green feature */
3354 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003355 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3356 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003357 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003358 rtl_writephy(tp, 0x1f, 0x0005);
3359 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3360 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003361
françois romieu9ecb9aa2012-12-07 11:20:21 +00003362 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3363 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003364}
3365
Hayes Wang5f886e02012-03-30 14:33:03 +08003366static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3367{
3368 /* For 4-corner performance improve */
3369 rtl_writephy(tp, 0x1f, 0x0005);
3370 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003371 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003372 rtl_writephy(tp, 0x1f, 0x0000);
3373
3374 /* PHY auto speed down */
3375 rtl_writephy(tp, 0x1f, 0x0007);
3376 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003377 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003378 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003379 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003380
3381 /* Improve 10M EEE waveform */
3382 rtl_writephy(tp, 0x1f, 0x0005);
3383 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003384 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003385 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01003386
3387 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01003388 rtl_enable_eee(tp);
Hayes Wang5f886e02012-03-30 14:33:03 +08003389}
3390
Hayes Wangc2218922011-09-06 16:55:18 +08003391static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3392{
3393 static const struct phy_reg phy_reg_init[] = {
3394 /* Channel estimation fine tune */
3395 { 0x1f, 0x0003 },
3396 { 0x09, 0xa20f },
3397 { 0x1f, 0x0000 },
3398
3399 /* Modify green table for giga & fnet */
3400 { 0x1f, 0x0005 },
3401 { 0x05, 0x8b55 },
3402 { 0x06, 0x0000 },
3403 { 0x05, 0x8b5e },
3404 { 0x06, 0x0000 },
3405 { 0x05, 0x8b67 },
3406 { 0x06, 0x0000 },
3407 { 0x05, 0x8b70 },
3408 { 0x06, 0x0000 },
3409 { 0x1f, 0x0000 },
3410 { 0x1f, 0x0007 },
3411 { 0x1e, 0x0078 },
3412 { 0x17, 0x0000 },
3413 { 0x19, 0x00fb },
3414 { 0x1f, 0x0000 },
3415
3416 /* Modify green table for 10M */
3417 { 0x1f, 0x0005 },
3418 { 0x05, 0x8b79 },
3419 { 0x06, 0xaa00 },
3420 { 0x1f, 0x0000 },
3421
3422 /* Disable hiimpedance detection (RTCT) */
3423 { 0x1f, 0x0003 },
3424 { 0x01, 0x328a },
3425 { 0x1f, 0x0000 }
3426 };
3427
3428 rtl_apply_firmware(tp);
3429
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003430 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangc2218922011-09-06 16:55:18 +08003431
Hayes Wang5f886e02012-03-30 14:33:03 +08003432 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003433
3434 /* Improve 2-pair detection performance */
3435 rtl_writephy(tp, 0x1f, 0x0005);
3436 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003437 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003438 rtl_writephy(tp, 0x1f, 0x0000);
3439}
3440
3441static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3442{
3443 rtl_apply_firmware(tp);
3444
Hayes Wang5f886e02012-03-30 14:33:03 +08003445 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003446}
3447
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003448static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3449{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003450 static const struct phy_reg phy_reg_init[] = {
3451 /* Channel estimation fine tune */
3452 { 0x1f, 0x0003 },
3453 { 0x09, 0xa20f },
3454 { 0x1f, 0x0000 },
3455
3456 /* Modify green table for giga & fnet */
3457 { 0x1f, 0x0005 },
3458 { 0x05, 0x8b55 },
3459 { 0x06, 0x0000 },
3460 { 0x05, 0x8b5e },
3461 { 0x06, 0x0000 },
3462 { 0x05, 0x8b67 },
3463 { 0x06, 0x0000 },
3464 { 0x05, 0x8b70 },
3465 { 0x06, 0x0000 },
3466 { 0x1f, 0x0000 },
3467 { 0x1f, 0x0007 },
3468 { 0x1e, 0x0078 },
3469 { 0x17, 0x0000 },
3470 { 0x19, 0x00aa },
3471 { 0x1f, 0x0000 },
3472
3473 /* Modify green table for 10M */
3474 { 0x1f, 0x0005 },
3475 { 0x05, 0x8b79 },
3476 { 0x06, 0xaa00 },
3477 { 0x1f, 0x0000 },
3478
3479 /* Disable hiimpedance detection (RTCT) */
3480 { 0x1f, 0x0003 },
3481 { 0x01, 0x328a },
3482 { 0x1f, 0x0000 }
3483 };
3484
3485
3486 rtl_apply_firmware(tp);
3487
3488 rtl8168f_hw_phy_config(tp);
3489
3490 /* Improve 2-pair detection performance */
3491 rtl_writephy(tp, 0x1f, 0x0005);
3492 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003493 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003494 rtl_writephy(tp, 0x1f, 0x0000);
3495
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003496 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003497
3498 /* Modify green table for giga */
3499 rtl_writephy(tp, 0x1f, 0x0005);
3500 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003501 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003502 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003503 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003504 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003505 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003506 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003507 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003508 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003509 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003510 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003511 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003512 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003513 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003514 rtl_writephy(tp, 0x1f, 0x0000);
3515
3516 /* uc same-seed solution */
3517 rtl_writephy(tp, 0x1f, 0x0005);
3518 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003519 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003520 rtl_writephy(tp, 0x1f, 0x0000);
3521
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003522 /* Green feature */
3523 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003524 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3525 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003526 rtl_writephy(tp, 0x1f, 0x0000);
3527}
3528
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003529static void rtl8168g_disable_aldps(struct rtl8169_private *tp)
3530{
3531 phy_write(tp->phydev, 0x1f, 0x0a43);
3532 phy_clear_bits(tp->phydev, 0x10, BIT(2));
3533}
3534
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003535static void rtl8168g_phy_adjust_10m_aldps(struct rtl8169_private *tp)
3536{
3537 struct phy_device *phydev = tp->phydev;
3538
3539 phy_write(phydev, 0x1f, 0x0bcc);
3540 phy_clear_bits(phydev, 0x14, BIT(8));
3541
3542 phy_write(phydev, 0x1f, 0x0a44);
3543 phy_set_bits(phydev, 0x11, BIT(7) | BIT(6));
3544
3545 phy_write(phydev, 0x1f, 0x0a43);
3546 phy_write(phydev, 0x13, 0x8084);
3547 phy_clear_bits(phydev, 0x14, BIT(14) | BIT(13));
3548 phy_set_bits(phydev, 0x10, BIT(12) | BIT(1) | BIT(0));
3549
3550 phy_write(phydev, 0x1f, 0x0000);
3551}
3552
Hayes Wangc5583862012-07-02 17:23:22 +08003553static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3554{
Hayes Wangc5583862012-07-02 17:23:22 +08003555 rtl_apply_firmware(tp);
3556
hayeswang41f44d12013-04-01 22:23:36 +00003557 rtl_writephy(tp, 0x1f, 0x0a46);
3558 if (rtl_readphy(tp, 0x10) & 0x0100) {
3559 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003560 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003561 } else {
3562 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003563 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003564 }
Hayes Wangc5583862012-07-02 17:23:22 +08003565
hayeswang41f44d12013-04-01 22:23:36 +00003566 rtl_writephy(tp, 0x1f, 0x0a46);
3567 if (rtl_readphy(tp, 0x13) & 0x0100) {
3568 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003569 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003570 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003571 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003572 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003573 }
Hayes Wangc5583862012-07-02 17:23:22 +08003574
hayeswang41f44d12013-04-01 22:23:36 +00003575 /* Enable PHY auto speed down */
3576 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003577 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003578
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003579 rtl8168g_phy_adjust_10m_aldps(tp);
hayeswangfe7524c2013-04-01 22:23:37 +00003580
hayeswang41f44d12013-04-01 22:23:36 +00003581 /* EEE auto-fallback function */
3582 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003583 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003584
hayeswang41f44d12013-04-01 22:23:36 +00003585 /* Enable UC LPF tune function */
3586 rtl_writephy(tp, 0x1f, 0x0a43);
3587 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003588 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003589
3590 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003591 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003592
hayeswangfe7524c2013-04-01 22:23:37 +00003593 /* Improve SWR Efficiency */
3594 rtl_writephy(tp, 0x1f, 0x0bcd);
3595 rtl_writephy(tp, 0x14, 0x5065);
3596 rtl_writephy(tp, 0x14, 0xd065);
3597 rtl_writephy(tp, 0x1f, 0x0bc8);
3598 rtl_writephy(tp, 0x11, 0x5655);
3599 rtl_writephy(tp, 0x1f, 0x0bcd);
3600 rtl_writephy(tp, 0x14, 0x1065);
3601 rtl_writephy(tp, 0x14, 0x9065);
3602 rtl_writephy(tp, 0x14, 0x1065);
3603
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003604 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003605 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003606 rtl_enable_eee(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08003607}
3608
hayeswang57538c42013-04-01 22:23:40 +00003609static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3610{
3611 rtl_apply_firmware(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003612 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003613 rtl_enable_eee(tp);
hayeswang57538c42013-04-01 22:23:40 +00003614}
3615
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003616static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3617{
3618 u16 dout_tapbin;
3619 u32 data;
3620
3621 rtl_apply_firmware(tp);
3622
3623 /* CHN EST parameters adjust - giga master */
3624 rtl_writephy(tp, 0x1f, 0x0a43);
3625 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003626 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003627 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003628 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003629 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003630 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003631 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003632 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003633 rtl_writephy(tp, 0x1f, 0x0000);
3634
3635 /* CHN EST parameters adjust - giga slave */
3636 rtl_writephy(tp, 0x1f, 0x0a43);
3637 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003638 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003639 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003640 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003641 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003642 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003643 rtl_writephy(tp, 0x1f, 0x0000);
3644
3645 /* CHN EST parameters adjust - fnet */
3646 rtl_writephy(tp, 0x1f, 0x0a43);
3647 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003648 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003649 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003650 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003651 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003652 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003653 rtl_writephy(tp, 0x1f, 0x0000);
3654
3655 /* enable R-tune & PGA-retune function */
3656 dout_tapbin = 0;
3657 rtl_writephy(tp, 0x1f, 0x0a46);
3658 data = rtl_readphy(tp, 0x13);
3659 data &= 3;
3660 data <<= 2;
3661 dout_tapbin |= data;
3662 data = rtl_readphy(tp, 0x12);
3663 data &= 0xc000;
3664 data >>= 14;
3665 dout_tapbin |= data;
3666 dout_tapbin = ~(dout_tapbin^0x08);
3667 dout_tapbin <<= 12;
3668 dout_tapbin &= 0xf000;
3669 rtl_writephy(tp, 0x1f, 0x0a43);
3670 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003671 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003672 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003673 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003674 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003675 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003676 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003677 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003678
3679 rtl_writephy(tp, 0x1f, 0x0a43);
3680 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003681 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003682 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003683 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003684 rtl_writephy(tp, 0x1f, 0x0000);
3685
3686 /* enable GPHY 10M */
3687 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003688 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003689 rtl_writephy(tp, 0x1f, 0x0000);
3690
3691 /* SAR ADC performance */
3692 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003693 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003694 rtl_writephy(tp, 0x1f, 0x0000);
3695
3696 rtl_writephy(tp, 0x1f, 0x0a43);
3697 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003698 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003699 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003700 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003701 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003702 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003703 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003704 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003705 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003706 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003707 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003708 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003709 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003710 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003711 rtl_writephy(tp, 0x1f, 0x0000);
3712
3713 /* disable phy pfm mode */
3714 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003715 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003716 rtl_writephy(tp, 0x1f, 0x0000);
3717
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003718 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003719 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003720 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003721}
3722
3723static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3724{
3725 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3726 u16 rlen;
3727 u32 data;
3728
3729 rtl_apply_firmware(tp);
3730
3731 /* CHIN EST parameter update */
3732 rtl_writephy(tp, 0x1f, 0x0a43);
3733 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003734 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003735 rtl_writephy(tp, 0x1f, 0x0000);
3736
3737 /* enable R-tune & PGA-retune function */
3738 rtl_writephy(tp, 0x1f, 0x0a43);
3739 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003740 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003741 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003742 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003743 rtl_writephy(tp, 0x1f, 0x0000);
3744
3745 /* enable GPHY 10M */
3746 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003747 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003748 rtl_writephy(tp, 0x1f, 0x0000);
3749
3750 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3751 data = r8168_mac_ocp_read(tp, 0xdd02);
3752 ioffset_p3 = ((data & 0x80)>>7);
3753 ioffset_p3 <<= 3;
3754
3755 data = r8168_mac_ocp_read(tp, 0xdd00);
3756 ioffset_p3 |= ((data & (0xe000))>>13);
3757 ioffset_p2 = ((data & (0x1e00))>>9);
3758 ioffset_p1 = ((data & (0x01e0))>>5);
3759 ioffset_p0 = ((data & 0x0010)>>4);
3760 ioffset_p0 <<= 3;
3761 ioffset_p0 |= (data & (0x07));
3762 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3763
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003764 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003765 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003766 rtl_writephy(tp, 0x1f, 0x0bcf);
3767 rtl_writephy(tp, 0x16, data);
3768 rtl_writephy(tp, 0x1f, 0x0000);
3769 }
3770
3771 /* Modify rlen (TX LPF corner frequency) level */
3772 rtl_writephy(tp, 0x1f, 0x0bcd);
3773 data = rtl_readphy(tp, 0x16);
3774 data &= 0x000f;
3775 rlen = 0;
3776 if (data > 3)
3777 rlen = data - 3;
3778 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3779 rtl_writephy(tp, 0x17, data);
3780 rtl_writephy(tp, 0x1f, 0x0bcd);
3781 rtl_writephy(tp, 0x1f, 0x0000);
3782
3783 /* disable phy pfm mode */
3784 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003785 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003786 rtl_writephy(tp, 0x1f, 0x0000);
3787
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003788 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003789 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003790 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003791}
3792
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003793static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3794{
3795 /* Enable PHY auto speed down */
3796 rtl_writephy(tp, 0x1f, 0x0a44);
3797 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3798 rtl_writephy(tp, 0x1f, 0x0000);
3799
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003800 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003801
3802 /* Enable EEE auto-fallback function */
3803 rtl_writephy(tp, 0x1f, 0x0a4b);
3804 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3805 rtl_writephy(tp, 0x1f, 0x0000);
3806
3807 /* Enable UC LPF tune function */
3808 rtl_writephy(tp, 0x1f, 0x0a43);
3809 rtl_writephy(tp, 0x13, 0x8012);
3810 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3811 rtl_writephy(tp, 0x1f, 0x0000);
3812
3813 /* set rg_sel_sdm_rate */
3814 rtl_writephy(tp, 0x1f, 0x0c42);
3815 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3816 rtl_writephy(tp, 0x1f, 0x0000);
3817
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003818 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003819 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003820 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003821}
3822
3823static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3824{
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003825 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003826
3827 /* Enable UC LPF tune function */
3828 rtl_writephy(tp, 0x1f, 0x0a43);
3829 rtl_writephy(tp, 0x13, 0x8012);
3830 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3831 rtl_writephy(tp, 0x1f, 0x0000);
3832
3833 /* Set rg_sel_sdm_rate */
3834 rtl_writephy(tp, 0x1f, 0x0c42);
3835 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3836 rtl_writephy(tp, 0x1f, 0x0000);
3837
3838 /* Channel estimation parameters */
3839 rtl_writephy(tp, 0x1f, 0x0a43);
3840 rtl_writephy(tp, 0x13, 0x80f3);
3841 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3842 rtl_writephy(tp, 0x13, 0x80f0);
3843 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3844 rtl_writephy(tp, 0x13, 0x80ef);
3845 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3846 rtl_writephy(tp, 0x13, 0x80f6);
3847 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3848 rtl_writephy(tp, 0x13, 0x80ec);
3849 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3850 rtl_writephy(tp, 0x13, 0x80ed);
3851 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3852 rtl_writephy(tp, 0x13, 0x80f2);
3853 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3854 rtl_writephy(tp, 0x13, 0x80f4);
3855 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3856 rtl_writephy(tp, 0x1f, 0x0a43);
3857 rtl_writephy(tp, 0x13, 0x8110);
3858 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3859 rtl_writephy(tp, 0x13, 0x810f);
3860 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3861 rtl_writephy(tp, 0x13, 0x8111);
3862 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3863 rtl_writephy(tp, 0x13, 0x8113);
3864 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3865 rtl_writephy(tp, 0x13, 0x8115);
3866 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3867 rtl_writephy(tp, 0x13, 0x810e);
3868 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3869 rtl_writephy(tp, 0x13, 0x810c);
3870 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3871 rtl_writephy(tp, 0x13, 0x810b);
3872 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3873 rtl_writephy(tp, 0x1f, 0x0a43);
3874 rtl_writephy(tp, 0x13, 0x80d1);
3875 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3876 rtl_writephy(tp, 0x13, 0x80cd);
3877 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3878 rtl_writephy(tp, 0x13, 0x80d3);
3879 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3880 rtl_writephy(tp, 0x13, 0x80d5);
3881 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3882 rtl_writephy(tp, 0x13, 0x80d7);
3883 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3884
3885 /* Force PWM-mode */
3886 rtl_writephy(tp, 0x1f, 0x0bcd);
3887 rtl_writephy(tp, 0x14, 0x5065);
3888 rtl_writephy(tp, 0x14, 0xd065);
3889 rtl_writephy(tp, 0x1f, 0x0bc8);
3890 rtl_writephy(tp, 0x12, 0x00ed);
3891 rtl_writephy(tp, 0x1f, 0x0bcd);
3892 rtl_writephy(tp, 0x14, 0x1065);
3893 rtl_writephy(tp, 0x14, 0x9065);
3894 rtl_writephy(tp, 0x14, 0x1065);
3895 rtl_writephy(tp, 0x1f, 0x0000);
3896
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003897 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003898 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003899 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003900}
3901
françois romieu4da19632011-01-03 15:07:55 +00003902static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003903{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003904 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003905 { 0x1f, 0x0003 },
3906 { 0x08, 0x441d },
3907 { 0x01, 0x9100 },
3908 { 0x1f, 0x0000 }
3909 };
3910
françois romieu4da19632011-01-03 15:07:55 +00003911 rtl_writephy(tp, 0x1f, 0x0000);
3912 rtl_patchphy(tp, 0x11, 1 << 12);
3913 rtl_patchphy(tp, 0x19, 1 << 13);
3914 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003915
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003916 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003917}
3918
Hayes Wang5a5e4442011-02-22 17:26:21 +08003919static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3920{
3921 static const struct phy_reg phy_reg_init[] = {
3922 { 0x1f, 0x0005 },
3923 { 0x1a, 0x0000 },
3924 { 0x1f, 0x0000 },
3925
3926 { 0x1f, 0x0004 },
3927 { 0x1c, 0x0000 },
3928 { 0x1f, 0x0000 },
3929
3930 { 0x1f, 0x0001 },
3931 { 0x15, 0x7701 },
3932 { 0x1f, 0x0000 }
3933 };
3934
3935 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003936 rtl_writephy(tp, 0x1f, 0x0000);
3937 rtl_writephy(tp, 0x18, 0x0310);
3938 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003939
François Romieu953a12c2011-04-24 17:38:48 +02003940 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003941
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003942 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003943}
3944
Hayes Wang7e18dca2012-03-30 14:33:02 +08003945static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3946{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003947 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003948 rtl_writephy(tp, 0x1f, 0x0000);
3949 rtl_writephy(tp, 0x18, 0x0310);
3950 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003951
3952 rtl_apply_firmware(tp);
3953
3954 /* EEE setting */
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003955 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003956 rtl_writephy(tp, 0x1f, 0x0004);
3957 rtl_writephy(tp, 0x10, 0x401f);
3958 rtl_writephy(tp, 0x19, 0x7030);
3959 rtl_writephy(tp, 0x1f, 0x0000);
3960}
3961
Hayes Wang5598bfe2012-07-02 17:23:21 +08003962static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3963{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003964 static const struct phy_reg phy_reg_init[] = {
3965 { 0x1f, 0x0004 },
3966 { 0x10, 0xc07f },
3967 { 0x19, 0x7030 },
3968 { 0x1f, 0x0000 }
3969 };
3970
3971 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003972 rtl_writephy(tp, 0x1f, 0x0000);
3973 rtl_writephy(tp, 0x18, 0x0310);
3974 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003975
3976 rtl_apply_firmware(tp);
3977
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003978 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003979 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003980
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003981 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003982}
3983
Francois Romieu5615d9f2007-08-17 17:50:46 +02003984static void rtl_hw_phy_config(struct net_device *dev)
3985{
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003986 static const rtl_generic_fct phy_configs[] = {
3987 /* PCI devices. */
3988 [RTL_GIGA_MAC_VER_01] = NULL,
3989 [RTL_GIGA_MAC_VER_02] = rtl8169s_hw_phy_config,
3990 [RTL_GIGA_MAC_VER_03] = rtl8169s_hw_phy_config,
3991 [RTL_GIGA_MAC_VER_04] = rtl8169sb_hw_phy_config,
3992 [RTL_GIGA_MAC_VER_05] = rtl8169scd_hw_phy_config,
3993 [RTL_GIGA_MAC_VER_06] = rtl8169sce_hw_phy_config,
3994 /* PCI-E devices. */
3995 [RTL_GIGA_MAC_VER_07] = rtl8102e_hw_phy_config,
3996 [RTL_GIGA_MAC_VER_08] = rtl8102e_hw_phy_config,
3997 [RTL_GIGA_MAC_VER_09] = rtl8102e_hw_phy_config,
3998 [RTL_GIGA_MAC_VER_10] = NULL,
3999 [RTL_GIGA_MAC_VER_11] = rtl8168bb_hw_phy_config,
4000 [RTL_GIGA_MAC_VER_12] = rtl8168bef_hw_phy_config,
4001 [RTL_GIGA_MAC_VER_13] = NULL,
4002 [RTL_GIGA_MAC_VER_14] = NULL,
4003 [RTL_GIGA_MAC_VER_15] = NULL,
4004 [RTL_GIGA_MAC_VER_16] = NULL,
4005 [RTL_GIGA_MAC_VER_17] = rtl8168bef_hw_phy_config,
4006 [RTL_GIGA_MAC_VER_18] = rtl8168cp_1_hw_phy_config,
4007 [RTL_GIGA_MAC_VER_19] = rtl8168c_1_hw_phy_config,
4008 [RTL_GIGA_MAC_VER_20] = rtl8168c_2_hw_phy_config,
4009 [RTL_GIGA_MAC_VER_21] = rtl8168c_3_hw_phy_config,
4010 [RTL_GIGA_MAC_VER_22] = rtl8168c_4_hw_phy_config,
4011 [RTL_GIGA_MAC_VER_23] = rtl8168cp_2_hw_phy_config,
4012 [RTL_GIGA_MAC_VER_24] = rtl8168cp_2_hw_phy_config,
4013 [RTL_GIGA_MAC_VER_25] = rtl8168d_1_hw_phy_config,
4014 [RTL_GIGA_MAC_VER_26] = rtl8168d_2_hw_phy_config,
4015 [RTL_GIGA_MAC_VER_27] = rtl8168d_3_hw_phy_config,
4016 [RTL_GIGA_MAC_VER_28] = rtl8168d_4_hw_phy_config,
4017 [RTL_GIGA_MAC_VER_29] = rtl8105e_hw_phy_config,
4018 [RTL_GIGA_MAC_VER_30] = rtl8105e_hw_phy_config,
4019 [RTL_GIGA_MAC_VER_31] = NULL,
4020 [RTL_GIGA_MAC_VER_32] = rtl8168e_1_hw_phy_config,
4021 [RTL_GIGA_MAC_VER_33] = rtl8168e_1_hw_phy_config,
4022 [RTL_GIGA_MAC_VER_34] = rtl8168e_2_hw_phy_config,
4023 [RTL_GIGA_MAC_VER_35] = rtl8168f_1_hw_phy_config,
4024 [RTL_GIGA_MAC_VER_36] = rtl8168f_2_hw_phy_config,
4025 [RTL_GIGA_MAC_VER_37] = rtl8402_hw_phy_config,
4026 [RTL_GIGA_MAC_VER_38] = rtl8411_hw_phy_config,
4027 [RTL_GIGA_MAC_VER_39] = rtl8106e_hw_phy_config,
4028 [RTL_GIGA_MAC_VER_40] = rtl8168g_1_hw_phy_config,
4029 [RTL_GIGA_MAC_VER_41] = NULL,
4030 [RTL_GIGA_MAC_VER_42] = rtl8168g_2_hw_phy_config,
4031 [RTL_GIGA_MAC_VER_43] = rtl8168g_2_hw_phy_config,
4032 [RTL_GIGA_MAC_VER_44] = rtl8168g_2_hw_phy_config,
4033 [RTL_GIGA_MAC_VER_45] = rtl8168h_1_hw_phy_config,
4034 [RTL_GIGA_MAC_VER_46] = rtl8168h_2_hw_phy_config,
4035 [RTL_GIGA_MAC_VER_47] = rtl8168h_1_hw_phy_config,
4036 [RTL_GIGA_MAC_VER_48] = rtl8168h_2_hw_phy_config,
4037 [RTL_GIGA_MAC_VER_49] = rtl8168ep_1_hw_phy_config,
4038 [RTL_GIGA_MAC_VER_50] = rtl8168ep_2_hw_phy_config,
4039 [RTL_GIGA_MAC_VER_51] = rtl8168ep_2_hw_phy_config,
4040 };
Francois Romieu5615d9f2007-08-17 17:50:46 +02004041 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004042
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02004043 if (phy_configs[tp->mac_version])
4044 phy_configs[tp->mac_version](tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02004045}
4046
Francois Romieuda78dbf2012-01-26 14:18:23 +01004047static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4048{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004049 if (!test_and_set_bit(flag, tp->wk.flags))
4050 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004051}
4052
David S. Miller8decf862011-09-22 03:23:13 -04004053static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4054{
David S. Miller8decf862011-09-22 03:23:13 -04004055 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Heiner Kallweite3972862018-06-29 08:07:04 +02004056 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004057}
4058
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004059static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004061 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004062
Marcus Sundberg773328942008-07-10 21:28:08 +02004063 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit7a67e112018-09-19 22:00:24 +02004064 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4065 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Heiner Kallweit49d17512018-06-28 20:36:15 +02004066 netif_dbg(tp, drv, dev,
4067 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004068 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004069 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004070
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02004071 /* We may have called phy_speed_down before */
Heiner Kallweit703732f2019-01-19 22:07:05 +01004072 phy_speed_up(tp->phydev);
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02004073
Heiner Kallweit703732f2019-01-19 22:07:05 +01004074 genphy_soft_reset(tp->phydev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004075}
4076
Francois Romieu773d2022007-01-31 23:47:43 +01004077static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4078{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004079 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004080
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01004081 rtl_unlock_config_regs(tp);
françois romieu908ba2bf2010-04-26 11:42:58 +00004082
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004083 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4084 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004085
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004086 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4087 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004088
françois romieu9ecb9aa2012-12-07 11:20:21 +00004089 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4090 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004091
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01004092 rtl_lock_config_regs(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004093
Francois Romieuda78dbf2012-01-26 14:18:23 +01004094 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004095}
4096
4097static int rtl_set_mac_address(struct net_device *dev, void *p)
4098{
4099 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004100 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004101 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004102
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004103 ret = eth_mac_addr(dev, p);
4104 if (ret)
4105 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004106
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004107 pm_runtime_get_noresume(d);
4108
4109 if (pm_runtime_active(d))
4110 rtl_rar_set(tp, dev->dev_addr);
4111
4112 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004113
4114 return 0;
4115}
4116
Heiner Kallweite3972862018-06-29 08:07:04 +02004117static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004118{
Heiner Kallweit703732f2019-01-19 22:07:05 +01004119 struct rtl8169_private *tp = netdev_priv(dev);
4120
Heiner Kallweit69b3c592018-07-17 22:51:53 +02004121 if (!netif_running(dev))
4122 return -ENODEV;
Heiner Kallweite3972862018-06-29 08:07:04 +02004123
Heiner Kallweit703732f2019-01-19 22:07:05 +01004124 return phy_mii_ioctl(tp->phydev, ifr, cmd);
Francois Romieu8b4ab282008-11-19 22:05:25 -08004125}
4126
Bill Pembertonbaf63292012-12-03 09:23:28 -05004127static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004128{
4129 struct mdio_ops *ops = &tp->mdio_ops;
4130
4131 switch (tp->mac_version) {
4132 case RTL_GIGA_MAC_VER_27:
4133 ops->write = r8168dp_1_mdio_write;
4134 ops->read = r8168dp_1_mdio_read;
4135 break;
françois romieue6de30d2011-01-03 15:08:37 +00004136 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004137 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004138 ops->write = r8168dp_2_mdio_write;
4139 ops->read = r8168dp_2_mdio_read;
4140 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004141 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004142 ops->write = r8168g_mdio_write;
4143 ops->read = r8168g_mdio_read;
4144 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004145 default:
4146 ops->write = r8169_mdio_write;
4147 ops->read = r8169_mdio_read;
4148 break;
4149 }
4150}
4151
David S. Miller1805b2f2011-10-24 18:18:09 -04004152static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4153{
David S. Miller1805b2f2011-10-24 18:18:09 -04004154 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004155 case RTL_GIGA_MAC_VER_25:
4156 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004157 case RTL_GIGA_MAC_VER_29:
4158 case RTL_GIGA_MAC_VER_30:
4159 case RTL_GIGA_MAC_VER_32:
4160 case RTL_GIGA_MAC_VER_33:
4161 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004162 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004163 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004164 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4165 break;
4166 default:
4167 break;
4168 }
4169}
4170
françois romieu065c27c2011-01-03 15:08:12 +00004171static void r8168_pll_power_down(struct rtl8169_private *tp)
4172{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004173 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004174 return;
4175
hayeswang01dc7fe2011-03-21 01:50:28 +00004176 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4177 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004178 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004179
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01004180 if (device_may_wakeup(tp_to_dev(tp))) {
4181 phy_speed_down(tp->phydev, false);
4182 rtl_wol_suspend_quirk(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004183 return;
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01004184 }
françois romieu065c27c2011-01-03 15:08:12 +00004185
françois romieu065c27c2011-01-03 15:08:12 +00004186 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004187 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004188 case RTL_GIGA_MAC_VER_37:
4189 case RTL_GIGA_MAC_VER_39:
4190 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004191 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004192 case RTL_GIGA_MAC_VER_45:
4193 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004194 case RTL_GIGA_MAC_VER_47:
4195 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004196 case RTL_GIGA_MAC_VER_50:
4197 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004198 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004199 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004200 case RTL_GIGA_MAC_VER_40:
4201 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004202 case RTL_GIGA_MAC_VER_49:
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004203 rtl_eri_clear_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004204 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004205 break;
françois romieu065c27c2011-01-03 15:08:12 +00004206 }
4207}
4208
4209static void r8168_pll_power_up(struct rtl8169_private *tp)
4210{
françois romieu065c27c2011-01-03 15:08:12 +00004211 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004212 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004213 case RTL_GIGA_MAC_VER_37:
4214 case RTL_GIGA_MAC_VER_39:
4215 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004216 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004217 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004218 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004219 case RTL_GIGA_MAC_VER_45:
4220 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004221 case RTL_GIGA_MAC_VER_47:
4222 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004223 case RTL_GIGA_MAC_VER_50:
4224 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004225 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004226 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004227 case RTL_GIGA_MAC_VER_40:
4228 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004229 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004230 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004231 rtl_eri_set_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
hayeswangbeb330a2013-04-01 22:23:39 +00004232 break;
françois romieu065c27c2011-01-03 15:08:12 +00004233 }
4234
Heiner Kallweit703732f2019-01-19 22:07:05 +01004235 phy_resume(tp->phydev);
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02004236 /* give MAC/PHY some time to resume */
4237 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00004238}
4239
françois romieu065c27c2011-01-03 15:08:12 +00004240static void rtl_pll_power_down(struct rtl8169_private *tp)
4241{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004242 switch (tp->mac_version) {
4243 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4244 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4245 break;
4246 default:
4247 r8168_pll_power_down(tp);
4248 }
françois romieu065c27c2011-01-03 15:08:12 +00004249}
4250
4251static void rtl_pll_power_up(struct rtl8169_private *tp)
4252{
françois romieu065c27c2011-01-03 15:08:12 +00004253 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004254 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4255 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004256 break;
françois romieu065c27c2011-01-03 15:08:12 +00004257 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004258 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004259 }
4260}
4261
Hayes Wange542a222011-07-06 15:58:04 +08004262static void rtl_init_rxcfg(struct rtl8169_private *tp)
4263{
Hayes Wange542a222011-07-06 15:58:04 +08004264 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004265 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4266 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004267 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004268 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004269 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
Maciej S. Szmigiero511cfd52018-10-11 16:02:10 +02004270 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
4271 case RTL_GIGA_MAC_VER_38:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004272 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004273 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004274 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004275 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004276 break;
Hayes Wange542a222011-07-06 15:58:04 +08004277 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004278 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004279 break;
4280 }
4281}
4282
Hayes Wang92fc43b2011-07-06 15:58:03 +08004283static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4284{
Timo Teräs9fba0812013-01-15 21:01:24 +00004285 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004286}
4287
Francois Romieud58d46b2011-05-03 16:38:29 +02004288static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4289{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004290 if (tp->jumbo_ops.enable) {
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01004291 rtl_unlock_config_regs(tp);
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004292 tp->jumbo_ops.enable(tp);
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01004293 rtl_lock_config_regs(tp);
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004294 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004295}
4296
4297static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4298{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004299 if (tp->jumbo_ops.disable) {
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01004300 rtl_unlock_config_regs(tp);
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004301 tp->jumbo_ops.disable(tp);
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01004302 rtl_lock_config_regs(tp);
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004303 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004304}
4305
4306static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4307{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004308 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4309 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004310 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004311}
4312
4313static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4314{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004315 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4316 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004317 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004318}
4319
4320static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4321{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004322 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004323}
4324
4325static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4326{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004327 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004328}
4329
4330static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4331{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004332 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4333 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4334 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004335 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004336}
4337
4338static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4339{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004340 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4341 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4342 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004343 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004344}
4345
4346static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4347{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004348 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004349 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004350}
4351
4352static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4353{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004354 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004355 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004356}
4357
4358static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4359{
Francois Romieud58d46b2011-05-03 16:38:29 +02004360 r8168b_0_hw_jumbo_enable(tp);
4361
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004362 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004363}
4364
4365static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4366{
Francois Romieud58d46b2011-05-03 16:38:29 +02004367 r8168b_0_hw_jumbo_disable(tp);
4368
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004369 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004370}
4371
Bill Pembertonbaf63292012-12-03 09:23:28 -05004372static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004373{
4374 struct jumbo_ops *ops = &tp->jumbo_ops;
4375
4376 switch (tp->mac_version) {
4377 case RTL_GIGA_MAC_VER_11:
4378 ops->disable = r8168b_0_hw_jumbo_disable;
4379 ops->enable = r8168b_0_hw_jumbo_enable;
4380 break;
4381 case RTL_GIGA_MAC_VER_12:
4382 case RTL_GIGA_MAC_VER_17:
4383 ops->disable = r8168b_1_hw_jumbo_disable;
4384 ops->enable = r8168b_1_hw_jumbo_enable;
4385 break;
4386 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4387 case RTL_GIGA_MAC_VER_19:
4388 case RTL_GIGA_MAC_VER_20:
4389 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4390 case RTL_GIGA_MAC_VER_22:
4391 case RTL_GIGA_MAC_VER_23:
4392 case RTL_GIGA_MAC_VER_24:
4393 case RTL_GIGA_MAC_VER_25:
4394 case RTL_GIGA_MAC_VER_26:
4395 ops->disable = r8168c_hw_jumbo_disable;
4396 ops->enable = r8168c_hw_jumbo_enable;
4397 break;
4398 case RTL_GIGA_MAC_VER_27:
4399 case RTL_GIGA_MAC_VER_28:
4400 ops->disable = r8168dp_hw_jumbo_disable;
4401 ops->enable = r8168dp_hw_jumbo_enable;
4402 break;
4403 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4404 case RTL_GIGA_MAC_VER_32:
4405 case RTL_GIGA_MAC_VER_33:
4406 case RTL_GIGA_MAC_VER_34:
4407 ops->disable = r8168e_hw_jumbo_disable;
4408 ops->enable = r8168e_hw_jumbo_enable;
4409 break;
4410
4411 /*
4412 * No action needed for jumbo frames with 8169.
4413 * No jumbo for 810x at all.
4414 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004415 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004416 default:
4417 ops->disable = NULL;
4418 ops->enable = NULL;
4419 break;
4420 }
4421}
4422
Francois Romieuffc46952012-07-06 14:19:23 +02004423DECLARE_RTL_COND(rtl_chipcmd_cond)
4424{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004425 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004426}
4427
Francois Romieu6f43adc2011-04-29 15:05:51 +02004428static void rtl_hw_reset(struct rtl8169_private *tp)
4429{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004430 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004431
Francois Romieuffc46952012-07-06 14:19:23 +02004432 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004433}
4434
Heiner Kallweit254764e2019-01-22 22:23:41 +01004435static void rtl_request_firmware(struct rtl8169_private *tp)
Francois Romieub6ffd972011-06-17 17:00:05 +02004436{
4437 struct rtl_fw *rtl_fw;
Francois Romieub6ffd972011-06-17 17:00:05 +02004438 int rc = -ENOMEM;
4439
Heiner Kallweit254764e2019-01-22 22:23:41 +01004440 /* firmware loaded already or no firmware available */
4441 if (tp->rtl_fw || !tp->fw_name)
4442 return;
Francois Romieub6ffd972011-06-17 17:00:05 +02004443
4444 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4445 if (!rtl_fw)
4446 goto err_warn;
4447
Heiner Kallweit254764e2019-01-22 22:23:41 +01004448 rc = request_firmware(&rtl_fw->fw, tp->fw_name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004449 if (rc < 0)
4450 goto err_free;
4451
Francois Romieufd112f22011-06-18 00:10:29 +02004452 rc = rtl_check_firmware(tp, rtl_fw);
4453 if (rc < 0)
4454 goto err_release_firmware;
4455
Francois Romieub6ffd972011-06-17 17:00:05 +02004456 tp->rtl_fw = rtl_fw;
Heiner Kallweit254764e2019-01-22 22:23:41 +01004457
Francois Romieub6ffd972011-06-17 17:00:05 +02004458 return;
4459
Francois Romieufd112f22011-06-18 00:10:29 +02004460err_release_firmware:
4461 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004462err_free:
4463 kfree(rtl_fw);
4464err_warn:
4465 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
Heiner Kallweit254764e2019-01-22 22:23:41 +01004466 tp->fw_name, rc);
François Romieu953a12c2011-04-24 17:38:48 +02004467}
4468
Hayes Wang92fc43b2011-07-06 15:58:03 +08004469static void rtl_rx_close(struct rtl8169_private *tp)
4470{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004471 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004472}
4473
Francois Romieuffc46952012-07-06 14:19:23 +02004474DECLARE_RTL_COND(rtl_npq_cond)
4475{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004476 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02004477}
4478
4479DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4480{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004481 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02004482}
4483
françois romieue6de30d2011-01-03 15:08:37 +00004484static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485{
4486 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004487 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Hayes Wang92fc43b2011-07-06 15:58:03 +08004489 rtl_rx_close(tp);
4490
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004491 switch (tp->mac_version) {
4492 case RTL_GIGA_MAC_VER_27:
4493 case RTL_GIGA_MAC_VER_28:
4494 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02004495 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004496 break;
4497 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4498 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004499 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004500 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004501 break;
4502 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004503 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004504 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004505 break;
françois romieue6de30d2011-01-03 15:08:37 +00004506 }
4507
Hayes Wang92fc43b2011-07-06 15:58:03 +08004508 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509}
4510
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004511static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004512{
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004513 u32 val = TX_DMA_BURST << TxDMAShift |
4514 InterFrameGap << TxInterFrameGapShift;
4515
4516 if (tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
4517 tp->mac_version != RTL_GIGA_MAC_VER_39)
4518 val |= TXCFG_AUTO_FIFO;
4519
4520 RTL_W32(tp, TxConfig, val);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004521}
4522
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004523static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004525 /* Low hurts. Let's disable the filtering. */
4526 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004527}
4528
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004529static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004530{
4531 /*
4532 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4533 * register to be written before TxDescAddrLow to work.
4534 * Switching from MMIO to I/O access fixes the issue as well.
4535 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004536 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4537 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4538 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4539 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004540}
4541
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004542static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004543{
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004544 u32 val;
Francois Romieu6dccd162007-02-13 23:38:05 +01004545
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004546 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4547 val = 0x000fff00;
4548 else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
4549 val = 0x00ffff00;
4550 else
4551 return;
4552
4553 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
4554 val |= 0xff;
4555
4556 RTL_W32(tp, 0x7c, val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004557}
4558
Francois Romieue6b763e2012-03-08 09:35:39 +01004559static void rtl_set_rx_mode(struct net_device *dev)
4560{
4561 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01004562 u32 mc_filter[2]; /* Multicast hash filter */
4563 int rx_mode;
4564 u32 tmp = 0;
4565
4566 if (dev->flags & IFF_PROMISC) {
4567 /* Unconditionally log net taps. */
4568 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4569 rx_mode =
4570 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4571 AcceptAllPhys;
4572 mc_filter[1] = mc_filter[0] = 0xffffffff;
4573 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4574 (dev->flags & IFF_ALLMULTI)) {
4575 /* Too many to filter perfectly -- accept all multicasts. */
4576 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4577 mc_filter[1] = mc_filter[0] = 0xffffffff;
4578 } else {
4579 struct netdev_hw_addr *ha;
4580
4581 rx_mode = AcceptBroadcast | AcceptMyPhys;
4582 mc_filter[1] = mc_filter[0] = 0;
4583 netdev_for_each_mc_addr(ha, dev) {
4584 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4585 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4586 rx_mode |= AcceptMulticast;
4587 }
4588 }
4589
4590 if (dev->features & NETIF_F_RXALL)
4591 rx_mode |= (AcceptErr | AcceptRunt);
4592
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004593 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01004594
4595 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4596 u32 data = mc_filter[0];
4597
4598 mc_filter[0] = swab32(mc_filter[1]);
4599 mc_filter[1] = swab32(data);
4600 }
4601
Nathan Walp04817762012-11-01 12:08:47 +00004602 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4603 mc_filter[1] = mc_filter[0] = 0xffffffff;
4604
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004605 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4606 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004607
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004608 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004609}
4610
Heiner Kallweit52f85602018-05-19 10:29:33 +02004611static void rtl_hw_start(struct rtl8169_private *tp)
4612{
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01004613 rtl_unlock_config_regs(tp);
Heiner Kallweit52f85602018-05-19 10:29:33 +02004614
4615 tp->hw_start(tp);
4616
4617 rtl_set_rx_max_size(tp);
4618 rtl_set_rx_tx_desc_registers(tp);
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01004619 rtl_lock_config_regs(tp);
Heiner Kallweit52f85602018-05-19 10:29:33 +02004620
Heiner Kallweiteb94dc92019-03-31 15:43:59 +02004621 /* disable interrupt coalescing */
4622 RTL_W16(tp, IntrMitigate, 0x0000);
Heiner Kallweit52f85602018-05-19 10:29:33 +02004623 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4624 RTL_R8(tp, IntrMask);
4625 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004626 rtl_init_rxcfg(tp);
Maciej S. Szmigierof74dd482018-09-07 20:15:22 +02004627 rtl_set_tx_config_registers(tp);
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004628
Heiner Kallweit52f85602018-05-19 10:29:33 +02004629 rtl_set_rx_mode(tp->dev);
4630 /* no early-rx interrupts */
4631 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Heiner Kallweitfe716f82018-11-19 22:31:32 +01004632 rtl_irq_enable(tp);
Heiner Kallweit52f85602018-05-19 10:29:33 +02004633}
4634
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004635static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01004636{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004637 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004638 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004639
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004640 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004642 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004643
Francois Romieucecb5fd2011-04-01 10:21:07 +02004644 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4645 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004646 netif_dbg(tp, drv, tp->dev,
4647 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004648 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 }
4650
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004651 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02004652
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004653 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01004654
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004655 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01004656}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
Francois Romieuffc46952012-07-06 14:19:23 +02004658DECLARE_RTL_COND(rtl_csiar_cond)
4659{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004660 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02004661}
4662
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004663static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004664{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004665 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4666
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004667 RTL_W32(tp, CSIDR, value);
4668 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004669 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004670
Francois Romieuffc46952012-07-06 14:19:23 +02004671 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004672}
4673
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004674static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004675{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004676 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4677
4678 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4679 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004680
Francois Romieuffc46952012-07-06 14:19:23 +02004681 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004682 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004683}
4684
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004685static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004686{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004687 struct pci_dev *pdev = tp->pci_dev;
4688 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004689
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004690 /* According to Realtek the value at config space address 0x070f
4691 * controls the L0s/L1 entrance latency. We try standard ECAM access
4692 * first and if it fails fall back to CSI.
4693 */
4694 if (pdev->cfg_size > 0x070f &&
4695 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4696 return;
4697
4698 netdev_notice_once(tp->dev,
4699 "No native access to PCI extended config space, falling back to CSI\n");
4700 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4701 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004702}
4703
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004704static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08004705{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004706 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02004707}
4708
4709struct ephy_info {
4710 unsigned int offset;
4711 u16 mask;
4712 u16 bits;
4713};
4714
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004715static void __rtl_ephy_init(struct rtl8169_private *tp,
4716 const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004717{
4718 u16 w;
4719
4720 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004721 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4722 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004723 e++;
4724 }
4725}
4726
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004727#define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
4728
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004729static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02004730{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004731 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004732 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004733}
4734
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004735static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004736{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004737 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004738 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004739}
4740
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004741static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
hayeswangb51ecea2014-07-09 14:52:51 +08004742{
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004743 /* work around an issue when PCI reset occurs during L2/L3 state */
4744 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
hayeswangb51ecea2014-07-09 14:52:51 +08004745}
4746
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004747static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4748{
4749 if (enable) {
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004750 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
Kai-Heng Feng94235462018-09-12 14:58:20 +08004751 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004752 } else {
4753 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4754 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4755 }
Kai-Heng Feng94235462018-09-12 14:58:20 +08004756
4757 udelay(10);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004758}
4759
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004760static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
4761 u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
4762{
4763 /* Usage of dynamic vs. static FIFO is controlled by bit
4764 * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
4765 */
4766 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
4767 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
4768}
4769
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004770static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
4771 u8 low, u8 high)
4772{
4773 /* FIFO thresholds for pause flow control */
4774 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
4775 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
4776}
4777
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004778static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004779{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004780 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004781
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004782 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004783 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02004784
françois romieufaf1e782013-02-27 13:01:57 +00004785 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004786 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00004787 PCI_EXP_DEVCTL_NOSNOOP_EN);
4788 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004789}
4790
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004791static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004792{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004793 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004794
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004795 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004796
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004797 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004798}
4799
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004800static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004801{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004802 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02004803
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004804 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004805
françois romieufaf1e782013-02-27 13:01:57 +00004806 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004807 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02004808
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004809 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004810
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004811 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004812 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02004813}
4814
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004815static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004816{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004817 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004818 { 0x01, 0, 0x0001 },
4819 { 0x02, 0x0800, 0x1000 },
4820 { 0x03, 0, 0x0042 },
4821 { 0x06, 0x0080, 0x0000 },
4822 { 0x07, 0, 0x2000 }
4823 };
4824
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004825 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004826
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004827 rtl_ephy_init(tp, e_info_8168cp);
Francois Romieub726e492008-06-28 12:22:59 +02004828
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004829 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004830}
4831
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004832static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004833{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004834 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004835
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004836 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02004837
françois romieufaf1e782013-02-27 13:01:57 +00004838 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004839 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02004840
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004841 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004842 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02004843}
4844
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004845static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004846{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004847 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004848
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004849 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004850
4851 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004852 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004853
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004854 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004855
françois romieufaf1e782013-02-27 13:01:57 +00004856 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004857 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004858
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004859 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004860 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004861}
4862
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004863static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004864{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004865 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004866 { 0x02, 0x0800, 0x1000 },
4867 { 0x03, 0, 0x0002 },
4868 { 0x06, 0x0080, 0x0000 }
4869 };
4870
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004871 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004872
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004873 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02004874
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004875 rtl_ephy_init(tp, e_info_8168c_1);
Francois Romieub726e492008-06-28 12:22:59 +02004876
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004877 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004878}
4879
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004880static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004881{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004882 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004883 { 0x01, 0, 0x0001 },
4884 { 0x03, 0x0400, 0x0220 }
4885 };
4886
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004887 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004888
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004889 rtl_ephy_init(tp, e_info_8168c_2);
Francois Romieub726e492008-06-28 12:22:59 +02004890
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004891 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004892}
4893
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004894static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004895{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004896 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004897}
4898
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004899static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004900{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004901 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004902
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004903 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004904}
4905
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004906static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004907{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004908 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004909
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004910 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004911
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004912 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004913
françois romieufaf1e782013-02-27 13:01:57 +00004914 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004915 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02004916
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004917 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004918 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02004919}
4920
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004921static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004922{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004923 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004924
françois romieufaf1e782013-02-27 13:01:57 +00004925 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004926 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00004927
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004928 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00004929
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004930 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004931}
4932
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004933static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004934{
4935 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004936 { 0x0b, 0x0000, 0x0048 },
4937 { 0x19, 0x0020, 0x0050 },
4938 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00004939 };
françois romieue6de30d2011-01-03 15:08:37 +00004940
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004941 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004942
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004943 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00004944
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004945 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00004946
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004947 rtl_ephy_init(tp, e_info_8168d_4);
françois romieue6de30d2011-01-03 15:08:37 +00004948
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004949 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004950}
4951
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004952static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004953{
Hayes Wang70090422011-07-06 15:58:06 +08004954 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004955 { 0x00, 0x0200, 0x0100 },
4956 { 0x00, 0x0000, 0x0004 },
4957 { 0x06, 0x0002, 0x0001 },
4958 { 0x06, 0x0000, 0x0030 },
4959 { 0x07, 0x0000, 0x2000 },
4960 { 0x00, 0x0000, 0x0020 },
4961 { 0x03, 0x5800, 0x2000 },
4962 { 0x03, 0x0000, 0x0001 },
4963 { 0x01, 0x0800, 0x1000 },
4964 { 0x07, 0x0000, 0x4000 },
4965 { 0x1e, 0x0000, 0x2000 },
4966 { 0x19, 0xffff, 0xfe6c },
4967 { 0x0a, 0x0000, 0x0040 }
4968 };
4969
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004970 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004971
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004972 rtl_ephy_init(tp, e_info_8168e_1);
hayeswang01dc7fe2011-03-21 01:50:28 +00004973
françois romieufaf1e782013-02-27 13:01:57 +00004974 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004975 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00004976
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004977 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00004978
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004979 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004980
4981 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004982 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4983 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004984
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004985 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004986}
4987
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004988static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004989{
4990 static const struct ephy_info e_info_8168e_2[] = {
4991 { 0x09, 0x0000, 0x0080 },
4992 { 0x19, 0x0000, 0x0224 }
4993 };
4994
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004995 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004996
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004997 rtl_ephy_init(tp, e_info_8168e_2);
Hayes Wang70090422011-07-06 15:58:06 +08004998
françois romieufaf1e782013-02-27 13:01:57 +00004999 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005000 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005001
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005002 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5003 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02005004 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005005 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
5006 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02005007 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005008 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang70090422011-07-06 15:58:06 +08005009
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005010 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005011
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005012 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005013
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005014 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005015
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01005016 rtl8168_config_eee_mac(tp);
5017
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005018 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5019 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5020 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02005021
5022 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08005023}
5024
Hayes Wang5f886e02012-03-30 14:33:03 +08005025static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005026{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005027 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005028
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005029 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005030
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005031 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5032 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02005033 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02005034 rtl_reset_packet_filter(tp);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02005035 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
5036 rtl_eri_set_bits(tp, 0x1d0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005037 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
5038 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
Hayes Wangc2218922011-09-06 16:55:18 +08005039
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005040 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005041
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005042 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005043
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005044 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5045 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5046 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5047 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01005048
5049 rtl8168_config_eee_mac(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005050}
5051
Hayes Wang5f886e02012-03-30 14:33:03 +08005052static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5053{
Hayes Wang5f886e02012-03-30 14:33:03 +08005054 static const struct ephy_info e_info_8168f_1[] = {
5055 { 0x06, 0x00c0, 0x0020 },
5056 { 0x08, 0x0001, 0x0002 },
5057 { 0x09, 0x0000, 0x0080 },
5058 { 0x19, 0x0000, 0x0224 }
5059 };
5060
5061 rtl_hw_start_8168f(tp);
5062
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005063 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wang5f886e02012-03-30 14:33:03 +08005064
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005065 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang5f886e02012-03-30 14:33:03 +08005066}
5067
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005068static void rtl_hw_start_8411(struct rtl8169_private *tp)
5069{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005070 static const struct ephy_info e_info_8168f_1[] = {
5071 { 0x06, 0x00c0, 0x0020 },
5072 { 0x0f, 0xffff, 0x5200 },
5073 { 0x1e, 0x0000, 0x4000 },
5074 { 0x19, 0x0000, 0x0224 }
5075 };
5076
5077 rtl_hw_start_8168f(tp);
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005078 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005079
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005080 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005081
Heiner Kallweite719b3e2019-04-28 11:11:47 +02005082 rtl_eri_set_bits(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005083}
5084
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005085static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005086{
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02005087 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02005088 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Hayes Wangc5583862012-07-02 17:23:22 +08005089
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005090 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08005091
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005092 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005093
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02005094 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005095 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
Hayes Wangc5583862012-07-02 17:23:22 +08005096
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005097 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5098 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005099
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005100 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5101 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08005102
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01005103 rtl8168_config_eee_mac(tp);
5104
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005105 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02005106 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
hayeswangb51ecea2014-07-09 14:52:51 +08005107
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005108 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08005109}
5110
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005111static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5112{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005113 static const struct ephy_info e_info_8168g_1[] = {
5114 { 0x00, 0x0000, 0x0008 },
5115 { 0x0c, 0x37d0, 0x0820 },
5116 { 0x1e, 0x0000, 0x0001 },
5117 { 0x19, 0x8000, 0x0000 }
5118 };
5119
5120 rtl_hw_start_8168g(tp);
5121
5122 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005123 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005124 rtl_ephy_init(tp, e_info_8168g_1);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005125 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005126}
5127
hayeswang57538c42013-04-01 22:23:40 +00005128static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5129{
hayeswang57538c42013-04-01 22:23:40 +00005130 static const struct ephy_info e_info_8168g_2[] = {
5131 { 0x00, 0x0000, 0x0008 },
5132 { 0x0c, 0x3df0, 0x0200 },
5133 { 0x19, 0xffff, 0xfc00 },
5134 { 0x1e, 0xffff, 0x20eb }
5135 };
5136
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005137 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005138
5139 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005140 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5141 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005142 rtl_ephy_init(tp, e_info_8168g_2);
hayeswang57538c42013-04-01 22:23:40 +00005143}
5144
hayeswang45dd95c2013-07-08 17:09:01 +08005145static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5146{
hayeswang45dd95c2013-07-08 17:09:01 +08005147 static const struct ephy_info e_info_8411_2[] = {
5148 { 0x00, 0x0000, 0x0008 },
5149 { 0x0c, 0x3df0, 0x0200 },
5150 { 0x0f, 0xffff, 0x5200 },
5151 { 0x19, 0x0020, 0x0000 },
5152 { 0x1e, 0x0000, 0x2000 }
5153 };
5154
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005155 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005156
5157 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005158 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005159 rtl_ephy_init(tp, e_info_8411_2);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005160 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08005161}
5162
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005163static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5164{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005165 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005166 u32 data;
5167 static const struct ephy_info e_info_8168h_1[] = {
5168 { 0x1e, 0x0800, 0x0001 },
5169 { 0x1d, 0x0000, 0x0800 },
5170 { 0x05, 0xffff, 0x2089 },
5171 { 0x06, 0xffff, 0x5881 },
5172 { 0x04, 0xffff, 0x154a },
5173 { 0x01, 0xffff, 0x068b }
5174 };
5175
5176 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005177 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005178 rtl_ephy_init(tp, e_info_8168h_1);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005179
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02005180 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02005181 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005182
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005183 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005184
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005185 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005186
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02005187 rtl_reset_packet_filter(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005188
Heiner Kallweite719b3e2019-04-28 11:11:47 +02005189 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_1111, BIT(4));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005190
Heiner Kallweite719b3e2019-04-28 11:11:47 +02005191 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005192
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005193 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005194
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005195 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5196 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005197
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005198 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5199 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005200
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01005201 rtl8168_config_eee_mac(tp);
5202
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005203 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5204 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005205
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005206 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005207
Heiner Kallweite719b3e2019-04-28 11:11:47 +02005208 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005209
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005210 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005211
5212 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005213 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005214 rtl_writephy(tp, 0x1f, 0x0000);
5215 if (rg_saw_cnt > 0) {
5216 u16 sw_cnt_1ms_ini;
5217
5218 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5219 sw_cnt_1ms_ini &= 0x0fff;
5220 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005221 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005222 data |= sw_cnt_1ms_ini;
5223 r8168_mac_ocp_write(tp, 0xd412, data);
5224 }
5225
5226 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005227 data &= ~0xf0;
5228 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005229 r8168_mac_ocp_write(tp, 0xe056, data);
5230
5231 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005232 data &= ~0x6000;
5233 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005234 r8168_mac_ocp_write(tp, 0xe052, data);
5235
5236 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005237 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005238 data |= 0x017f;
5239 r8168_mac_ocp_write(tp, 0xe0d6, data);
5240
5241 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005242 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005243 data |= 0x047f;
5244 r8168_mac_ocp_write(tp, 0xd420, data);
5245
5246 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5247 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5248 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5249 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005250
5251 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005252}
5253
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005254static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5255{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005256 rtl8168ep_stop_cmac(tp);
5257
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02005258 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02005259 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005260
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005261 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005262
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005263 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005264
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02005265 rtl_reset_packet_filter(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005266
Heiner Kallweite719b3e2019-04-28 11:11:47 +02005267 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f80);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005268
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005269 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005270
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005271 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5272 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005273
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005274 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5275 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005276
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01005277 rtl8168_config_eee_mac(tp);
5278
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005279 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005280
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005281 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005282
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005283 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005284}
5285
5286static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5287{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005288 static const struct ephy_info e_info_8168ep_1[] = {
5289 { 0x00, 0xffff, 0x10ab },
5290 { 0x06, 0xffff, 0xf030 },
5291 { 0x08, 0xffff, 0x2006 },
5292 { 0x0d, 0xffff, 0x1666 },
5293 { 0x0c, 0x3ff0, 0x0000 }
5294 };
5295
5296 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005297 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005298 rtl_ephy_init(tp, e_info_8168ep_1);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005299
5300 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005301
5302 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005303}
5304
5305static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5306{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005307 static const struct ephy_info e_info_8168ep_2[] = {
5308 { 0x00, 0xffff, 0x10a3 },
5309 { 0x19, 0xffff, 0xfc00 },
5310 { 0x1e, 0xffff, 0x20ea }
5311 };
5312
5313 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005314 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005315 rtl_ephy_init(tp, e_info_8168ep_2);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005316
5317 rtl_hw_start_8168ep(tp);
5318
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005319 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5320 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005321
5322 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005323}
5324
5325static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5326{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005327 u32 data;
5328 static const struct ephy_info e_info_8168ep_3[] = {
5329 { 0x00, 0xffff, 0x10a3 },
5330 { 0x19, 0xffff, 0x7c00 },
5331 { 0x1e, 0xffff, 0x20eb },
5332 { 0x0d, 0xffff, 0x1666 }
5333 };
5334
5335 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005336 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005337 rtl_ephy_init(tp, e_info_8168ep_3);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005338
5339 rtl_hw_start_8168ep(tp);
5340
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005341 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5342 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005343
5344 data = r8168_mac_ocp_read(tp, 0xd3e2);
5345 data &= 0xf000;
5346 data |= 0x0271;
5347 r8168_mac_ocp_write(tp, 0xd3e2, data);
5348
5349 data = r8168_mac_ocp_read(tp, 0xd3e4);
5350 data &= 0xff00;
5351 r8168_mac_ocp_write(tp, 0xd3e4, data);
5352
5353 data = r8168_mac_ocp_read(tp, 0xe860);
5354 data |= 0x0080;
5355 r8168_mac_ocp_write(tp, 0xe860, data);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005356
5357 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005358}
5359
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005360static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005361{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005362 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005363 { 0x01, 0, 0x6e65 },
5364 { 0x02, 0, 0x091f },
5365 { 0x03, 0, 0xc2f9 },
5366 { 0x06, 0, 0xafb5 },
5367 { 0x07, 0, 0x0e00 },
5368 { 0x19, 0, 0xec80 },
5369 { 0x01, 0, 0x2e65 },
5370 { 0x01, 0, 0x6e65 }
5371 };
5372 u8 cfg1;
5373
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005374 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005375
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005376 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005377
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005378 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005379
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005380 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02005381 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005382 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005383
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005384 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005385 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005386 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005387
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005388 rtl_ephy_init(tp, e_info_8102e_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005389}
5390
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005391static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005392{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005393 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005394
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005395 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005396
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005397 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5398 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005399}
5400
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005401static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005402{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005403 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005404
Francois Romieufdf6fc02012-07-06 22:40:38 +02005405 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005406}
5407
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005408static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005409{
5410 static const struct ephy_info e_info_8105e_1[] = {
5411 { 0x07, 0, 0x4000 },
5412 { 0x19, 0, 0x0200 },
5413 { 0x19, 0, 0x0020 },
5414 { 0x1e, 0, 0x2000 },
5415 { 0x03, 0, 0x0001 },
5416 { 0x19, 0, 0x0100 },
5417 { 0x19, 0, 0x0004 },
5418 { 0x0a, 0, 0x0020 }
5419 };
5420
Francois Romieucecb5fd2011-04-01 10:21:07 +02005421 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005422 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005423
Francois Romieucecb5fd2011-04-01 10:21:07 +02005424 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005425 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005426
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005427 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5428 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005429
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005430 rtl_ephy_init(tp, e_info_8105e_1);
hayeswangb51ecea2014-07-09 14:52:51 +08005431
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005432 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005433}
5434
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005435static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005436{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005437 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005438 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005439}
5440
Hayes Wang7e18dca2012-03-30 14:33:02 +08005441static void rtl_hw_start_8402(struct rtl8169_private *tp)
5442{
Hayes Wang7e18dca2012-03-30 14:33:02 +08005443 static const struct ephy_info e_info_8402[] = {
5444 { 0x19, 0xffff, 0xff64 },
5445 { 0x1e, 0, 0x4000 }
5446 };
5447
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005448 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005449
5450 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005451 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005452
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005453 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005454
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005455 rtl_ephy_init(tp, e_info_8402);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005456
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005457 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005458
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02005459 rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02005460 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005461 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5462 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
5463 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00);
hayeswangb51ecea2014-07-09 14:52:51 +08005464
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005465 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005466}
5467
Hayes Wang5598bfe2012-07-02 17:23:21 +08005468static void rtl_hw_start_8106(struct rtl8169_private *tp)
5469{
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005470 rtl_hw_aspm_clkreq_enable(tp, false);
5471
Hayes Wang5598bfe2012-07-02 17:23:21 +08005472 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005473 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005474
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005475 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5476 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5477 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005478
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005479 rtl_pcie_state_l2l3_disable(tp);
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005480 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005481}
5482
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005483static void rtl_hw_config(struct rtl8169_private *tp)
5484{
5485 static const rtl_generic_fct hw_configs[] = {
5486 [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
5487 [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
5488 [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
5489 [RTL_GIGA_MAC_VER_10] = NULL,
5490 [RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168bb,
5491 [RTL_GIGA_MAC_VER_12] = rtl_hw_start_8168bef,
5492 [RTL_GIGA_MAC_VER_13] = NULL,
5493 [RTL_GIGA_MAC_VER_14] = NULL,
5494 [RTL_GIGA_MAC_VER_15] = NULL,
5495 [RTL_GIGA_MAC_VER_16] = NULL,
5496 [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168bef,
5497 [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
5498 [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
5499 [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
5500 [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_3,
5501 [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
5502 [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
5503 [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
5504 [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
5505 [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
5506 [RTL_GIGA_MAC_VER_27] = rtl_hw_start_8168d,
5507 [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
5508 [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
5509 [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
5510 [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168dp,
5511 [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
5512 [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
5513 [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
5514 [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
5515 [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
5516 [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
5517 [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
5518 [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
5519 [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
5520 [RTL_GIGA_MAC_VER_41] = rtl_hw_start_8168g_1,
5521 [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
5522 [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
5523 [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
5524 [RTL_GIGA_MAC_VER_45] = rtl_hw_start_8168h_1,
5525 [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
5526 [RTL_GIGA_MAC_VER_47] = rtl_hw_start_8168h_1,
5527 [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
5528 [RTL_GIGA_MAC_VER_49] = rtl_hw_start_8168ep_1,
5529 [RTL_GIGA_MAC_VER_50] = rtl_hw_start_8168ep_2,
5530 [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
5531 };
5532
5533 if (hw_configs[tp->mac_version])
5534 hw_configs[tp->mac_version](tp);
5535}
5536
5537static void rtl_hw_start_8168(struct rtl8169_private *tp)
5538{
5539 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5540
5541 /* Workaround for RxFIFO overflow. */
5542 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
5543 tp->irq_mask |= RxFIFOOver;
5544 tp->irq_mask &= ~RxOverflow;
5545 }
5546
5547 rtl_hw_config(tp);
5548}
5549
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005550static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005551{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005552 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
Heiner Kallweit559c3c02018-11-19 22:34:17 +01005553 tp->irq_mask &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005554
Francois Romieucecb5fd2011-04-01 10:21:07 +02005555 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005556 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005557 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005558 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005559
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005560 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005561
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005562 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005563 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00005564
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005565 rtl_hw_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566}
5567
5568static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5569{
Francois Romieud58d46b2011-05-03 16:38:29 +02005570 struct rtl8169_private *tp = netdev_priv(dev);
5571
Francois Romieud58d46b2011-05-03 16:38:29 +02005572 if (new_mtu > ETH_DATA_LEN)
5573 rtl_hw_jumbo_enable(tp);
5574 else
5575 rtl_hw_jumbo_disable(tp);
5576
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005578 netdev_update_features(dev);
5579
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581}
5582
5583static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5584{
Al Viro95e09182007-12-22 18:55:39 +00005585 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5587}
5588
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005589static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5590 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005592 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5593 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005594
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005595 kfree(*data_buff);
5596 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 rtl8169_make_unusable_by_asic(desc);
5598}
5599
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005600static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601{
5602 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5603
Alexander Duycka0750132014-12-11 15:02:17 -08005604 /* Force memory writes to complete before releasing descriptor */
5605 dma_wmb();
5606
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005607 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608}
5609
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005610static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5611 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005612{
5613 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005615 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02005616 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005618 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005619 if (!data)
5620 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005621
Heiner Kallweit6dc8b742019-01-20 11:12:01 +01005622 /* Memory should be properly aligned, but better check. */
5623 if (!IS_ALIGNED((unsigned long)data, 8)) {
5624 netdev_err_once(tp->dev, "RX buffer not 8-byte-aligned\n");
5625 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005626 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005627
Heiner Kallweit6dc8b742019-01-20 11:12:01 +01005628 mapping = dma_map_single(d, data, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005629 if (unlikely(dma_mapping_error(d, mapping))) {
5630 if (net_ratelimit())
5631 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005632 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
Heiner Kallweitd731af72018-04-17 23:26:41 +02005635 desc->addr = cpu_to_le64(mapping);
5636 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005637 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005638
5639err_out:
5640 kfree(data);
5641 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642}
5643
5644static void rtl8169_rx_clear(struct rtl8169_private *tp)
5645{
Francois Romieu07d3f512007-02-21 22:40:46 +01005646 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
5648 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005649 if (tp->Rx_databuff[i]) {
5650 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 tp->RxDescArray + i);
5652 }
5653 }
5654}
5655
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005656static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005658 desc->opts1 |= cpu_to_le32(RingEnd);
5659}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005660
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005661static int rtl8169_rx_fill(struct rtl8169_private *tp)
5662{
5663 unsigned int i;
5664
5665 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005666 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005667
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005668 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005669 if (!data) {
5670 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005671 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005672 }
5673 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005676 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5677 return 0;
5678
5679err_out:
5680 rtl8169_rx_clear(tp);
5681 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682}
5683
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005684static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 rtl8169_init_ring_indexes(tp);
5687
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005688 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5689 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005691 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692}
5693
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005694static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 struct TxDesc *desc)
5696{
5697 unsigned int len = tx_skb->len;
5698
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005699 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5700
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 desc->opts1 = 0x00;
5702 desc->opts2 = 0x00;
5703 desc->addr = 0x00;
5704 tx_skb->len = 0;
5705}
5706
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005707static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5708 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709{
5710 unsigned int i;
5711
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005712 for (i = 0; i < n; i++) {
5713 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 struct ring_info *tx_skb = tp->tx_skb + entry;
5715 unsigned int len = tx_skb->len;
5716
5717 if (len) {
5718 struct sk_buff *skb = tx_skb->skb;
5719
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005720 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 tp->TxDescArray + entry);
5722 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07005723 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 tx_skb->skb = NULL;
5725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 }
5727 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005728}
5729
5730static void rtl8169_tx_clear(struct rtl8169_private *tp)
5731{
5732 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 tp->cur_tx = tp->dirty_tx = 0;
Florian Westphald92060b2018-10-20 12:25:27 +02005734 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735}
5736
Francois Romieu4422bcd2012-01-26 11:23:32 +01005737static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738{
David Howellsc4028952006-11-22 14:57:56 +00005739 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005740 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
Francois Romieuda78dbf2012-01-26 14:18:23 +01005742 napi_disable(&tp->napi);
5743 netif_stop_queue(dev);
Paul E. McKenney16f11502018-11-05 17:07:39 -08005744 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
françois romieuc7c2c392011-12-04 20:30:52 +00005746 rtl8169_hw_reset(tp);
5747
Francois Romieu56de4142011-03-15 17:29:31 +01005748 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005749 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01005750
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005752 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753
Francois Romieuda78dbf2012-01-26 14:18:23 +01005754 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005755 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005756 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757}
5758
5759static void rtl8169_tx_timeout(struct net_device *dev)
5760{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005761 struct rtl8169_private *tp = netdev_priv(dev);
5762
5763 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764}
5765
Heiner Kallweit734c1402018-11-22 21:56:48 +01005766static __le32 rtl8169_get_txd_opts1(u32 opts0, u32 len, unsigned int entry)
5767{
5768 u32 status = opts0 | len;
5769
5770 if (entry == NUM_TX_DESC - 1)
5771 status |= RingEnd;
5772
5773 return cpu_to_le32(status);
5774}
5775
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005777 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
5779 struct skb_shared_info *info = skb_shinfo(skb);
5780 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005781 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005782 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
5784 entry = tp->cur_tx;
5785 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005786 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005788 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 void *addr;
5790
5791 entry = (entry + 1) % NUM_TX_DESC;
5792
5793 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005794 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005795 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005796 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005797 if (unlikely(dma_mapping_error(d, mapping))) {
5798 if (net_ratelimit())
5799 netif_err(tp, drv, tp->dev,
5800 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005801 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
Heiner Kallweit734c1402018-11-22 21:56:48 +01005804 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005805 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 txd->addr = cpu_to_le64(mapping);
5807
5808 tp->tx_skb[entry].len = len;
5809 }
5810
5811 if (cur_frag) {
5812 tp->tx_skb[entry].skb = skb;
5813 txd->opts1 |= cpu_to_le32(LastFrag);
5814 }
5815
5816 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005817
5818err_out:
5819 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5820 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821}
5822
françois romieub423e9a2013-05-18 01:24:46 +00005823static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5824{
5825 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5826}
5827
hayeswange9746042014-07-11 16:25:58 +08005828static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5829 struct net_device *dev);
5830/* r8169_csum_workaround()
5831 * The hw limites the value the transport offset. When the offset is out of the
5832 * range, calculate the checksum by sw.
5833 */
5834static void r8169_csum_workaround(struct rtl8169_private *tp,
5835 struct sk_buff *skb)
5836{
5837 if (skb_shinfo(skb)->gso_size) {
5838 netdev_features_t features = tp->dev->features;
5839 struct sk_buff *segs, *nskb;
5840
5841 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
5842 segs = skb_gso_segment(skb, features);
5843 if (IS_ERR(segs) || !segs)
5844 goto drop;
5845
5846 do {
5847 nskb = segs;
5848 segs = segs->next;
5849 nskb->next = NULL;
5850 rtl8169_start_xmit(nskb, tp->dev);
5851 } while (segs);
5852
Alexander Duyckeb781392015-05-01 10:34:44 -07005853 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08005854 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5855 if (skb_checksum_help(skb) < 0)
5856 goto drop;
5857
5858 rtl8169_start_xmit(skb, tp->dev);
5859 } else {
5860 struct net_device_stats *stats;
5861
5862drop:
5863 stats = &tp->dev->stats;
5864 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07005865 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08005866 }
5867}
5868
5869/* msdn_giant_send_check()
5870 * According to the document of microsoft, the TCP Pseudo Header excludes the
5871 * packet length for IPv6 TCP large packets.
5872 */
5873static int msdn_giant_send_check(struct sk_buff *skb)
5874{
5875 const struct ipv6hdr *ipv6h;
5876 struct tcphdr *th;
5877 int ret;
5878
5879 ret = skb_cow_head(skb, 0);
5880 if (ret)
5881 return ret;
5882
5883 ipv6h = ipv6_hdr(skb);
5884 th = tcp_hdr(skb);
5885
5886 th->check = 0;
5887 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
5888
5889 return ret;
5890}
5891
hayeswang5888d3f2014-07-11 16:25:56 +08005892static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
5893 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894{
Michał Mirosław350fb322011-04-08 06:35:56 +00005895 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
Francois Romieu2b7b4312011-04-18 22:53:24 -07005897 if (mss) {
5898 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08005899 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5900 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5901 const struct iphdr *ip = ip_hdr(skb);
5902
5903 if (ip->protocol == IPPROTO_TCP)
5904 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5905 else if (ip->protocol == IPPROTO_UDP)
5906 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5907 else
5908 WARN_ON_ONCE(1);
5909 }
5910
5911 return true;
5912}
5913
5914static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5915 struct sk_buff *skb, u32 *opts)
5916{
hayeswangbdfa4ed2014-07-11 16:25:57 +08005917 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08005918 u32 mss = skb_shinfo(skb)->gso_size;
5919
5920 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08005921 if (transport_offset > GTTCPHO_MAX) {
5922 netif_warn(tp, tx_err, tp->dev,
5923 "Invalid transport offset 0x%x for TSO\n",
5924 transport_offset);
5925 return false;
5926 }
5927
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005928 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005929 case htons(ETH_P_IP):
5930 opts[0] |= TD1_GTSENV4;
5931 break;
5932
5933 case htons(ETH_P_IPV6):
5934 if (msdn_giant_send_check(skb))
5935 return false;
5936
5937 opts[0] |= TD1_GTSENV6;
5938 break;
5939
5940 default:
5941 WARN_ON_ONCE(1);
5942 break;
5943 }
5944
hayeswangbdfa4ed2014-07-11 16:25:57 +08005945 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08005946 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005947 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08005948 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
françois romieub423e9a2013-05-18 01:24:46 +00005950 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08005951 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00005952
hayeswange9746042014-07-11 16:25:58 +08005953 if (transport_offset > TCPHO_MAX) {
5954 netif_warn(tp, tx_err, tp->dev,
5955 "Invalid transport offset 0x%x\n",
5956 transport_offset);
5957 return false;
5958 }
5959
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005960 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005961 case htons(ETH_P_IP):
5962 opts[1] |= TD1_IPv4_CS;
5963 ip_protocol = ip_hdr(skb)->protocol;
5964 break;
5965
5966 case htons(ETH_P_IPV6):
5967 opts[1] |= TD1_IPv6_CS;
5968 ip_protocol = ipv6_hdr(skb)->nexthdr;
5969 break;
5970
5971 default:
5972 ip_protocol = IPPROTO_RAW;
5973 break;
5974 }
5975
5976 if (ip_protocol == IPPROTO_TCP)
5977 opts[1] |= TD1_TCP_CS;
5978 else if (ip_protocol == IPPROTO_UDP)
5979 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005980 else
5981 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08005982
5983 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00005984 } else {
5985 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08005986 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 }
hayeswang5888d3f2014-07-11 16:25:56 +08005988
françois romieub423e9a2013-05-18 01:24:46 +00005989 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990}
5991
Heiner Kallweit76085c92018-11-22 22:03:08 +01005992static bool rtl_tx_slots_avail(struct rtl8169_private *tp,
5993 unsigned int nr_frags)
5994{
5995 unsigned int slots_avail = tp->dirty_tx + NUM_TX_DESC - tp->cur_tx;
5996
5997 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
5998 return slots_avail > nr_frags;
5999}
6000
Stephen Hemminger613573252009-08-31 19:50:58 +00006001static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6002 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
6004 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006005 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006007 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01006009 u32 opts[2], len;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006010 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006011
Heiner Kallweit76085c92018-11-22 22:03:08 +01006012 if (unlikely(!rtl_tx_slots_avail(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006013 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006014 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 }
6016
6017 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006018 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
françois romieub423e9a2013-05-18 01:24:46 +00006020 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6021 opts[0] = DescOwn;
6022
hayeswange9746042014-07-11 16:25:58 +08006023 if (!tp->tso_csum(tp, skb, opts)) {
6024 r8169_csum_workaround(tp, skb);
6025 return NETDEV_TX_OK;
6026 }
françois romieub423e9a2013-05-18 01:24:46 +00006027
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006028 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006029 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006030 if (unlikely(dma_mapping_error(d, mapping))) {
6031 if (net_ratelimit())
6032 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006033 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
6036 tp->tx_skb[entry].len = len;
6037 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
Francois Romieu2b7b4312011-04-18 22:53:24 -07006039 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006040 if (frags < 0)
6041 goto err_dma_1;
6042 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006043 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006044 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006045 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006046 tp->tx_skb[entry].skb = skb;
6047 }
6048
Francois Romieu2b7b4312011-04-18 22:53:24 -07006049 txd->opts2 = cpu_to_le32(opts[1]);
6050
Heiner Kallweit0255d592019-02-10 15:28:04 +01006051 netdev_sent_queue(dev, skb->len);
6052
Richard Cochran5047fb52012-03-10 07:29:42 +00006053 skb_tx_timestamp(skb);
6054
Alexander Duycka0750132014-12-11 15:02:17 -08006055 /* Force memory writes to complete before releasing descriptor */
6056 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
Heiner Kallweit734c1402018-11-22 21:56:48 +01006058 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
Alexander Duycka0750132014-12-11 15:02:17 -08006060 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006061 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
Alexander Duycka0750132014-12-11 15:02:17 -08006063 tp->cur_tx += frags + 1;
6064
Heiner Kallweit0255d592019-02-10 15:28:04 +01006065 RTL_W8(tp, TxPoll, NPQ);
6066
Heiner Kallweit0255d592019-02-10 15:28:04 +01006067 if (!rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
6068 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6069 * not miss a ring update when it notices a stopped queue.
6070 */
6071 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006073 /* Sync with rtl_tx:
6074 * - publish queue status and cur_tx ring index (write barrier)
6075 * - refresh dirty_tx ring index (read barrier).
6076 * May the current thread have a pessimistic view of the ring
6077 * status and forget to wake up queue, a racing rtl_tx thread
6078 * can't.
6079 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006080 smp_mb();
Heiner Kallweit76085c92018-11-22 22:03:08 +01006081 if (rtl_tx_slots_avail(tp, MAX_SKB_FRAGS))
Heiner Kallweit601ed4d2019-03-21 21:41:48 +01006082 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 }
6084
Stephen Hemminger613573252009-08-31 19:50:58 +00006085 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006087err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006088 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006089err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006090 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006091 dev->stats.tx_dropped++;
6092 return NETDEV_TX_OK;
6093
6094err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006096 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006097 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098}
6099
6100static void rtl8169_pcierr_interrupt(struct net_device *dev)
6101{
6102 struct rtl8169_private *tp = netdev_priv(dev);
6103 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 u16 pci_status, pci_cmd;
6105
6106 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6107 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6108
Joe Perchesbf82c182010-02-09 11:49:50 +00006109 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6110 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111
6112 /*
6113 * The recovery sequence below admits a very elaborated explanation:
6114 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006115 * - I did not see what else could be done;
6116 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 *
6118 * Feel free to adjust to your needs.
6119 */
Francois Romieua27993f2006-12-18 00:04:19 +01006120 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006121 pci_cmd &= ~PCI_COMMAND_PARITY;
6122 else
6123 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6124
6125 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
6127 pci_write_config_word(pdev, PCI_STATUS,
6128 pci_status & (PCI_STATUS_DETECTED_PARITY |
6129 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6130 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6131
Francois Romieu98ddf982012-01-31 10:47:34 +01006132 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133}
6134
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01006135static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
6136 int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137{
Florian Westphald92060b2018-10-20 12:25:27 +02006138 unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 dirty_tx = tp->dirty_tx;
6141 smp_rmb();
6142 tx_left = tp->cur_tx - dirty_tx;
6143
6144 while (tx_left > 0) {
6145 unsigned int entry = dirty_tx % NUM_TX_DESC;
6146 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 u32 status;
6148
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6150 if (status & DescOwn)
6151 break;
6152
Alexander Duycka0750132014-12-11 15:02:17 -08006153 /* This barrier is needed to keep us from reading
6154 * any other fields out of the Tx descriptor until
6155 * we know the status of DescOwn
6156 */
6157 dma_rmb();
6158
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006159 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006160 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 if (status & LastFrag) {
Florian Westphald92060b2018-10-20 12:25:27 +02006162 pkts_compl++;
6163 bytes_compl += tx_skb->skb->len;
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01006164 napi_consume_skb(tx_skb->skb, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 tx_skb->skb = NULL;
6166 }
6167 dirty_tx++;
6168 tx_left--;
6169 }
6170
6171 if (tp->dirty_tx != dirty_tx) {
Florian Westphald92060b2018-10-20 12:25:27 +02006172 netdev_completed_queue(dev, pkts_compl, bytes_compl);
6173
6174 u64_stats_update_begin(&tp->tx_stats.syncp);
6175 tp->tx_stats.packets += pkts_compl;
6176 tp->tx_stats.bytes += bytes_compl;
6177 u64_stats_update_end(&tp->tx_stats.syncp);
6178
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006180 /* Sync with rtl8169_start_xmit:
6181 * - publish dirty_tx ring index (write barrier)
6182 * - refresh cur_tx ring index and queue status (read barrier)
6183 * May the current thread miss the stopped queue condition,
6184 * a racing xmit thread can only have a right view of the
6185 * ring status.
6186 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006187 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 if (netif_queue_stopped(dev) &&
Heiner Kallweit76085c92018-11-22 22:03:08 +01006189 rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 netif_wake_queue(dev);
6191 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006192 /*
6193 * 8168 hack: TxPoll requests are lost when the Tx packets are
6194 * too close. Let's kick an extra TxPoll request when a burst
6195 * of start_xmit activity is detected (if it is not detected,
6196 * it is slow enough). -- FR
6197 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006198 if (tp->cur_tx != dirty_tx)
6199 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 }
6201}
6202
Francois Romieu126fa4b2005-05-12 20:09:17 -04006203static inline int rtl8169_fragmented_frame(u32 status)
6204{
6205 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6206}
6207
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006208static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 u32 status = opts1 & RxProtoMask;
6211
6212 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006213 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 skb->ip_summed = CHECKSUM_UNNECESSARY;
6215 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006216 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217}
6218
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006219static struct sk_buff *rtl8169_try_rx_copy(void *data,
6220 struct rtl8169_private *tp,
6221 int pkt_size,
6222 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006224 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006225 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006227 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006228 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006229 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006230 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006231 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006232 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6233
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006234 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235}
6236
Francois Romieuda78dbf2012-01-26 14:18:23 +01006237static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238{
6239 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006240 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
Timo Teräs9fba0812013-01-15 21:01:24 +00006244 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006246 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 u32 status;
6248
Heiner Kallweit62028062018-04-17 23:30:29 +02006249 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 if (status & DescOwn)
6251 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006252
6253 /* This barrier is needed to keep us from reading
6254 * any other fields out of the Rx descriptor until
6255 * we know the status of DescOwn
6256 */
6257 dma_rmb();
6258
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006259 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006260 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6261 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006262 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006264 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006266 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006267 /* RxFOVF is a reserved bit on later chip versions */
6268 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6269 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006270 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006271 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006272 } else if (status & (RxRUNT | RxCRC) &&
6273 !(status & RxRWT) &&
6274 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006275 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006278 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006279 dma_addr_t addr;
6280 int pkt_size;
6281
6282process_pkt:
6283 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006284 if (likely(!(dev->features & NETIF_F_RXFCS)))
6285 pkt_size = (status & 0x00003fff) - 4;
6286 else
6287 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
Francois Romieu126fa4b2005-05-12 20:09:17 -04006289 /*
6290 * The driver does not support incoming fragmented
6291 * frames. They are seen as a symptom of over-mtu
6292 * sized frames.
6293 */
6294 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006295 dev->stats.rx_dropped++;
6296 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006297 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006298 }
6299
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006300 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6301 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006302 if (!skb) {
6303 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006304 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 }
6306
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006307 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 skb_put(skb, pkt_size);
6309 skb->protocol = eth_type_trans(skb, dev);
6310
Francois Romieu7a8fc772011-03-01 17:18:33 +01006311 rtl8169_rx_vlan_tag(desc, skb);
6312
françois romieu39174292015-11-11 23:35:18 +01006313 if (skb->pkt_type == PACKET_MULTICAST)
6314 dev->stats.multicast++;
6315
Heiner Kallweit448a2412019-04-03 19:54:12 +02006316 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
Junchang Wang8027aa22012-03-04 23:30:32 +01006318 u64_stats_update_begin(&tp->rx_stats.syncp);
6319 tp->rx_stats.packets++;
6320 tp->rx_stats.bytes += pkt_size;
6321 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 }
françois romieuce11ff52013-01-24 13:30:06 +00006323release_descriptor:
6324 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006325 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 }
6327
6328 count = cur_rx - tp->cur_rx;
6329 tp->cur_rx = cur_rx;
6330
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 return count;
6332}
6333
Francois Romieu07d3f512007-02-21 22:40:46 +01006334static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006336 struct rtl8169_private *tp = dev_instance;
Heiner Kallweit3ff752c2019-01-19 22:02:40 +01006337 u16 status = RTL_R16(tp, IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
Heiner Kallweitc8248c62019-03-21 21:23:14 +01006339 if (!tp->irq_enabled || status == 0xffff || !(status & tp->irq_mask))
Heiner Kallweit05bbe552018-08-10 22:38:29 +02006340 return IRQ_NONE;
françois romieu811fd302011-12-04 20:30:45 +00006341
Heiner Kallweit38caff52018-10-18 22:19:28 +02006342 if (unlikely(status & SYSErr)) {
6343 rtl8169_pcierr_interrupt(tp->dev);
6344 goto out;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006345 }
6346
Heiner Kallweit703732f2019-01-19 22:07:05 +01006347 if (status & LinkChg)
6348 phy_mac_interrupt(tp->phydev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006349
Heiner Kallweit38caff52018-10-18 22:19:28 +02006350 if (unlikely(status & RxFIFOOver &&
6351 tp->mac_version == RTL_GIGA_MAC_VER_11)) {
6352 netif_stop_queue(tp->dev);
6353 /* XXX - Hack alert. See rtl_task(). */
6354 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6355 }
6356
Heiner Kallweite62b2fd2019-04-14 11:48:39 +02006357 rtl_irq_disable(tp);
6358 napi_schedule_irqoff(&tp->napi);
Heiner Kallweit38caff52018-10-18 22:19:28 +02006359out:
6360 rtl_ack_events(tp, status);
6361
6362 return IRQ_HANDLED;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006363}
6364
Francois Romieu4422bcd2012-01-26 11:23:32 +01006365static void rtl_task(struct work_struct *work)
6366{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006367 static const struct {
6368 int bitnr;
6369 void (*action)(struct rtl8169_private *);
6370 } rtl_work[] = {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006371 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01006372 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006373 struct rtl8169_private *tp =
6374 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006375 struct net_device *dev = tp->dev;
6376 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006377
Francois Romieuda78dbf2012-01-26 14:18:23 +01006378 rtl_lock_work(tp);
6379
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006380 if (!netif_running(dev) ||
6381 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006382 goto out_unlock;
6383
6384 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6385 bool pending;
6386
Francois Romieuda78dbf2012-01-26 14:18:23 +01006387 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006388 if (pending)
6389 rtl_work[i].action(tp);
6390 }
6391
6392out_unlock:
6393 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006394}
6395
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006396static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006398 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6399 struct net_device *dev = tp->dev;
Heiner Kallweit6b839b62018-10-18 19:56:01 +02006400 int work_done;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006401
Heiner Kallweit6b839b62018-10-18 19:56:01 +02006402 work_done = rtl_rx(dev, tp, (u32) budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006403
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01006404 rtl_tx(dev, tp, budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006405
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006406 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08006407 napi_complete_done(napi, work_done);
Heiner Kallweitfe716f82018-11-19 22:31:32 +01006408 rtl_irq_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 }
6410
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006411 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006414static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02006415{
6416 struct rtl8169_private *tp = netdev_priv(dev);
6417
6418 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6419 return;
6420
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006421 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6422 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02006423}
6424
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006425static void r8169_phylink_handler(struct net_device *ndev)
6426{
6427 struct rtl8169_private *tp = netdev_priv(ndev);
6428
6429 if (netif_carrier_ok(ndev)) {
6430 rtl_link_chg_patch(tp);
6431 pm_request_resume(&tp->pci_dev->dev);
6432 } else {
6433 pm_runtime_idle(&tp->pci_dev->dev);
6434 }
6435
6436 if (net_ratelimit())
Heiner Kallweit703732f2019-01-19 22:07:05 +01006437 phy_print_status(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006438}
6439
6440static int r8169_phy_connect(struct rtl8169_private *tp)
6441{
Heiner Kallweit703732f2019-01-19 22:07:05 +01006442 struct phy_device *phydev = tp->phydev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006443 phy_interface_t phy_mode;
6444 int ret;
6445
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02006446 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006447 PHY_INTERFACE_MODE_MII;
6448
6449 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6450 phy_mode);
6451 if (ret)
6452 return ret;
6453
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02006454 if (!tp->supports_gmii)
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006455 phy_set_max_speed(phydev, SPEED_100);
6456
Heiner Kallweit9cf9b842019-05-04 12:01:03 +02006457 phy_support_asym_pause(phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006458
6459 phy_attached_info(phydev);
6460
6461 return 0;
6462}
6463
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464static void rtl8169_down(struct net_device *dev)
6465{
6466 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
Heiner Kallweit703732f2019-01-19 22:07:05 +01006468 phy_stop(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006469
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006470 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006471 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472
Hayes Wang92fc43b2011-07-06 15:58:03 +08006473 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006474 /*
6475 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006476 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6477 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006478 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006479 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenney16f11502018-11-05 17:07:39 -08006482 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 rtl8169_tx_clear(tp);
6485
6486 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006487
6488 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489}
6490
6491static int rtl8169_close(struct net_device *dev)
6492{
6493 struct rtl8169_private *tp = netdev_priv(dev);
6494 struct pci_dev *pdev = tp->pci_dev;
6495
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006496 pm_runtime_get_sync(&pdev->dev);
6497
Francois Romieucecb5fd2011-04-01 10:21:07 +02006498 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006499 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006500
Francois Romieuda78dbf2012-01-26 14:18:23 +01006501 rtl_lock_work(tp);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006502 /* Clear all task flags */
6503 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006504
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006506 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507
Lekensteyn4ea72442013-07-22 09:53:30 +02006508 cancel_work_sync(&tp->wk.work);
6509
Heiner Kallweit703732f2019-01-19 22:07:05 +01006510 phy_disconnect(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006511
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006512 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006514 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6515 tp->RxPhyAddr);
6516 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6517 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 tp->TxDescArray = NULL;
6519 tp->RxDescArray = NULL;
6520
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006521 pm_runtime_put_sync(&pdev->dev);
6522
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 return 0;
6524}
6525
Francois Romieudc1c00c2012-03-08 10:06:18 +01006526#ifdef CONFIG_NET_POLL_CONTROLLER
6527static void rtl8169_netpoll(struct net_device *dev)
6528{
6529 struct rtl8169_private *tp = netdev_priv(dev);
6530
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03006531 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01006532}
6533#endif
6534
Francois Romieudf43ac72012-03-08 09:48:40 +01006535static int rtl_open(struct net_device *dev)
6536{
6537 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006538 struct pci_dev *pdev = tp->pci_dev;
6539 int retval = -ENOMEM;
6540
6541 pm_runtime_get_sync(&pdev->dev);
6542
6543 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006544 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006545 * dma_alloc_coherent provides more.
6546 */
6547 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6548 &tp->TxPhyAddr, GFP_KERNEL);
6549 if (!tp->TxDescArray)
6550 goto err_pm_runtime_put;
6551
6552 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6553 &tp->RxPhyAddr, GFP_KERNEL);
6554 if (!tp->RxDescArray)
6555 goto err_free_tx_0;
6556
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006557 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006558 if (retval < 0)
6559 goto err_free_rx_1;
6560
Francois Romieudf43ac72012-03-08 09:48:40 +01006561 rtl_request_firmware(tp);
6562
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006563 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006564 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01006565 if (retval < 0)
6566 goto err_release_fw_2;
6567
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006568 retval = r8169_phy_connect(tp);
6569 if (retval)
6570 goto err_free_irq;
6571
Francois Romieudf43ac72012-03-08 09:48:40 +01006572 rtl_lock_work(tp);
6573
6574 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6575
6576 napi_enable(&tp->napi);
6577
6578 rtl8169_init_phy(dev, tp);
6579
Francois Romieudf43ac72012-03-08 09:48:40 +01006580 rtl_pll_power_up(tp);
6581
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006582 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006583
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006584 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006585 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6586
Heiner Kallweit703732f2019-01-19 22:07:05 +01006587 phy_start(tp->phydev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006588 netif_start_queue(dev);
6589
6590 rtl_unlock_work(tp);
6591
Heiner Kallweita92a0842018-01-08 21:39:13 +01006592 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006593out:
6594 return retval;
6595
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006596err_free_irq:
6597 pci_free_irq(pdev, 0, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006598err_release_fw_2:
6599 rtl_release_firmware(tp);
6600 rtl8169_rx_clear(tp);
6601err_free_rx_1:
6602 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6603 tp->RxPhyAddr);
6604 tp->RxDescArray = NULL;
6605err_free_tx_0:
6606 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6607 tp->TxPhyAddr);
6608 tp->TxDescArray = NULL;
6609err_pm_runtime_put:
6610 pm_runtime_put_noidle(&pdev->dev);
6611 goto out;
6612}
6613
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006614static void
Junchang Wang8027aa22012-03-04 23:30:32 +01006615rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616{
6617 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006618 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02006619 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01006620 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006622 pm_runtime_get_noresume(&pdev->dev);
6623
6624 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006625 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006626
Junchang Wang8027aa22012-03-04 23:30:32 +01006627 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006628 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006629 stats->rx_packets = tp->rx_stats.packets;
6630 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006631 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006632
Junchang Wang8027aa22012-03-04 23:30:32 +01006633 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006634 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006635 stats->tx_packets = tp->tx_stats.packets;
6636 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006637 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006638
6639 stats->rx_dropped = dev->stats.rx_dropped;
6640 stats->tx_dropped = dev->stats.tx_dropped;
6641 stats->rx_length_errors = dev->stats.rx_length_errors;
6642 stats->rx_errors = dev->stats.rx_errors;
6643 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6644 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6645 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02006646 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01006647
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006648 /*
6649 * Fetch additonal counter values missing in stats collected by driver
6650 * from tally counters.
6651 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006652 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006653 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006654
6655 /*
6656 * Subtract values fetched during initalization.
6657 * See rtl8169_init_counter_offsets for a description why we do that.
6658 */
Corinna Vinschen42020322015-09-10 10:47:35 +02006659 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006660 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02006661 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006662 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02006663 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006664 le16_to_cpu(tp->tc_offset.tx_aborted);
6665
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006666 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667}
6668
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006669static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006670{
françois romieu065c27c2011-01-03 15:08:12 +00006671 struct rtl8169_private *tp = netdev_priv(dev);
6672
Francois Romieu5d06a992006-02-23 00:47:58 +01006673 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006674 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006675
Heiner Kallweit703732f2019-01-19 22:07:05 +01006676 phy_stop(tp->phydev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006677 netif_device_detach(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006678
6679 rtl_lock_work(tp);
6680 napi_disable(&tp->napi);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006681 /* Clear all task flags */
6682 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6683
Francois Romieuda78dbf2012-01-26 14:18:23 +01006684 rtl_unlock_work(tp);
6685
6686 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006687}
Francois Romieu5d06a992006-02-23 00:47:58 +01006688
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006689#ifdef CONFIG_PM
6690
6691static int rtl8169_suspend(struct device *device)
6692{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006693 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006694 struct rtl8169_private *tp = netdev_priv(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006695
6696 rtl8169_net_suspend(dev);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006697 clk_disable_unprepare(tp->clk);
Francois Romieu1371fa62007-04-02 23:01:11 +02006698
Francois Romieu5d06a992006-02-23 00:47:58 +01006699 return 0;
6700}
6701
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006702static void __rtl8169_resume(struct net_device *dev)
6703{
françois romieu065c27c2011-01-03 15:08:12 +00006704 struct rtl8169_private *tp = netdev_priv(dev);
6705
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006706 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006707
6708 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02006709 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00006710
Heiner Kallweit703732f2019-01-19 22:07:05 +01006711 phy_start(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006712
Artem Savkovcff4c162012-04-03 10:29:11 +00006713 rtl_lock_work(tp);
6714 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006715 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Heiner Kallweit58ba5662019-01-19 22:06:25 +01006716 rtl_reset_work(tp);
Artem Savkovcff4c162012-04-03 10:29:11 +00006717 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006718}
6719
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006720static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006721{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006722 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006723 struct rtl8169_private *tp = netdev_priv(dev);
6724
6725 clk_prepare_enable(tp->clk);
Francois Romieu5d06a992006-02-23 00:47:58 +01006726
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006727 if (netif_running(dev))
6728 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006729
Francois Romieu5d06a992006-02-23 00:47:58 +01006730 return 0;
6731}
6732
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006733static int rtl8169_runtime_suspend(struct device *device)
6734{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006735 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006736 struct rtl8169_private *tp = netdev_priv(dev);
6737
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006738 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006739 return 0;
6740
Francois Romieuda78dbf2012-01-26 14:18:23 +01006741 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006742 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006743 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006744
6745 rtl8169_net_suspend(dev);
6746
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006747 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006748 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006749 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006750
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006751 return 0;
6752}
6753
6754static int rtl8169_runtime_resume(struct device *device)
6755{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006756 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006757 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08006758 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006759
6760 if (!tp->TxDescArray)
6761 return 0;
6762
Francois Romieuda78dbf2012-01-26 14:18:23 +01006763 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006764 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006765 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006766
6767 __rtl8169_resume(dev);
6768
6769 return 0;
6770}
6771
6772static int rtl8169_runtime_idle(struct device *device)
6773{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006774 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006775
Heiner Kallweita92a0842018-01-08 21:39:13 +01006776 if (!netif_running(dev) || !netif_carrier_ok(dev))
6777 pm_schedule_suspend(device, 10000);
6778
6779 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006780}
6781
Alexey Dobriyan47145212009-12-14 18:00:08 -08006782static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006783 .suspend = rtl8169_suspend,
6784 .resume = rtl8169_resume,
6785 .freeze = rtl8169_suspend,
6786 .thaw = rtl8169_resume,
6787 .poweroff = rtl8169_suspend,
6788 .restore = rtl8169_resume,
6789 .runtime_suspend = rtl8169_runtime_suspend,
6790 .runtime_resume = rtl8169_runtime_resume,
6791 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006792};
6793
6794#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6795
6796#else /* !CONFIG_PM */
6797
6798#define RTL8169_PM_OPS NULL
6799
6800#endif /* !CONFIG_PM */
6801
David S. Miller1805b2f2011-10-24 18:18:09 -04006802static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6803{
David S. Miller1805b2f2011-10-24 18:18:09 -04006804 /* WoL fails with 8168b when the receiver is disabled. */
6805 switch (tp->mac_version) {
6806 case RTL_GIGA_MAC_VER_11:
6807 case RTL_GIGA_MAC_VER_12:
6808 case RTL_GIGA_MAC_VER_17:
6809 pci_clear_master(tp->pci_dev);
6810
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006811 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006812 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006813 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04006814 break;
6815 default:
6816 break;
6817 }
6818}
6819
Francois Romieu1765f952008-09-13 17:21:40 +02006820static void rtl_shutdown(struct pci_dev *pdev)
6821{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006822 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006823 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006824
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006825 rtl8169_net_suspend(dev);
6826
Francois Romieucecb5fd2011-04-01 10:21:07 +02006827 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006828 rtl_rar_set(tp, dev->perm_addr);
6829
Hayes Wang92fc43b2011-07-06 15:58:03 +08006830 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006831
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006832 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02006833 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006834 rtl_wol_suspend_quirk(tp);
6835 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006836 }
6837
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006838 pci_wake_from_d3(pdev, true);
6839 pci_set_power_state(pdev, PCI_D3hot);
6840 }
6841}
Francois Romieu5d06a992006-02-23 00:47:58 +01006842
Bill Pembertonbaf63292012-12-03 09:23:28 -05006843static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006844{
6845 struct net_device *dev = pci_get_drvdata(pdev);
6846 struct rtl8169_private *tp = netdev_priv(dev);
6847
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006848 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01006849 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01006850
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006851 netif_napi_del(&tp->napi);
6852
Francois Romieue27566e2012-03-08 09:54:01 +01006853 unregister_netdev(dev);
Heiner Kallweit703732f2019-01-19 22:07:05 +01006854 mdiobus_unregister(tp->phydev->mdio.bus);
Francois Romieue27566e2012-03-08 09:54:01 +01006855
6856 rtl_release_firmware(tp);
6857
6858 if (pci_dev_run_wake(pdev))
6859 pm_runtime_get_noresume(&pdev->dev);
6860
6861 /* restore original MAC address */
6862 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01006863}
6864
Francois Romieufa9c3852012-03-08 10:01:50 +01006865static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006866 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006867 .ndo_stop = rtl8169_close,
6868 .ndo_get_stats64 = rtl8169_get_stats64,
6869 .ndo_start_xmit = rtl8169_start_xmit,
6870 .ndo_tx_timeout = rtl8169_tx_timeout,
6871 .ndo_validate_addr = eth_validate_addr,
6872 .ndo_change_mtu = rtl8169_change_mtu,
6873 .ndo_fix_features = rtl8169_fix_features,
6874 .ndo_set_features = rtl8169_set_features,
6875 .ndo_set_mac_address = rtl_set_mac_address,
6876 .ndo_do_ioctl = rtl8169_ioctl,
6877 .ndo_set_rx_mode = rtl_set_rx_mode,
6878#ifdef CONFIG_NET_POLL_CONTROLLER
6879 .ndo_poll_controller = rtl8169_netpoll,
6880#endif
6881
6882};
6883
Francois Romieu31fa8b12012-03-08 10:09:40 +01006884static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006885 void (*hw_start)(struct rtl8169_private *tp);
Heiner Kallweit559c3c02018-11-19 22:34:17 +01006886 u16 irq_mask;
Heiner Kallweit14967f92018-02-28 07:55:20 +01006887 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03006888 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006889} rtl_cfg_infos [] = {
6890 [RTL_CFG_0] = {
6891 .hw_start = rtl_hw_start_8169,
Heiner Kallweit559c3c02018-11-19 22:34:17 +01006892 .irq_mask = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01006893 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03006894 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01006895 },
6896 [RTL_CFG_1] = {
6897 .hw_start = rtl_hw_start_8168,
Heiner Kallweit559c3c02018-11-19 22:34:17 +01006898 .irq_mask = LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01006899 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03006900 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01006901 },
6902 [RTL_CFG_2] = {
6903 .hw_start = rtl_hw_start_8101,
Heiner Kallweit559c3c02018-11-19 22:34:17 +01006904 .irq_mask = LinkChg | RxOverflow | RxFIFOOver,
Francois Romieu50970832017-10-27 13:24:49 +03006905 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01006906 }
6907};
6908
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006909static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01006910{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006911 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006912
Jian-Hong Pand49c88d2018-09-27 12:09:48 +08006913 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006914 rtl_unlock_config_regs(tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006915 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006916 rtl_lock_config_regs(tp);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006917 flags = PCI_IRQ_LEGACY;
Jian-Hong Pand49c88d2018-09-27 12:09:48 +08006918 } else {
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006919 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006920 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006921
6922 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01006923}
6924
Thierry Reding04c77882019-02-06 13:30:17 +01006925static void rtl_read_mac_address(struct rtl8169_private *tp,
6926 u8 mac_addr[ETH_ALEN])
6927{
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006928 u32 value;
6929
Thierry Reding04c77882019-02-06 13:30:17 +01006930 /* Get MAC address */
6931 switch (tp->mac_version) {
6932 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
6933 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02006934 value = rtl_eri_read(tp, 0xe0);
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006935 mac_addr[0] = (value >> 0) & 0xff;
6936 mac_addr[1] = (value >> 8) & 0xff;
6937 mac_addr[2] = (value >> 16) & 0xff;
6938 mac_addr[3] = (value >> 24) & 0xff;
6939
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02006940 value = rtl_eri_read(tp, 0xe4);
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006941 mac_addr[4] = (value >> 0) & 0xff;
6942 mac_addr[5] = (value >> 8) & 0xff;
Thierry Reding04c77882019-02-06 13:30:17 +01006943 break;
6944 default:
6945 break;
6946 }
6947}
6948
Hayes Wangc5583862012-07-02 17:23:22 +08006949DECLARE_RTL_COND(rtl_link_list_ready_cond)
6950{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006951 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08006952}
6953
6954DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6955{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006956 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08006957}
6958
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006959static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
6960{
6961 struct rtl8169_private *tp = mii_bus->priv;
6962
6963 if (phyaddr > 0)
6964 return -ENODEV;
6965
6966 return rtl_readphy(tp, phyreg);
6967}
6968
6969static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
6970 int phyreg, u16 val)
6971{
6972 struct rtl8169_private *tp = mii_bus->priv;
6973
6974 if (phyaddr > 0)
6975 return -ENODEV;
6976
6977 rtl_writephy(tp, phyreg, val);
6978
6979 return 0;
6980}
6981
6982static int r8169_mdio_register(struct rtl8169_private *tp)
6983{
6984 struct pci_dev *pdev = tp->pci_dev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006985 struct mii_bus *new_bus;
6986 int ret;
6987
6988 new_bus = devm_mdiobus_alloc(&pdev->dev);
6989 if (!new_bus)
6990 return -ENOMEM;
6991
6992 new_bus->name = "r8169";
6993 new_bus->priv = tp;
6994 new_bus->parent = &pdev->dev;
6995 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
Heiner Kallweita1950162019-04-24 21:13:25 +02006996 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x", pci_dev_id(pdev));
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006997
6998 new_bus->read = r8169_mdio_read_reg;
6999 new_bus->write = r8169_mdio_write_reg;
7000
7001 ret = mdiobus_register(new_bus);
7002 if (ret)
7003 return ret;
7004
Heiner Kallweit703732f2019-01-19 22:07:05 +01007005 tp->phydev = mdiobus_get_phy(new_bus, 0);
7006 if (!tp->phydev) {
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007007 mdiobus_unregister(new_bus);
7008 return -ENODEV;
7009 }
7010
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02007011 /* PHY will be woken up in rtl_open() */
Heiner Kallweit703732f2019-01-19 22:07:05 +01007012 phy_suspend(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007013
7014 return 0;
7015}
7016
Bill Pembertonbaf63292012-12-03 09:23:28 -05007017static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007018{
Hayes Wangc5583862012-07-02 17:23:22 +08007019 u32 data;
7020
7021 tp->ocp_base = OCP_STD_PHY_BASE;
7022
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007023 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007024
7025 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7026 return;
7027
7028 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7029 return;
7030
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007031 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007032 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007033 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007034
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007035 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007036 data &= ~(1 << 14);
7037 r8168_mac_ocp_write(tp, 0xe8de, data);
7038
7039 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7040 return;
7041
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007042 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007043 data |= (1 << 15);
7044 r8168_mac_ocp_write(tp, 0xe8de, data);
7045
7046 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7047 return;
7048}
7049
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007050static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7051{
7052 rtl8168ep_stop_cmac(tp);
7053 rtl_hw_init_8168g(tp);
7054}
7055
Bill Pembertonbaf63292012-12-03 09:23:28 -05007056static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007057{
7058 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007059 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007060 rtl_hw_init_8168g(tp);
7061 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007062 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007063 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007064 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007065 default:
7066 break;
7067 }
7068}
7069
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007070/* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
7071static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
7072{
7073 switch (tp->mac_version) {
7074 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7075 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
7076 return false;
7077 default:
7078 return true;
7079 }
7080}
7081
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007082static int rtl_jumbo_max(struct rtl8169_private *tp)
7083{
7084 /* Non-GBit versions don't support jumbo frames */
7085 if (!tp->supports_gmii)
7086 return JUMBO_1K;
7087
7088 switch (tp->mac_version) {
7089 /* RTL8169 */
7090 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7091 return JUMBO_7K;
7092 /* RTL8168b */
7093 case RTL_GIGA_MAC_VER_11:
7094 case RTL_GIGA_MAC_VER_12:
7095 case RTL_GIGA_MAC_VER_17:
7096 return JUMBO_4K;
7097 /* RTL8168c */
7098 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
7099 return JUMBO_6K;
7100 default:
7101 return JUMBO_9K;
7102 }
7103}
7104
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02007105static void rtl_disable_clk(void *data)
7106{
7107 clk_disable_unprepare(data);
7108}
7109
Heiner Kallweitb779dae2019-01-19 22:07:34 +01007110static int rtl_get_ether_clk(struct rtl8169_private *tp)
7111{
7112 struct device *d = tp_to_dev(tp);
7113 struct clk *clk;
7114 int rc;
7115
7116 clk = devm_clk_get(d, "ether_clk");
7117 if (IS_ERR(clk)) {
7118 rc = PTR_ERR(clk);
7119 if (rc == -ENOENT)
7120 /* clk-core allows NULL (for suspend / resume) */
7121 rc = 0;
7122 else if (rc != -EPROBE_DEFER)
7123 dev_err(d, "failed to get clk: %d\n", rc);
7124 } else {
7125 tp->clk = clk;
7126 rc = clk_prepare_enable(clk);
7127 if (rc)
7128 dev_err(d, "failed to enable clk: %d\n", rc);
7129 else
7130 rc = devm_add_action_or_reset(d, rtl_disable_clk, clk);
7131 }
7132
7133 return rc;
7134}
7135
hayeswang929a0312014-09-16 11:40:47 +08007136static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007137{
7138 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Thierry Redingdeedf1f2019-02-06 13:30:18 +01007139 /* align to u16 for is_valid_ether_addr() */
7140 u8 mac_addr[ETH_ALEN] __aligned(2) = {};
Francois Romieu3b6cf252012-03-08 09:59:04 +01007141 struct rtl8169_private *tp;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007142 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007143 int chipset, region, i;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007144 int jumbo_max, rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007145
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007146 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7147 if (!dev)
7148 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007149
7150 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007151 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007152 tp = netdev_priv(dev);
7153 tp->dev = dev;
7154 tp->pci_dev = pdev;
7155 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02007156 tp->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007157
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02007158 /* Get the *optional* external "ether_clk" used on some boards */
Heiner Kallweitb779dae2019-01-19 22:07:34 +01007159 rc = rtl_get_ether_clk(tp);
7160 if (rc)
7161 return rc;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02007162
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +02007163 /* Disable ASPM completely as that cause random device stop working
7164 * problems as well as full system hangs for some PCIe devices users.
7165 */
7166 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
7167
Francois Romieu3b6cf252012-03-08 09:59:04 +01007168 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007169 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007170 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007171 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007172 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007173 }
7174
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007175 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007176 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007177
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007178 /* use first MMIO region */
7179 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7180 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007181 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007182 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007183 }
7184
7185 /* check for weird/broken PCI region reporting */
7186 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007187 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007188 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007189 }
7190
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007191 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007192 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007193 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007194 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007195 }
7196
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007197 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007198
Francois Romieu3b6cf252012-03-08 09:59:04 +01007199 /* Identify chip attached to board */
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01007200 rtl8169_get_mac_version(tp);
7201 if (tp->mac_version == RTL_GIGA_MAC_NONE)
7202 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007203
Heiner Kallweite3972862018-06-29 08:07:04 +02007204 if (rtl_tbi_enabled(tp)) {
7205 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7206 return -ENODEV;
7207 }
7208
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007209 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007210
Heiner Kallweit10b63e82019-01-20 11:45:20 +01007211 if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
Heiner Kallweita0456792018-09-25 07:59:36 +02007212 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007213 dev->features |= NETIF_F_HIGHDMA;
7214 } else {
7215 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7216 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007217 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007218 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007219 }
7220 }
7221
Francois Romieu3b6cf252012-03-08 09:59:04 +01007222 rtl_init_rxcfg(tp);
7223
Heiner Kallweitde20e122018-09-25 07:58:00 +02007224 rtl8169_irq_mask_and_ack(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007225
Hayes Wangc5583862012-07-02 17:23:22 +08007226 rtl_hw_initialize(tp);
7227
Francois Romieu3b6cf252012-03-08 09:59:04 +01007228 rtl_hw_reset(tp);
7229
Francois Romieu3b6cf252012-03-08 09:59:04 +01007230 pci_set_master(pdev);
7231
Francois Romieu3b6cf252012-03-08 09:59:04 +01007232 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007233 rtl_init_jumbo_ops(tp);
7234
Francois Romieu3b6cf252012-03-08 09:59:04 +01007235 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007236
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007237 rc = rtl_alloc_irq(tp);
7238 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007239 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007240 return rc;
7241 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007242
Francois Romieu3b6cf252012-03-08 09:59:04 +01007243 mutex_init(&tp->wk.mutex);
Heiner Kallweit5c41e782019-01-19 22:03:49 +01007244 INIT_WORK(&tp->wk.work, rtl_task);
Kyle McMartin340fea32014-02-24 20:12:28 -05007245 u64_stats_init(&tp->rx_stats.syncp);
7246 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007247
Thierry Reding04c77882019-02-06 13:30:17 +01007248 /* get MAC address */
7249 rc = eth_platform_get_mac_address(&pdev->dev, mac_addr);
7250 if (rc)
7251 rtl_read_mac_address(tp, mac_addr);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007252
Thierry Reding04c77882019-02-06 13:30:17 +01007253 if (is_valid_ether_addr(mac_addr))
7254 rtl_rar_set(tp, mac_addr);
7255
Francois Romieu3b6cf252012-03-08 09:59:04 +01007256 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007257 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007258
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007259 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007260
Heiner Kallweit37621492018-04-17 23:20:03 +02007261 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007262
7263 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7264 * properly for all devices */
7265 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007266 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007267
7268 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007269 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7270 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007271 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7272 NETIF_F_HIGHDMA;
Heiner Kallweit2d0ec542018-07-02 22:49:35 +02007273 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007274
hayeswang929a0312014-09-16 11:40:47 +08007275 tp->cp_cmd |= RxChkSum | RxVlan;
7276
7277 /*
7278 * Pretend we are using VLANs; This bypasses a nasty bug where
7279 * Interrupts stop flowing on high load on 8110SCd controllers.
7280 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007281 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007282 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007283 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007284
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007285 if (rtl_chip_supports_csum_v2(tp)) {
hayeswang5888d3f2014-07-11 16:25:56 +08007286 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007287 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007288 } else {
7289 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007290 }
hayeswang5888d3f2014-07-11 16:25:56 +08007291
Francois Romieu3b6cf252012-03-08 09:59:04 +01007292 dev->hw_features |= NETIF_F_RXALL;
7293 dev->hw_features |= NETIF_F_RXFCS;
7294
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007295 /* MTU range: 60 - hw-specific max */
7296 dev->min_mtu = ETH_ZLEN;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007297 jumbo_max = rtl_jumbo_max(tp);
7298 dev->max_mtu = jumbo_max;
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007299
Francois Romieu3b6cf252012-03-08 09:59:04 +01007300 tp->hw_start = cfg->hw_start;
Heiner Kallweit559c3c02018-11-19 22:34:17 +01007301 tp->irq_mask = RTL_EVENT_NAPI | cfg->irq_mask;
Francois Romieu50970832017-10-27 13:24:49 +03007302 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007303
Heiner Kallweit254764e2019-01-22 22:23:41 +01007304 tp->fw_name = rtl_chip_infos[chipset].fw_name;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007305
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007306 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7307 &tp->counters_phys_addr,
7308 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007309 if (!tp->counters)
7310 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02007311
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02007312 pci_set_drvdata(pdev, dev);
7313
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007314 rc = r8169_mdio_register(tp);
7315 if (rc)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007316 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007317
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02007318 /* chip gets powered up in rtl_open() */
7319 rtl_pll_power_down(tp);
7320
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007321 rc = register_netdev(dev);
7322 if (rc)
7323 goto err_mdio_unregister;
7324
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01007325 netif_info(tp, probe, dev, "%s, %pM, XID %03x, IRQ %d\n",
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02007326 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01007327 (RTL_R32(tp, TxConfig) >> 20) & 0xfcf,
Heiner Kallweit29274992018-02-28 20:43:38 +01007328 pci_irq_vector(pdev, 0));
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007329
7330 if (jumbo_max > JUMBO_1K)
7331 netif_info(tp, probe, dev,
7332 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
7333 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
7334 "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007335
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007336 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01007337 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007338
Heiner Kallweita92a0842018-01-08 21:39:13 +01007339 if (pci_dev_run_wake(pdev))
7340 pm_runtime_put_sync(&pdev->dev);
7341
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007342 return 0;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007343
7344err_mdio_unregister:
Heiner Kallweit703732f2019-01-19 22:07:05 +01007345 mdiobus_unregister(tp->phydev->mdio.bus);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007346 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007347}
7348
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349static struct pci_driver rtl8169_pci_driver = {
7350 .name = MODULENAME,
7351 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007352 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007353 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007354 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007355 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356};
7357
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007358module_pci_driver(rtl8169_pci_driver);