blob: 4d02095e3ca3376d07ffadf64595528a47b4629d [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Francois Romieu07d3f512007-02-21 22:40:46 +01003 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4 *
5 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
6 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
7 * Copyright (c) a lot of people too. Please respect their work.
8 *
9 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/pci.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
Hans de Goedec2f6f3e2018-09-12 11:34:55 +020017#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/delay.h>
19#include <linux/ethtool.h>
Heiner Kallweitf1e911d2018-07-17 22:51:26 +020020#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/if_vlan.h>
22#include <linux/crc32.h>
23#include <linux/in.h>
Heiner Kallweit098b01a2018-08-10 22:37:31 +020024#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/ip.h>
26#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000027#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000029#include <linux/pm_runtime.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +020031#include <linux/pci-aspm.h>
hayeswange9746042014-07-11 16:25:58 +080032#include <linux/ipv6.h>
33#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Heiner Kallweit8197f9d2019-06-05 08:02:31 +020035#include "r8169_firmware.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080043#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080044#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
45#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080046#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080047#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080048#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080049#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080050#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000051#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000052#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000053#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080054#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
55#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
56#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
57#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000058
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070060 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
63 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Heiner Kallweit81cd17a2019-07-24 23:34:45 +020064#define MC_FILTER_LIMIT 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Michal Schmidtaee77e42012-09-09 13:55:26 +000066#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
68
69#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020070#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000072#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
74#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
75
Heiner Kallweit145a40e2019-06-10 18:25:29 +020076#define RTL_CFG_NO_GBIT 1
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +020079#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
80#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
81#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
82#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
83#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
84#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86enum mac_version {
Heiner Kallweite9588eb2019-05-25 21:14:39 +020087 /* support for ancient RTL_GIGA_MAC_VER_01 has been removed */
Francois Romieu85bffe62011-04-27 08:22:39 +020088 RTL_GIGA_MAC_VER_02,
89 RTL_GIGA_MAC_VER_03,
90 RTL_GIGA_MAC_VER_04,
91 RTL_GIGA_MAC_VER_05,
92 RTL_GIGA_MAC_VER_06,
93 RTL_GIGA_MAC_VER_07,
94 RTL_GIGA_MAC_VER_08,
95 RTL_GIGA_MAC_VER_09,
96 RTL_GIGA_MAC_VER_10,
97 RTL_GIGA_MAC_VER_11,
98 RTL_GIGA_MAC_VER_12,
99 RTL_GIGA_MAC_VER_13,
100 RTL_GIGA_MAC_VER_14,
101 RTL_GIGA_MAC_VER_15,
102 RTL_GIGA_MAC_VER_16,
103 RTL_GIGA_MAC_VER_17,
104 RTL_GIGA_MAC_VER_18,
105 RTL_GIGA_MAC_VER_19,
106 RTL_GIGA_MAC_VER_20,
107 RTL_GIGA_MAC_VER_21,
108 RTL_GIGA_MAC_VER_22,
109 RTL_GIGA_MAC_VER_23,
110 RTL_GIGA_MAC_VER_24,
111 RTL_GIGA_MAC_VER_25,
112 RTL_GIGA_MAC_VER_26,
113 RTL_GIGA_MAC_VER_27,
114 RTL_GIGA_MAC_VER_28,
115 RTL_GIGA_MAC_VER_29,
116 RTL_GIGA_MAC_VER_30,
117 RTL_GIGA_MAC_VER_31,
118 RTL_GIGA_MAC_VER_32,
119 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800120 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800121 RTL_GIGA_MAC_VER_35,
122 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800123 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800124 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800125 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800126 RTL_GIGA_MAC_VER_40,
127 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000128 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000129 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800130 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800131 RTL_GIGA_MAC_VER_45,
132 RTL_GIGA_MAC_VER_46,
133 RTL_GIGA_MAC_VER_47,
134 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800135 RTL_GIGA_MAC_VER_49,
136 RTL_GIGA_MAC_VER_50,
137 RTL_GIGA_MAC_VER_51,
Heiner Kallweit76719ee2019-05-25 20:45:04 +0200138 RTL_GIGA_MAC_NONE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Francois Romieud58d46b2011-05-03 16:38:29 +0200141#define JUMBO_1K ETH_DATA_LEN
142#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
143#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
144#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
145#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
146
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800147static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *name;
Francois Romieu85bffe62011-04-27 08:22:39 +0200149 const char *fw_name;
150} rtl_chip_infos[] = {
151 /* PCI devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200152 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
153 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
154 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
155 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
156 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
Francois Romieu85bffe62011-04-27 08:22:39 +0200157 /* PCI-E devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200158 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
159 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
Heiner Kallweit9e0773c2019-07-07 13:59:54 +0200160 [RTL_GIGA_MAC_VER_09] = {"RTL8102e/RTL8103e" },
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200161 [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
162 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
163 [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
164 [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
165 [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
166 [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
167 [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
168 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
169 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
170 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
171 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
172 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
173 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
174 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
175 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
176 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
177 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
178 [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
179 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
180 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
181 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
182 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
183 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
184 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
185 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
186 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
187 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
188 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
189 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
190 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
191 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
192 [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
Heiner Kallweit9e0773c2019-07-07 13:59:54 +0200193 [RTL_GIGA_MAC_VER_42] = {"RTL8168gu/8111gu", FIRMWARE_8168G_3},
194 [RTL_GIGA_MAC_VER_43] = {"RTL8106eus", FIRMWARE_8106E_2},
195 [RTL_GIGA_MAC_VER_44] = {"RTL8411b", FIRMWARE_8411_2 },
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200196 [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
197 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
198 [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
199 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
200 [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
201 [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
202 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Benoit Taine9baa3c32014-08-08 15:56:03 +0200205static const struct pci_device_id rtl8169_pci_tbl[] = {
Heiner Kallweit145a40e2019-06-10 18:25:29 +0200206 { PCI_VDEVICE(REALTEK, 0x2502) },
207 { PCI_VDEVICE(REALTEK, 0x2600) },
208 { PCI_VDEVICE(REALTEK, 0x8129) },
209 { PCI_VDEVICE(REALTEK, 0x8136), RTL_CFG_NO_GBIT },
210 { PCI_VDEVICE(REALTEK, 0x8161) },
211 { PCI_VDEVICE(REALTEK, 0x8167) },
212 { PCI_VDEVICE(REALTEK, 0x8168) },
213 { PCI_VDEVICE(NCUBE, 0x8168) },
214 { PCI_VDEVICE(REALTEK, 0x8169) },
Heiner Kallweit6f0d3082018-11-19 22:35:08 +0100215 { PCI_VENDOR_ID_DLINK, 0x4300,
Heiner Kallweit145a40e2019-06-10 18:25:29 +0200216 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0 },
Heiner Kallweit9d9f3fb2019-06-15 09:58:21 +0200217 { PCI_VDEVICE(DLINK, 0x4300) },
218 { PCI_VDEVICE(DLINK, 0x4302) },
219 { PCI_VDEVICE(AT, 0xc107) },
220 { PCI_VDEVICE(USR, 0x0116) },
221 { PCI_VENDOR_ID_LINKSYS, 0x1032, PCI_ANY_ID, 0x0024 },
222 { 0x0001, 0x8168, PCI_ANY_ID, 0x2410 },
Heiner Kallweit6f0d3082018-11-19 22:35:08 +0100223 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
226MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
227
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200228static struct {
229 u32 msg_enable;
230} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Francois Romieu07d3f512007-02-21 22:40:46 +0100232enum rtl_registers {
233 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100234 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100235 MAR0 = 8, /* Multicast filter. */
236 CounterAddrLow = 0x10,
237 CounterAddrHigh = 0x14,
238 TxDescStartAddrLow = 0x20,
239 TxDescStartAddrHigh = 0x24,
240 TxHDescStartAddrLow = 0x28,
241 TxHDescStartAddrHigh = 0x2c,
242 FLASH = 0x30,
243 ERSR = 0x36,
244 ChipCmd = 0x37,
245 TxPoll = 0x38,
246 IntrMask = 0x3c,
247 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700248
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800249 TxConfig = 0x40,
250#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
251#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
252
253 RxConfig = 0x44,
254#define RX128_INT_EN (1 << 15) /* 8111c and later */
255#define RX_MULTI_EN (1 << 14) /* 8111c only */
256#define RXCFG_FIFO_SHIFT 13
257 /* No threshold before first PCI xfer */
258#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000259#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800260#define RXCFG_DMA_SHIFT 8
261 /* Unlimited maximum PCI burst. */
262#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700263
Francois Romieu07d3f512007-02-21 22:40:46 +0100264 RxMissed = 0x4c,
265 Cfg9346 = 0x50,
266 Config0 = 0x51,
267 Config1 = 0x52,
268 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200269#define PME_SIGNAL (1 << 5) /* 8168c and later */
270
Francois Romieu07d3f512007-02-21 22:40:46 +0100271 Config3 = 0x54,
272 Config4 = 0x55,
273 Config5 = 0x56,
Francois Romieu07d3f512007-02-21 22:40:46 +0100274 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100275 PHYstatus = 0x6c,
276 RxMaxSize = 0xda,
277 CPlusCmd = 0xe0,
278 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300279
280#define RTL_COALESCE_MASK 0x0f
281#define RTL_COALESCE_SHIFT 4
282#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
283#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
284
Francois Romieu07d3f512007-02-21 22:40:46 +0100285 RxDescAddrLow = 0xe4,
286 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000287 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
288
289#define NoEarlyTx 0x3f /* Max value : no early transmit. */
290
291 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
292
293#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800294#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000295
Francois Romieu07d3f512007-02-21 22:40:46 +0100296 FuncEvent = 0xf0,
297 FuncEventMask = 0xf4,
298 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800299 IBCR0 = 0xf8,
300 IBCR2 = 0xf9,
301 IBIMR0 = 0xfa,
302 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100303 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Francois Romieuf162a5d2008-06-01 22:37:49 +0200306enum rtl8168_8101_registers {
307 CSIDR = 0x64,
308 CSIAR = 0x68,
309#define CSIAR_FLAG 0x80000000
310#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200311#define CSIAR_BYTE_ENABLE 0x0000f000
312#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000313 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200314 EPHYAR = 0x80,
315#define EPHYAR_FLAG 0x80000000
316#define EPHYAR_WRITE_CMD 0x80000000
317#define EPHYAR_REG_MASK 0x1f
318#define EPHYAR_REG_SHIFT 16
319#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800320 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800321#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800322#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200323 DBG_REG = 0xd1,
324#define FIX_NAK_1 (1 << 4)
325#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800326 TWSI = 0xd2,
327 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800328#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800329#define TX_EMPTY (1 << 5)
330#define RX_EMPTY (1 << 4)
331#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800332#define EN_NDP (1 << 3)
333#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800334#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000335 EFUSEAR = 0xdc,
336#define EFUSEAR_FLAG 0x80000000
337#define EFUSEAR_WRITE_CMD 0x80000000
338#define EFUSEAR_READ_CMD 0x00000000
339#define EFUSEAR_REG_MASK 0x03ff
340#define EFUSEAR_REG_SHIFT 8
341#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800342 MISC_1 = 0xf2,
343#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200344};
345
françois romieuc0e45c12011-01-03 15:08:04 +0000346enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800347 LED_FREQ = 0x1a,
348 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000349 ERIDR = 0x70,
350 ERIAR = 0x74,
351#define ERIAR_FLAG 0x80000000
352#define ERIAR_WRITE_CMD 0x80000000
353#define ERIAR_READ_CMD 0x00000000
354#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000355#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800356#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
357#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
358#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800359#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800360#define ERIAR_MASK_SHIFT 12
361#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
362#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800363#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800364#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800365#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000366 EPHY_RXER_NUM = 0x7c,
367 OCPDR = 0xb0, /* OCP GPHY access */
368#define OCPDR_WRITE_CMD 0x80000000
369#define OCPDR_READ_CMD 0x00000000
370#define OCPDR_REG_MASK 0x7f
371#define OCPDR_GPHY_REG_SHIFT 16
372#define OCPDR_DATA_MASK 0xffff
373 OCPAR = 0xb4,
374#define OCPAR_FLAG 0x80000000
375#define OCPAR_GPHY_WRITE_CMD 0x8000f060
376#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800377 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000378 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
379 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200380#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800381#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800382#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800383#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800384#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000385};
386
Francois Romieu07d3f512007-02-21 22:40:46 +0100387enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100389 SYSErr = 0x8000,
390 PCSTimeout = 0x4000,
391 SWInt = 0x0100,
392 TxDescUnavail = 0x0080,
393 RxFIFOOver = 0x0040,
394 LinkChg = 0x0020,
395 RxOverflow = 0x0010,
396 TxErr = 0x0008,
397 TxOK = 0x0004,
398 RxErr = 0x0002,
399 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200402 RxRWT = (1 << 22),
403 RxRES = (1 << 21),
404 RxRUNT = (1 << 20),
405 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800408 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100409 CmdReset = 0x10,
410 CmdRxEnb = 0x08,
411 CmdTxEnb = 0x04,
412 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Francois Romieu275391a2007-02-23 23:50:28 +0100414 /* TXPoll register p.5 */
415 HPQ = 0x80, /* Poll cmd on the high prio queue */
416 NPQ = 0x40, /* Poll cmd on the low prio queue */
417 FSWInt = 0x01, /* Forced software interrupt */
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100420 Cfg9346_Lock = 0x00,
421 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100424 AcceptErr = 0x20,
425 AcceptRunt = 0x10,
426 AcceptBroadcast = 0x08,
427 AcceptMulticast = 0x04,
428 AcceptMyPhys = 0x02,
429 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200430#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* TxConfigBits */
433 TxInterFrameGapShift = 24,
434 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
435
Francois Romieu5d06a992006-02-23 00:47:58 +0100436 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200437 LEDS1 = (1 << 7),
438 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200439 Speed_down = (1 << 4),
440 MEMMAP = (1 << 3),
441 IOMAP = (1 << 2),
442 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100443 PMEnable = (1 << 0), /* Power Management Enable */
444
Francois Romieu6dccd162007-02-13 23:38:05 +0100445 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000446 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000447 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100448 PCI_Clock_66MHz = 0x01,
449 PCI_Clock_33MHz = 0x00,
450
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100451 /* Config3 register p.25 */
452 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
453 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200454 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800455 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200456 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100457
Francois Romieud58d46b2011-05-03 16:38:29 +0200458 /* Config4 register */
459 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
460
Francois Romieu5d06a992006-02-23 00:47:58 +0100461 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100462 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
463 MWF = (1 << 5), /* Accept Multicast wakeup frame */
464 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200465 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100466 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100467 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000468 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200471 EnableBist = (1 << 15), // 8168 8101
472 Mac_dbgo_oe = (1 << 14), // 8168 8101
473 Normal_mode = (1 << 13), // unused
474 Force_half_dup = (1 << 12), // 8168 8101
475 Force_rxflow_en = (1 << 11), // 8168 8101
476 Force_txflow_en = (1 << 10), // 8168 8101
477 Cxpl_dbg_sel = (1 << 9), // 8168 8101
478 ASF = (1 << 8), // 8168 8101
479 PktCntrDisable = (1 << 7), // 8168 8101
480 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 RxVlan = (1 << 6),
482 RxChkSum = (1 << 5),
483 PCIDAC = (1 << 4),
484 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200485#define INTT_MASK GENMASK(1, 0)
Heiner Kallweitbc732412019-06-10 18:22:33 +0200486#define CPCMD_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100489 TBI_Enable = 0x80,
490 TxFlowCtrl = 0x40,
491 RxFlowCtrl = 0x20,
492 _1000bpsF = 0x10,
493 _100bps = 0x08,
494 _10bps = 0x04,
495 LinkStatus = 0x02,
496 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200498 /* ResetCounterCommand */
499 CounterReset = 0x1,
500
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200501 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100502 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800503
504 /* magic enable v2 */
505 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506};
507
Francois Romieu2b7b4312011-04-18 22:53:24 -0700508enum rtl_desc_bit {
509 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
511 RingEnd = (1 << 30), /* End of descriptor ring */
512 FirstFrag = (1 << 29), /* First segment of a packet */
513 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700514};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Francois Romieu2b7b4312011-04-18 22:53:24 -0700516/* Generic case. */
517enum rtl_tx_desc_bit {
518 /* First doubleword. */
519 TD_LSO = (1 << 27), /* Large Send Offload */
520#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Francois Romieu2b7b4312011-04-18 22:53:24 -0700522 /* Second doubleword. */
523 TxVlanTag = (1 << 17), /* Add VLAN tag */
524};
525
526/* 8169, 8168b and 810x except 8102e. */
527enum rtl_tx_desc_bit_0 {
528 /* First doubleword. */
529#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
530 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
531 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
532 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
533};
534
535/* 8102e, 8168c and beyond. */
536enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800537 /* First doubleword. */
538 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800539 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800540#define GTTCPHO_SHIFT 18
Heiner Kallweite64e0c82019-07-26 21:49:22 +0200541#define GTTCPHO_MAX 0x7f
hayeswangbdfa4ed2014-07-11 16:25:57 +0800542
Francois Romieu2b7b4312011-04-18 22:53:24 -0700543 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800544#define TCPHO_SHIFT 18
Heiner Kallweite64e0c82019-07-26 21:49:22 +0200545#define TCPHO_MAX 0x3ff
Francois Romieu2b7b4312011-04-18 22:53:24 -0700546#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800547 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
548 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700549 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
550 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
551};
552
Francois Romieu2b7b4312011-04-18 22:53:24 -0700553enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* Rx private */
555 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500556 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558#define RxProtoUDP (PID1)
559#define RxProtoTCP (PID0)
560#define RxProtoIP (PID1 | PID0)
561#define RxProtoMask RxProtoIP
562
563 IPFail = (1 << 16), /* IP checksum failed */
564 UDPFail = (1 << 15), /* UDP/IP checksum failed */
565 TCPFail = (1 << 14), /* TCP/IP checksum failed */
566 RxVlanTag = (1 << 16), /* VLAN tag available */
567};
568
569#define RsvdMask 0x3fffc000
570
Heiner Kallweit0170d592019-07-26 21:48:32 +0200571#define RTL_GSO_MAX_SIZE_V1 32000
572#define RTL_GSO_MAX_SEGS_V1 24
573#define RTL_GSO_MAX_SIZE_V2 64000
574#define RTL_GSO_MAX_SEGS_V2 64
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200577 __le32 opts1;
578 __le32 opts2;
579 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580};
581
582struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200583 __le32 opts1;
584 __le32 opts2;
585 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586};
587
588struct ring_info {
589 struct sk_buff *skb;
590 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591};
592
Ivan Vecera355423d2009-02-06 21:49:57 -0800593struct rtl8169_counters {
594 __le64 tx_packets;
595 __le64 rx_packets;
596 __le64 tx_errors;
597 __le32 rx_errors;
598 __le16 rx_missed;
599 __le16 align_errors;
600 __le32 tx_one_collision;
601 __le32 tx_multi_collision;
602 __le64 rx_unicast;
603 __le64 rx_broadcast;
604 __le32 rx_multicast;
605 __le16 tx_aborted;
606 __le16 tx_underun;
607};
608
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200609struct rtl8169_tc_offsets {
610 bool inited;
611 __le64 tx_errors;
612 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200613 __le16 tx_aborted;
614};
615
Francois Romieuda78dbf2012-01-26 14:18:23 +0100616enum rtl_flag {
Kai-Heng Feng6ad56902018-09-11 01:51:43 +0800617 RTL_FLAG_TASK_ENABLED = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100618 RTL_FLAG_TASK_RESET_PENDING,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100619 RTL_FLAG_MAX
620};
621
Junchang Wang8027aa22012-03-04 23:30:32 +0100622struct rtl8169_stats {
623 u64 packets;
624 u64 bytes;
625 struct u64_stats_sync syncp;
626};
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628struct rtl8169_private {
629 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200630 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000631 struct net_device *dev;
Heiner Kallweit703732f2019-01-19 22:07:05 +0100632 struct phy_device *phydev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700633 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200634 u32 msg_enable;
Heiner Kallweit76719ee2019-05-25 20:45:04 +0200635 enum mac_version mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
637 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100639 struct rtl8169_stats rx_stats;
640 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
642 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
643 dma_addr_t TxPhyAddr;
644 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000645 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 u16 cp_cmd;
Heiner Kallweit559c3c02018-11-19 22:34:17 +0100648 u16 irq_mask;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +0200649 struct clk *clk;
françois romieuc0e45c12011-01-03 15:08:04 +0000650
Francois Romieu4422bcd2012-01-26 11:23:32 +0100651 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100652 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
653 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100654 struct work_struct work;
655 } wk;
656
Heiner Kallweitc8248c62019-03-21 21:23:14 +0100657 unsigned irq_enabled:1;
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +0200658 unsigned supports_gmii:1;
Heiner Kallweit62b1b3b2019-06-18 23:14:50 +0200659 unsigned aspm_manageable:1;
Corinna Vinschen42020322015-09-10 10:47:35 +0200660 dma_addr_t counters_phys_addr;
661 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200662 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000663 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000664
Heiner Kallweit254764e2019-01-22 22:23:41 +0100665 const char *fw_name;
Heiner Kallweit8197f9d2019-06-05 08:02:31 +0200666 struct rtl_fw *rtl_fw;
Hayes Wangc5583862012-07-02 17:23:22 +0800667
668 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669};
670
Heiner Kallweit1fcd1652019-04-14 10:30:24 +0200671typedef void (*rtl_generic_fct)(struct rtl8169_private *tp);
672
Ralf Baechle979b6c12005-06-13 14:30:40 -0700673MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200675module_param_named(debug, debug.msg_enable, int, 0);
676MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Heiner Kallweit11287b62019-01-07 21:49:09 +0100677MODULE_SOFTDEP("pre: realtek");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678MODULE_LICENSE("GPL");
françois romieubca03d52011-01-03 15:07:31 +0000679MODULE_FIRMWARE(FIRMWARE_8168D_1);
680MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000681MODULE_FIRMWARE(FIRMWARE_8168E_1);
682MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400683MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800684MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800685MODULE_FIRMWARE(FIRMWARE_8168F_1);
686MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800687MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800688MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800689MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800690MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000691MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000692MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000693MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800694MODULE_FIRMWARE(FIRMWARE_8168H_1);
695MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200696MODULE_FIRMWARE(FIRMWARE_8107E_1);
697MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100699static inline struct device *tp_to_dev(struct rtl8169_private *tp)
700{
701 return &tp->pci_dev->dev;
702}
703
Francois Romieuda78dbf2012-01-26 14:18:23 +0100704static void rtl_lock_work(struct rtl8169_private *tp)
705{
706 mutex_lock(&tp->wk.mutex);
707}
708
709static void rtl_unlock_work(struct rtl8169_private *tp)
710{
711 mutex_unlock(&tp->wk.mutex);
712}
713
Heiner Kallweitdf320ed2019-01-19 22:05:48 +0100714static void rtl_lock_config_regs(struct rtl8169_private *tp)
715{
716 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
717}
718
719static void rtl_unlock_config_regs(struct rtl8169_private *tp)
720{
721 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
722}
723
Heiner Kallweitcb732002018-03-20 07:45:35 +0100724static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200725{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100726 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800727 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200728}
729
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +0200730static bool rtl_is_8168evl_up(struct rtl8169_private *tp)
731{
732 return tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
733 tp->mac_version != RTL_GIGA_MAC_VER_39;
734}
735
Francois Romieuffc46952012-07-06 14:19:23 +0200736struct rtl_cond {
737 bool (*check)(struct rtl8169_private *);
738 const char *msg;
739};
740
741static void rtl_udelay(unsigned int d)
742{
743 udelay(d);
744}
745
746static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
747 void (*delay)(unsigned int), unsigned int d, int n,
748 bool high)
749{
750 int i;
751
752 for (i = 0; i < n; i++) {
Francois Romieuffc46952012-07-06 14:19:23 +0200753 if (c->check(tp) == high)
754 return true;
Heiner Kallweitd1f50502019-05-04 15:20:38 +0200755 delay(d);
Francois Romieuffc46952012-07-06 14:19:23 +0200756 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200757 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
758 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200759 return false;
760}
761
762static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
763 const struct rtl_cond *c,
764 unsigned int d, int n)
765{
766 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
767}
768
769static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
770 const struct rtl_cond *c,
771 unsigned int d, int n)
772{
773 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
774}
775
776static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
777 const struct rtl_cond *c,
778 unsigned int d, int n)
779{
780 return rtl_loop_wait(tp, c, msleep, d, n, true);
781}
782
783static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
784 const struct rtl_cond *c,
785 unsigned int d, int n)
786{
787 return rtl_loop_wait(tp, c, msleep, d, n, false);
788}
789
790#define DECLARE_RTL_COND(name) \
791static bool name ## _check(struct rtl8169_private *); \
792 \
793static const struct rtl_cond name = { \
794 .check = name ## _check, \
795 .msg = #name \
796}; \
797 \
798static bool name ## _check(struct rtl8169_private *tp)
799
Hayes Wangc5583862012-07-02 17:23:22 +0800800static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
801{
802 if (reg & 0xffff0001) {
803 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
804 return true;
805 }
806 return false;
807}
808
809DECLARE_RTL_COND(rtl_ocp_gphy_cond)
810{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200811 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800812}
813
814static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
815{
Hayes Wangc5583862012-07-02 17:23:22 +0800816 if (rtl_ocp_reg_failure(tp, reg))
817 return;
818
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200819 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800820
821 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
822}
823
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200824static int r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
Hayes Wangc5583862012-07-02 17:23:22 +0800825{
Hayes Wangc5583862012-07-02 17:23:22 +0800826 if (rtl_ocp_reg_failure(tp, reg))
827 return 0;
828
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200829 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800830
831 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200832 (RTL_R32(tp, GPHY_OCP) & 0xffff) : -ETIMEDOUT;
Hayes Wangc5583862012-07-02 17:23:22 +0800833}
834
Hayes Wangc5583862012-07-02 17:23:22 +0800835static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
836{
Hayes Wangc5583862012-07-02 17:23:22 +0800837 if (rtl_ocp_reg_failure(tp, reg))
838 return;
839
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200840 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800841}
842
843static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
844{
Hayes Wangc5583862012-07-02 17:23:22 +0800845 if (rtl_ocp_reg_failure(tp, reg))
846 return 0;
847
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200848 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800849
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200850 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800851}
852
Heiner Kallweitef712ed2019-08-04 09:47:51 +0200853static void r8168_mac_ocp_modify(struct rtl8169_private *tp, u32 reg, u16 mask,
854 u16 set)
855{
856 u16 data = r8168_mac_ocp_read(tp, reg);
857
858 r8168_mac_ocp_write(tp, reg, (data & ~mask) | set);
859}
860
Hayes Wangc5583862012-07-02 17:23:22 +0800861#define OCP_STD_PHY_BASE 0xa400
862
863static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
864{
865 if (reg == 0x1f) {
866 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
867 return;
868 }
869
870 if (tp->ocp_base != OCP_STD_PHY_BASE)
871 reg -= 0x10;
872
873 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
874}
875
876static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
877{
878 if (tp->ocp_base != OCP_STD_PHY_BASE)
879 reg -= 0x10;
880
881 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
882}
883
hayeswangeee37862013-04-01 22:23:38 +0000884static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
885{
886 if (reg == 0x1f) {
887 tp->ocp_base = value << 4;
888 return;
889 }
890
891 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
892}
893
894static int mac_mcu_read(struct rtl8169_private *tp, int reg)
895{
896 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
897}
898
Francois Romieuffc46952012-07-06 14:19:23 +0200899DECLARE_RTL_COND(rtl_phyar_cond)
900{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200901 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200902}
903
Francois Romieu24192212012-07-06 20:19:42 +0200904static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200906 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Francois Romieuffc46952012-07-06 14:19:23 +0200908 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700909 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700910 * According to hardware specs a 20us delay is required after write
911 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700912 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700913 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Francois Romieu24192212012-07-06 20:19:42 +0200916static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Francois Romieuffc46952012-07-06 14:19:23 +0200918 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200920 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Francois Romieuffc46952012-07-06 14:19:23 +0200922 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200923 RTL_R32(tp, PHYAR) & 0xffff : -ETIMEDOUT;
Francois Romieuffc46952012-07-06 14:19:23 +0200924
Timo Teräs81a95f02010-06-09 17:31:48 -0700925 /*
926 * According to hardware specs a 20us delay is required after read
927 * complete indication, but before sending next command.
928 */
929 udelay(20);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return value;
932}
933
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800934DECLARE_RTL_COND(rtl_ocpar_cond)
935{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200936 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800937}
938
Francois Romieu24192212012-07-06 20:19:42 +0200939static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000940{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200941 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
942 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_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 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +0000946}
947
Francois Romieu24192212012-07-06 20:19:42 +0200948static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000949{
Francois Romieu24192212012-07-06 20:19:42 +0200950 r8168dp_1_mdio_access(tp, reg,
951 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000952}
953
Francois Romieu24192212012-07-06 20:19:42 +0200954static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000955{
Francois Romieu24192212012-07-06 20:19:42 +0200956 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +0000957
958 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200959 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
960 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000961
Francois Romieuffc46952012-07-06 14:19:23 +0200962 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Heiner Kallweit9b994b42019-06-11 21:04:09 +0200963 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : -ETIMEDOUT;
françois romieuc0e45c12011-01-03 15:08:04 +0000964}
965
françois romieue6de30d2011-01-03 15:08:37 +0000966#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
967
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200968static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000969{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200970 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000971}
972
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200973static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000974{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200975 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000976}
977
Francois Romieu24192212012-07-06 20:19:42 +0200978static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +0000979{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200980 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000981
Francois Romieu24192212012-07-06 20:19:42 +0200982 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +0000983
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200984 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000985}
986
Francois Romieu24192212012-07-06 20:19:42 +0200987static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +0000988{
989 int value;
990
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200991 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000992
Francois Romieu24192212012-07-06 20:19:42 +0200993 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +0000994
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200995 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000996
997 return value;
998}
999
Heiner Kallweitce8843a2019-05-29 21:15:06 +02001000static void rtl_writephy(struct rtl8169_private *tp, int location, int val)
Francois Romieudacf8152008-08-02 20:44:13 +02001001{
Heiner Kallweit5f950522019-05-31 19:53:28 +02001002 switch (tp->mac_version) {
1003 case RTL_GIGA_MAC_VER_27:
1004 r8168dp_1_mdio_write(tp, location, val);
1005 break;
1006 case RTL_GIGA_MAC_VER_28:
1007 case RTL_GIGA_MAC_VER_31:
1008 r8168dp_2_mdio_write(tp, location, val);
1009 break;
1010 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1011 r8168g_mdio_write(tp, location, val);
1012 break;
1013 default:
1014 r8169_mdio_write(tp, location, val);
1015 break;
1016 }
Francois Romieudacf8152008-08-02 20:44:13 +02001017}
1018
françois romieu4da19632011-01-03 15:07:55 +00001019static int rtl_readphy(struct rtl8169_private *tp, int location)
1020{
Heiner Kallweit5f950522019-05-31 19:53:28 +02001021 switch (tp->mac_version) {
1022 case RTL_GIGA_MAC_VER_27:
1023 return r8168dp_1_mdio_read(tp, location);
1024 case RTL_GIGA_MAC_VER_28:
1025 case RTL_GIGA_MAC_VER_31:
1026 return r8168dp_2_mdio_read(tp, location);
1027 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1028 return r8168g_mdio_read(tp, location);
1029 default:
1030 return r8169_mdio_read(tp, location);
1031 }
françois romieu4da19632011-01-03 15:07:55 +00001032}
1033
1034static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1035{
1036 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1037}
1038
Chun-Hao Lin76564422014-10-01 23:17:17 +08001039static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001040{
1041 int val;
1042
françois romieu4da19632011-01-03 15:07:55 +00001043 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001044 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001045}
1046
Francois Romieuffc46952012-07-06 14:19:23 +02001047DECLARE_RTL_COND(rtl_ephyar_cond)
1048{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001049 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001050}
1051
Francois Romieufdf6fc02012-07-06 22:40:38 +02001052static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001053{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001054 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001055 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1056
Francois Romieuffc46952012-07-06 14:19:23 +02001057 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1058
1059 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001060}
1061
Francois Romieufdf6fc02012-07-06 22:40:38 +02001062static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001063{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001064 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001065
Francois Romieuffc46952012-07-06 14:19:23 +02001066 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001067 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001068}
1069
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001070DECLARE_RTL_COND(rtl_eriar_cond)
1071{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001072 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001073}
1074
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001075static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1076 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001077{
Hayes Wang133ac402011-07-06 15:58:05 +08001078 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001079 RTL_W32(tp, ERIDR, val);
1080 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001081
Francois Romieuffc46952012-07-06 14:19:23 +02001082 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001083}
1084
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001085static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1086 u32 val)
1087{
1088 _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
1089}
1090
1091static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001092{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001093 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001094
Francois Romieuffc46952012-07-06 14:19:23 +02001095 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001096 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001097}
1098
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001099static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
1100{
1101 return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
1102}
1103
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001104static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001105 u32 m)
Hayes Wang133ac402011-07-06 15:58:05 +08001106{
1107 u32 val;
1108
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001109 val = rtl_eri_read(tp, addr);
1110 rtl_eri_write(tp, addr, mask, (val & ~m) | p);
Hayes Wang133ac402011-07-06 15:58:05 +08001111}
1112
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001113static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 mask,
1114 u32 p)
1115{
1116 rtl_w0w1_eri(tp, addr, mask, p, 0);
1117}
1118
1119static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 mask,
1120 u32 m)
1121{
1122 rtl_w0w1_eri(tp, addr, mask, 0, m);
1123}
1124
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001125static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1126{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001127 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001128 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001129 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001130}
1131
1132static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1133{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001134 return _rtl_eri_read(tp, reg, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001135}
1136
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001137static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1138 u32 data)
1139{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001140 RTL_W32(tp, OCPDR, data);
1141 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001142 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1143}
1144
1145static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1146 u32 data)
1147{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001148 _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1149 data, ERIAR_OOB);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001150}
1151
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001152static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001153{
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001154 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001155
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001156 r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001157}
1158
1159#define OOB_CMD_RESET 0x00
1160#define OOB_CMD_DRIVER_START 0x05
1161#define OOB_CMD_DRIVER_STOP 0x06
1162
1163static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1164{
1165 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1166}
1167
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001168DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001169{
1170 u16 reg;
1171
1172 reg = rtl8168_get_ocp_reg(tp);
1173
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001174 return r8168dp_ocp_read(tp, 0x0f, reg) & 0x00000800;
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001175}
1176
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001177DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1178{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001179 return r8168ep_ocp_read(tp, 0x0f, 0x124) & 0x00000001;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001180}
1181
1182DECLARE_RTL_COND(rtl_ocp_tx_cond)
1183{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001184 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001185}
1186
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001187static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1188{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001189 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001190 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001191 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1192 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001193}
1194
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001195static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001196{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001197 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1198 rtl_msleep_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001199}
1200
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001201static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1202{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001203 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1204 r8168ep_ocp_write(tp, 0x01, 0x30,
1205 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001206 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1207}
1208
1209static void rtl8168_driver_start(struct rtl8169_private *tp)
1210{
1211 switch (tp->mac_version) {
1212 case RTL_GIGA_MAC_VER_27:
1213 case RTL_GIGA_MAC_VER_28:
1214 case RTL_GIGA_MAC_VER_31:
1215 rtl8168dp_driver_start(tp);
1216 break;
1217 case RTL_GIGA_MAC_VER_49:
1218 case RTL_GIGA_MAC_VER_50:
1219 case RTL_GIGA_MAC_VER_51:
1220 rtl8168ep_driver_start(tp);
1221 break;
1222 default:
1223 BUG();
1224 break;
1225 }
1226}
1227
1228static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1229{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001230 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1231 rtl_msleep_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001232}
1233
1234static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1235{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001236 rtl8168ep_stop_cmac(tp);
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001237 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1238 r8168ep_ocp_write(tp, 0x01, 0x30,
1239 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001240 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1241}
1242
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001243static void rtl8168_driver_stop(struct rtl8169_private *tp)
1244{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001245 switch (tp->mac_version) {
1246 case RTL_GIGA_MAC_VER_27:
1247 case RTL_GIGA_MAC_VER_28:
1248 case RTL_GIGA_MAC_VER_31:
1249 rtl8168dp_driver_stop(tp);
1250 break;
1251 case RTL_GIGA_MAC_VER_49:
1252 case RTL_GIGA_MAC_VER_50:
1253 case RTL_GIGA_MAC_VER_51:
1254 rtl8168ep_driver_stop(tp);
1255 break;
1256 default:
1257 BUG();
1258 break;
1259 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001260}
1261
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001262static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001263{
1264 u16 reg = rtl8168_get_ocp_reg(tp);
1265
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001266 return !!(r8168dp_ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001267}
1268
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001269static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001270{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001271 return !!(r8168ep_ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001272}
1273
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001274static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001275{
1276 switch (tp->mac_version) {
1277 case RTL_GIGA_MAC_VER_27:
1278 case RTL_GIGA_MAC_VER_28:
1279 case RTL_GIGA_MAC_VER_31:
1280 return r8168dp_check_dash(tp);
1281 case RTL_GIGA_MAC_VER_49:
1282 case RTL_GIGA_MAC_VER_50:
1283 case RTL_GIGA_MAC_VER_51:
1284 return r8168ep_check_dash(tp);
1285 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001286 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001287 }
1288}
1289
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001290static void rtl_reset_packet_filter(struct rtl8169_private *tp)
1291{
1292 rtl_eri_clear_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1293 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1294}
1295
Francois Romieuffc46952012-07-06 14:19:23 +02001296DECLARE_RTL_COND(rtl_efusear_cond)
1297{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001298 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001299}
1300
Francois Romieufdf6fc02012-07-06 22:40:38 +02001301static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001302{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001303 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001304
Francois Romieuffc46952012-07-06 14:19:23 +02001305 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001306 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001307}
1308
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001309static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1310{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001311 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001312}
1313
1314static void rtl_irq_disable(struct rtl8169_private *tp)
1315{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001316 RTL_W16(tp, IntrMask, 0);
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001317 tp->irq_enabled = 0;
Francois Romieu3e990ff2012-01-26 12:50:01 +01001318}
1319
Francois Romieuda78dbf2012-01-26 14:18:23 +01001320#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1321#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1322#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1323
Heiner Kallweitfe716f82018-11-19 22:31:32 +01001324static void rtl_irq_enable(struct rtl8169_private *tp)
Francois Romieuda78dbf2012-01-26 14:18:23 +01001325{
Heiner Kallweitc8248c62019-03-21 21:23:14 +01001326 tp->irq_enabled = 1;
Heiner Kallweit559c3c02018-11-19 22:34:17 +01001327 RTL_W16(tp, IntrMask, tp->irq_mask);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001328}
1329
françois romieu811fd302011-12-04 20:30:45 +00001330static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001332 rtl_irq_disable(tp);
Heiner Kallweitde20e122018-09-25 07:58:00 +02001333 rtl_ack_events(tp, 0xffff);
1334 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001335 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Hayes Wang70090422011-07-06 15:58:06 +08001338static void rtl_link_chg_patch(struct rtl8169_private *tp)
1339{
Hayes Wang70090422011-07-06 15:58:06 +08001340 struct net_device *dev = tp->dev;
Heiner Kallweit703732f2019-01-19 22:07:05 +01001341 struct phy_device *phydev = tp->phydev;
Hayes Wang70090422011-07-06 15:58:06 +08001342
1343 if (!netif_running(dev))
1344 return;
1345
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001346 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1347 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001348 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001349 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1350 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001351 } else if (phydev->speed == SPEED_100) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001352 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1353 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wang70090422011-07-06 15:58:06 +08001354 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001355 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1356 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wang70090422011-07-06 15:58:06 +08001357 }
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02001358 rtl_reset_packet_filter(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08001359 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1360 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001361 if (phydev->speed == SPEED_1000) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001362 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1363 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
Hayes Wangc2218922011-09-06 16:55:18 +08001364 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001365 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1366 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
Hayes Wangc2218922011-09-06 16:55:18 +08001367 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001368 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001369 if (phydev->speed == SPEED_10) {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001370 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
1371 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001372 } else {
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02001373 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001374 }
Hayes Wang70090422011-07-06 15:58:06 +08001375 }
1376}
1377
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001378#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1379
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001380static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1381{
1382 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001383
Francois Romieuda78dbf2012-01-26 14:18:23 +01001384 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001385 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001386 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001387 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001388}
1389
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001390static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001391{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001392 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001393 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001394 u32 opt;
1395 u16 reg;
1396 u8 mask;
1397 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001398 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001399 { WAKE_UCAST, Config5, UWF },
1400 { WAKE_BCAST, Config5, BWF },
1401 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001402 { WAKE_ANY, Config5, LanWake },
1403 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001404 };
Francois Romieu851e6022012-04-17 11:10:11 +02001405 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001406
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001407 rtl_unlock_config_regs(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001408
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02001409 if (rtl_is_8168evl_up(tp)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001410 tmp = ARRAY_SIZE(cfg) - 1;
1411 if (wolopts & WAKE_MAGIC)
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001412 rtl_eri_set_bits(tp, 0x0dc, ERIAR_MASK_0100,
1413 MagicPacket_v2);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001414 else
Heiner Kallweite719b3e2019-04-28 11:11:47 +02001415 rtl_eri_clear_bits(tp, 0x0dc, ERIAR_MASK_0100,
1416 MagicPacket_v2);
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02001417 } else {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001418 tmp = ARRAY_SIZE(cfg);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001419 }
1420
1421 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001422 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001423 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001424 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001425 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001426 }
1427
Francois Romieu851e6022012-04-17 11:10:11 +02001428 switch (tp->mac_version) {
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001429 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001430 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001431 if (wolopts)
1432 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001433 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001434 break;
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001435 case RTL_GIGA_MAC_VER_34:
1436 case RTL_GIGA_MAC_VER_37:
1437 case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001438 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001439 if (wolopts)
1440 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001441 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001442 break;
Heiner Kallweitedcde3e2019-07-26 20:56:20 +02001443 default:
1444 break;
Francois Romieu851e6022012-04-17 11:10:11 +02001445 }
1446
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01001447 rtl_lock_config_regs(tp);
Heiner Kallweit3bd82642018-12-30 13:16:12 +01001448
1449 device_set_wakeup_enable(tp_to_dev(tp), wolopts);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001450}
1451
1452static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1453{
1454 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001455 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001456
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001457 if (wol->wolopts & ~WAKE_ANY)
1458 return -EINVAL;
1459
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001460 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001461
Francois Romieuda78dbf2012-01-26 14:18:23 +01001462 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001463
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001464 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001465
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001466 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001467 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001468
1469 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001470
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001471 pm_runtime_put_noidle(d);
1472
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001473 return 0;
1474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476static void rtl8169_get_drvinfo(struct net_device *dev,
1477 struct ethtool_drvinfo *info)
1478{
1479 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001480 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Rick Jones68aad782011-11-07 13:29:27 +00001482 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001483 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001484 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Heiner Kallweit254764e2019-01-22 22:23:41 +01001485 if (rtl_fw)
Rick Jones8ac72d12011-11-22 14:06:26 +00001486 strlcpy(info->fw_version, rtl_fw->version,
1487 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
1490static int rtl8169_get_regs_len(struct net_device *dev)
1491{
1492 return R8169_REGS_SIZE;
1493}
1494
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001495static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1496 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Francois Romieud58d46b2011-05-03 16:38:29 +02001498 struct rtl8169_private *tp = netdev_priv(dev);
1499
Francois Romieu2b7b4312011-04-18 22:53:24 -07001500 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001501 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Francois Romieud58d46b2011-05-03 16:38:29 +02001503 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001504 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001505 features &= ~NETIF_F_IP_CSUM;
1506
Michał Mirosław350fb322011-04-08 06:35:56 +00001507 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
Heiner Kallweita3984572018-04-28 22:19:15 +02001510static int rtl8169_set_features(struct net_device *dev,
1511 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001514 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Heiner Kallweita3984572018-04-28 22:19:15 +02001516 rtl_lock_work(tp);
1517
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001518 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001519 if (features & NETIF_F_RXALL)
1520 rx_config |= (AcceptErr | AcceptRunt);
1521 else
1522 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001524 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001525
hayeswang929a0312014-09-16 11:40:47 +08001526 if (features & NETIF_F_RXCSUM)
1527 tp->cp_cmd |= RxChkSum;
1528 else
1529 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001530
hayeswang929a0312014-09-16 11:40:47 +08001531 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1532 tp->cp_cmd |= RxVlan;
1533 else
1534 tp->cp_cmd &= ~RxVlan;
1535
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001536 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1537 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Francois Romieuda78dbf2012-01-26 14:18:23 +01001539 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 return 0;
1542}
1543
Kirill Smelkov810f4892012-11-10 21:11:02 +04001544static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001546 return (skb_vlan_tag_present(skb)) ?
Heiner Kallweit7424edb2019-07-02 07:59:17 +02001547 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Francois Romieu7a8fc772011-03-01 17:18:33 +01001550static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Francois Romieu7a8fc772011-03-01 17:18:33 +01001554 if (opts2 & RxVlanTag)
Heiner Kallweit7424edb2019-07-02 07:59:17 +02001555 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1559 void *p)
1560{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001561 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001562 u32 __iomem *data = tp->mmio_addr;
1563 u32 *dw = p;
1564 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Francois Romieuda78dbf2012-01-26 14:18:23 +01001566 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001567 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1568 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001569 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001572static u32 rtl8169_get_msglevel(struct net_device *dev)
1573{
1574 struct rtl8169_private *tp = netdev_priv(dev);
1575
1576 return tp->msg_enable;
1577}
1578
1579static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1580{
1581 struct rtl8169_private *tp = netdev_priv(dev);
1582
1583 tp->msg_enable = value;
1584}
1585
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001586static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1587 "tx_packets",
1588 "rx_packets",
1589 "tx_errors",
1590 "rx_errors",
1591 "rx_missed",
1592 "align_errors",
1593 "tx_single_collisions",
1594 "tx_multi_collisions",
1595 "unicast",
1596 "broadcast",
1597 "multicast",
1598 "tx_aborted",
1599 "tx_underrun",
1600};
1601
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001602static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001603{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001604 switch (sset) {
1605 case ETH_SS_STATS:
1606 return ARRAY_SIZE(rtl8169_gstrings);
1607 default:
1608 return -EOPNOTSUPP;
1609 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001610}
1611
Corinna Vinschen42020322015-09-10 10:47:35 +02001612DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001613{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001614 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001615}
1616
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001617static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001618{
Corinna Vinschen42020322015-09-10 10:47:35 +02001619 dma_addr_t paddr = tp->counters_phys_addr;
1620 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001621
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001622 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1623 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001624 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001625 RTL_W32(tp, CounterAddrLow, cmd);
1626 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001627
Francois Romieua78e9362018-01-26 01:53:26 +01001628 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001629}
1630
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001631static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001632{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001633 /*
1634 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1635 * tally counters.
1636 */
1637 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1638 return true;
1639
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001640 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001641}
1642
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001643static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001644{
Heiner Kallweit10262b02019-01-06 20:44:00 +01001645 u8 val = RTL_R8(tp, ChipCmd);
1646
Ivan Vecera355423d2009-02-06 21:49:57 -08001647 /*
1648 * Some chips are unable to dump tally counters when the receiver
Heiner Kallweit10262b02019-01-06 20:44:00 +01001649 * is disabled. If 0xff chip may be in a PCI power-save state.
Ivan Vecera355423d2009-02-06 21:49:57 -08001650 */
Heiner Kallweit10262b02019-01-06 20:44:00 +01001651 if (!(val & CmdRxEnb) || val == 0xff)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001652 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001653
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001654 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001655}
1656
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001657static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001658{
Corinna Vinschen42020322015-09-10 10:47:35 +02001659 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001660 bool ret = false;
1661
1662 /*
1663 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1664 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1665 * reset by a power cycle, while the counter values collected by the
1666 * driver are reset at every driver unload/load cycle.
1667 *
1668 * To make sure the HW values returned by @get_stats64 match the SW
1669 * values, we collect the initial values at first open(*) and use them
1670 * as offsets to normalize the values returned by @get_stats64.
1671 *
1672 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1673 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1674 * set at open time by rtl_hw_start.
1675 */
1676
1677 if (tp->tc_offset.inited)
1678 return true;
1679
1680 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001681 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001682 ret = true;
1683
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001684 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001685 ret = true;
1686
Corinna Vinschen42020322015-09-10 10:47:35 +02001687 tp->tc_offset.tx_errors = counters->tx_errors;
1688 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1689 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001690 tp->tc_offset.inited = true;
1691
1692 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001693}
1694
Ivan Vecera355423d2009-02-06 21:49:57 -08001695static void rtl8169_get_ethtool_stats(struct net_device *dev,
1696 struct ethtool_stats *stats, u64 *data)
1697{
1698 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001699 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02001700 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08001701
1702 ASSERT_RTNL();
1703
Chun-Hao Line0636232016-07-29 16:37:55 +08001704 pm_runtime_get_noresume(d);
1705
1706 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001707 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08001708
1709 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08001710
Corinna Vinschen42020322015-09-10 10:47:35 +02001711 data[0] = le64_to_cpu(counters->tx_packets);
1712 data[1] = le64_to_cpu(counters->rx_packets);
1713 data[2] = le64_to_cpu(counters->tx_errors);
1714 data[3] = le32_to_cpu(counters->rx_errors);
1715 data[4] = le16_to_cpu(counters->rx_missed);
1716 data[5] = le16_to_cpu(counters->align_errors);
1717 data[6] = le32_to_cpu(counters->tx_one_collision);
1718 data[7] = le32_to_cpu(counters->tx_multi_collision);
1719 data[8] = le64_to_cpu(counters->rx_unicast);
1720 data[9] = le64_to_cpu(counters->rx_broadcast);
1721 data[10] = le32_to_cpu(counters->rx_multicast);
1722 data[11] = le16_to_cpu(counters->tx_aborted);
1723 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08001724}
1725
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001726static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1727{
1728 switch(stringset) {
1729 case ETH_SS_STATS:
1730 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1731 break;
1732 }
1733}
1734
Francois Romieu50970832017-10-27 13:24:49 +03001735/*
1736 * Interrupt coalescing
1737 *
1738 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1739 * > 8169, 8168 and 810x line of chipsets
1740 *
1741 * 8169, 8168, and 8136(810x) serial chipsets support it.
1742 *
1743 * > 2 - the Tx timer unit at gigabit speed
1744 *
1745 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1746 * (0xe0) bit 1 and bit 0.
1747 *
1748 * For 8169
1749 * bit[1:0] \ speed 1000M 100M 10M
1750 * 0 0 320ns 2.56us 40.96us
1751 * 0 1 2.56us 20.48us 327.7us
1752 * 1 0 5.12us 40.96us 655.4us
1753 * 1 1 10.24us 81.92us 1.31ms
1754 *
1755 * For the other
1756 * bit[1:0] \ speed 1000M 100M 10M
1757 * 0 0 5us 2.56us 40.96us
1758 * 0 1 40us 20.48us 327.7us
1759 * 1 0 80us 40.96us 655.4us
1760 * 1 1 160us 81.92us 1.31ms
1761 */
1762
1763/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1764struct rtl_coalesce_scale {
1765 /* Rx / Tx */
1766 u32 nsecs[2];
1767};
1768
1769/* rx/tx scale factors for all CPlusCmd[0:1] cases */
1770struct rtl_coalesce_info {
1771 u32 speed;
1772 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1773};
1774
1775/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1776#define rxtx_x1822(r, t) { \
1777 {{(r), (t)}}, \
1778 {{(r)*8, (t)*8}}, \
1779 {{(r)*8*2, (t)*8*2}}, \
1780 {{(r)*8*2*2, (t)*8*2*2}}, \
1781}
1782static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1783 /* speed delays: rx00 tx00 */
1784 { SPEED_10, rxtx_x1822(40960, 40960) },
1785 { SPEED_100, rxtx_x1822( 2560, 2560) },
1786 { SPEED_1000, rxtx_x1822( 320, 320) },
1787 { 0 },
1788};
1789
1790static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1791 /* speed delays: rx00 tx00 */
1792 { SPEED_10, rxtx_x1822(40960, 40960) },
1793 { SPEED_100, rxtx_x1822( 2560, 2560) },
1794 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1795 { 0 },
1796};
1797#undef rxtx_x1822
1798
1799/* get rx/tx scale vector corresponding to current speed */
1800static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1801{
1802 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001803 const struct rtl_coalesce_info *ci;
Francois Romieu50970832017-10-27 13:24:49 +03001804
Heiner Kallweit20023d32019-06-11 21:09:19 +02001805 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
1806 ci = rtl_coalesce_info_8169;
1807 else
1808 ci = rtl_coalesce_info_8168_8136;
Francois Romieu50970832017-10-27 13:24:49 +03001809
Heiner Kallweit20023d32019-06-11 21:09:19 +02001810 for (; ci->speed; ci++) {
1811 if (tp->phydev->speed == ci->speed)
Francois Romieu50970832017-10-27 13:24:49 +03001812 return ci;
Francois Romieu50970832017-10-27 13:24:49 +03001813 }
1814
1815 return ERR_PTR(-ELNRNG);
1816}
1817
1818static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1819{
1820 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001821 const struct rtl_coalesce_info *ci;
1822 const struct rtl_coalesce_scale *scale;
1823 struct {
1824 u32 *max_frames;
1825 u32 *usecs;
1826 } coal_settings [] = {
1827 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1828 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1829 }, *p = coal_settings;
1830 int i;
1831 u16 w;
1832
1833 memset(ec, 0, sizeof(*ec));
1834
1835 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1836 ci = rtl_coalesce_info(dev);
1837 if (IS_ERR(ci))
1838 return PTR_ERR(ci);
1839
Heiner Kallweit0ae09742018-04-28 22:19:26 +02001840 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03001841
1842 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001843 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03001844 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1845 w >>= RTL_COALESCE_SHIFT;
1846 *p->usecs = w & RTL_COALESCE_MASK;
1847 }
1848
1849 for (i = 0; i < 2; i++) {
1850 p = coal_settings + i;
1851 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1852
1853 /*
1854 * ethtool_coalesce says it is illegal to set both usecs and
1855 * max_frames to 0.
1856 */
1857 if (!*p->usecs && !*p->max_frames)
1858 *p->max_frames = 1;
1859 }
1860
1861 return 0;
1862}
1863
1864/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1865static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1866 struct net_device *dev, u32 nsec, u16 *cp01)
1867{
1868 const struct rtl_coalesce_info *ci;
1869 u16 i;
1870
1871 ci = rtl_coalesce_info(dev);
1872 if (IS_ERR(ci))
1873 return ERR_CAST(ci);
1874
1875 for (i = 0; i < 4; i++) {
1876 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1877 ci->scalev[i].nsecs[1]);
1878 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1879 *cp01 = i;
1880 return &ci->scalev[i];
1881 }
1882 }
1883
1884 return ERR_PTR(-EINVAL);
1885}
1886
1887static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1888{
1889 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001890 const struct rtl_coalesce_scale *scale;
1891 struct {
1892 u32 frames;
1893 u32 usecs;
1894 } coal_settings [] = {
1895 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1896 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1897 }, *p = coal_settings;
1898 u16 w = 0, cp01;
1899 int i;
1900
1901 scale = rtl_coalesce_choose_scale(dev,
1902 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1903 if (IS_ERR(scale))
1904 return PTR_ERR(scale);
1905
1906 for (i = 0; i < 2; i++, p++) {
1907 u32 units;
1908
1909 /*
1910 * accept max_frames=1 we returned in rtl_get_coalesce.
1911 * accept it not only when usecs=0 because of e.g. the following scenario:
1912 *
1913 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
1914 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
1915 * - then user does `ethtool -C eth0 rx-usecs 100`
1916 *
1917 * since ethtool sends to kernel whole ethtool_coalesce
1918 * settings, if we do not handle rx_usecs=!0, rx_frames=1
1919 * we'll reject it below in `frames % 4 != 0`.
1920 */
1921 if (p->frames == 1) {
1922 p->frames = 0;
1923 }
1924
1925 units = p->usecs * 1000 / scale->nsecs[i];
1926 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
1927 return -EINVAL;
1928
1929 w <<= RTL_COALESCE_SHIFT;
1930 w |= units;
1931 w <<= RTL_COALESCE_SHIFT;
1932 w |= p->frames >> 2;
1933 }
1934
1935 rtl_lock_work(tp);
1936
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001937 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03001938
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02001939 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001940 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1941 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03001942
1943 rtl_unlock_work(tp);
1944
1945 return 0;
1946}
1947
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001948static int rtl_get_eee_supp(struct rtl8169_private *tp)
1949{
1950 struct phy_device *phydev = tp->phydev;
1951 int ret;
1952
1953 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01001954 case RTL_GIGA_MAC_VER_34:
1955 case RTL_GIGA_MAC_VER_35:
1956 case RTL_GIGA_MAC_VER_36:
1957 case RTL_GIGA_MAC_VER_38:
1958 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1959 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001960 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Heiner Kallweita2928d22019-06-02 10:53:49 +02001961 ret = phy_read_paged(phydev, 0x0a5c, 0x12);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001962 break;
1963 default:
1964 ret = -EPROTONOSUPPORT;
1965 break;
1966 }
1967
1968 return ret;
1969}
1970
1971static int rtl_get_eee_lpadv(struct rtl8169_private *tp)
1972{
1973 struct phy_device *phydev = tp->phydev;
1974 int ret;
1975
1976 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01001977 case RTL_GIGA_MAC_VER_34:
1978 case RTL_GIGA_MAC_VER_35:
1979 case RTL_GIGA_MAC_VER_36:
1980 case RTL_GIGA_MAC_VER_38:
1981 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1982 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001983 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Heiner Kallweita2928d22019-06-02 10:53:49 +02001984 ret = phy_read_paged(phydev, 0x0a5d, 0x11);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01001985 break;
1986 default:
1987 ret = -EPROTONOSUPPORT;
1988 break;
1989 }
1990
1991 return ret;
1992}
1993
1994static int rtl_get_eee_adv(struct rtl8169_private *tp)
1995{
1996 struct phy_device *phydev = tp->phydev;
1997 int ret;
1998
1999 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002000 case RTL_GIGA_MAC_VER_34:
2001 case RTL_GIGA_MAC_VER_35:
2002 case RTL_GIGA_MAC_VER_36:
2003 case RTL_GIGA_MAC_VER_38:
2004 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
2005 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002006 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Heiner Kallweita2928d22019-06-02 10:53:49 +02002007 ret = phy_read_paged(phydev, 0x0a5d, 0x10);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002008 break;
2009 default:
2010 ret = -EPROTONOSUPPORT;
2011 break;
2012 }
2013
2014 return ret;
2015}
2016
2017static int rtl_set_eee_adv(struct rtl8169_private *tp, int val)
2018{
2019 struct phy_device *phydev = tp->phydev;
2020 int ret = 0;
2021
2022 switch (tp->mac_version) {
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002023 case RTL_GIGA_MAC_VER_34:
2024 case RTL_GIGA_MAC_VER_35:
2025 case RTL_GIGA_MAC_VER_36:
2026 case RTL_GIGA_MAC_VER_38:
2027 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
2028 break;
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002029 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Heiner Kallweita2928d22019-06-02 10:53:49 +02002030 phy_write_paged(phydev, 0x0a5d, 0x10, val);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002031 break;
2032 default:
2033 ret = -EPROTONOSUPPORT;
2034 break;
2035 }
2036
2037 return ret;
2038}
2039
2040static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
2041{
2042 struct rtl8169_private *tp = netdev_priv(dev);
2043 struct device *d = tp_to_dev(tp);
2044 int ret;
2045
2046 pm_runtime_get_noresume(d);
2047
2048 if (!pm_runtime_active(d)) {
2049 ret = -EOPNOTSUPP;
2050 goto out;
2051 }
2052
2053 /* Get Supported EEE */
2054 ret = rtl_get_eee_supp(tp);
2055 if (ret < 0)
2056 goto out;
2057 data->supported = mmd_eee_cap_to_ethtool_sup_t(ret);
2058
2059 /* Get advertisement EEE */
2060 ret = rtl_get_eee_adv(tp);
2061 if (ret < 0)
2062 goto out;
2063 data->advertised = mmd_eee_adv_to_ethtool_adv_t(ret);
2064 data->eee_enabled = !!data->advertised;
2065
2066 /* Get LP advertisement EEE */
2067 ret = rtl_get_eee_lpadv(tp);
2068 if (ret < 0)
2069 goto out;
2070 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(ret);
2071 data->eee_active = !!(data->advertised & data->lp_advertised);
2072out:
2073 pm_runtime_put_noidle(d);
2074 return ret < 0 ? ret : 0;
2075}
2076
2077static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
2078{
2079 struct rtl8169_private *tp = netdev_priv(dev);
2080 struct device *d = tp_to_dev(tp);
2081 int old_adv, adv = 0, cap, ret;
2082
2083 pm_runtime_get_noresume(d);
2084
2085 if (!dev->phydev || !pm_runtime_active(d)) {
2086 ret = -EOPNOTSUPP;
2087 goto out;
2088 }
2089
2090 if (dev->phydev->autoneg == AUTONEG_DISABLE ||
2091 dev->phydev->duplex != DUPLEX_FULL) {
2092 ret = -EPROTONOSUPPORT;
2093 goto out;
2094 }
2095
2096 /* Get Supported EEE */
2097 ret = rtl_get_eee_supp(tp);
2098 if (ret < 0)
2099 goto out;
2100 cap = ret;
2101
2102 ret = rtl_get_eee_adv(tp);
2103 if (ret < 0)
2104 goto out;
2105 old_adv = ret;
2106
2107 if (data->eee_enabled) {
2108 adv = !data->advertised ? cap :
2109 ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
2110 /* Mask prohibited EEE modes */
2111 adv &= ~dev->phydev->eee_broken_modes;
2112 }
2113
2114 if (old_adv != adv) {
2115 ret = rtl_set_eee_adv(tp, adv);
2116 if (ret < 0)
2117 goto out;
2118
2119 /* Restart autonegotiation so the new modes get sent to the
2120 * link partner.
2121 */
2122 ret = phy_restart_aneg(dev->phydev);
2123 }
2124
2125out:
2126 pm_runtime_put_noidle(d);
2127 return ret < 0 ? ret : 0;
2128}
2129
Jeff Garzik7282d492006-09-13 14:30:00 -04002130static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 .get_drvinfo = rtl8169_get_drvinfo,
2132 .get_regs_len = rtl8169_get_regs_len,
2133 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002134 .get_coalesce = rtl_get_coalesce,
2135 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002136 .get_msglevel = rtl8169_get_msglevel,
2137 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002139 .get_wol = rtl8169_get_wol,
2140 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002141 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002142 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002143 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002144 .get_ts_info = ethtool_op_get_ts_info,
Heiner Kallweitdd849572018-07-17 22:51:48 +02002145 .nway_reset = phy_ethtool_nway_reset,
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002146 .get_eee = rtl8169_get_eee,
2147 .set_eee = rtl8169_set_eee,
Heiner Kallweit45772432018-07-17 22:51:44 +02002148 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2149 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150};
2151
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01002152static void rtl_enable_eee(struct rtl8169_private *tp)
2153{
2154 int supported = rtl_get_eee_supp(tp);
2155
2156 if (supported > 0)
2157 rtl_set_eee_adv(tp, supported);
2158}
2159
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002160static void rtl8169_get_mac_version(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Francois Romieu0e485152007-02-20 00:00:26 +01002162 /*
2163 * The driver currently handles the 8168Bf and the 8168Be identically
2164 * but they can be identified more specifically through the test below
2165 * if needed:
2166 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002167 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002168 *
2169 * Same thing for the 8101Eb and the 8101Ec:
2170 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002171 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002172 */
Francois Romieu37441002011-06-17 22:58:54 +02002173 static const struct rtl_mac_info {
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002174 u16 mask;
2175 u16 val;
2176 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002178 /* 8168EP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002179 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
2180 { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
2181 { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002182
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002183 /* 8168H family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002184 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
2185 { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002186
Hayes Wangc5583862012-07-02 17:23:22 +08002187 /* 8168G family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002188 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
2189 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
2190 { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
2191 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
Hayes Wangc5583862012-07-02 17:23:22 +08002192
Hayes Wangc2218922011-09-06 16:55:18 +08002193 /* 8168F family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002194 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
2195 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
2196 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
Hayes Wangc2218922011-09-06 16:55:18 +08002197
hayeswang01dc7fe2011-03-21 01:50:28 +00002198 /* 8168E family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002199 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
2200 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
2201 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002202
Francois Romieu5b538df2008-07-20 16:22:45 +02002203 /* 8168D family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002204 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
2205 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002206
françois romieue6de30d2011-01-03 15:08:37 +00002207 /* 8168DP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002208 { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
2209 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
2210 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002211
Francois Romieuef808d52008-06-29 13:10:54 +02002212 /* 8168C family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002213 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
2214 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
2215 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
2216 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
2217 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
2218 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
2219 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002220
2221 /* 8168B family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002222 { 0x7cf, 0x380, RTL_GIGA_MAC_VER_12 },
2223 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
2224 { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002225
2226 /* 8101 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002227 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
2228 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
2229 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
2230 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
2231 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
2232 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
2233 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
2234 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
2235 { 0x7cf, 0x340, RTL_GIGA_MAC_VER_13 },
2236 { 0x7cf, 0x343, RTL_GIGA_MAC_VER_10 },
2237 { 0x7cf, 0x342, RTL_GIGA_MAC_VER_16 },
2238 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
2239 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
2240 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_16 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002241 /* FIXME: where did these entries come from ? -- FR */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002242 { 0xfc8, 0x388, RTL_GIGA_MAC_VER_15 },
2243 { 0xfc8, 0x308, RTL_GIGA_MAC_VER_14 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002244
2245 /* 8110 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002246 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
2247 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
2248 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
2249 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
2250 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002251
Jean Delvaref21b75e2009-05-26 20:54:48 -07002252 /* Catch-all */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002253 { 0x000, 0x000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002254 };
2255 const struct rtl_mac_info *p = mac_info;
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002256 u16 reg = RTL_R32(tp, TxConfig) >> 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002258 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 p++;
2260 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002261
2262 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002263 dev_err(tp_to_dev(tp), "unknown chip XID %03x\n", reg & 0xfcf);
Heiner Kallweit45f19962018-11-22 22:00:10 +01002264 } else if (!tp->supports_gmii) {
2265 if (tp->mac_version == RTL_GIGA_MAC_VER_42)
2266 tp->mac_version = RTL_GIGA_MAC_VER_43;
2267 else if (tp->mac_version == RTL_GIGA_MAC_VER_45)
2268 tp->mac_version = RTL_GIGA_MAC_VER_47;
2269 else if (tp->mac_version == RTL_GIGA_MAC_VER_46)
2270 tp->mac_version = RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
Francois Romieu867763c2007-08-17 18:21:58 +02002274struct phy_reg {
2275 u16 reg;
2276 u16 val;
2277};
2278
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002279static void __rtl_writephy_batch(struct rtl8169_private *tp,
2280 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002281{
2282 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002283 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002284 regs++;
2285 }
2286}
2287
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002288#define rtl_writephy_batch(tp, a) __rtl_writephy_batch(tp, a, ARRAY_SIZE(a))
2289
françois romieuf1e02ed2011-01-13 13:07:53 +00002290static void rtl_release_firmware(struct rtl8169_private *tp)
2291{
Heiner Kallweit254764e2019-01-22 22:23:41 +01002292 if (tp->rtl_fw) {
Heiner Kallweit47ad5932019-06-03 21:26:31 +02002293 rtl_fw_release_firmware(tp->rtl_fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02002294 kfree(tp->rtl_fw);
Heiner Kallweit254764e2019-01-22 22:23:41 +01002295 tp->rtl_fw = NULL;
Francois Romieub6ffd972011-06-17 17:00:05 +02002296 }
françois romieuf1e02ed2011-01-13 13:07:53 +00002297}
2298
François Romieu953a12c2011-04-24 17:38:48 +02002299static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002300{
Heiner Kallweitce8843a2019-05-29 21:15:06 +02002301 /* TODO: release firmware if rtl_fw_write_firmware signals failure. */
Heiner Kallweit254764e2019-01-22 22:23:41 +01002302 if (tp->rtl_fw)
Heiner Kallweitce8843a2019-05-29 21:15:06 +02002303 rtl_fw_write_firmware(tp, tp->rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002304}
2305
2306static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2307{
2308 if (rtl_readphy(tp, reg) != val)
2309 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2310 else
2311 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002312}
2313
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002314static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
2315{
Heiner Kallweitf4528252019-05-04 17:13:09 +02002316 /* Adjust EEE LED frequency */
2317 if (tp->mac_version != RTL_GIGA_MAC_VER_38)
2318 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
2319
Heiner Kallweite719b3e2019-04-28 11:11:47 +02002320 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_1111, 0x0003);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002321}
2322
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01002323static void rtl8168f_config_eee_phy(struct rtl8169_private *tp)
2324{
2325 struct phy_device *phydev = tp->phydev;
2326
2327 phy_write(phydev, 0x1f, 0x0007);
2328 phy_write(phydev, 0x1e, 0x0020);
2329 phy_set_bits(phydev, 0x15, BIT(8));
2330
2331 phy_write(phydev, 0x1f, 0x0005);
2332 phy_write(phydev, 0x05, 0x8b85);
2333 phy_set_bits(phydev, 0x06, BIT(13));
2334
2335 phy_write(phydev, 0x1f, 0x0000);
2336}
2337
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002338static void rtl8168g_config_eee_phy(struct rtl8169_private *tp)
2339{
Heiner Kallweita2928d22019-06-02 10:53:49 +02002340 phy_modify_paged(tp->phydev, 0x0a43, 0x11, 0, BIT(4));
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01002341}
2342
françois romieu4da19632011-01-03 15:07:55 +00002343static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002345 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002346 { 0x1f, 0x0001 },
2347 { 0x06, 0x006e },
2348 { 0x08, 0x0708 },
2349 { 0x15, 0x4000 },
2350 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
françois romieu0b9b5712009-08-10 19:44:56 +00002352 { 0x1f, 0x0001 },
2353 { 0x03, 0x00a1 },
2354 { 0x02, 0x0008 },
2355 { 0x01, 0x0120 },
2356 { 0x00, 0x1000 },
2357 { 0x04, 0x0800 },
2358 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
françois romieu0b9b5712009-08-10 19:44:56 +00002360 { 0x03, 0xff41 },
2361 { 0x02, 0xdf60 },
2362 { 0x01, 0x0140 },
2363 { 0x00, 0x0077 },
2364 { 0x04, 0x7800 },
2365 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
françois romieu0b9b5712009-08-10 19:44:56 +00002367 { 0x03, 0x802f },
2368 { 0x02, 0x4f02 },
2369 { 0x01, 0x0409 },
2370 { 0x00, 0xf0f9 },
2371 { 0x04, 0x9800 },
2372 { 0x04, 0x9000 },
2373
2374 { 0x03, 0xdf01 },
2375 { 0x02, 0xdf20 },
2376 { 0x01, 0xff95 },
2377 { 0x00, 0xba00 },
2378 { 0x04, 0xa800 },
2379 { 0x04, 0xa000 },
2380
2381 { 0x03, 0xff41 },
2382 { 0x02, 0xdf20 },
2383 { 0x01, 0x0140 },
2384 { 0x00, 0x00bb },
2385 { 0x04, 0xb800 },
2386 { 0x04, 0xb000 },
2387
2388 { 0x03, 0xdf41 },
2389 { 0x02, 0xdc60 },
2390 { 0x01, 0x6340 },
2391 { 0x00, 0x007d },
2392 { 0x04, 0xd800 },
2393 { 0x04, 0xd000 },
2394
2395 { 0x03, 0xdf01 },
2396 { 0x02, 0xdf20 },
2397 { 0x01, 0x100a },
2398 { 0x00, 0xa0ff },
2399 { 0x04, 0xf800 },
2400 { 0x04, 0xf000 },
2401
2402 { 0x1f, 0x0000 },
2403 { 0x0b, 0x0000 },
2404 { 0x00, 0x9200 }
2405 };
2406
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002407 rtl_writephy_batch(tp, phy_reg_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
françois romieu4da19632011-01-03 15:07:55 +00002410static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002411{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002412 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002413 { 0x1f, 0x0002 },
2414 { 0x01, 0x90d0 },
2415 { 0x1f, 0x0000 }
2416 };
2417
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002418 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002419}
2420
françois romieu4da19632011-01-03 15:07:55 +00002421static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002422{
2423 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002424
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002425 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2426 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002427 return;
2428
françois romieu4da19632011-01-03 15:07:55 +00002429 rtl_writephy(tp, 0x1f, 0x0001);
2430 rtl_writephy(tp, 0x10, 0xf01b);
2431 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002432}
2433
françois romieu4da19632011-01-03 15:07:55 +00002434static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002435{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002436 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002437 { 0x1f, 0x0001 },
2438 { 0x04, 0x0000 },
2439 { 0x03, 0x00a1 },
2440 { 0x02, 0x0008 },
2441 { 0x01, 0x0120 },
2442 { 0x00, 0x1000 },
2443 { 0x04, 0x0800 },
2444 { 0x04, 0x9000 },
2445 { 0x03, 0x802f },
2446 { 0x02, 0x4f02 },
2447 { 0x01, 0x0409 },
2448 { 0x00, 0xf099 },
2449 { 0x04, 0x9800 },
2450 { 0x04, 0xa000 },
2451 { 0x03, 0xdf01 },
2452 { 0x02, 0xdf20 },
2453 { 0x01, 0xff95 },
2454 { 0x00, 0xba00 },
2455 { 0x04, 0xa800 },
2456 { 0x04, 0xf000 },
2457 { 0x03, 0xdf01 },
2458 { 0x02, 0xdf20 },
2459 { 0x01, 0x101a },
2460 { 0x00, 0xa0ff },
2461 { 0x04, 0xf800 },
2462 { 0x04, 0x0000 },
2463 { 0x1f, 0x0000 },
2464
2465 { 0x1f, 0x0001 },
2466 { 0x10, 0xf41b },
2467 { 0x14, 0xfb54 },
2468 { 0x18, 0xf5c7 },
2469 { 0x1f, 0x0000 },
2470
2471 { 0x1f, 0x0001 },
2472 { 0x17, 0x0cc0 },
2473 { 0x1f, 0x0000 }
2474 };
2475
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002476 rtl_writephy_batch(tp, phy_reg_init);
françois romieu2e9558562009-08-10 19:44:19 +00002477
françois romieu4da19632011-01-03 15:07:55 +00002478 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002479}
2480
françois romieu4da19632011-01-03 15:07:55 +00002481static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002482{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002483 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002484 { 0x1f, 0x0001 },
2485 { 0x04, 0x0000 },
2486 { 0x03, 0x00a1 },
2487 { 0x02, 0x0008 },
2488 { 0x01, 0x0120 },
2489 { 0x00, 0x1000 },
2490 { 0x04, 0x0800 },
2491 { 0x04, 0x9000 },
2492 { 0x03, 0x802f },
2493 { 0x02, 0x4f02 },
2494 { 0x01, 0x0409 },
2495 { 0x00, 0xf099 },
2496 { 0x04, 0x9800 },
2497 { 0x04, 0xa000 },
2498 { 0x03, 0xdf01 },
2499 { 0x02, 0xdf20 },
2500 { 0x01, 0xff95 },
2501 { 0x00, 0xba00 },
2502 { 0x04, 0xa800 },
2503 { 0x04, 0xf000 },
2504 { 0x03, 0xdf01 },
2505 { 0x02, 0xdf20 },
2506 { 0x01, 0x101a },
2507 { 0x00, 0xa0ff },
2508 { 0x04, 0xf800 },
2509 { 0x04, 0x0000 },
2510 { 0x1f, 0x0000 },
2511
2512 { 0x1f, 0x0001 },
2513 { 0x0b, 0x8480 },
2514 { 0x1f, 0x0000 },
2515
2516 { 0x1f, 0x0001 },
2517 { 0x18, 0x67c7 },
2518 { 0x04, 0x2000 },
2519 { 0x03, 0x002f },
2520 { 0x02, 0x4360 },
2521 { 0x01, 0x0109 },
2522 { 0x00, 0x3022 },
2523 { 0x04, 0x2800 },
2524 { 0x1f, 0x0000 },
2525
2526 { 0x1f, 0x0001 },
2527 { 0x17, 0x0cc0 },
2528 { 0x1f, 0x0000 }
2529 };
2530
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002531 rtl_writephy_batch(tp, phy_reg_init);
françois romieu8c7006a2009-08-10 19:43:29 +00002532}
2533
françois romieu4da19632011-01-03 15:07:55 +00002534static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002535{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002536 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002537 { 0x10, 0xf41b },
2538 { 0x1f, 0x0000 }
2539 };
2540
françois romieu4da19632011-01-03 15:07:55 +00002541 rtl_writephy(tp, 0x1f, 0x0001);
2542 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002543
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002544 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002545}
2546
françois romieu4da19632011-01-03 15:07:55 +00002547static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002548{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002549 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002550 { 0x1f, 0x0001 },
2551 { 0x10, 0xf41b },
2552 { 0x1f, 0x0000 }
2553 };
2554
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002555 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu236b8082008-05-30 16:11:48 +02002556}
2557
françois romieu4da19632011-01-03 15:07:55 +00002558static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002559{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002560 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002561 { 0x1f, 0x0000 },
2562 { 0x1d, 0x0f00 },
2563 { 0x1f, 0x0002 },
2564 { 0x0c, 0x1ec8 },
2565 { 0x1f, 0x0000 }
2566 };
2567
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002568 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu867763c2007-08-17 18:21:58 +02002569}
2570
françois romieu4da19632011-01-03 15:07:55 +00002571static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002572{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002573 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002574 { 0x1f, 0x0001 },
2575 { 0x1d, 0x3d98 },
2576 { 0x1f, 0x0000 }
2577 };
2578
françois romieu4da19632011-01-03 15:07:55 +00002579 rtl_writephy(tp, 0x1f, 0x0000);
2580 rtl_patchphy(tp, 0x14, 1 << 5);
2581 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002582
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002583 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuef3386f2008-06-29 12:24:30 +02002584}
2585
françois romieu4da19632011-01-03 15:07:55 +00002586static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002587{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002588 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002589 { 0x1f, 0x0001 },
2590 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002591 { 0x1f, 0x0002 },
2592 { 0x00, 0x88d4 },
2593 { 0x01, 0x82b1 },
2594 { 0x03, 0x7002 },
2595 { 0x08, 0x9e30 },
2596 { 0x09, 0x01f0 },
2597 { 0x0a, 0x5500 },
2598 { 0x0c, 0x00c8 },
2599 { 0x1f, 0x0003 },
2600 { 0x12, 0xc096 },
2601 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002602 { 0x1f, 0x0000 },
2603 { 0x1f, 0x0000 },
2604 { 0x09, 0x2000 },
2605 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002606 };
2607
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002608 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002609
françois romieu4da19632011-01-03 15:07:55 +00002610 rtl_patchphy(tp, 0x14, 1 << 5);
2611 rtl_patchphy(tp, 0x0d, 1 << 5);
2612 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002613}
2614
françois romieu4da19632011-01-03 15:07:55 +00002615static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002616{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002617 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002618 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002619 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002620 { 0x03, 0x802f },
2621 { 0x02, 0x4f02 },
2622 { 0x01, 0x0409 },
2623 { 0x00, 0xf099 },
2624 { 0x04, 0x9800 },
2625 { 0x04, 0x9000 },
2626 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002627 { 0x1f, 0x0002 },
2628 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002629 { 0x06, 0x0761 },
2630 { 0x1f, 0x0003 },
2631 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002632 { 0x1f, 0x0000 }
2633 };
2634
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002635 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieuf50d4272008-05-30 16:07:07 +02002636
françois romieu4da19632011-01-03 15:07:55 +00002637 rtl_patchphy(tp, 0x16, 1 << 0);
2638 rtl_patchphy(tp, 0x14, 1 << 5);
2639 rtl_patchphy(tp, 0x0d, 1 << 5);
2640 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002641}
2642
françois romieu4da19632011-01-03 15:07:55 +00002643static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002644{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002645 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002646 { 0x1f, 0x0001 },
2647 { 0x12, 0x2300 },
2648 { 0x1d, 0x3d98 },
2649 { 0x1f, 0x0002 },
2650 { 0x0c, 0x7eb8 },
2651 { 0x06, 0x5461 },
2652 { 0x1f, 0x0003 },
2653 { 0x16, 0x0f0a },
2654 { 0x1f, 0x0000 }
2655 };
2656
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002657 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu197ff762008-06-28 13:16:02 +02002658
françois romieu4da19632011-01-03 15:07:55 +00002659 rtl_patchphy(tp, 0x16, 1 << 0);
2660 rtl_patchphy(tp, 0x14, 1 << 5);
2661 rtl_patchphy(tp, 0x0d, 1 << 5);
2662 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002663}
2664
françois romieu4da19632011-01-03 15:07:55 +00002665static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002666{
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002668}
2669
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002670static const struct phy_reg rtl8168d_1_phy_reg_init_0[] = {
2671 /* Channel Estimation */
2672 { 0x1f, 0x0001 },
2673 { 0x06, 0x4064 },
2674 { 0x07, 0x2863 },
2675 { 0x08, 0x059c },
2676 { 0x09, 0x26b4 },
2677 { 0x0a, 0x6a19 },
2678 { 0x0b, 0xdcc8 },
2679 { 0x10, 0xf06d },
2680 { 0x14, 0x7f68 },
2681 { 0x18, 0x7fd9 },
2682 { 0x1c, 0xf0ff },
2683 { 0x1d, 0x3d9c },
2684 { 0x1f, 0x0003 },
2685 { 0x12, 0xf49f },
2686 { 0x13, 0x070b },
2687 { 0x1a, 0x05ad },
2688 { 0x14, 0x94c0 },
2689
2690 /*
2691 * Tx Error Issue
2692 * Enhance line driver power
2693 */
2694 { 0x1f, 0x0002 },
2695 { 0x06, 0x5561 },
2696 { 0x1f, 0x0005 },
2697 { 0x05, 0x8332 },
2698 { 0x06, 0x5561 },
2699
2700 /*
2701 * Can not link to 1Gbps with bad cable
2702 * Decrease SNR threshold form 21.07dB to 19.04dB
2703 */
2704 { 0x1f, 0x0001 },
2705 { 0x17, 0x0cc0 },
2706
2707 { 0x1f, 0x0000 },
2708 { 0x0d, 0xf880 }
2709};
2710
2711static const struct phy_reg rtl8168d_1_phy_reg_init_1[] = {
2712 { 0x1f, 0x0002 },
2713 { 0x05, 0x669a },
2714 { 0x1f, 0x0005 },
2715 { 0x05, 0x8330 },
2716 { 0x06, 0x669a },
2717 { 0x1f, 0x0002 }
2718};
2719
françois romieubca03d52011-01-03 15:07:31 +00002720static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002721{
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002722 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
Francois Romieu5b538df2008-07-20 16:22:45 +02002723
françois romieubca03d52011-01-03 15:07:31 +00002724 /*
2725 * Rx Error Issue
2726 * Fine Tune Switching regulator parameter
2727 */
françois romieu4da19632011-01-03 15:07:55 +00002728 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002729 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2730 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002731
Francois Romieufdf6fc02012-07-06 22:40:38 +02002732 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
françois romieudaf9df62009-10-07 12:44:20 +00002733 int val;
2734
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002735 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
françois romieudaf9df62009-10-07 12:44:20 +00002736
françois romieu4da19632011-01-03 15:07:55 +00002737 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002738
2739 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002740 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002741 0x0065, 0x0066, 0x0067, 0x0068,
2742 0x0069, 0x006a, 0x006b, 0x006c
2743 };
2744 int i;
2745
françois romieu4da19632011-01-03 15:07:55 +00002746 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002747
2748 val &= 0xff00;
2749 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002750 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002751 }
2752 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002753 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002754 { 0x1f, 0x0002 },
2755 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002756 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002757 { 0x05, 0x8330 },
2758 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002759 };
2760
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002761 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02002762 }
2763
françois romieubca03d52011-01-03 15:07:31 +00002764 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002765 rtl_writephy(tp, 0x1f, 0x0002);
2766 rtl_patchphy(tp, 0x0d, 0x0300);
2767 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002768
françois romieubca03d52011-01-03 15:07:31 +00002769 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002770 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002771 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2772 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002773
françois romieu4da19632011-01-03 15:07:55 +00002774 rtl_writephy(tp, 0x1f, 0x0005);
2775 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002776
2777 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002778
françois romieu4da19632011-01-03 15:07:55 +00002779 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002780}
2781
françois romieubca03d52011-01-03 15:07:31 +00002782static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002783{
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002784 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
françois romieudaf9df62009-10-07 12:44:20 +00002785
Francois Romieufdf6fc02012-07-06 22:40:38 +02002786 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
françois romieudaf9df62009-10-07 12:44:20 +00002787 int val;
2788
Heiner Kallweitfcb40e12019-05-25 20:57:42 +02002789 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
françois romieudaf9df62009-10-07 12:44:20 +00002790
françois romieu4da19632011-01-03 15:07:55 +00002791 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002792 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002793 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002794 0x0065, 0x0066, 0x0067, 0x0068,
2795 0x0069, 0x006a, 0x006b, 0x006c
2796 };
2797 int i;
2798
françois romieu4da19632011-01-03 15:07:55 +00002799 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002800
2801 val &= 0xff00;
2802 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002803 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002804 }
2805 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002806 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002807 { 0x1f, 0x0002 },
2808 { 0x05, 0x2642 },
2809 { 0x1f, 0x0005 },
2810 { 0x05, 0x8330 },
2811 { 0x06, 0x2642 }
2812 };
2813
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002814 rtl_writephy_batch(tp, phy_reg_init);
françois romieudaf9df62009-10-07 12:44:20 +00002815 }
2816
françois romieubca03d52011-01-03 15:07:31 +00002817 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002818 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002819 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2820 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002821
françois romieubca03d52011-01-03 15:07:31 +00002822 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002823 rtl_writephy(tp, 0x1f, 0x0002);
2824 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002825
françois romieu4da19632011-01-03 15:07:55 +00002826 rtl_writephy(tp, 0x1f, 0x0005);
2827 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002828
2829 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002830
françois romieu4da19632011-01-03 15:07:55 +00002831 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002832}
2833
françois romieu4da19632011-01-03 15:07:55 +00002834static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002835{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002836 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002837 { 0x1f, 0x0002 },
2838 { 0x10, 0x0008 },
2839 { 0x0d, 0x006c },
2840
2841 { 0x1f, 0x0000 },
2842 { 0x0d, 0xf880 },
2843
2844 { 0x1f, 0x0001 },
2845 { 0x17, 0x0cc0 },
2846
2847 { 0x1f, 0x0001 },
2848 { 0x0b, 0xa4d8 },
2849 { 0x09, 0x281c },
2850 { 0x07, 0x2883 },
2851 { 0x0a, 0x6b35 },
2852 { 0x1d, 0x3da4 },
2853 { 0x1c, 0xeffd },
2854 { 0x14, 0x7f52 },
2855 { 0x18, 0x7fc6 },
2856 { 0x08, 0x0601 },
2857 { 0x06, 0x4063 },
2858 { 0x10, 0xf074 },
2859 { 0x1f, 0x0003 },
2860 { 0x13, 0x0789 },
2861 { 0x12, 0xf4bd },
2862 { 0x1a, 0x04fd },
2863 { 0x14, 0x84b0 },
2864 { 0x1f, 0x0000 },
2865 { 0x00, 0x9200 },
2866
2867 { 0x1f, 0x0005 },
2868 { 0x01, 0x0340 },
2869 { 0x1f, 0x0001 },
2870 { 0x04, 0x4000 },
2871 { 0x03, 0x1d21 },
2872 { 0x02, 0x0c32 },
2873 { 0x01, 0x0200 },
2874 { 0x00, 0x5554 },
2875 { 0x04, 0x4800 },
2876 { 0x04, 0x4000 },
2877 { 0x04, 0xf000 },
2878 { 0x03, 0xdf01 },
2879 { 0x02, 0xdf20 },
2880 { 0x01, 0x101a },
2881 { 0x00, 0xa0ff },
2882 { 0x04, 0xf800 },
2883 { 0x04, 0xf000 },
2884 { 0x1f, 0x0000 },
2885
2886 { 0x1f, 0x0007 },
2887 { 0x1e, 0x0023 },
2888 { 0x16, 0x0000 },
2889 { 0x1f, 0x0000 }
2890 };
2891
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002892 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu5b538df2008-07-20 16:22:45 +02002893}
2894
françois romieue6de30d2011-01-03 15:08:37 +00002895static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2896{
2897 static const struct phy_reg phy_reg_init[] = {
2898 { 0x1f, 0x0001 },
2899 { 0x17, 0x0cc0 },
2900
2901 { 0x1f, 0x0007 },
2902 { 0x1e, 0x002d },
2903 { 0x18, 0x0040 },
2904 { 0x1f, 0x0000 }
2905 };
2906
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002907 rtl_writephy_batch(tp, phy_reg_init);
françois romieue6de30d2011-01-03 15:08:37 +00002908 rtl_patchphy(tp, 0x0d, 1 << 5);
2909}
2910
Hayes Wang70090422011-07-06 15:58:06 +08002911static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002912{
2913 static const struct phy_reg phy_reg_init[] = {
2914 /* Enable Delay cap */
2915 { 0x1f, 0x0005 },
2916 { 0x05, 0x8b80 },
2917 { 0x06, 0xc896 },
2918 { 0x1f, 0x0000 },
2919
2920 /* Channel estimation fine tune */
2921 { 0x1f, 0x0001 },
2922 { 0x0b, 0x6c20 },
2923 { 0x07, 0x2872 },
2924 { 0x1c, 0xefff },
2925 { 0x1f, 0x0003 },
2926 { 0x14, 0x6420 },
2927 { 0x1f, 0x0000 },
2928
2929 /* Update PFM & 10M TX idle timer */
2930 { 0x1f, 0x0007 },
2931 { 0x1e, 0x002f },
2932 { 0x15, 0x1919 },
2933 { 0x1f, 0x0000 },
2934
2935 { 0x1f, 0x0007 },
2936 { 0x1e, 0x00ac },
2937 { 0x18, 0x0006 },
2938 { 0x1f, 0x0000 }
2939 };
2940
Francois Romieu15ecd032011-04-27 13:52:22 -07002941 rtl_apply_firmware(tp);
2942
Heiner Kallweit1791ad52019-05-04 16:57:49 +02002943 rtl_writephy_batch(tp, phy_reg_init);
hayeswang01dc7fe2011-03-21 01:50:28 +00002944
2945 /* DCO enable for 10M IDLE Power */
2946 rtl_writephy(tp, 0x1f, 0x0007);
2947 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002948 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002949 rtl_writephy(tp, 0x1f, 0x0000);
2950
2951 /* For impedance matching */
2952 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002953 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002954 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002955
2956 /* PHY auto speed down */
2957 rtl_writephy(tp, 0x1f, 0x0007);
2958 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002959 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002960 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002961 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002962
2963 rtl_writephy(tp, 0x1f, 0x0005);
2964 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002965 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002966 rtl_writephy(tp, 0x1f, 0x0000);
2967
2968 rtl_writephy(tp, 0x1f, 0x0005);
2969 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002970 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002971 rtl_writephy(tp, 0x1f, 0x0007);
2972 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002973 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00002974 rtl_writephy(tp, 0x1f, 0x0006);
2975 rtl_writephy(tp, 0x00, 0x5a00);
2976 rtl_writephy(tp, 0x1f, 0x0000);
2977 rtl_writephy(tp, 0x0d, 0x0007);
2978 rtl_writephy(tp, 0x0e, 0x003c);
2979 rtl_writephy(tp, 0x0d, 0x4007);
2980 rtl_writephy(tp, 0x0e, 0x0000);
2981 rtl_writephy(tp, 0x0d, 0x0000);
2982}
2983
françois romieu9ecb9aa2012-12-07 11:20:21 +00002984static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
2985{
2986 const u16 w[] = {
2987 addr[0] | (addr[1] << 8),
2988 addr[2] | (addr[3] << 8),
2989 addr[4] | (addr[5] << 8)
2990 };
françois romieu9ecb9aa2012-12-07 11:20:21 +00002991
Heiner Kallweit3aa4c492019-05-02 20:46:52 +02002992 rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, w[0] | (w[1] << 16));
2993 rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, w[2]);
2994 rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, w[0] << 16);
2995 rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, w[1] | (w[2] << 16));
françois romieu9ecb9aa2012-12-07 11:20:21 +00002996}
2997
Hayes Wang70090422011-07-06 15:58:06 +08002998static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2999{
3000 static const struct phy_reg phy_reg_init[] = {
3001 /* Enable Delay cap */
3002 { 0x1f, 0x0004 },
3003 { 0x1f, 0x0007 },
3004 { 0x1e, 0x00ac },
3005 { 0x18, 0x0006 },
3006 { 0x1f, 0x0002 },
3007 { 0x1f, 0x0000 },
3008 { 0x1f, 0x0000 },
3009
3010 /* Channel estimation fine tune */
3011 { 0x1f, 0x0003 },
3012 { 0x09, 0xa20f },
3013 { 0x1f, 0x0000 },
3014 { 0x1f, 0x0000 },
3015
3016 /* Green Setting */
3017 { 0x1f, 0x0005 },
3018 { 0x05, 0x8b5b },
3019 { 0x06, 0x9222 },
3020 { 0x05, 0x8b6d },
3021 { 0x06, 0x8000 },
3022 { 0x05, 0x8b76 },
3023 { 0x06, 0x8000 },
3024 { 0x1f, 0x0000 }
3025 };
3026
3027 rtl_apply_firmware(tp);
3028
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003029 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang70090422011-07-06 15:58:06 +08003030
3031 /* For 4-corner performance improve */
3032 rtl_writephy(tp, 0x1f, 0x0005);
3033 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003034 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003035 rtl_writephy(tp, 0x1f, 0x0000);
3036
3037 /* PHY auto speed down */
3038 rtl_writephy(tp, 0x1f, 0x0004);
3039 rtl_writephy(tp, 0x1f, 0x0007);
3040 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003041 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003042 rtl_writephy(tp, 0x1f, 0x0002);
3043 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003044 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003045
3046 /* improve 10M EEE waveform */
3047 rtl_writephy(tp, 0x1f, 0x0005);
3048 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003049 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003050 rtl_writephy(tp, 0x1f, 0x0000);
3051
3052 /* Improve 2-pair detection performance */
3053 rtl_writephy(tp, 0x1f, 0x0005);
3054 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003055 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003056 rtl_writephy(tp, 0x1f, 0x0000);
3057
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01003058 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01003059 rtl_enable_eee(tp);
Hayes Wang70090422011-07-06 15:58:06 +08003060
3061 /* Green feature */
3062 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003063 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3064 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003065 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003066 rtl_writephy(tp, 0x1f, 0x0005);
3067 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3068 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003069
françois romieu9ecb9aa2012-12-07 11:20:21 +00003070 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3071 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003072}
3073
Hayes Wang5f886e02012-03-30 14:33:03 +08003074static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3075{
3076 /* For 4-corner performance improve */
3077 rtl_writephy(tp, 0x1f, 0x0005);
3078 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003079 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003080 rtl_writephy(tp, 0x1f, 0x0000);
3081
3082 /* PHY auto speed down */
3083 rtl_writephy(tp, 0x1f, 0x0007);
3084 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003085 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003086 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003087 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003088
3089 /* Improve 10M EEE waveform */
3090 rtl_writephy(tp, 0x1f, 0x0005);
3091 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003092 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003093 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01003094
3095 rtl8168f_config_eee_phy(tp);
Heiner Kallweit1563daa2019-01-26 10:36:35 +01003096 rtl_enable_eee(tp);
Hayes Wang5f886e02012-03-30 14:33:03 +08003097}
3098
Hayes Wangc2218922011-09-06 16:55:18 +08003099static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3100{
3101 static const struct phy_reg phy_reg_init[] = {
3102 /* Channel estimation fine tune */
3103 { 0x1f, 0x0003 },
3104 { 0x09, 0xa20f },
3105 { 0x1f, 0x0000 },
3106
3107 /* Modify green table for giga & fnet */
3108 { 0x1f, 0x0005 },
3109 { 0x05, 0x8b55 },
3110 { 0x06, 0x0000 },
3111 { 0x05, 0x8b5e },
3112 { 0x06, 0x0000 },
3113 { 0x05, 0x8b67 },
3114 { 0x06, 0x0000 },
3115 { 0x05, 0x8b70 },
3116 { 0x06, 0x0000 },
3117 { 0x1f, 0x0000 },
3118 { 0x1f, 0x0007 },
3119 { 0x1e, 0x0078 },
3120 { 0x17, 0x0000 },
3121 { 0x19, 0x00fb },
3122 { 0x1f, 0x0000 },
3123
3124 /* Modify green table for 10M */
3125 { 0x1f, 0x0005 },
3126 { 0x05, 0x8b79 },
3127 { 0x06, 0xaa00 },
3128 { 0x1f, 0x0000 },
3129
3130 /* Disable hiimpedance detection (RTCT) */
3131 { 0x1f, 0x0003 },
3132 { 0x01, 0x328a },
3133 { 0x1f, 0x0000 }
3134 };
3135
3136 rtl_apply_firmware(tp);
3137
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003138 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangc2218922011-09-06 16:55:18 +08003139
Hayes Wang5f886e02012-03-30 14:33:03 +08003140 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003141
3142 /* Improve 2-pair detection performance */
3143 rtl_writephy(tp, 0x1f, 0x0005);
3144 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003145 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003146 rtl_writephy(tp, 0x1f, 0x0000);
3147}
3148
3149static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3150{
3151 rtl_apply_firmware(tp);
3152
Hayes Wang5f886e02012-03-30 14:33:03 +08003153 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003154}
3155
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003156static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3157{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003158 static const struct phy_reg phy_reg_init[] = {
3159 /* Channel estimation fine tune */
3160 { 0x1f, 0x0003 },
3161 { 0x09, 0xa20f },
3162 { 0x1f, 0x0000 },
3163
3164 /* Modify green table for giga & fnet */
3165 { 0x1f, 0x0005 },
3166 { 0x05, 0x8b55 },
3167 { 0x06, 0x0000 },
3168 { 0x05, 0x8b5e },
3169 { 0x06, 0x0000 },
3170 { 0x05, 0x8b67 },
3171 { 0x06, 0x0000 },
3172 { 0x05, 0x8b70 },
3173 { 0x06, 0x0000 },
3174 { 0x1f, 0x0000 },
3175 { 0x1f, 0x0007 },
3176 { 0x1e, 0x0078 },
3177 { 0x17, 0x0000 },
3178 { 0x19, 0x00aa },
3179 { 0x1f, 0x0000 },
3180
3181 /* Modify green table for 10M */
3182 { 0x1f, 0x0005 },
3183 { 0x05, 0x8b79 },
3184 { 0x06, 0xaa00 },
3185 { 0x1f, 0x0000 },
3186
3187 /* Disable hiimpedance detection (RTCT) */
3188 { 0x1f, 0x0003 },
3189 { 0x01, 0x328a },
3190 { 0x1f, 0x0000 }
3191 };
3192
3193
3194 rtl_apply_firmware(tp);
3195
3196 rtl8168f_hw_phy_config(tp);
3197
3198 /* Improve 2-pair detection performance */
3199 rtl_writephy(tp, 0x1f, 0x0005);
3200 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003201 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003202 rtl_writephy(tp, 0x1f, 0x0000);
3203
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003204 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003205
3206 /* Modify green table for giga */
3207 rtl_writephy(tp, 0x1f, 0x0005);
3208 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003209 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003210 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003211 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003212 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003213 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003214 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003215 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003216 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003217 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003218 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003219 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003220 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003221 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003222 rtl_writephy(tp, 0x1f, 0x0000);
3223
3224 /* uc same-seed solution */
3225 rtl_writephy(tp, 0x1f, 0x0005);
3226 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003227 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003228 rtl_writephy(tp, 0x1f, 0x0000);
3229
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003230 /* Green feature */
3231 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003232 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3233 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003234 rtl_writephy(tp, 0x1f, 0x0000);
3235}
3236
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003237static void rtl8168g_disable_aldps(struct rtl8169_private *tp)
3238{
Heiner Kallweita2928d22019-06-02 10:53:49 +02003239 phy_modify_paged(tp->phydev, 0x0a43, 0x10, BIT(2), 0);
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003240}
3241
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003242static void rtl8168g_phy_adjust_10m_aldps(struct rtl8169_private *tp)
3243{
3244 struct phy_device *phydev = tp->phydev;
3245
Heiner Kallweita2928d22019-06-02 10:53:49 +02003246 phy_modify_paged(phydev, 0x0bcc, 0x14, BIT(8), 0);
3247 phy_modify_paged(phydev, 0x0a44, 0x11, 0, BIT(7) | BIT(6));
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003248 phy_write(phydev, 0x1f, 0x0a43);
3249 phy_write(phydev, 0x13, 0x8084);
3250 phy_clear_bits(phydev, 0x14, BIT(14) | BIT(13));
3251 phy_set_bits(phydev, 0x10, BIT(12) | BIT(1) | BIT(0));
3252
3253 phy_write(phydev, 0x1f, 0x0000);
3254}
3255
Hayes Wangc5583862012-07-02 17:23:22 +08003256static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3257{
Heiner Kallweita2928d22019-06-02 10:53:49 +02003258 int ret;
3259
Hayes Wangc5583862012-07-02 17:23:22 +08003260 rtl_apply_firmware(tp);
3261
Heiner Kallweita2928d22019-06-02 10:53:49 +02003262 ret = phy_read_paged(tp->phydev, 0x0a46, 0x10);
3263 if (ret & BIT(8))
3264 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, BIT(15), 0);
3265 else
3266 phy_modify_paged(tp->phydev, 0x0bcc, 0x12, 0, BIT(15));
Hayes Wangc5583862012-07-02 17:23:22 +08003267
Heiner Kallweita2928d22019-06-02 10:53:49 +02003268 ret = phy_read_paged(tp->phydev, 0x0a46, 0x13);
3269 if (ret & BIT(8))
Thomas Voegtle1a03bb52019-07-20 19:01:22 +02003270 phy_modify_paged(tp->phydev, 0x0c41, 0x15, 0, BIT(1));
Heiner Kallweita2928d22019-06-02 10:53:49 +02003271 else
Thomas Voegtle1a03bb52019-07-20 19:01:22 +02003272 phy_modify_paged(tp->phydev, 0x0c41, 0x15, BIT(1), 0);
Hayes Wangc5583862012-07-02 17:23:22 +08003273
hayeswang41f44d12013-04-01 22:23:36 +00003274 /* Enable PHY auto speed down */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003275 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
Hayes Wangc5583862012-07-02 17:23:22 +08003276
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003277 rtl8168g_phy_adjust_10m_aldps(tp);
hayeswangfe7524c2013-04-01 22:23:37 +00003278
hayeswang41f44d12013-04-01 22:23:36 +00003279 /* EEE auto-fallback function */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003280 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
Hayes Wangc5583862012-07-02 17:23:22 +08003281
hayeswang41f44d12013-04-01 22:23:36 +00003282 /* Enable UC LPF tune function */
3283 rtl_writephy(tp, 0x1f, 0x0a43);
3284 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003285 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003286
Heiner Kallweita2928d22019-06-02 10:53:49 +02003287 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
hayeswang41f44d12013-04-01 22:23:36 +00003288
hayeswangfe7524c2013-04-01 22:23:37 +00003289 /* Improve SWR Efficiency */
3290 rtl_writephy(tp, 0x1f, 0x0bcd);
3291 rtl_writephy(tp, 0x14, 0x5065);
3292 rtl_writephy(tp, 0x14, 0xd065);
3293 rtl_writephy(tp, 0x1f, 0x0bc8);
3294 rtl_writephy(tp, 0x11, 0x5655);
3295 rtl_writephy(tp, 0x1f, 0x0bcd);
3296 rtl_writephy(tp, 0x14, 0x1065);
3297 rtl_writephy(tp, 0x14, 0x9065);
3298 rtl_writephy(tp, 0x14, 0x1065);
Heiner Kallweita2928d22019-06-02 10:53:49 +02003299 rtl_writephy(tp, 0x1f, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003300
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003301 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003302 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003303 rtl_enable_eee(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08003304}
3305
hayeswang57538c42013-04-01 22:23:40 +00003306static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3307{
3308 rtl_apply_firmware(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003309 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003310 rtl_enable_eee(tp);
hayeswang57538c42013-04-01 22:23:40 +00003311}
3312
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003313static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3314{
3315 u16 dout_tapbin;
3316 u32 data;
3317
3318 rtl_apply_firmware(tp);
3319
3320 /* CHN EST parameters adjust - giga master */
3321 rtl_writephy(tp, 0x1f, 0x0a43);
3322 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003323 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003324 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003325 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003326 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003327 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003328 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003329 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003330 rtl_writephy(tp, 0x1f, 0x0000);
3331
3332 /* CHN EST parameters adjust - giga slave */
3333 rtl_writephy(tp, 0x1f, 0x0a43);
3334 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003335 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003336 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003337 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003338 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003339 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003340 rtl_writephy(tp, 0x1f, 0x0000);
3341
3342 /* CHN EST parameters adjust - fnet */
3343 rtl_writephy(tp, 0x1f, 0x0a43);
3344 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003345 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003346 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003347 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003348 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003349 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003350 rtl_writephy(tp, 0x1f, 0x0000);
3351
3352 /* enable R-tune & PGA-retune function */
3353 dout_tapbin = 0;
3354 rtl_writephy(tp, 0x1f, 0x0a46);
3355 data = rtl_readphy(tp, 0x13);
3356 data &= 3;
3357 data <<= 2;
3358 dout_tapbin |= data;
3359 data = rtl_readphy(tp, 0x12);
3360 data &= 0xc000;
3361 data >>= 14;
3362 dout_tapbin |= data;
3363 dout_tapbin = ~(dout_tapbin^0x08);
3364 dout_tapbin <<= 12;
3365 dout_tapbin &= 0xf000;
3366 rtl_writephy(tp, 0x1f, 0x0a43);
3367 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003368 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003369 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003370 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003371 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003372 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003373 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003374 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003375
3376 rtl_writephy(tp, 0x1f, 0x0a43);
3377 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003378 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003379 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003380 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003381 rtl_writephy(tp, 0x1f, 0x0000);
3382
3383 /* enable GPHY 10M */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003384 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003385
3386 /* SAR ADC performance */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003387 phy_modify_paged(tp->phydev, 0x0bca, 0x17, BIT(12) | BIT(13), BIT(14));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003388
3389 rtl_writephy(tp, 0x1f, 0x0a43);
3390 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003391 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003392 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003393 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003394 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003395 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003396 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003397 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003398 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003399 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003400 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003401 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003402 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003403 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003404 rtl_writephy(tp, 0x1f, 0x0000);
3405
3406 /* disable phy pfm mode */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003407 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003408
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003409 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003410 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003411 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003412}
3413
3414static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3415{
3416 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3417 u16 rlen;
3418 u32 data;
3419
3420 rtl_apply_firmware(tp);
3421
3422 /* CHIN EST parameter update */
3423 rtl_writephy(tp, 0x1f, 0x0a43);
3424 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003425 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003426 rtl_writephy(tp, 0x1f, 0x0000);
3427
3428 /* enable R-tune & PGA-retune function */
3429 rtl_writephy(tp, 0x1f, 0x0a43);
3430 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003431 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003432 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003433 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003434 rtl_writephy(tp, 0x1f, 0x0000);
3435
3436 /* enable GPHY 10M */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003437 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(11));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003438
3439 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3440 data = r8168_mac_ocp_read(tp, 0xdd02);
3441 ioffset_p3 = ((data & 0x80)>>7);
3442 ioffset_p3 <<= 3;
3443
3444 data = r8168_mac_ocp_read(tp, 0xdd00);
3445 ioffset_p3 |= ((data & (0xe000))>>13);
3446 ioffset_p2 = ((data & (0x1e00))>>9);
3447 ioffset_p1 = ((data & (0x01e0))>>5);
3448 ioffset_p0 = ((data & 0x0010)>>4);
3449 ioffset_p0 <<= 3;
3450 ioffset_p0 |= (data & (0x07));
3451 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3452
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003453 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003454 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003455 rtl_writephy(tp, 0x1f, 0x0bcf);
3456 rtl_writephy(tp, 0x16, data);
3457 rtl_writephy(tp, 0x1f, 0x0000);
3458 }
3459
3460 /* Modify rlen (TX LPF corner frequency) level */
3461 rtl_writephy(tp, 0x1f, 0x0bcd);
3462 data = rtl_readphy(tp, 0x16);
3463 data &= 0x000f;
3464 rlen = 0;
3465 if (data > 3)
3466 rlen = data - 3;
3467 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3468 rtl_writephy(tp, 0x17, data);
3469 rtl_writephy(tp, 0x1f, 0x0bcd);
3470 rtl_writephy(tp, 0x1f, 0x0000);
3471
3472 /* disable phy pfm mode */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003473 phy_modify_paged(tp->phydev, 0x0a44, 0x11, BIT(7), 0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003474
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003475 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003476 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003477 rtl_enable_eee(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003478}
3479
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003480static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3481{
3482 /* Enable PHY auto speed down */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003483 phy_modify_paged(tp->phydev, 0x0a44, 0x11, 0, BIT(3) | BIT(2));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003484
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003485 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003486
3487 /* Enable EEE auto-fallback function */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003488 phy_modify_paged(tp->phydev, 0x0a4b, 0x11, 0, BIT(2));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003489
3490 /* Enable UC LPF tune function */
3491 rtl_writephy(tp, 0x1f, 0x0a43);
3492 rtl_writephy(tp, 0x13, 0x8012);
3493 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3494 rtl_writephy(tp, 0x1f, 0x0000);
3495
3496 /* set rg_sel_sdm_rate */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003497 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003498
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003499 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003500 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003501 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003502}
3503
3504static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3505{
Heiner Kallweita1ead2e2019-01-23 20:47:30 +01003506 rtl8168g_phy_adjust_10m_aldps(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003507
3508 /* Enable UC LPF tune function */
3509 rtl_writephy(tp, 0x1f, 0x0a43);
3510 rtl_writephy(tp, 0x13, 0x8012);
3511 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3512 rtl_writephy(tp, 0x1f, 0x0000);
3513
3514 /* Set rg_sel_sdm_rate */
Heiner Kallweita2928d22019-06-02 10:53:49 +02003515 phy_modify_paged(tp->phydev, 0x0c42, 0x11, BIT(13), BIT(14));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003516
3517 /* Channel estimation parameters */
3518 rtl_writephy(tp, 0x1f, 0x0a43);
3519 rtl_writephy(tp, 0x13, 0x80f3);
3520 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3521 rtl_writephy(tp, 0x13, 0x80f0);
3522 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3523 rtl_writephy(tp, 0x13, 0x80ef);
3524 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3525 rtl_writephy(tp, 0x13, 0x80f6);
3526 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3527 rtl_writephy(tp, 0x13, 0x80ec);
3528 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3529 rtl_writephy(tp, 0x13, 0x80ed);
3530 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3531 rtl_writephy(tp, 0x13, 0x80f2);
3532 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3533 rtl_writephy(tp, 0x13, 0x80f4);
3534 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3535 rtl_writephy(tp, 0x1f, 0x0a43);
3536 rtl_writephy(tp, 0x13, 0x8110);
3537 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3538 rtl_writephy(tp, 0x13, 0x810f);
3539 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3540 rtl_writephy(tp, 0x13, 0x8111);
3541 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3542 rtl_writephy(tp, 0x13, 0x8113);
3543 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3544 rtl_writephy(tp, 0x13, 0x8115);
3545 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3546 rtl_writephy(tp, 0x13, 0x810e);
3547 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3548 rtl_writephy(tp, 0x13, 0x810c);
3549 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3550 rtl_writephy(tp, 0x13, 0x810b);
3551 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3552 rtl_writephy(tp, 0x1f, 0x0a43);
3553 rtl_writephy(tp, 0x13, 0x80d1);
3554 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3555 rtl_writephy(tp, 0x13, 0x80cd);
3556 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3557 rtl_writephy(tp, 0x13, 0x80d3);
3558 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3559 rtl_writephy(tp, 0x13, 0x80d5);
3560 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3561 rtl_writephy(tp, 0x13, 0x80d7);
3562 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3563
3564 /* Force PWM-mode */
3565 rtl_writephy(tp, 0x1f, 0x0bcd);
3566 rtl_writephy(tp, 0x14, 0x5065);
3567 rtl_writephy(tp, 0x14, 0xd065);
3568 rtl_writephy(tp, 0x1f, 0x0bc8);
3569 rtl_writephy(tp, 0x12, 0x00ed);
3570 rtl_writephy(tp, 0x1f, 0x0bcd);
3571 rtl_writephy(tp, 0x14, 0x1065);
3572 rtl_writephy(tp, 0x14, 0x9065);
3573 rtl_writephy(tp, 0x14, 0x1065);
3574 rtl_writephy(tp, 0x1f, 0x0000);
3575
Heiner Kallweitc46863a2019-01-23 20:39:09 +01003576 rtl8168g_disable_aldps(tp);
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01003577 rtl8168g_config_eee_phy(tp);
Heiner Kallweitb6c7fa42019-01-25 20:39:42 +01003578 rtl_enable_eee(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003579}
3580
françois romieu4da19632011-01-03 15:07:55 +00003581static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003582{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003583 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003584 { 0x1f, 0x0003 },
3585 { 0x08, 0x441d },
3586 { 0x01, 0x9100 },
3587 { 0x1f, 0x0000 }
3588 };
3589
françois romieu4da19632011-01-03 15:07:55 +00003590 rtl_writephy(tp, 0x1f, 0x0000);
3591 rtl_patchphy(tp, 0x11, 1 << 12);
3592 rtl_patchphy(tp, 0x19, 1 << 13);
3593 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003594
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003595 rtl_writephy_batch(tp, phy_reg_init);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003596}
3597
Hayes Wang5a5e4442011-02-22 17:26:21 +08003598static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3599{
3600 static const struct phy_reg phy_reg_init[] = {
3601 { 0x1f, 0x0005 },
3602 { 0x1a, 0x0000 },
3603 { 0x1f, 0x0000 },
3604
3605 { 0x1f, 0x0004 },
3606 { 0x1c, 0x0000 },
3607 { 0x1f, 0x0000 },
3608
3609 { 0x1f, 0x0001 },
3610 { 0x15, 0x7701 },
3611 { 0x1f, 0x0000 }
3612 };
3613
3614 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003615 rtl_writephy(tp, 0x1f, 0x0000);
3616 rtl_writephy(tp, 0x18, 0x0310);
3617 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003618
François Romieu953a12c2011-04-24 17:38:48 +02003619 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003620
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003621 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003622}
3623
Hayes Wang7e18dca2012-03-30 14:33:02 +08003624static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3625{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003626 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003627 rtl_writephy(tp, 0x1f, 0x0000);
3628 rtl_writephy(tp, 0x18, 0x0310);
3629 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003630
3631 rtl_apply_firmware(tp);
3632
3633 /* EEE setting */
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003634 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003635 rtl_writephy(tp, 0x1f, 0x0004);
3636 rtl_writephy(tp, 0x10, 0x401f);
3637 rtl_writephy(tp, 0x19, 0x7030);
3638 rtl_writephy(tp, 0x1f, 0x0000);
3639}
3640
Hayes Wang5598bfe2012-07-02 17:23:21 +08003641static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3642{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003643 static const struct phy_reg phy_reg_init[] = {
3644 { 0x1f, 0x0004 },
3645 { 0x10, 0xc07f },
3646 { 0x19, 0x7030 },
3647 { 0x1f, 0x0000 }
3648 };
3649
3650 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003651 rtl_writephy(tp, 0x1f, 0x0000);
3652 rtl_writephy(tp, 0x18, 0x0310);
3653 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003654
3655 rtl_apply_firmware(tp);
3656
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003657 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02003658 rtl_writephy_batch(tp, phy_reg_init);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003659
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02003660 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003661}
3662
Francois Romieu5615d9f2007-08-17 17:50:46 +02003663static void rtl_hw_phy_config(struct net_device *dev)
3664{
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003665 static const rtl_generic_fct phy_configs[] = {
3666 /* PCI devices. */
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003667 [RTL_GIGA_MAC_VER_02] = rtl8169s_hw_phy_config,
3668 [RTL_GIGA_MAC_VER_03] = rtl8169s_hw_phy_config,
3669 [RTL_GIGA_MAC_VER_04] = rtl8169sb_hw_phy_config,
3670 [RTL_GIGA_MAC_VER_05] = rtl8169scd_hw_phy_config,
3671 [RTL_GIGA_MAC_VER_06] = rtl8169sce_hw_phy_config,
3672 /* PCI-E devices. */
3673 [RTL_GIGA_MAC_VER_07] = rtl8102e_hw_phy_config,
3674 [RTL_GIGA_MAC_VER_08] = rtl8102e_hw_phy_config,
3675 [RTL_GIGA_MAC_VER_09] = rtl8102e_hw_phy_config,
3676 [RTL_GIGA_MAC_VER_10] = NULL,
3677 [RTL_GIGA_MAC_VER_11] = rtl8168bb_hw_phy_config,
3678 [RTL_GIGA_MAC_VER_12] = rtl8168bef_hw_phy_config,
3679 [RTL_GIGA_MAC_VER_13] = NULL,
3680 [RTL_GIGA_MAC_VER_14] = NULL,
3681 [RTL_GIGA_MAC_VER_15] = NULL,
3682 [RTL_GIGA_MAC_VER_16] = NULL,
3683 [RTL_GIGA_MAC_VER_17] = rtl8168bef_hw_phy_config,
3684 [RTL_GIGA_MAC_VER_18] = rtl8168cp_1_hw_phy_config,
3685 [RTL_GIGA_MAC_VER_19] = rtl8168c_1_hw_phy_config,
3686 [RTL_GIGA_MAC_VER_20] = rtl8168c_2_hw_phy_config,
3687 [RTL_GIGA_MAC_VER_21] = rtl8168c_3_hw_phy_config,
3688 [RTL_GIGA_MAC_VER_22] = rtl8168c_4_hw_phy_config,
3689 [RTL_GIGA_MAC_VER_23] = rtl8168cp_2_hw_phy_config,
3690 [RTL_GIGA_MAC_VER_24] = rtl8168cp_2_hw_phy_config,
3691 [RTL_GIGA_MAC_VER_25] = rtl8168d_1_hw_phy_config,
3692 [RTL_GIGA_MAC_VER_26] = rtl8168d_2_hw_phy_config,
3693 [RTL_GIGA_MAC_VER_27] = rtl8168d_3_hw_phy_config,
3694 [RTL_GIGA_MAC_VER_28] = rtl8168d_4_hw_phy_config,
3695 [RTL_GIGA_MAC_VER_29] = rtl8105e_hw_phy_config,
3696 [RTL_GIGA_MAC_VER_30] = rtl8105e_hw_phy_config,
3697 [RTL_GIGA_MAC_VER_31] = NULL,
3698 [RTL_GIGA_MAC_VER_32] = rtl8168e_1_hw_phy_config,
3699 [RTL_GIGA_MAC_VER_33] = rtl8168e_1_hw_phy_config,
3700 [RTL_GIGA_MAC_VER_34] = rtl8168e_2_hw_phy_config,
3701 [RTL_GIGA_MAC_VER_35] = rtl8168f_1_hw_phy_config,
3702 [RTL_GIGA_MAC_VER_36] = rtl8168f_2_hw_phy_config,
3703 [RTL_GIGA_MAC_VER_37] = rtl8402_hw_phy_config,
3704 [RTL_GIGA_MAC_VER_38] = rtl8411_hw_phy_config,
3705 [RTL_GIGA_MAC_VER_39] = rtl8106e_hw_phy_config,
3706 [RTL_GIGA_MAC_VER_40] = rtl8168g_1_hw_phy_config,
3707 [RTL_GIGA_MAC_VER_41] = NULL,
3708 [RTL_GIGA_MAC_VER_42] = rtl8168g_2_hw_phy_config,
3709 [RTL_GIGA_MAC_VER_43] = rtl8168g_2_hw_phy_config,
3710 [RTL_GIGA_MAC_VER_44] = rtl8168g_2_hw_phy_config,
3711 [RTL_GIGA_MAC_VER_45] = rtl8168h_1_hw_phy_config,
3712 [RTL_GIGA_MAC_VER_46] = rtl8168h_2_hw_phy_config,
3713 [RTL_GIGA_MAC_VER_47] = rtl8168h_1_hw_phy_config,
3714 [RTL_GIGA_MAC_VER_48] = rtl8168h_2_hw_phy_config,
3715 [RTL_GIGA_MAC_VER_49] = rtl8168ep_1_hw_phy_config,
3716 [RTL_GIGA_MAC_VER_50] = rtl8168ep_2_hw_phy_config,
3717 [RTL_GIGA_MAC_VER_51] = rtl8168ep_2_hw_phy_config,
3718 };
Francois Romieu5615d9f2007-08-17 17:50:46 +02003719 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003720
Heiner Kallweit1fcd1652019-04-14 10:30:24 +02003721 if (phy_configs[tp->mac_version])
3722 phy_configs[tp->mac_version](tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003723}
3724
Francois Romieuda78dbf2012-01-26 14:18:23 +01003725static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3726{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003727 if (!test_and_set_bit(flag, tp->wk.flags))
3728 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003729}
3730
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003731static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
Francois Romieu5615d9f2007-08-17 17:50:46 +02003733 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003734
Marcus Sundberg773328942008-07-10 21:28:08 +02003735 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit7a67e112018-09-19 22:00:24 +02003736 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3737 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Heiner Kallweit49d17512018-06-28 20:36:15 +02003738 netif_dbg(tp, drv, dev,
3739 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003740 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02003741 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003742
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003743 /* We may have called phy_speed_down before */
Heiner Kallweit703732f2019-01-19 22:07:05 +01003744 phy_speed_up(tp->phydev);
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003745
Heiner Kallweit703732f2019-01-19 22:07:05 +01003746 genphy_soft_reset(tp->phydev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003747}
3748
Francois Romieu773d2022007-01-31 23:47:43 +01003749static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3750{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003751 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003752
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01003753 rtl_unlock_config_regs(tp);
françois romieu908ba2bf2010-04-26 11:42:58 +00003754
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003755 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
3756 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00003757
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003758 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
3759 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00003760
françois romieu9ecb9aa2012-12-07 11:20:21 +00003761 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
3762 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00003763
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01003764 rtl_lock_config_regs(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003765
Francois Romieuda78dbf2012-01-26 14:18:23 +01003766 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01003767}
3768
3769static int rtl_set_mac_address(struct net_device *dev, void *p)
3770{
3771 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01003772 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01003773 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01003774
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01003775 ret = eth_mac_addr(dev, p);
3776 if (ret)
3777 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01003778
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08003779 pm_runtime_get_noresume(d);
3780
3781 if (pm_runtime_active(d))
3782 rtl_rar_set(tp, dev->dev_addr);
3783
3784 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01003785
3786 return 0;
3787}
3788
Heiner Kallweite3972862018-06-29 08:07:04 +02003789static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003790{
Heiner Kallweit703732f2019-01-19 22:07:05 +01003791 struct rtl8169_private *tp = netdev_priv(dev);
3792
Heiner Kallweit69b3c592018-07-17 22:51:53 +02003793 if (!netif_running(dev))
3794 return -ENODEV;
Heiner Kallweite3972862018-06-29 08:07:04 +02003795
Heiner Kallweit703732f2019-01-19 22:07:05 +01003796 return phy_mii_ioctl(tp->phydev, ifr, cmd);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003797}
3798
David S. Miller1805b2f2011-10-24 18:18:09 -04003799static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
3800{
David S. Miller1805b2f2011-10-24 18:18:09 -04003801 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00003802 case RTL_GIGA_MAC_VER_25:
3803 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04003804 case RTL_GIGA_MAC_VER_29:
3805 case RTL_GIGA_MAC_VER_30:
3806 case RTL_GIGA_MAC_VER_32:
3807 case RTL_GIGA_MAC_VER_33:
3808 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02003809 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003810 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04003811 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3812 break;
3813 default:
3814 break;
3815 }
3816}
3817
Heiner Kallweit25e94112019-05-29 20:52:03 +02003818static void rtl_pll_power_down(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00003819{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01003820 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00003821 return;
3822
hayeswang01dc7fe2011-03-21 01:50:28 +00003823 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3824 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02003825 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00003826
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01003827 if (device_may_wakeup(tp_to_dev(tp))) {
3828 phy_speed_down(tp->phydev, false);
3829 rtl_wol_suspend_quirk(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003830 return;
Heiner Kallweit5ea25b12019-01-31 22:03:48 +01003831 }
françois romieu065c27c2011-01-03 15:08:12 +00003832
françois romieu065c27c2011-01-03 15:08:12 +00003833 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02003834 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003835 case RTL_GIGA_MAC_VER_37:
3836 case RTL_GIGA_MAC_VER_39:
3837 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08003838 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003839 case RTL_GIGA_MAC_VER_45:
3840 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003841 case RTL_GIGA_MAC_VER_47:
3842 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003843 case RTL_GIGA_MAC_VER_50:
3844 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003845 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00003846 break;
hayeswangbeb330a2013-04-01 22:23:39 +00003847 case RTL_GIGA_MAC_VER_40:
3848 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003849 case RTL_GIGA_MAC_VER_49:
Heiner Kallweite719b3e2019-04-28 11:11:47 +02003850 rtl_eri_clear_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003851 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00003852 break;
Heiner Kallweit76719ee2019-05-25 20:45:04 +02003853 default:
3854 break;
françois romieu065c27c2011-01-03 15:08:12 +00003855 }
3856}
3857
Heiner Kallweit25e94112019-05-29 20:52:03 +02003858static void rtl_pll_power_up(struct rtl8169_private *tp)
françois romieu065c27c2011-01-03 15:08:12 +00003859{
françois romieu065c27c2011-01-03 15:08:12 +00003860 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02003861 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003862 case RTL_GIGA_MAC_VER_37:
3863 case RTL_GIGA_MAC_VER_39:
3864 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003865 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00003866 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08003867 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003868 case RTL_GIGA_MAC_VER_45:
3869 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02003870 case RTL_GIGA_MAC_VER_47:
3871 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003872 case RTL_GIGA_MAC_VER_50:
3873 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003874 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003875 break;
hayeswangbeb330a2013-04-01 22:23:39 +00003876 case RTL_GIGA_MAC_VER_40:
3877 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003878 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003879 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02003880 rtl_eri_set_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
hayeswangbeb330a2013-04-01 22:23:39 +00003881 break;
Heiner Kallweit76719ee2019-05-25 20:45:04 +02003882 default:
3883 break;
françois romieu065c27c2011-01-03 15:08:12 +00003884 }
3885
Heiner Kallweit703732f2019-01-19 22:07:05 +01003886 phy_resume(tp->phydev);
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02003887 /* give MAC/PHY some time to resume */
3888 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00003889}
3890
Hayes Wange542a222011-07-06 15:58:04 +08003891static void rtl_init_rxcfg(struct rtl8169_private *tp)
3892{
Hayes Wange542a222011-07-06 15:58:04 +08003893 switch (tp->mac_version) {
Heiner Kallweite9588eb2019-05-25 21:14:39 +02003894 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweit2a718832018-05-02 21:39:49 +02003895 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003896 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003897 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02003898 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
Maciej S. Szmigiero511cfd52018-10-11 16:02:10 +02003899 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
3900 case RTL_GIGA_MAC_VER_38:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003901 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003902 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02003903 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003904 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00003905 break;
Hayes Wange542a222011-07-06 15:58:04 +08003906 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003907 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08003908 break;
3909 }
3910}
3911
Hayes Wang92fc43b2011-07-06 15:58:03 +08003912static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3913{
Timo Teräs9fba0812013-01-15 21:01:24 +00003914 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003915}
3916
Francois Romieud58d46b2011-05-03 16:38:29 +02003917static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
3918{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003919 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3920 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01003921 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003922}
3923
3924static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
3925{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003926 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3927 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003928 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003929}
3930
3931static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
3932{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003933 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02003934}
3935
3936static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
3937{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003938 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02003939}
3940
3941static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
3942{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003943 RTL_W8(tp, MaxTxPacketSize, 0x3f);
3944 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
3945 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01003946 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003947}
3948
3949static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
3950{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003951 RTL_W8(tp, MaxTxPacketSize, 0x0c);
3952 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
3953 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003954 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02003955}
3956
3957static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
3958{
Heiner Kallweitcb732002018-03-20 07:45:35 +01003959 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01003960 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02003961}
3962
3963static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
3964{
Heiner Kallweitcb732002018-03-20 07:45:35 +01003965 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02003966 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02003967}
3968
3969static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
3970{
Francois Romieud58d46b2011-05-03 16:38:29 +02003971 r8168b_0_hw_jumbo_enable(tp);
3972
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003973 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02003974}
3975
3976static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
3977{
Francois Romieud58d46b2011-05-03 16:38:29 +02003978 r8168b_0_hw_jumbo_disable(tp);
3979
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003980 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02003981}
3982
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003983static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02003984{
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003985 rtl_unlock_config_regs(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003986 switch (tp->mac_version) {
3987 case RTL_GIGA_MAC_VER_11:
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003988 r8168b_0_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003989 break;
3990 case RTL_GIGA_MAC_VER_12:
3991 case RTL_GIGA_MAC_VER_17:
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003992 r8168b_1_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003993 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003994 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
3995 r8168c_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003996 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02003997 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
3998 r8168dp_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02003999 break;
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02004000 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
4001 r8168e_hw_jumbo_enable(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004002 break;
Francois Romieud58d46b2011-05-03 16:38:29 +02004003 default:
Francois Romieud58d46b2011-05-03 16:38:29 +02004004 break;
4005 }
Heiner Kallweit485bb1b2019-05-31 19:54:03 +02004006 rtl_lock_config_regs(tp);
4007}
4008
4009static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4010{
4011 rtl_unlock_config_regs(tp);
4012 switch (tp->mac_version) {
4013 case RTL_GIGA_MAC_VER_11:
4014 r8168b_0_hw_jumbo_disable(tp);
4015 break;
4016 case RTL_GIGA_MAC_VER_12:
4017 case RTL_GIGA_MAC_VER_17:
4018 r8168b_1_hw_jumbo_disable(tp);
4019 break;
4020 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_26:
4021 r8168c_hw_jumbo_disable(tp);
4022 break;
4023 case RTL_GIGA_MAC_VER_27 ... RTL_GIGA_MAC_VER_28:
4024 r8168dp_hw_jumbo_disable(tp);
4025 break;
4026 case RTL_GIGA_MAC_VER_31 ... RTL_GIGA_MAC_VER_34:
4027 r8168e_hw_jumbo_disable(tp);
4028 break;
4029 default:
4030 break;
4031 }
4032 rtl_lock_config_regs(tp);
Francois Romieud58d46b2011-05-03 16:38:29 +02004033}
4034
Francois Romieuffc46952012-07-06 14:19:23 +02004035DECLARE_RTL_COND(rtl_chipcmd_cond)
4036{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004037 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004038}
4039
Francois Romieu6f43adc2011-04-29 15:05:51 +02004040static void rtl_hw_reset(struct rtl8169_private *tp)
4041{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004042 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004043
Francois Romieuffc46952012-07-06 14:19:23 +02004044 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004045}
4046
Heiner Kallweit254764e2019-01-22 22:23:41 +01004047static void rtl_request_firmware(struct rtl8169_private *tp)
Francois Romieub6ffd972011-06-17 17:00:05 +02004048{
4049 struct rtl_fw *rtl_fw;
Francois Romieub6ffd972011-06-17 17:00:05 +02004050
Heiner Kallweit254764e2019-01-22 22:23:41 +01004051 /* firmware loaded already or no firmware available */
4052 if (tp->rtl_fw || !tp->fw_name)
4053 return;
Francois Romieub6ffd972011-06-17 17:00:05 +02004054
4055 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
Heiner Kallweit47ad5932019-06-03 21:26:31 +02004056 if (!rtl_fw) {
4057 netif_warn(tp, ifup, tp->dev, "Unable to load firmware, out of memory\n");
4058 return;
4059 }
Francois Romieub6ffd972011-06-17 17:00:05 +02004060
Heiner Kallweitce8843a2019-05-29 21:15:06 +02004061 rtl_fw->phy_write = rtl_writephy;
4062 rtl_fw->phy_read = rtl_readphy;
4063 rtl_fw->mac_mcu_write = mac_mcu_write;
4064 rtl_fw->mac_mcu_read = mac_mcu_read;
Heiner Kallweit4edb00f2019-06-03 21:25:43 +02004065 rtl_fw->fw_name = tp->fw_name;
4066 rtl_fw->dev = tp_to_dev(tp);
Heiner Kallweitce8843a2019-05-29 21:15:06 +02004067
Heiner Kallweit47ad5932019-06-03 21:26:31 +02004068 if (rtl_fw_request_firmware(rtl_fw))
4069 kfree(rtl_fw);
4070 else
4071 tp->rtl_fw = rtl_fw;
François Romieu953a12c2011-04-24 17:38:48 +02004072}
4073
Hayes Wang92fc43b2011-07-06 15:58:03 +08004074static void rtl_rx_close(struct rtl8169_private *tp)
4075{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004076 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004077}
4078
Francois Romieuffc46952012-07-06 14:19:23 +02004079DECLARE_RTL_COND(rtl_npq_cond)
4080{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004081 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02004082}
4083
4084DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4085{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004086 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02004087}
4088
françois romieue6de30d2011-01-03 15:08:37 +00004089static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
4091 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004092 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Hayes Wang92fc43b2011-07-06 15:58:03 +08004094 rtl_rx_close(tp);
4095
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004096 switch (tp->mac_version) {
4097 case RTL_GIGA_MAC_VER_27:
4098 case RTL_GIGA_MAC_VER_28:
4099 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02004100 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004101 break;
4102 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4103 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004104 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004105 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004106 break;
4107 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004108 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004109 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004110 break;
françois romieue6de30d2011-01-03 15:08:37 +00004111 }
4112
Hayes Wang92fc43b2011-07-06 15:58:03 +08004113 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114}
4115
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004116static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004117{
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004118 u32 val = TX_DMA_BURST << TxDMAShift |
4119 InterFrameGap << TxInterFrameGapShift;
4120
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02004121 if (rtl_is_8168evl_up(tp))
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004122 val |= TXCFG_AUTO_FIFO;
4123
4124 RTL_W32(tp, TxConfig, val);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004125}
4126
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004127static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004129 /* Low hurts. Let's disable the filtering. */
4130 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004131}
4132
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004133static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004134{
4135 /*
4136 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4137 * register to be written before TxDescAddrLow to work.
4138 * Switching from MMIO to I/O access fixes the issue as well.
4139 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004140 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4141 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4142 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4143 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004144}
4145
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004146static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004147{
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004148 u32 val;
Francois Romieu6dccd162007-02-13 23:38:05 +01004149
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004150 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4151 val = 0x000fff00;
4152 else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
4153 val = 0x00ffff00;
4154 else
4155 return;
4156
4157 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
4158 val |= 0xff;
4159
4160 RTL_W32(tp, 0x7c, val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004161}
4162
Francois Romieue6b763e2012-03-08 09:35:39 +01004163static void rtl_set_rx_mode(struct net_device *dev)
4164{
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004165 u32 rx_mode = AcceptBroadcast | AcceptMyPhys | AcceptMulticast;
4166 /* Multicast hash filter */
4167 u32 mc_filter[2] = { 0xffffffff, 0xffffffff };
Francois Romieue6b763e2012-03-08 09:35:39 +01004168 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004169 u32 tmp;
Francois Romieue6b763e2012-03-08 09:35:39 +01004170
4171 if (dev->flags & IFF_PROMISC) {
4172 /* Unconditionally log net taps. */
4173 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004174 rx_mode |= AcceptAllPhys;
4175 } else if (netdev_mc_count(dev) > MC_FILTER_LIMIT ||
4176 dev->flags & IFF_ALLMULTI ||
4177 tp->mac_version == RTL_GIGA_MAC_VER_35) {
4178 /* accept all multicasts */
4179 } else if (netdev_mc_empty(dev)) {
4180 rx_mode &= ~AcceptMulticast;
Francois Romieue6b763e2012-03-08 09:35:39 +01004181 } else {
4182 struct netdev_hw_addr *ha;
4183
Francois Romieue6b763e2012-03-08 09:35:39 +01004184 mc_filter[1] = mc_filter[0] = 0;
4185 netdev_for_each_mc_addr(ha, dev) {
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004186 u32 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4187 mc_filter[bit_nr >> 5] |= BIT(bit_nr & 31);
4188 }
4189
4190 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4191 tmp = mc_filter[0];
4192 mc_filter[0] = swab32(mc_filter[1]);
4193 mc_filter[1] = swab32(tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004194 }
4195 }
4196
4197 if (dev->features & NETIF_F_RXALL)
4198 rx_mode |= (AcceptErr | AcceptRunt);
4199
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004200 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4201 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004202
Heiner Kallweit81cd17a2019-07-24 23:34:45 +02004203 tmp = RTL_R32(tp, RxConfig);
4204 RTL_W32(tp, RxConfig, (tmp & ~RX_CONFIG_ACCEPT_MASK) | rx_mode);
Francois Romieue6b763e2012-03-08 09:35:39 +01004205}
4206
Francois Romieuffc46952012-07-06 14:19:23 +02004207DECLARE_RTL_COND(rtl_csiar_cond)
4208{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004209 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02004210}
4211
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004212static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004213{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004214 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4215
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004216 RTL_W32(tp, CSIDR, value);
4217 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004218 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004219
Francois Romieuffc46952012-07-06 14:19:23 +02004220 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004221}
4222
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004223static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004224{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004225 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4226
4227 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4228 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004229
Francois Romieuffc46952012-07-06 14:19:23 +02004230 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004231 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004232}
4233
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004234static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004235{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004236 struct pci_dev *pdev = tp->pci_dev;
4237 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004238
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004239 /* According to Realtek the value at config space address 0x070f
4240 * controls the L0s/L1 entrance latency. We try standard ECAM access
4241 * first and if it fails fall back to CSI.
4242 */
4243 if (pdev->cfg_size > 0x070f &&
4244 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4245 return;
4246
4247 netdev_notice_once(tp->dev,
4248 "No native access to PCI extended config space, falling back to CSI\n");
4249 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4250 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004251}
4252
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004253static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08004254{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004255 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02004256}
4257
4258struct ephy_info {
4259 unsigned int offset;
4260 u16 mask;
4261 u16 bits;
4262};
4263
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004264static void __rtl_ephy_init(struct rtl8169_private *tp,
4265 const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004266{
4267 u16 w;
4268
4269 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004270 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4271 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004272 e++;
4273 }
4274}
4275
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004276#define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
4277
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004278static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02004279{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004280 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004281 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004282}
4283
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004284static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004285{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004286 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004287 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004288}
4289
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004290static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
hayeswangb51ecea2014-07-09 14:52:51 +08004291{
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004292 /* work around an issue when PCI reset occurs during L2/L3 state */
4293 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
hayeswangb51ecea2014-07-09 14:52:51 +08004294}
4295
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004296static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4297{
Heiner Kallweit62b1b3b2019-06-18 23:14:50 +02004298 /* Don't enable ASPM in the chip if OS can't control ASPM */
4299 if (enable && tp->aspm_manageable) {
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004300 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
Kai-Heng Feng94235462018-09-12 14:58:20 +08004301 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004302 } else {
4303 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4304 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4305 }
Kai-Heng Feng94235462018-09-12 14:58:20 +08004306
4307 udelay(10);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004308}
4309
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004310static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
4311 u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
4312{
4313 /* Usage of dynamic vs. static FIFO is controlled by bit
4314 * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
4315 */
4316 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
4317 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
4318}
4319
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004320static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
4321 u8 low, u8 high)
4322{
4323 /* FIFO thresholds for pause flow control */
4324 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
4325 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
4326}
4327
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004328static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004329{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004330 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004331
françois romieufaf1e782013-02-27 13:01:57 +00004332 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004333 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00004334 PCI_EXP_DEVCTL_NOSNOOP_EN);
4335 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004336}
4337
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004338static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004339{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004340 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004341
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004342 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004343}
4344
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004345static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004346{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004347 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02004348
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004349 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004350
françois romieufaf1e782013-02-27 13:01:57 +00004351 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004352 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02004353
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004354 rtl_disable_clock_request(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004355}
4356
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004357static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004358{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004359 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004360 { 0x01, 0, 0x0001 },
4361 { 0x02, 0x0800, 0x1000 },
4362 { 0x03, 0, 0x0042 },
4363 { 0x06, 0x0080, 0x0000 },
4364 { 0x07, 0, 0x2000 }
4365 };
4366
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004367 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004368
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004369 rtl_ephy_init(tp, e_info_8168cp);
Francois Romieub726e492008-06-28 12:22:59 +02004370
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004371 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004372}
4373
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004374static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004375{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004376 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004377
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004378 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02004379
françois romieufaf1e782013-02-27 13:01:57 +00004380 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004381 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02004382}
4383
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004384static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004385{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004386 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004387
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004388 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004389
4390 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004391 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004392
françois romieufaf1e782013-02-27 13:01:57 +00004393 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004394 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004395}
4396
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004397static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004398{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004399 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004400 { 0x02, 0x0800, 0x1000 },
4401 { 0x03, 0, 0x0002 },
4402 { 0x06, 0x0080, 0x0000 }
4403 };
4404
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004405 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004406
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004407 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02004408
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004409 rtl_ephy_init(tp, e_info_8168c_1);
Francois Romieub726e492008-06-28 12:22:59 +02004410
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004411 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004412}
4413
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004414static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004415{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004416 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004417 { 0x01, 0, 0x0001 },
4418 { 0x03, 0x0400, 0x0220 }
4419 };
4420
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004421 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004422
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004423 rtl_ephy_init(tp, e_info_8168c_2);
Francois Romieub726e492008-06-28 12:22:59 +02004424
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004425 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004426}
4427
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004428static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004429{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004430 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004431}
4432
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004433static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004434{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004435 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004436
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004437 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004438}
4439
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004440static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004441{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004442 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004443
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004444 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004445
françois romieufaf1e782013-02-27 13:01:57 +00004446 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004447 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02004448}
4449
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004450static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004451{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004452 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004453
françois romieufaf1e782013-02-27 13:01:57 +00004454 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004455 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00004456
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004457 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004458}
4459
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004460static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004461{
4462 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004463 { 0x0b, 0x0000, 0x0048 },
4464 { 0x19, 0x0020, 0x0050 },
4465 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00004466 };
françois romieue6de30d2011-01-03 15:08:37 +00004467
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004468 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004469
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004470 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00004471
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004472 rtl_ephy_init(tp, e_info_8168d_4);
françois romieue6de30d2011-01-03 15:08:37 +00004473
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004474 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004475}
4476
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004477static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004478{
Hayes Wang70090422011-07-06 15:58:06 +08004479 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004480 { 0x00, 0x0200, 0x0100 },
4481 { 0x00, 0x0000, 0x0004 },
4482 { 0x06, 0x0002, 0x0001 },
4483 { 0x06, 0x0000, 0x0030 },
4484 { 0x07, 0x0000, 0x2000 },
4485 { 0x00, 0x0000, 0x0020 },
4486 { 0x03, 0x5800, 0x2000 },
4487 { 0x03, 0x0000, 0x0001 },
4488 { 0x01, 0x0800, 0x1000 },
4489 { 0x07, 0x0000, 0x4000 },
4490 { 0x1e, 0x0000, 0x2000 },
4491 { 0x19, 0xffff, 0xfe6c },
4492 { 0x0a, 0x0000, 0x0040 }
4493 };
4494
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004495 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004496
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004497 rtl_ephy_init(tp, e_info_8168e_1);
hayeswang01dc7fe2011-03-21 01:50:28 +00004498
françois romieufaf1e782013-02-27 13:01:57 +00004499 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004500 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00004501
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004502 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004503
4504 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004505 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4506 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004507
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004508 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004509}
4510
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004511static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004512{
4513 static const struct ephy_info e_info_8168e_2[] = {
4514 { 0x09, 0x0000, 0x0080 },
4515 { 0x19, 0x0000, 0x0224 }
4516 };
4517
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004518 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004519
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004520 rtl_ephy_init(tp, e_info_8168e_2);
Hayes Wang70090422011-07-06 15:58:06 +08004521
françois romieufaf1e782013-02-27 13:01:57 +00004522 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004523 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08004524
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004525 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4526 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004527 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004528 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4529 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004530 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004531 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang70090422011-07-06 15:58:06 +08004532
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004533 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004534
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004535 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08004536
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01004537 rtl8168_config_eee_mac(tp);
4538
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004539 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4540 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4541 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02004542
4543 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08004544}
4545
Hayes Wang5f886e02012-03-30 14:33:03 +08004546static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004547{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004548 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004549
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004550 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08004551
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004552 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4553 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004554 rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004555 rtl_reset_packet_filter(tp);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004556 rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
4557 rtl_eri_set_bits(tp, 0x1d0, ERIAR_MASK_0001, BIT(4));
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004558 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4559 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
Hayes Wangc2218922011-09-06 16:55:18 +08004560
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004561 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004562
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004563 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
4564 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4565 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4566 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitcc07d2712019-01-26 10:35:30 +01004567
4568 rtl8168_config_eee_mac(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004569}
4570
Hayes Wang5f886e02012-03-30 14:33:03 +08004571static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
4572{
Hayes Wang5f886e02012-03-30 14:33:03 +08004573 static const struct ephy_info e_info_8168f_1[] = {
4574 { 0x06, 0x00c0, 0x0020 },
4575 { 0x08, 0x0001, 0x0002 },
4576 { 0x09, 0x0000, 0x0080 },
4577 { 0x19, 0x0000, 0x0224 }
4578 };
4579
4580 rtl_hw_start_8168f(tp);
4581
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004582 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wang5f886e02012-03-30 14:33:03 +08004583
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004584 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
Hayes Wang5f886e02012-03-30 14:33:03 +08004585}
4586
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004587static void rtl_hw_start_8411(struct rtl8169_private *tp)
4588{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004589 static const struct ephy_info e_info_8168f_1[] = {
4590 { 0x06, 0x00c0, 0x0020 },
4591 { 0x0f, 0xffff, 0x5200 },
4592 { 0x1e, 0x0000, 0x4000 },
4593 { 0x19, 0x0000, 0x0224 }
4594 };
4595
4596 rtl_hw_start_8168f(tp);
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004597 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004598
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004599 rtl_ephy_init(tp, e_info_8168f_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004600
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004601 rtl_eri_set_bits(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08004602}
4603
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004604static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08004605{
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004606 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004607 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Hayes Wangc5583862012-07-02 17:23:22 +08004608
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004609 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08004610
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004611 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08004612
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004613 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004614 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
Hayes Wangc5583862012-07-02 17:23:22 +08004615
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004616 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08004617
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004618 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4619 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08004620
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004621 rtl8168_config_eee_mac(tp);
4622
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004623 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004624 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
hayeswangb51ecea2014-07-09 14:52:51 +08004625
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004626 rtl_pcie_state_l2l3_disable(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08004627}
4628
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004629static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
4630{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004631 static const struct ephy_info e_info_8168g_1[] = {
4632 { 0x00, 0x0000, 0x0008 },
4633 { 0x0c, 0x37d0, 0x0820 },
4634 { 0x1e, 0x0000, 0x0001 },
4635 { 0x19, 0x8000, 0x0000 }
4636 };
4637
4638 rtl_hw_start_8168g(tp);
4639
4640 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004641 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004642 rtl_ephy_init(tp, e_info_8168g_1);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004643 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004644}
4645
hayeswang57538c42013-04-01 22:23:40 +00004646static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
4647{
hayeswang57538c42013-04-01 22:23:40 +00004648 static const struct ephy_info e_info_8168g_2[] = {
4649 { 0x00, 0x0000, 0x0008 },
4650 { 0x0c, 0x3df0, 0x0200 },
4651 { 0x19, 0xffff, 0xfc00 },
4652 { 0x1e, 0xffff, 0x20eb }
4653 };
4654
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004655 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00004656
4657 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004658 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4659 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004660 rtl_ephy_init(tp, e_info_8168g_2);
hayeswang57538c42013-04-01 22:23:40 +00004661}
4662
hayeswang45dd95c2013-07-08 17:09:01 +08004663static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
4664{
hayeswang45dd95c2013-07-08 17:09:01 +08004665 static const struct ephy_info e_info_8411_2[] = {
4666 { 0x00, 0x0000, 0x0008 },
4667 { 0x0c, 0x3df0, 0x0200 },
4668 { 0x0f, 0xffff, 0x5200 },
4669 { 0x19, 0x0020, 0x0000 },
4670 { 0x1e, 0x0000, 0x2000 }
4671 };
4672
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08004673 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08004674
4675 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004676 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004677 rtl_ephy_init(tp, e_info_8411_2);
Heiner Kallweitfe4e8db02019-07-13 13:45:47 +02004678
4679 /* The following Realtek-provided magic fixes an issue with the RX unit
4680 * getting confused after the PHY having been powered-down.
4681 */
4682 r8168_mac_ocp_write(tp, 0xFC28, 0x0000);
4683 r8168_mac_ocp_write(tp, 0xFC2A, 0x0000);
4684 r8168_mac_ocp_write(tp, 0xFC2C, 0x0000);
4685 r8168_mac_ocp_write(tp, 0xFC2E, 0x0000);
4686 r8168_mac_ocp_write(tp, 0xFC30, 0x0000);
4687 r8168_mac_ocp_write(tp, 0xFC32, 0x0000);
4688 r8168_mac_ocp_write(tp, 0xFC34, 0x0000);
4689 r8168_mac_ocp_write(tp, 0xFC36, 0x0000);
4690 mdelay(3);
4691 r8168_mac_ocp_write(tp, 0xFC26, 0x0000);
4692
4693 r8168_mac_ocp_write(tp, 0xF800, 0xE008);
4694 r8168_mac_ocp_write(tp, 0xF802, 0xE00A);
4695 r8168_mac_ocp_write(tp, 0xF804, 0xE00C);
4696 r8168_mac_ocp_write(tp, 0xF806, 0xE00E);
4697 r8168_mac_ocp_write(tp, 0xF808, 0xE027);
4698 r8168_mac_ocp_write(tp, 0xF80A, 0xE04F);
4699 r8168_mac_ocp_write(tp, 0xF80C, 0xE05E);
4700 r8168_mac_ocp_write(tp, 0xF80E, 0xE065);
4701 r8168_mac_ocp_write(tp, 0xF810, 0xC602);
4702 r8168_mac_ocp_write(tp, 0xF812, 0xBE00);
4703 r8168_mac_ocp_write(tp, 0xF814, 0x0000);
4704 r8168_mac_ocp_write(tp, 0xF816, 0xC502);
4705 r8168_mac_ocp_write(tp, 0xF818, 0xBD00);
4706 r8168_mac_ocp_write(tp, 0xF81A, 0x074C);
4707 r8168_mac_ocp_write(tp, 0xF81C, 0xC302);
4708 r8168_mac_ocp_write(tp, 0xF81E, 0xBB00);
4709 r8168_mac_ocp_write(tp, 0xF820, 0x080A);
4710 r8168_mac_ocp_write(tp, 0xF822, 0x6420);
4711 r8168_mac_ocp_write(tp, 0xF824, 0x48C2);
4712 r8168_mac_ocp_write(tp, 0xF826, 0x8C20);
4713 r8168_mac_ocp_write(tp, 0xF828, 0xC516);
4714 r8168_mac_ocp_write(tp, 0xF82A, 0x64A4);
4715 r8168_mac_ocp_write(tp, 0xF82C, 0x49C0);
4716 r8168_mac_ocp_write(tp, 0xF82E, 0xF009);
4717 r8168_mac_ocp_write(tp, 0xF830, 0x74A2);
4718 r8168_mac_ocp_write(tp, 0xF832, 0x8CA5);
4719 r8168_mac_ocp_write(tp, 0xF834, 0x74A0);
4720 r8168_mac_ocp_write(tp, 0xF836, 0xC50E);
4721 r8168_mac_ocp_write(tp, 0xF838, 0x9CA2);
4722 r8168_mac_ocp_write(tp, 0xF83A, 0x1C11);
4723 r8168_mac_ocp_write(tp, 0xF83C, 0x9CA0);
4724 r8168_mac_ocp_write(tp, 0xF83E, 0xE006);
4725 r8168_mac_ocp_write(tp, 0xF840, 0x74F8);
4726 r8168_mac_ocp_write(tp, 0xF842, 0x48C4);
4727 r8168_mac_ocp_write(tp, 0xF844, 0x8CF8);
4728 r8168_mac_ocp_write(tp, 0xF846, 0xC404);
4729 r8168_mac_ocp_write(tp, 0xF848, 0xBC00);
4730 r8168_mac_ocp_write(tp, 0xF84A, 0xC403);
4731 r8168_mac_ocp_write(tp, 0xF84C, 0xBC00);
4732 r8168_mac_ocp_write(tp, 0xF84E, 0x0BF2);
4733 r8168_mac_ocp_write(tp, 0xF850, 0x0C0A);
4734 r8168_mac_ocp_write(tp, 0xF852, 0xE434);
4735 r8168_mac_ocp_write(tp, 0xF854, 0xD3C0);
4736 r8168_mac_ocp_write(tp, 0xF856, 0x49D9);
4737 r8168_mac_ocp_write(tp, 0xF858, 0xF01F);
4738 r8168_mac_ocp_write(tp, 0xF85A, 0xC526);
4739 r8168_mac_ocp_write(tp, 0xF85C, 0x64A5);
4740 r8168_mac_ocp_write(tp, 0xF85E, 0x1400);
4741 r8168_mac_ocp_write(tp, 0xF860, 0xF007);
4742 r8168_mac_ocp_write(tp, 0xF862, 0x0C01);
4743 r8168_mac_ocp_write(tp, 0xF864, 0x8CA5);
4744 r8168_mac_ocp_write(tp, 0xF866, 0x1C15);
4745 r8168_mac_ocp_write(tp, 0xF868, 0xC51B);
4746 r8168_mac_ocp_write(tp, 0xF86A, 0x9CA0);
4747 r8168_mac_ocp_write(tp, 0xF86C, 0xE013);
4748 r8168_mac_ocp_write(tp, 0xF86E, 0xC519);
4749 r8168_mac_ocp_write(tp, 0xF870, 0x74A0);
4750 r8168_mac_ocp_write(tp, 0xF872, 0x48C4);
4751 r8168_mac_ocp_write(tp, 0xF874, 0x8CA0);
4752 r8168_mac_ocp_write(tp, 0xF876, 0xC516);
4753 r8168_mac_ocp_write(tp, 0xF878, 0x74A4);
4754 r8168_mac_ocp_write(tp, 0xF87A, 0x48C8);
4755 r8168_mac_ocp_write(tp, 0xF87C, 0x48CA);
4756 r8168_mac_ocp_write(tp, 0xF87E, 0x9CA4);
4757 r8168_mac_ocp_write(tp, 0xF880, 0xC512);
4758 r8168_mac_ocp_write(tp, 0xF882, 0x1B00);
4759 r8168_mac_ocp_write(tp, 0xF884, 0x9BA0);
4760 r8168_mac_ocp_write(tp, 0xF886, 0x1B1C);
4761 r8168_mac_ocp_write(tp, 0xF888, 0x483F);
4762 r8168_mac_ocp_write(tp, 0xF88A, 0x9BA2);
4763 r8168_mac_ocp_write(tp, 0xF88C, 0x1B04);
4764 r8168_mac_ocp_write(tp, 0xF88E, 0xC508);
4765 r8168_mac_ocp_write(tp, 0xF890, 0x9BA0);
4766 r8168_mac_ocp_write(tp, 0xF892, 0xC505);
4767 r8168_mac_ocp_write(tp, 0xF894, 0xBD00);
4768 r8168_mac_ocp_write(tp, 0xF896, 0xC502);
4769 r8168_mac_ocp_write(tp, 0xF898, 0xBD00);
4770 r8168_mac_ocp_write(tp, 0xF89A, 0x0300);
4771 r8168_mac_ocp_write(tp, 0xF89C, 0x051E);
4772 r8168_mac_ocp_write(tp, 0xF89E, 0xE434);
4773 r8168_mac_ocp_write(tp, 0xF8A0, 0xE018);
4774 r8168_mac_ocp_write(tp, 0xF8A2, 0xE092);
4775 r8168_mac_ocp_write(tp, 0xF8A4, 0xDE20);
4776 r8168_mac_ocp_write(tp, 0xF8A6, 0xD3C0);
4777 r8168_mac_ocp_write(tp, 0xF8A8, 0xC50F);
4778 r8168_mac_ocp_write(tp, 0xF8AA, 0x76A4);
4779 r8168_mac_ocp_write(tp, 0xF8AC, 0x49E3);
4780 r8168_mac_ocp_write(tp, 0xF8AE, 0xF007);
4781 r8168_mac_ocp_write(tp, 0xF8B0, 0x49C0);
4782 r8168_mac_ocp_write(tp, 0xF8B2, 0xF103);
4783 r8168_mac_ocp_write(tp, 0xF8B4, 0xC607);
4784 r8168_mac_ocp_write(tp, 0xF8B6, 0xBE00);
4785 r8168_mac_ocp_write(tp, 0xF8B8, 0xC606);
4786 r8168_mac_ocp_write(tp, 0xF8BA, 0xBE00);
4787 r8168_mac_ocp_write(tp, 0xF8BC, 0xC602);
4788 r8168_mac_ocp_write(tp, 0xF8BE, 0xBE00);
4789 r8168_mac_ocp_write(tp, 0xF8C0, 0x0C4C);
4790 r8168_mac_ocp_write(tp, 0xF8C2, 0x0C28);
4791 r8168_mac_ocp_write(tp, 0xF8C4, 0x0C2C);
4792 r8168_mac_ocp_write(tp, 0xF8C6, 0xDC00);
4793 r8168_mac_ocp_write(tp, 0xF8C8, 0xC707);
4794 r8168_mac_ocp_write(tp, 0xF8CA, 0x1D00);
4795 r8168_mac_ocp_write(tp, 0xF8CC, 0x8DE2);
4796 r8168_mac_ocp_write(tp, 0xF8CE, 0x48C1);
4797 r8168_mac_ocp_write(tp, 0xF8D0, 0xC502);
4798 r8168_mac_ocp_write(tp, 0xF8D2, 0xBD00);
4799 r8168_mac_ocp_write(tp, 0xF8D4, 0x00AA);
4800 r8168_mac_ocp_write(tp, 0xF8D6, 0xE0C0);
4801 r8168_mac_ocp_write(tp, 0xF8D8, 0xC502);
4802 r8168_mac_ocp_write(tp, 0xF8DA, 0xBD00);
4803 r8168_mac_ocp_write(tp, 0xF8DC, 0x0132);
4804
4805 r8168_mac_ocp_write(tp, 0xFC26, 0x8000);
4806
4807 r8168_mac_ocp_write(tp, 0xFC2A, 0x0743);
4808 r8168_mac_ocp_write(tp, 0xFC2C, 0x0801);
4809 r8168_mac_ocp_write(tp, 0xFC2E, 0x0BE9);
4810 r8168_mac_ocp_write(tp, 0xFC30, 0x02FD);
4811 r8168_mac_ocp_write(tp, 0xFC32, 0x0C25);
4812 r8168_mac_ocp_write(tp, 0xFC34, 0x00A9);
4813 r8168_mac_ocp_write(tp, 0xFC36, 0x012D);
4814
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004815 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08004816}
4817
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004818static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
4819{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004820 static const struct ephy_info e_info_8168h_1[] = {
4821 { 0x1e, 0x0800, 0x0001 },
4822 { 0x1d, 0x0000, 0x0800 },
4823 { 0x05, 0xffff, 0x2089 },
4824 { 0x06, 0xffff, 0x5881 },
4825 { 0x04, 0xffff, 0x154a },
4826 { 0x01, 0xffff, 0x068b }
4827 };
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004828 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004829
4830 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004831 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004832 rtl_ephy_init(tp, e_info_8168h_1);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004833
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004834 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004835 rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004836
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004837 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004838
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004839 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004840
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004841 rtl_reset_packet_filter(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004842
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004843 rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_1111, BIT(4));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004844
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004845 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004846
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004847 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004848
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004849 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004850
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004851 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4852 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004853
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004854 rtl8168_config_eee_mac(tp);
4855
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004856 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4857 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004858
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004859 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004860
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004861 rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004862
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004863 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004864
4865 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08004866 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004867 rtl_writephy(tp, 0x1f, 0x0000);
4868 if (rg_saw_cnt > 0) {
4869 u16 sw_cnt_1ms_ini;
4870
4871 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
4872 sw_cnt_1ms_ini &= 0x0fff;
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004873 r8168_mac_ocp_modify(tp, 0xd412, 0x0fff, sw_cnt_1ms_ini);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004874 }
4875
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004876 r8168_mac_ocp_modify(tp, 0xe056, 0x00f0, 0x0070);
4877 r8168_mac_ocp_modify(tp, 0xe052, 0x6000, 0x8008);
4878 r8168_mac_ocp_modify(tp, 0xe0d6, 0x01ff, 0x017f);
4879 r8168_mac_ocp_modify(tp, 0xd420, 0x0fff, 0x047f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004880
4881 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
4882 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
4883 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
4884 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004885
4886 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004887}
4888
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004889static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
4890{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08004891 rtl8168ep_stop_cmac(tp);
4892
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02004893 rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
Heiner Kallweit0ebacd12019-05-05 12:34:25 +02004894 rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004895
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004896 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004897
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004898 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004899
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02004900 rtl_reset_packet_filter(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004901
Heiner Kallweite719b3e2019-04-28 11:11:47 +02004902 rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f80);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004903
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004904 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004905
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004906 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004907
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004908 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4909 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004910
Heiner Kallweitdf6f1852019-01-25 20:38:38 +01004911 rtl8168_config_eee_mac(tp);
4912
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02004913 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004914
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004915 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004916
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01004917 rtl_pcie_state_l2l3_disable(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004918}
4919
4920static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
4921{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004922 static const struct ephy_info e_info_8168ep_1[] = {
4923 { 0x00, 0xffff, 0x10ab },
4924 { 0x06, 0xffff, 0xf030 },
4925 { 0x08, 0xffff, 0x2006 },
4926 { 0x0d, 0xffff, 0x1666 },
4927 { 0x0c, 0x3ff0, 0x0000 }
4928 };
4929
4930 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004931 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004932 rtl_ephy_init(tp, e_info_8168ep_1);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004933
4934 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004935
4936 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004937}
4938
4939static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
4940{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004941 static const struct ephy_info e_info_8168ep_2[] = {
4942 { 0x00, 0xffff, 0x10a3 },
4943 { 0x19, 0xffff, 0xfc00 },
4944 { 0x1e, 0xffff, 0x20ea }
4945 };
4946
4947 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004948 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004949 rtl_ephy_init(tp, e_info_8168ep_2);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004950
4951 rtl_hw_start_8168ep(tp);
4952
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004953 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4954 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004955
4956 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004957}
4958
4959static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
4960{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004961 static const struct ephy_info e_info_8168ep_3[] = {
4962 { 0x00, 0xffff, 0x10a3 },
4963 { 0x19, 0xffff, 0x7c00 },
4964 { 0x1e, 0xffff, 0x20eb },
4965 { 0x0d, 0xffff, 0x1666 }
4966 };
4967
4968 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004969 rtl_hw_aspm_clkreq_enable(tp, false);
Heiner Kallweit1791ad52019-05-04 16:57:49 +02004970 rtl_ephy_init(tp, e_info_8168ep_3);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004971
4972 rtl_hw_start_8168ep(tp);
4973
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004974 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
4975 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004976
Heiner Kallweitef712ed2019-08-04 09:47:51 +02004977 r8168_mac_ocp_modify(tp, 0xd3e2, 0x0fff, 0x0271);
4978 r8168_mac_ocp_modify(tp, 0xd3e4, 0x00ff, 0x0000);
4979 r8168_mac_ocp_modify(tp, 0xe860, 0x0000, 0x0080);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004980
4981 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004982}
4983
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004984static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004985{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004986 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004987 { 0x01, 0, 0x6e65 },
4988 { 0x02, 0, 0x091f },
4989 { 0x03, 0, 0xc2f9 },
4990 { 0x06, 0, 0xafb5 },
4991 { 0x07, 0, 0x0e00 },
4992 { 0x19, 0, 0xec80 },
4993 { 0x01, 0, 0x2e65 },
4994 { 0x01, 0, 0x6e65 }
4995 };
4996 u8 cfg1;
4997
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004998 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004999
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005000 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005001
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005002 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005003
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005004 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02005005 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005006 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005007
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005008 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005009 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005010 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005011
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005012 rtl_ephy_init(tp, e_info_8102e_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005013}
5014
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005015static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005016{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005017 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005018
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005019 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005020
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005021 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5022 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005023}
5024
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005025static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005026{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005027 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005028
Francois Romieufdf6fc02012-07-06 22:40:38 +02005029 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005030}
5031
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005032static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005033{
5034 static const struct ephy_info e_info_8105e_1[] = {
5035 { 0x07, 0, 0x4000 },
5036 { 0x19, 0, 0x0200 },
5037 { 0x19, 0, 0x0020 },
5038 { 0x1e, 0, 0x2000 },
5039 { 0x03, 0, 0x0001 },
5040 { 0x19, 0, 0x0100 },
5041 { 0x19, 0, 0x0004 },
5042 { 0x0a, 0, 0x0020 }
5043 };
5044
Francois Romieucecb5fd2011-04-01 10:21:07 +02005045 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005046 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005047
Francois Romieucecb5fd2011-04-01 10:21:07 +02005048 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005049 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005050
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005051 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5052 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005053
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005054 rtl_ephy_init(tp, e_info_8105e_1);
hayeswangb51ecea2014-07-09 14:52:51 +08005055
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005056 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005057}
5058
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005059static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005060{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005061 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005062 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005063}
5064
Hayes Wang7e18dca2012-03-30 14:33:02 +08005065static void rtl_hw_start_8402(struct rtl8169_private *tp)
5066{
Hayes Wang7e18dca2012-03-30 14:33:02 +08005067 static const struct ephy_info e_info_8402[] = {
5068 { 0x19, 0xffff, 0xff64 },
5069 { 0x1e, 0, 0x4000 }
5070 };
5071
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005072 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005073
5074 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005075 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005076
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005077 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005078
Heiner Kallweit1791ad52019-05-04 16:57:49 +02005079 rtl_ephy_init(tp, e_info_8402);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005080
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005081 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005082
Heiner Kallweit6b1bd242019-05-05 12:33:40 +02005083 rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
Heiner Kallweit4e7e4622019-04-28 11:12:56 +02005084 rtl_reset_packet_filter(tp);
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02005085 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5086 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
5087 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00);
hayeswangb51ecea2014-07-09 14:52:51 +08005088
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005089 rtl_pcie_state_l2l3_disable(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005090}
5091
Hayes Wang5598bfe2012-07-02 17:23:21 +08005092static void rtl_hw_start_8106(struct rtl8169_private *tp)
5093{
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005094 rtl_hw_aspm_clkreq_enable(tp, false);
5095
Hayes Wang5598bfe2012-07-02 17:23:21 +08005096 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005097 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005098
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005099 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5100 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5101 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005102
Heiner Kallweitc259b7f2019-01-19 22:05:14 +01005103 rtl_pcie_state_l2l3_disable(tp);
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005104 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005105}
5106
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005107static void rtl_hw_config(struct rtl8169_private *tp)
5108{
5109 static const rtl_generic_fct hw_configs[] = {
5110 [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
5111 [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
5112 [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
5113 [RTL_GIGA_MAC_VER_10] = NULL,
5114 [RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168bb,
5115 [RTL_GIGA_MAC_VER_12] = rtl_hw_start_8168bef,
5116 [RTL_GIGA_MAC_VER_13] = NULL,
5117 [RTL_GIGA_MAC_VER_14] = NULL,
5118 [RTL_GIGA_MAC_VER_15] = NULL,
5119 [RTL_GIGA_MAC_VER_16] = NULL,
5120 [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168bef,
5121 [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
5122 [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
5123 [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
5124 [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_3,
5125 [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
5126 [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
5127 [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
5128 [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
5129 [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
5130 [RTL_GIGA_MAC_VER_27] = rtl_hw_start_8168d,
5131 [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
5132 [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
5133 [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
5134 [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168dp,
5135 [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
5136 [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
5137 [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
5138 [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
5139 [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
5140 [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
5141 [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
5142 [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
5143 [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
5144 [RTL_GIGA_MAC_VER_41] = rtl_hw_start_8168g_1,
5145 [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
5146 [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
5147 [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
5148 [RTL_GIGA_MAC_VER_45] = rtl_hw_start_8168h_1,
5149 [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
5150 [RTL_GIGA_MAC_VER_47] = rtl_hw_start_8168h_1,
5151 [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
5152 [RTL_GIGA_MAC_VER_49] = rtl_hw_start_8168ep_1,
5153 [RTL_GIGA_MAC_VER_50] = rtl_hw_start_8168ep_2,
5154 [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
5155 };
5156
5157 if (hw_configs[tp->mac_version])
5158 hw_configs[tp->mac_version](tp);
5159}
5160
5161static void rtl_hw_start_8168(struct rtl8169_private *tp)
5162{
Francois Romieucecb5fd2011-04-01 10:21:07 +02005163 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005164 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005165 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005166 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005167
Heiner Kallweit272b2262019-06-14 07:55:21 +02005168 if (rtl_is_8168evl_up(tp))
5169 RTL_W8(tp, MaxTxPacketSize, EarlySize);
5170 else
5171 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005172
Heiner Kallweit8344fff2019-04-14 10:32:07 +02005173 rtl_hw_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174}
5175
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005176static void rtl_hw_start_8169(struct rtl8169_private *tp)
5177{
5178 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
5179 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
5180
5181 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
5182
5183 tp->cp_cmd |= PCIMulRW;
5184
5185 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5186 tp->mac_version == RTL_GIGA_MAC_VER_03) {
5187 netif_dbg(tp, drv, tp->dev,
5188 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
5189 tp->cp_cmd |= (1 << 14);
5190 }
5191
5192 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5193
5194 rtl8169_set_magic_reg(tp, tp->mac_version);
5195
5196 RTL_W32(tp, RxMissed, 0);
5197}
5198
5199static void rtl_hw_start(struct rtl8169_private *tp)
5200{
5201 rtl_unlock_config_regs(tp);
5202
5203 tp->cp_cmd &= CPCMD_MASK;
5204 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5205
5206 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
5207 rtl_hw_start_8169(tp);
5208 else
5209 rtl_hw_start_8168(tp);
5210
5211 rtl_set_rx_max_size(tp);
5212 rtl_set_rx_tx_desc_registers(tp);
5213 rtl_lock_config_regs(tp);
5214
5215 /* disable interrupt coalescing */
5216 RTL_W16(tp, IntrMitigate, 0x0000);
5217 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5218 RTL_R8(tp, IntrMask);
5219 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
5220 rtl_init_rxcfg(tp);
5221 rtl_set_tx_config_registers(tp);
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005222 rtl_set_rx_mode(tp->dev);
Heiner Kallweit6c19156e292019-06-10 18:23:30 +02005223 rtl_irq_enable(tp);
5224}
5225
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5227{
Francois Romieud58d46b2011-05-03 16:38:29 +02005228 struct rtl8169_private *tp = netdev_priv(dev);
5229
Francois Romieud58d46b2011-05-03 16:38:29 +02005230 if (new_mtu > ETH_DATA_LEN)
5231 rtl_hw_jumbo_enable(tp);
5232 else
5233 rtl_hw_jumbo_disable(tp);
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005236 netdev_update_features(dev);
5237
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239}
5240
5241static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5242{
Al Viro95e09182007-12-22 18:55:39 +00005243 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5245}
5246
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005247static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5248 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005250 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5251 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005252
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005253 kfree(*data_buff);
5254 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 rtl8169_make_unusable_by_asic(desc);
5256}
5257
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005258static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259{
5260 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5261
Alexander Duycka0750132014-12-11 15:02:17 -08005262 /* Force memory writes to complete before releasing descriptor */
5263 dma_wmb();
5264
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005265 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266}
5267
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005268static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5269 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005270{
5271 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005273 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02005274 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005276 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005277 if (!data)
5278 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005279
Heiner Kallweit6dc8b742019-01-20 11:12:01 +01005280 /* Memory should be properly aligned, but better check. */
5281 if (!IS_ALIGNED((unsigned long)data, 8)) {
5282 netdev_err_once(tp->dev, "RX buffer not 8-byte-aligned\n");
5283 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005284 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005285
Heiner Kallweit6dc8b742019-01-20 11:12:01 +01005286 mapping = dma_map_single(d, data, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005287 if (unlikely(dma_mapping_error(d, mapping))) {
5288 if (net_ratelimit())
5289 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005290 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
Heiner Kallweitd731af72018-04-17 23:26:41 +02005293 desc->addr = cpu_to_le64(mapping);
5294 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005295 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005296
5297err_out:
5298 kfree(data);
5299 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300}
5301
5302static void rtl8169_rx_clear(struct rtl8169_private *tp)
5303{
Francois Romieu07d3f512007-02-21 22:40:46 +01005304 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
5306 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005307 if (tp->Rx_databuff[i]) {
5308 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 tp->RxDescArray + i);
5310 }
5311 }
5312}
5313
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005314static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005316 desc->opts1 |= cpu_to_le32(RingEnd);
5317}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005318
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005319static int rtl8169_rx_fill(struct rtl8169_private *tp)
5320{
5321 unsigned int i;
5322
5323 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005324 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005325
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005326 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005327 if (!data) {
5328 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005329 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005330 }
5331 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005334 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5335 return 0;
5336
5337err_out:
5338 rtl8169_rx_clear(tp);
5339 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340}
5341
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005342static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 rtl8169_init_ring_indexes(tp);
5345
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005346 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5347 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005349 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350}
5351
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005352static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 struct TxDesc *desc)
5354{
5355 unsigned int len = tx_skb->len;
5356
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005357 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5358
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 desc->opts1 = 0x00;
5360 desc->opts2 = 0x00;
5361 desc->addr = 0x00;
5362 tx_skb->len = 0;
5363}
5364
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005365static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5366 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367{
5368 unsigned int i;
5369
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005370 for (i = 0; i < n; i++) {
5371 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 struct ring_info *tx_skb = tp->tx_skb + entry;
5373 unsigned int len = tx_skb->len;
5374
5375 if (len) {
5376 struct sk_buff *skb = tx_skb->skb;
5377
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005378 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 tp->TxDescArray + entry);
5380 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07005381 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 tx_skb->skb = NULL;
5383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 }
5385 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005386}
5387
5388static void rtl8169_tx_clear(struct rtl8169_private *tp)
5389{
5390 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 tp->cur_tx = tp->dirty_tx = 0;
Florian Westphald92060b2018-10-20 12:25:27 +02005392 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393}
5394
Francois Romieu4422bcd2012-01-26 11:23:32 +01005395static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396{
David Howellsc4028952006-11-22 14:57:56 +00005397 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005398 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
Francois Romieuda78dbf2012-01-26 14:18:23 +01005400 napi_disable(&tp->napi);
5401 netif_stop_queue(dev);
Paul E. McKenney16f11502018-11-05 17:07:39 -08005402 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
françois romieuc7c2c392011-12-04 20:30:52 +00005404 rtl8169_hw_reset(tp);
5405
Francois Romieu56de4142011-03-15 17:29:31 +01005406 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005407 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01005408
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005410 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
Francois Romieuda78dbf2012-01-26 14:18:23 +01005412 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005413 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005414 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415}
5416
5417static void rtl8169_tx_timeout(struct net_device *dev)
5418{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005419 struct rtl8169_private *tp = netdev_priv(dev);
5420
5421 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422}
5423
Heiner Kallweit734c1402018-11-22 21:56:48 +01005424static __le32 rtl8169_get_txd_opts1(u32 opts0, u32 len, unsigned int entry)
5425{
5426 u32 status = opts0 | len;
5427
5428 if (entry == NUM_TX_DESC - 1)
5429 status |= RingEnd;
5430
5431 return cpu_to_le32(status);
5432}
5433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005435 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
5437 struct skb_shared_info *info = skb_shinfo(skb);
5438 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005439 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005440 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
5442 entry = tp->cur_tx;
5443 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005444 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005446 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 void *addr;
5448
5449 entry = (entry + 1) % NUM_TX_DESC;
5450
5451 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005452 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005453 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005454 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005455 if (unlikely(dma_mapping_error(d, mapping))) {
5456 if (net_ratelimit())
5457 netif_err(tp, drv, tp->dev,
5458 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005459 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Heiner Kallweit734c1402018-11-22 21:56:48 +01005462 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005463 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 txd->addr = cpu_to_le64(mapping);
5465
5466 tp->tx_skb[entry].len = len;
5467 }
5468
5469 if (cur_frag) {
5470 tp->tx_skb[entry].skb = skb;
5471 txd->opts1 |= cpu_to_le32(LastFrag);
5472 }
5473
5474 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005475
5476err_out:
5477 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5478 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479}
5480
françois romieub423e9a2013-05-18 01:24:46 +00005481static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5482{
5483 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5484}
5485
hayeswange9746042014-07-11 16:25:58 +08005486/* msdn_giant_send_check()
5487 * According to the document of microsoft, the TCP Pseudo Header excludes the
5488 * packet length for IPv6 TCP large packets.
5489 */
5490static int msdn_giant_send_check(struct sk_buff *skb)
5491{
5492 const struct ipv6hdr *ipv6h;
5493 struct tcphdr *th;
5494 int ret;
5495
5496 ret = skb_cow_head(skb, 0);
5497 if (ret)
5498 return ret;
5499
5500 ipv6h = ipv6_hdr(skb);
5501 th = tcp_hdr(skb);
5502
5503 th->check = 0;
5504 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
5505
5506 return ret;
5507}
5508
Heiner Kallweit87945b62019-05-31 19:55:11 +02005509static void rtl8169_tso_csum_v1(struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510{
Michał Mirosław350fb322011-04-08 06:35:56 +00005511 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Francois Romieu2b7b4312011-04-18 22:53:24 -07005513 if (mss) {
5514 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08005515 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5516 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5517 const struct iphdr *ip = ip_hdr(skb);
5518
5519 if (ip->protocol == IPPROTO_TCP)
5520 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5521 else if (ip->protocol == IPPROTO_UDP)
5522 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5523 else
5524 WARN_ON_ONCE(1);
5525 }
hayeswang5888d3f2014-07-11 16:25:56 +08005526}
5527
5528static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5529 struct sk_buff *skb, u32 *opts)
5530{
hayeswangbdfa4ed2014-07-11 16:25:57 +08005531 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08005532 u32 mss = skb_shinfo(skb)->gso_size;
5533
5534 if (mss) {
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005535 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005536 case htons(ETH_P_IP):
5537 opts[0] |= TD1_GTSENV4;
5538 break;
5539
5540 case htons(ETH_P_IPV6):
5541 if (msdn_giant_send_check(skb))
5542 return false;
5543
5544 opts[0] |= TD1_GTSENV6;
5545 break;
5546
5547 default:
5548 WARN_ON_ONCE(1);
5549 break;
5550 }
5551
hayeswangbdfa4ed2014-07-11 16:25:57 +08005552 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08005553 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005554 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08005555 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005557 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005558 case htons(ETH_P_IP):
5559 opts[1] |= TD1_IPv4_CS;
5560 ip_protocol = ip_hdr(skb)->protocol;
5561 break;
5562
5563 case htons(ETH_P_IPV6):
5564 opts[1] |= TD1_IPv6_CS;
5565 ip_protocol = ipv6_hdr(skb)->nexthdr;
5566 break;
5567
5568 default:
5569 ip_protocol = IPPROTO_RAW;
5570 break;
5571 }
5572
5573 if (ip_protocol == IPPROTO_TCP)
5574 opts[1] |= TD1_TCP_CS;
5575 else if (ip_protocol == IPPROTO_UDP)
5576 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005577 else
5578 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08005579
5580 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00005581 } else {
5582 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08005583 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 }
hayeswang5888d3f2014-07-11 16:25:56 +08005585
françois romieub423e9a2013-05-18 01:24:46 +00005586 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587}
5588
Heiner Kallweit76085c92018-11-22 22:03:08 +01005589static bool rtl_tx_slots_avail(struct rtl8169_private *tp,
5590 unsigned int nr_frags)
5591{
5592 unsigned int slots_avail = tp->dirty_tx + NUM_TX_DESC - tp->cur_tx;
5593
5594 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
5595 return slots_avail > nr_frags;
5596}
5597
Heiner Kallweit87945b62019-05-31 19:55:11 +02005598/* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
5599static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
5600{
5601 switch (tp->mac_version) {
5602 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
5603 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
5604 return false;
5605 default:
5606 return true;
5607 }
5608}
5609
Stephen Hemminger613573252009-08-31 19:50:58 +00005610static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5611 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612{
5613 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005614 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005616 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005618 u32 opts[2], len;
Heiner Kallweitef143582019-07-28 11:25:19 +02005619 bool stop_queue;
5620 bool door_bell;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005621 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005622
Heiner Kallweit76085c92018-11-22 22:03:08 +01005623 if (unlikely(!rtl_tx_slots_avail(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005624 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005625 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 }
5627
5628 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005629 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
Heiner Kallweit355f9482019-06-06 07:49:17 +02005631 opts[1] = rtl8169_tx_vlan_tag(skb);
françois romieub423e9a2013-05-18 01:24:46 +00005632 opts[0] = DescOwn;
5633
Heiner Kallweit87945b62019-05-31 19:55:11 +02005634 if (rtl_chip_supports_csum_v2(tp)) {
Heiner Kallweit96ea7722019-07-26 21:50:34 +02005635 if (!rtl8169_tso_csum_v2(tp, skb, opts))
5636 goto err_dma_0;
Heiner Kallweit87945b62019-05-31 19:55:11 +02005637 } else {
5638 rtl8169_tso_csum_v1(skb, opts);
hayeswange9746042014-07-11 16:25:58 +08005639 }
françois romieub423e9a2013-05-18 01:24:46 +00005640
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005641 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005642 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005643 if (unlikely(dma_mapping_error(d, mapping))) {
5644 if (net_ratelimit())
5645 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005646 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648
5649 tp->tx_skb[entry].len = len;
5650 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
Francois Romieu2b7b4312011-04-18 22:53:24 -07005652 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005653 if (frags < 0)
5654 goto err_dma_1;
5655 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005656 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005657 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005658 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005659 tp->tx_skb[entry].skb = skb;
5660 }
5661
Francois Romieu2b7b4312011-04-18 22:53:24 -07005662 txd->opts2 = cpu_to_le32(opts[1]);
5663
Richard Cochran5047fb52012-03-10 07:29:42 +00005664 skb_tx_timestamp(skb);
5665
Alexander Duycka0750132014-12-11 15:02:17 -08005666 /* Force memory writes to complete before releasing descriptor */
5667 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
Heiner Kallweitef143582019-07-28 11:25:19 +02005669 door_bell = __netdev_sent_queue(dev, skb->len, netdev_xmit_more());
5670
Heiner Kallweit734c1402018-11-22 21:56:48 +01005671 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
Alexander Duycka0750132014-12-11 15:02:17 -08005673 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00005674 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675
Alexander Duycka0750132014-12-11 15:02:17 -08005676 tp->cur_tx += frags + 1;
5677
Heiner Kallweitef143582019-07-28 11:25:19 +02005678 stop_queue = !rtl_tx_slots_avail(tp, MAX_SKB_FRAGS);
5679 if (unlikely(stop_queue)) {
Heiner Kallweit0255d592019-02-10 15:28:04 +01005680 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
5681 * not miss a ring update when it notices a stopped queue.
5682 */
5683 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 netif_stop_queue(dev);
Heiner Kallweitef143582019-07-28 11:25:19 +02005685 }
5686
5687 if (door_bell)
5688 RTL_W8(tp, TxPoll, NPQ);
5689
5690 if (unlikely(stop_queue)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01005691 /* Sync with rtl_tx:
5692 * - publish queue status and cur_tx ring index (write barrier)
5693 * - refresh dirty_tx ring index (read barrier).
5694 * May the current thread have a pessimistic view of the ring
5695 * status and forget to wake up queue, a racing rtl_tx thread
5696 * can't.
5697 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005698 smp_mb();
Heiner Kallweit76085c92018-11-22 22:03:08 +01005699 if (rtl_tx_slots_avail(tp, MAX_SKB_FRAGS))
Heiner Kallweit601ed4d2019-03-21 21:41:48 +01005700 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 }
5702
Stephen Hemminger613573252009-08-31 19:50:58 +00005703 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005705err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005706 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005707err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07005708 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005709 dev->stats.tx_dropped++;
5710 return NETDEV_TX_OK;
5711
5712err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005714 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005715 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
5717
Heiner Kallweite64e0c82019-07-26 21:49:22 +02005718static netdev_features_t rtl8169_features_check(struct sk_buff *skb,
5719 struct net_device *dev,
5720 netdev_features_t features)
5721{
5722 int transport_offset = skb_transport_offset(skb);
5723 struct rtl8169_private *tp = netdev_priv(dev);
5724
5725 if (skb_is_gso(skb)) {
5726 if (transport_offset > GTTCPHO_MAX &&
5727 rtl_chip_supports_csum_v2(tp))
5728 features &= ~NETIF_F_ALL_TSO;
5729 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5730 if (skb->len < ETH_ZLEN) {
5731 switch (tp->mac_version) {
5732 case RTL_GIGA_MAC_VER_11:
5733 case RTL_GIGA_MAC_VER_12:
5734 case RTL_GIGA_MAC_VER_17:
5735 case RTL_GIGA_MAC_VER_34:
5736 features &= ~NETIF_F_CSUM_MASK;
5737 break;
5738 default:
5739 break;
5740 }
5741 }
5742
5743 if (transport_offset > TCPHO_MAX &&
5744 rtl_chip_supports_csum_v2(tp))
5745 features &= ~NETIF_F_CSUM_MASK;
5746 }
5747
5748 return vlan_features_check(skb, features);
5749}
5750
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751static void rtl8169_pcierr_interrupt(struct net_device *dev)
5752{
5753 struct rtl8169_private *tp = netdev_priv(dev);
5754 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 u16 pci_status, pci_cmd;
5756
5757 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5758 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5759
Joe Perchesbf82c182010-02-09 11:49:50 +00005760 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5761 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
5763 /*
5764 * The recovery sequence below admits a very elaborated explanation:
5765 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005766 * - I did not see what else could be done;
5767 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 *
5769 * Feel free to adjust to your needs.
5770 */
Francois Romieua27993f2006-12-18 00:04:19 +01005771 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005772 pci_cmd &= ~PCI_COMMAND_PARITY;
5773 else
5774 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5775
5776 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
5778 pci_write_config_word(pdev, PCI_STATUS,
5779 pci_status & (PCI_STATUS_DETECTED_PARITY |
5780 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5781 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5782
Francois Romieu98ddf982012-01-31 10:47:34 +01005783 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784}
5785
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005786static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
5787 int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788{
Florian Westphald92060b2018-10-20 12:25:27 +02005789 unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 dirty_tx = tp->dirty_tx;
5792 smp_rmb();
5793 tx_left = tp->cur_tx - dirty_tx;
5794
5795 while (tx_left > 0) {
5796 unsigned int entry = dirty_tx % NUM_TX_DESC;
5797 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 u32 status;
5799
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5801 if (status & DescOwn)
5802 break;
5803
Alexander Duycka0750132014-12-11 15:02:17 -08005804 /* This barrier is needed to keep us from reading
5805 * any other fields out of the Tx descriptor until
5806 * we know the status of DescOwn
5807 */
5808 dma_rmb();
5809
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005810 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005811 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 if (status & LastFrag) {
Florian Westphald92060b2018-10-20 12:25:27 +02005813 pkts_compl++;
5814 bytes_compl += tx_skb->skb->len;
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01005815 napi_consume_skb(tx_skb->skb, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 tx_skb->skb = NULL;
5817 }
5818 dirty_tx++;
5819 tx_left--;
5820 }
5821
5822 if (tp->dirty_tx != dirty_tx) {
Florian Westphald92060b2018-10-20 12:25:27 +02005823 netdev_completed_queue(dev, pkts_compl, bytes_compl);
5824
5825 u64_stats_update_begin(&tp->tx_stats.syncp);
5826 tp->tx_stats.packets += pkts_compl;
5827 tp->tx_stats.bytes += bytes_compl;
5828 u64_stats_update_end(&tp->tx_stats.syncp);
5829
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01005831 /* Sync with rtl8169_start_xmit:
5832 * - publish dirty_tx ring index (write barrier)
5833 * - refresh cur_tx ring index and queue status (read barrier)
5834 * May the current thread miss the stopped queue condition,
5835 * a racing xmit thread can only have a right view of the
5836 * ring status.
5837 */
Francois Romieu1e874e02012-01-27 15:05:38 +01005838 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 if (netif_queue_stopped(dev) &&
Heiner Kallweit76085c92018-11-22 22:03:08 +01005840 rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 netif_wake_queue(dev);
5842 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005843 /*
5844 * 8168 hack: TxPoll requests are lost when the Tx packets are
5845 * too close. Let's kick an extra TxPoll request when a burst
5846 * of start_xmit activity is detected (if it is not detected,
5847 * it is slow enough). -- FR
5848 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005849 if (tp->cur_tx != dirty_tx)
5850 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 }
5852}
5853
Francois Romieu126fa4b2005-05-12 20:09:17 -04005854static inline int rtl8169_fragmented_frame(u32 status)
5855{
5856 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5857}
5858
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005859static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 u32 status = opts1 & RxProtoMask;
5862
5863 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005864 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 skb->ip_summed = CHECKSUM_UNNECESSARY;
5866 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005867 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868}
5869
Francois Romieuda78dbf2012-01-26 14:18:23 +01005870static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871{
5872 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005873 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
Timo Teräs9fba0812013-01-15 21:01:24 +00005877 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005879 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 u32 status;
5881
Heiner Kallweit62028062018-04-17 23:30:29 +02005882 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 if (status & DescOwn)
5884 break;
Alexander Duycka0750132014-12-11 15:02:17 -08005885
5886 /* This barrier is needed to keep us from reading
5887 * any other fields out of the Rx descriptor until
5888 * we know the status of DescOwn
5889 */
5890 dma_rmb();
5891
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005892 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005893 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5894 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005895 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005897 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005899 dev->stats.rx_crc_errors++;
Heiner Kallweite9588eb2019-05-25 21:14:39 +02005900 if (status & (RxRUNT | RxCRC) && !(status & RxRWT) &&
5901 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00005902 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02005903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 } else {
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005905 unsigned int pkt_size;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005906 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00005907
5908process_pkt:
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005909 pkt_size = status & GENMASK(13, 0);
Ben Greear79d0c1d2012-02-10 15:04:34 +00005910 if (likely(!(dev->features & NETIF_F_RXFCS)))
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005911 pkt_size -= ETH_FCS_LEN;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005912 /*
5913 * The driver does not support incoming fragmented
5914 * frames. They are seen as a symptom of over-mtu
5915 * sized frames.
5916 */
5917 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005918 dev->stats.rx_dropped++;
5919 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00005920 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005921 }
5922
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005923 dma_sync_single_for_cpu(tp_to_dev(tp),
5924 le64_to_cpu(desc->addr),
5925 pkt_size, DMA_FROM_DEVICE);
5926
5927 skb = napi_alloc_skb(&tp->napi, pkt_size);
5928 if (unlikely(!skb)) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005929 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00005930 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 }
5932
Heiner Kallweitfcd4e602019-07-22 22:01:15 +02005933 prefetch(tp->Rx_databuff[entry]);
5934 skb_copy_to_linear_data(skb, tp->Rx_databuff[entry],
5935 pkt_size);
5936 skb->tail += pkt_size;
5937 skb->len = pkt_size;
5938
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005939 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 skb->protocol = eth_type_trans(skb, dev);
5941
Francois Romieu7a8fc772011-03-01 17:18:33 +01005942 rtl8169_rx_vlan_tag(desc, skb);
5943
françois romieu39174292015-11-11 23:35:18 +01005944 if (skb->pkt_type == PACKET_MULTICAST)
5945 dev->stats.multicast++;
5946
Heiner Kallweit448a2412019-04-03 19:54:12 +02005947 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
Junchang Wang8027aa22012-03-04 23:30:32 +01005949 u64_stats_update_begin(&tp->rx_stats.syncp);
5950 tp->rx_stats.packets++;
5951 tp->rx_stats.bytes += pkt_size;
5952 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 }
françois romieuce11ff52013-01-24 13:30:06 +00005954release_descriptor:
5955 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005956 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 }
5958
5959 count = cur_rx - tp->cur_rx;
5960 tp->cur_rx = cur_rx;
5961
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 return count;
5963}
5964
Francois Romieu07d3f512007-02-21 22:40:46 +01005965static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02005967 struct rtl8169_private *tp = dev_instance;
Heiner Kallweit3ff752c2019-01-19 22:02:40 +01005968 u16 status = RTL_R16(tp, IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969
Heiner Kallweitc8248c62019-03-21 21:23:14 +01005970 if (!tp->irq_enabled || status == 0xffff || !(status & tp->irq_mask))
Heiner Kallweit05bbe552018-08-10 22:38:29 +02005971 return IRQ_NONE;
françois romieu811fd302011-12-04 20:30:45 +00005972
Heiner Kallweit38caff52018-10-18 22:19:28 +02005973 if (unlikely(status & SYSErr)) {
5974 rtl8169_pcierr_interrupt(tp->dev);
5975 goto out;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005976 }
5977
Heiner Kallweit703732f2019-01-19 22:07:05 +01005978 if (status & LinkChg)
5979 phy_mac_interrupt(tp->phydev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01005980
Heiner Kallweit38caff52018-10-18 22:19:28 +02005981 if (unlikely(status & RxFIFOOver &&
5982 tp->mac_version == RTL_GIGA_MAC_VER_11)) {
5983 netif_stop_queue(tp->dev);
5984 /* XXX - Hack alert. See rtl_task(). */
5985 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
5986 }
5987
Heiner Kallweite62b2fd2019-04-14 11:48:39 +02005988 rtl_irq_disable(tp);
5989 napi_schedule_irqoff(&tp->napi);
Heiner Kallweit38caff52018-10-18 22:19:28 +02005990out:
5991 rtl_ack_events(tp, status);
5992
5993 return IRQ_HANDLED;
Francois Romieuda78dbf2012-01-26 14:18:23 +01005994}
5995
Francois Romieu4422bcd2012-01-26 11:23:32 +01005996static void rtl_task(struct work_struct *work)
5997{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005998 static const struct {
5999 int bitnr;
6000 void (*action)(struct rtl8169_private *);
6001 } rtl_work[] = {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006002 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01006003 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006004 struct rtl8169_private *tp =
6005 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006006 struct net_device *dev = tp->dev;
6007 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006008
Francois Romieuda78dbf2012-01-26 14:18:23 +01006009 rtl_lock_work(tp);
6010
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006011 if (!netif_running(dev) ||
6012 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006013 goto out_unlock;
6014
6015 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6016 bool pending;
6017
Francois Romieuda78dbf2012-01-26 14:18:23 +01006018 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006019 if (pending)
6020 rtl_work[i].action(tp);
6021 }
6022
6023out_unlock:
6024 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006025}
6026
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006027static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006029 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6030 struct net_device *dev = tp->dev;
Heiner Kallweit6b839b62018-10-18 19:56:01 +02006031 int work_done;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006032
Heiner Kallweit6b839b62018-10-18 19:56:01 +02006033 work_done = rtl_rx(dev, tp, (u32) budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006034
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01006035 rtl_tx(dev, tp, budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006036
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006037 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08006038 napi_complete_done(napi, work_done);
Heiner Kallweitfe716f82018-11-19 22:31:32 +01006039 rtl_irq_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 }
6041
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006042 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006045static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02006046{
6047 struct rtl8169_private *tp = netdev_priv(dev);
6048
6049 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6050 return;
6051
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006052 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6053 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02006054}
6055
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006056static void r8169_phylink_handler(struct net_device *ndev)
6057{
6058 struct rtl8169_private *tp = netdev_priv(ndev);
6059
6060 if (netif_carrier_ok(ndev)) {
6061 rtl_link_chg_patch(tp);
6062 pm_request_resume(&tp->pci_dev->dev);
6063 } else {
6064 pm_runtime_idle(&tp->pci_dev->dev);
6065 }
6066
6067 if (net_ratelimit())
Heiner Kallweit703732f2019-01-19 22:07:05 +01006068 phy_print_status(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006069}
6070
6071static int r8169_phy_connect(struct rtl8169_private *tp)
6072{
Heiner Kallweit703732f2019-01-19 22:07:05 +01006073 struct phy_device *phydev = tp->phydev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006074 phy_interface_t phy_mode;
6075 int ret;
6076
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02006077 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006078 PHY_INTERFACE_MODE_MII;
6079
6080 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6081 phy_mode);
6082 if (ret)
6083 return ret;
6084
Heiner Kallweita6851c62019-05-28 18:43:46 +02006085 if (tp->supports_gmii)
6086 phy_remove_link_mode(phydev,
6087 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
6088 else
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006089 phy_set_max_speed(phydev, SPEED_100);
6090
Heiner Kallweit9cf9b842019-05-04 12:01:03 +02006091 phy_support_asym_pause(phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006092
6093 phy_attached_info(phydev);
6094
6095 return 0;
6096}
6097
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098static void rtl8169_down(struct net_device *dev)
6099{
6100 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
Heiner Kallweit703732f2019-01-19 22:07:05 +01006102 phy_stop(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006103
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006104 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006105 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106
Hayes Wang92fc43b2011-07-06 15:58:03 +08006107 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006108 /*
6109 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006110 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6111 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006112 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006113 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenney16f11502018-11-05 17:07:39 -08006116 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 rtl8169_tx_clear(tp);
6119
6120 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006121
6122 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123}
6124
6125static int rtl8169_close(struct net_device *dev)
6126{
6127 struct rtl8169_private *tp = netdev_priv(dev);
6128 struct pci_dev *pdev = tp->pci_dev;
6129
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006130 pm_runtime_get_sync(&pdev->dev);
6131
Francois Romieucecb5fd2011-04-01 10:21:07 +02006132 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006133 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006134
Francois Romieuda78dbf2012-01-26 14:18:23 +01006135 rtl_lock_work(tp);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006136 /* Clear all task flags */
6137 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006138
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006140 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
Lekensteyn4ea72442013-07-22 09:53:30 +02006142 cancel_work_sync(&tp->wk.work);
6143
Heiner Kallweit703732f2019-01-19 22:07:05 +01006144 phy_disconnect(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006145
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006146 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006148 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6149 tp->RxPhyAddr);
6150 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6151 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 tp->TxDescArray = NULL;
6153 tp->RxDescArray = NULL;
6154
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006155 pm_runtime_put_sync(&pdev->dev);
6156
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 return 0;
6158}
6159
Francois Romieudc1c00c2012-03-08 10:06:18 +01006160#ifdef CONFIG_NET_POLL_CONTROLLER
6161static void rtl8169_netpoll(struct net_device *dev)
6162{
6163 struct rtl8169_private *tp = netdev_priv(dev);
6164
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03006165 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01006166}
6167#endif
6168
Francois Romieudf43ac72012-03-08 09:48:40 +01006169static int rtl_open(struct net_device *dev)
6170{
6171 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006172 struct pci_dev *pdev = tp->pci_dev;
6173 int retval = -ENOMEM;
6174
6175 pm_runtime_get_sync(&pdev->dev);
6176
6177 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006178 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006179 * dma_alloc_coherent provides more.
6180 */
6181 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6182 &tp->TxPhyAddr, GFP_KERNEL);
6183 if (!tp->TxDescArray)
6184 goto err_pm_runtime_put;
6185
6186 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6187 &tp->RxPhyAddr, GFP_KERNEL);
6188 if (!tp->RxDescArray)
6189 goto err_free_tx_0;
6190
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006191 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006192 if (retval < 0)
6193 goto err_free_rx_1;
6194
Francois Romieudf43ac72012-03-08 09:48:40 +01006195 rtl_request_firmware(tp);
6196
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006197 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006198 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01006199 if (retval < 0)
6200 goto err_release_fw_2;
6201
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006202 retval = r8169_phy_connect(tp);
6203 if (retval)
6204 goto err_free_irq;
6205
Francois Romieudf43ac72012-03-08 09:48:40 +01006206 rtl_lock_work(tp);
6207
6208 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6209
6210 napi_enable(&tp->napi);
6211
6212 rtl8169_init_phy(dev, tp);
6213
Francois Romieudf43ac72012-03-08 09:48:40 +01006214 rtl_pll_power_up(tp);
6215
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006216 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006217
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006218 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006219 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6220
Heiner Kallweit703732f2019-01-19 22:07:05 +01006221 phy_start(tp->phydev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006222 netif_start_queue(dev);
6223
6224 rtl_unlock_work(tp);
6225
Heiner Kallweita92a0842018-01-08 21:39:13 +01006226 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006227out:
6228 return retval;
6229
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006230err_free_irq:
6231 pci_free_irq(pdev, 0, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006232err_release_fw_2:
6233 rtl_release_firmware(tp);
6234 rtl8169_rx_clear(tp);
6235err_free_rx_1:
6236 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6237 tp->RxPhyAddr);
6238 tp->RxDescArray = NULL;
6239err_free_tx_0:
6240 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6241 tp->TxPhyAddr);
6242 tp->TxDescArray = NULL;
6243err_pm_runtime_put:
6244 pm_runtime_put_noidle(&pdev->dev);
6245 goto out;
6246}
6247
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006248static void
Junchang Wang8027aa22012-03-04 23:30:32 +01006249rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250{
6251 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006252 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02006253 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01006254 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006256 pm_runtime_get_noresume(&pdev->dev);
6257
6258 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006259 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006260
Junchang Wang8027aa22012-03-04 23:30:32 +01006261 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006262 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006263 stats->rx_packets = tp->rx_stats.packets;
6264 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006265 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006266
Junchang Wang8027aa22012-03-04 23:30:32 +01006267 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006268 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006269 stats->tx_packets = tp->tx_stats.packets;
6270 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006271 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006272
6273 stats->rx_dropped = dev->stats.rx_dropped;
6274 stats->tx_dropped = dev->stats.tx_dropped;
6275 stats->rx_length_errors = dev->stats.rx_length_errors;
6276 stats->rx_errors = dev->stats.rx_errors;
6277 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6278 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6279 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02006280 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01006281
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006282 /*
Corentin Musarded72a9b2019-07-24 14:34:43 +02006283 * Fetch additional counter values missing in stats collected by driver
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006284 * from tally counters.
6285 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006286 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006287 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006288
6289 /*
6290 * Subtract values fetched during initalization.
6291 * See rtl8169_init_counter_offsets for a description why we do that.
6292 */
Corinna Vinschen42020322015-09-10 10:47:35 +02006293 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006294 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02006295 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006296 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02006297 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006298 le16_to_cpu(tp->tc_offset.tx_aborted);
6299
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006300 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301}
6302
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006303static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006304{
françois romieu065c27c2011-01-03 15:08:12 +00006305 struct rtl8169_private *tp = netdev_priv(dev);
6306
Francois Romieu5d06a992006-02-23 00:47:58 +01006307 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006308 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006309
Heiner Kallweit703732f2019-01-19 22:07:05 +01006310 phy_stop(tp->phydev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006311 netif_device_detach(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006312
6313 rtl_lock_work(tp);
6314 napi_disable(&tp->napi);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006315 /* Clear all task flags */
6316 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6317
Francois Romieuda78dbf2012-01-26 14:18:23 +01006318 rtl_unlock_work(tp);
6319
6320 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006321}
Francois Romieu5d06a992006-02-23 00:47:58 +01006322
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006323#ifdef CONFIG_PM
6324
6325static int rtl8169_suspend(struct device *device)
6326{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006327 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006328 struct rtl8169_private *tp = netdev_priv(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006329
6330 rtl8169_net_suspend(dev);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006331 clk_disable_unprepare(tp->clk);
Francois Romieu1371fa62007-04-02 23:01:11 +02006332
Francois Romieu5d06a992006-02-23 00:47:58 +01006333 return 0;
6334}
6335
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006336static void __rtl8169_resume(struct net_device *dev)
6337{
françois romieu065c27c2011-01-03 15:08:12 +00006338 struct rtl8169_private *tp = netdev_priv(dev);
6339
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006340 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006341
6342 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02006343 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00006344
Heiner Kallweit703732f2019-01-19 22:07:05 +01006345 phy_start(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006346
Artem Savkovcff4c162012-04-03 10:29:11 +00006347 rtl_lock_work(tp);
6348 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006349 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Heiner Kallweit58ba5662019-01-19 22:06:25 +01006350 rtl_reset_work(tp);
Artem Savkovcff4c162012-04-03 10:29:11 +00006351 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006352}
6353
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006354static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006355{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006356 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006357 struct rtl8169_private *tp = netdev_priv(dev);
6358
Heiner Kallweit59715172019-05-29 07:44:01 +02006359 rtl_rar_set(tp, dev->dev_addr);
6360
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006361 clk_prepare_enable(tp->clk);
Francois Romieu5d06a992006-02-23 00:47:58 +01006362
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006363 if (netif_running(dev))
6364 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006365
Francois Romieu5d06a992006-02-23 00:47:58 +01006366 return 0;
6367}
6368
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006369static int rtl8169_runtime_suspend(struct device *device)
6370{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006371 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006372 struct rtl8169_private *tp = netdev_priv(dev);
6373
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006374 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006375 return 0;
6376
Francois Romieuda78dbf2012-01-26 14:18:23 +01006377 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006378 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006379 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006380
6381 rtl8169_net_suspend(dev);
6382
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006383 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006384 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006385 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006386
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006387 return 0;
6388}
6389
6390static int rtl8169_runtime_resume(struct device *device)
6391{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006392 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006393 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit59715172019-05-29 07:44:01 +02006394
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08006395 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006396
6397 if (!tp->TxDescArray)
6398 return 0;
6399
Francois Romieuda78dbf2012-01-26 14:18:23 +01006400 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006401 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006402 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006403
6404 __rtl8169_resume(dev);
6405
6406 return 0;
6407}
6408
6409static int rtl8169_runtime_idle(struct device *device)
6410{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006411 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006412
Heiner Kallweita92a0842018-01-08 21:39:13 +01006413 if (!netif_running(dev) || !netif_carrier_ok(dev))
6414 pm_schedule_suspend(device, 10000);
6415
6416 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006417}
6418
Alexey Dobriyan47145212009-12-14 18:00:08 -08006419static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006420 .suspend = rtl8169_suspend,
6421 .resume = rtl8169_resume,
6422 .freeze = rtl8169_suspend,
6423 .thaw = rtl8169_resume,
6424 .poweroff = rtl8169_suspend,
6425 .restore = rtl8169_resume,
6426 .runtime_suspend = rtl8169_runtime_suspend,
6427 .runtime_resume = rtl8169_runtime_resume,
6428 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006429};
6430
6431#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6432
6433#else /* !CONFIG_PM */
6434
6435#define RTL8169_PM_OPS NULL
6436
6437#endif /* !CONFIG_PM */
6438
David S. Miller1805b2f2011-10-24 18:18:09 -04006439static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6440{
David S. Miller1805b2f2011-10-24 18:18:09 -04006441 /* WoL fails with 8168b when the receiver is disabled. */
6442 switch (tp->mac_version) {
6443 case RTL_GIGA_MAC_VER_11:
6444 case RTL_GIGA_MAC_VER_12:
6445 case RTL_GIGA_MAC_VER_17:
6446 pci_clear_master(tp->pci_dev);
6447
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006448 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006449 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006450 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04006451 break;
6452 default:
6453 break;
6454 }
6455}
6456
Francois Romieu1765f952008-09-13 17:21:40 +02006457static void rtl_shutdown(struct pci_dev *pdev)
6458{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006459 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006460 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006461
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006462 rtl8169_net_suspend(dev);
6463
Francois Romieucecb5fd2011-04-01 10:21:07 +02006464 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006465 rtl_rar_set(tp, dev->perm_addr);
6466
Hayes Wang92fc43b2011-07-06 15:58:03 +08006467 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006468
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006469 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02006470 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006471 rtl_wol_suspend_quirk(tp);
6472 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006473 }
6474
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006475 pci_wake_from_d3(pdev, true);
6476 pci_set_power_state(pdev, PCI_D3hot);
6477 }
6478}
Francois Romieu5d06a992006-02-23 00:47:58 +01006479
Bill Pembertonbaf63292012-12-03 09:23:28 -05006480static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006481{
6482 struct net_device *dev = pci_get_drvdata(pdev);
6483 struct rtl8169_private *tp = netdev_priv(dev);
6484
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006485 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01006486 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01006487
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006488 netif_napi_del(&tp->napi);
6489
Francois Romieue27566e2012-03-08 09:54:01 +01006490 unregister_netdev(dev);
Heiner Kallweit703732f2019-01-19 22:07:05 +01006491 mdiobus_unregister(tp->phydev->mdio.bus);
Francois Romieue27566e2012-03-08 09:54:01 +01006492
6493 rtl_release_firmware(tp);
6494
6495 if (pci_dev_run_wake(pdev))
6496 pm_runtime_get_noresume(&pdev->dev);
6497
6498 /* restore original MAC address */
6499 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01006500}
6501
Francois Romieufa9c3852012-03-08 10:01:50 +01006502static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006503 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006504 .ndo_stop = rtl8169_close,
6505 .ndo_get_stats64 = rtl8169_get_stats64,
6506 .ndo_start_xmit = rtl8169_start_xmit,
Heiner Kallweite64e0c82019-07-26 21:49:22 +02006507 .ndo_features_check = rtl8169_features_check,
Francois Romieufa9c3852012-03-08 10:01:50 +01006508 .ndo_tx_timeout = rtl8169_tx_timeout,
6509 .ndo_validate_addr = eth_validate_addr,
6510 .ndo_change_mtu = rtl8169_change_mtu,
6511 .ndo_fix_features = rtl8169_fix_features,
6512 .ndo_set_features = rtl8169_set_features,
6513 .ndo_set_mac_address = rtl_set_mac_address,
6514 .ndo_do_ioctl = rtl8169_ioctl,
6515 .ndo_set_rx_mode = rtl_set_rx_mode,
6516#ifdef CONFIG_NET_POLL_CONTROLLER
6517 .ndo_poll_controller = rtl8169_netpoll,
6518#endif
6519
6520};
6521
Heiner Kallweitec9a4082019-06-10 18:21:50 +02006522static void rtl_set_irq_mask(struct rtl8169_private *tp)
6523{
6524 tp->irq_mask = RTL_EVENT_NAPI | LinkChg;
6525
6526 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
6527 tp->irq_mask |= SYSErr | RxOverflow | RxFIFOOver;
6528 else if (tp->mac_version == RTL_GIGA_MAC_VER_11)
6529 /* special workaround needed */
6530 tp->irq_mask |= RxFIFOOver;
6531 else
6532 tp->irq_mask |= RxOverflow;
6533}
6534
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006535static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01006536{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006537 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006538
Jian-Hong Pand49c88d2018-09-27 12:09:48 +08006539 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006540 rtl_unlock_config_regs(tp);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006541 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
Heiner Kallweitdf320ed2019-01-19 22:05:48 +01006542 rtl_lock_config_regs(tp);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006543 flags = PCI_IRQ_LEGACY;
Jian-Hong Pand49c88d2018-09-27 12:09:48 +08006544 } else {
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006545 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006546 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006547
6548 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01006549}
6550
Thierry Reding04c77882019-02-06 13:30:17 +01006551static void rtl_read_mac_address(struct rtl8169_private *tp,
6552 u8 mac_addr[ETH_ALEN])
6553{
6554 /* Get MAC address */
Heiner Kallweit9e9f33b2019-06-14 07:54:07 +02006555 if (rtl_is_8168evl_up(tp) && tp->mac_version != RTL_GIGA_MAC_VER_34) {
6556 u32 value = rtl_eri_read(tp, 0xe0);
6557
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006558 mac_addr[0] = (value >> 0) & 0xff;
6559 mac_addr[1] = (value >> 8) & 0xff;
6560 mac_addr[2] = (value >> 16) & 0xff;
6561 mac_addr[3] = (value >> 24) & 0xff;
6562
Heiner Kallweit724c6fd2019-04-28 11:10:50 +02006563 value = rtl_eri_read(tp, 0xe4);
Thierry Redingdeedf1f2019-02-06 13:30:18 +01006564 mac_addr[4] = (value >> 0) & 0xff;
6565 mac_addr[5] = (value >> 8) & 0xff;
Thierry Reding04c77882019-02-06 13:30:17 +01006566 }
6567}
6568
Hayes Wangc5583862012-07-02 17:23:22 +08006569DECLARE_RTL_COND(rtl_link_list_ready_cond)
6570{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006571 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08006572}
6573
6574DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6575{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006576 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08006577}
6578
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006579static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
6580{
6581 struct rtl8169_private *tp = mii_bus->priv;
6582
6583 if (phyaddr > 0)
6584 return -ENODEV;
6585
6586 return rtl_readphy(tp, phyreg);
6587}
6588
6589static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
6590 int phyreg, u16 val)
6591{
6592 struct rtl8169_private *tp = mii_bus->priv;
6593
6594 if (phyaddr > 0)
6595 return -ENODEV;
6596
6597 rtl_writephy(tp, phyreg, val);
6598
6599 return 0;
6600}
6601
6602static int r8169_mdio_register(struct rtl8169_private *tp)
6603{
6604 struct pci_dev *pdev = tp->pci_dev;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006605 struct mii_bus *new_bus;
6606 int ret;
6607
6608 new_bus = devm_mdiobus_alloc(&pdev->dev);
6609 if (!new_bus)
6610 return -ENOMEM;
6611
6612 new_bus->name = "r8169";
6613 new_bus->priv = tp;
6614 new_bus->parent = &pdev->dev;
6615 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
Heiner Kallweita1950162019-04-24 21:13:25 +02006616 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x", pci_dev_id(pdev));
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006617
6618 new_bus->read = r8169_mdio_read_reg;
6619 new_bus->write = r8169_mdio_write_reg;
6620
6621 ret = mdiobus_register(new_bus);
6622 if (ret)
6623 return ret;
6624
Heiner Kallweit703732f2019-01-19 22:07:05 +01006625 tp->phydev = mdiobus_get_phy(new_bus, 0);
6626 if (!tp->phydev) {
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006627 mdiobus_unregister(new_bus);
6628 return -ENODEV;
6629 }
6630
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02006631 /* PHY will be woken up in rtl_open() */
Heiner Kallweit703732f2019-01-19 22:07:05 +01006632 phy_suspend(tp->phydev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006633
6634 return 0;
6635}
6636
Bill Pembertonbaf63292012-12-03 09:23:28 -05006637static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006638{
Hayes Wangc5583862012-07-02 17:23:22 +08006639 tp->ocp_base = OCP_STD_PHY_BASE;
6640
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006641 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08006642
6643 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6644 return;
6645
6646 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6647 return;
6648
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006649 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08006650 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006651 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08006652
Heiner Kallweitef712ed2019-08-04 09:47:51 +02006653 r8168_mac_ocp_modify(tp, 0xe8de, BIT(14), 0);
Hayes Wangc5583862012-07-02 17:23:22 +08006654
6655 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6656 return;
6657
Heiner Kallweitef712ed2019-08-04 09:47:51 +02006658 r8168_mac_ocp_modify(tp, 0xe8de, 0, BIT(15));
Hayes Wangc5583862012-07-02 17:23:22 +08006659
Heiner Kallweit7160be22019-05-25 20:44:01 +02006660 rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
Hayes Wangc5583862012-07-02 17:23:22 +08006661}
6662
Bill Pembertonbaf63292012-12-03 09:23:28 -05006663static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08006664{
6665 switch (tp->mac_version) {
Heiner Kallweit29ec0482019-05-25 20:43:25 +02006666 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
6667 rtl8168ep_stop_cmac(tp);
6668 /* fall through */
Heiner Kallweit2a718832018-05-02 21:39:49 +02006669 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08006670 rtl_hw_init_8168g(tp);
6671 break;
Hayes Wangc5583862012-07-02 17:23:22 +08006672 default:
6673 break;
6674 }
6675}
6676
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006677static int rtl_jumbo_max(struct rtl8169_private *tp)
6678{
6679 /* Non-GBit versions don't support jumbo frames */
6680 if (!tp->supports_gmii)
6681 return JUMBO_1K;
6682
6683 switch (tp->mac_version) {
6684 /* RTL8169 */
Heiner Kallweite9588eb2019-05-25 21:14:39 +02006685 case RTL_GIGA_MAC_VER_02 ... RTL_GIGA_MAC_VER_06:
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006686 return JUMBO_7K;
6687 /* RTL8168b */
6688 case RTL_GIGA_MAC_VER_11:
6689 case RTL_GIGA_MAC_VER_12:
6690 case RTL_GIGA_MAC_VER_17:
6691 return JUMBO_4K;
6692 /* RTL8168c */
6693 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
6694 return JUMBO_6K;
6695 default:
6696 return JUMBO_9K;
6697 }
6698}
6699
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006700static void rtl_disable_clk(void *data)
6701{
6702 clk_disable_unprepare(data);
6703}
6704
Heiner Kallweitb779dae2019-01-19 22:07:34 +01006705static int rtl_get_ether_clk(struct rtl8169_private *tp)
6706{
6707 struct device *d = tp_to_dev(tp);
6708 struct clk *clk;
6709 int rc;
6710
6711 clk = devm_clk_get(d, "ether_clk");
6712 if (IS_ERR(clk)) {
6713 rc = PTR_ERR(clk);
6714 if (rc == -ENOENT)
6715 /* clk-core allows NULL (for suspend / resume) */
6716 rc = 0;
6717 else if (rc != -EPROBE_DEFER)
6718 dev_err(d, "failed to get clk: %d\n", rc);
6719 } else {
6720 tp->clk = clk;
6721 rc = clk_prepare_enable(clk);
6722 if (rc)
6723 dev_err(d, "failed to enable clk: %d\n", rc);
6724 else
6725 rc = devm_add_action_or_reset(d, rtl_disable_clk, clk);
6726 }
6727
6728 return rc;
6729}
6730
Heiner Kallweitc782e202019-07-02 20:46:09 +02006731static void rtl_init_mac_address(struct rtl8169_private *tp)
6732{
6733 struct net_device *dev = tp->dev;
6734 u8 *mac_addr = dev->dev_addr;
6735 int rc, i;
6736
6737 rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr);
6738 if (!rc)
6739 goto done;
6740
6741 rtl_read_mac_address(tp, mac_addr);
6742 if (is_valid_ether_addr(mac_addr))
6743 goto done;
6744
6745 for (i = 0; i < ETH_ALEN; i++)
6746 mac_addr[i] = RTL_R8(tp, MAC0 + i);
6747 if (is_valid_ether_addr(mac_addr))
6748 goto done;
6749
6750 eth_hw_addr_random(dev);
6751 dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n");
6752done:
6753 rtl_rar_set(tp, mac_addr);
6754}
6755
hayeswang929a0312014-09-16 11:40:47 +08006756static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01006757{
Francois Romieu3b6cf252012-03-08 09:59:04 +01006758 struct rtl8169_private *tp;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006759 struct net_device *dev;
Heiner Kallweitc782e202019-07-02 20:46:09 +02006760 int chipset, region;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006761 int jumbo_max, rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006762
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006763 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
6764 if (!dev)
6765 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006766
6767 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01006768 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006769 tp = netdev_priv(dev);
6770 tp->dev = dev;
6771 tp->pci_dev = pdev;
6772 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Heiner Kallweit145a40e2019-06-10 18:25:29 +02006773 tp->supports_gmii = ent->driver_data == RTL_CFG_NO_GBIT ? 0 : 1;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006774
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006775 /* Get the *optional* external "ether_clk" used on some boards */
Heiner Kallweitb779dae2019-01-19 22:07:34 +01006776 rc = rtl_get_ether_clk(tp);
6777 if (rc)
6778 return rc;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02006779
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +02006780 /* Disable ASPM completely as that cause random device stop working
6781 * problems as well as full system hangs for some PCIe devices users.
6782 */
Heiner Kallweit62b1b3b2019-06-18 23:14:50 +02006783 rc = pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
6784 PCIE_LINK_STATE_L1);
6785 tp->aspm_manageable = !rc;
Heiner Kallweitb75bb8a2019-04-05 20:46:46 +02006786
Francois Romieu3b6cf252012-03-08 09:59:04 +01006787 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006788 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006789 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006790 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006791 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006792 }
6793
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006794 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02006795 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01006796
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02006797 /* use first MMIO region */
6798 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
6799 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006800 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006801 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006802 }
6803
6804 /* check for weird/broken PCI region reporting */
6805 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006806 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006807 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006808 }
6809
Andy Shevchenko93a00d42018-03-01 13:27:35 +02006810 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006811 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006812 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006813 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006814 }
6815
Andy Shevchenko93a00d42018-03-01 13:27:35 +02006816 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01006817
Francois Romieu3b6cf252012-03-08 09:59:04 +01006818 /* Identify chip attached to board */
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01006819 rtl8169_get_mac_version(tp);
6820 if (tp->mac_version == RTL_GIGA_MAC_NONE)
6821 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006822
Heiner Kallweit0ae09742018-04-28 22:19:26 +02006823 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006824
Heiner Kallweit10b63e82019-01-20 11:45:20 +01006825 if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
Heiner Kallweit3c18cbe2019-06-27 23:12:39 +02006826 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)))
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006827 dev->features |= NETIF_F_HIGHDMA;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02006828
Francois Romieu3b6cf252012-03-08 09:59:04 +01006829 rtl_init_rxcfg(tp);
6830
Heiner Kallweitde20e122018-09-25 07:58:00 +02006831 rtl8169_irq_mask_and_ack(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006832
Hayes Wangc5583862012-07-02 17:23:22 +08006833 rtl_hw_initialize(tp);
6834
Francois Romieu3b6cf252012-03-08 09:59:04 +01006835 rtl_hw_reset(tp);
6836
Francois Romieu3b6cf252012-03-08 09:59:04 +01006837 pci_set_master(pdev);
6838
Francois Romieu3b6cf252012-03-08 09:59:04 +01006839 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006840
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006841 rc = rtl_alloc_irq(tp);
6842 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02006843 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006844 return rc;
6845 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01006846
Francois Romieu3b6cf252012-03-08 09:59:04 +01006847 mutex_init(&tp->wk.mutex);
Heiner Kallweit5c41e782019-01-19 22:03:49 +01006848 INIT_WORK(&tp->wk.work, rtl_task);
Kyle McMartin340fea32014-02-24 20:12:28 -05006849 u64_stats_init(&tp->rx_stats.syncp);
6850 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006851
Heiner Kallweitc782e202019-07-02 20:46:09 +02006852 rtl_init_mac_address(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006853
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00006854 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006855
Heiner Kallweit37621492018-04-17 23:20:03 +02006856 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006857
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006858 dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6859 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6860 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006861 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00006862 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
6863 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006864 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6865 NETIF_F_HIGHDMA;
Heiner Kallweit2d0ec542018-07-02 22:49:35 +02006866 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006867
hayeswang929a0312014-09-16 11:40:47 +08006868 tp->cp_cmd |= RxChkSum | RxVlan;
6869
6870 /*
6871 * Pretend we are using VLANs; This bypasses a nasty bug where
6872 * Interrupts stop flowing on high load on 8110SCd controllers.
6873 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01006874 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08006875 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00006876 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006877
Heiner Kallweit0170d592019-07-26 21:48:32 +02006878 if (rtl_chip_supports_csum_v2(tp)) {
hayeswange9746042014-07-11 16:25:58 +08006879 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006880 dev->features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweit0170d592019-07-26 21:48:32 +02006881 dev->gso_max_size = RTL_GSO_MAX_SIZE_V2;
6882 dev->gso_max_segs = RTL_GSO_MAX_SEGS_V2;
6883 } else {
6884 dev->gso_max_size = RTL_GSO_MAX_SIZE_V1;
6885 dev->gso_max_segs = RTL_GSO_MAX_SEGS_V1;
6886 }
hayeswang5888d3f2014-07-11 16:25:56 +08006887
Heiner Kallweit93681cd2019-07-26 21:51:36 +02006888 /* RTL8168e-vl has a HW issue with TSO */
6889 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
6890 dev->vlan_features &= ~NETIF_F_ALL_TSO;
6891 dev->hw_features &= ~NETIF_F_ALL_TSO;
6892 dev->features &= ~NETIF_F_ALL_TSO;
6893 }
6894
Francois Romieu3b6cf252012-03-08 09:59:04 +01006895 dev->hw_features |= NETIF_F_RXALL;
6896 dev->hw_features |= NETIF_F_RXFCS;
6897
Jarod Wilsonc7315a92016-10-17 15:54:09 -04006898 /* MTU range: 60 - hw-specific max */
6899 dev->min_mtu = ETH_ZLEN;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006900 jumbo_max = rtl_jumbo_max(tp);
6901 dev->max_mtu = jumbo_max;
Jarod Wilsonc7315a92016-10-17 15:54:09 -04006902
Heiner Kallweitec9a4082019-06-10 18:21:50 +02006903 rtl_set_irq_mask(tp);
Heiner Kallweit9fa0a8e2019-06-10 18:24:25 +02006904
Heiner Kallweit254764e2019-01-22 22:23:41 +01006905 tp->fw_name = rtl_chip_infos[chipset].fw_name;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006906
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006907 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
6908 &tp->counters_phys_addr,
6909 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01006910 if (!tp->counters)
6911 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02006912
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02006913 pci_set_drvdata(pdev, dev);
6914
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006915 rc = r8169_mdio_register(tp);
6916 if (rc)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01006917 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006918
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006919 /* chip gets powered up in rtl_open() */
6920 rtl_pll_power_down(tp);
6921
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006922 rc = register_netdev(dev);
6923 if (rc)
6924 goto err_mdio_unregister;
6925
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01006926 netif_info(tp, probe, dev, "%s, %pM, XID %03x, IRQ %d\n",
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02006927 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01006928 (RTL_R32(tp, TxConfig) >> 20) & 0xfcf,
Heiner Kallweit29274992018-02-28 20:43:38 +01006929 pci_irq_vector(pdev, 0));
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02006930
6931 if (jumbo_max > JUMBO_1K)
6932 netif_info(tp, probe, dev,
6933 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
6934 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
6935 "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01006936
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006937 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01006938 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01006939
Heiner Kallweita92a0842018-01-08 21:39:13 +01006940 if (pci_dev_run_wake(pdev))
6941 pm_runtime_put_sync(&pdev->dev);
6942
Heiner Kallweit4c45d242017-12-12 07:41:02 +01006943 return 0;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006944
6945err_mdio_unregister:
Heiner Kallweit703732f2019-01-19 22:07:05 +01006946 mdiobus_unregister(tp->phydev->mdio.bus);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006947 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01006948}
6949
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950static struct pci_driver rtl8169_pci_driver = {
6951 .name = MODULENAME,
6952 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01006953 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05006954 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02006955 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006956 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957};
6958
Devendra Naga3eeb7da2012-10-26 09:27:42 +00006959module_pci_driver(rtl8169_pci_driver);