blob: 8519f88aca37579ec1c53dd18d539cad8f0f013f [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Francois Romieu07d3f512007-02-21 22:40:46 +01003 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 *
5 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
6 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
7 * Copyright (c) a lot of people too. Please respect their work.
8 *
9 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/pci.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
Hans de Goedec2f6f3e2018-09-12 11:34:55 +020017#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
19#include <linux/ethtool.h>
Heiner Kallweitf1e911d2018-07-17 22:51:26 +020020#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/if_vlan.h>
22#include <linux/crc32.h>
23#include <linux/in.h>
Heiner Kallweit098b01a2018-08-10 22:37:31 +020024#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/ip.h>
26#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000027#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000029#include <linux/pm_runtime.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +020031#include <linux/pci-aspm.h>
hayeswange9746042014-07-11 16:25:58 +080032#include <linux/ipv6.h>
33#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Heiner Kallweit8197f9d2019-06-05 08:02:31 +020035#include "r8169_firmware.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080043#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080044#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080046#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080047#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080048#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080049#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080050#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000051#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000052#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000053#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080054#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
55#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
56#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
57#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000058
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070060 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
63 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050064static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Michal Schmidtaee77e42012-09-09 13:55:26 +000066#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
68
69#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020070#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000072#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
74#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
75
Heiner Kallweit145a40e2019-06-10 18:25:29 +020076#define RTL_CFG_NO_GBIT 1
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +020079#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
80#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
81#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
82#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
83#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
84#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86enum mac_version {
Heiner Kallweite9588eb2019-05-25 21:14:39 +020087 /* support for ancient RTL_GIGA_MAC_VER_01 has been removed */
Francois Romieu85bffe62011-04-27 08:22:39 +020088 RTL_GIGA_MAC_VER_02,
89 RTL_GIGA_MAC_VER_03,
90 RTL_GIGA_MAC_VER_04,
91 RTL_GIGA_MAC_VER_05,
92 RTL_GIGA_MAC_VER_06,
93 RTL_GIGA_MAC_VER_07,
94 RTL_GIGA_MAC_VER_08,
95 RTL_GIGA_MAC_VER_09,
96 RTL_GIGA_MAC_VER_10,
97 RTL_GIGA_MAC_VER_11,
98 RTL_GIGA_MAC_VER_12,
99 RTL_GIGA_MAC_VER_13,
100 RTL_GIGA_MAC_VER_14,
101 RTL_GIGA_MAC_VER_15,
102 RTL_GIGA_MAC_VER_16,
103 RTL_GIGA_MAC_VER_17,
104 RTL_GIGA_MAC_VER_18,
105 RTL_GIGA_MAC_VER_19,
106 RTL_GIGA_MAC_VER_20,
107 RTL_GIGA_MAC_VER_21,
108 RTL_GIGA_MAC_VER_22,
109 RTL_GIGA_MAC_VER_23,
110 RTL_GIGA_MAC_VER_24,
111 RTL_GIGA_MAC_VER_25,
112 RTL_GIGA_MAC_VER_26,
113 RTL_GIGA_MAC_VER_27,
114 RTL_GIGA_MAC_VER_28,
115 RTL_GIGA_MAC_VER_29,
116 RTL_GIGA_MAC_VER_30,
117 RTL_GIGA_MAC_VER_31,
118 RTL_GIGA_MAC_VER_32,
119 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800120 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800121 RTL_GIGA_MAC_VER_35,
122 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800123 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800124 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800125 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800126 RTL_GIGA_MAC_VER_40,
127 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000128 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000129 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800130 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800131 RTL_GIGA_MAC_VER_45,
132 RTL_GIGA_MAC_VER_46,
133 RTL_GIGA_MAC_VER_47,
134 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800135 RTL_GIGA_MAC_VER_49,
136 RTL_GIGA_MAC_VER_50,
137 RTL_GIGA_MAC_VER_51,
Heiner Kallweit76719ee2019-05-25 20:45:04 +0200138 RTL_GIGA_MAC_NONE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Francois Romieud58d46b2011-05-03 16:38:29 +0200141#define JUMBO_1K ETH_DATA_LEN
142#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
143#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
144#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
145#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
146
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800147static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *name;
Francois Romieu85bffe62011-04-27 08:22:39 +0200149 const char *fw_name;
150} rtl_chip_infos[] = {
151 /* PCI devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200152 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
153 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
154 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
155 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
156 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
Francois Romieu85bffe62011-04-27 08:22:39 +0200157 /* PCI-E devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200158 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
159 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
160 [RTL_GIGA_MAC_VER_09] = {"RTL8102e" },
161 [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
162 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
163 [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
164 [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
165 [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
166 [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
167 [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
168 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
169 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
170 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
171 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
172 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
173 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
174 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
175 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
176 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
177 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
178 [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
179 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
180 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
181 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
182 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
183 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
184 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
185 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
186 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
187 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
188 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
189 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
190 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
191 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
192 [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
193 [RTL_GIGA_MAC_VER_42] = {"RTL8168g/8111g", FIRMWARE_8168G_3},
194 [RTL_GIGA_MAC_VER_43] = {"RTL8106e", FIRMWARE_8106E_2},
195 [RTL_GIGA_MAC_VER_44] = {"RTL8411", FIRMWARE_8411_2 },
196 [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
197 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
198 [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
199 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
200 [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
201 [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
202 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Benoit Taine9baa3c32014-08-08 15:56:03 +0200205static const struct pci_device_id rtl8169_pci_tbl[] = {
Heiner Kallweit145a40e2019-06-10 18:25:29 +0200206 { PCI_VDEVICE(REALTEK, 0x2502) },
207 { PCI_VDEVICE(REALTEK, 0x2600) },
208 { PCI_VDEVICE(REALTEK, 0x8129) },
209 { PCI_VDEVICE(REALTEK, 0x8136), RTL_CFG_NO_GBIT },
210 { PCI_VDEVICE(REALTEK, 0x8161) },
211 { PCI_VDEVICE(REALTEK, 0x8167) },
212 { PCI_VDEVICE(REALTEK, 0x8168) },
213 { PCI_VDEVICE(NCUBE, 0x8168) },
214 { PCI_VDEVICE(REALTEK, 0x8169) },
Heiner Kallweit6f0d3082018-11-19 22:35:08 +0100215 { PCI_VENDOR_ID_DLINK, 0x4300,
Heiner Kallweit145a40e2019-06-10 18:25:29 +0200216 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0 },
217 { PCI_VDEVICE(DLINK, 0x4300), },
218 { PCI_VDEVICE(DLINK, 0x4302), },
219 { PCI_VDEVICE(AT, 0xc107), },
220 { PCI_VDEVICE(USR, 0x0116), },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200221 { PCI_VENDOR_ID_LINKSYS, 0x1032,
Heiner Kallweit145a40e2019-06-10 18:25:29 +0200222 PCI_ANY_ID, 0x0024, 0, 0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100223 { 0x0001, 0x8168,
Heiner Kallweit145a40e2019-06-10 18:25:29 +0200224 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_NO_GBIT },
Heiner Kallweit6f0d3082018-11-19 22:35:08 +0100225 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
228MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
229
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200230static struct {
231 u32 msg_enable;
232} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Francois Romieu07d3f512007-02-21 22:40:46 +0100234enum rtl_registers {
235 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100236 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100237 MAR0 = 8, /* Multicast filter. */
238 CounterAddrLow = 0x10,
239 CounterAddrHigh = 0x14,
240 TxDescStartAddrLow = 0x20,
241 TxDescStartAddrHigh = 0x24,
242 TxHDescStartAddrLow = 0x28,
243 TxHDescStartAddrHigh = 0x2c,
244 FLASH = 0x30,
245 ERSR = 0x36,
246 ChipCmd = 0x37,
247 TxPoll = 0x38,
248 IntrMask = 0x3c,
249 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700250
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800251 TxConfig = 0x40,
252#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
253#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
254
255 RxConfig = 0x44,
256#define RX128_INT_EN (1 << 15) /* 8111c and later */
257#define RX_MULTI_EN (1 << 14) /* 8111c only */
258#define RXCFG_FIFO_SHIFT 13
259 /* No threshold before first PCI xfer */
260#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000261#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800262#define RXCFG_DMA_SHIFT 8
263 /* Unlimited maximum PCI burst. */
264#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700265
Francois Romieu07d3f512007-02-21 22:40:46 +0100266 RxMissed = 0x4c,
267 Cfg9346 = 0x50,
268 Config0 = 0x51,
269 Config1 = 0x52,
270 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200271#define PME_SIGNAL (1 << 5) /* 8168c and later */
272
Francois Romieu07d3f512007-02-21 22:40:46 +0100273 Config3 = 0x54,
274 Config4 = 0x55,
275 Config5 = 0x56,
276 MultiIntr = 0x5c,
277 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100278 PHYstatus = 0x6c,
279 RxMaxSize = 0xda,
280 CPlusCmd = 0xe0,
281 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300282
283#define RTL_COALESCE_MASK 0x0f
284#define RTL_COALESCE_SHIFT 4
285#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
286#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
287
Francois Romieu07d3f512007-02-21 22:40:46 +0100288 RxDescAddrLow = 0xe4,
289 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000290 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
291
292#define NoEarlyTx 0x3f /* Max value : no early transmit. */
293
294 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
295
296#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800297#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000298
Francois Romieu07d3f512007-02-21 22:40:46 +0100299 FuncEvent = 0xf0,
300 FuncEventMask = 0xf4,
301 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800302 IBCR0 = 0xf8,
303 IBCR2 = 0xf9,
304 IBIMR0 = 0xfa,
305 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100306 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};
308
Francois Romieuf162a5d2008-06-01 22:37:49 +0200309enum rtl8168_8101_registers {
310 CSIDR = 0x64,
311 CSIAR = 0x68,
312#define CSIAR_FLAG 0x80000000
313#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200314#define CSIAR_BYTE_ENABLE 0x0000f000
315#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000316 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200317 EPHYAR = 0x80,
318#define EPHYAR_FLAG 0x80000000
319#define EPHYAR_WRITE_CMD 0x80000000
320#define EPHYAR_REG_MASK 0x1f
321#define EPHYAR_REG_SHIFT 16
322#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800323 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800324#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800325#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200326 DBG_REG = 0xd1,
327#define FIX_NAK_1 (1 << 4)
328#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800329 TWSI = 0xd2,
330 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800331#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800332#define TX_EMPTY (1 << 5)
333#define RX_EMPTY (1 << 4)
334#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800335#define EN_NDP (1 << 3)
336#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800337#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000338 EFUSEAR = 0xdc,
339#define EFUSEAR_FLAG 0x80000000
340#define EFUSEAR_WRITE_CMD 0x80000000
341#define EFUSEAR_READ_CMD 0x00000000
342#define EFUSEAR_REG_MASK 0x03ff
343#define EFUSEAR_REG_SHIFT 8
344#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800345 MISC_1 = 0xf2,
346#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200347};
348
françois romieuc0e45c12011-01-03 15:08:04 +0000349enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800350 LED_FREQ = 0x1a,
351 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000352 ERIDR = 0x70,
353 ERIAR = 0x74,
354#define ERIAR_FLAG 0x80000000
355#define ERIAR_WRITE_CMD 0x80000000
356#define ERIAR_READ_CMD 0x00000000
357#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000358#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800359#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
360#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
361#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800362#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800363#define ERIAR_MASK_SHIFT 12
364#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
365#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800366#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800367#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800368#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000369 EPHY_RXER_NUM = 0x7c,
370 OCPDR = 0xb0, /* OCP GPHY access */
371#define OCPDR_WRITE_CMD 0x80000000
372#define OCPDR_READ_CMD 0x00000000
373#define OCPDR_REG_MASK 0x7f
374#define OCPDR_GPHY_REG_SHIFT 16
375#define OCPDR_DATA_MASK 0xffff
376 OCPAR = 0xb4,
377#define OCPAR_FLAG 0x80000000
378#define OCPAR_GPHY_WRITE_CMD 0x8000f060
379#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800380 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000381 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
382 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200383#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800384#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800385#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800386#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800387#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000388};
389
Francois Romieu07d3f512007-02-21 22:40:46 +0100390enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100392 SYSErr = 0x8000,
393 PCSTimeout = 0x4000,
394 SWInt = 0x0100,
395 TxDescUnavail = 0x0080,
396 RxFIFOOver = 0x0040,
397 LinkChg = 0x0020,
398 RxOverflow = 0x0010,
399 TxErr = 0x0008,
400 TxOK = 0x0004,
401 RxErr = 0x0002,
402 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200405 RxRWT = (1 << 22),
406 RxRES = (1 << 21),
407 RxRUNT = (1 << 20),
408 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800411 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100412 CmdReset = 0x10,
413 CmdRxEnb = 0x08,
414 CmdTxEnb = 0x04,
415 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Francois Romieu275391a2007-02-23 23:50:28 +0100417 /* TXPoll register p.5 */
418 HPQ = 0x80, /* Poll cmd on the high prio queue */
419 NPQ = 0x40, /* Poll cmd on the low prio queue */
420 FSWInt = 0x01, /* Forced software interrupt */
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100423 Cfg9346_Lock = 0x00,
424 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100427 AcceptErr = 0x20,
428 AcceptRunt = 0x10,
429 AcceptBroadcast = 0x08,
430 AcceptMulticast = 0x04,
431 AcceptMyPhys = 0x02,
432 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200433#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* TxConfigBits */
436 TxInterFrameGapShift = 24,
437 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
438
Francois Romieu5d06a992006-02-23 00:47:58 +0100439 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200440 LEDS1 = (1 << 7),
441 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200442 Speed_down = (1 << 4),
443 MEMMAP = (1 << 3),
444 IOMAP = (1 << 2),
445 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100446 PMEnable = (1 << 0), /* Power Management Enable */
447
Francois Romieu6dccd162007-02-13 23:38:05 +0100448 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000449 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000450 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100451 PCI_Clock_66MHz = 0x01,
452 PCI_Clock_33MHz = 0x00,
453
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100454 /* Config3 register p.25 */
455 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
456 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200457 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800458 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200459 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100460
Francois Romieud58d46b2011-05-03 16:38:29 +0200461 /* Config4 register */
462 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
463
Francois Romieu5d06a992006-02-23 00:47:58 +0100464 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100465 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
466 MWF = (1 << 5), /* Accept Multicast wakeup frame */
467 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200468 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100469 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100470 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000471 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200474 EnableBist = (1 << 15), // 8168 8101
475 Mac_dbgo_oe = (1 << 14), // 8168 8101
476 Normal_mode = (1 << 13), // unused
477 Force_half_dup = (1 << 12), // 8168 8101
478 Force_rxflow_en = (1 << 11), // 8168 8101
479 Force_txflow_en = (1 << 10), // 8168 8101
480 Cxpl_dbg_sel = (1 << 9), // 8168 8101
481 ASF = (1 << 8), // 8168 8101
482 PktCntrDisable = (1 << 7), // 8168 8101
483 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 RxVlan = (1 << 6),
485 RxChkSum = (1 << 5),
486 PCIDAC = (1 << 4),
487 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200488#define INTT_MASK GENMASK(1, 0)
Heiner Kallweitbc732412019-06-10 18:22:33 +0200489#define CPCMD_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100492 TBI_Enable = 0x80,
493 TxFlowCtrl = 0x40,
494 RxFlowCtrl = 0x20,
495 _1000bpsF = 0x10,
496 _100bps = 0x08,
497 _10bps = 0x04,
498 LinkStatus = 0x02,
499 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200501 /* ResetCounterCommand */
502 CounterReset = 0x1,
503
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200504 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100505 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800506
507 /* magic enable v2 */
508 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509};
510
Francois Romieu2b7b4312011-04-18 22:53:24 -0700511enum rtl_desc_bit {
512 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
514 RingEnd = (1 << 30), /* End of descriptor ring */
515 FirstFrag = (1 << 29), /* First segment of a packet */
516 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700517};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Francois Romieu2b7b4312011-04-18 22:53:24 -0700519/* Generic case. */
520enum rtl_tx_desc_bit {
521 /* First doubleword. */
522 TD_LSO = (1 << 27), /* Large Send Offload */
523#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Francois Romieu2b7b4312011-04-18 22:53:24 -0700525 /* Second doubleword. */
526 TxVlanTag = (1 << 17), /* Add VLAN tag */
527};
528
529/* 8169, 8168b and 810x except 8102e. */
530enum rtl_tx_desc_bit_0 {
531 /* First doubleword. */
532#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
533 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
534 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
535 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
536};
537
538/* 8102e, 8168c and beyond. */
539enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800540 /* First doubleword. */
541 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800542 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800543#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800544#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800545
Francois Romieu2b7b4312011-04-18 22:53:24 -0700546 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800547#define TCPHO_SHIFT 18
548#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700549#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800550 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
551 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700552 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
553 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
554};
555
Francois Romieu2b7b4312011-04-18 22:53:24 -0700556enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* Rx private */
558 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500559 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561#define RxProtoUDP (PID1)
562#define RxProtoTCP (PID0)
563#define RxProtoIP (PID1 | PID0)
564#define RxProtoMask RxProtoIP
565
566 IPFail = (1 << 16), /* IP checksum failed */
567 UDPFail = (1 << 15), /* UDP/IP checksum failed */
568 TCPFail = (1 << 14), /* TCP/IP checksum failed */
569 RxVlanTag = (1 << 16), /* VLAN tag available */
570};
571
572#define RsvdMask 0x3fffc000
573
574struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200575 __le32 opts1;
576 __le32 opts2;
577 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578};
579
580struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200581 __le32 opts1;
582 __le32 opts2;
583 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584};
585
586struct ring_info {
587 struct sk_buff *skb;
588 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589};
590
Ivan Vecera355423d2009-02-06 21:49:57 -0800591struct rtl8169_counters {
592 __le64 tx_packets;
593 __le64 rx_packets;
594 __le64 tx_errors;
595 __le32 rx_errors;
596 __le16 rx_missed;
597 __le16 align_errors;
598 __le32 tx_one_collision;
599 __le32 tx_multi_collision;
600 __le64 rx_unicast;
601 __le64 rx_broadcast;
602 __le32 rx_multicast;
603 __le16 tx_aborted;
604 __le16 tx_underun;
605};
606
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200607struct rtl8169_tc_offsets {
608 bool inited;
609 __le64 tx_errors;
610 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200611 __le16 tx_aborted;
612};
613
Francois Romieuda78dbf2012-01-26 14:18:23 +0100614enum rtl_flag {
Kai-Heng Feng6ad56902018-09-11 01:51:43 +0800615 RTL_FLAG_TASK_ENABLED = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100616 RTL_FLAG_TASK_RESET_PENDING,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100617 RTL_FLAG_MAX
618};
619
Junchang Wang8027aa22012-03-04 23:30:32 +0100620struct rtl8169_stats {
621 u64 packets;
622 u64 bytes;
623 struct u64_stats_sync syncp;
624};
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626struct rtl8169_private {
627 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200628 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000629 struct net_device *dev;
Heiner Kallweit703732f2019-01-19 22:07:05 +0100630 struct phy_device *phydev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700631 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200632 u32 msg_enable;
Heiner Kallweit76719ee2019-05-25 20:45:04 +0200633 enum mac_version mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
635 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100637 struct rtl8169_stats rx_stats;
638 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
640 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
641 dma_addr_t TxPhyAddr;
642 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000643 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 u16 cp_cmd;
Heiner Kallweit559c3c02018-11-19 22:34:17 +0100646 u16 irq_mask;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +0200647 struct clk *clk;
françois romieuc0e45c12011-01-03 15:08:04 +0000648
Francois Romieu4422bcd2012-01-26 11:23:32 +0100649 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100650 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
651 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100652 struct work_struct work;
653 } wk;
654
Heiner Kallweitc8248c62019-03-21 21:23:14 +0100655 unsigned irq_enabled:1;
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +0200656 unsigned supports_gmii:1;
Corinna Vinschen42020322015-09-10 10:47:35 +0200657 dma_addr_t counters_phys_addr;
658 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200659 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000660 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000661
Heiner Kallweit254764e2019-01-22 22:23:41 +0100662 const char *fw_name;
Heiner Kallweit8197f9d2019-06-05 08:02:31 +0200663 struct rtl_fw *rtl_fw;
Hayes Wangc5583862012-07-02 17:23:22 +0800664
665 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666};
667
Heiner Kallweit1fcd1652019-04-14 10:30:24 +0200668typedef void (*rtl_generic_fct)(struct rtl8169_private *tp);
669
Ralf Baechle979b6c12005-06-13 14:30:40 -0700670MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200672module_param_named(debug, debug.msg_enable, int, 0);
673MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Heiner Kallweit11287b62019-01-07 21:49:09 +0100674MODULE_SOFTDEP("pre: realtek");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675MODULE_LICENSE("GPL");
françois romieubca03d52011-01-03 15:07:31 +0000676MODULE_FIRMWARE(FIRMWARE_8168D_1);
677MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000678MODULE_FIRMWARE(FIRMWARE_8168E_1);
679MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400680MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800681MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800682MODULE_FIRMWARE(FIRMWARE_8168F_1);
683MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800684MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800685MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800686MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800687MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000688MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000689MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000690MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800691MODULE_FIRMWARE(FIRMWARE_8168H_1);
692MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200693MODULE_FIRMWARE(FIRMWARE_8107E_1);
694MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100696static inline struct device *tp_to_dev(struct rtl8169_private *tp)
697{
698 return &tp->pci_dev->dev;
699}
700
Francois Romieuda78dbf2012-01-26 14:18:23 +0100701static void rtl_lock_work(struct rtl8169_private *tp)
702{
703 mutex_lock(&tp->wk.mutex);
704}
705
706static void rtl_unlock_work(struct rtl8169_private *tp)
707{
708 mutex_unlock(&tp->wk.mutex);
709}
710
Heiner Kallweitdf320ed2019-01-19 22:05:48 +0100711static void rtl_lock_config_regs(struct rtl8169_private *tp)
712{
713 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
714}
715
716static void rtl_unlock_config_regs(struct rtl8169_private *tp)
717{
718 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
719}
720
Heiner Kallweitcb732002018-03-20 07:45:35 +0100721static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200722{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100723 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800724 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200725}
726
Francois Romieuffc46952012-07-06 14:19:23 +0200727struct rtl_cond {
728 bool (*check)(struct rtl8169_private *);
729 const char *msg;
730};
731
732static void rtl_udelay(unsigned int d)
733{
734 udelay(d);
735}
736
737static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
738 void (*delay)(unsigned int), unsigned int d, int n,
739 bool high)
740{
741 int i;
742
743 for (i = 0; i < n; i++) {
Francois Romieuffc46952012-07-06 14:19:23 +0200744 if (c->check(tp) == high)
745 return true;
Heiner Kallweitd1f50502019-05-04 15:20:38 +0200746 delay(d);
Francois Romieuffc46952012-07-06 14:19:23 +0200747 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200748 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
749 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200750 return false;
751}
752
753static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
754 const struct rtl_cond *c,
755 unsigned int d, int n)
756{
757 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
758}
759
760static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
761 const struct rtl_cond *c,
762 unsigned int d, int n)
763{
764 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
765}
766
767static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
768 const struct rtl_cond *c,
769 unsigned int d, int n)
770{
771 return rtl_loop_wait(tp, c, msleep, d, n, true);
772}
773
774static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
775 const struct rtl_cond *c,
776 unsigned int d, int n)
777{
778 return rtl_loop_wait(tp, c, msleep, d, n, false);
779}
780
781#define DECLARE_RTL_COND(name) \
782static bool name ## _check(struct rtl8169_private *); \
783 \
784static const struct rtl_cond name = { \
785 .check = name ## _check, \
786 .msg = #name \
787}; \
788 \
789static bool name ## _check(struct rtl8169_private *tp)
790
Hayes Wangc5583862012-07-02 17:23:22 +0800791static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
792{
793 if (reg & 0xffff0001) {
794 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
795 return true;
796 }
797 return false;
798}
799
800DECLARE_RTL_COND(rtl_ocp_gphy_cond)
801{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200802 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800803}
804
805static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
806{
Hayes Wangc5583862012-07-02 17:23:22 +0800807 if (rtl_ocp_reg_failure(tp, reg))
808 return;
809
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200810 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800811
812 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
813}
814
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200815static int r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
Hayes Wangc5583862012-07-02 17:23:22 +0800816{
Hayes Wangc5583862012-07-02 17:23:22 +0800817 if (rtl_ocp_reg_failure(tp, reg))
818 return 0;
819
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200820 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800821
822 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200823 (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;
Hayes Wangc5583862012-07-02 17:23:22 +0800824}
825
Hayes Wangc5583862012-07-02 17:23:22 +0800826static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
827{
Hayes Wangc5583862012-07-02 17:23:22 +0800828 if (rtl_ocp_reg_failure(tp, reg))
829 return;
830
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200831 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800832}
833
834static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
835{
Hayes Wangc5583862012-07-02 17:23:22 +0800836 if (rtl_ocp_reg_failure(tp, reg))
837 return 0;
838
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200839 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800840
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200841 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800842}
843
844#define OCP_STD_PHY_BASE 0xa400
845
846static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
847{
848 if (reg == 0x1f) {
849 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
850 return;
851 }
852
853 if (tp->ocp_base != OCP_STD_PHY_BASE)
854 reg -= 0x10;
855
856 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
857}
858
859static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
860{
861 if (tp->ocp_base != OCP_STD_PHY_BASE)
862 reg -= 0x10;
863
864 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
865}
866
hayeswangeee37862013-04-01 22:23:38 +0000867static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
868{
869 if (reg == 0x1f) {
870 tp->ocp_base = value << 4;
871 return;
872 }
873
874 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
875}
876
877static int mac_mcu_read(struct rtl8169_private *tp, int reg)
878{
879 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
880}
881
Francois Romieuffc46952012-07-06 14:19:23 +0200882DECLARE_RTL_COND(rtl_phyar_cond)
883{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200884 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200885}
886
Francois Romieu24192212012-07-06 20:19:42 +0200887static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200889 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Francois Romieuffc46952012-07-06 14:19:23 +0200891 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700892 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700893 * According to hardware specs a 20us delay is required after write
894 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700895 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700896 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
Francois Romieu24192212012-07-06 20:19:42 +0200899static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Francois Romieuffc46952012-07-06 14:19:23 +0200901 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200903 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Francois Romieuffc46952012-07-06 14:19:23 +0200905 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200906 RTL_R32(tp, PHYAR) & 0xffff : -ETIMEDOUT;
Francois Romieuffc46952012-07-06 14:19:23 +0200907
Timo Teräs81a95f02010-06-09 17:31:48 -0700908 /*
909 * According to hardware specs a 20us delay is required after read
910 * complete indication, but before sending next command.
911 */
912 udelay(20);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return value;
915}
916
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800917DECLARE_RTL_COND(rtl_ocpar_cond)
918{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200919 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800920}
921
Francois Romieu24192212012-07-06 20:19:42 +0200922static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000923{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200924 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
925 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
926 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000927
Francois Romieuffc46952012-07-06 14:19:23 +0200928 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +0000929}
930
Francois Romieu24192212012-07-06 20:19:42 +0200931static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000932{
Francois Romieu24192212012-07-06 20:19:42 +0200933 r8168dp_1_mdio_access(tp, reg,
934 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000935}
936
Francois Romieu24192212012-07-06 20:19:42 +0200937static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000938{
Francois Romieu24192212012-07-06 20:19:42 +0200939 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +0000940
941 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200942 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
943 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000944
Francois Romieuffc46952012-07-06 14:19:23 +0200945 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200946 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : -ETIMEDOUT;
françois romieuc0e45c12011-01-03 15:08:04 +0000947}
948
françois romieue6de30d2011-01-03 15:08:37 +0000949#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
950
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200951static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000952{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200953 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000954}
955
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200956static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000957{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200958 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000959}
960
Francois Romieu24192212012-07-06 20:19:42 +0200961static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +0000962{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200963 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000964
Francois Romieu24192212012-07-06 20:19:42 +0200965 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +0000966
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200967 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000968}
969
Francois Romieu24192212012-07-06 20:19:42 +0200970static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +0000971{
972 int value;
973
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200974 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000975
Francois Romieu24192212012-07-06 20:19:42 +0200976 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +0000977
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200978 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000979
980 return value;
981}
982
Heiner Kallweitce8843a2019-05-29 21:15:06 +0200983static void rtl_writephy(struct rtl8169_private *tp, int location, int val)
Francois Romieudacf8152008-08-02 20:44:13 +0200984{
Heiner Kallweit5f950522019-05-31 19:53:28 +0200985 switch (tp->mac_version) {
986 case RTL_GIGA_MAC_VER_27:
987 r8168dp_1_mdio_write(tp, location, val);
988 break;
989 case RTL_GIGA_MAC_VER_28:
990 case RTL_GIGA_MAC_VER_31:
991 r8168dp_2_mdio_write(tp, location, val);
992 break;
993 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
994 r8168g_mdio_write(tp, location, val);
995 break;
996 default:
997 r8169_mdio_write(tp, location, val);
998 break;
999 }
Francois Romieudacf8152008-08-02 20:44:13 +02001000}
1001
françois romieu4da19632011-01-03 15:07:55 +00001002static int rtl_readphy(struct rtl8169_private *tp, int location)
1003{
Heiner Kallweit5f950522019-05-31 19:53:28 +02001004 switch (tp->mac_version) {
1005 case RTL_GIGA_MAC_VER_27:
1006 return r8168dp_1_mdio_read(tp, location);
1007 case RTL_GIGA_MAC_VER_28:
1008 case RTL_GIGA_MAC_VER_31:
1009 return r8168dp_2_mdio_read(tp, location);
1010 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1011 return r8168g_mdio_read(tp, location);
1012 default:
1013 return r8169_mdio_read(tp, location);
1014 }
françois romieu4da19632011-01-03 15:07:55 +00001015}
1016
1017static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1018{
1019 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1020}
1021
Chun-Hao Lin76564422014-10-01 23:17:17 +08001022static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001023{
1024 int val;
1025
françois romieu4da19632011-01-03 15:07:55 +00001026 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001027 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001028}
1029
Francois Romieuffc46952012-07-06 14:19:23 +02001030DECLARE_RTL_COND(rtl_ephyar_cond)
1031{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001032 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001033}
1034
Francois Romieufdf6fc02012-07-06 22:40:38 +02001035static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001036{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001037 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001038 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1039
Francois Romieuffc46952012-07-06 14:19:23 +02001040 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1041
1042 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001043}
1044
Francois Romieufdf6fc02012-07-06 22:40:38 +02001045static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001046{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001047 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001048
Francois Romieuffc46952012-07-06 14:19:23 +02001049 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001050 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001051}
1052
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001053DECLARE_RTL_COND(rtl_eriar_cond)
1054{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001055 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001056}
1057
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001058static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1059 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001060{
Hayes Wang133ac402011-07-06 15:58:05 +08001061 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001062 RTL_W32(tp, ERIDR, val);
1063 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001064
Francois Romieuffc46952012-07-06 14:19:23 +02001065 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001066}
1067
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001068static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1069 u32 val)
1070{
1071 _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
1072}
1073
1074static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001075{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001076 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001077
Francois Romieuffc46952012-07-06 14:19:23 +02001078 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001079 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001080}
1081
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001082static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
1083{
1084 return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
1085}
1086
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001087static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001088 u32 m)
Hayes Wang133ac402011-07-06 15:58:05 +08001089{
1090 u32 val;
1091
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001092 val = rtl_eri_read(tp, addr);
1093 rtl_eri_write(tp, addr, mask, (val & ~m) | p);
Hayes Wang133ac402011-07-06 15:58:05 +08001094}
1095
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001096static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 mask,
1097 u32 p)
1098{
1099 rtl_w0w1_eri(tp, addr, mask, p, 0);
1100}
1101
1102static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 mask,
1103 u32 m)
1104{
1105 rtl_w0w1_eri(tp, addr, mask, 0, m);
1106}
1107
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001108static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1109{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001110 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001111 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001112 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001113}
1114
1115static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1116{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001117 return _rtl_eri_read(tp, reg, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001118}
1119
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001120static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1121 u32 data)
1122{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001123 RTL_W32(tp, OCPDR, data);
1124 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001125 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1126}
1127
1128static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1129 u32 data)
1130{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001131 _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1132 data, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001133}
1134
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001135static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001136{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001137 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001138
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001139 r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001140}
1141
1142#define OOB_CMD_RESET 0x00
1143#define OOB_CMD_DRIVER_START 0x05
1144#define OOB_CMD_DRIVER_STOP 0x06
1145
1146static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1147{
1148 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1149}
1150
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001151DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001152{
1153 u16 reg;
1154
1155 reg = rtl8168_get_ocp_reg(tp);
1156
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001157 return r8168dp_ocp_read(tp, 0x0f, reg) & 0x00000800;
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001158}
1159
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001160DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1161{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001162 return r8168ep_ocp_read(tp, 0x0f, 0x124) & 0x00000001;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001163}
1164
1165DECLARE_RTL_COND(rtl_ocp_tx_cond)
1166{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001167 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001168}
1169
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001170static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1171{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001172 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001173 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001174 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1175 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001176}
1177
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001178static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001179{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001180 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1181 rtl_msleep_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001182}
1183
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001184static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1185{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001186 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1187 r8168ep_ocp_write(tp, 0x01, 0x30,
1188 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001189 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1190}
1191
1192static void rtl8168_driver_start(struct rtl8169_private *tp)
1193{
1194 switch (tp->mac_version) {
1195 case RTL_GIGA_MAC_VER_27:
1196 case RTL_GIGA_MAC_VER_28:
1197 case RTL_GIGA_MAC_VER_31:
1198 rtl8168dp_driver_start(tp);
1199 break;
1200 case RTL_GIGA_MAC_VER_49:
1201 case RTL_GIGA_MAC_VER_50:
1202 case RTL_GIGA_MAC_VER_51:
1203 rtl8168ep_driver_start(tp);
1204 break;
1205 default:
1206 BUG();
1207 break;
1208 }
1209}
1210
1211static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1212{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001213 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1214 rtl_msleep_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001215}
1216
1217static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1218{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001219 rtl8168ep_stop_cmac(tp);
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001220 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1221 r8168ep_ocp_write(tp, 0x01, 0x30,
1222 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001223 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1224}
1225
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001226static void rtl8168_driver_stop(struct rtl8169_private *tp)
1227{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001228 switch (tp->mac_version) {
1229 case RTL_GIGA_MAC_VER_27:
1230 case RTL_GIGA_MAC_VER_28:
1231 case RTL_GIGA_MAC_VER_31:
1232 rtl8168dp_driver_stop(tp);
1233 break;
1234 case RTL_GIGA_MAC_VER_49:
1235 case RTL_GIGA_MAC_VER_50:
1236 case RTL_GIGA_MAC_VER_51:
1237 rtl8168ep_driver_stop(tp);
1238 break;
1239 default:
1240 BUG();
1241 break;
1242 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001243}
1244
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001245static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001246{
1247 u16 reg = rtl8168_get_ocp_reg(tp);
1248
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001249 return !!(r8168dp_ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001250}
1251
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001252static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001253{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001254 return !!(r8168ep_ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001255}
1256
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001257static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001258{
1259 switch (tp->mac_version) {
1260 case RTL_GIGA_MAC_VER_27:
1261 case RTL_GIGA_MAC_VER_28:
1262 case RTL_GIGA_MAC_VER_31:
1263 return r8168dp_check_dash(tp);
1264 case RTL_GIGA_MAC_VER_49:
1265 case RTL_GIGA_MAC_VER_50:
1266 case RTL_GIGA_MAC_VER_51:
1267 return r8168ep_check_dash(tp);
1268 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001269 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001270 }
1271}
1272
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001273static void rtl_reset_packet_filter(struct rtl8169_private *tp)
1274{
1275 rtl_eri_clear_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1276 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1277}
1278
Francois Romieuffc46952012-07-06 14:19:23 +02001279DECLARE_RTL_COND(rtl_efusear_cond)
1280{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001281 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001282}
1283
Francois Romieufdf6fc02012-07-06 22:40:38 +02001284static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001285{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001286 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001287
Francois Romieuffc46952012-07-06 14:19:23 +02001288 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001289 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001290}
1291
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001292static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1293{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001294 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001295}
1296
1297static void rtl_irq_disable(struct rtl8169_private *tp)
1298{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001299 RTL_W16(tp, IntrMask, 0);
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001300 tp->irq_enabled = 0;
Francois Romieu3e990ff2012-01-26 12:50:01 +01001301}
1302
Francois Romieuda78dbf2012-01-26 14:18:23 +01001303#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1304#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1305#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1306
Heiner Kallweitfe716f82018-11-19 22:31:32 +01001307static void rtl_irq_enable(struct rtl8169_private *tp)
Francois Romieuda78dbf2012-01-26 14:18:23 +01001308{
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001309 tp->irq_enabled = 1;
Heiner Kallweit559c3c02018-11-19 22:34:17 +01001310 RTL_W16(tp, IntrMask, tp->irq_mask);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001311}
1312
françois romieu811fd302011-12-04 20:30:45 +00001313static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001315 rtl_irq_disable(tp);
Heiner Kallweitde20e122018-09-25 07:58:00 +02001316 rtl_ack_events(tp, 0xffff);
1317 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001318 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
Hayes Wang70090422011-07-06 15:58:06 +08001321static void rtl_link_chg_patch(struct rtl8169_private *tp)
1322{
Hayes Wang70090422011-07-06 15:58:06 +08001323 struct net_device *dev = tp->dev;
Heiner Kallweit703732f2019-01-19 22:07:05 +01001324 struct phy_device *phydev = tp->phydev;
Hayes Wang70090422011-07-06 15:58:06 +08001325
1326 if (!netif_running(dev))
1327 return;
1328
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001329 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1330 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001331 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001332 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1333 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001334 } else if (phydev->speed == SPEED_100) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001335 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1336 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wang70090422011-07-06 15:58:06 +08001337 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001338 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1339 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wang70090422011-07-06 15:58:06 +08001340 }
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001341 rtl_reset_packet_filter(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08001342 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1343 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001344 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001345 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1346 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wangc2218922011-09-06 16:55:18 +08001347 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001348 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1349 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wangc2218922011-09-06 16:55:18 +08001350 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001351 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001352 if (phydev->speed == SPEED_10) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001353 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
1354 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001355 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001356 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001357 }
Hayes Wang70090422011-07-06 15:58:06 +08001358 }
1359}
1360
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001361#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1362
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001363static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1364{
1365 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001366
Francois Romieuda78dbf2012-01-26 14:18:23 +01001367 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001368 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001369 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001370 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001371}
1372
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001373static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001374{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001375 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001376 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001377 u32 opt;
1378 u16 reg;
1379 u8 mask;
1380 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001381 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001382 { WAKE_UCAST, Config5, UWF },
1383 { WAKE_BCAST, Config5, BWF },
1384 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001385 { WAKE_ANY, Config5, LanWake },
1386 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001387 };
Francois Romieu851e6022012-04-17 11:10:11 +02001388 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001389
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001390 rtl_unlock_config_regs(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001391
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001392 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001393 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1394 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001395 tmp = ARRAY_SIZE(cfg) - 1;
1396 if (wolopts & WAKE_MAGIC)
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001397 rtl_eri_set_bits(tp, 0x0dc, ERIAR_MASK_0100,
1398 MagicPacket_v2);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001399 else
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001400 rtl_eri_clear_bits(tp, 0x0dc, ERIAR_MASK_0100,
1401 MagicPacket_v2);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001402 break;
1403 default:
1404 tmp = ARRAY_SIZE(cfg);
1405 break;
1406 }
1407
1408 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001409 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001410 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001411 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001412 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001413 }
1414
Francois Romieu851e6022012-04-17 11:10:11 +02001415 switch (tp->mac_version) {
Heiner Kallweite9588eb2019-05-25 21:14:39 +02001416 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001417 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001418 if (wolopts)
1419 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001420 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001421 break;
1422 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001423 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001424 if (wolopts)
1425 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001426 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001427 break;
1428 }
1429
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001430 rtl_lock_config_regs(tp);
Heiner Kallweit3bd82642018-12-30 13:16:12 +01001431
1432 device_set_wakeup_enable(tp_to_dev(tp), wolopts);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001433}
1434
1435static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1436{
1437 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001438 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001439
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001440 if (wol->wolopts & ~WAKE_ANY)
1441 return -EINVAL;
1442
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001443 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001444
Francois Romieuda78dbf2012-01-26 14:18:23 +01001445 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001446
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001447 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001448
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001449 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001450 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001451
1452 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001453
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001454 pm_runtime_put_noidle(d);
1455
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001456 return 0;
1457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459static void rtl8169_get_drvinfo(struct net_device *dev,
1460 struct ethtool_drvinfo *info)
1461{
1462 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001463 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Rick Jones68aad782011-11-07 13:29:27 +00001465 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001466 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001467 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Heiner Kallweit254764e2019-01-22 22:23:41 +01001468 if (rtl_fw)
Rick Jones8ac72d12011-11-22 14:06:26 +00001469 strlcpy(info->fw_version, rtl_fw->version,
1470 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
1473static int rtl8169_get_regs_len(struct net_device *dev)
1474{
1475 return R8169_REGS_SIZE;
1476}
1477
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001478static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1479 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Francois Romieud58d46b2011-05-03 16:38:29 +02001481 struct rtl8169_private *tp = netdev_priv(dev);
1482
Francois Romieu2b7b4312011-04-18 22:53:24 -07001483 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001484 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Francois Romieud58d46b2011-05-03 16:38:29 +02001486 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001487 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001488 features &= ~NETIF_F_IP_CSUM;
1489
Michał Mirosław350fb322011-04-08 06:35:56 +00001490 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Heiner Kallweita3984572018-04-28 22:19:15 +02001493static int rtl8169_set_features(struct net_device *dev,
1494 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001497 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Heiner Kallweita3984572018-04-28 22:19:15 +02001499 rtl_lock_work(tp);
1500
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001501 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001502 if (features & NETIF_F_RXALL)
1503 rx_config |= (AcceptErr | AcceptRunt);
1504 else
1505 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001507 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001508
hayeswang929a0312014-09-16 11:40:47 +08001509 if (features & NETIF_F_RXCSUM)
1510 tp->cp_cmd |= RxChkSum;
1511 else
1512 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001513
hayeswang929a0312014-09-16 11:40:47 +08001514 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1515 tp->cp_cmd |= RxVlan;
1516 else
1517 tp->cp_cmd &= ~RxVlan;
1518
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001519 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1520 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Francois Romieuda78dbf2012-01-26 14:18:23 +01001522 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 return 0;
1525}
1526
Kirill Smelkov810f4892012-11-10 21:11:02 +04001527static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001529 return (skb_vlan_tag_present(skb)) ?
1530 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Francois Romieu7a8fc772011-03-01 17:18:33 +01001533static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
1535 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Francois Romieu7a8fc772011-03-01 17:18:33 +01001537 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001538 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1542 void *p)
1543{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001544 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001545 u32 __iomem *data = tp->mmio_addr;
1546 u32 *dw = p;
1547 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Francois Romieuda78dbf2012-01-26 14:18:23 +01001549 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001550 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1551 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001552 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001555static u32 rtl8169_get_msglevel(struct net_device *dev)
1556{
1557 struct rtl8169_private *tp = netdev_priv(dev);
1558
1559 return tp->msg_enable;
1560}
1561
1562static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1563{
1564 struct rtl8169_private *tp = netdev_priv(dev);
1565
1566 tp->msg_enable = value;
1567}
1568
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001569static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1570 "tx_packets",
1571 "rx_packets",
1572 "tx_errors",
1573 "rx_errors",
1574 "rx_missed",
1575 "align_errors",
1576 "tx_single_collisions",
1577 "tx_multi_collisions",
1578 "unicast",
1579 "broadcast",
1580 "multicast",
1581 "tx_aborted",
1582 "tx_underrun",
1583};
1584
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001585static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001586{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001587 switch (sset) {
1588 case ETH_SS_STATS:
1589 return ARRAY_SIZE(rtl8169_gstrings);
1590 default:
1591 return -EOPNOTSUPP;
1592 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001593}
1594
Corinna Vinschen42020322015-09-10 10:47:35 +02001595DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001596{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001597 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001598}
1599
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001600static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001601{
Corinna Vinschen42020322015-09-10 10:47:35 +02001602 dma_addr_t paddr = tp->counters_phys_addr;
1603 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001604
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001605 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1606 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001607 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001608 RTL_W32(tp, CounterAddrLow, cmd);
1609 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001610
Francois Romieua78e9362018-01-26 01:53:26 +01001611 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001612}
1613
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001614static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001615{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001616 /*
1617 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1618 * tally counters.
1619 */
1620 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1621 return true;
1622
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001623 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001624}
1625
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001626static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001627{
Heiner Kallweit10262b02019-01-06 20:44:00 +01001628 u8 val = RTL_R8(tp, ChipCmd);
1629
Ivan Vecera355423d2009-02-06 21:49:57 -08001630 /*
1631 * Some chips are unable to dump tally counters when the receiver
Heiner Kallweit10262b02019-01-06 20:44:00 +01001632 * is disabled. If 0xff chip may be in a PCI power-save state.
Ivan Vecera355423d2009-02-06 21:49:57 -08001633 */
Heiner Kallweit10262b02019-01-06 20:44:00 +01001634 if (!(val & CmdRxEnb) || val == 0xff)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001635 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001636
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001637 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001638}
1639
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001640static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001641{
Corinna Vinschen42020322015-09-10 10:47:35 +02001642 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001643 bool ret = false;
1644
1645 /*
1646 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1647 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1648 * reset by a power cycle, while the counter values collected by the
1649 * driver are reset at every driver unload/load cycle.
1650 *
1651 * To make sure the HW values returned by @get_stats64 match the SW
1652 * values, we collect the initial values at first open(*) and use them
1653 * as offsets to normalize the values returned by @get_stats64.
1654 *
1655 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1656 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1657 * set at open time by rtl_hw_start.
1658 */
1659
1660 if (tp->tc_offset.inited)
1661 return true;
1662
1663 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001664 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001665 ret = true;
1666
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001667 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001668 ret = true;
1669
Corinna Vinschen42020322015-09-10 10:47:35 +02001670 tp->tc_offset.tx_errors = counters->tx_errors;
1671 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1672 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001673 tp->tc_offset.inited = true;
1674
1675 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001676}
1677
Ivan Vecera355423d2009-02-06 21:49:57 -08001678static void rtl8169_get_ethtool_stats(struct net_device *dev,
1679 struct ethtool_stats *stats, u64 *data)
1680{
1681 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001682 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02001683 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08001684
1685 ASSERT_RTNL();
1686
Chun-Hao Line0636232016-07-29 16:37:55 +08001687 pm_runtime_get_noresume(d);
1688
1689 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001690 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08001691
1692 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08001693
Corinna Vinschen42020322015-09-10 10:47:35 +02001694 data[0] = le64_to_cpu(counters->tx_packets);
1695 data[1] = le64_to_cpu(counters->rx_packets);
1696 data[2] = le64_to_cpu(counters->tx_errors);
1697 data[3] = le32_to_cpu(counters->rx_errors);
1698 data[4] = le16_to_cpu(counters->rx_missed);
1699 data[5] = le16_to_cpu(counters->align_errors);
1700 data[6] = le32_to_cpu(counters->tx_one_collision);
1701 data[7] = le32_to_cpu(counters->tx_multi_collision);
1702 data[8] = le64_to_cpu(counters->rx_unicast);
1703 data[9] = le64_to_cpu(counters->rx_broadcast);
1704 data[10] = le32_to_cpu(counters->rx_multicast);
1705 data[11] = le16_to_cpu(counters->tx_aborted);
1706 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08001707}
1708
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001709static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1710{
1711 switch(stringset) {
1712 case ETH_SS_STATS:
1713 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1714 break;
1715 }
1716}
1717
Francois Romieu50970832017-10-27 13:24:49 +03001718/*
1719 * Interrupt coalescing
1720 *
1721 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1722 * > 8169, 8168 and 810x line of chipsets
1723 *
1724 * 8169, 8168, and 8136(810x) serial chipsets support it.
1725 *
1726 * > 2 - the Tx timer unit at gigabit speed
1727 *
1728 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1729 * (0xe0) bit 1 and bit 0.
1730 *
1731 * For 8169
1732 * bit[1:0] \ speed 1000M 100M 10M
1733 * 0 0 320ns 2.56us 40.96us
1734 * 0 1 2.56us 20.48us 327.7us
1735 * 1 0 5.12us 40.96us 655.4us
1736 * 1 1 10.24us 81.92us 1.31ms
1737 *
1738 * For the other
1739 * bit[1:0] \ speed 1000M 100M 10M
1740 * 0 0 5us 2.56us 40.96us
1741 * 0 1 40us 20.48us 327.7us
1742 * 1 0 80us 40.96us 655.4us
1743 * 1 1 160us 81.92us 1.31ms
1744 */
1745
1746/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1747struct rtl_coalesce_scale {
1748 /* Rx / Tx */
1749 u32 nsecs[2];
1750};
1751
1752/* rx/tx scale factors for all CPlusCmd[0:1] cases */
1753struct rtl_coalesce_info {
1754 u32 speed;
1755 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1756};
1757
1758/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1759#define rxtx_x1822(r, t) { \
1760 {{(r), (t)}}, \
1761 {{(r)*8, (t)*8}}, \
1762 {{(r)*8*2, (t)*8*2}}, \
1763 {{(r)*8*2*2, (t)*8*2*2}}, \
1764}
1765static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1766 /* speed delays: rx00 tx00 */
1767 { SPEED_10, rxtx_x1822(40960, 40960) },
1768 { SPEED_100, rxtx_x1822( 2560, 2560) },
1769 { SPEED_1000, rxtx_x1822( 320, 320) },
1770 { 0 },
1771};
1772
1773static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1774 /* speed delays: rx00 tx00 */
1775 { SPEED_10, rxtx_x1822(40960, 40960) },
1776 { SPEED_100, rxtx_x1822( 2560, 2560) },
1777 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1778 { 0 },
1779};
1780#undef rxtx_x1822
1781
1782/* get rx/tx scale vector corresponding to current speed */
1783static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1784{
1785 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001786 const struct rtl_coalesce_info *ci;
Francois Romieu50970832017-10-27 13:24:49 +03001787
Heiner Kallweit20023d32019-06-11 21:09:19 +02001788 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
1789 ci = rtl_coalesce_info_8169;
1790 else
1791 ci = rtl_coalesce_info_8168_8136;
Francois Romieu50970832017-10-27 13:24:49 +03001792
Heiner Kallweit20023d32019-06-11 21:09:19 +02001793 for (; ci->speed; ci++) {
1794 if (tp->phydev->speed == ci->speed)
Francois Romieu50970832017-10-27 13:24:49 +03001795 return ci;
Francois Romieu50970832017-10-27 13:24:49 +03001796 }
1797
1798 return ERR_PTR(-ELNRNG);
1799}
1800
1801static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1802{
1803 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001804 const struct rtl_coalesce_info *ci;
1805 const struct rtl_coalesce_scale *scale;
1806 struct {
1807 u32 *max_frames;
1808 u32 *usecs;
1809 } coal_settings [] = {
1810 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1811 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1812 }, *p = coal_settings;
1813 int i;
1814 u16 w;
1815
1816 memset(ec, 0, sizeof(*ec));
1817
1818 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1819 ci = rtl_coalesce_info(dev);
1820 if (IS_ERR(ci))
1821 return PTR_ERR(ci);
1822
Heiner Kallweit0ae09742018-04-28 22:19:26 +02001823 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03001824
1825 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001826 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03001827 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1828 w >>= RTL_COALESCE_SHIFT;
1829 *p->usecs = w & RTL_COALESCE_MASK;
1830 }
1831
1832 for (i = 0; i < 2; i++) {
1833 p = coal_settings + i;
1834 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1835
1836 /*
1837 * ethtool_coalesce says it is illegal to set both usecs and
1838 * max_frames to 0.
1839 */
1840 if (!*p->usecs && !*p->max_frames)
1841 *p->max_frames = 1;
1842 }
1843
1844 return 0;
1845}
1846
1847/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1848static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1849 struct net_device *dev, u32 nsec, u16 *cp01)
1850{
1851 const struct rtl_coalesce_info *ci;
1852 u16 i;
1853
1854 ci = rtl_coalesce_info(dev);
1855 if (IS_ERR(ci))
1856 return ERR_CAST(ci);
1857
1858 for (i = 0; i < 4; i++) {
1859 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1860 ci->scalev[i].nsecs[1]);
1861 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1862 *cp01 = i;
1863 return &ci->scalev[i];
1864 }
1865 }
1866
1867 return ERR_PTR(-EINVAL);
1868}
1869
1870static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1871{
1872 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001873 const struct rtl_coalesce_scale *scale;
1874 struct {
1875 u32 frames;
1876 u32 usecs;
1877 } coal_settings [] = {
1878 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1879 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1880 }, *p = coal_settings;
1881 u16 w = 0, cp01;
1882 int i;
1883
1884 scale = rtl_coalesce_choose_scale(dev,
1885 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1886 if (IS_ERR(scale))
1887 return PTR_ERR(scale);
1888
1889 for (i = 0; i < 2; i++, p++) {
1890 u32 units;
1891
1892 /*
1893 * accept max_frames=1 we returned in rtl_get_coalesce.
1894 * accept it not only when usecs=0 because of e.g. the following scenario:
1895 *
1896 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
1897 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
1898 * - then user does `ethtool -C eth0 rx-usecs 100`
1899 *
1900 * since ethtool sends to kernel whole ethtool_coalesce
1901 * settings, if we do not handle rx_usecs=!0, rx_frames=1
1902 * we'll reject it below in `frames % 4 != 0`.
1903 */
1904 if (p->frames == 1) {
1905 p->frames = 0;
1906 }
1907
1908 units = p->usecs * 1000 / scale->nsecs[i];
1909 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
1910 return -EINVAL;
1911
1912 w <<= RTL_COALESCE_SHIFT;
1913 w |= units;
1914 w <<= RTL_COALESCE_SHIFT;
1915 w |= p->frames >> 2;
1916 }
1917
1918 rtl_lock_work(tp);
1919
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001920 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03001921
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02001922 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001923 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1924 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03001925
1926 rtl_unlock_work(tp);
1927
1928 return 0;
1929}
1930
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001931static int rtl_get_eee_supp(struct rtl8169_private *tp)
1932{
1933 struct phy_device *phydev = tp->phydev;
1934 int ret;
1935
1936 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01001937 case RTL_GIGA_MAC_VER_34:
1938 case RTL_GIGA_MAC_VER_35:
1939 case RTL_GIGA_MAC_VER_36:
1940 case RTL_GIGA_MAC_VER_38:
1941 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1942 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001943 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Heiner Kallweita2928d22019-06-02 10:53:49 +02001944 ret = phy_read_paged(phydev, 0x0a5c, 0x12);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001945 break;
1946 default:
1947 ret = -EPROTONOSUPPORT;
1948 break;
1949 }
1950
1951 return ret;
1952}
1953
1954static int rtl_get_eee_lpadv(struct rtl8169_private *tp)
1955{
1956 struct phy_device *phydev = tp->phydev;
1957 int ret;
1958
1959 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01001960 case RTL_GIGA_MAC_VER_34:
1961 case RTL_GIGA_MAC_VER_35:
1962 case RTL_GIGA_MAC_VER_36:
1963 case RTL_GIGA_MAC_VER_38:
1964 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1965 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001966 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Heiner Kallweita2928d22019-06-02 10:53:49 +02001967 ret = phy_read_paged(phydev, 0x0a5d, 0x11);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001968 break;
1969 default:
1970 ret = -EPROTONOSUPPORT;
1971 break;
1972 }
1973
1974 return ret;
1975}
1976
1977static int rtl_get_eee_adv(struct rtl8169_private *tp)
1978{
1979 struct phy_device *phydev = tp->phydev;
1980 int ret;
1981
1982 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01001983 case RTL_GIGA_MAC_VER_34:
1984 case RTL_GIGA_MAC_VER_35:
1985 case RTL_GIGA_MAC_VER_36:
1986 case RTL_GIGA_MAC_VER_38:
1987 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1988 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001989 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Heiner Kallweita2928d22019-06-02 10:53:49 +02001990 ret = phy_read_paged(phydev, 0x0a5d, 0x10);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001991 break;
1992 default:
1993 ret = -EPROTONOSUPPORT;
1994 break;
1995 }
1996
1997 return ret;
1998}
1999
2000static int rtl_set_eee_adv(struct rtl8169_private *tp, int val)
2001{
2002 struct phy_device *phydev = tp->phydev;
2003 int ret = 0;
2004
2005 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002006 case RTL_GIGA_MAC_VER_34:
2007 case RTL_GIGA_MAC_VER_35:
2008 case RTL_GIGA_MAC_VER_36:
2009 case RTL_GIGA_MAC_VER_38:
2010 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
2011 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002012 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Heiner Kallweita2928d22019-06-02 10:53:49 +02002013 phy_write_paged(phydev, 0x0a5d, 0x10, val);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002014 break;
2015 default:
2016 ret = -EPROTONOSUPPORT;
2017 break;
2018 }
2019
2020 return ret;
2021}
2022
2023static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
2024{
2025 struct rtl8169_private *tp = netdev_priv(dev);
2026 struct device *d = tp_to_dev(tp);
2027 int ret;
2028
2029 pm_runtime_get_noresume(d);
2030
2031 if (!pm_runtime_active(d)) {
2032 ret = -EOPNOTSUPP;
2033 goto out;
2034 }
2035
2036 /* Get Supported EEE */
2037 ret = rtl_get_eee_supp(tp);
2038 if (ret < 0)
2039 goto out;
2040 data->supported = mmd_eee_cap_to_ethtool_sup_t(ret);
2041
2042 /* Get advertisement EEE */
2043 ret = rtl_get_eee_adv(tp);
2044 if (ret < 0)
2045 goto out;
2046 data->advertised = mmd_eee_adv_to_ethtool_adv_t(ret);
2047 data->eee_enabled = !!data->advertised;
2048
2049 /* Get LP advertisement EEE */
2050 ret = rtl_get_eee_lpadv(tp);
2051 if (ret < 0)
2052 goto out;
2053 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(ret);
2054 data->eee_active = !!(data->advertised & data->lp_advertised);
2055out:
2056 pm_runtime_put_noidle(d);
2057 return ret < 0 ? ret : 0;
2058}
2059
2060static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
2061{
2062 struct rtl8169_private *tp = netdev_priv(dev);
2063 struct device *d = tp_to_dev(tp);
2064 int old_adv, adv = 0, cap, ret;
2065
2066 pm_runtime_get_noresume(d);
2067
2068 if (!dev->phydev || !pm_runtime_active(d)) {
2069 ret = -EOPNOTSUPP;
2070 goto out;
2071 }
2072
2073 if (dev->phydev->autoneg == AUTONEG_DISABLE ||
2074 dev->phydev->duplex != DUPLEX_FULL) {
2075 ret = -EPROTONOSUPPORT;
2076 goto out;
2077 }
2078
2079 /* Get Supported EEE */
2080 ret = rtl_get_eee_supp(tp);
2081 if (ret < 0)
2082 goto out;
2083 cap = ret;
2084
2085 ret = rtl_get_eee_adv(tp);
2086 if (ret < 0)
2087 goto out;
2088 old_adv = ret;
2089
2090 if (data->eee_enabled) {
2091 adv = !data->advertised ? cap :
2092 ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
2093 /* Mask prohibited EEE modes */
2094 adv &= ~dev->phydev->eee_broken_modes;
2095 }
2096
2097 if (old_adv != adv) {
2098 ret = rtl_set_eee_adv(tp, adv);
2099 if (ret < 0)
2100 goto out;
2101
2102 /* Restart autonegotiation so the new modes get sent to the
2103 * link partner.
2104 */
2105 ret = phy_restart_aneg(dev->phydev);
2106 }
2107
2108out:
2109 pm_runtime_put_noidle(d);
2110 return ret < 0 ? ret : 0;
2111}
2112
Jeff Garzik7282d492006-09-13 14:30:00 -04002113static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 .get_drvinfo = rtl8169_get_drvinfo,
2115 .get_regs_len = rtl8169_get_regs_len,
2116 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002117 .get_coalesce = rtl_get_coalesce,
2118 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002119 .get_msglevel = rtl8169_get_msglevel,
2120 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002122 .get_wol = rtl8169_get_wol,
2123 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002124 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002125 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002126 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002127 .get_ts_info = ethtool_op_get_ts_info,
Heiner Kallweitdd849572018-07-17 22:51:48 +02002128 .nway_reset = phy_ethtool_nway_reset,
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002129 .get_eee = rtl8169_get_eee,
2130 .set_eee = rtl8169_set_eee,
Heiner Kallweit45772432018-07-17 22:51:44 +02002131 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2132 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133};
2134
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002135static void rtl_enable_eee(struct rtl8169_private *tp)
2136{
2137 int supported = rtl_get_eee_supp(tp);
2138
2139 if (supported > 0)
2140 rtl_set_eee_adv(tp, supported);
2141}
2142
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002143static void rtl8169_get_mac_version(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Francois Romieu0e485152007-02-20 00:00:26 +01002145 /*
2146 * The driver currently handles the 8168Bf and the 8168Be identically
2147 * but they can be identified more specifically through the test below
2148 * if needed:
2149 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002150 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002151 *
2152 * Same thing for the 8101Eb and the 8101Ec:
2153 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002154 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002155 */
Francois Romieu37441002011-06-17 22:58:54 +02002156 static const struct rtl_mac_info {
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002157 u16 mask;
2158 u16 val;
2159 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002161 /* 8168EP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002162 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
2163 { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
2164 { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002165
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002166 /* 8168H family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002167 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
2168 { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002169
Hayes Wangc5583862012-07-02 17:23:22 +08002170 /* 8168G family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002171 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
2172 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
2173 { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
2174 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
Hayes Wangc5583862012-07-02 17:23:22 +08002175
Hayes Wangc2218922011-09-06 16:55:18 +08002176 /* 8168F family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002177 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
2178 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
2179 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
Hayes Wangc2218922011-09-06 16:55:18 +08002180
hayeswang01dc7fe2011-03-21 01:50:28 +00002181 /* 8168E family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002182 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
2183 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
2184 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002185
Francois Romieu5b538df2008-07-20 16:22:45 +02002186 /* 8168D family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002187 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
2188 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002189
françois romieue6de30d2011-01-03 15:08:37 +00002190 /* 8168DP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002191 { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
2192 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
2193 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002194
Francois Romieuef808d52008-06-29 13:10:54 +02002195 /* 8168C family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002196 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
2197 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
2198 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
2199 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
2200 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
2201 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
2202 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002203
2204 /* 8168B family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002205 { 0x7cf, 0x380, RTL_GIGA_MAC_VER_12 },
2206 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
2207 { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002208
2209 /* 8101 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002210 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
2211 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
2212 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
2213 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
2214 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
2215 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
2216 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
2217 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
2218 { 0x7cf, 0x340, RTL_GIGA_MAC_VER_13 },
2219 { 0x7cf, 0x343, RTL_GIGA_MAC_VER_10 },
2220 { 0x7cf, 0x342, RTL_GIGA_MAC_VER_16 },
2221 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
2222 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
2223 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_16 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002224 /* FIXME: where did these entries come from ? -- FR */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002225 { 0xfc8, 0x388, RTL_GIGA_MAC_VER_15 },
2226 { 0xfc8, 0x308, RTL_GIGA_MAC_VER_14 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002227
2228 /* 8110 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002229 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
2230 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
2231 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
2232 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
2233 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002234
Jean Delvaref21b75e2009-05-26 20:54:48 -07002235 /* Catch-all */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002236 { 0x000, 0x000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002237 };
2238 const struct rtl_mac_info *p = mac_info;
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002239 u16 reg = RTL_R32(tp, TxConfig) >> 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002241 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 p++;
2243 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002244
2245 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002246 dev_err(tp_to_dev(tp), "unknown chip XID %03x\n", reg & 0xfcf);
Heiner Kallweit45f19962018-11-22 22:00:10 +01002247 } else if (!tp->supports_gmii) {
2248 if (tp->mac_version == RTL_GIGA_MAC_VER_42)
2249 tp->mac_version = RTL_GIGA_MAC_VER_43;
2250 else if (tp->mac_version == RTL_GIGA_MAC_VER_45)
2251 tp->mac_version = RTL_GIGA_MAC_VER_47;
2252 else if (tp->mac_version == RTL_GIGA_MAC_VER_46)
2253 tp->mac_version = RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Francois Romieu867763c2007-08-17 18:21:58 +02002257struct phy_reg {
2258 u16 reg;
2259 u16 val;
2260};
2261
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002262static void __rtl_writephy_batch(struct rtl8169_private *tp,
2263 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002264{
2265 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002266 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002267 regs++;
2268 }
2269}
2270
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002271#define rtl_writephy_batch(tp, a) __rtl_writephy_batch(tp, a, ARRAY_SIZE(a))
2272
françois romieuf1e02ed2011-01-13 13:07:53 +00002273static void rtl_release_firmware(struct rtl8169_private *tp)
2274{
Heiner Kallweit254764e2019-01-22 22:23:41 +01002275 if (tp->rtl_fw) {
Heiner Kallweit47ad5932019-06-03 21:26:31 +02002276 rtl_fw_release_firmware(tp->rtl_fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02002277 kfree(tp->rtl_fw);
Heiner Kallweit254764e2019-01-22 22:23:41 +01002278 tp->rtl_fw = NULL;
Francois Romieub6ffd972011-06-17 17:00:05 +02002279 }
françois romieuf1e02ed2011-01-13 13:07:53 +00002280}
2281
François Romieu953a12c2011-04-24 17:38:48 +02002282static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002283{
Heiner Kallweitce8843a2019-05-29 21:15:06 +02002284 /* TODO: release firmware if rtl_fw_write_firmware signals failure. */
Heiner Kallweit254764e2019-01-22 22:23:41 +01002285 if (tp->rtl_fw)
Heiner Kallweitce8843a2019-05-29 21:15:06 +02002286 rtl_fw_write_firmware(tp, tp->rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002287}
2288
2289static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2290{
2291 if (rtl_readphy(tp, reg) != val)
2292 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2293 else
2294 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002295}
2296
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002297static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
2298{
Heiner Kallweitf4528252019-05-04 17:13:09 +02002299 /* Adjust EEE LED frequency */
2300 if (tp->mac_version != RTL_GIGA_MAC_VER_38)
2301 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
2302
Heiner Kallweite719b3e2019-04-28 11:11:47 +02002303 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_1111, 0x0003);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002304}
2305
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002306static void rtl8168f_config_eee_phy(struct rtl8169_private *tp)
2307{
2308 struct phy_device *phydev = tp->phydev;
2309
2310 phy_write(phydev, 0x1f, 0x0007);
2311 phy_write(phydev, 0x1e, 0x0020);
2312 phy_set_bits(phydev, 0x15, BIT(8));
2313
2314 phy_write(phydev, 0x1f, 0x0005);
2315 phy_write(phydev, 0x05, 0x8b85);
2316 phy_set_bits(phydev, 0x06, BIT(13));
2317
2318 phy_write(phydev, 0x1f, 0x0000);
2319}
2320
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002321static void rtl8168g_config_eee_phy(struct rtl8169_private *tp)
2322{
Heiner Kallweita2928d22019-06-02 10:53:49 +02002323 phy_modify_paged(tp->phydev, 0x0a43, 0x11, 0, BIT(4));
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002324}
2325
françois romieu4da19632011-01-03 15:07:55 +00002326static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002328 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002329 { 0x1f, 0x0001 },
2330 { 0x06, 0x006e },
2331 { 0x08, 0x0708 },
2332 { 0x15, 0x4000 },
2333 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
françois romieu0b9b5712009-08-10 19:44:56 +00002335 { 0x1f, 0x0001 },
2336 { 0x03, 0x00a1 },
2337 { 0x02, 0x0008 },
2338 { 0x01, 0x0120 },
2339 { 0x00, 0x1000 },
2340 { 0x04, 0x0800 },
2341 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
françois romieu0b9b5712009-08-10 19:44:56 +00002343 { 0x03, 0xff41 },
2344 { 0x02, 0xdf60 },
2345 { 0x01, 0x0140 },
2346 { 0x00, 0x0077 },
2347 { 0x04, 0x7800 },
2348 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
françois romieu0b9b5712009-08-10 19:44:56 +00002350 { 0x03, 0x802f },
2351 { 0x02, 0x4f02 },
2352 { 0x01, 0x0409 },
2353 { 0x00, 0xf0f9 },
2354 { 0x04, 0x9800 },
2355 { 0x04, 0x9000 },
2356
2357 { 0x03, 0xdf01 },
2358 { 0x02, 0xdf20 },
2359 { 0x01, 0xff95 },
2360 { 0x00, 0xba00 },
2361 { 0x04, 0xa800 },
2362 { 0x04, 0xa000 },
2363
2364 { 0x03, 0xff41 },
2365 { 0x02, 0xdf20 },
2366 { 0x01, 0x0140 },
2367 { 0x00, 0x00bb },
2368 { 0x04, 0xb800 },
2369 { 0x04, 0xb000 },
2370
2371 { 0x03, 0xdf41 },
2372 { 0x02, 0xdc60 },
2373 { 0x01, 0x6340 },
2374 { 0x00, 0x007d },
2375 { 0x04, 0xd800 },
2376 { 0x04, 0xd000 },
2377
2378 { 0x03, 0xdf01 },
2379 { 0x02, 0xdf20 },
2380 { 0x01, 0x100a },
2381 { 0x00, 0xa0ff },
2382 { 0x04, 0xf800 },
2383 { 0x04, 0xf000 },
2384
2385 { 0x1f, 0x0000 },
2386 { 0x0b, 0x0000 },
2387 { 0x00, 0x9200 }
2388 };
2389
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002390 rtl_writephy_batch(tp, phy_reg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
françois romieu4da19632011-01-03 15:07:55 +00002393static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002394{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002395 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002396 { 0x1f, 0x0002 },
2397 { 0x01, 0x90d0 },
2398 { 0x1f, 0x0000 }
2399 };
2400
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002401 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002402}
2403
françois romieu4da19632011-01-03 15:07:55 +00002404static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002405{
2406 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002407
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002408 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2409 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002410 return;
2411
françois romieu4da19632011-01-03 15:07:55 +00002412 rtl_writephy(tp, 0x1f, 0x0001);
2413 rtl_writephy(tp, 0x10, 0xf01b);
2414 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002415}
2416
françois romieu4da19632011-01-03 15:07:55 +00002417static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002418{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002419 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002420 { 0x1f, 0x0001 },
2421 { 0x04, 0x0000 },
2422 { 0x03, 0x00a1 },
2423 { 0x02, 0x0008 },
2424 { 0x01, 0x0120 },
2425 { 0x00, 0x1000 },
2426 { 0x04, 0x0800 },
2427 { 0x04, 0x9000 },
2428 { 0x03, 0x802f },
2429 { 0x02, 0x4f02 },
2430 { 0x01, 0x0409 },
2431 { 0x00, 0xf099 },
2432 { 0x04, 0x9800 },
2433 { 0x04, 0xa000 },
2434 { 0x03, 0xdf01 },
2435 { 0x02, 0xdf20 },
2436 { 0x01, 0xff95 },
2437 { 0x00, 0xba00 },
2438 { 0x04, 0xa800 },
2439 { 0x04, 0xf000 },
2440 { 0x03, 0xdf01 },
2441 { 0x02, 0xdf20 },
2442 { 0x01, 0x101a },
2443 { 0x00, 0xa0ff },
2444 { 0x04, 0xf800 },
2445 { 0x04, 0x0000 },
2446 { 0x1f, 0x0000 },
2447
2448 { 0x1f, 0x0001 },
2449 { 0x10, 0xf41b },
2450 { 0x14, 0xfb54 },
2451 { 0x18, 0xf5c7 },
2452 { 0x1f, 0x0000 },
2453
2454 { 0x1f, 0x0001 },
2455 { 0x17, 0x0cc0 },
2456 { 0x1f, 0x0000 }
2457 };
2458
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002459 rtl_writephy_batch(tp, phy_reg_init);
françois romieu2e9558562009-08-10 19:44:19 +00002460
françois romieu4da19632011-01-03 15:07:55 +00002461 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002462}
2463
françois romieu4da19632011-01-03 15:07:55 +00002464static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002465{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002466 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002467 { 0x1f, 0x0001 },
2468 { 0x04, 0x0000 },
2469 { 0x03, 0x00a1 },
2470 { 0x02, 0x0008 },
2471 { 0x01, 0x0120 },
2472 { 0x00, 0x1000 },
2473 { 0x04, 0x0800 },
2474 { 0x04, 0x9000 },
2475 { 0x03, 0x802f },
2476 { 0x02, 0x4f02 },
2477 { 0x01, 0x0409 },
2478 { 0x00, 0xf099 },
2479 { 0x04, 0x9800 },
2480 { 0x04, 0xa000 },
2481 { 0x03, 0xdf01 },
2482 { 0x02, 0xdf20 },
2483 { 0x01, 0xff95 },
2484 { 0x00, 0xba00 },
2485 { 0x04, 0xa800 },
2486 { 0x04, 0xf000 },
2487 { 0x03, 0xdf01 },
2488 { 0x02, 0xdf20 },
2489 { 0x01, 0x101a },
2490 { 0x00, 0xa0ff },
2491 { 0x04, 0xf800 },
2492 { 0x04, 0x0000 },
2493 { 0x1f, 0x0000 },
2494
2495 { 0x1f, 0x0001 },
2496 { 0x0b, 0x8480 },
2497 { 0x1f, 0x0000 },
2498
2499 { 0x1f, 0x0001 },
2500 { 0x18, 0x67c7 },
2501 { 0x04, 0x2000 },
2502 { 0x03, 0x002f },
2503 { 0x02, 0x4360 },
2504 { 0x01, 0x0109 },
2505 { 0x00, 0x3022 },
2506 { 0x04, 0x2800 },
2507 { 0x1f, 0x0000 },
2508
2509 { 0x1f, 0x0001 },
2510 { 0x17, 0x0cc0 },
2511 { 0x1f, 0x0000 }
2512 };
2513
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002514 rtl_writephy_batch(tp, phy_reg_init);
françois romieu8c7006a2009-08-10 19:43:29 +00002515}
2516
françois romieu4da19632011-01-03 15:07:55 +00002517static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002518{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002519 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002520 { 0x10, 0xf41b },
2521 { 0x1f, 0x0000 }
2522 };
2523
françois romieu4da19632011-01-03 15:07:55 +00002524 rtl_writephy(tp, 0x1f, 0x0001);
2525 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002526
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002527 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002528}
2529
françois romieu4da19632011-01-03 15:07:55 +00002530static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002531{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002532 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002533 { 0x1f, 0x0001 },
2534 { 0x10, 0xf41b },
2535 { 0x1f, 0x0000 }
2536 };
2537
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002538 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002539}
2540
françois romieu4da19632011-01-03 15:07:55 +00002541static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002542{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002543 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002544 { 0x1f, 0x0000 },
2545 { 0x1d, 0x0f00 },
2546 { 0x1f, 0x0002 },
2547 { 0x0c, 0x1ec8 },
2548 { 0x1f, 0x0000 }
2549 };
2550
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002551 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu867763c2007-08-17 18:21:58 +02002552}
2553
françois romieu4da19632011-01-03 15:07:55 +00002554static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002555{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002556 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002557 { 0x1f, 0x0001 },
2558 { 0x1d, 0x3d98 },
2559 { 0x1f, 0x0000 }
2560 };
2561
françois romieu4da19632011-01-03 15:07:55 +00002562 rtl_writephy(tp, 0x1f, 0x0000);
2563 rtl_patchphy(tp, 0x14, 1 << 5);
2564 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002565
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002566 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuef3386f2008-06-29 12:24:30 +02002567}
2568
françois romieu4da19632011-01-03 15:07:55 +00002569static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002570{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002571 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002572 { 0x1f, 0x0001 },
2573 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002574 { 0x1f, 0x0002 },
2575 { 0x00, 0x88d4 },
2576 { 0x01, 0x82b1 },
2577 { 0x03, 0x7002 },
2578 { 0x08, 0x9e30 },
2579 { 0x09, 0x01f0 },
2580 { 0x0a, 0x5500 },
2581 { 0x0c, 0x00c8 },
2582 { 0x1f, 0x0003 },
2583 { 0x12, 0xc096 },
2584 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002585 { 0x1f, 0x0000 },
2586 { 0x1f, 0x0000 },
2587 { 0x09, 0x2000 },
2588 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002589 };
2590
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002591 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002592
françois romieu4da19632011-01-03 15:07:55 +00002593 rtl_patchphy(tp, 0x14, 1 << 5);
2594 rtl_patchphy(tp, 0x0d, 1 << 5);
2595 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002596}
2597
françois romieu4da19632011-01-03 15:07:55 +00002598static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002599{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002600 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002601 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002602 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002603 { 0x03, 0x802f },
2604 { 0x02, 0x4f02 },
2605 { 0x01, 0x0409 },
2606 { 0x00, 0xf099 },
2607 { 0x04, 0x9800 },
2608 { 0x04, 0x9000 },
2609 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002610 { 0x1f, 0x0002 },
2611 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002612 { 0x06, 0x0761 },
2613 { 0x1f, 0x0003 },
2614 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002615 { 0x1f, 0x0000 }
2616 };
2617
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002618 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002619
françois romieu4da19632011-01-03 15:07:55 +00002620 rtl_patchphy(tp, 0x16, 1 << 0);
2621 rtl_patchphy(tp, 0x14, 1 << 5);
2622 rtl_patchphy(tp, 0x0d, 1 << 5);
2623 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002624}
2625
françois romieu4da19632011-01-03 15:07:55 +00002626static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002627{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002628 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002629 { 0x1f, 0x0001 },
2630 { 0x12, 0x2300 },
2631 { 0x1d, 0x3d98 },
2632 { 0x1f, 0x0002 },
2633 { 0x0c, 0x7eb8 },
2634 { 0x06, 0x5461 },
2635 { 0x1f, 0x0003 },
2636 { 0x16, 0x0f0a },
2637 { 0x1f, 0x0000 }
2638 };
2639
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002640 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu197ff762008-06-28 13:16:02 +02002641
françois romieu4da19632011-01-03 15:07:55 +00002642 rtl_patchphy(tp, 0x16, 1 << 0);
2643 rtl_patchphy(tp, 0x14, 1 << 5);
2644 rtl_patchphy(tp, 0x0d, 1 << 5);
2645 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002646}
2647
françois romieu4da19632011-01-03 15:07:55 +00002648static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002649{
françois romieu4da19632011-01-03 15:07:55 +00002650 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002651}
2652
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002653static const struct phy_reg rtl8168d_1_phy_reg_init_0[] = {
2654 /* Channel Estimation */
2655 { 0x1f, 0x0001 },
2656 { 0x06, 0x4064 },
2657 { 0x07, 0x2863 },
2658 { 0x08, 0x059c },
2659 { 0x09, 0x26b4 },
2660 { 0x0a, 0x6a19 },
2661 { 0x0b, 0xdcc8 },
2662 { 0x10, 0xf06d },
2663 { 0x14, 0x7f68 },
2664 { 0x18, 0x7fd9 },
2665 { 0x1c, 0xf0ff },
2666 { 0x1d, 0x3d9c },
2667 { 0x1f, 0x0003 },
2668 { 0x12, 0xf49f },
2669 { 0x13, 0x070b },
2670 { 0x1a, 0x05ad },
2671 { 0x14, 0x94c0 },
2672
2673 /*
2674 * Tx Error Issue
2675 * Enhance line driver power
2676 */
2677 { 0x1f, 0x0002 },
2678 { 0x06, 0x5561 },
2679 { 0x1f, 0x0005 },
2680 { 0x05, 0x8332 },
2681 { 0x06, 0x5561 },
2682
2683 /*
2684 * Can not link to 1Gbps with bad cable
2685 * Decrease SNR threshold form 21.07dB to 19.04dB
2686 */
2687 { 0x1f, 0x0001 },
2688 { 0x17, 0x0cc0 },
2689
2690 { 0x1f, 0x0000 },
2691 { 0x0d, 0xf880 }
2692};
2693
2694static const struct phy_reg rtl8168d_1_phy_reg_init_1[] = {
2695 { 0x1f, 0x0002 },
2696 { 0x05, 0x669a },
2697 { 0x1f, 0x0005 },
2698 { 0x05, 0x8330 },
2699 { 0x06, 0x669a },
2700 { 0x1f, 0x0002 }
2701};
2702
françois romieubca03d52011-01-03 15:07:31 +00002703static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002704{
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002705 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
Francois Romieu5b538df2008-07-20 16:22:45 +02002706
françois romieubca03d52011-01-03 15:07:31 +00002707 /*
2708 * Rx Error Issue
2709 * Fine Tune Switching regulator parameter
2710 */
françois romieu4da19632011-01-03 15:07:55 +00002711 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002712 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2713 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002714
Francois Romieufdf6fc02012-07-06 22:40:38 +02002715 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
françois romieudaf9df62009-10-07 12:44:20 +00002716 int val;
2717
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002718 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
françois romieudaf9df62009-10-07 12:44:20 +00002719
françois romieu4da19632011-01-03 15:07:55 +00002720 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002721
2722 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002723 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002724 0x0065, 0x0066, 0x0067, 0x0068,
2725 0x0069, 0x006a, 0x006b, 0x006c
2726 };
2727 int i;
2728
françois romieu4da19632011-01-03 15:07:55 +00002729 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002730
2731 val &= 0xff00;
2732 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002733 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002734 }
2735 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002736 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002737 { 0x1f, 0x0002 },
2738 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002739 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002740 { 0x05, 0x8330 },
2741 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002742 };
2743
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002744 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02002745 }
2746
françois romieubca03d52011-01-03 15:07:31 +00002747 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002748 rtl_writephy(tp, 0x1f, 0x0002);
2749 rtl_patchphy(tp, 0x0d, 0x0300);
2750 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002751
françois romieubca03d52011-01-03 15:07:31 +00002752 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002753 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002754 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2755 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002756
françois romieu4da19632011-01-03 15:07:55 +00002757 rtl_writephy(tp, 0x1f, 0x0005);
2758 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002759
2760 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002761
françois romieu4da19632011-01-03 15:07:55 +00002762 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002763}
2764
françois romieubca03d52011-01-03 15:07:31 +00002765static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002766{
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002767 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
françois romieudaf9df62009-10-07 12:44:20 +00002768
Francois Romieufdf6fc02012-07-06 22:40:38 +02002769 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
françois romieudaf9df62009-10-07 12:44:20 +00002770 int val;
2771
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002772 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
françois romieudaf9df62009-10-07 12:44:20 +00002773
françois romieu4da19632011-01-03 15:07:55 +00002774 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002775 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002776 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002777 0x0065, 0x0066, 0x0067, 0x0068,
2778 0x0069, 0x006a, 0x006b, 0x006c
2779 };
2780 int i;
2781
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002783
2784 val &= 0xff00;
2785 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002786 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002787 }
2788 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002789 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002790 { 0x1f, 0x0002 },
2791 { 0x05, 0x2642 },
2792 { 0x1f, 0x0005 },
2793 { 0x05, 0x8330 },
2794 { 0x06, 0x2642 }
2795 };
2796
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002797 rtl_writephy_batch(tp, phy_reg_init);
françois romieudaf9df62009-10-07 12:44:20 +00002798 }
2799
françois romieubca03d52011-01-03 15:07:31 +00002800 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002801 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002802 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2803 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002804
françois romieubca03d52011-01-03 15:07:31 +00002805 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002806 rtl_writephy(tp, 0x1f, 0x0002);
2807 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002808
françois romieu4da19632011-01-03 15:07:55 +00002809 rtl_writephy(tp, 0x1f, 0x0005);
2810 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002811
2812 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002813
françois romieu4da19632011-01-03 15:07:55 +00002814 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002815}
2816
françois romieu4da19632011-01-03 15:07:55 +00002817static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002818{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002819 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002820 { 0x1f, 0x0002 },
2821 { 0x10, 0x0008 },
2822 { 0x0d, 0x006c },
2823
2824 { 0x1f, 0x0000 },
2825 { 0x0d, 0xf880 },
2826
2827 { 0x1f, 0x0001 },
2828 { 0x17, 0x0cc0 },
2829
2830 { 0x1f, 0x0001 },
2831 { 0x0b, 0xa4d8 },
2832 { 0x09, 0x281c },
2833 { 0x07, 0x2883 },
2834 { 0x0a, 0x6b35 },
2835 { 0x1d, 0x3da4 },
2836 { 0x1c, 0xeffd },
2837 { 0x14, 0x7f52 },
2838 { 0x18, 0x7fc6 },
2839 { 0x08, 0x0601 },
2840 { 0x06, 0x4063 },
2841 { 0x10, 0xf074 },
2842 { 0x1f, 0x0003 },
2843 { 0x13, 0x0789 },
2844 { 0x12, 0xf4bd },
2845 { 0x1a, 0x04fd },
2846 { 0x14, 0x84b0 },
2847 { 0x1f, 0x0000 },
2848 { 0x00, 0x9200 },
2849
2850 { 0x1f, 0x0005 },
2851 { 0x01, 0x0340 },
2852 { 0x1f, 0x0001 },
2853 { 0x04, 0x4000 },
2854 { 0x03, 0x1d21 },
2855 { 0x02, 0x0c32 },
2856 { 0x01, 0x0200 },
2857 { 0x00, 0x5554 },
2858 { 0x04, 0x4800 },
2859 { 0x04, 0x4000 },
2860 { 0x04, 0xf000 },
2861 { 0x03, 0xdf01 },
2862 { 0x02, 0xdf20 },
2863 { 0x01, 0x101a },
2864 { 0x00, 0xa0ff },
2865 { 0x04, 0xf800 },
2866 { 0x04, 0xf000 },
2867 { 0x1f, 0x0000 },
2868
2869 { 0x1f, 0x0007 },
2870 { 0x1e, 0x0023 },
2871 { 0x16, 0x0000 },
2872 { 0x1f, 0x0000 }
2873 };
2874
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002875 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02002876}
2877
françois romieue6de30d2011-01-03 15:08:37 +00002878static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2879{
2880 static const struct phy_reg phy_reg_init[] = {
2881 { 0x1f, 0x0001 },
2882 { 0x17, 0x0cc0 },
2883
2884 { 0x1f, 0x0007 },
2885 { 0x1e, 0x002d },
2886 { 0x18, 0x0040 },
2887 { 0x1f, 0x0000 }
2888 };
2889
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002890 rtl_writephy_batch(tp, phy_reg_init);
françois romieue6de30d2011-01-03 15:08:37 +00002891 rtl_patchphy(tp, 0x0d, 1 << 5);
2892}
2893
Hayes Wang70090422011-07-06 15:58:06 +08002894static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002895{
2896 static const struct phy_reg phy_reg_init[] = {
2897 /* Enable Delay cap */
2898 { 0x1f, 0x0005 },
2899 { 0x05, 0x8b80 },
2900 { 0x06, 0xc896 },
2901 { 0x1f, 0x0000 },
2902
2903 /* Channel estimation fine tune */
2904 { 0x1f, 0x0001 },
2905 { 0x0b, 0x6c20 },
2906 { 0x07, 0x2872 },
2907 { 0x1c, 0xefff },
2908 { 0x1f, 0x0003 },
2909 { 0x14, 0x6420 },
2910 { 0x1f, 0x0000 },
2911
2912 /* Update PFM & 10M TX idle timer */
2913 { 0x1f, 0x0007 },
2914 { 0x1e, 0x002f },
2915 { 0x15, 0x1919 },
2916 { 0x1f, 0x0000 },
2917
2918 { 0x1f, 0x0007 },
2919 { 0x1e, 0x00ac },
2920 { 0x18, 0x0006 },
2921 { 0x1f, 0x0000 }
2922 };
2923
Francois Romieu15ecd032011-04-27 13:52:22 -07002924 rtl_apply_firmware(tp);
2925
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002926 rtl_writephy_batch(tp, phy_reg_init);
hayeswang01dc7fe2011-03-21 01:50:28 +00002927
2928 /* DCO enable for 10M IDLE Power */
2929 rtl_writephy(tp, 0x1f, 0x0007);
2930 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002931 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002932 rtl_writephy(tp, 0x1f, 0x0000);
2933
2934 /* For impedance matching */
2935 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002936 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002937 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002938
2939 /* PHY auto speed down */
2940 rtl_writephy(tp, 0x1f, 0x0007);
2941 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002942 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002943 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002944 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002945
2946 rtl_writephy(tp, 0x1f, 0x0005);
2947 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002948 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002949 rtl_writephy(tp, 0x1f, 0x0000);
2950
2951 rtl_writephy(tp, 0x1f, 0x0005);
2952 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002953 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002954 rtl_writephy(tp, 0x1f, 0x0007);
2955 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002956 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00002957 rtl_writephy(tp, 0x1f, 0x0006);
2958 rtl_writephy(tp, 0x00, 0x5a00);
2959 rtl_writephy(tp, 0x1f, 0x0000);
2960 rtl_writephy(tp, 0x0d, 0x0007);
2961 rtl_writephy(tp, 0x0e, 0x003c);
2962 rtl_writephy(tp, 0x0d, 0x4007);
2963 rtl_writephy(tp, 0x0e, 0x0000);
2964 rtl_writephy(tp, 0x0d, 0x0000);
2965}
2966
françois romieu9ecb9aa2012-12-07 11:20:21 +00002967static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
2968{
2969 const u16 w[] = {
2970 addr[0] | (addr[1] << 8),
2971 addr[2] | (addr[3] << 8),
2972 addr[4] | (addr[5] << 8)
2973 };
françois romieu9ecb9aa2012-12-07 11:20:21 +00002974
Heiner Kallweit3aa4c492019-05-02 20:46:52 +02002975 rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, w[0] | (w[1] << 16));
2976 rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, w[2]);
2977 rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, w[0] << 16);
2978 rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, w[1] | (w[2] << 16));
françois romieu9ecb9aa2012-12-07 11:20:21 +00002979}
2980
Hayes Wang70090422011-07-06 15:58:06 +08002981static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2982{
2983 static const struct phy_reg phy_reg_init[] = {
2984 /* Enable Delay cap */
2985 { 0x1f, 0x0004 },
2986 { 0x1f, 0x0007 },
2987 { 0x1e, 0x00ac },
2988 { 0x18, 0x0006 },
2989 { 0x1f, 0x0002 },
2990 { 0x1f, 0x0000 },
2991 { 0x1f, 0x0000 },
2992
2993 /* Channel estimation fine tune */
2994 { 0x1f, 0x0003 },
2995 { 0x09, 0xa20f },
2996 { 0x1f, 0x0000 },
2997 { 0x1f, 0x0000 },
2998
2999 /* Green Setting */
3000 { 0x1f, 0x0005 },
3001 { 0x05, 0x8b5b },
3002 { 0x06, 0x9222 },
3003 { 0x05, 0x8b6d },
3004 { 0x06, 0x8000 },
3005 { 0x05, 0x8b76 },
3006 { 0x06, 0x8000 },
3007 { 0x1f, 0x0000 }
3008 };
3009
3010 rtl_apply_firmware(tp);
3011
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003012 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang70090422011-07-06 15:58:06 +08003013
3014 /* For 4-corner performance improve */
3015 rtl_writephy(tp, 0x1f, 0x0005);
3016 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003017 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003018 rtl_writephy(tp, 0x1f, 0x0000);
3019
3020 /* PHY auto speed down */
3021 rtl_writephy(tp, 0x1f, 0x0004);
3022 rtl_writephy(tp, 0x1f, 0x0007);
3023 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003024 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003025 rtl_writephy(tp, 0x1f, 0x0002);
3026 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003027 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003028
3029 /* improve 10M EEE waveform */
3030 rtl_writephy(tp, 0x1f, 0x0005);
3031 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003032 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003033 rtl_writephy(tp, 0x1f, 0x0000);
3034
3035 /* Improve 2-pair detection performance */
3036 rtl_writephy(tp, 0x1f, 0x0005);
3037 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003038 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003039 rtl_writephy(tp, 0x1f, 0x0000);
3040
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01003041 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01003042 rtl_enable_eee(tp);
Hayes Wang70090422011-07-06 15:58:06 +08003043
3044 /* Green feature */
3045 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003046 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3047 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003048 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003049 rtl_writephy(tp, 0x1f, 0x0005);
3050 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3051 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003052
françois romieu9ecb9aa2012-12-07 11:20:21 +00003053 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3054 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003055}
3056
Hayes Wang5f886e02012-03-30 14:33:03 +08003057static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3058{
3059 /* For 4-corner performance improve */
3060 rtl_writephy(tp, 0x1f, 0x0005);
3061 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003062 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003063 rtl_writephy(tp, 0x1f, 0x0000);
3064
3065 /* PHY auto speed down */
3066 rtl_writephy(tp, 0x1f, 0x0007);
3067 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003068 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003069 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003070 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003071
3072 /* Improve 10M EEE waveform */
3073 rtl_writephy(tp, 0x1f, 0x0005);
3074 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003075 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003076 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01003077
3078 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01003079 rtl_enable_eee(tp);
Hayes Wang5f886e02012-03-30 14:33:03 +08003080}
3081
Hayes Wangc2218922011-09-06 16:55:18 +08003082static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3083{
3084 static const struct phy_reg phy_reg_init[] = {
3085 /* Channel estimation fine tune */
3086 { 0x1f, 0x0003 },
3087 { 0x09, 0xa20f },
3088 { 0x1f, 0x0000 },
3089
3090 /* Modify green table for giga & fnet */
3091 { 0x1f, 0x0005 },
3092 { 0x05, 0x8b55 },
3093 { 0x06, 0x0000 },
3094 { 0x05, 0x8b5e },
3095 { 0x06, 0x0000 },
3096 { 0x05, 0x8b67 },
3097 { 0x06, 0x0000 },
3098 { 0x05, 0x8b70 },
3099 { 0x06, 0x0000 },
3100 { 0x1f, 0x0000 },
3101 { 0x1f, 0x0007 },
3102 { 0x1e, 0x0078 },
3103 { 0x17, 0x0000 },
3104 { 0x19, 0x00fb },
3105 { 0x1f, 0x0000 },
3106
3107 /* Modify green table for 10M */
3108 { 0x1f, 0x0005 },
3109 { 0x05, 0x8b79 },
3110 { 0x06, 0xaa00 },
3111 { 0x1f, 0x0000 },
3112
3113 /* Disable hiimpedance detection (RTCT) */
3114 { 0x1f, 0x0003 },
3115 { 0x01, 0x328a },
3116 { 0x1f, 0x0000 }
3117 };
3118
3119 rtl_apply_firmware(tp);
3120
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003121 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangc2218922011-09-06 16:55:18 +08003122
Hayes Wang5f886e02012-03-30 14:33:03 +08003123 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003124
3125 /* Improve 2-pair detection performance */
3126 rtl_writephy(tp, 0x1f, 0x0005);
3127 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003128 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003129 rtl_writephy(tp, 0x1f, 0x0000);
3130}
3131
3132static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3133{
3134 rtl_apply_firmware(tp);
3135
Hayes Wang5f886e02012-03-30 14:33:03 +08003136 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003137}
3138
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003139static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3140{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003141 static const struct phy_reg phy_reg_init[] = {
3142 /* Channel estimation fine tune */
3143 { 0x1f, 0x0003 },
3144 { 0x09, 0xa20f },
3145 { 0x1f, 0x0000 },
3146
3147 /* Modify green table for giga & fnet */
3148 { 0x1f, 0x0005 },
3149 { 0x05, 0x8b55 },
3150 { 0x06, 0x0000 },
3151 { 0x05, 0x8b5e },
3152 { 0x06, 0x0000 },
3153 { 0x05, 0x8b67 },
3154 { 0x06, 0x0000 },
3155 { 0x05, 0x8b70 },
3156 { 0x06, 0x0000 },
3157 { 0x1f, 0x0000 },
3158 { 0x1f, 0x0007 },
3159 { 0x1e, 0x0078 },
3160 { 0x17, 0x0000 },
3161 { 0x19, 0x00aa },
3162 { 0x1f, 0x0000 },
3163
3164 /* Modify green table for 10M */
3165 { 0x1f, 0x0005 },
3166 { 0x05, 0x8b79 },
3167 { 0x06, 0xaa00 },
3168 { 0x1f, 0x0000 },
3169
3170 /* Disable hiimpedance detection (RTCT) */
3171 { 0x1f, 0x0003 },
3172 { 0x01, 0x328a },
3173 { 0x1f, 0x0000 }
3174 };
3175
3176
3177 rtl_apply_firmware(tp);
3178
3179 rtl8168f_hw_phy_config(tp);
3180
3181 /* Improve 2-pair detection performance */
3182 rtl_writephy(tp, 0x1f, 0x0005);
3183 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003184 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003185 rtl_writephy(tp, 0x1f, 0x0000);
3186
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003187 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003188
3189 /* Modify green table for giga */
3190 rtl_writephy(tp, 0x1f, 0x0005);
3191 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003192 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003193 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003194 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003195 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003196 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003197 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003198 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003199 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003200 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003201 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003202 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003203 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003204 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003205 rtl_writephy(tp, 0x1f, 0x0000);
3206
3207 /* uc same-seed solution */
3208 rtl_writephy(tp, 0x1f, 0x0005);
3209 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003210 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003211 rtl_writephy(tp, 0x1f, 0x0000);
3212
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003213 /* Green feature */
3214 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003215 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3216 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003217 rtl_writephy(tp, 0x1f, 0x0000);
3218}
3219
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003220static void rtl8168g_disable_aldps(struct rtl8169_private *tp)
3221{
Heiner Kallweita2928d22019-06-02 10:53:49 +02003222 phy_modify_paged(tp->phydev, 0x0a43, 0x10, BIT(2), 0);
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003223}
3224
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003225static void rtl8168g_phy_adjust_10m_aldps(struct rtl8169_private *tp)
3226{
3227 struct phy_device *phydev = tp->phydev;
3228
Heiner Kallweita2928d22019-06-02 10:53:49 +02003229 phy_modify_paged(phydev, 0x0bcc, 0x14, BIT(8), 0);
3230 phy_modify_paged(phydev, 0x0a44, 0x11, 0, BIT(7) | BIT(6));
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003231 phy_write(phydev, 0x1f, 0x0a43);
3232 phy_write(phydev, 0x13, 0x8084);
3233 phy_clear_bits(phydev, 0x14, BIT(14) | BIT(13));
3234 phy_set_bits(phydev, 0x10, BIT(12) | BIT(1) | BIT(0));
3235
3236 phy_write(phydev, 0x1f, 0x0000);
3237}
3238
Hayes Wangc5583862012-07-02 17:23:22 +08003239static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3240{
Heiner Kallweita2928d22019-06-02 10:53:49 +02003241 int ret;
3242
Hayes Wangc5583862012-07-02 17:23:22 +08003243 rtl_apply_firmware(tp);
3244
Heiner Kallweita2928d22019-06-02 10:53:49 +02003245 ret = phy_read_paged(tp->phydev, 0x0a46, 0x10);
3246 if (ret & BIT(8))
3247 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, BIT(15), 0);
3248 else
3249 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, 0, BIT(15));
Hayes Wangc5583862012-07-02 17:23:22 +08003250
Heiner Kallweita2928d22019-06-02 10:53:49 +02003251 ret = phy_read_paged(tp->phydev, 0x0a46, 0x13);
3252 if (ret & BIT(8))
3253 phy_modify_paged(tp->phydev, 0x0c41, 0x12, 0, BIT(1));
3254 else
3255 phy_modify_paged(tp->phydev, 0x0c41, 0x12, BIT(1), 0);
Hayes Wangc5583862012-07-02 17:23:22 +08003256
hayeswang41f44d12013-04-01 22:23:36 +00003257 /* Enable PHY auto speed down */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003258 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
Hayes Wangc5583862012-07-02 17:23:22 +08003259
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003260 rtl8168g_phy_adjust_10m_aldps(tp);
hayeswangfe7524c2013-04-01 22:23:37 +00003261
hayeswang41f44d12013-04-01 22:23:36 +00003262 /* EEE auto-fallback function */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003263 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
Hayes Wangc5583862012-07-02 17:23:22 +08003264
hayeswang41f44d12013-04-01 22:23:36 +00003265 /* Enable UC LPF tune function */
3266 rtl_writephy(tp, 0x1f, 0x0a43);
3267 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003268 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003269
Heiner Kallweita2928d22019-06-02 10:53:49 +02003270 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
hayeswang41f44d12013-04-01 22:23:36 +00003271
hayeswangfe7524c2013-04-01 22:23:37 +00003272 /* Improve SWR Efficiency */
3273 rtl_writephy(tp, 0x1f, 0x0bcd);
3274 rtl_writephy(tp, 0x14, 0x5065);
3275 rtl_writephy(tp, 0x14, 0xd065);
3276 rtl_writephy(tp, 0x1f, 0x0bc8);
3277 rtl_writephy(tp, 0x11, 0x5655);
3278 rtl_writephy(tp, 0x1f, 0x0bcd);
3279 rtl_writephy(tp, 0x14, 0x1065);
3280 rtl_writephy(tp, 0x14, 0x9065);
3281 rtl_writephy(tp, 0x14, 0x1065);
Heiner Kallweita2928d22019-06-02 10:53:49 +02003282 rtl_writephy(tp, 0x1f, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003283
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003284 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003285 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003286 rtl_enable_eee(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08003287}
3288
hayeswang57538c42013-04-01 22:23:40 +00003289static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3290{
3291 rtl_apply_firmware(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003292 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003293 rtl_enable_eee(tp);
hayeswang57538c42013-04-01 22:23:40 +00003294}
3295
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003296static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3297{
3298 u16 dout_tapbin;
3299 u32 data;
3300
3301 rtl_apply_firmware(tp);
3302
3303 /* CHN EST parameters adjust - giga master */
3304 rtl_writephy(tp, 0x1f, 0x0a43);
3305 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003306 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003307 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003308 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003309 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003310 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003311 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003312 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003313 rtl_writephy(tp, 0x1f, 0x0000);
3314
3315 /* CHN EST parameters adjust - giga slave */
3316 rtl_writephy(tp, 0x1f, 0x0a43);
3317 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003318 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003319 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003320 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003321 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003322 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003323 rtl_writephy(tp, 0x1f, 0x0000);
3324
3325 /* CHN EST parameters adjust - fnet */
3326 rtl_writephy(tp, 0x1f, 0x0a43);
3327 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003328 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003329 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003330 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003331 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003332 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003333 rtl_writephy(tp, 0x1f, 0x0000);
3334
3335 /* enable R-tune & PGA-retune function */
3336 dout_tapbin = 0;
3337 rtl_writephy(tp, 0x1f, 0x0a46);
3338 data = rtl_readphy(tp, 0x13);
3339 data &= 3;
3340 data <<= 2;
3341 dout_tapbin |= data;
3342 data = rtl_readphy(tp, 0x12);
3343 data &= 0xc000;
3344 data >>= 14;
3345 dout_tapbin |= data;
3346 dout_tapbin = ~(dout_tapbin^0x08);
3347 dout_tapbin <<= 12;
3348 dout_tapbin &= 0xf000;
3349 rtl_writephy(tp, 0x1f, 0x0a43);
3350 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003351 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003352 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003353 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003354 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003355 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003356 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003357 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003358
3359 rtl_writephy(tp, 0x1f, 0x0a43);
3360 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003361 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003362 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003363 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003364 rtl_writephy(tp, 0x1f, 0x0000);
3365
3366 /* enable GPHY 10M */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003367 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003368
3369 /* SAR ADC performance */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003370 phy_modify_paged(tp->phydev, 0x0bca, 0x17, BIT(12) | BIT(13), BIT(14));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003371
3372 rtl_writephy(tp, 0x1f, 0x0a43);
3373 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003374 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003375 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003376 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003377 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003378 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003379 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003380 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003381 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003382 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003383 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003384 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003385 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003386 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003387 rtl_writephy(tp, 0x1f, 0x0000);
3388
3389 /* disable phy pfm mode */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003390 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003391
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003392 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003393 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003394 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003395}
3396
3397static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3398{
3399 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3400 u16 rlen;
3401 u32 data;
3402
3403 rtl_apply_firmware(tp);
3404
3405 /* CHIN EST parameter update */
3406 rtl_writephy(tp, 0x1f, 0x0a43);
3407 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003408 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003409 rtl_writephy(tp, 0x1f, 0x0000);
3410
3411 /* enable R-tune & PGA-retune function */
3412 rtl_writephy(tp, 0x1f, 0x0a43);
3413 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003414 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003415 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003416 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003417 rtl_writephy(tp, 0x1f, 0x0000);
3418
3419 /* enable GPHY 10M */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003420 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003421
3422 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3423 data = r8168_mac_ocp_read(tp, 0xdd02);
3424 ioffset_p3 = ((data & 0x80)>>7);
3425 ioffset_p3 <<= 3;
3426
3427 data = r8168_mac_ocp_read(tp, 0xdd00);
3428 ioffset_p3 |= ((data & (0xe000))>>13);
3429 ioffset_p2 = ((data & (0x1e00))>>9);
3430 ioffset_p1 = ((data & (0x01e0))>>5);
3431 ioffset_p0 = ((data & 0x0010)>>4);
3432 ioffset_p0 <<= 3;
3433 ioffset_p0 |= (data & (0x07));
3434 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3435
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003436 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003437 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003438 rtl_writephy(tp, 0x1f, 0x0bcf);
3439 rtl_writephy(tp, 0x16, data);
3440 rtl_writephy(tp, 0x1f, 0x0000);
3441 }
3442
3443 /* Modify rlen (TX LPF corner frequency) level */
3444 rtl_writephy(tp, 0x1f, 0x0bcd);
3445 data = rtl_readphy(tp, 0x16);
3446 data &= 0x000f;
3447 rlen = 0;
3448 if (data > 3)
3449 rlen = data - 3;
3450 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3451 rtl_writephy(tp, 0x17, data);
3452 rtl_writephy(tp, 0x1f, 0x0bcd);
3453 rtl_writephy(tp, 0x1f, 0x0000);
3454
3455 /* disable phy pfm mode */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003456 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003457
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003458 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003459 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003460 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003461}
3462
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003463static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3464{
3465 /* Enable PHY auto speed down */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003466 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003467
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003468 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003469
3470 /* Enable EEE auto-fallback function */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003471 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003472
3473 /* Enable UC LPF tune function */
3474 rtl_writephy(tp, 0x1f, 0x0a43);
3475 rtl_writephy(tp, 0x13, 0x8012);
3476 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3477 rtl_writephy(tp, 0x1f, 0x0000);
3478
3479 /* set rg_sel_sdm_rate */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003480 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003481
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003482 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003483 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003484 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003485}
3486
3487static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3488{
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003489 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003490
3491 /* Enable UC LPF tune function */
3492 rtl_writephy(tp, 0x1f, 0x0a43);
3493 rtl_writephy(tp, 0x13, 0x8012);
3494 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3495 rtl_writephy(tp, 0x1f, 0x0000);
3496
3497 /* Set rg_sel_sdm_rate */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003498 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003499
3500 /* Channel estimation parameters */
3501 rtl_writephy(tp, 0x1f, 0x0a43);
3502 rtl_writephy(tp, 0x13, 0x80f3);
3503 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3504 rtl_writephy(tp, 0x13, 0x80f0);
3505 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3506 rtl_writephy(tp, 0x13, 0x80ef);
3507 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3508 rtl_writephy(tp, 0x13, 0x80f6);
3509 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3510 rtl_writephy(tp, 0x13, 0x80ec);
3511 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3512 rtl_writephy(tp, 0x13, 0x80ed);
3513 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3514 rtl_writephy(tp, 0x13, 0x80f2);
3515 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3516 rtl_writephy(tp, 0x13, 0x80f4);
3517 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3518 rtl_writephy(tp, 0x1f, 0x0a43);
3519 rtl_writephy(tp, 0x13, 0x8110);
3520 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3521 rtl_writephy(tp, 0x13, 0x810f);
3522 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3523 rtl_writephy(tp, 0x13, 0x8111);
3524 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3525 rtl_writephy(tp, 0x13, 0x8113);
3526 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3527 rtl_writephy(tp, 0x13, 0x8115);
3528 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3529 rtl_writephy(tp, 0x13, 0x810e);
3530 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3531 rtl_writephy(tp, 0x13, 0x810c);
3532 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3533 rtl_writephy(tp, 0x13, 0x810b);
3534 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3535 rtl_writephy(tp, 0x1f, 0x0a43);
3536 rtl_writephy(tp, 0x13, 0x80d1);
3537 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3538 rtl_writephy(tp, 0x13, 0x80cd);
3539 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3540 rtl_writephy(tp, 0x13, 0x80d3);
3541 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3542 rtl_writephy(tp, 0x13, 0x80d5);
3543 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3544 rtl_writephy(tp, 0x13, 0x80d7);
3545 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3546
3547 /* Force PWM-mode */
3548 rtl_writephy(tp, 0x1f, 0x0bcd);
3549 rtl_writephy(tp, 0x14, 0x5065);
3550 rtl_writephy(tp, 0x14, 0xd065);
3551 rtl_writephy(tp, 0x1f, 0x0bc8);
3552 rtl_writephy(tp, 0x12, 0x00ed);
3553 rtl_writephy(tp, 0x1f, 0x0bcd);
3554 rtl_writephy(tp, 0x14, 0x1065);
3555 rtl_writephy(tp, 0x14, 0x9065);
3556 rtl_writephy(tp, 0x14, 0x1065);
3557 rtl_writephy(tp, 0x1f, 0x0000);
3558
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003559 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003560 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003561 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003562}
3563
françois romieu4da19632011-01-03 15:07:55 +00003564static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003565{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003566 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003567 { 0x1f, 0x0003 },
3568 { 0x08, 0x441d },
3569 { 0x01, 0x9100 },
3570 { 0x1f, 0x0000 }
3571 };
3572
françois romieu4da19632011-01-03 15:07:55 +00003573 rtl_writephy(tp, 0x1f, 0x0000);
3574 rtl_patchphy(tp, 0x11, 1 << 12);
3575 rtl_patchphy(tp, 0x19, 1 << 13);
3576 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003577
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003578 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003579}
3580
Hayes Wang5a5e4442011-02-22 17:26:21 +08003581static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3582{
3583 static const struct phy_reg phy_reg_init[] = {
3584 { 0x1f, 0x0005 },
3585 { 0x1a, 0x0000 },
3586 { 0x1f, 0x0000 },
3587
3588 { 0x1f, 0x0004 },
3589 { 0x1c, 0x0000 },
3590 { 0x1f, 0x0000 },
3591
3592 { 0x1f, 0x0001 },
3593 { 0x15, 0x7701 },
3594 { 0x1f, 0x0000 }
3595 };
3596
3597 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003598 rtl_writephy(tp, 0x1f, 0x0000);
3599 rtl_writephy(tp, 0x18, 0x0310);
3600 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003601
François Romieu953a12c2011-04-24 17:38:48 +02003602 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003603
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003604 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003605}
3606
Hayes Wang7e18dca2012-03-30 14:33:02 +08003607static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3608{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003609 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003610 rtl_writephy(tp, 0x1f, 0x0000);
3611 rtl_writephy(tp, 0x18, 0x0310);
3612 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003613
3614 rtl_apply_firmware(tp);
3615
3616 /* EEE setting */
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003617 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003618 rtl_writephy(tp, 0x1f, 0x0004);
3619 rtl_writephy(tp, 0x10, 0x401f);
3620 rtl_writephy(tp, 0x19, 0x7030);
3621 rtl_writephy(tp, 0x1f, 0x0000);
3622}
3623
Hayes Wang5598bfe2012-07-02 17:23:21 +08003624static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3625{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003626 static const struct phy_reg phy_reg_init[] = {
3627 { 0x1f, 0x0004 },
3628 { 0x10, 0xc07f },
3629 { 0x19, 0x7030 },
3630 { 0x1f, 0x0000 }
3631 };
3632
3633 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003634 rtl_writephy(tp, 0x1f, 0x0000);
3635 rtl_writephy(tp, 0x18, 0x0310);
3636 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003637
3638 rtl_apply_firmware(tp);
3639
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003640 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003641 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003642
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003643 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003644}
3645
Francois Romieu5615d9f2007-08-17 17:50:46 +02003646static void rtl_hw_phy_config(struct net_device *dev)
3647{
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003648 static const rtl_generic_fct phy_configs[] = {
3649 /* PCI devices. */
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003650 [RTL_GIGA_MAC_VER_02] = rtl8169s_hw_phy_config,
3651 [RTL_GIGA_MAC_VER_03] = rtl8169s_hw_phy_config,
3652 [RTL_GIGA_MAC_VER_04] = rtl8169sb_hw_phy_config,
3653 [RTL_GIGA_MAC_VER_05] = rtl8169scd_hw_phy_config,
3654 [RTL_GIGA_MAC_VER_06] = rtl8169sce_hw_phy_config,
3655 /* PCI-E devices. */
3656 [RTL_GIGA_MAC_VER_07] = rtl8102e_hw_phy_config,
3657 [RTL_GIGA_MAC_VER_08] = rtl8102e_hw_phy_config,
3658 [RTL_GIGA_MAC_VER_09] = rtl8102e_hw_phy_config,
3659 [RTL_GIGA_MAC_VER_10] = NULL,
3660 [RTL_GIGA_MAC_VER_11] = rtl8168bb_hw_phy_config,
3661 [RTL_GIGA_MAC_VER_12] = rtl8168bef_hw_phy_config,
3662 [RTL_GIGA_MAC_VER_13] = NULL,
3663 [RTL_GIGA_MAC_VER_14] = NULL,
3664 [RTL_GIGA_MAC_VER_15] = NULL,
3665 [RTL_GIGA_MAC_VER_16] = NULL,
3666 [RTL_GIGA_MAC_VER_17] = rtl8168bef_hw_phy_config,
3667 [RTL_GIGA_MAC_VER_18] = rtl8168cp_1_hw_phy_config,
3668 [RTL_GIGA_MAC_VER_19] = rtl8168c_1_hw_phy_config,
3669 [RTL_GIGA_MAC_VER_20] = rtl8168c_2_hw_phy_config,
3670 [RTL_GIGA_MAC_VER_21] = rtl8168c_3_hw_phy_config,
3671 [RTL_GIGA_MAC_VER_22] = rtl8168c_4_hw_phy_config,
3672 [RTL_GIGA_MAC_VER_23] = rtl8168cp_2_hw_phy_config,
3673 [RTL_GIGA_MAC_VER_24] = rtl8168cp_2_hw_phy_config,
3674 [RTL_GIGA_MAC_VER_25] = rtl8168d_1_hw_phy_config,
3675 [RTL_GIGA_MAC_VER_26] = rtl8168d_2_hw_phy_config,
3676 [RTL_GIGA_MAC_VER_27] = rtl8168d_3_hw_phy_config,
3677 [RTL_GIGA_MAC_VER_28] = rtl8168d_4_hw_phy_config,
3678 [RTL_GIGA_MAC_VER_29] = rtl8105e_hw_phy_config,
3679 [RTL_GIGA_MAC_VER_30] = rtl8105e_hw_phy_config,
3680 [RTL_GIGA_MAC_VER_31] = NULL,
3681 [RTL_GIGA_MAC_VER_32] = rtl8168e_1_hw_phy_config,
3682 [RTL_GIGA_MAC_VER_33] = rtl8168e_1_hw_phy_config,
3683 [RTL_GIGA_MAC_VER_34] = rtl8168e_2_hw_phy_config,
3684 [RTL_GIGA_MAC_VER_35] = rtl8168f_1_hw_phy_config,
3685 [RTL_GIGA_MAC_VER_36] = rtl8168f_2_hw_phy_config,
3686 [RTL_GIGA_MAC_VER_37] = rtl8402_hw_phy_config,
3687 [RTL_GIGA_MAC_VER_38] = rtl8411_hw_phy_config,
3688 [RTL_GIGA_MAC_VER_39] = rtl8106e_hw_phy_config,
3689 [RTL_GIGA_MAC_VER_40] = rtl8168g_1_hw_phy_config,
3690 [RTL_GIGA_MAC_VER_41] = NULL,
3691 [RTL_GIGA_MAC_VER_42] = rtl8168g_2_hw_phy_config,
3692 [RTL_GIGA_MAC_VER_43] = rtl8168g_2_hw_phy_config,
3693 [RTL_GIGA_MAC_VER_44] = rtl8168g_2_hw_phy_config,
3694 [RTL_GIGA_MAC_VER_45] = rtl8168h_1_hw_phy_config,
3695 [RTL_GIGA_MAC_VER_46] = rtl8168h_2_hw_phy_config,
3696 [RTL_GIGA_MAC_VER_47] = rtl8168h_1_hw_phy_config,
3697 [RTL_GIGA_MAC_VER_48] = rtl8168h_2_hw_phy_config,
3698 [RTL_GIGA_MAC_VER_49] = rtl8168ep_1_hw_phy_config,
3699 [RTL_GIGA_MAC_VER_50] = rtl8168ep_2_hw_phy_config,
3700 [RTL_GIGA_MAC_VER_51] = rtl8168ep_2_hw_phy_config,
3701 };
Francois Romieu5615d9f2007-08-17 17:50:46 +02003702 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003703
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003704 if (phy_configs[tp->mac_version])
3705 phy_configs[tp->mac_version](tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003706}
3707
Francois Romieuda78dbf2012-01-26 14:18:23 +01003708static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3709{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003710 if (!test_and_set_bit(flag, tp->wk.flags))
3711 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003712}
3713
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003714static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
Francois Romieu5615d9f2007-08-17 17:50:46 +02003716 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003717
Marcus Sundberg773328942008-07-10 21:28:08 +02003718 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit7a67e112018-09-19 22:00:24 +02003719 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3720 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Heiner Kallweit49d17512018-06-28 20:36:15 +02003721 netif_dbg(tp, drv, dev,
3722 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003723 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02003724 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003725
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003726 /* We may have called phy_speed_down before */
Heiner Kallweit703732f2019-01-19 22:07:05 +01003727 phy_speed_up(tp->phydev);
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003728
Heiner Kallweit703732f2019-01-19 22:07:05 +01003729 genphy_soft_reset(tp->phydev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003730}
3731
Francois Romieu773d2022007-01-31 23:47:43 +01003732static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3733{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003734 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003735
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01003736 rtl_unlock_config_regs(tp);
françois romieu908ba2bf2010-04-26 11:42:58 +00003737
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003738 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
3739 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00003740
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003741 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
3742 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00003743
françois romieu9ecb9aa2012-12-07 11:20:21 +00003744 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3745 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00003746
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01003747 rtl_lock_config_regs(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003748
Francois Romieuda78dbf2012-01-26 14:18:23 +01003749 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003750}
3751
3752static int rtl_set_mac_address(struct net_device *dev, void *p)
3753{
3754 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01003755 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01003756 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01003757
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01003758 ret = eth_mac_addr(dev, p);
3759 if (ret)
3760 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01003761
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08003762 pm_runtime_get_noresume(d);
3763
3764 if (pm_runtime_active(d))
3765 rtl_rar_set(tp, dev->dev_addr);
3766
3767 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01003768
3769 return 0;
3770}
3771
Heiner Kallweite3972862018-06-29 08:07:04 +02003772static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003773{
Heiner Kallweit703732f2019-01-19 22:07:05 +01003774 struct rtl8169_private *tp = netdev_priv(dev);
3775
Heiner Kallweit69b3c592018-07-17 22:51:53 +02003776 if (!netif_running(dev))
3777 return -ENODEV;
Heiner Kallweite3972862018-06-29 08:07:04 +02003778
Heiner Kallweit703732f2019-01-19 22:07:05 +01003779 return phy_mii_ioctl(tp->phydev, ifr, cmd);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003780}
3781
David S. Miller1805b2f2011-10-24 18:18:09 -04003782static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3783{
David S. Miller1805b2f2011-10-24 18:18:09 -04003784 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00003785 case RTL_GIGA_MAC_VER_25:
3786 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003787 case RTL_GIGA_MAC_VER_29:
3788 case RTL_GIGA_MAC_VER_30:
3789 case RTL_GIGA_MAC_VER_32:
3790 case RTL_GIGA_MAC_VER_33:
3791 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02003792 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003793 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04003794 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3795 break;
3796 default:
3797 break;
3798 }
3799}
3800
Heiner Kallweit25e94112019-05-29 20:52:03 +02003801static void rtl_pll_power_down(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00003802{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01003803 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003804 return;
3805
hayeswang01dc7fe2011-03-21 01:50:28 +00003806 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3807 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02003808 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00003809
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01003810 if (device_may_wakeup(tp_to_dev(tp))) {
3811 phy_speed_down(tp->phydev, false);
3812 rtl_wol_suspend_quirk(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003813 return;
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01003814 }
françois romieu065c27c2011-01-03 15:08:12 +00003815
françois romieu065c27c2011-01-03 15:08:12 +00003816 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02003817 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003818 case RTL_GIGA_MAC_VER_37:
3819 case RTL_GIGA_MAC_VER_39:
3820 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08003821 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003822 case RTL_GIGA_MAC_VER_45:
3823 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003824 case RTL_GIGA_MAC_VER_47:
3825 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003826 case RTL_GIGA_MAC_VER_50:
3827 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003828 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00003829 break;
hayeswangbeb330a2013-04-01 22:23:39 +00003830 case RTL_GIGA_MAC_VER_40:
3831 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003832 case RTL_GIGA_MAC_VER_49:
Heiner Kallweite719b3e2019-04-28 11:11:47 +02003833 rtl_eri_clear_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003834 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00003835 break;
Heiner Kallweit76719ee2019-05-25 20:45:04 +02003836 default:
3837 break;
françois romieu065c27c2011-01-03 15:08:12 +00003838 }
3839}
3840
Heiner Kallweit25e94112019-05-29 20:52:03 +02003841static void rtl_pll_power_up(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00003842{
françois romieu065c27c2011-01-03 15:08:12 +00003843 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02003844 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003845 case RTL_GIGA_MAC_VER_37:
3846 case RTL_GIGA_MAC_VER_39:
3847 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003848 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00003849 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08003850 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003851 case RTL_GIGA_MAC_VER_45:
3852 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003853 case RTL_GIGA_MAC_VER_47:
3854 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003855 case RTL_GIGA_MAC_VER_50:
3856 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003857 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003858 break;
hayeswangbeb330a2013-04-01 22:23:39 +00003859 case RTL_GIGA_MAC_VER_40:
3860 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003861 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003862 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02003863 rtl_eri_set_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
hayeswangbeb330a2013-04-01 22:23:39 +00003864 break;
Heiner Kallweit76719ee2019-05-25 20:45:04 +02003865 default:
3866 break;
françois romieu065c27c2011-01-03 15:08:12 +00003867 }
3868
Heiner Kallweit703732f2019-01-19 22:07:05 +01003869 phy_resume(tp->phydev);
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02003870 /* give MAC/PHY some time to resume */
3871 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00003872}
3873
Hayes Wange542a222011-07-06 15:58:04 +08003874static void rtl_init_rxcfg(struct rtl8169_private *tp)
3875{
Hayes Wange542a222011-07-06 15:58:04 +08003876 switch (tp->mac_version) {
Heiner Kallweite9588eb2019-05-25 21:14:39 +02003877 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweit2a718832018-05-02 21:39:49 +02003878 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003879 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003880 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02003881 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
Maciej S. Szmigiero511cfd52018-10-11 16:02:10 +02003882 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
3883 case RTL_GIGA_MAC_VER_38:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003884 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003885 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02003886 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003887 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00003888 break;
Hayes Wange542a222011-07-06 15:58:04 +08003889 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003890 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003891 break;
3892 }
3893}
3894
Hayes Wang92fc43b2011-07-06 15:58:03 +08003895static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3896{
Timo Teräs9fba0812013-01-15 21:01:24 +00003897 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003898}
3899
Francois Romieud58d46b2011-05-03 16:38:29 +02003900static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3901{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003902 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3903 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01003904 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003905}
3906
3907static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3908{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003909 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3910 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003911 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003912}
3913
3914static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3915{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003916 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02003917}
3918
3919static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3920{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003921 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02003922}
3923
3924static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3925{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003926 RTL_W8(tp, MaxTxPacketSize, 0x3f);
3927 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3928 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01003929 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003930}
3931
3932static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3933{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003934 RTL_W8(tp, MaxTxPacketSize, 0x0c);
3935 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3936 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003937 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003938}
3939
3940static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3941{
Heiner Kallweitcb732002018-03-20 07:45:35 +01003942 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01003943 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02003944}
3945
3946static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3947{
Heiner Kallweitcb732002018-03-20 07:45:35 +01003948 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003949 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02003950}
3951
3952static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3953{
Francois Romieud58d46b2011-05-03 16:38:29 +02003954 r8168b_0_hw_jumbo_enable(tp);
3955
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003956 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02003957}
3958
3959static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3960{
Francois Romieud58d46b2011-05-03 16:38:29 +02003961 r8168b_0_hw_jumbo_disable(tp);
3962
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003963 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02003964}
3965
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003966static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02003967{
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003968 rtl_unlock_config_regs(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003969 switch (tp->mac_version) {
3970 case RTL_GIGA_MAC_VER_11:
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003971 r8168b_0_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003972 break;
3973 case RTL_GIGA_MAC_VER_12:
3974 case RTL_GIGA_MAC_VER_17:
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003975 r8168b_1_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003976 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003977 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
3978 r8168c_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003979 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003980 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
3981 r8168dp_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003982 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003983 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
3984 r8168e_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003985 break;
Francois Romieud58d46b2011-05-03 16:38:29 +02003986 default:
Francois Romieud58d46b2011-05-03 16:38:29 +02003987 break;
3988 }
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003989 rtl_lock_config_regs(tp);
3990}
3991
3992static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
3993{
3994 rtl_unlock_config_regs(tp);
3995 switch (tp->mac_version) {
3996 case RTL_GIGA_MAC_VER_11:
3997 r8168b_0_hw_jumbo_disable(tp);
3998 break;
3999 case RTL_GIGA_MAC_VER_12:
4000 case RTL_GIGA_MAC_VER_17:
4001 r8168b_1_hw_jumbo_disable(tp);
4002 break;
4003 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
4004 r8168c_hw_jumbo_disable(tp);
4005 break;
4006 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
4007 r8168dp_hw_jumbo_disable(tp);
4008 break;
4009 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
4010 r8168e_hw_jumbo_disable(tp);
4011 break;
4012 default:
4013 break;
4014 }
4015 rtl_lock_config_regs(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004016}
4017
Francois Romieuffc46952012-07-06 14:19:23 +02004018DECLARE_RTL_COND(rtl_chipcmd_cond)
4019{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004020 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004021}
4022
Francois Romieu6f43adc2011-04-29 15:05:51 +02004023static void rtl_hw_reset(struct rtl8169_private *tp)
4024{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004025 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004026
Francois Romieuffc46952012-07-06 14:19:23 +02004027 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004028}
4029
Heiner Kallweit254764e2019-01-22 22:23:41 +01004030static void rtl_request_firmware(struct rtl8169_private *tp)
Francois Romieub6ffd972011-06-17 17:00:05 +02004031{
4032 struct rtl_fw *rtl_fw;
Francois Romieub6ffd972011-06-17 17:00:05 +02004033
Heiner Kallweit254764e2019-01-22 22:23:41 +01004034 /* firmware loaded already or no firmware available */
4035 if (tp->rtl_fw || !tp->fw_name)
4036 return;
Francois Romieub6ffd972011-06-17 17:00:05 +02004037
4038 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
Heiner Kallweit47ad5932019-06-03 21:26:31 +02004039 if (!rtl_fw) {
4040 netif_warn(tp, ifup, tp->dev, "Unable to load firmware, out of memory\n");
4041 return;
4042 }
Francois Romieub6ffd972011-06-17 17:00:05 +02004043
Heiner Kallweitce8843a2019-05-29 21:15:06 +02004044 rtl_fw->phy_write = rtl_writephy;
4045 rtl_fw->phy_read = rtl_readphy;
4046 rtl_fw->mac_mcu_write = mac_mcu_write;
4047 rtl_fw->mac_mcu_read = mac_mcu_read;
Heiner Kallweit4edb00f2019-06-03 21:25:43 +02004048 rtl_fw->fw_name = tp->fw_name;
4049 rtl_fw->dev = tp_to_dev(tp);
Heiner Kallweitce8843a2019-05-29 21:15:06 +02004050
Heiner Kallweit47ad5932019-06-03 21:26:31 +02004051 if (rtl_fw_request_firmware(rtl_fw))
4052 kfree(rtl_fw);
4053 else
4054 tp->rtl_fw = rtl_fw;
François Romieu953a12c2011-04-24 17:38:48 +02004055}
4056
Hayes Wang92fc43b2011-07-06 15:58:03 +08004057static void rtl_rx_close(struct rtl8169_private *tp)
4058{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004059 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004060}
4061
Francois Romieuffc46952012-07-06 14:19:23 +02004062DECLARE_RTL_COND(rtl_npq_cond)
4063{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004064 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02004065}
4066
4067DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4068{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004069 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02004070}
4071
françois romieue6de30d2011-01-03 15:08:37 +00004072static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
4074 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004075 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Hayes Wang92fc43b2011-07-06 15:58:03 +08004077 rtl_rx_close(tp);
4078
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004079 switch (tp->mac_version) {
4080 case RTL_GIGA_MAC_VER_27:
4081 case RTL_GIGA_MAC_VER_28:
4082 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02004083 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004084 break;
4085 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4086 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004087 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004088 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004089 break;
4090 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004091 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004092 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004093 break;
françois romieue6de30d2011-01-03 15:08:37 +00004094 }
4095
Hayes Wang92fc43b2011-07-06 15:58:03 +08004096 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097}
4098
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004099static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004100{
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004101 u32 val = TX_DMA_BURST << TxDMAShift |
4102 InterFrameGap << TxInterFrameGapShift;
4103
4104 if (tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
4105 tp->mac_version != RTL_GIGA_MAC_VER_39)
4106 val |= TXCFG_AUTO_FIFO;
4107
4108 RTL_W32(tp, TxConfig, val);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004109}
4110
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004111static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004113 /* Low hurts. Let's disable the filtering. */
4114 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004115}
4116
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004117static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004118{
4119 /*
4120 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4121 * register to be written before TxDescAddrLow to work.
4122 * Switching from MMIO to I/O access fixes the issue as well.
4123 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004124 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4125 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4126 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4127 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004128}
4129
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004130static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004131{
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004132 u32 val;
Francois Romieu6dccd162007-02-13 23:38:05 +01004133
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004134 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4135 val = 0x000fff00;
4136 else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
4137 val = 0x00ffff00;
4138 else
4139 return;
4140
4141 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
4142 val |= 0xff;
4143
4144 RTL_W32(tp, 0x7c, val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004145}
4146
Francois Romieue6b763e2012-03-08 09:35:39 +01004147static void rtl_set_rx_mode(struct net_device *dev)
4148{
4149 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01004150 u32 mc_filter[2]; /* Multicast hash filter */
4151 int rx_mode;
4152 u32 tmp = 0;
4153
4154 if (dev->flags & IFF_PROMISC) {
4155 /* Unconditionally log net taps. */
4156 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4157 rx_mode =
4158 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4159 AcceptAllPhys;
4160 mc_filter[1] = mc_filter[0] = 0xffffffff;
4161 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4162 (dev->flags & IFF_ALLMULTI)) {
4163 /* Too many to filter perfectly -- accept all multicasts. */
4164 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4165 mc_filter[1] = mc_filter[0] = 0xffffffff;
4166 } else {
4167 struct netdev_hw_addr *ha;
4168
4169 rx_mode = AcceptBroadcast | AcceptMyPhys;
4170 mc_filter[1] = mc_filter[0] = 0;
4171 netdev_for_each_mc_addr(ha, dev) {
4172 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4173 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4174 rx_mode |= AcceptMulticast;
4175 }
4176 }
4177
4178 if (dev->features & NETIF_F_RXALL)
4179 rx_mode |= (AcceptErr | AcceptRunt);
4180
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004181 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01004182
4183 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4184 u32 data = mc_filter[0];
4185
4186 mc_filter[0] = swab32(mc_filter[1]);
4187 mc_filter[1] = swab32(data);
4188 }
4189
Nathan Walp04817762012-11-01 12:08:47 +00004190 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4191 mc_filter[1] = mc_filter[0] = 0xffffffff;
4192
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004193 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4194 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004195
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004196 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004197}
4198
Francois Romieuffc46952012-07-06 14:19:23 +02004199DECLARE_RTL_COND(rtl_csiar_cond)
4200{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004201 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02004202}
4203
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004204static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004205{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004206 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4207
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004208 RTL_W32(tp, CSIDR, value);
4209 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004210 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004211
Francois Romieuffc46952012-07-06 14:19:23 +02004212 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004213}
4214
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004215static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004216{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004217 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4218
4219 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4220 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004221
Francois Romieuffc46952012-07-06 14:19:23 +02004222 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004223 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004224}
4225
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004226static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004227{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004228 struct pci_dev *pdev = tp->pci_dev;
4229 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004230
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004231 /* According to Realtek the value at config space address 0x070f
4232 * controls the L0s/L1 entrance latency. We try standard ECAM access
4233 * first and if it fails fall back to CSI.
4234 */
4235 if (pdev->cfg_size > 0x070f &&
4236 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4237 return;
4238
4239 netdev_notice_once(tp->dev,
4240 "No native access to PCI extended config space, falling back to CSI\n");
4241 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4242 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004243}
4244
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004245static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08004246{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004247 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02004248}
4249
4250struct ephy_info {
4251 unsigned int offset;
4252 u16 mask;
4253 u16 bits;
4254};
4255
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004256static void __rtl_ephy_init(struct rtl8169_private *tp,
4257 const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004258{
4259 u16 w;
4260
4261 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004262 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4263 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004264 e++;
4265 }
4266}
4267
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004268#define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
4269
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004270static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02004271{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004272 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004273 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004274}
4275
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004276static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004277{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004278 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004279 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004280}
4281
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004282static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
hayeswangb51ecea2014-07-09 14:52:51 +08004283{
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004284 /* work around an issue when PCI reset occurs during L2/L3 state */
4285 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
hayeswangb51ecea2014-07-09 14:52:51 +08004286}
4287
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004288static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4289{
4290 if (enable) {
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004291 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
Kai-Heng Feng94235462018-09-12 14:58:20 +08004292 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004293 } else {
4294 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4295 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4296 }
Kai-Heng Feng94235462018-09-12 14:58:20 +08004297
4298 udelay(10);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004299}
4300
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004301static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
4302 u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
4303{
4304 /* Usage of dynamic vs. static FIFO is controlled by bit
4305 * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
4306 */
4307 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
4308 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
4309}
4310
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004311static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
4312 u8 low, u8 high)
4313{
4314 /* FIFO thresholds for pause flow control */
4315 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
4316 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
4317}
4318
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004319static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004320{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004321 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004322
françois romieufaf1e782013-02-27 13:01:57 +00004323 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004324 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00004325 PCI_EXP_DEVCTL_NOSNOOP_EN);
4326 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004327}
4328
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004329static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004330{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004331 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004332
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004333 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004334
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004335 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004336}
4337
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004338static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004339{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004340 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02004341
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004342 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004343
françois romieufaf1e782013-02-27 13:01:57 +00004344 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004345 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02004346
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004347 rtl_disable_clock_request(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004348}
4349
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004350static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004351{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004352 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004353 { 0x01, 0, 0x0001 },
4354 { 0x02, 0x0800, 0x1000 },
4355 { 0x03, 0, 0x0042 },
4356 { 0x06, 0x0080, 0x0000 },
4357 { 0x07, 0, 0x2000 }
4358 };
4359
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004360 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004361
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004362 rtl_ephy_init(tp, e_info_8168cp);
Francois Romieub726e492008-06-28 12:22:59 +02004363
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004364 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004365}
4366
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004367static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004368{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004369 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004370
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004371 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02004372
françois romieufaf1e782013-02-27 13:01:57 +00004373 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004374 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02004375}
4376
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004377static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004378{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004379 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004380
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004381 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004382
4383 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004384 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004385
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004386 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004387
françois romieufaf1e782013-02-27 13:01:57 +00004388 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004389 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004390}
4391
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004392static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004393{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004394 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004395 { 0x02, 0x0800, 0x1000 },
4396 { 0x03, 0, 0x0002 },
4397 { 0x06, 0x0080, 0x0000 }
4398 };
4399
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004400 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004401
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004402 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02004403
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004404 rtl_ephy_init(tp, e_info_8168c_1);
Francois Romieub726e492008-06-28 12:22:59 +02004405
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004406 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004407}
4408
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004409static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004410{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004411 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004412 { 0x01, 0, 0x0001 },
4413 { 0x03, 0x0400, 0x0220 }
4414 };
4415
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004416 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004417
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004418 rtl_ephy_init(tp, e_info_8168c_2);
Francois Romieub726e492008-06-28 12:22:59 +02004419
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004420 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004421}
4422
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004423static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004424{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004425 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004426}
4427
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004428static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004429{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004430 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004431
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004432 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004433}
4434
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004435static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004436{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004437 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004438
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004439 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004440
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004441 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004442
françois romieufaf1e782013-02-27 13:01:57 +00004443 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004444 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02004445}
4446
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004447static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004448{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004449 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004450
françois romieufaf1e782013-02-27 13:01:57 +00004451 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004452 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00004453
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004454 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00004455
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004456 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004457}
4458
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004459static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004460{
4461 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004462 { 0x0b, 0x0000, 0x0048 },
4463 { 0x19, 0x0020, 0x0050 },
4464 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00004465 };
françois romieue6de30d2011-01-03 15:08:37 +00004466
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004467 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004468
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004469 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00004470
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004471 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00004472
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004473 rtl_ephy_init(tp, e_info_8168d_4);
françois romieue6de30d2011-01-03 15:08:37 +00004474
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004475 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004476}
4477
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004478static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004479{
Hayes Wang70090422011-07-06 15:58:06 +08004480 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004481 { 0x00, 0x0200, 0x0100 },
4482 { 0x00, 0x0000, 0x0004 },
4483 { 0x06, 0x0002, 0x0001 },
4484 { 0x06, 0x0000, 0x0030 },
4485 { 0x07, 0x0000, 0x2000 },
4486 { 0x00, 0x0000, 0x0020 },
4487 { 0x03, 0x5800, 0x2000 },
4488 { 0x03, 0x0000, 0x0001 },
4489 { 0x01, 0x0800, 0x1000 },
4490 { 0x07, 0x0000, 0x4000 },
4491 { 0x1e, 0x0000, 0x2000 },
4492 { 0x19, 0xffff, 0xfe6c },
4493 { 0x0a, 0x0000, 0x0040 }
4494 };
4495
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004496 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004497
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004498 rtl_ephy_init(tp, e_info_8168e_1);
hayeswang01dc7fe2011-03-21 01:50:28 +00004499
françois romieufaf1e782013-02-27 13:01:57 +00004500 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004501 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00004502
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004503 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00004504
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004505 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004506
4507 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004508 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4509 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004510
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004511 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004512}
4513
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004514static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004515{
4516 static const struct ephy_info e_info_8168e_2[] = {
4517 { 0x09, 0x0000, 0x0080 },
4518 { 0x19, 0x0000, 0x0224 }
4519 };
4520
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004521 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004522
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004523 rtl_ephy_init(tp, e_info_8168e_2);
Hayes Wang70090422011-07-06 15:58:06 +08004524
françois romieufaf1e782013-02-27 13:01:57 +00004525 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004526 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08004527
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004528 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4529 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004530 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004531 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4532 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004533 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004534 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang70090422011-07-06 15:58:06 +08004535
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004536 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004537
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004538 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004539
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004540 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08004541
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01004542 rtl8168_config_eee_mac(tp);
4543
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004544 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4545 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4546 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02004547
4548 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08004549}
4550
Hayes Wang5f886e02012-03-30 14:33:03 +08004551static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004552{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004553 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004554
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004555 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08004556
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004557 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4558 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004559 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004560 rtl_reset_packet_filter(tp);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004561 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
4562 rtl_eri_set_bits(tp, 0x1d0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004563 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4564 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
Hayes Wangc2218922011-09-06 16:55:18 +08004565
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004566 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08004567
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004568 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004569
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004570 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
4571 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4572 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4573 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01004574
4575 rtl8168_config_eee_mac(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004576}
4577
Hayes Wang5f886e02012-03-30 14:33:03 +08004578static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4579{
Hayes Wang5f886e02012-03-30 14:33:03 +08004580 static const struct ephy_info e_info_8168f_1[] = {
4581 { 0x06, 0x00c0, 0x0020 },
4582 { 0x08, 0x0001, 0x0002 },
4583 { 0x09, 0x0000, 0x0080 },
4584 { 0x19, 0x0000, 0x0224 }
4585 };
4586
4587 rtl_hw_start_8168f(tp);
4588
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004589 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wang5f886e02012-03-30 14:33:03 +08004590
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004591 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang5f886e02012-03-30 14:33:03 +08004592}
4593
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004594static void rtl_hw_start_8411(struct rtl8169_private *tp)
4595{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004596 static const struct ephy_info e_info_8168f_1[] = {
4597 { 0x06, 0x00c0, 0x0020 },
4598 { 0x0f, 0xffff, 0x5200 },
4599 { 0x1e, 0x0000, 0x4000 },
4600 { 0x19, 0x0000, 0x0224 }
4601 };
4602
4603 rtl_hw_start_8168f(tp);
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004604 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004605
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004606 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004607
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004608 rtl_eri_set_bits(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004609}
4610
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004611static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08004612{
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004613 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004614 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Hayes Wangc5583862012-07-02 17:23:22 +08004615
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004616 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08004617
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004618 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08004619
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004620 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004621 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
Hayes Wangc5583862012-07-02 17:23:22 +08004622
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004623 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
4624 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08004625
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004626 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4627 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004628
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004629 rtl8168_config_eee_mac(tp);
4630
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004631 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004632 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
hayeswangb51ecea2014-07-09 14:52:51 +08004633
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004634 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08004635}
4636
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004637static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
4638{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004639 static const struct ephy_info e_info_8168g_1[] = {
4640 { 0x00, 0x0000, 0x0008 },
4641 { 0x0c, 0x37d0, 0x0820 },
4642 { 0x1e, 0x0000, 0x0001 },
4643 { 0x19, 0x8000, 0x0000 }
4644 };
4645
4646 rtl_hw_start_8168g(tp);
4647
4648 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004649 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004650 rtl_ephy_init(tp, e_info_8168g_1);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004651 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004652}
4653
hayeswang57538c42013-04-01 22:23:40 +00004654static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
4655{
hayeswang57538c42013-04-01 22:23:40 +00004656 static const struct ephy_info e_info_8168g_2[] = {
4657 { 0x00, 0x0000, 0x0008 },
4658 { 0x0c, 0x3df0, 0x0200 },
4659 { 0x19, 0xffff, 0xfc00 },
4660 { 0x1e, 0xffff, 0x20eb }
4661 };
4662
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004663 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00004664
4665 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004666 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4667 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004668 rtl_ephy_init(tp, e_info_8168g_2);
hayeswang57538c42013-04-01 22:23:40 +00004669}
4670
hayeswang45dd95c2013-07-08 17:09:01 +08004671static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
4672{
hayeswang45dd95c2013-07-08 17:09:01 +08004673 static const struct ephy_info e_info_8411_2[] = {
4674 { 0x00, 0x0000, 0x0008 },
4675 { 0x0c, 0x3df0, 0x0200 },
4676 { 0x0f, 0xffff, 0x5200 },
4677 { 0x19, 0x0020, 0x0000 },
4678 { 0x1e, 0x0000, 0x2000 }
4679 };
4680
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004681 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08004682
4683 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004684 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004685 rtl_ephy_init(tp, e_info_8411_2);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004686 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08004687}
4688
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004689static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
4690{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02004691 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004692 u32 data;
4693 static const struct ephy_info e_info_8168h_1[] = {
4694 { 0x1e, 0x0800, 0x0001 },
4695 { 0x1d, 0x0000, 0x0800 },
4696 { 0x05, 0xffff, 0x2089 },
4697 { 0x06, 0xffff, 0x5881 },
4698 { 0x04, 0xffff, 0x154a },
4699 { 0x01, 0xffff, 0x068b }
4700 };
4701
4702 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004703 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004704 rtl_ephy_init(tp, e_info_8168h_1);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004705
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004706 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004707 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004708
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004709 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004710
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004711 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004712
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004713 rtl_reset_packet_filter(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004714
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004715 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_1111, BIT(4));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004716
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004717 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004718
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004719 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004720
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004721 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
4722 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004723
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004724 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4725 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004726
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004727 rtl8168_config_eee_mac(tp);
4728
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004729 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4730 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004731
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004732 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004733
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004734 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004735
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004736 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004737
4738 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08004739 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004740 rtl_writephy(tp, 0x1f, 0x0000);
4741 if (rg_saw_cnt > 0) {
4742 u16 sw_cnt_1ms_ini;
4743
4744 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
4745 sw_cnt_1ms_ini &= 0x0fff;
4746 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08004747 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004748 data |= sw_cnt_1ms_ini;
4749 r8168_mac_ocp_write(tp, 0xd412, data);
4750 }
4751
4752 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08004753 data &= ~0xf0;
4754 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004755 r8168_mac_ocp_write(tp, 0xe056, data);
4756
4757 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08004758 data &= ~0x6000;
4759 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004760 r8168_mac_ocp_write(tp, 0xe052, data);
4761
4762 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08004763 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004764 data |= 0x017f;
4765 r8168_mac_ocp_write(tp, 0xe0d6, data);
4766
4767 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08004768 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004769 data |= 0x047f;
4770 r8168_mac_ocp_write(tp, 0xd420, data);
4771
4772 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
4773 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
4774 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
4775 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004776
4777 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004778}
4779
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004780static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
4781{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08004782 rtl8168ep_stop_cmac(tp);
4783
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004784 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004785 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004786
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004787 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004788
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004789 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004790
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004791 rtl_reset_packet_filter(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004792
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004793 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f80);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004794
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004795 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004796
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004797 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
4798 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004799
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004800 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4801 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004802
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004803 rtl8168_config_eee_mac(tp);
4804
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004805 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004806
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004807 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004808
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004809 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004810}
4811
4812static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
4813{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004814 static const struct ephy_info e_info_8168ep_1[] = {
4815 { 0x00, 0xffff, 0x10ab },
4816 { 0x06, 0xffff, 0xf030 },
4817 { 0x08, 0xffff, 0x2006 },
4818 { 0x0d, 0xffff, 0x1666 },
4819 { 0x0c, 0x3ff0, 0x0000 }
4820 };
4821
4822 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004823 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004824 rtl_ephy_init(tp, e_info_8168ep_1);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004825
4826 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004827
4828 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004829}
4830
4831static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
4832{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004833 static const struct ephy_info e_info_8168ep_2[] = {
4834 { 0x00, 0xffff, 0x10a3 },
4835 { 0x19, 0xffff, 0xfc00 },
4836 { 0x1e, 0xffff, 0x20ea }
4837 };
4838
4839 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004840 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004841 rtl_ephy_init(tp, e_info_8168ep_2);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004842
4843 rtl_hw_start_8168ep(tp);
4844
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004845 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4846 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004847
4848 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004849}
4850
4851static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
4852{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004853 u32 data;
4854 static const struct ephy_info e_info_8168ep_3[] = {
4855 { 0x00, 0xffff, 0x10a3 },
4856 { 0x19, 0xffff, 0x7c00 },
4857 { 0x1e, 0xffff, 0x20eb },
4858 { 0x0d, 0xffff, 0x1666 }
4859 };
4860
4861 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004862 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004863 rtl_ephy_init(tp, e_info_8168ep_3);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004864
4865 rtl_hw_start_8168ep(tp);
4866
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004867 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4868 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004869
4870 data = r8168_mac_ocp_read(tp, 0xd3e2);
4871 data &= 0xf000;
4872 data |= 0x0271;
4873 r8168_mac_ocp_write(tp, 0xd3e2, data);
4874
4875 data = r8168_mac_ocp_read(tp, 0xd3e4);
4876 data &= 0xff00;
4877 r8168_mac_ocp_write(tp, 0xd3e4, data);
4878
4879 data = r8168_mac_ocp_read(tp, 0xe860);
4880 data |= 0x0080;
4881 r8168_mac_ocp_write(tp, 0xe860, data);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004882
4883 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004884}
4885
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004886static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004887{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004888 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004889 { 0x01, 0, 0x6e65 },
4890 { 0x02, 0, 0x091f },
4891 { 0x03, 0, 0xc2f9 },
4892 { 0x06, 0, 0xafb5 },
4893 { 0x07, 0, 0x0e00 },
4894 { 0x19, 0, 0xec80 },
4895 { 0x01, 0, 0x2e65 },
4896 { 0x01, 0, 0x6e65 }
4897 };
4898 u8 cfg1;
4899
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004900 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004901
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004902 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004903
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004904 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004905
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004906 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02004907 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004908 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004909
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004910 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004911 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004912 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004913
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004914 rtl_ephy_init(tp, e_info_8102e_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004915}
4916
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004917static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004918{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004919 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004920
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004921 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004922
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004923 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
4924 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004925}
4926
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004927static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004928{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004929 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004930
Francois Romieufdf6fc02012-07-06 22:40:38 +02004931 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004932}
4933
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004934static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08004935{
4936 static const struct ephy_info e_info_8105e_1[] = {
4937 { 0x07, 0, 0x4000 },
4938 { 0x19, 0, 0x0200 },
4939 { 0x19, 0, 0x0020 },
4940 { 0x1e, 0, 0x2000 },
4941 { 0x03, 0, 0x0001 },
4942 { 0x19, 0, 0x0100 },
4943 { 0x19, 0, 0x0004 },
4944 { 0x0a, 0, 0x0020 }
4945 };
4946
Francois Romieucecb5fd2011-04-01 10:21:07 +02004947 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004948 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004949
Francois Romieucecb5fd2011-04-01 10:21:07 +02004950 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004951 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004952
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004953 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
4954 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004955
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004956 rtl_ephy_init(tp, e_info_8105e_1);
hayeswangb51ecea2014-07-09 14:52:51 +08004957
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004958 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004959}
4960
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004961static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08004962{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004963 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02004964 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004965}
4966
Hayes Wang7e18dca2012-03-30 14:33:02 +08004967static void rtl_hw_start_8402(struct rtl8169_private *tp)
4968{
Hayes Wang7e18dca2012-03-30 14:33:02 +08004969 static const struct ephy_info e_info_8402[] = {
4970 { 0x19, 0xffff, 0xff64 },
4971 { 0x1e, 0, 0x4000 }
4972 };
4973
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004974 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004975
4976 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004977 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004978
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004979 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004980
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004981 rtl_ephy_init(tp, e_info_8402);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004982
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004983 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004984
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004985 rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004986 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004987 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4988 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4989 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00);
hayeswangb51ecea2014-07-09 14:52:51 +08004990
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004991 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004992}
4993
Hayes Wang5598bfe2012-07-02 17:23:21 +08004994static void rtl_hw_start_8106(struct rtl8169_private *tp)
4995{
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08004996 rtl_hw_aspm_clkreq_enable(tp, false);
4997
Hayes Wang5598bfe2012-07-02 17:23:21 +08004998 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004999 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005000
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005001 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5002 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5003 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005004
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005005 rtl_pcie_state_l2l3_disable(tp);
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005006 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005007}
5008
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005009static void rtl_hw_config(struct rtl8169_private *tp)
5010{
5011 static const rtl_generic_fct hw_configs[] = {
5012 [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
5013 [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
5014 [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
5015 [RTL_GIGA_MAC_VER_10] = NULL,
5016 [RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168bb,
5017 [RTL_GIGA_MAC_VER_12] = rtl_hw_start_8168bef,
5018 [RTL_GIGA_MAC_VER_13] = NULL,
5019 [RTL_GIGA_MAC_VER_14] = NULL,
5020 [RTL_GIGA_MAC_VER_15] = NULL,
5021 [RTL_GIGA_MAC_VER_16] = NULL,
5022 [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168bef,
5023 [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
5024 [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
5025 [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
5026 [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_3,
5027 [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
5028 [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
5029 [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
5030 [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
5031 [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
5032 [RTL_GIGA_MAC_VER_27] = rtl_hw_start_8168d,
5033 [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
5034 [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
5035 [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
5036 [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168dp,
5037 [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
5038 [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
5039 [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
5040 [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
5041 [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
5042 [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
5043 [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
5044 [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
5045 [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
5046 [RTL_GIGA_MAC_VER_41] = rtl_hw_start_8168g_1,
5047 [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
5048 [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
5049 [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
5050 [RTL_GIGA_MAC_VER_45] = rtl_hw_start_8168h_1,
5051 [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
5052 [RTL_GIGA_MAC_VER_47] = rtl_hw_start_8168h_1,
5053 [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
5054 [RTL_GIGA_MAC_VER_49] = rtl_hw_start_8168ep_1,
5055 [RTL_GIGA_MAC_VER_50] = rtl_hw_start_8168ep_2,
5056 [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
5057 };
5058
5059 if (hw_configs[tp->mac_version])
5060 hw_configs[tp->mac_version](tp);
5061}
5062
5063static void rtl_hw_start_8168(struct rtl8169_private *tp)
5064{
Francois Romieucecb5fd2011-04-01 10:21:07 +02005065 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005066 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005067 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005068 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005069
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005070 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005071
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005072 rtl_hw_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073}
5074
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005075static void rtl_hw_start_8169(struct rtl8169_private *tp)
5076{
5077 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
5078 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
5079
5080 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
5081
5082 tp->cp_cmd |= PCIMulRW;
5083
5084 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5085 tp->mac_version == RTL_GIGA_MAC_VER_03) {
5086 netif_dbg(tp, drv, tp->dev,
5087 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
5088 tp->cp_cmd |= (1 << 14);
5089 }
5090
5091 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5092
5093 rtl8169_set_magic_reg(tp, tp->mac_version);
5094
5095 RTL_W32(tp, RxMissed, 0);
5096}
5097
5098static void rtl_hw_start(struct rtl8169_private *tp)
5099{
5100 rtl_unlock_config_regs(tp);
5101
5102 tp->cp_cmd &= CPCMD_MASK;
5103 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5104
5105 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
5106 rtl_hw_start_8169(tp);
5107 else
5108 rtl_hw_start_8168(tp);
5109
5110 rtl_set_rx_max_size(tp);
5111 rtl_set_rx_tx_desc_registers(tp);
5112 rtl_lock_config_regs(tp);
5113
5114 /* disable interrupt coalescing */
5115 RTL_W16(tp, IntrMitigate, 0x0000);
5116 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5117 RTL_R8(tp, IntrMask);
5118 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5119 rtl_init_rxcfg(tp);
5120 rtl_set_tx_config_registers(tp);
5121
5122 rtl_set_rx_mode(tp->dev);
5123 /* no early-rx interrupts */
5124 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
5125 rtl_irq_enable(tp);
5126}
5127
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5129{
Francois Romieud58d46b2011-05-03 16:38:29 +02005130 struct rtl8169_private *tp = netdev_priv(dev);
5131
Francois Romieud58d46b2011-05-03 16:38:29 +02005132 if (new_mtu > ETH_DATA_LEN)
5133 rtl_hw_jumbo_enable(tp);
5134 else
5135 rtl_hw_jumbo_disable(tp);
5136
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005138 netdev_update_features(dev);
5139
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141}
5142
5143static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5144{
Al Viro95e09182007-12-22 18:55:39 +00005145 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5147}
5148
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005149static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5150 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005152 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5153 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005154
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005155 kfree(*data_buff);
5156 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 rtl8169_make_unusable_by_asic(desc);
5158}
5159
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005160static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161{
5162 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5163
Alexander Duycka0750132014-12-11 15:02:17 -08005164 /* Force memory writes to complete before releasing descriptor */
5165 dma_wmb();
5166
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005167 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168}
5169
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005170static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5171 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005172{
5173 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005175 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02005176 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005178 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005179 if (!data)
5180 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005181
Heiner Kallweit6dc8b742019-01-20 11:12:01 +01005182 /* Memory should be properly aligned, but better check. */
5183 if (!IS_ALIGNED((unsigned long)data, 8)) {
5184 netdev_err_once(tp->dev, "RX buffer not 8-byte-aligned\n");
5185 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005186 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005187
Heiner Kallweit6dc8b742019-01-20 11:12:01 +01005188 mapping = dma_map_single(d, data, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005189 if (unlikely(dma_mapping_error(d, mapping))) {
5190 if (net_ratelimit())
5191 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005192 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
Heiner Kallweitd731af72018-04-17 23:26:41 +02005195 desc->addr = cpu_to_le64(mapping);
5196 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005197 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005198
5199err_out:
5200 kfree(data);
5201 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202}
5203
5204static void rtl8169_rx_clear(struct rtl8169_private *tp)
5205{
Francois Romieu07d3f512007-02-21 22:40:46 +01005206 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
5208 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005209 if (tp->Rx_databuff[i]) {
5210 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 tp->RxDescArray + i);
5212 }
5213 }
5214}
5215
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005216static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005218 desc->opts1 |= cpu_to_le32(RingEnd);
5219}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005220
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005221static int rtl8169_rx_fill(struct rtl8169_private *tp)
5222{
5223 unsigned int i;
5224
5225 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005226 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005227
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005228 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005229 if (!data) {
5230 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005231 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005232 }
5233 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005236 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5237 return 0;
5238
5239err_out:
5240 rtl8169_rx_clear(tp);
5241 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242}
5243
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005244static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 rtl8169_init_ring_indexes(tp);
5247
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005248 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5249 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005251 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252}
5253
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005254static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 struct TxDesc *desc)
5256{
5257 unsigned int len = tx_skb->len;
5258
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005259 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5260
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 desc->opts1 = 0x00;
5262 desc->opts2 = 0x00;
5263 desc->addr = 0x00;
5264 tx_skb->len = 0;
5265}
5266
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005267static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5268 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269{
5270 unsigned int i;
5271
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005272 for (i = 0; i < n; i++) {
5273 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 struct ring_info *tx_skb = tp->tx_skb + entry;
5275 unsigned int len = tx_skb->len;
5276
5277 if (len) {
5278 struct sk_buff *skb = tx_skb->skb;
5279
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005280 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 tp->TxDescArray + entry);
5282 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07005283 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 tx_skb->skb = NULL;
5285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 }
5287 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005288}
5289
5290static void rtl8169_tx_clear(struct rtl8169_private *tp)
5291{
5292 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 tp->cur_tx = tp->dirty_tx = 0;
Florian Westphald92060b2018-10-20 12:25:27 +02005294 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295}
5296
Francois Romieu4422bcd2012-01-26 11:23:32 +01005297static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
David Howellsc4028952006-11-22 14:57:56 +00005299 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005300 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
Francois Romieuda78dbf2012-01-26 14:18:23 +01005302 napi_disable(&tp->napi);
5303 netif_stop_queue(dev);
Paul E. McKenney16f11502018-11-05 17:07:39 -08005304 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
françois romieuc7c2c392011-12-04 20:30:52 +00005306 rtl8169_hw_reset(tp);
5307
Francois Romieu56de4142011-03-15 17:29:31 +01005308 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005309 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01005310
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005312 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
Francois Romieuda78dbf2012-01-26 14:18:23 +01005314 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005315 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005316 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317}
5318
5319static void rtl8169_tx_timeout(struct net_device *dev)
5320{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005321 struct rtl8169_private *tp = netdev_priv(dev);
5322
5323 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324}
5325
Heiner Kallweit734c1402018-11-22 21:56:48 +01005326static __le32 rtl8169_get_txd_opts1(u32 opts0, u32 len, unsigned int entry)
5327{
5328 u32 status = opts0 | len;
5329
5330 if (entry == NUM_TX_DESC - 1)
5331 status |= RingEnd;
5332
5333 return cpu_to_le32(status);
5334}
5335
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005337 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338{
5339 struct skb_shared_info *info = skb_shinfo(skb);
5340 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005341 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005342 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
5344 entry = tp->cur_tx;
5345 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005346 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005348 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 void *addr;
5350
5351 entry = (entry + 1) % NUM_TX_DESC;
5352
5353 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005354 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005355 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005356 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005357 if (unlikely(dma_mapping_error(d, mapping))) {
5358 if (net_ratelimit())
5359 netif_err(tp, drv, tp->dev,
5360 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005361 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
Heiner Kallweit734c1402018-11-22 21:56:48 +01005364 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005365 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 txd->addr = cpu_to_le64(mapping);
5367
5368 tp->tx_skb[entry].len = len;
5369 }
5370
5371 if (cur_frag) {
5372 tp->tx_skb[entry].skb = skb;
5373 txd->opts1 |= cpu_to_le32(LastFrag);
5374 }
5375
5376 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005377
5378err_out:
5379 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5380 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381}
5382
françois romieub423e9a2013-05-18 01:24:46 +00005383static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5384{
5385 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5386}
5387
hayeswange9746042014-07-11 16:25:58 +08005388static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5389 struct net_device *dev);
5390/* r8169_csum_workaround()
5391 * The hw limites the value the transport offset. When the offset is out of the
5392 * range, calculate the checksum by sw.
5393 */
5394static void r8169_csum_workaround(struct rtl8169_private *tp,
5395 struct sk_buff *skb)
5396{
Heiner Kallweit0b12c732019-05-31 19:17:15 +02005397 if (skb_is_gso(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005398 netdev_features_t features = tp->dev->features;
5399 struct sk_buff *segs, *nskb;
5400
5401 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
5402 segs = skb_gso_segment(skb, features);
5403 if (IS_ERR(segs) || !segs)
5404 goto drop;
5405
5406 do {
5407 nskb = segs;
5408 segs = segs->next;
5409 nskb->next = NULL;
5410 rtl8169_start_xmit(nskb, tp->dev);
5411 } while (segs);
5412
Alexander Duyckeb781392015-05-01 10:34:44 -07005413 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08005414 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5415 if (skb_checksum_help(skb) < 0)
5416 goto drop;
5417
5418 rtl8169_start_xmit(skb, tp->dev);
5419 } else {
hayeswange9746042014-07-11 16:25:58 +08005420drop:
Heiner Kallweit0b12c732019-05-31 19:17:15 +02005421 tp->dev->stats.tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07005422 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08005423 }
5424}
5425
5426/* msdn_giant_send_check()
5427 * According to the document of microsoft, the TCP Pseudo Header excludes the
5428 * packet length for IPv6 TCP large packets.
5429 */
5430static int msdn_giant_send_check(struct sk_buff *skb)
5431{
5432 const struct ipv6hdr *ipv6h;
5433 struct tcphdr *th;
5434 int ret;
5435
5436 ret = skb_cow_head(skb, 0);
5437 if (ret)
5438 return ret;
5439
5440 ipv6h = ipv6_hdr(skb);
5441 th = tcp_hdr(skb);
5442
5443 th->check = 0;
5444 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
5445
5446 return ret;
5447}
5448
Heiner Kallweit87945b62019-05-31 19:55:11 +02005449static void rtl8169_tso_csum_v1(struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
Michał Mirosław350fb322011-04-08 06:35:56 +00005451 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
Francois Romieu2b7b4312011-04-18 22:53:24 -07005453 if (mss) {
5454 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08005455 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5456 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5457 const struct iphdr *ip = ip_hdr(skb);
5458
5459 if (ip->protocol == IPPROTO_TCP)
5460 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5461 else if (ip->protocol == IPPROTO_UDP)
5462 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5463 else
5464 WARN_ON_ONCE(1);
5465 }
hayeswang5888d3f2014-07-11 16:25:56 +08005466}
5467
5468static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5469 struct sk_buff *skb, u32 *opts)
5470{
hayeswangbdfa4ed2014-07-11 16:25:57 +08005471 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08005472 u32 mss = skb_shinfo(skb)->gso_size;
5473
5474 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08005475 if (transport_offset > GTTCPHO_MAX) {
5476 netif_warn(tp, tx_err, tp->dev,
5477 "Invalid transport offset 0x%x for TSO\n",
5478 transport_offset);
5479 return false;
5480 }
5481
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005482 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005483 case htons(ETH_P_IP):
5484 opts[0] |= TD1_GTSENV4;
5485 break;
5486
5487 case htons(ETH_P_IPV6):
5488 if (msdn_giant_send_check(skb))
5489 return false;
5490
5491 opts[0] |= TD1_GTSENV6;
5492 break;
5493
5494 default:
5495 WARN_ON_ONCE(1);
5496 break;
5497 }
5498
hayeswangbdfa4ed2014-07-11 16:25:57 +08005499 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08005500 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005501 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08005502 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
françois romieub423e9a2013-05-18 01:24:46 +00005504 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08005505 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00005506
hayeswange9746042014-07-11 16:25:58 +08005507 if (transport_offset > TCPHO_MAX) {
5508 netif_warn(tp, tx_err, tp->dev,
5509 "Invalid transport offset 0x%x\n",
5510 transport_offset);
5511 return false;
5512 }
5513
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005514 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005515 case htons(ETH_P_IP):
5516 opts[1] |= TD1_IPv4_CS;
5517 ip_protocol = ip_hdr(skb)->protocol;
5518 break;
5519
5520 case htons(ETH_P_IPV6):
5521 opts[1] |= TD1_IPv6_CS;
5522 ip_protocol = ipv6_hdr(skb)->nexthdr;
5523 break;
5524
5525 default:
5526 ip_protocol = IPPROTO_RAW;
5527 break;
5528 }
5529
5530 if (ip_protocol == IPPROTO_TCP)
5531 opts[1] |= TD1_TCP_CS;
5532 else if (ip_protocol == IPPROTO_UDP)
5533 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005534 else
5535 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08005536
5537 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00005538 } else {
5539 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08005540 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 }
hayeswang5888d3f2014-07-11 16:25:56 +08005542
françois romieub423e9a2013-05-18 01:24:46 +00005543 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544}
5545
Heiner Kallweit76085c92018-11-22 22:03:08 +01005546static bool rtl_tx_slots_avail(struct rtl8169_private *tp,
5547 unsigned int nr_frags)
5548{
5549 unsigned int slots_avail = tp->dirty_tx + NUM_TX_DESC - tp->cur_tx;
5550
5551 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
5552 return slots_avail > nr_frags;
5553}
5554
Heiner Kallweit87945b62019-05-31 19:55:11 +02005555/* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
5556static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
5557{
5558 switch (tp->mac_version) {
5559 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
5560 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
5561 return false;
5562 default:
5563 return true;
5564 }
5565}
5566
Stephen Hemminger613573252009-08-31 19:50:58 +00005567static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5568 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569{
5570 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005571 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005573 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005575 u32 opts[2], len;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005576 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005577
Heiner Kallweit76085c92018-11-22 22:03:08 +01005578 if (unlikely(!rtl_tx_slots_avail(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005579 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005580 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 }
5582
5583 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005584 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
Heiner Kallweit355f9482019-06-06 07:49:17 +02005586 opts[1] = rtl8169_tx_vlan_tag(skb);
françois romieub423e9a2013-05-18 01:24:46 +00005587 opts[0] = DescOwn;
5588
Heiner Kallweit87945b62019-05-31 19:55:11 +02005589 if (rtl_chip_supports_csum_v2(tp)) {
5590 if (!rtl8169_tso_csum_v2(tp, skb, opts)) {
5591 r8169_csum_workaround(tp, skb);
5592 return NETDEV_TX_OK;
5593 }
5594 } else {
5595 rtl8169_tso_csum_v1(skb, opts);
hayeswange9746042014-07-11 16:25:58 +08005596 }
françois romieub423e9a2013-05-18 01:24:46 +00005597
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005598 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005599 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005600 if (unlikely(dma_mapping_error(d, mapping))) {
5601 if (net_ratelimit())
5602 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005603 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
5606 tp->tx_skb[entry].len = len;
5607 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
Francois Romieu2b7b4312011-04-18 22:53:24 -07005609 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005610 if (frags < 0)
5611 goto err_dma_1;
5612 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005613 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005614 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005615 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005616 tp->tx_skb[entry].skb = skb;
5617 }
5618
Francois Romieu2b7b4312011-04-18 22:53:24 -07005619 txd->opts2 = cpu_to_le32(opts[1]);
5620
Heiner Kallweit0255d592019-02-10 15:28:04 +01005621 netdev_sent_queue(dev, skb->len);
5622
Richard Cochran5047fb52012-03-10 07:29:42 +00005623 skb_tx_timestamp(skb);
5624
Alexander Duycka0750132014-12-11 15:02:17 -08005625 /* Force memory writes to complete before releasing descriptor */
5626 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Heiner Kallweit734c1402018-11-22 21:56:48 +01005628 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
Alexander Duycka0750132014-12-11 15:02:17 -08005630 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00005631 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
Alexander Duycka0750132014-12-11 15:02:17 -08005633 tp->cur_tx += frags + 1;
5634
Heiner Kallweit0255d592019-02-10 15:28:04 +01005635 RTL_W8(tp, TxPoll, NPQ);
5636
Heiner Kallweit0255d592019-02-10 15:28:04 +01005637 if (!rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
5638 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5639 * not miss a ring update when it notices a stopped queue.
5640 */
5641 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01005643 /* Sync with rtl_tx:
5644 * - publish queue status and cur_tx ring index (write barrier)
5645 * - refresh dirty_tx ring index (read barrier).
5646 * May the current thread have a pessimistic view of the ring
5647 * status and forget to wake up queue, a racing rtl_tx thread
5648 * can't.
5649 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005650 smp_mb();
Heiner Kallweit76085c92018-11-22 22:03:08 +01005651 if (rtl_tx_slots_avail(tp, MAX_SKB_FRAGS))
Heiner Kallweit601ed4d2019-03-21 21:41:48 +01005652 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 }
5654
Stephen Hemminger613573252009-08-31 19:50:58 +00005655 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005657err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005658 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005659err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07005660 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005661 dev->stats.tx_dropped++;
5662 return NETDEV_TX_OK;
5663
5664err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005666 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005667 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668}
5669
5670static void rtl8169_pcierr_interrupt(struct net_device *dev)
5671{
5672 struct rtl8169_private *tp = netdev_priv(dev);
5673 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 u16 pci_status, pci_cmd;
5675
5676 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5677 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5678
Joe Perchesbf82c182010-02-09 11:49:50 +00005679 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5680 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
5682 /*
5683 * The recovery sequence below admits a very elaborated explanation:
5684 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005685 * - I did not see what else could be done;
5686 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 *
5688 * Feel free to adjust to your needs.
5689 */
Francois Romieua27993f2006-12-18 00:04:19 +01005690 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005691 pci_cmd &= ~PCI_COMMAND_PARITY;
5692 else
5693 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5694
5695 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 pci_write_config_word(pdev, PCI_STATUS,
5698 pci_status & (PCI_STATUS_DETECTED_PARITY |
5699 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5700 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5701
Francois Romieu98ddf982012-01-31 10:47:34 +01005702 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703}
5704
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005705static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
5706 int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707{
Florian Westphald92060b2018-10-20 12:25:27 +02005708 unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 dirty_tx = tp->dirty_tx;
5711 smp_rmb();
5712 tx_left = tp->cur_tx - dirty_tx;
5713
5714 while (tx_left > 0) {
5715 unsigned int entry = dirty_tx % NUM_TX_DESC;
5716 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 u32 status;
5718
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5720 if (status & DescOwn)
5721 break;
5722
Alexander Duycka0750132014-12-11 15:02:17 -08005723 /* This barrier is needed to keep us from reading
5724 * any other fields out of the Tx descriptor until
5725 * we know the status of DescOwn
5726 */
5727 dma_rmb();
5728
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005729 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005730 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 if (status & LastFrag) {
Florian Westphald92060b2018-10-20 12:25:27 +02005732 pkts_compl++;
5733 bytes_compl += tx_skb->skb->len;
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005734 napi_consume_skb(tx_skb->skb, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 tx_skb->skb = NULL;
5736 }
5737 dirty_tx++;
5738 tx_left--;
5739 }
5740
5741 if (tp->dirty_tx != dirty_tx) {
Florian Westphald92060b2018-10-20 12:25:27 +02005742 netdev_completed_queue(dev, pkts_compl, bytes_compl);
5743
5744 u64_stats_update_begin(&tp->tx_stats.syncp);
5745 tp->tx_stats.packets += pkts_compl;
5746 tp->tx_stats.bytes += bytes_compl;
5747 u64_stats_update_end(&tp->tx_stats.syncp);
5748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005750 /* Sync with rtl8169_start_xmit:
5751 * - publish dirty_tx ring index (write barrier)
5752 * - refresh cur_tx ring index and queue status (read barrier)
5753 * May the current thread miss the stopped queue condition,
5754 * a racing xmit thread can only have a right view of the
5755 * ring status.
5756 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005757 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 if (netif_queue_stopped(dev) &&
Heiner Kallweit76085c92018-11-22 22:03:08 +01005759 rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 netif_wake_queue(dev);
5761 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005762 /*
5763 * 8168 hack: TxPoll requests are lost when the Tx packets are
5764 * too close. Let's kick an extra TxPoll request when a burst
5765 * of start_xmit activity is detected (if it is not detected,
5766 * it is slow enough). -- FR
5767 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005768 if (tp->cur_tx != dirty_tx)
5769 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 }
5771}
5772
Francois Romieu126fa4b2005-05-12 20:09:17 -04005773static inline int rtl8169_fragmented_frame(u32 status)
5774{
5775 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5776}
5777
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005778static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 u32 status = opts1 & RxProtoMask;
5781
5782 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005783 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 skb->ip_summed = CHECKSUM_UNNECESSARY;
5785 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005786 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787}
5788
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005789static struct sk_buff *rtl8169_try_rx_copy(void *data,
5790 struct rtl8169_private *tp,
5791 int pkt_size,
5792 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005794 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005795 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005797 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005798 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08005799 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005800 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02005801 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005802 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5803
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005804 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805}
5806
Francois Romieuda78dbf2012-01-26 14:18:23 +01005807static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808{
5809 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005810 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
Timo Teräs9fba0812013-01-15 21:01:24 +00005814 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005816 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 u32 status;
5818
Heiner Kallweit62028062018-04-17 23:30:29 +02005819 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 if (status & DescOwn)
5821 break;
Alexander Duycka0750132014-12-11 15:02:17 -08005822
5823 /* This barrier is needed to keep us from reading
5824 * any other fields out of the Rx descriptor until
5825 * we know the status of DescOwn
5826 */
5827 dma_rmb();
5828
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005829 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005830 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5831 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005832 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005834 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005836 dev->stats.rx_crc_errors++;
Heiner Kallweite9588eb2019-05-25 21:14:39 +02005837 if (status & (RxRUNT | RxCRC) && !(status & RxRWT) &&
5838 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00005839 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02005840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005842 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005843 dma_addr_t addr;
5844 int pkt_size;
5845
5846process_pkt:
5847 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005848 if (likely(!(dev->features & NETIF_F_RXFCS)))
5849 pkt_size = (status & 0x00003fff) - 4;
5850 else
5851 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
Francois Romieu126fa4b2005-05-12 20:09:17 -04005853 /*
5854 * The driver does not support incoming fragmented
5855 * frames. They are seen as a symptom of over-mtu
5856 * sized frames.
5857 */
5858 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005859 dev->stats.rx_dropped++;
5860 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00005861 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005862 }
5863
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005864 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5865 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005866 if (!skb) {
5867 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00005868 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 }
5870
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005871 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 skb_put(skb, pkt_size);
5873 skb->protocol = eth_type_trans(skb, dev);
5874
Francois Romieu7a8fc772011-03-01 17:18:33 +01005875 rtl8169_rx_vlan_tag(desc, skb);
5876
françois romieu39174292015-11-11 23:35:18 +01005877 if (skb->pkt_type == PACKET_MULTICAST)
5878 dev->stats.multicast++;
5879
Heiner Kallweit448a2412019-04-03 19:54:12 +02005880 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881
Junchang Wang8027aa22012-03-04 23:30:32 +01005882 u64_stats_update_begin(&tp->rx_stats.syncp);
5883 tp->rx_stats.packets++;
5884 tp->rx_stats.bytes += pkt_size;
5885 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 }
françois romieuce11ff52013-01-24 13:30:06 +00005887release_descriptor:
5888 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005889 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 }
5891
5892 count = cur_rx - tp->cur_rx;
5893 tp->cur_rx = cur_rx;
5894
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 return count;
5896}
5897
Francois Romieu07d3f512007-02-21 22:40:46 +01005898static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02005900 struct rtl8169_private *tp = dev_instance;
Heiner Kallweit3ff752c2019-01-19 22:02:40 +01005901 u16 status = RTL_R16(tp, IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
Heiner Kallweitc8248c62019-03-21 21:23:14 +01005903 if (!tp->irq_enabled || status == 0xffff || !(status & tp->irq_mask))
Heiner Kallweit05bbe552018-08-10 22:38:29 +02005904 return IRQ_NONE;
françois romieu811fd302011-12-04 20:30:45 +00005905
Heiner Kallweit38caff52018-10-18 22:19:28 +02005906 if (unlikely(status & SYSErr)) {
5907 rtl8169_pcierr_interrupt(tp->dev);
5908 goto out;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005909 }
5910
Heiner Kallweit703732f2019-01-19 22:07:05 +01005911 if (status & LinkChg)
5912 phy_mac_interrupt(tp->phydev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005913
Heiner Kallweit38caff52018-10-18 22:19:28 +02005914 if (unlikely(status & RxFIFOOver &&
5915 tp->mac_version == RTL_GIGA_MAC_VER_11)) {
5916 netif_stop_queue(tp->dev);
5917 /* XXX - Hack alert. See rtl_task(). */
5918 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
5919 }
5920
Heiner Kallweite62b2fd2019-04-14 11:48:39 +02005921 rtl_irq_disable(tp);
5922 napi_schedule_irqoff(&tp->napi);
Heiner Kallweit38caff52018-10-18 22:19:28 +02005923out:
5924 rtl_ack_events(tp, status);
5925
5926 return IRQ_HANDLED;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005927}
5928
Francois Romieu4422bcd2012-01-26 11:23:32 +01005929static void rtl_task(struct work_struct *work)
5930{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005931 static const struct {
5932 int bitnr;
5933 void (*action)(struct rtl8169_private *);
5934 } rtl_work[] = {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005935 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01005936 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01005937 struct rtl8169_private *tp =
5938 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005939 struct net_device *dev = tp->dev;
5940 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01005941
Francois Romieuda78dbf2012-01-26 14:18:23 +01005942 rtl_lock_work(tp);
5943
Francois Romieu6c4a70c2012-01-31 10:56:44 +01005944 if (!netif_running(dev) ||
5945 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01005946 goto out_unlock;
5947
5948 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
5949 bool pending;
5950
Francois Romieuda78dbf2012-01-26 14:18:23 +01005951 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005952 if (pending)
5953 rtl_work[i].action(tp);
5954 }
5955
5956out_unlock:
5957 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01005958}
5959
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005960static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005962 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5963 struct net_device *dev = tp->dev;
Heiner Kallweit6b839b62018-10-18 19:56:01 +02005964 int work_done;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005965
Heiner Kallweit6b839b62018-10-18 19:56:01 +02005966 work_done = rtl_rx(dev, tp, (u32) budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005967
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005968 rtl_tx(dev, tp, budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005969
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005970 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08005971 napi_complete_done(napi, work_done);
Heiner Kallweitfe716f82018-11-19 22:31:32 +01005972 rtl_irq_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 }
5974
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005975 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005978static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02005979{
5980 struct rtl8169_private *tp = netdev_priv(dev);
5981
5982 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5983 return;
5984
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005985 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
5986 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02005987}
5988
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02005989static void r8169_phylink_handler(struct net_device *ndev)
5990{
5991 struct rtl8169_private *tp = netdev_priv(ndev);
5992
5993 if (netif_carrier_ok(ndev)) {
5994 rtl_link_chg_patch(tp);
5995 pm_request_resume(&tp->pci_dev->dev);
5996 } else {
5997 pm_runtime_idle(&tp->pci_dev->dev);
5998 }
5999
6000 if (net_ratelimit())
Heiner Kallweit703732f2019-01-19 22:07:05 +01006001 phy_print_status(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006002}
6003
6004static int r8169_phy_connect(struct rtl8169_private *tp)
6005{
Heiner Kallweit703732f2019-01-19 22:07:05 +01006006 struct phy_device *phydev = tp->phydev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006007 phy_interface_t phy_mode;
6008 int ret;
6009
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02006010 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006011 PHY_INTERFACE_MODE_MII;
6012
6013 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6014 phy_mode);
6015 if (ret)
6016 return ret;
6017
Heiner Kallweita6851c62019-05-28 18:43:46 +02006018 if (tp->supports_gmii)
6019 phy_remove_link_mode(phydev,
6020 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
6021 else
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006022 phy_set_max_speed(phydev, SPEED_100);
6023
Heiner Kallweit9cf9b842019-05-04 12:01:03 +02006024 phy_support_asym_pause(phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006025
6026 phy_attached_info(phydev);
6027
6028 return 0;
6029}
6030
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031static void rtl8169_down(struct net_device *dev)
6032{
6033 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Heiner Kallweit703732f2019-01-19 22:07:05 +01006035 phy_stop(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006036
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006037 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006038 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
Hayes Wang92fc43b2011-07-06 15:58:03 +08006040 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006041 /*
6042 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006043 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6044 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006045 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006046 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenney16f11502018-11-05 17:07:39 -08006049 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 rtl8169_tx_clear(tp);
6052
6053 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006054
6055 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056}
6057
6058static int rtl8169_close(struct net_device *dev)
6059{
6060 struct rtl8169_private *tp = netdev_priv(dev);
6061 struct pci_dev *pdev = tp->pci_dev;
6062
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006063 pm_runtime_get_sync(&pdev->dev);
6064
Francois Romieucecb5fd2011-04-01 10:21:07 +02006065 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006066 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006067
Francois Romieuda78dbf2012-01-26 14:18:23 +01006068 rtl_lock_work(tp);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006069 /* Clear all task flags */
6070 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006071
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006073 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074
Lekensteyn4ea72442013-07-22 09:53:30 +02006075 cancel_work_sync(&tp->wk.work);
6076
Heiner Kallweit703732f2019-01-19 22:07:05 +01006077 phy_disconnect(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006078
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006079 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006081 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6082 tp->RxPhyAddr);
6083 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6084 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 tp->TxDescArray = NULL;
6086 tp->RxDescArray = NULL;
6087
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006088 pm_runtime_put_sync(&pdev->dev);
6089
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 return 0;
6091}
6092
Francois Romieudc1c00c2012-03-08 10:06:18 +01006093#ifdef CONFIG_NET_POLL_CONTROLLER
6094static void rtl8169_netpoll(struct net_device *dev)
6095{
6096 struct rtl8169_private *tp = netdev_priv(dev);
6097
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03006098 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01006099}
6100#endif
6101
Francois Romieudf43ac72012-03-08 09:48:40 +01006102static int rtl_open(struct net_device *dev)
6103{
6104 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006105 struct pci_dev *pdev = tp->pci_dev;
6106 int retval = -ENOMEM;
6107
6108 pm_runtime_get_sync(&pdev->dev);
6109
6110 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006111 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006112 * dma_alloc_coherent provides more.
6113 */
6114 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6115 &tp->TxPhyAddr, GFP_KERNEL);
6116 if (!tp->TxDescArray)
6117 goto err_pm_runtime_put;
6118
6119 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6120 &tp->RxPhyAddr, GFP_KERNEL);
6121 if (!tp->RxDescArray)
6122 goto err_free_tx_0;
6123
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006124 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006125 if (retval < 0)
6126 goto err_free_rx_1;
6127
Francois Romieudf43ac72012-03-08 09:48:40 +01006128 rtl_request_firmware(tp);
6129
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006130 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006131 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01006132 if (retval < 0)
6133 goto err_release_fw_2;
6134
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006135 retval = r8169_phy_connect(tp);
6136 if (retval)
6137 goto err_free_irq;
6138
Francois Romieudf43ac72012-03-08 09:48:40 +01006139 rtl_lock_work(tp);
6140
6141 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6142
6143 napi_enable(&tp->napi);
6144
6145 rtl8169_init_phy(dev, tp);
6146
Francois Romieudf43ac72012-03-08 09:48:40 +01006147 rtl_pll_power_up(tp);
6148
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006149 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006150
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006151 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006152 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6153
Heiner Kallweit703732f2019-01-19 22:07:05 +01006154 phy_start(tp->phydev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006155 netif_start_queue(dev);
6156
6157 rtl_unlock_work(tp);
6158
Heiner Kallweita92a0842018-01-08 21:39:13 +01006159 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006160out:
6161 return retval;
6162
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006163err_free_irq:
6164 pci_free_irq(pdev, 0, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006165err_release_fw_2:
6166 rtl_release_firmware(tp);
6167 rtl8169_rx_clear(tp);
6168err_free_rx_1:
6169 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6170 tp->RxPhyAddr);
6171 tp->RxDescArray = NULL;
6172err_free_tx_0:
6173 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6174 tp->TxPhyAddr);
6175 tp->TxDescArray = NULL;
6176err_pm_runtime_put:
6177 pm_runtime_put_noidle(&pdev->dev);
6178 goto out;
6179}
6180
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006181static void
Junchang Wang8027aa22012-03-04 23:30:32 +01006182rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183{
6184 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006185 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02006186 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01006187 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006189 pm_runtime_get_noresume(&pdev->dev);
6190
6191 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006192 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006193
Junchang Wang8027aa22012-03-04 23:30:32 +01006194 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006195 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006196 stats->rx_packets = tp->rx_stats.packets;
6197 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006198 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006199
Junchang Wang8027aa22012-03-04 23:30:32 +01006200 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006201 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006202 stats->tx_packets = tp->tx_stats.packets;
6203 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006204 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006205
6206 stats->rx_dropped = dev->stats.rx_dropped;
6207 stats->tx_dropped = dev->stats.tx_dropped;
6208 stats->rx_length_errors = dev->stats.rx_length_errors;
6209 stats->rx_errors = dev->stats.rx_errors;
6210 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6211 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6212 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02006213 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01006214
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006215 /*
6216 * Fetch additonal counter values missing in stats collected by driver
6217 * from tally counters.
6218 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006219 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006220 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006221
6222 /*
6223 * Subtract values fetched during initalization.
6224 * See rtl8169_init_counter_offsets for a description why we do that.
6225 */
Corinna Vinschen42020322015-09-10 10:47:35 +02006226 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006227 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02006228 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006229 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02006230 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006231 le16_to_cpu(tp->tc_offset.tx_aborted);
6232
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006233 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234}
6235
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006236static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006237{
françois romieu065c27c2011-01-03 15:08:12 +00006238 struct rtl8169_private *tp = netdev_priv(dev);
6239
Francois Romieu5d06a992006-02-23 00:47:58 +01006240 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006241 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006242
Heiner Kallweit703732f2019-01-19 22:07:05 +01006243 phy_stop(tp->phydev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006244 netif_device_detach(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006245
6246 rtl_lock_work(tp);
6247 napi_disable(&tp->napi);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006248 /* Clear all task flags */
6249 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6250
Francois Romieuda78dbf2012-01-26 14:18:23 +01006251 rtl_unlock_work(tp);
6252
6253 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006254}
Francois Romieu5d06a992006-02-23 00:47:58 +01006255
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006256#ifdef CONFIG_PM
6257
6258static int rtl8169_suspend(struct device *device)
6259{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006260 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006261 struct rtl8169_private *tp = netdev_priv(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006262
6263 rtl8169_net_suspend(dev);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006264 clk_disable_unprepare(tp->clk);
Francois Romieu1371fa62007-04-02 23:01:11 +02006265
Francois Romieu5d06a992006-02-23 00:47:58 +01006266 return 0;
6267}
6268
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006269static void __rtl8169_resume(struct net_device *dev)
6270{
françois romieu065c27c2011-01-03 15:08:12 +00006271 struct rtl8169_private *tp = netdev_priv(dev);
6272
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006273 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006274
6275 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02006276 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00006277
Heiner Kallweit703732f2019-01-19 22:07:05 +01006278 phy_start(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006279
Artem Savkovcff4c162012-04-03 10:29:11 +00006280 rtl_lock_work(tp);
6281 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006282 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Heiner Kallweit58ba5662019-01-19 22:06:25 +01006283 rtl_reset_work(tp);
Artem Savkovcff4c162012-04-03 10:29:11 +00006284 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006285}
6286
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006287static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006288{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006289 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006290 struct rtl8169_private *tp = netdev_priv(dev);
6291
Heiner Kallweit59715172019-05-29 07:44:01 +02006292 rtl_rar_set(tp, dev->dev_addr);
6293
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006294 clk_prepare_enable(tp->clk);
Francois Romieu5d06a992006-02-23 00:47:58 +01006295
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006296 if (netif_running(dev))
6297 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006298
Francois Romieu5d06a992006-02-23 00:47:58 +01006299 return 0;
6300}
6301
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006302static int rtl8169_runtime_suspend(struct device *device)
6303{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006304 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006305 struct rtl8169_private *tp = netdev_priv(dev);
6306
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006307 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006308 return 0;
6309
Francois Romieuda78dbf2012-01-26 14:18:23 +01006310 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006311 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006312 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006313
6314 rtl8169_net_suspend(dev);
6315
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006316 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006317 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006318 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006319
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006320 return 0;
6321}
6322
6323static int rtl8169_runtime_resume(struct device *device)
6324{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006325 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006326 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit59715172019-05-29 07:44:01 +02006327
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08006328 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006329
6330 if (!tp->TxDescArray)
6331 return 0;
6332
Francois Romieuda78dbf2012-01-26 14:18:23 +01006333 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006334 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006335 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006336
6337 __rtl8169_resume(dev);
6338
6339 return 0;
6340}
6341
6342static int rtl8169_runtime_idle(struct device *device)
6343{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006344 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006345
Heiner Kallweita92a0842018-01-08 21:39:13 +01006346 if (!netif_running(dev) || !netif_carrier_ok(dev))
6347 pm_schedule_suspend(device, 10000);
6348
6349 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006350}
6351
Alexey Dobriyan47145212009-12-14 18:00:08 -08006352static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006353 .suspend = rtl8169_suspend,
6354 .resume = rtl8169_resume,
6355 .freeze = rtl8169_suspend,
6356 .thaw = rtl8169_resume,
6357 .poweroff = rtl8169_suspend,
6358 .restore = rtl8169_resume,
6359 .runtime_suspend = rtl8169_runtime_suspend,
6360 .runtime_resume = rtl8169_runtime_resume,
6361 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006362};
6363
6364#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6365
6366#else /* !CONFIG_PM */
6367
6368#define RTL8169_PM_OPS NULL
6369
6370#endif /* !CONFIG_PM */
6371
David S. Miller1805b2f2011-10-24 18:18:09 -04006372static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6373{
David S. Miller1805b2f2011-10-24 18:18:09 -04006374 /* WoL fails with 8168b when the receiver is disabled. */
6375 switch (tp->mac_version) {
6376 case RTL_GIGA_MAC_VER_11:
6377 case RTL_GIGA_MAC_VER_12:
6378 case RTL_GIGA_MAC_VER_17:
6379 pci_clear_master(tp->pci_dev);
6380
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006381 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006382 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006383 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04006384 break;
6385 default:
6386 break;
6387 }
6388}
6389
Francois Romieu1765f952008-09-13 17:21:40 +02006390static void rtl_shutdown(struct pci_dev *pdev)
6391{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006392 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006393 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006394
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006395 rtl8169_net_suspend(dev);
6396
Francois Romieucecb5fd2011-04-01 10:21:07 +02006397 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006398 rtl_rar_set(tp, dev->perm_addr);
6399
Hayes Wang92fc43b2011-07-06 15:58:03 +08006400 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006401
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006402 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02006403 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006404 rtl_wol_suspend_quirk(tp);
6405 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006406 }
6407
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006408 pci_wake_from_d3(pdev, true);
6409 pci_set_power_state(pdev, PCI_D3hot);
6410 }
6411}
Francois Romieu5d06a992006-02-23 00:47:58 +01006412
Bill Pembertonbaf63292012-12-03 09:23:28 -05006413static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006414{
6415 struct net_device *dev = pci_get_drvdata(pdev);
6416 struct rtl8169_private *tp = netdev_priv(dev);
6417
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006418 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01006419 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01006420
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006421 netif_napi_del(&tp->napi);
6422
Francois Romieue27566e2012-03-08 09:54:01 +01006423 unregister_netdev(dev);
Heiner Kallweit703732f2019-01-19 22:07:05 +01006424 mdiobus_unregister(tp->phydev->mdio.bus);
Francois Romieue27566e2012-03-08 09:54:01 +01006425
6426 rtl_release_firmware(tp);
6427
6428 if (pci_dev_run_wake(pdev))
6429 pm_runtime_get_noresume(&pdev->dev);
6430
6431 /* restore original MAC address */
6432 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01006433}
6434
Francois Romieufa9c3852012-03-08 10:01:50 +01006435static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006436 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006437 .ndo_stop = rtl8169_close,
6438 .ndo_get_stats64 = rtl8169_get_stats64,
6439 .ndo_start_xmit = rtl8169_start_xmit,
6440 .ndo_tx_timeout = rtl8169_tx_timeout,
6441 .ndo_validate_addr = eth_validate_addr,
6442 .ndo_change_mtu = rtl8169_change_mtu,
6443 .ndo_fix_features = rtl8169_fix_features,
6444 .ndo_set_features = rtl8169_set_features,
6445 .ndo_set_mac_address = rtl_set_mac_address,
6446 .ndo_do_ioctl = rtl8169_ioctl,
6447 .ndo_set_rx_mode = rtl_set_rx_mode,
6448#ifdef CONFIG_NET_POLL_CONTROLLER
6449 .ndo_poll_controller = rtl8169_netpoll,
6450#endif
6451
6452};
6453
Heiner Kallweitec9a4082019-06-10 18:21:50 +02006454static void rtl_set_irq_mask(struct rtl8169_private *tp)
6455{
6456 tp->irq_mask = RTL_EVENT_NAPI | LinkChg;
6457
6458 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6459 tp->irq_mask |= SYSErr | RxOverflow | RxFIFOOver;
6460 else if (tp->mac_version == RTL_GIGA_MAC_VER_11)
6461 /* special workaround needed */
6462 tp->irq_mask |= RxFIFOOver;
6463 else
6464 tp->irq_mask |= RxOverflow;
6465}
6466
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006467static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01006468{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006469 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006470
Jian-Hong Pand49c88d2018-09-27 12:09:48 +08006471 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006472 rtl_unlock_config_regs(tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006473 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006474 rtl_lock_config_regs(tp);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006475 flags = PCI_IRQ_LEGACY;
Jian-Hong Pand49c88d2018-09-27 12:09:48 +08006476 } else {
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006477 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006478 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006479
6480 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01006481}
6482
Thierry Reding04c77882019-02-06 13:30:17 +01006483static void rtl_read_mac_address(struct rtl8169_private *tp,
6484 u8 mac_addr[ETH_ALEN])
6485{
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006486 u32 value;
6487
Thierry Reding04c77882019-02-06 13:30:17 +01006488 /* Get MAC address */
6489 switch (tp->mac_version) {
6490 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
6491 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02006492 value = rtl_eri_read(tp, 0xe0);
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006493 mac_addr[0] = (value >> 0) & 0xff;
6494 mac_addr[1] = (value >> 8) & 0xff;
6495 mac_addr[2] = (value >> 16) & 0xff;
6496 mac_addr[3] = (value >> 24) & 0xff;
6497
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02006498 value = rtl_eri_read(tp, 0xe4);
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006499 mac_addr[4] = (value >> 0) & 0xff;
6500 mac_addr[5] = (value >> 8) & 0xff;
Thierry Reding04c77882019-02-06 13:30:17 +01006501 break;
6502 default:
6503 break;
6504 }
6505}
6506
Hayes Wangc5583862012-07-02 17:23:22 +08006507DECLARE_RTL_COND(rtl_link_list_ready_cond)
6508{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006509 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08006510}
6511
6512DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6513{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006514 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08006515}
6516
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006517static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
6518{
6519 struct rtl8169_private *tp = mii_bus->priv;
6520
6521 if (phyaddr > 0)
6522 return -ENODEV;
6523
6524 return rtl_readphy(tp, phyreg);
6525}
6526
6527static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
6528 int phyreg, u16 val)
6529{
6530 struct rtl8169_private *tp = mii_bus->priv;
6531
6532 if (phyaddr > 0)
6533 return -ENODEV;
6534
6535 rtl_writephy(tp, phyreg, val);
6536
6537 return 0;
6538}
6539
6540static int r8169_mdio_register(struct rtl8169_private *tp)
6541{
6542 struct pci_dev *pdev = tp->pci_dev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006543 struct mii_bus *new_bus;
6544 int ret;
6545
6546 new_bus = devm_mdiobus_alloc(&pdev->dev);
6547 if (!new_bus)
6548 return -ENOMEM;
6549
6550 new_bus->name = "r8169";
6551 new_bus->priv = tp;
6552 new_bus->parent = &pdev->dev;
6553 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
Heiner Kallweita1950162019-04-24 21:13:25 +02006554 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x", pci_dev_id(pdev));
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006555
6556 new_bus->read = r8169_mdio_read_reg;
6557 new_bus->write = r8169_mdio_write_reg;
6558
6559 ret = mdiobus_register(new_bus);
6560 if (ret)
6561 return ret;
6562
Heiner Kallweit703732f2019-01-19 22:07:05 +01006563 tp->phydev = mdiobus_get_phy(new_bus, 0);
6564 if (!tp->phydev) {
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006565 mdiobus_unregister(new_bus);
6566 return -ENODEV;
6567 }
6568
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02006569 /* PHY will be woken up in rtl_open() */
Heiner Kallweit703732f2019-01-19 22:07:05 +01006570 phy_suspend(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006571
6572 return 0;
6573}
6574
Bill Pembertonbaf63292012-12-03 09:23:28 -05006575static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006576{
Hayes Wangc5583862012-07-02 17:23:22 +08006577 u32 data;
6578
6579 tp->ocp_base = OCP_STD_PHY_BASE;
6580
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006581 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006582
6583 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6584 return;
6585
6586 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6587 return;
6588
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006589 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08006590 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006591 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08006592
Hayes Wang5f8bcce2012-07-10 08:47:05 +02006593 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08006594 data &= ~(1 << 14);
6595 r8168_mac_ocp_write(tp, 0xe8de, data);
6596
6597 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6598 return;
6599
Hayes Wang5f8bcce2012-07-10 08:47:05 +02006600 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08006601 data |= (1 << 15);
6602 r8168_mac_ocp_write(tp, 0xe8de, data);
6603
Heiner Kallweit7160be22019-05-25 20:44:01 +02006604 rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
Hayes Wangc5583862012-07-02 17:23:22 +08006605}
6606
Bill Pembertonbaf63292012-12-03 09:23:28 -05006607static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006608{
6609 switch (tp->mac_version) {
Heiner Kallweit29ec0482019-05-25 20:43:25 +02006610 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
6611 rtl8168ep_stop_cmac(tp);
6612 /* fall through */
Heiner Kallweit2a718832018-05-02 21:39:49 +02006613 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006614 rtl_hw_init_8168g(tp);
6615 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006616 default:
6617 break;
6618 }
6619}
6620
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006621static int rtl_jumbo_max(struct rtl8169_private *tp)
6622{
6623 /* Non-GBit versions don't support jumbo frames */
6624 if (!tp->supports_gmii)
6625 return JUMBO_1K;
6626
6627 switch (tp->mac_version) {
6628 /* RTL8169 */
Heiner Kallweite9588eb2019-05-25 21:14:39 +02006629 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006630 return JUMBO_7K;
6631 /* RTL8168b */
6632 case RTL_GIGA_MAC_VER_11:
6633 case RTL_GIGA_MAC_VER_12:
6634 case RTL_GIGA_MAC_VER_17:
6635 return JUMBO_4K;
6636 /* RTL8168c */
6637 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
6638 return JUMBO_6K;
6639 default:
6640 return JUMBO_9K;
6641 }
6642}
6643
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006644static void rtl_disable_clk(void *data)
6645{
6646 clk_disable_unprepare(data);
6647}
6648
Heiner Kallweitb779dae2019-01-19 22:07:34 +01006649static int rtl_get_ether_clk(struct rtl8169_private *tp)
6650{
6651 struct device *d = tp_to_dev(tp);
6652 struct clk *clk;
6653 int rc;
6654
6655 clk = devm_clk_get(d, "ether_clk");
6656 if (IS_ERR(clk)) {
6657 rc = PTR_ERR(clk);
6658 if (rc == -ENOENT)
6659 /* clk-core allows NULL (for suspend / resume) */
6660 rc = 0;
6661 else if (rc != -EPROBE_DEFER)
6662 dev_err(d, "failed to get clk: %d\n", rc);
6663 } else {
6664 tp->clk = clk;
6665 rc = clk_prepare_enable(clk);
6666 if (rc)
6667 dev_err(d, "failed to enable clk: %d\n", rc);
6668 else
6669 rc = devm_add_action_or_reset(d, rtl_disable_clk, clk);
6670 }
6671
6672 return rc;
6673}
6674
hayeswang929a0312014-09-16 11:40:47 +08006675static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01006676{
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006677 /* align to u16 for is_valid_ether_addr() */
6678 u8 mac_addr[ETH_ALEN] __aligned(2) = {};
Francois Romieu3b6cf252012-03-08 09:59:04 +01006679 struct rtl8169_private *tp;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006680 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02006681 int chipset, region, i;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006682 int jumbo_max, rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006683
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006684 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
6685 if (!dev)
6686 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006687
6688 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006689 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006690 tp = netdev_priv(dev);
6691 tp->dev = dev;
6692 tp->pci_dev = pdev;
6693 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Heiner Kallweit145a40e2019-06-10 18:25:29 +02006694 tp->supports_gmii = ent->driver_data == RTL_CFG_NO_GBIT ? 0 : 1;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006695
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006696 /* Get the *optional* external "ether_clk" used on some boards */
Heiner Kallweitb779dae2019-01-19 22:07:34 +01006697 rc = rtl_get_ether_clk(tp);
6698 if (rc)
6699 return rc;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006700
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +02006701 /* Disable ASPM completely as that cause random device stop working
6702 * problems as well as full system hangs for some PCIe devices users.
6703 */
6704 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
6705
Francois Romieu3b6cf252012-03-08 09:59:04 +01006706 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006707 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006708 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006709 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006710 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006711 }
6712
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006713 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02006714 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01006715
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02006716 /* use first MMIO region */
6717 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
6718 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006719 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006720 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006721 }
6722
6723 /* check for weird/broken PCI region reporting */
6724 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006725 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006726 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006727 }
6728
Andy Shevchenko93a00d42018-03-01 13:27:35 +02006729 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006730 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006731 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006732 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006733 }
6734
Andy Shevchenko93a00d42018-03-01 13:27:35 +02006735 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01006736
Francois Romieu3b6cf252012-03-08 09:59:04 +01006737 /* Identify chip attached to board */
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01006738 rtl8169_get_mac_version(tp);
6739 if (tp->mac_version == RTL_GIGA_MAC_NONE)
6740 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006741
Heiner Kallweit0ae09742018-04-28 22:19:26 +02006742 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006743
Heiner Kallweit10b63e82019-01-20 11:45:20 +01006744 if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
Heiner Kallweita0456792018-09-25 07:59:36 +02006745 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006746 dev->features |= NETIF_F_HIGHDMA;
6747 } else {
6748 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6749 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006750 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006751 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006752 }
6753 }
6754
Francois Romieu3b6cf252012-03-08 09:59:04 +01006755 rtl_init_rxcfg(tp);
6756
Heiner Kallweitde20e122018-09-25 07:58:00 +02006757 rtl8169_irq_mask_and_ack(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006758
Hayes Wangc5583862012-07-02 17:23:22 +08006759 rtl_hw_initialize(tp);
6760
Francois Romieu3b6cf252012-03-08 09:59:04 +01006761 rtl_hw_reset(tp);
6762
Francois Romieu3b6cf252012-03-08 09:59:04 +01006763 pci_set_master(pdev);
6764
Francois Romieu3b6cf252012-03-08 09:59:04 +01006765 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006766
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006767 rc = rtl_alloc_irq(tp);
6768 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006769 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006770 return rc;
6771 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01006772
Francois Romieu3b6cf252012-03-08 09:59:04 +01006773 mutex_init(&tp->wk.mutex);
Heiner Kallweit5c41e782019-01-19 22:03:49 +01006774 INIT_WORK(&tp->wk.work, rtl_task);
Kyle McMartin340fea32014-02-24 20:12:28 -05006775 u64_stats_init(&tp->rx_stats.syncp);
6776 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006777
Thierry Reding04c77882019-02-06 13:30:17 +01006778 /* get MAC address */
6779 rc = eth_platform_get_mac_address(&pdev->dev, mac_addr);
6780 if (rc)
6781 rtl_read_mac_address(tp, mac_addr);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08006782
Thierry Reding04c77882019-02-06 13:30:17 +01006783 if (is_valid_ether_addr(mac_addr))
6784 rtl_rar_set(tp, mac_addr);
6785
Francois Romieu3b6cf252012-03-08 09:59:04 +01006786 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006787 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006788
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00006789 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006790
Heiner Kallweit37621492018-04-17 23:20:03 +02006791 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006792
6793 /* don't enable SG, IP_CSUM and TSO by default - it might not work
6794 * properly for all devices */
6795 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00006796 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006797
6798 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00006799 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6800 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006801 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6802 NETIF_F_HIGHDMA;
Heiner Kallweit2d0ec542018-07-02 22:49:35 +02006803 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006804
hayeswang929a0312014-09-16 11:40:47 +08006805 tp->cp_cmd |= RxChkSum | RxVlan;
6806
6807 /*
6808 * Pretend we are using VLANs; This bypasses a nasty bug where
6809 * Interrupts stop flowing on high load on 8110SCd controllers.
6810 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01006811 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08006812 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00006813 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006814
Heiner Kallweit87945b62019-05-31 19:55:11 +02006815 if (rtl_chip_supports_csum_v2(tp))
hayeswange9746042014-07-11 16:25:58 +08006816 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
hayeswang5888d3f2014-07-11 16:25:56 +08006817
Francois Romieu3b6cf252012-03-08 09:59:04 +01006818 dev->hw_features |= NETIF_F_RXALL;
6819 dev->hw_features |= NETIF_F_RXFCS;
6820
Jarod Wilsonc7315a92016-10-17 15:54:09 -04006821 /* MTU range: 60 - hw-specific max */
6822 dev->min_mtu = ETH_ZLEN;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006823 jumbo_max = rtl_jumbo_max(tp);
6824 dev->max_mtu = jumbo_max;
Jarod Wilsonc7315a92016-10-17 15:54:09 -04006825
Heiner Kallweitec9a4082019-06-10 18:21:50 +02006826 rtl_set_irq_mask(tp);
Heiner Kallweit9fa0a8e2019-06-10 18:24:25 +02006827
Heiner Kallweit254764e2019-01-22 22:23:41 +01006828 tp->fw_name = rtl_chip_infos[chipset].fw_name;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006829
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006830 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
6831 &tp->counters_phys_addr,
6832 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01006833 if (!tp->counters)
6834 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02006835
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02006836 pci_set_drvdata(pdev, dev);
6837
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006838 rc = r8169_mdio_register(tp);
6839 if (rc)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01006840 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006841
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006842 /* chip gets powered up in rtl_open() */
6843 rtl_pll_power_down(tp);
6844
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006845 rc = register_netdev(dev);
6846 if (rc)
6847 goto err_mdio_unregister;
6848
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01006849 netif_info(tp, probe, dev, "%s, %pM, XID %03x, IRQ %d\n",
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02006850 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01006851 (RTL_R32(tp, TxConfig) >> 20) & 0xfcf,
Heiner Kallweit29274992018-02-28 20:43:38 +01006852 pci_irq_vector(pdev, 0));
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006853
6854 if (jumbo_max > JUMBO_1K)
6855 netif_info(tp, probe, dev,
6856 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
6857 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
6858 "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01006859
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006860 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01006861 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006862
Heiner Kallweita92a0842018-01-08 21:39:13 +01006863 if (pci_dev_run_wake(pdev))
6864 pm_runtime_put_sync(&pdev->dev);
6865
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006866 return 0;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006867
6868err_mdio_unregister:
Heiner Kallweit703732f2019-01-19 22:07:05 +01006869 mdiobus_unregister(tp->phydev->mdio.bus);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006870 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006871}
6872
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873static struct pci_driver rtl8169_pci_driver = {
6874 .name = MODULENAME,
6875 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006876 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05006877 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02006878 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006879 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880};
6881
Devendra Naga3eeb7da2012-10-26 09:27:42 +00006882module_pci_driver(rtl8169_pci_driver);