blob: 7fe9180261b880f9f5cc7fd2a7b842798aaa5bf1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
Hans de Goedec2f6f3e2018-09-12 11:34:55 +020016#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/delay.h>
18#include <linux/ethtool.h>
Heiner Kallweitf1e911d2018-07-17 22:51:26 +020019#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/if_vlan.h>
21#include <linux/crc32.h>
22#include <linux/in.h>
Heiner Kallweit098b01a2018-08-10 22:37:31 +020023#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/ip.h>
25#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000026#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000028#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000029#include <linux/firmware.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080031#include <linux/ipv6.h>
32#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define MODULENAME "r8169"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
françois romieubca03d52011-01-03 15:07:31 +000036#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
37#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000038#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
39#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080040#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080041#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
42#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080043#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080044#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080045#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080046#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080047#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000048#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000049#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000050#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080051#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
52#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
53#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
54#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000055
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020056#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070057 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
60 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050061static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Michal Schmidtaee77e42012-09-09 13:55:26 +000063#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
65
66#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020067#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000069#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
71#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +020074#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
75#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
76#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
77#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
78#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
79#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +020082 RTL_GIGA_MAC_VER_01 = 0,
83 RTL_GIGA_MAC_VER_02,
84 RTL_GIGA_MAC_VER_03,
85 RTL_GIGA_MAC_VER_04,
86 RTL_GIGA_MAC_VER_05,
87 RTL_GIGA_MAC_VER_06,
88 RTL_GIGA_MAC_VER_07,
89 RTL_GIGA_MAC_VER_08,
90 RTL_GIGA_MAC_VER_09,
91 RTL_GIGA_MAC_VER_10,
92 RTL_GIGA_MAC_VER_11,
93 RTL_GIGA_MAC_VER_12,
94 RTL_GIGA_MAC_VER_13,
95 RTL_GIGA_MAC_VER_14,
96 RTL_GIGA_MAC_VER_15,
97 RTL_GIGA_MAC_VER_16,
98 RTL_GIGA_MAC_VER_17,
99 RTL_GIGA_MAC_VER_18,
100 RTL_GIGA_MAC_VER_19,
101 RTL_GIGA_MAC_VER_20,
102 RTL_GIGA_MAC_VER_21,
103 RTL_GIGA_MAC_VER_22,
104 RTL_GIGA_MAC_VER_23,
105 RTL_GIGA_MAC_VER_24,
106 RTL_GIGA_MAC_VER_25,
107 RTL_GIGA_MAC_VER_26,
108 RTL_GIGA_MAC_VER_27,
109 RTL_GIGA_MAC_VER_28,
110 RTL_GIGA_MAC_VER_29,
111 RTL_GIGA_MAC_VER_30,
112 RTL_GIGA_MAC_VER_31,
113 RTL_GIGA_MAC_VER_32,
114 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800115 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800116 RTL_GIGA_MAC_VER_35,
117 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800118 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800119 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800120 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800121 RTL_GIGA_MAC_VER_40,
122 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000123 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000124 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800125 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800126 RTL_GIGA_MAC_VER_45,
127 RTL_GIGA_MAC_VER_46,
128 RTL_GIGA_MAC_VER_47,
129 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800130 RTL_GIGA_MAC_VER_49,
131 RTL_GIGA_MAC_VER_50,
132 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200133 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Francois Romieud58d46b2011-05-03 16:38:29 +0200136#define JUMBO_1K ETH_DATA_LEN
137#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
138#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
139#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
140#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
141
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800142static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 const char *name;
Francois Romieu85bffe62011-04-27 08:22:39 +0200144 const char *fw_name;
145} rtl_chip_infos[] = {
146 /* PCI devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200147 [RTL_GIGA_MAC_VER_01] = {"RTL8169" },
148 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
149 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
150 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
151 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
152 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
Francois Romieu85bffe62011-04-27 08:22:39 +0200153 /* PCI-E devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200154 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
155 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
156 [RTL_GIGA_MAC_VER_09] = {"RTL8102e" },
157 [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
158 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
159 [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
160 [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
161 [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
162 [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
163 [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
164 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
165 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
166 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
167 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
168 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
169 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
170 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
171 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
172 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
173 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
174 [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
175 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
176 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
177 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
178 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
179 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
180 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
181 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
182 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
183 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
184 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
185 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
186 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
187 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
188 [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
189 [RTL_GIGA_MAC_VER_42] = {"RTL8168g/8111g", FIRMWARE_8168G_3},
190 [RTL_GIGA_MAC_VER_43] = {"RTL8106e", FIRMWARE_8106E_2},
191 [RTL_GIGA_MAC_VER_44] = {"RTL8411", FIRMWARE_8411_2 },
192 [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
193 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
194 [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
195 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
196 [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
197 [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
198 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Francois Romieubcf0bf92006-07-26 23:14:13 +0200201enum cfg_version {
202 RTL_CFG_0 = 0x00,
203 RTL_CFG_1,
204 RTL_CFG_2
205};
206
Benoit Taine9baa3c32014-08-08 15:56:03 +0200207static const struct pci_device_id rtl8169_pci_tbl[] = {
Kai-Heng Feng36352992019-01-02 14:45:07 +0800208 { PCI_VDEVICE(REALTEK, 0x2502), RTL_CFG_1 },
209 { PCI_VDEVICE(REALTEK, 0x2600), RTL_CFG_1 },
Heiner Kallweit6f0d3082018-11-19 22:35:08 +0100210 { PCI_VDEVICE(REALTEK, 0x8129), RTL_CFG_0 },
211 { PCI_VDEVICE(REALTEK, 0x8136), RTL_CFG_2 },
212 { PCI_VDEVICE(REALTEK, 0x8161), RTL_CFG_1 },
213 { PCI_VDEVICE(REALTEK, 0x8167), RTL_CFG_0 },
214 { PCI_VDEVICE(REALTEK, 0x8168), RTL_CFG_1 },
215 { PCI_VDEVICE(NCUBE, 0x8168), RTL_CFG_1 },
216 { PCI_VDEVICE(REALTEK, 0x8169), RTL_CFG_0 },
217 { PCI_VENDOR_ID_DLINK, 0x4300,
218 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
219 { PCI_VDEVICE(DLINK, 0x4300), RTL_CFG_0 },
220 { PCI_VDEVICE(DLINK, 0x4302), RTL_CFG_0 },
221 { PCI_VDEVICE(AT, 0xc107), RTL_CFG_0 },
222 { PCI_VDEVICE(USR, 0x0116), RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200223 { PCI_VENDOR_ID_LINKSYS, 0x1032,
224 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100225 { 0x0001, 0x8168,
226 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Heiner Kallweit6f0d3082018-11-19 22:35:08 +0100227 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
229
230MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
231
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200232static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200233static struct {
234 u32 msg_enable;
235} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Francois Romieu07d3f512007-02-21 22:40:46 +0100237enum rtl_registers {
238 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100239 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100240 MAR0 = 8, /* Multicast filter. */
241 CounterAddrLow = 0x10,
242 CounterAddrHigh = 0x14,
243 TxDescStartAddrLow = 0x20,
244 TxDescStartAddrHigh = 0x24,
245 TxHDescStartAddrLow = 0x28,
246 TxHDescStartAddrHigh = 0x2c,
247 FLASH = 0x30,
248 ERSR = 0x36,
249 ChipCmd = 0x37,
250 TxPoll = 0x38,
251 IntrMask = 0x3c,
252 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700253
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800254 TxConfig = 0x40,
255#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
256#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
257
258 RxConfig = 0x44,
259#define RX128_INT_EN (1 << 15) /* 8111c and later */
260#define RX_MULTI_EN (1 << 14) /* 8111c only */
261#define RXCFG_FIFO_SHIFT 13
262 /* No threshold before first PCI xfer */
263#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000264#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800265#define RXCFG_DMA_SHIFT 8
266 /* Unlimited maximum PCI burst. */
267#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700268
Francois Romieu07d3f512007-02-21 22:40:46 +0100269 RxMissed = 0x4c,
270 Cfg9346 = 0x50,
271 Config0 = 0x51,
272 Config1 = 0x52,
273 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200274#define PME_SIGNAL (1 << 5) /* 8168c and later */
275
Francois Romieu07d3f512007-02-21 22:40:46 +0100276 Config3 = 0x54,
277 Config4 = 0x55,
278 Config5 = 0x56,
279 MultiIntr = 0x5c,
280 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100281 PHYstatus = 0x6c,
282 RxMaxSize = 0xda,
283 CPlusCmd = 0xe0,
284 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300285
286#define RTL_COALESCE_MASK 0x0f
287#define RTL_COALESCE_SHIFT 4
288#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
289#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
290
Francois Romieu07d3f512007-02-21 22:40:46 +0100291 RxDescAddrLow = 0xe4,
292 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000293 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
294
295#define NoEarlyTx 0x3f /* Max value : no early transmit. */
296
297 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
298
299#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800300#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000301
Francois Romieu07d3f512007-02-21 22:40:46 +0100302 FuncEvent = 0xf0,
303 FuncEventMask = 0xf4,
304 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800305 IBCR0 = 0xf8,
306 IBCR2 = 0xf9,
307 IBIMR0 = 0xfa,
308 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100309 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
Francois Romieuf162a5d2008-06-01 22:37:49 +0200312enum rtl8168_8101_registers {
313 CSIDR = 0x64,
314 CSIAR = 0x68,
315#define CSIAR_FLAG 0x80000000
316#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200317#define CSIAR_BYTE_ENABLE 0x0000f000
318#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000319 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200320 EPHYAR = 0x80,
321#define EPHYAR_FLAG 0x80000000
322#define EPHYAR_WRITE_CMD 0x80000000
323#define EPHYAR_REG_MASK 0x1f
324#define EPHYAR_REG_SHIFT 16
325#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800326 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800327#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800328#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200329 DBG_REG = 0xd1,
330#define FIX_NAK_1 (1 << 4)
331#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800332 TWSI = 0xd2,
333 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800334#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800335#define TX_EMPTY (1 << 5)
336#define RX_EMPTY (1 << 4)
337#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800338#define EN_NDP (1 << 3)
339#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800340#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000341 EFUSEAR = 0xdc,
342#define EFUSEAR_FLAG 0x80000000
343#define EFUSEAR_WRITE_CMD 0x80000000
344#define EFUSEAR_READ_CMD 0x00000000
345#define EFUSEAR_REG_MASK 0x03ff
346#define EFUSEAR_REG_SHIFT 8
347#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800348 MISC_1 = 0xf2,
349#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200350};
351
françois romieuc0e45c12011-01-03 15:08:04 +0000352enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800353 LED_FREQ = 0x1a,
354 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000355 ERIDR = 0x70,
356 ERIAR = 0x74,
357#define ERIAR_FLAG 0x80000000
358#define ERIAR_WRITE_CMD 0x80000000
359#define ERIAR_READ_CMD 0x00000000
360#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000361#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800362#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
363#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
364#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800365#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800366#define ERIAR_MASK_SHIFT 12
367#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
368#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800369#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800370#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000372 EPHY_RXER_NUM = 0x7c,
373 OCPDR = 0xb0, /* OCP GPHY access */
374#define OCPDR_WRITE_CMD 0x80000000
375#define OCPDR_READ_CMD 0x00000000
376#define OCPDR_REG_MASK 0x7f
377#define OCPDR_GPHY_REG_SHIFT 16
378#define OCPDR_DATA_MASK 0xffff
379 OCPAR = 0xb4,
380#define OCPAR_FLAG 0x80000000
381#define OCPAR_GPHY_WRITE_CMD 0x8000f060
382#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800383 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000384 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
385 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200386#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800387#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800388#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800389#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800390#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000391};
392
Francois Romieu07d3f512007-02-21 22:40:46 +0100393enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100395 SYSErr = 0x8000,
396 PCSTimeout = 0x4000,
397 SWInt = 0x0100,
398 TxDescUnavail = 0x0080,
399 RxFIFOOver = 0x0040,
400 LinkChg = 0x0020,
401 RxOverflow = 0x0010,
402 TxErr = 0x0008,
403 TxOK = 0x0004,
404 RxErr = 0x0002,
405 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400408 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200409 RxFOVF = (1 << 23),
410 RxRWT = (1 << 22),
411 RxRES = (1 << 21),
412 RxRUNT = (1 << 20),
413 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800416 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100417 CmdReset = 0x10,
418 CmdRxEnb = 0x08,
419 CmdTxEnb = 0x04,
420 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Francois Romieu275391a2007-02-23 23:50:28 +0100422 /* TXPoll register p.5 */
423 HPQ = 0x80, /* Poll cmd on the high prio queue */
424 NPQ = 0x40, /* Poll cmd on the low prio queue */
425 FSWInt = 0x01, /* Forced software interrupt */
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100428 Cfg9346_Lock = 0x00,
429 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100432 AcceptErr = 0x20,
433 AcceptRunt = 0x10,
434 AcceptBroadcast = 0x08,
435 AcceptMulticast = 0x04,
436 AcceptMyPhys = 0x02,
437 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200438#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* TxConfigBits */
441 TxInterFrameGapShift = 24,
442 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
443
Francois Romieu5d06a992006-02-23 00:47:58 +0100444 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200445 LEDS1 = (1 << 7),
446 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200447 Speed_down = (1 << 4),
448 MEMMAP = (1 << 3),
449 IOMAP = (1 << 2),
450 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100451 PMEnable = (1 << 0), /* Power Management Enable */
452
Francois Romieu6dccd162007-02-13 23:38:05 +0100453 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000454 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000455 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100456 PCI_Clock_66MHz = 0x01,
457 PCI_Clock_33MHz = 0x00,
458
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100459 /* Config3 register p.25 */
460 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
461 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200462 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800463 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200464 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100465
Francois Romieud58d46b2011-05-03 16:38:29 +0200466 /* Config4 register */
467 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
468
Francois Romieu5d06a992006-02-23 00:47:58 +0100469 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100470 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
471 MWF = (1 << 5), /* Accept Multicast wakeup frame */
472 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200473 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100474 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100475 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000476 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200479 EnableBist = (1 << 15), // 8168 8101
480 Mac_dbgo_oe = (1 << 14), // 8168 8101
481 Normal_mode = (1 << 13), // unused
482 Force_half_dup = (1 << 12), // 8168 8101
483 Force_rxflow_en = (1 << 11), // 8168 8101
484 Force_txflow_en = (1 << 10), // 8168 8101
485 Cxpl_dbg_sel = (1 << 9), // 8168 8101
486 ASF = (1 << 8), // 8168 8101
487 PktCntrDisable = (1 << 7), // 8168 8101
488 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 RxVlan = (1 << 6),
490 RxChkSum = (1 << 5),
491 PCIDAC = (1 << 4),
492 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200493#define INTT_MASK GENMASK(1, 0)
Francois Romieu0e485152007-02-20 00:00:26 +0100494 INTT_0 = 0x0000, // 8168
495 INTT_1 = 0x0001, // 8168
496 INTT_2 = 0x0002, // 8168
497 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100500 TBI_Enable = 0x80,
501 TxFlowCtrl = 0x40,
502 RxFlowCtrl = 0x20,
503 _1000bpsF = 0x10,
504 _100bps = 0x08,
505 _10bps = 0x04,
506 LinkStatus = 0x02,
507 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100510 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200511
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200512 /* ResetCounterCommand */
513 CounterReset = 0x1,
514
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200515 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100516 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800517
518 /* magic enable v2 */
519 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520};
521
Francois Romieu2b7b4312011-04-18 22:53:24 -0700522enum rtl_desc_bit {
523 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
525 RingEnd = (1 << 30), /* End of descriptor ring */
526 FirstFrag = (1 << 29), /* First segment of a packet */
527 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700528};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Francois Romieu2b7b4312011-04-18 22:53:24 -0700530/* Generic case. */
531enum rtl_tx_desc_bit {
532 /* First doubleword. */
533 TD_LSO = (1 << 27), /* Large Send Offload */
534#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Francois Romieu2b7b4312011-04-18 22:53:24 -0700536 /* Second doubleword. */
537 TxVlanTag = (1 << 17), /* Add VLAN tag */
538};
539
540/* 8169, 8168b and 810x except 8102e. */
541enum rtl_tx_desc_bit_0 {
542 /* First doubleword. */
543#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
544 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
545 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
546 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
547};
548
549/* 8102e, 8168c and beyond. */
550enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800551 /* First doubleword. */
552 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800553 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800554#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800555#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800556
Francois Romieu2b7b4312011-04-18 22:53:24 -0700557 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800558#define TCPHO_SHIFT 18
559#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700560#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800561 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
562 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700563 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
564 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
565};
566
Francois Romieu2b7b4312011-04-18 22:53:24 -0700567enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Rx private */
569 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500570 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572#define RxProtoUDP (PID1)
573#define RxProtoTCP (PID0)
574#define RxProtoIP (PID1 | PID0)
575#define RxProtoMask RxProtoIP
576
577 IPFail = (1 << 16), /* IP checksum failed */
578 UDPFail = (1 << 15), /* UDP/IP checksum failed */
579 TCPFail = (1 << 14), /* TCP/IP checksum failed */
580 RxVlanTag = (1 << 16), /* VLAN tag available */
581};
582
583#define RsvdMask 0x3fffc000
Heiner Kallweit12d42c52018-04-28 22:19:30 +0200584#define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200587 __le32 opts1;
588 __le32 opts2;
589 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590};
591
592struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200593 __le32 opts1;
594 __le32 opts2;
595 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596};
597
598struct ring_info {
599 struct sk_buff *skb;
600 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601};
602
Ivan Vecera355423d2009-02-06 21:49:57 -0800603struct rtl8169_counters {
604 __le64 tx_packets;
605 __le64 rx_packets;
606 __le64 tx_errors;
607 __le32 rx_errors;
608 __le16 rx_missed;
609 __le16 align_errors;
610 __le32 tx_one_collision;
611 __le32 tx_multi_collision;
612 __le64 rx_unicast;
613 __le64 rx_broadcast;
614 __le32 rx_multicast;
615 __le16 tx_aborted;
616 __le16 tx_underun;
617};
618
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200619struct rtl8169_tc_offsets {
620 bool inited;
621 __le64 tx_errors;
622 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200623 __le16 tx_aborted;
624};
625
Francois Romieuda78dbf2012-01-26 14:18:23 +0100626enum rtl_flag {
Kai-Heng Feng6ad56902018-09-11 01:51:43 +0800627 RTL_FLAG_TASK_ENABLED = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100628 RTL_FLAG_TASK_RESET_PENDING,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100629 RTL_FLAG_MAX
630};
631
Junchang Wang8027aa22012-03-04 23:30:32 +0100632struct rtl8169_stats {
633 u64 packets;
634 u64 bytes;
635 struct u64_stats_sync syncp;
636};
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638struct rtl8169_private {
639 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200640 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000641 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700642 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200643 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700644 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
646 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100648 struct rtl8169_stats rx_stats;
649 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
651 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
652 dma_addr_t TxPhyAddr;
653 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000654 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100657
Heiner Kallweit559c3c02018-11-19 22:34:17 +0100658 u16 irq_mask;
Francois Romieu50970832017-10-27 13:24:49 +0300659 const struct rtl_coalesce_info *coalesce_info;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +0200660 struct clk *clk;
françois romieuc0e45c12011-01-03 15:08:04 +0000661
662 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200663 void (*write)(struct rtl8169_private *, int, int);
664 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000665 } mdio_ops;
666
Francois Romieud58d46b2011-05-03 16:38:29 +0200667 struct jumbo_ops {
668 void (*enable)(struct rtl8169_private *);
669 void (*disable)(struct rtl8169_private *);
670 } jumbo_ops;
671
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200672 void (*hw_start)(struct rtl8169_private *tp);
hayeswang5888d3f2014-07-11 16:25:56 +0800673 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100674
675 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100676 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
677 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100678 struct work_struct work;
679 } wk;
680
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +0200681 unsigned supports_gmii:1;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +0200682 struct mii_bus *mii_bus;
Corinna Vinschen42020322015-09-10 10:47:35 +0200683 dma_addr_t counters_phys_addr;
684 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200685 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000686 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000687
Francois Romieub6ffd972011-06-17 17:00:05 +0200688 struct rtl_fw {
689 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200690
691#define RTL_VER_SIZE 32
692
693 char version[RTL_VER_SIZE];
694
695 struct rtl_fw_phy_action {
696 __le32 *code;
697 size_t size;
698 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200699 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300700#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800701
702 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703};
704
Ralf Baechle979b6c12005-06-13 14:30:40 -0700705MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700708MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200709module_param_named(debug, debug.msg_enable, int, 0);
710MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711MODULE_LICENSE("GPL");
françois romieubca03d52011-01-03 15:07:31 +0000712MODULE_FIRMWARE(FIRMWARE_8168D_1);
713MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000714MODULE_FIRMWARE(FIRMWARE_8168E_1);
715MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400716MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800717MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800718MODULE_FIRMWARE(FIRMWARE_8168F_1);
719MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800720MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800721MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800722MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800723MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000724MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000725MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000726MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800727MODULE_FIRMWARE(FIRMWARE_8168H_1);
728MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200729MODULE_FIRMWARE(FIRMWARE_8107E_1);
730MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100732static inline struct device *tp_to_dev(struct rtl8169_private *tp)
733{
734 return &tp->pci_dev->dev;
735}
736
Francois Romieuda78dbf2012-01-26 14:18:23 +0100737static void rtl_lock_work(struct rtl8169_private *tp)
738{
739 mutex_lock(&tp->wk.mutex);
740}
741
742static void rtl_unlock_work(struct rtl8169_private *tp)
743{
744 mutex_unlock(&tp->wk.mutex);
745}
746
Heiner Kallweitcb732002018-03-20 07:45:35 +0100747static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200748{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100749 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800750 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200751}
752
Francois Romieuffc46952012-07-06 14:19:23 +0200753struct rtl_cond {
754 bool (*check)(struct rtl8169_private *);
755 const char *msg;
756};
757
758static void rtl_udelay(unsigned int d)
759{
760 udelay(d);
761}
762
763static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
764 void (*delay)(unsigned int), unsigned int d, int n,
765 bool high)
766{
767 int i;
768
769 for (i = 0; i < n; i++) {
770 delay(d);
771 if (c->check(tp) == high)
772 return true;
773 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200774 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
775 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200776 return false;
777}
778
779static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
780 const struct rtl_cond *c,
781 unsigned int d, int n)
782{
783 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
784}
785
786static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
787 const struct rtl_cond *c,
788 unsigned int d, int n)
789{
790 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
791}
792
793static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
794 const struct rtl_cond *c,
795 unsigned int d, int n)
796{
797 return rtl_loop_wait(tp, c, msleep, d, n, true);
798}
799
800static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
801 const struct rtl_cond *c,
802 unsigned int d, int n)
803{
804 return rtl_loop_wait(tp, c, msleep, d, n, false);
805}
806
807#define DECLARE_RTL_COND(name) \
808static bool name ## _check(struct rtl8169_private *); \
809 \
810static const struct rtl_cond name = { \
811 .check = name ## _check, \
812 .msg = #name \
813}; \
814 \
815static bool name ## _check(struct rtl8169_private *tp)
816
Hayes Wangc5583862012-07-02 17:23:22 +0800817static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
818{
819 if (reg & 0xffff0001) {
820 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
821 return true;
822 }
823 return false;
824}
825
826DECLARE_RTL_COND(rtl_ocp_gphy_cond)
827{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200828 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800829}
830
831static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
832{
Hayes Wangc5583862012-07-02 17:23:22 +0800833 if (rtl_ocp_reg_failure(tp, reg))
834 return;
835
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200836 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800837
838 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
839}
840
841static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
842{
Hayes Wangc5583862012-07-02 17:23:22 +0800843 if (rtl_ocp_reg_failure(tp, reg))
844 return 0;
845
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200846 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800847
848 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200849 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +0800850}
851
Hayes Wangc5583862012-07-02 17:23:22 +0800852static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
853{
Hayes Wangc5583862012-07-02 17:23:22 +0800854 if (rtl_ocp_reg_failure(tp, reg))
855 return;
856
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200857 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800858}
859
860static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
861{
Hayes Wangc5583862012-07-02 17:23:22 +0800862 if (rtl_ocp_reg_failure(tp, reg))
863 return 0;
864
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200865 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800866
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200867 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800868}
869
870#define OCP_STD_PHY_BASE 0xa400
871
872static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
873{
874 if (reg == 0x1f) {
875 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
876 return;
877 }
878
879 if (tp->ocp_base != OCP_STD_PHY_BASE)
880 reg -= 0x10;
881
882 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
883}
884
885static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
886{
887 if (tp->ocp_base != OCP_STD_PHY_BASE)
888 reg -= 0x10;
889
890 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
891}
892
hayeswangeee37862013-04-01 22:23:38 +0000893static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
894{
895 if (reg == 0x1f) {
896 tp->ocp_base = value << 4;
897 return;
898 }
899
900 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
901}
902
903static int mac_mcu_read(struct rtl8169_private *tp, int reg)
904{
905 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
906}
907
Francois Romieuffc46952012-07-06 14:19:23 +0200908DECLARE_RTL_COND(rtl_phyar_cond)
909{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200910 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200911}
912
Francois Romieu24192212012-07-06 20:19:42 +0200913static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200915 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Francois Romieuffc46952012-07-06 14:19:23 +0200917 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700918 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700919 * According to hardware specs a 20us delay is required after write
920 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700921 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700922 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Francois Romieu24192212012-07-06 20:19:42 +0200925static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Francois Romieuffc46952012-07-06 14:19:23 +0200927 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200929 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Francois Romieuffc46952012-07-06 14:19:23 +0200931 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200932 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +0200933
Timo Teräs81a95f02010-06-09 17:31:48 -0700934 /*
935 * According to hardware specs a 20us delay is required after read
936 * complete indication, but before sending next command.
937 */
938 udelay(20);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return value;
941}
942
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800943DECLARE_RTL_COND(rtl_ocpar_cond)
944{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200945 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800946}
947
Francois Romieu24192212012-07-06 20:19:42 +0200948static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000949{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200950 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
951 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
952 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000953
Francois Romieuffc46952012-07-06 14:19:23 +0200954 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +0000955}
956
Francois Romieu24192212012-07-06 20:19:42 +0200957static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000958{
Francois Romieu24192212012-07-06 20:19:42 +0200959 r8168dp_1_mdio_access(tp, reg,
960 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000961}
962
Francois Romieu24192212012-07-06 20:19:42 +0200963static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000964{
Francois Romieu24192212012-07-06 20:19:42 +0200965 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +0000966
967 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200968 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
969 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000970
Francois Romieuffc46952012-07-06 14:19:23 +0200971 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200972 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +0000973}
974
françois romieue6de30d2011-01-03 15:08:37 +0000975#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
976
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200977static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000978{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200979 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000980}
981
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200982static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000983{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200984 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000985}
986
Francois Romieu24192212012-07-06 20:19:42 +0200987static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +0000988{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200989 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000990
Francois Romieu24192212012-07-06 20:19:42 +0200991 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +0000992
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200993 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000994}
995
Francois Romieu24192212012-07-06 20:19:42 +0200996static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +0000997{
998 int value;
999
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001000 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001001
Francois Romieu24192212012-07-06 20:19:42 +02001002 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001003
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001004 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001005
1006 return value;
1007}
1008
françois romieu4da19632011-01-03 15:07:55 +00001009static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001010{
Francois Romieu24192212012-07-06 20:19:42 +02001011 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001012}
1013
françois romieu4da19632011-01-03 15:07:55 +00001014static int rtl_readphy(struct rtl8169_private *tp, int location)
1015{
Francois Romieu24192212012-07-06 20:19:42 +02001016 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001017}
1018
1019static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1020{
1021 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1022}
1023
Chun-Hao Lin76564422014-10-01 23:17:17 +08001024static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001025{
1026 int val;
1027
françois romieu4da19632011-01-03 15:07:55 +00001028 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001029 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001030}
1031
Francois Romieuffc46952012-07-06 14:19:23 +02001032DECLARE_RTL_COND(rtl_ephyar_cond)
1033{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001034 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001035}
1036
Francois Romieufdf6fc02012-07-06 22:40:38 +02001037static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001038{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001039 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001040 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1041
Francois Romieuffc46952012-07-06 14:19:23 +02001042 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1043
1044 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001045}
1046
Francois Romieufdf6fc02012-07-06 22:40:38 +02001047static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001048{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001049 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001050
Francois Romieuffc46952012-07-06 14:19:23 +02001051 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001052 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001053}
1054
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001055DECLARE_RTL_COND(rtl_eriar_cond)
1056{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001057 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001058}
1059
Francois Romieufdf6fc02012-07-06 22:40:38 +02001060static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1061 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001062{
Hayes Wang133ac402011-07-06 15:58:05 +08001063 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001064 RTL_W32(tp, ERIDR, val);
1065 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001066
Francois Romieuffc46952012-07-06 14:19:23 +02001067 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001068}
1069
Francois Romieufdf6fc02012-07-06 22:40:38 +02001070static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001071{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001072 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001073
Francois Romieuffc46952012-07-06 14:19:23 +02001074 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001075 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001076}
1077
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001078static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001079 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001080{
1081 u32 val;
1082
Francois Romieufdf6fc02012-07-06 22:40:38 +02001083 val = rtl_eri_read(tp, addr, type);
1084 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001085}
1086
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001087static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1088{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001089 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001090 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001091 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001092}
1093
1094static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1095{
1096 return rtl_eri_read(tp, reg, ERIAR_OOB);
1097}
1098
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001099static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1100 u32 data)
1101{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001102 RTL_W32(tp, OCPDR, data);
1103 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001104 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1105}
1106
1107static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1108 u32 data)
1109{
1110 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1111 data, ERIAR_OOB);
1112}
1113
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001114static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001115{
1116 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1117
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001118 r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001119}
1120
1121#define OOB_CMD_RESET 0x00
1122#define OOB_CMD_DRIVER_START 0x05
1123#define OOB_CMD_DRIVER_STOP 0x06
1124
1125static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1126{
1127 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1128}
1129
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001130DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001131{
1132 u16 reg;
1133
1134 reg = rtl8168_get_ocp_reg(tp);
1135
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001136 return r8168dp_ocp_read(tp, 0x0f, reg) & 0x00000800;
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001137}
1138
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001139DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1140{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001141 return r8168ep_ocp_read(tp, 0x0f, 0x124) & 0x00000001;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001142}
1143
1144DECLARE_RTL_COND(rtl_ocp_tx_cond)
1145{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001146 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001147}
1148
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001149static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1150{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001151 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001152 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001153 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1154 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001155}
1156
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001157static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001158{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001159 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1160 rtl_msleep_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001161}
1162
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001163static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1164{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001165 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1166 r8168ep_ocp_write(tp, 0x01, 0x30,
1167 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001168 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1169}
1170
1171static void rtl8168_driver_start(struct rtl8169_private *tp)
1172{
1173 switch (tp->mac_version) {
1174 case RTL_GIGA_MAC_VER_27:
1175 case RTL_GIGA_MAC_VER_28:
1176 case RTL_GIGA_MAC_VER_31:
1177 rtl8168dp_driver_start(tp);
1178 break;
1179 case RTL_GIGA_MAC_VER_49:
1180 case RTL_GIGA_MAC_VER_50:
1181 case RTL_GIGA_MAC_VER_51:
1182 rtl8168ep_driver_start(tp);
1183 break;
1184 default:
1185 BUG();
1186 break;
1187 }
1188}
1189
1190static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1191{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001192 r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1193 rtl_msleep_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10, 10);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001194}
1195
1196static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1197{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001198 rtl8168ep_stop_cmac(tp);
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001199 r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1200 r8168ep_ocp_write(tp, 0x01, 0x30,
1201 r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001202 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1203}
1204
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001205static void rtl8168_driver_stop(struct rtl8169_private *tp)
1206{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001207 switch (tp->mac_version) {
1208 case RTL_GIGA_MAC_VER_27:
1209 case RTL_GIGA_MAC_VER_28:
1210 case RTL_GIGA_MAC_VER_31:
1211 rtl8168dp_driver_stop(tp);
1212 break;
1213 case RTL_GIGA_MAC_VER_49:
1214 case RTL_GIGA_MAC_VER_50:
1215 case RTL_GIGA_MAC_VER_51:
1216 rtl8168ep_driver_stop(tp);
1217 break;
1218 default:
1219 BUG();
1220 break;
1221 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001222}
1223
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001224static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001225{
1226 u16 reg = rtl8168_get_ocp_reg(tp);
1227
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001228 return !!(r8168dp_ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001229}
1230
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001231static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001232{
Heiner Kallweit3c72bf72018-11-19 22:40:04 +01001233 return !!(r8168ep_ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001234}
1235
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001236static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001237{
1238 switch (tp->mac_version) {
1239 case RTL_GIGA_MAC_VER_27:
1240 case RTL_GIGA_MAC_VER_28:
1241 case RTL_GIGA_MAC_VER_31:
1242 return r8168dp_check_dash(tp);
1243 case RTL_GIGA_MAC_VER_49:
1244 case RTL_GIGA_MAC_VER_50:
1245 case RTL_GIGA_MAC_VER_51:
1246 return r8168ep_check_dash(tp);
1247 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001248 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001249 }
1250}
1251
françois romieuc28aa382011-08-02 03:53:43 +00001252struct exgmac_reg {
1253 u16 addr;
1254 u16 mask;
1255 u32 val;
1256};
1257
Francois Romieufdf6fc02012-07-06 22:40:38 +02001258static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001259 const struct exgmac_reg *r, int len)
1260{
1261 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001262 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001263 r++;
1264 }
1265}
1266
Francois Romieuffc46952012-07-06 14:19:23 +02001267DECLARE_RTL_COND(rtl_efusear_cond)
1268{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001269 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001270}
1271
Francois Romieufdf6fc02012-07-06 22:40:38 +02001272static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001273{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001274 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001275
Francois Romieuffc46952012-07-06 14:19:23 +02001276 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001277 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001278}
1279
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001280static u16 rtl_get_events(struct rtl8169_private *tp)
1281{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001282 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001283}
1284
1285static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1286{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001287 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001288}
1289
1290static void rtl_irq_disable(struct rtl8169_private *tp)
1291{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001292 RTL_W16(tp, IntrMask, 0);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001293}
1294
Francois Romieuda78dbf2012-01-26 14:18:23 +01001295#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1296#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1297#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1298
Heiner Kallweitfe716f82018-11-19 22:31:32 +01001299static void rtl_irq_enable(struct rtl8169_private *tp)
Francois Romieuda78dbf2012-01-26 14:18:23 +01001300{
Heiner Kallweit559c3c02018-11-19 22:34:17 +01001301 RTL_W16(tp, IntrMask, tp->irq_mask);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001302}
1303
françois romieu811fd302011-12-04 20:30:45 +00001304static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001306 rtl_irq_disable(tp);
Heiner Kallweitde20e122018-09-25 07:58:00 +02001307 rtl_ack_events(tp, 0xffff);
1308 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001309 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Hayes Wang70090422011-07-06 15:58:06 +08001312static void rtl_link_chg_patch(struct rtl8169_private *tp)
1313{
Hayes Wang70090422011-07-06 15:58:06 +08001314 struct net_device *dev = tp->dev;
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001315 struct phy_device *phydev = dev->phydev;
Hayes Wang70090422011-07-06 15:58:06 +08001316
1317 if (!netif_running(dev))
1318 return;
1319
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001320 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1321 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001322 if (phydev->speed == SPEED_1000) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001323 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1324 ERIAR_EXGMAC);
1325 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1326 ERIAR_EXGMAC);
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001327 } else if (phydev->speed == SPEED_100) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001328 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1329 ERIAR_EXGMAC);
1330 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1331 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001332 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001333 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1334 ERIAR_EXGMAC);
1335 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1336 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001337 }
1338 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001339 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001340 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001341 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001342 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001343 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1344 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001345 if (phydev->speed == SPEED_1000) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001346 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1347 ERIAR_EXGMAC);
1348 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1349 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001350 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001351 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1352 ERIAR_EXGMAC);
1353 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1354 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001355 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001356 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001357 if (phydev->speed == SPEED_10) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001358 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1359 ERIAR_EXGMAC);
1360 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1361 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001362 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001363 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1364 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001365 }
Hayes Wang70090422011-07-06 15:58:06 +08001366 }
1367}
1368
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001369#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1370
1371static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1372{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001373 u8 options;
1374 u32 wolopts = 0;
1375
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001376 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001377 if (!(options & PMEnable))
1378 return 0;
1379
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001380 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001381 if (options & LinkUp)
1382 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001383 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001384 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1385 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001386 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1387 wolopts |= WAKE_MAGIC;
1388 break;
1389 default:
1390 if (options & MagicPacket)
1391 wolopts |= WAKE_MAGIC;
1392 break;
1393 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001394
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001395 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001396 if (options & UWF)
1397 wolopts |= WAKE_UCAST;
1398 if (options & BWF)
1399 wolopts |= WAKE_BCAST;
1400 if (options & MWF)
1401 wolopts |= WAKE_MCAST;
1402
1403 return wolopts;
1404}
1405
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001406static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1407{
1408 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001409
Francois Romieuda78dbf2012-01-26 14:18:23 +01001410 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001411 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001412 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001413 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001414}
1415
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001416static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001417{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001418 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001419 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001420 u32 opt;
1421 u16 reg;
1422 u8 mask;
1423 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001424 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001425 { WAKE_UCAST, Config5, UWF },
1426 { WAKE_BCAST, Config5, BWF },
1427 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001428 { WAKE_ANY, Config5, LanWake },
1429 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001430 };
Francois Romieu851e6022012-04-17 11:10:11 +02001431 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001432
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001433 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001434
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001435 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001436 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1437 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001438 tmp = ARRAY_SIZE(cfg) - 1;
1439 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001440 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001441 0x0dc,
1442 ERIAR_MASK_0100,
1443 MagicPacket_v2,
1444 0x0000,
1445 ERIAR_EXGMAC);
1446 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001447 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001448 0x0dc,
1449 ERIAR_MASK_0100,
1450 0x0000,
1451 MagicPacket_v2,
1452 ERIAR_EXGMAC);
1453 break;
1454 default:
1455 tmp = ARRAY_SIZE(cfg);
1456 break;
1457 }
1458
1459 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001460 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001461 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001462 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001463 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001464 }
1465
Francois Romieu851e6022012-04-17 11:10:11 +02001466 switch (tp->mac_version) {
1467 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001468 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001469 if (wolopts)
1470 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001471 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001472 break;
1473 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001474 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001475 if (wolopts)
1476 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001477 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001478 break;
1479 }
1480
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001481 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit3bd82642018-12-30 13:16:12 +01001482
1483 device_set_wakeup_enable(tp_to_dev(tp), wolopts);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001484}
1485
1486static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1487{
1488 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001489 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001490
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001491 if (wol->wolopts & ~WAKE_ANY)
1492 return -EINVAL;
1493
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001494 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001495
Francois Romieuda78dbf2012-01-26 14:18:23 +01001496 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001497
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001498 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001499
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001500 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001501 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001502
1503 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001504
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001505 pm_runtime_put_noidle(d);
1506
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001507 return 0;
1508}
1509
Francois Romieu31bd2042011-04-26 18:58:59 +02001510static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1511{
Francois Romieu85bffe62011-04-27 08:22:39 +02001512 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001513}
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515static void rtl8169_get_drvinfo(struct net_device *dev,
1516 struct ethtool_drvinfo *info)
1517{
1518 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001519 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Rick Jones68aad782011-11-07 13:29:27 +00001521 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001522 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001523 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001524 if (!IS_ERR_OR_NULL(rtl_fw))
1525 strlcpy(info->fw_version, rtl_fw->version,
1526 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
1529static int rtl8169_get_regs_len(struct net_device *dev)
1530{
1531 return R8169_REGS_SIZE;
1532}
1533
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001534static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1535 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Francois Romieud58d46b2011-05-03 16:38:29 +02001537 struct rtl8169_private *tp = netdev_priv(dev);
1538
Francois Romieu2b7b4312011-04-18 22:53:24 -07001539 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001540 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Francois Romieud58d46b2011-05-03 16:38:29 +02001542 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001543 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001544 features &= ~NETIF_F_IP_CSUM;
1545
Michał Mirosław350fb322011-04-08 06:35:56 +00001546 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Heiner Kallweita3984572018-04-28 22:19:15 +02001549static int rtl8169_set_features(struct net_device *dev,
1550 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001553 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Heiner Kallweita3984572018-04-28 22:19:15 +02001555 rtl_lock_work(tp);
1556
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001557 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001558 if (features & NETIF_F_RXALL)
1559 rx_config |= (AcceptErr | AcceptRunt);
1560 else
1561 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001563 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001564
hayeswang929a0312014-09-16 11:40:47 +08001565 if (features & NETIF_F_RXCSUM)
1566 tp->cp_cmd |= RxChkSum;
1567 else
1568 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001569
hayeswang929a0312014-09-16 11:40:47 +08001570 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1571 tp->cp_cmd |= RxVlan;
1572 else
1573 tp->cp_cmd &= ~RxVlan;
1574
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001575 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1576 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Francois Romieuda78dbf2012-01-26 14:18:23 +01001578 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 return 0;
1581}
1582
Kirill Smelkov810f4892012-11-10 21:11:02 +04001583static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001585 return (skb_vlan_tag_present(skb)) ?
1586 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
Francois Romieu7a8fc772011-03-01 17:18:33 +01001589static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Francois Romieu7a8fc772011-03-01 17:18:33 +01001593 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001594 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1598 void *p)
1599{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001600 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001601 u32 __iomem *data = tp->mmio_addr;
1602 u32 *dw = p;
1603 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Francois Romieuda78dbf2012-01-26 14:18:23 +01001605 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001606 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1607 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001608 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001611static u32 rtl8169_get_msglevel(struct net_device *dev)
1612{
1613 struct rtl8169_private *tp = netdev_priv(dev);
1614
1615 return tp->msg_enable;
1616}
1617
1618static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1619{
1620 struct rtl8169_private *tp = netdev_priv(dev);
1621
1622 tp->msg_enable = value;
1623}
1624
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001625static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1626 "tx_packets",
1627 "rx_packets",
1628 "tx_errors",
1629 "rx_errors",
1630 "rx_missed",
1631 "align_errors",
1632 "tx_single_collisions",
1633 "tx_multi_collisions",
1634 "unicast",
1635 "broadcast",
1636 "multicast",
1637 "tx_aborted",
1638 "tx_underrun",
1639};
1640
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001641static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001642{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001643 switch (sset) {
1644 case ETH_SS_STATS:
1645 return ARRAY_SIZE(rtl8169_gstrings);
1646 default:
1647 return -EOPNOTSUPP;
1648 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001649}
1650
Corinna Vinschen42020322015-09-10 10:47:35 +02001651DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001652{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001653 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001654}
1655
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001656static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001657{
Corinna Vinschen42020322015-09-10 10:47:35 +02001658 dma_addr_t paddr = tp->counters_phys_addr;
1659 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001660
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001661 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1662 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001663 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001664 RTL_W32(tp, CounterAddrLow, cmd);
1665 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001666
Francois Romieua78e9362018-01-26 01:53:26 +01001667 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001668}
1669
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001670static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001671{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001672 /*
1673 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1674 * tally counters.
1675 */
1676 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1677 return true;
1678
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001679 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001680}
1681
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001682static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001683{
Ivan Vecera355423d2009-02-06 21:49:57 -08001684 /*
1685 * Some chips are unable to dump tally counters when the receiver
1686 * is disabled.
1687 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001688 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001689 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001690
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001691 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001692}
1693
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001694static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001695{
Corinna Vinschen42020322015-09-10 10:47:35 +02001696 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001697 bool ret = false;
1698
1699 /*
1700 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1701 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1702 * reset by a power cycle, while the counter values collected by the
1703 * driver are reset at every driver unload/load cycle.
1704 *
1705 * To make sure the HW values returned by @get_stats64 match the SW
1706 * values, we collect the initial values at first open(*) and use them
1707 * as offsets to normalize the values returned by @get_stats64.
1708 *
1709 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1710 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1711 * set at open time by rtl_hw_start.
1712 */
1713
1714 if (tp->tc_offset.inited)
1715 return true;
1716
1717 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001718 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001719 ret = true;
1720
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001721 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001722 ret = true;
1723
Corinna Vinschen42020322015-09-10 10:47:35 +02001724 tp->tc_offset.tx_errors = counters->tx_errors;
1725 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1726 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001727 tp->tc_offset.inited = true;
1728
1729 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001730}
1731
Ivan Vecera355423d2009-02-06 21:49:57 -08001732static void rtl8169_get_ethtool_stats(struct net_device *dev,
1733 struct ethtool_stats *stats, u64 *data)
1734{
1735 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001736 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02001737 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08001738
1739 ASSERT_RTNL();
1740
Chun-Hao Line0636232016-07-29 16:37:55 +08001741 pm_runtime_get_noresume(d);
1742
1743 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001744 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08001745
1746 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08001747
Corinna Vinschen42020322015-09-10 10:47:35 +02001748 data[0] = le64_to_cpu(counters->tx_packets);
1749 data[1] = le64_to_cpu(counters->rx_packets);
1750 data[2] = le64_to_cpu(counters->tx_errors);
1751 data[3] = le32_to_cpu(counters->rx_errors);
1752 data[4] = le16_to_cpu(counters->rx_missed);
1753 data[5] = le16_to_cpu(counters->align_errors);
1754 data[6] = le32_to_cpu(counters->tx_one_collision);
1755 data[7] = le32_to_cpu(counters->tx_multi_collision);
1756 data[8] = le64_to_cpu(counters->rx_unicast);
1757 data[9] = le64_to_cpu(counters->rx_broadcast);
1758 data[10] = le32_to_cpu(counters->rx_multicast);
1759 data[11] = le16_to_cpu(counters->tx_aborted);
1760 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08001761}
1762
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001763static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1764{
1765 switch(stringset) {
1766 case ETH_SS_STATS:
1767 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1768 break;
1769 }
1770}
1771
Francois Romieu50970832017-10-27 13:24:49 +03001772/*
1773 * Interrupt coalescing
1774 *
1775 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1776 * > 8169, 8168 and 810x line of chipsets
1777 *
1778 * 8169, 8168, and 8136(810x) serial chipsets support it.
1779 *
1780 * > 2 - the Tx timer unit at gigabit speed
1781 *
1782 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1783 * (0xe0) bit 1 and bit 0.
1784 *
1785 * For 8169
1786 * bit[1:0] \ speed 1000M 100M 10M
1787 * 0 0 320ns 2.56us 40.96us
1788 * 0 1 2.56us 20.48us 327.7us
1789 * 1 0 5.12us 40.96us 655.4us
1790 * 1 1 10.24us 81.92us 1.31ms
1791 *
1792 * For the other
1793 * bit[1:0] \ speed 1000M 100M 10M
1794 * 0 0 5us 2.56us 40.96us
1795 * 0 1 40us 20.48us 327.7us
1796 * 1 0 80us 40.96us 655.4us
1797 * 1 1 160us 81.92us 1.31ms
1798 */
1799
1800/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1801struct rtl_coalesce_scale {
1802 /* Rx / Tx */
1803 u32 nsecs[2];
1804};
1805
1806/* rx/tx scale factors for all CPlusCmd[0:1] cases */
1807struct rtl_coalesce_info {
1808 u32 speed;
1809 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1810};
1811
1812/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1813#define rxtx_x1822(r, t) { \
1814 {{(r), (t)}}, \
1815 {{(r)*8, (t)*8}}, \
1816 {{(r)*8*2, (t)*8*2}}, \
1817 {{(r)*8*2*2, (t)*8*2*2}}, \
1818}
1819static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1820 /* speed delays: rx00 tx00 */
1821 { SPEED_10, rxtx_x1822(40960, 40960) },
1822 { SPEED_100, rxtx_x1822( 2560, 2560) },
1823 { SPEED_1000, rxtx_x1822( 320, 320) },
1824 { 0 },
1825};
1826
1827static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1828 /* speed delays: rx00 tx00 */
1829 { SPEED_10, rxtx_x1822(40960, 40960) },
1830 { SPEED_100, rxtx_x1822( 2560, 2560) },
1831 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1832 { 0 },
1833};
1834#undef rxtx_x1822
1835
1836/* get rx/tx scale vector corresponding to current speed */
1837static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1838{
1839 struct rtl8169_private *tp = netdev_priv(dev);
1840 struct ethtool_link_ksettings ecmd;
1841 const struct rtl_coalesce_info *ci;
1842 int rc;
1843
Heiner Kallweit45772432018-07-17 22:51:44 +02001844 rc = phy_ethtool_get_link_ksettings(dev, &ecmd);
Francois Romieu50970832017-10-27 13:24:49 +03001845 if (rc < 0)
1846 return ERR_PTR(rc);
1847
1848 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
1849 if (ecmd.base.speed == ci->speed) {
1850 return ci;
1851 }
1852 }
1853
1854 return ERR_PTR(-ELNRNG);
1855}
1856
1857static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1858{
1859 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001860 const struct rtl_coalesce_info *ci;
1861 const struct rtl_coalesce_scale *scale;
1862 struct {
1863 u32 *max_frames;
1864 u32 *usecs;
1865 } coal_settings [] = {
1866 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1867 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1868 }, *p = coal_settings;
1869 int i;
1870 u16 w;
1871
1872 memset(ec, 0, sizeof(*ec));
1873
1874 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1875 ci = rtl_coalesce_info(dev);
1876 if (IS_ERR(ci))
1877 return PTR_ERR(ci);
1878
Heiner Kallweit0ae09742018-04-28 22:19:26 +02001879 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03001880
1881 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001882 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03001883 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1884 w >>= RTL_COALESCE_SHIFT;
1885 *p->usecs = w & RTL_COALESCE_MASK;
1886 }
1887
1888 for (i = 0; i < 2; i++) {
1889 p = coal_settings + i;
1890 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1891
1892 /*
1893 * ethtool_coalesce says it is illegal to set both usecs and
1894 * max_frames to 0.
1895 */
1896 if (!*p->usecs && !*p->max_frames)
1897 *p->max_frames = 1;
1898 }
1899
1900 return 0;
1901}
1902
1903/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1904static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1905 struct net_device *dev, u32 nsec, u16 *cp01)
1906{
1907 const struct rtl_coalesce_info *ci;
1908 u16 i;
1909
1910 ci = rtl_coalesce_info(dev);
1911 if (IS_ERR(ci))
1912 return ERR_CAST(ci);
1913
1914 for (i = 0; i < 4; i++) {
1915 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1916 ci->scalev[i].nsecs[1]);
1917 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1918 *cp01 = i;
1919 return &ci->scalev[i];
1920 }
1921 }
1922
1923 return ERR_PTR(-EINVAL);
1924}
1925
1926static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1927{
1928 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001929 const struct rtl_coalesce_scale *scale;
1930 struct {
1931 u32 frames;
1932 u32 usecs;
1933 } coal_settings [] = {
1934 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1935 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1936 }, *p = coal_settings;
1937 u16 w = 0, cp01;
1938 int i;
1939
1940 scale = rtl_coalesce_choose_scale(dev,
1941 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1942 if (IS_ERR(scale))
1943 return PTR_ERR(scale);
1944
1945 for (i = 0; i < 2; i++, p++) {
1946 u32 units;
1947
1948 /*
1949 * accept max_frames=1 we returned in rtl_get_coalesce.
1950 * accept it not only when usecs=0 because of e.g. the following scenario:
1951 *
1952 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
1953 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
1954 * - then user does `ethtool -C eth0 rx-usecs 100`
1955 *
1956 * since ethtool sends to kernel whole ethtool_coalesce
1957 * settings, if we do not handle rx_usecs=!0, rx_frames=1
1958 * we'll reject it below in `frames % 4 != 0`.
1959 */
1960 if (p->frames == 1) {
1961 p->frames = 0;
1962 }
1963
1964 units = p->usecs * 1000 / scale->nsecs[i];
1965 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
1966 return -EINVAL;
1967
1968 w <<= RTL_COALESCE_SHIFT;
1969 w |= units;
1970 w <<= RTL_COALESCE_SHIFT;
1971 w |= p->frames >> 2;
1972 }
1973
1974 rtl_lock_work(tp);
1975
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001976 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03001977
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02001978 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001979 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1980 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03001981
1982 rtl_unlock_work(tp);
1983
1984 return 0;
1985}
1986
Jeff Garzik7282d492006-09-13 14:30:00 -04001987static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 .get_drvinfo = rtl8169_get_drvinfo,
1989 .get_regs_len = rtl8169_get_regs_len,
1990 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03001991 .get_coalesce = rtl_get_coalesce,
1992 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001993 .get_msglevel = rtl8169_get_msglevel,
1994 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001996 .get_wol = rtl8169_get_wol,
1997 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001998 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001999 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002000 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002001 .get_ts_info = ethtool_op_get_ts_info,
Heiner Kallweitdd849572018-07-17 22:51:48 +02002002 .nway_reset = phy_ethtool_nway_reset,
Heiner Kallweit45772432018-07-17 22:51:44 +02002003 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2004 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005};
2006
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002007static void rtl8169_get_mac_version(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Francois Romieu0e485152007-02-20 00:00:26 +01002009 /*
2010 * The driver currently handles the 8168Bf and the 8168Be identically
2011 * but they can be identified more specifically through the test below
2012 * if needed:
2013 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002014 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002015 *
2016 * Same thing for the 8101Eb and the 8101Ec:
2017 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002018 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002019 */
Francois Romieu37441002011-06-17 22:58:54 +02002020 static const struct rtl_mac_info {
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002021 u16 mask;
2022 u16 val;
2023 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002025 /* 8168EP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002026 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
2027 { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
2028 { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002029
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002030 /* 8168H family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002031 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
2032 { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002033
Hayes Wangc5583862012-07-02 17:23:22 +08002034 /* 8168G family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002035 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
2036 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
2037 { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
2038 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
Hayes Wangc5583862012-07-02 17:23:22 +08002039
Hayes Wangc2218922011-09-06 16:55:18 +08002040 /* 8168F family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002041 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
2042 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
2043 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
Hayes Wangc2218922011-09-06 16:55:18 +08002044
hayeswang01dc7fe2011-03-21 01:50:28 +00002045 /* 8168E family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002046 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
2047 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
2048 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002049
Francois Romieu5b538df2008-07-20 16:22:45 +02002050 /* 8168D family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002051 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
2052 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002053
françois romieue6de30d2011-01-03 15:08:37 +00002054 /* 8168DP family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002055 { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
2056 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
2057 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002058
Francois Romieuef808d52008-06-29 13:10:54 +02002059 /* 8168C family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002060 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
2061 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
2062 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
2063 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
2064 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
2065 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
2066 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002067
2068 /* 8168B family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002069 { 0x7cf, 0x380, RTL_GIGA_MAC_VER_12 },
2070 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
2071 { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002072
2073 /* 8101 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002074 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
2075 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
2076 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
2077 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
2078 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
2079 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
2080 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
2081 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
2082 { 0x7cf, 0x340, RTL_GIGA_MAC_VER_13 },
2083 { 0x7cf, 0x343, RTL_GIGA_MAC_VER_10 },
2084 { 0x7cf, 0x342, RTL_GIGA_MAC_VER_16 },
2085 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
2086 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
2087 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_16 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002088 /* FIXME: where did these entries come from ? -- FR */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002089 { 0xfc8, 0x388, RTL_GIGA_MAC_VER_15 },
2090 { 0xfc8, 0x308, RTL_GIGA_MAC_VER_14 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002091
2092 /* 8110 family. */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002093 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
2094 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
2095 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
2096 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
2097 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
2098 { 0xfc8, 0x000, RTL_GIGA_MAC_VER_01 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002099
Jean Delvaref21b75e2009-05-26 20:54:48 -07002100 /* Catch-all */
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002101 { 0x000, 0x000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002102 };
2103 const struct rtl_mac_info *p = mac_info;
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01002104 u16 reg = RTL_R32(tp, TxConfig) >> 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002106 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 p++;
2108 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002109
2110 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01002111 dev_err(tp_to_dev(tp), "unknown chip XID %03x\n", reg & 0xfcf);
Heiner Kallweit45f19962018-11-22 22:00:10 +01002112 } else if (!tp->supports_gmii) {
2113 if (tp->mac_version == RTL_GIGA_MAC_VER_42)
2114 tp->mac_version = RTL_GIGA_MAC_VER_43;
2115 else if (tp->mac_version == RTL_GIGA_MAC_VER_45)
2116 tp->mac_version = RTL_GIGA_MAC_VER_47;
2117 else if (tp->mac_version == RTL_GIGA_MAC_VER_46)
2118 tp->mac_version = RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Francois Romieu867763c2007-08-17 18:21:58 +02002122struct phy_reg {
2123 u16 reg;
2124 u16 val;
2125};
2126
françois romieu4da19632011-01-03 15:07:55 +00002127static void rtl_writephy_batch(struct rtl8169_private *tp,
2128 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002129{
2130 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002131 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002132 regs++;
2133 }
2134}
2135
françois romieubca03d52011-01-03 15:07:31 +00002136#define PHY_READ 0x00000000
2137#define PHY_DATA_OR 0x10000000
2138#define PHY_DATA_AND 0x20000000
2139#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002140#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002141#define PHY_CLEAR_READCOUNT 0x70000000
2142#define PHY_WRITE 0x80000000
2143#define PHY_READCOUNT_EQ_SKIP 0x90000000
2144#define PHY_COMP_EQ_SKIPN 0xa0000000
2145#define PHY_COMP_NEQ_SKIPN 0xb0000000
2146#define PHY_WRITE_PREVIOUS 0xc0000000
2147#define PHY_SKIPN 0xd0000000
2148#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002149
Hayes Wang960aee62011-06-18 11:37:48 +02002150struct fw_info {
2151 u32 magic;
2152 char version[RTL_VER_SIZE];
2153 __le32 fw_start;
2154 __le32 fw_len;
2155 u8 chksum;
2156} __packed;
2157
Francois Romieu1c361ef2011-06-17 17:16:24 +02002158#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2159
2160static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002161{
Francois Romieub6ffd972011-06-17 17:00:05 +02002162 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002163 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002164 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2165 char *version = rtl_fw->version;
2166 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002167
Francois Romieu1c361ef2011-06-17 17:16:24 +02002168 if (fw->size < FW_OPCODE_SIZE)
2169 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002170
2171 if (!fw_info->magic) {
2172 size_t i, size, start;
2173 u8 checksum = 0;
2174
2175 if (fw->size < sizeof(*fw_info))
2176 goto out;
2177
2178 for (i = 0; i < fw->size; i++)
2179 checksum += fw->data[i];
2180 if (checksum != 0)
2181 goto out;
2182
2183 start = le32_to_cpu(fw_info->fw_start);
2184 if (start > fw->size)
2185 goto out;
2186
2187 size = le32_to_cpu(fw_info->fw_len);
2188 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2189 goto out;
2190
2191 memcpy(version, fw_info->version, RTL_VER_SIZE);
2192
2193 pa->code = (__le32 *)(fw->data + start);
2194 pa->size = size;
2195 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002196 if (fw->size % FW_OPCODE_SIZE)
2197 goto out;
2198
2199 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2200
2201 pa->code = (__le32 *)fw->data;
2202 pa->size = fw->size / FW_OPCODE_SIZE;
2203 }
2204 version[RTL_VER_SIZE - 1] = 0;
2205
2206 rc = true;
2207out:
2208 return rc;
2209}
2210
Francois Romieufd112f22011-06-18 00:10:29 +02002211static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2212 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002213{
Francois Romieufd112f22011-06-18 00:10:29 +02002214 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002215 size_t index;
2216
Francois Romieu1c361ef2011-06-17 17:16:24 +02002217 for (index = 0; index < pa->size; index++) {
2218 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002219 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002220
hayeswang42b82dc2011-01-10 02:07:25 +00002221 switch(action & 0xf0000000) {
2222 case PHY_READ:
2223 case PHY_DATA_OR:
2224 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002225 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002226 case PHY_CLEAR_READCOUNT:
2227 case PHY_WRITE:
2228 case PHY_WRITE_PREVIOUS:
2229 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002230 break;
2231
hayeswang42b82dc2011-01-10 02:07:25 +00002232 case PHY_BJMPN:
2233 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002234 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002235 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002236 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002237 }
2238 break;
2239 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002240 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002241 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002242 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002243 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002244 }
2245 break;
2246 case PHY_COMP_EQ_SKIPN:
2247 case PHY_COMP_NEQ_SKIPN:
2248 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002249 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002250 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002251 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002252 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002253 }
2254 break;
2255
hayeswang42b82dc2011-01-10 02:07:25 +00002256 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002257 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002258 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002259 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002260 }
2261 }
Francois Romieufd112f22011-06-18 00:10:29 +02002262 rc = true;
2263out:
2264 return rc;
2265}
françois romieubca03d52011-01-03 15:07:31 +00002266
Francois Romieufd112f22011-06-18 00:10:29 +02002267static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2268{
2269 struct net_device *dev = tp->dev;
2270 int rc = -EINVAL;
2271
2272 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002273 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002274 goto out;
2275 }
2276
2277 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2278 rc = 0;
2279out:
2280 return rc;
2281}
2282
2283static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2284{
2285 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002286 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002287 u32 predata, count;
2288 size_t index;
2289
2290 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002291 org.write = ops->write;
2292 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002293
Francois Romieu1c361ef2011-06-17 17:16:24 +02002294 for (index = 0; index < pa->size; ) {
2295 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002296 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002297 u32 regno = (action & 0x0fff0000) >> 16;
2298
2299 if (!action)
2300 break;
françois romieubca03d52011-01-03 15:07:31 +00002301
2302 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002303 case PHY_READ:
2304 predata = rtl_readphy(tp, regno);
2305 count++;
2306 index++;
françois romieubca03d52011-01-03 15:07:31 +00002307 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002308 case PHY_DATA_OR:
2309 predata |= data;
2310 index++;
2311 break;
2312 case PHY_DATA_AND:
2313 predata &= data;
2314 index++;
2315 break;
2316 case PHY_BJMPN:
2317 index -= regno;
2318 break;
hayeswangeee37862013-04-01 22:23:38 +00002319 case PHY_MDIO_CHG:
2320 if (data == 0) {
2321 ops->write = org.write;
2322 ops->read = org.read;
2323 } else if (data == 1) {
2324 ops->write = mac_mcu_write;
2325 ops->read = mac_mcu_read;
2326 }
2327
hayeswang42b82dc2011-01-10 02:07:25 +00002328 index++;
2329 break;
2330 case PHY_CLEAR_READCOUNT:
2331 count = 0;
2332 index++;
2333 break;
2334 case PHY_WRITE:
2335 rtl_writephy(tp, regno, data);
2336 index++;
2337 break;
2338 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002339 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002340 break;
2341 case PHY_COMP_EQ_SKIPN:
2342 if (predata == data)
2343 index += regno;
2344 index++;
2345 break;
2346 case PHY_COMP_NEQ_SKIPN:
2347 if (predata != data)
2348 index += regno;
2349 index++;
2350 break;
2351 case PHY_WRITE_PREVIOUS:
2352 rtl_writephy(tp, regno, predata);
2353 index++;
2354 break;
2355 case PHY_SKIPN:
2356 index += regno + 1;
2357 break;
2358 case PHY_DELAY_MS:
2359 mdelay(data);
2360 index++;
2361 break;
2362
françois romieubca03d52011-01-03 15:07:31 +00002363 default:
2364 BUG();
2365 }
2366 }
hayeswangeee37862013-04-01 22:23:38 +00002367
2368 ops->write = org.write;
2369 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002370}
2371
françois romieuf1e02ed2011-01-13 13:07:53 +00002372static void rtl_release_firmware(struct rtl8169_private *tp)
2373{
Francois Romieub6ffd972011-06-17 17:00:05 +02002374 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2375 release_firmware(tp->rtl_fw->fw);
2376 kfree(tp->rtl_fw);
2377 }
2378 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002379}
2380
François Romieu953a12c2011-04-24 17:38:48 +02002381static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002382{
Francois Romieub6ffd972011-06-17 17:00:05 +02002383 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002384
2385 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002386 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002387 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002388}
2389
2390static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2391{
2392 if (rtl_readphy(tp, reg) != val)
2393 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2394 else
2395 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002396}
2397
françois romieu4da19632011-01-03 15:07:55 +00002398static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002400 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002401 { 0x1f, 0x0001 },
2402 { 0x06, 0x006e },
2403 { 0x08, 0x0708 },
2404 { 0x15, 0x4000 },
2405 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
françois romieu0b9b5712009-08-10 19:44:56 +00002407 { 0x1f, 0x0001 },
2408 { 0x03, 0x00a1 },
2409 { 0x02, 0x0008 },
2410 { 0x01, 0x0120 },
2411 { 0x00, 0x1000 },
2412 { 0x04, 0x0800 },
2413 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
françois romieu0b9b5712009-08-10 19:44:56 +00002415 { 0x03, 0xff41 },
2416 { 0x02, 0xdf60 },
2417 { 0x01, 0x0140 },
2418 { 0x00, 0x0077 },
2419 { 0x04, 0x7800 },
2420 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
françois romieu0b9b5712009-08-10 19:44:56 +00002422 { 0x03, 0x802f },
2423 { 0x02, 0x4f02 },
2424 { 0x01, 0x0409 },
2425 { 0x00, 0xf0f9 },
2426 { 0x04, 0x9800 },
2427 { 0x04, 0x9000 },
2428
2429 { 0x03, 0xdf01 },
2430 { 0x02, 0xdf20 },
2431 { 0x01, 0xff95 },
2432 { 0x00, 0xba00 },
2433 { 0x04, 0xa800 },
2434 { 0x04, 0xa000 },
2435
2436 { 0x03, 0xff41 },
2437 { 0x02, 0xdf20 },
2438 { 0x01, 0x0140 },
2439 { 0x00, 0x00bb },
2440 { 0x04, 0xb800 },
2441 { 0x04, 0xb000 },
2442
2443 { 0x03, 0xdf41 },
2444 { 0x02, 0xdc60 },
2445 { 0x01, 0x6340 },
2446 { 0x00, 0x007d },
2447 { 0x04, 0xd800 },
2448 { 0x04, 0xd000 },
2449
2450 { 0x03, 0xdf01 },
2451 { 0x02, 0xdf20 },
2452 { 0x01, 0x100a },
2453 { 0x00, 0xa0ff },
2454 { 0x04, 0xf800 },
2455 { 0x04, 0xf000 },
2456
2457 { 0x1f, 0x0000 },
2458 { 0x0b, 0x0000 },
2459 { 0x00, 0x9200 }
2460 };
2461
françois romieu4da19632011-01-03 15:07:55 +00002462 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
françois romieu4da19632011-01-03 15:07:55 +00002465static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002466{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002467 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002468 { 0x1f, 0x0002 },
2469 { 0x01, 0x90d0 },
2470 { 0x1f, 0x0000 }
2471 };
2472
françois romieu4da19632011-01-03 15:07:55 +00002473 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002474}
2475
françois romieu4da19632011-01-03 15:07:55 +00002476static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002477{
2478 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002479
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002480 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2481 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002482 return;
2483
françois romieu4da19632011-01-03 15:07:55 +00002484 rtl_writephy(tp, 0x1f, 0x0001);
2485 rtl_writephy(tp, 0x10, 0xf01b);
2486 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002487}
2488
françois romieu4da19632011-01-03 15:07:55 +00002489static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002490{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002491 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002492 { 0x1f, 0x0001 },
2493 { 0x04, 0x0000 },
2494 { 0x03, 0x00a1 },
2495 { 0x02, 0x0008 },
2496 { 0x01, 0x0120 },
2497 { 0x00, 0x1000 },
2498 { 0x04, 0x0800 },
2499 { 0x04, 0x9000 },
2500 { 0x03, 0x802f },
2501 { 0x02, 0x4f02 },
2502 { 0x01, 0x0409 },
2503 { 0x00, 0xf099 },
2504 { 0x04, 0x9800 },
2505 { 0x04, 0xa000 },
2506 { 0x03, 0xdf01 },
2507 { 0x02, 0xdf20 },
2508 { 0x01, 0xff95 },
2509 { 0x00, 0xba00 },
2510 { 0x04, 0xa800 },
2511 { 0x04, 0xf000 },
2512 { 0x03, 0xdf01 },
2513 { 0x02, 0xdf20 },
2514 { 0x01, 0x101a },
2515 { 0x00, 0xa0ff },
2516 { 0x04, 0xf800 },
2517 { 0x04, 0x0000 },
2518 { 0x1f, 0x0000 },
2519
2520 { 0x1f, 0x0001 },
2521 { 0x10, 0xf41b },
2522 { 0x14, 0xfb54 },
2523 { 0x18, 0xf5c7 },
2524 { 0x1f, 0x0000 },
2525
2526 { 0x1f, 0x0001 },
2527 { 0x17, 0x0cc0 },
2528 { 0x1f, 0x0000 }
2529 };
2530
françois romieu4da19632011-01-03 15:07:55 +00002531 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002532
françois romieu4da19632011-01-03 15:07:55 +00002533 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002534}
2535
françois romieu4da19632011-01-03 15:07:55 +00002536static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002537{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002538 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002539 { 0x1f, 0x0001 },
2540 { 0x04, 0x0000 },
2541 { 0x03, 0x00a1 },
2542 { 0x02, 0x0008 },
2543 { 0x01, 0x0120 },
2544 { 0x00, 0x1000 },
2545 { 0x04, 0x0800 },
2546 { 0x04, 0x9000 },
2547 { 0x03, 0x802f },
2548 { 0x02, 0x4f02 },
2549 { 0x01, 0x0409 },
2550 { 0x00, 0xf099 },
2551 { 0x04, 0x9800 },
2552 { 0x04, 0xa000 },
2553 { 0x03, 0xdf01 },
2554 { 0x02, 0xdf20 },
2555 { 0x01, 0xff95 },
2556 { 0x00, 0xba00 },
2557 { 0x04, 0xa800 },
2558 { 0x04, 0xf000 },
2559 { 0x03, 0xdf01 },
2560 { 0x02, 0xdf20 },
2561 { 0x01, 0x101a },
2562 { 0x00, 0xa0ff },
2563 { 0x04, 0xf800 },
2564 { 0x04, 0x0000 },
2565 { 0x1f, 0x0000 },
2566
2567 { 0x1f, 0x0001 },
2568 { 0x0b, 0x8480 },
2569 { 0x1f, 0x0000 },
2570
2571 { 0x1f, 0x0001 },
2572 { 0x18, 0x67c7 },
2573 { 0x04, 0x2000 },
2574 { 0x03, 0x002f },
2575 { 0x02, 0x4360 },
2576 { 0x01, 0x0109 },
2577 { 0x00, 0x3022 },
2578 { 0x04, 0x2800 },
2579 { 0x1f, 0x0000 },
2580
2581 { 0x1f, 0x0001 },
2582 { 0x17, 0x0cc0 },
2583 { 0x1f, 0x0000 }
2584 };
2585
françois romieu4da19632011-01-03 15:07:55 +00002586 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002587}
2588
françois romieu4da19632011-01-03 15:07:55 +00002589static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002590{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002591 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002592 { 0x10, 0xf41b },
2593 { 0x1f, 0x0000 }
2594 };
2595
françois romieu4da19632011-01-03 15:07:55 +00002596 rtl_writephy(tp, 0x1f, 0x0001);
2597 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002598
françois romieu4da19632011-01-03 15:07:55 +00002599 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002600}
2601
françois romieu4da19632011-01-03 15:07:55 +00002602static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002603{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002604 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002605 { 0x1f, 0x0001 },
2606 { 0x10, 0xf41b },
2607 { 0x1f, 0x0000 }
2608 };
2609
françois romieu4da19632011-01-03 15:07:55 +00002610 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002611}
2612
françois romieu4da19632011-01-03 15:07:55 +00002613static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002614{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002615 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002616 { 0x1f, 0x0000 },
2617 { 0x1d, 0x0f00 },
2618 { 0x1f, 0x0002 },
2619 { 0x0c, 0x1ec8 },
2620 { 0x1f, 0x0000 }
2621 };
2622
françois romieu4da19632011-01-03 15:07:55 +00002623 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002624}
2625
françois romieu4da19632011-01-03 15:07:55 +00002626static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002627{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002628 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002629 { 0x1f, 0x0001 },
2630 { 0x1d, 0x3d98 },
2631 { 0x1f, 0x0000 }
2632 };
2633
françois romieu4da19632011-01-03 15:07:55 +00002634 rtl_writephy(tp, 0x1f, 0x0000);
2635 rtl_patchphy(tp, 0x14, 1 << 5);
2636 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002637
françois romieu4da19632011-01-03 15:07:55 +00002638 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002639}
2640
françois romieu4da19632011-01-03 15:07:55 +00002641static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002642{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002643 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002644 { 0x1f, 0x0001 },
2645 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002646 { 0x1f, 0x0002 },
2647 { 0x00, 0x88d4 },
2648 { 0x01, 0x82b1 },
2649 { 0x03, 0x7002 },
2650 { 0x08, 0x9e30 },
2651 { 0x09, 0x01f0 },
2652 { 0x0a, 0x5500 },
2653 { 0x0c, 0x00c8 },
2654 { 0x1f, 0x0003 },
2655 { 0x12, 0xc096 },
2656 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002657 { 0x1f, 0x0000 },
2658 { 0x1f, 0x0000 },
2659 { 0x09, 0x2000 },
2660 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002661 };
2662
françois romieu4da19632011-01-03 15:07:55 +00002663 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002664
françois romieu4da19632011-01-03 15:07:55 +00002665 rtl_patchphy(tp, 0x14, 1 << 5);
2666 rtl_patchphy(tp, 0x0d, 1 << 5);
2667 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002668}
2669
françois romieu4da19632011-01-03 15:07:55 +00002670static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002671{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002672 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002673 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002674 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002675 { 0x03, 0x802f },
2676 { 0x02, 0x4f02 },
2677 { 0x01, 0x0409 },
2678 { 0x00, 0xf099 },
2679 { 0x04, 0x9800 },
2680 { 0x04, 0x9000 },
2681 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002682 { 0x1f, 0x0002 },
2683 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002684 { 0x06, 0x0761 },
2685 { 0x1f, 0x0003 },
2686 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002687 { 0x1f, 0x0000 }
2688 };
2689
françois romieu4da19632011-01-03 15:07:55 +00002690 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002691
françois romieu4da19632011-01-03 15:07:55 +00002692 rtl_patchphy(tp, 0x16, 1 << 0);
2693 rtl_patchphy(tp, 0x14, 1 << 5);
2694 rtl_patchphy(tp, 0x0d, 1 << 5);
2695 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002696}
2697
françois romieu4da19632011-01-03 15:07:55 +00002698static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002699{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002700 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002701 { 0x1f, 0x0001 },
2702 { 0x12, 0x2300 },
2703 { 0x1d, 0x3d98 },
2704 { 0x1f, 0x0002 },
2705 { 0x0c, 0x7eb8 },
2706 { 0x06, 0x5461 },
2707 { 0x1f, 0x0003 },
2708 { 0x16, 0x0f0a },
2709 { 0x1f, 0x0000 }
2710 };
2711
françois romieu4da19632011-01-03 15:07:55 +00002712 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002713
françois romieu4da19632011-01-03 15:07:55 +00002714 rtl_patchphy(tp, 0x16, 1 << 0);
2715 rtl_patchphy(tp, 0x14, 1 << 5);
2716 rtl_patchphy(tp, 0x0d, 1 << 5);
2717 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002718}
2719
françois romieu4da19632011-01-03 15:07:55 +00002720static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002721{
françois romieu4da19632011-01-03 15:07:55 +00002722 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002723}
2724
françois romieubca03d52011-01-03 15:07:31 +00002725static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002726{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002727 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002728 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002729 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002730 { 0x06, 0x4064 },
2731 { 0x07, 0x2863 },
2732 { 0x08, 0x059c },
2733 { 0x09, 0x26b4 },
2734 { 0x0a, 0x6a19 },
2735 { 0x0b, 0xdcc8 },
2736 { 0x10, 0xf06d },
2737 { 0x14, 0x7f68 },
2738 { 0x18, 0x7fd9 },
2739 { 0x1c, 0xf0ff },
2740 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002741 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002742 { 0x12, 0xf49f },
2743 { 0x13, 0x070b },
2744 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002745 { 0x14, 0x94c0 },
2746
2747 /*
2748 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002749 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002750 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002751 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002752 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002753 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002754 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002755 { 0x06, 0x5561 },
2756
2757 /*
2758 * Can not link to 1Gbps with bad cable
2759 * Decrease SNR threshold form 21.07dB to 19.04dB
2760 */
2761 { 0x1f, 0x0001 },
2762 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002763
2764 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002765 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002766 };
2767
françois romieu4da19632011-01-03 15:07:55 +00002768 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002769
françois romieubca03d52011-01-03 15:07:31 +00002770 /*
2771 * Rx Error Issue
2772 * Fine Tune Switching regulator parameter
2773 */
françois romieu4da19632011-01-03 15:07:55 +00002774 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002775 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2776 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002777
Francois Romieufdf6fc02012-07-06 22:40:38 +02002778 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002779 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002780 { 0x1f, 0x0002 },
2781 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002782 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002783 { 0x05, 0x8330 },
2784 { 0x06, 0x669a },
2785 { 0x1f, 0x0002 }
2786 };
2787 int val;
2788
françois romieu4da19632011-01-03 15:07:55 +00002789 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
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
2793 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002794 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002795 0x0065, 0x0066, 0x0067, 0x0068,
2796 0x0069, 0x006a, 0x006b, 0x006c
2797 };
2798 int i;
2799
françois romieu4da19632011-01-03 15:07:55 +00002800 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002801
2802 val &= 0xff00;
2803 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002804 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002805 }
2806 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002807 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002808 { 0x1f, 0x0002 },
2809 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002810 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002811 { 0x05, 0x8330 },
2812 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002813 };
2814
françois romieu4da19632011-01-03 15:07:55 +00002815 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002816 }
2817
françois romieubca03d52011-01-03 15:07:31 +00002818 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002819 rtl_writephy(tp, 0x1f, 0x0002);
2820 rtl_patchphy(tp, 0x0d, 0x0300);
2821 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002822
françois romieubca03d52011-01-03 15:07:31 +00002823 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002824 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002825 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2826 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002827
françois romieu4da19632011-01-03 15:07:55 +00002828 rtl_writephy(tp, 0x1f, 0x0005);
2829 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002830
2831 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002832
françois romieu4da19632011-01-03 15:07:55 +00002833 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002834}
2835
françois romieubca03d52011-01-03 15:07:31 +00002836static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002837{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002838 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002839 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002840 { 0x1f, 0x0001 },
2841 { 0x06, 0x4064 },
2842 { 0x07, 0x2863 },
2843 { 0x08, 0x059c },
2844 { 0x09, 0x26b4 },
2845 { 0x0a, 0x6a19 },
2846 { 0x0b, 0xdcc8 },
2847 { 0x10, 0xf06d },
2848 { 0x14, 0x7f68 },
2849 { 0x18, 0x7fd9 },
2850 { 0x1c, 0xf0ff },
2851 { 0x1d, 0x3d9c },
2852 { 0x1f, 0x0003 },
2853 { 0x12, 0xf49f },
2854 { 0x13, 0x070b },
2855 { 0x1a, 0x05ad },
2856 { 0x14, 0x94c0 },
2857
françois romieubca03d52011-01-03 15:07:31 +00002858 /*
2859 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002860 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002861 */
françois romieudaf9df62009-10-07 12:44:20 +00002862 { 0x1f, 0x0002 },
2863 { 0x06, 0x5561 },
2864 { 0x1f, 0x0005 },
2865 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002866 { 0x06, 0x5561 },
2867
2868 /*
2869 * Can not link to 1Gbps with bad cable
2870 * Decrease SNR threshold form 21.07dB to 19.04dB
2871 */
2872 { 0x1f, 0x0001 },
2873 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002874
2875 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002876 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002877 };
2878
françois romieu4da19632011-01-03 15:07:55 +00002879 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002880
Francois Romieufdf6fc02012-07-06 22:40:38 +02002881 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002882 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002883 { 0x1f, 0x0002 },
2884 { 0x05, 0x669a },
2885 { 0x1f, 0x0005 },
2886 { 0x05, 0x8330 },
2887 { 0x06, 0x669a },
2888
2889 { 0x1f, 0x0002 }
2890 };
2891 int val;
2892
françois romieu4da19632011-01-03 15:07:55 +00002893 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002894
françois romieu4da19632011-01-03 15:07:55 +00002895 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002896 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002897 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002898 0x0065, 0x0066, 0x0067, 0x0068,
2899 0x0069, 0x006a, 0x006b, 0x006c
2900 };
2901 int i;
2902
françois romieu4da19632011-01-03 15:07:55 +00002903 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002904
2905 val &= 0xff00;
2906 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002907 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002908 }
2909 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002910 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002911 { 0x1f, 0x0002 },
2912 { 0x05, 0x2642 },
2913 { 0x1f, 0x0005 },
2914 { 0x05, 0x8330 },
2915 { 0x06, 0x2642 }
2916 };
2917
françois romieu4da19632011-01-03 15:07:55 +00002918 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002919 }
2920
françois romieubca03d52011-01-03 15:07:31 +00002921 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002922 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002923 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2924 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002925
françois romieubca03d52011-01-03 15:07:31 +00002926 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002927 rtl_writephy(tp, 0x1f, 0x0002);
2928 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002929
françois romieu4da19632011-01-03 15:07:55 +00002930 rtl_writephy(tp, 0x1f, 0x0005);
2931 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002932
2933 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002934
françois romieu4da19632011-01-03 15:07:55 +00002935 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002936}
2937
françois romieu4da19632011-01-03 15:07:55 +00002938static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002939{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002940 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002941 { 0x1f, 0x0002 },
2942 { 0x10, 0x0008 },
2943 { 0x0d, 0x006c },
2944
2945 { 0x1f, 0x0000 },
2946 { 0x0d, 0xf880 },
2947
2948 { 0x1f, 0x0001 },
2949 { 0x17, 0x0cc0 },
2950
2951 { 0x1f, 0x0001 },
2952 { 0x0b, 0xa4d8 },
2953 { 0x09, 0x281c },
2954 { 0x07, 0x2883 },
2955 { 0x0a, 0x6b35 },
2956 { 0x1d, 0x3da4 },
2957 { 0x1c, 0xeffd },
2958 { 0x14, 0x7f52 },
2959 { 0x18, 0x7fc6 },
2960 { 0x08, 0x0601 },
2961 { 0x06, 0x4063 },
2962 { 0x10, 0xf074 },
2963 { 0x1f, 0x0003 },
2964 { 0x13, 0x0789 },
2965 { 0x12, 0xf4bd },
2966 { 0x1a, 0x04fd },
2967 { 0x14, 0x84b0 },
2968 { 0x1f, 0x0000 },
2969 { 0x00, 0x9200 },
2970
2971 { 0x1f, 0x0005 },
2972 { 0x01, 0x0340 },
2973 { 0x1f, 0x0001 },
2974 { 0x04, 0x4000 },
2975 { 0x03, 0x1d21 },
2976 { 0x02, 0x0c32 },
2977 { 0x01, 0x0200 },
2978 { 0x00, 0x5554 },
2979 { 0x04, 0x4800 },
2980 { 0x04, 0x4000 },
2981 { 0x04, 0xf000 },
2982 { 0x03, 0xdf01 },
2983 { 0x02, 0xdf20 },
2984 { 0x01, 0x101a },
2985 { 0x00, 0xa0ff },
2986 { 0x04, 0xf800 },
2987 { 0x04, 0xf000 },
2988 { 0x1f, 0x0000 },
2989
2990 { 0x1f, 0x0007 },
2991 { 0x1e, 0x0023 },
2992 { 0x16, 0x0000 },
2993 { 0x1f, 0x0000 }
2994 };
2995
françois romieu4da19632011-01-03 15:07:55 +00002996 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002997}
2998
françois romieue6de30d2011-01-03 15:08:37 +00002999static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3000{
3001 static const struct phy_reg phy_reg_init[] = {
3002 { 0x1f, 0x0001 },
3003 { 0x17, 0x0cc0 },
3004
3005 { 0x1f, 0x0007 },
3006 { 0x1e, 0x002d },
3007 { 0x18, 0x0040 },
3008 { 0x1f, 0x0000 }
3009 };
3010
3011 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3012 rtl_patchphy(tp, 0x0d, 1 << 5);
3013}
3014
Hayes Wang70090422011-07-06 15:58:06 +08003015static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003016{
3017 static const struct phy_reg phy_reg_init[] = {
3018 /* Enable Delay cap */
3019 { 0x1f, 0x0005 },
3020 { 0x05, 0x8b80 },
3021 { 0x06, 0xc896 },
3022 { 0x1f, 0x0000 },
3023
3024 /* Channel estimation fine tune */
3025 { 0x1f, 0x0001 },
3026 { 0x0b, 0x6c20 },
3027 { 0x07, 0x2872 },
3028 { 0x1c, 0xefff },
3029 { 0x1f, 0x0003 },
3030 { 0x14, 0x6420 },
3031 { 0x1f, 0x0000 },
3032
3033 /* Update PFM & 10M TX idle timer */
3034 { 0x1f, 0x0007 },
3035 { 0x1e, 0x002f },
3036 { 0x15, 0x1919 },
3037 { 0x1f, 0x0000 },
3038
3039 { 0x1f, 0x0007 },
3040 { 0x1e, 0x00ac },
3041 { 0x18, 0x0006 },
3042 { 0x1f, 0x0000 }
3043 };
3044
Francois Romieu15ecd032011-04-27 13:52:22 -07003045 rtl_apply_firmware(tp);
3046
hayeswang01dc7fe2011-03-21 01:50:28 +00003047 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3048
3049 /* DCO enable for 10M IDLE Power */
3050 rtl_writephy(tp, 0x1f, 0x0007);
3051 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003052 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003053 rtl_writephy(tp, 0x1f, 0x0000);
3054
3055 /* For impedance matching */
3056 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003057 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003058 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003059
3060 /* PHY auto speed down */
3061 rtl_writephy(tp, 0x1f, 0x0007);
3062 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003063 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003064 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003065 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003066
3067 rtl_writephy(tp, 0x1f, 0x0005);
3068 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003069 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003070 rtl_writephy(tp, 0x1f, 0x0000);
3071
3072 rtl_writephy(tp, 0x1f, 0x0005);
3073 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003074 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003075 rtl_writephy(tp, 0x1f, 0x0007);
3076 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003077 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003078 rtl_writephy(tp, 0x1f, 0x0006);
3079 rtl_writephy(tp, 0x00, 0x5a00);
3080 rtl_writephy(tp, 0x1f, 0x0000);
3081 rtl_writephy(tp, 0x0d, 0x0007);
3082 rtl_writephy(tp, 0x0e, 0x003c);
3083 rtl_writephy(tp, 0x0d, 0x4007);
3084 rtl_writephy(tp, 0x0e, 0x0000);
3085 rtl_writephy(tp, 0x0d, 0x0000);
3086}
3087
françois romieu9ecb9aa2012-12-07 11:20:21 +00003088static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3089{
3090 const u16 w[] = {
3091 addr[0] | (addr[1] << 8),
3092 addr[2] | (addr[3] << 8),
3093 addr[4] | (addr[5] << 8)
3094 };
3095 const struct exgmac_reg e[] = {
3096 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3097 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3098 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3099 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3100 };
3101
3102 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3103}
3104
Hayes Wang70090422011-07-06 15:58:06 +08003105static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3106{
3107 static const struct phy_reg phy_reg_init[] = {
3108 /* Enable Delay cap */
3109 { 0x1f, 0x0004 },
3110 { 0x1f, 0x0007 },
3111 { 0x1e, 0x00ac },
3112 { 0x18, 0x0006 },
3113 { 0x1f, 0x0002 },
3114 { 0x1f, 0x0000 },
3115 { 0x1f, 0x0000 },
3116
3117 /* Channel estimation fine tune */
3118 { 0x1f, 0x0003 },
3119 { 0x09, 0xa20f },
3120 { 0x1f, 0x0000 },
3121 { 0x1f, 0x0000 },
3122
3123 /* Green Setting */
3124 { 0x1f, 0x0005 },
3125 { 0x05, 0x8b5b },
3126 { 0x06, 0x9222 },
3127 { 0x05, 0x8b6d },
3128 { 0x06, 0x8000 },
3129 { 0x05, 0x8b76 },
3130 { 0x06, 0x8000 },
3131 { 0x1f, 0x0000 }
3132 };
3133
3134 rtl_apply_firmware(tp);
3135
3136 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3137
3138 /* For 4-corner performance improve */
3139 rtl_writephy(tp, 0x1f, 0x0005);
3140 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003141 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003142 rtl_writephy(tp, 0x1f, 0x0000);
3143
3144 /* PHY auto speed down */
3145 rtl_writephy(tp, 0x1f, 0x0004);
3146 rtl_writephy(tp, 0x1f, 0x0007);
3147 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003148 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003149 rtl_writephy(tp, 0x1f, 0x0002);
3150 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003151 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003152
3153 /* improve 10M EEE waveform */
3154 rtl_writephy(tp, 0x1f, 0x0005);
3155 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003156 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003157 rtl_writephy(tp, 0x1f, 0x0000);
3158
3159 /* Improve 2-pair detection performance */
3160 rtl_writephy(tp, 0x1f, 0x0005);
3161 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003162 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003163 rtl_writephy(tp, 0x1f, 0x0000);
3164
3165 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003166 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003167 rtl_writephy(tp, 0x1f, 0x0005);
3168 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003169 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003170 rtl_writephy(tp, 0x1f, 0x0004);
3171 rtl_writephy(tp, 0x1f, 0x0007);
3172 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003173 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003174 rtl_writephy(tp, 0x1f, 0x0002);
3175 rtl_writephy(tp, 0x1f, 0x0000);
3176 rtl_writephy(tp, 0x0d, 0x0007);
3177 rtl_writephy(tp, 0x0e, 0x003c);
3178 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003179 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003180 rtl_writephy(tp, 0x0d, 0x0000);
3181
3182 /* Green feature */
3183 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003184 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3185 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003186 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003187 rtl_writephy(tp, 0x1f, 0x0005);
3188 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3189 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003190
françois romieu9ecb9aa2012-12-07 11:20:21 +00003191 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3192 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003193}
3194
Hayes Wang5f886e02012-03-30 14:33:03 +08003195static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3196{
3197 /* For 4-corner performance improve */
3198 rtl_writephy(tp, 0x1f, 0x0005);
3199 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003200 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003201 rtl_writephy(tp, 0x1f, 0x0000);
3202
3203 /* PHY auto speed down */
3204 rtl_writephy(tp, 0x1f, 0x0007);
3205 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003206 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003207 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003208 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003209
3210 /* Improve 10M EEE waveform */
3211 rtl_writephy(tp, 0x1f, 0x0005);
3212 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003213 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003214 rtl_writephy(tp, 0x1f, 0x0000);
3215}
3216
Hayes Wangc2218922011-09-06 16:55:18 +08003217static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3218{
3219 static const struct phy_reg phy_reg_init[] = {
3220 /* Channel estimation fine tune */
3221 { 0x1f, 0x0003 },
3222 { 0x09, 0xa20f },
3223 { 0x1f, 0x0000 },
3224
3225 /* Modify green table for giga & fnet */
3226 { 0x1f, 0x0005 },
3227 { 0x05, 0x8b55 },
3228 { 0x06, 0x0000 },
3229 { 0x05, 0x8b5e },
3230 { 0x06, 0x0000 },
3231 { 0x05, 0x8b67 },
3232 { 0x06, 0x0000 },
3233 { 0x05, 0x8b70 },
3234 { 0x06, 0x0000 },
3235 { 0x1f, 0x0000 },
3236 { 0x1f, 0x0007 },
3237 { 0x1e, 0x0078 },
3238 { 0x17, 0x0000 },
3239 { 0x19, 0x00fb },
3240 { 0x1f, 0x0000 },
3241
3242 /* Modify green table for 10M */
3243 { 0x1f, 0x0005 },
3244 { 0x05, 0x8b79 },
3245 { 0x06, 0xaa00 },
3246 { 0x1f, 0x0000 },
3247
3248 /* Disable hiimpedance detection (RTCT) */
3249 { 0x1f, 0x0003 },
3250 { 0x01, 0x328a },
3251 { 0x1f, 0x0000 }
3252 };
3253
3254 rtl_apply_firmware(tp);
3255
3256 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3257
Hayes Wang5f886e02012-03-30 14:33:03 +08003258 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003259
3260 /* Improve 2-pair detection performance */
3261 rtl_writephy(tp, 0x1f, 0x0005);
3262 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003263 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003264 rtl_writephy(tp, 0x1f, 0x0000);
3265}
3266
3267static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3268{
3269 rtl_apply_firmware(tp);
3270
Hayes Wang5f886e02012-03-30 14:33:03 +08003271 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003272}
3273
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003274static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3275{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003276 static const struct phy_reg phy_reg_init[] = {
3277 /* Channel estimation fine tune */
3278 { 0x1f, 0x0003 },
3279 { 0x09, 0xa20f },
3280 { 0x1f, 0x0000 },
3281
3282 /* Modify green table for giga & fnet */
3283 { 0x1f, 0x0005 },
3284 { 0x05, 0x8b55 },
3285 { 0x06, 0x0000 },
3286 { 0x05, 0x8b5e },
3287 { 0x06, 0x0000 },
3288 { 0x05, 0x8b67 },
3289 { 0x06, 0x0000 },
3290 { 0x05, 0x8b70 },
3291 { 0x06, 0x0000 },
3292 { 0x1f, 0x0000 },
3293 { 0x1f, 0x0007 },
3294 { 0x1e, 0x0078 },
3295 { 0x17, 0x0000 },
3296 { 0x19, 0x00aa },
3297 { 0x1f, 0x0000 },
3298
3299 /* Modify green table for 10M */
3300 { 0x1f, 0x0005 },
3301 { 0x05, 0x8b79 },
3302 { 0x06, 0xaa00 },
3303 { 0x1f, 0x0000 },
3304
3305 /* Disable hiimpedance detection (RTCT) */
3306 { 0x1f, 0x0003 },
3307 { 0x01, 0x328a },
3308 { 0x1f, 0x0000 }
3309 };
3310
3311
3312 rtl_apply_firmware(tp);
3313
3314 rtl8168f_hw_phy_config(tp);
3315
3316 /* Improve 2-pair detection performance */
3317 rtl_writephy(tp, 0x1f, 0x0005);
3318 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003319 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003320 rtl_writephy(tp, 0x1f, 0x0000);
3321
3322 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3323
3324 /* Modify green table for giga */
3325 rtl_writephy(tp, 0x1f, 0x0005);
3326 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003327 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003328 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003329 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003330 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003331 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003332 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003333 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003334 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003335 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003336 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003337 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003338 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003339 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003340 rtl_writephy(tp, 0x1f, 0x0000);
3341
3342 /* uc same-seed solution */
3343 rtl_writephy(tp, 0x1f, 0x0005);
3344 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003345 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003346 rtl_writephy(tp, 0x1f, 0x0000);
3347
3348 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003349 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003350 rtl_writephy(tp, 0x1f, 0x0005);
3351 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003352 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003353 rtl_writephy(tp, 0x1f, 0x0004);
3354 rtl_writephy(tp, 0x1f, 0x0007);
3355 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003356 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003357 rtl_writephy(tp, 0x1f, 0x0000);
3358 rtl_writephy(tp, 0x0d, 0x0007);
3359 rtl_writephy(tp, 0x0e, 0x003c);
3360 rtl_writephy(tp, 0x0d, 0x4007);
3361 rtl_writephy(tp, 0x0e, 0x0000);
3362 rtl_writephy(tp, 0x0d, 0x0000);
3363
3364 /* Green feature */
3365 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003366 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3367 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003368 rtl_writephy(tp, 0x1f, 0x0000);
3369}
3370
Hayes Wangc5583862012-07-02 17:23:22 +08003371static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3372{
Hayes Wangc5583862012-07-02 17:23:22 +08003373 rtl_apply_firmware(tp);
3374
hayeswang41f44d12013-04-01 22:23:36 +00003375 rtl_writephy(tp, 0x1f, 0x0a46);
3376 if (rtl_readphy(tp, 0x10) & 0x0100) {
3377 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003378 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003379 } else {
3380 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003381 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003382 }
Hayes Wangc5583862012-07-02 17:23:22 +08003383
hayeswang41f44d12013-04-01 22:23:36 +00003384 rtl_writephy(tp, 0x1f, 0x0a46);
3385 if (rtl_readphy(tp, 0x13) & 0x0100) {
3386 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003387 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003388 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003389 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003390 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003391 }
Hayes Wangc5583862012-07-02 17:23:22 +08003392
hayeswang41f44d12013-04-01 22:23:36 +00003393 /* Enable PHY auto speed down */
3394 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003395 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003396
hayeswangfe7524c2013-04-01 22:23:37 +00003397 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003398 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003399 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003400 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003401 rtl_writephy(tp, 0x1f, 0x0a43);
3402 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003403 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3404 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003405
hayeswang41f44d12013-04-01 22:23:36 +00003406 /* EEE auto-fallback function */
3407 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003408 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003409
hayeswang41f44d12013-04-01 22:23:36 +00003410 /* Enable UC LPF tune function */
3411 rtl_writephy(tp, 0x1f, 0x0a43);
3412 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003413 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003414
3415 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003416 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003417
hayeswangfe7524c2013-04-01 22:23:37 +00003418 /* Improve SWR Efficiency */
3419 rtl_writephy(tp, 0x1f, 0x0bcd);
3420 rtl_writephy(tp, 0x14, 0x5065);
3421 rtl_writephy(tp, 0x14, 0xd065);
3422 rtl_writephy(tp, 0x1f, 0x0bc8);
3423 rtl_writephy(tp, 0x11, 0x5655);
3424 rtl_writephy(tp, 0x1f, 0x0bcd);
3425 rtl_writephy(tp, 0x14, 0x1065);
3426 rtl_writephy(tp, 0x14, 0x9065);
3427 rtl_writephy(tp, 0x14, 0x1065);
3428
David Chang1bac1072013-11-27 15:48:36 +08003429 /* Check ALDPS bit, disable it if enabled */
3430 rtl_writephy(tp, 0x1f, 0x0a43);
3431 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003432 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003433
hayeswang41f44d12013-04-01 22:23:36 +00003434 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003435}
3436
hayeswang57538c42013-04-01 22:23:40 +00003437static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3438{
3439 rtl_apply_firmware(tp);
3440}
3441
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003442static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3443{
3444 u16 dout_tapbin;
3445 u32 data;
3446
3447 rtl_apply_firmware(tp);
3448
3449 /* CHN EST parameters adjust - giga master */
3450 rtl_writephy(tp, 0x1f, 0x0a43);
3451 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003452 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003453 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003454 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003455 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003456 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003457 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003458 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003459 rtl_writephy(tp, 0x1f, 0x0000);
3460
3461 /* CHN EST parameters adjust - giga slave */
3462 rtl_writephy(tp, 0x1f, 0x0a43);
3463 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003464 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003465 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003466 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003467 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003468 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003469 rtl_writephy(tp, 0x1f, 0x0000);
3470
3471 /* CHN EST parameters adjust - fnet */
3472 rtl_writephy(tp, 0x1f, 0x0a43);
3473 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003474 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003475 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003476 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003477 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003478 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003479 rtl_writephy(tp, 0x1f, 0x0000);
3480
3481 /* enable R-tune & PGA-retune function */
3482 dout_tapbin = 0;
3483 rtl_writephy(tp, 0x1f, 0x0a46);
3484 data = rtl_readphy(tp, 0x13);
3485 data &= 3;
3486 data <<= 2;
3487 dout_tapbin |= data;
3488 data = rtl_readphy(tp, 0x12);
3489 data &= 0xc000;
3490 data >>= 14;
3491 dout_tapbin |= data;
3492 dout_tapbin = ~(dout_tapbin^0x08);
3493 dout_tapbin <<= 12;
3494 dout_tapbin &= 0xf000;
3495 rtl_writephy(tp, 0x1f, 0x0a43);
3496 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003497 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003498 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003499 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003500 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003501 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003502 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003503 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003504
3505 rtl_writephy(tp, 0x1f, 0x0a43);
3506 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003507 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003508 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003509 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003510 rtl_writephy(tp, 0x1f, 0x0000);
3511
3512 /* enable GPHY 10M */
3513 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003514 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003515 rtl_writephy(tp, 0x1f, 0x0000);
3516
3517 /* SAR ADC performance */
3518 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003519 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003520 rtl_writephy(tp, 0x1f, 0x0000);
3521
3522 rtl_writephy(tp, 0x1f, 0x0a43);
3523 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003524 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003525 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003526 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003527 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003528 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003529 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003530 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003531 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003532 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003533 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003534 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003535 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003536 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003537 rtl_writephy(tp, 0x1f, 0x0000);
3538
3539 /* disable phy pfm mode */
3540 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003541 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003542 rtl_writephy(tp, 0x1f, 0x0000);
3543
3544 /* Check ALDPS bit, disable it if enabled */
3545 rtl_writephy(tp, 0x1f, 0x0a43);
3546 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003547 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003548
3549 rtl_writephy(tp, 0x1f, 0x0000);
3550}
3551
3552static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3553{
3554 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3555 u16 rlen;
3556 u32 data;
3557
3558 rtl_apply_firmware(tp);
3559
3560 /* CHIN EST parameter update */
3561 rtl_writephy(tp, 0x1f, 0x0a43);
3562 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003563 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003564 rtl_writephy(tp, 0x1f, 0x0000);
3565
3566 /* enable R-tune & PGA-retune function */
3567 rtl_writephy(tp, 0x1f, 0x0a43);
3568 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003569 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003570 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003571 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003572 rtl_writephy(tp, 0x1f, 0x0000);
3573
3574 /* enable GPHY 10M */
3575 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003576 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003577 rtl_writephy(tp, 0x1f, 0x0000);
3578
3579 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3580 data = r8168_mac_ocp_read(tp, 0xdd02);
3581 ioffset_p3 = ((data & 0x80)>>7);
3582 ioffset_p3 <<= 3;
3583
3584 data = r8168_mac_ocp_read(tp, 0xdd00);
3585 ioffset_p3 |= ((data & (0xe000))>>13);
3586 ioffset_p2 = ((data & (0x1e00))>>9);
3587 ioffset_p1 = ((data & (0x01e0))>>5);
3588 ioffset_p0 = ((data & 0x0010)>>4);
3589 ioffset_p0 <<= 3;
3590 ioffset_p0 |= (data & (0x07));
3591 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3592
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003593 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003594 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003595 rtl_writephy(tp, 0x1f, 0x0bcf);
3596 rtl_writephy(tp, 0x16, data);
3597 rtl_writephy(tp, 0x1f, 0x0000);
3598 }
3599
3600 /* Modify rlen (TX LPF corner frequency) level */
3601 rtl_writephy(tp, 0x1f, 0x0bcd);
3602 data = rtl_readphy(tp, 0x16);
3603 data &= 0x000f;
3604 rlen = 0;
3605 if (data > 3)
3606 rlen = data - 3;
3607 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3608 rtl_writephy(tp, 0x17, data);
3609 rtl_writephy(tp, 0x1f, 0x0bcd);
3610 rtl_writephy(tp, 0x1f, 0x0000);
3611
3612 /* disable phy pfm mode */
3613 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003614 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003615 rtl_writephy(tp, 0x1f, 0x0000);
3616
3617 /* Check ALDPS bit, disable it if enabled */
3618 rtl_writephy(tp, 0x1f, 0x0a43);
3619 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003620 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003621
3622 rtl_writephy(tp, 0x1f, 0x0000);
3623}
3624
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003625static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3626{
3627 /* Enable PHY auto speed down */
3628 rtl_writephy(tp, 0x1f, 0x0a44);
3629 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3630 rtl_writephy(tp, 0x1f, 0x0000);
3631
3632 /* patch 10M & ALDPS */
3633 rtl_writephy(tp, 0x1f, 0x0bcc);
3634 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3635 rtl_writephy(tp, 0x1f, 0x0a44);
3636 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3637 rtl_writephy(tp, 0x1f, 0x0a43);
3638 rtl_writephy(tp, 0x13, 0x8084);
3639 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3640 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3641 rtl_writephy(tp, 0x1f, 0x0000);
3642
3643 /* Enable EEE auto-fallback function */
3644 rtl_writephy(tp, 0x1f, 0x0a4b);
3645 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3646 rtl_writephy(tp, 0x1f, 0x0000);
3647
3648 /* Enable UC LPF tune function */
3649 rtl_writephy(tp, 0x1f, 0x0a43);
3650 rtl_writephy(tp, 0x13, 0x8012);
3651 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3652 rtl_writephy(tp, 0x1f, 0x0000);
3653
3654 /* set rg_sel_sdm_rate */
3655 rtl_writephy(tp, 0x1f, 0x0c42);
3656 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3657 rtl_writephy(tp, 0x1f, 0x0000);
3658
3659 /* Check ALDPS bit, disable it if enabled */
3660 rtl_writephy(tp, 0x1f, 0x0a43);
3661 if (rtl_readphy(tp, 0x10) & 0x0004)
3662 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3663
3664 rtl_writephy(tp, 0x1f, 0x0000);
3665}
3666
3667static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3668{
3669 /* patch 10M & ALDPS */
3670 rtl_writephy(tp, 0x1f, 0x0bcc);
3671 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3672 rtl_writephy(tp, 0x1f, 0x0a44);
3673 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3674 rtl_writephy(tp, 0x1f, 0x0a43);
3675 rtl_writephy(tp, 0x13, 0x8084);
3676 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3677 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3678 rtl_writephy(tp, 0x1f, 0x0000);
3679
3680 /* Enable UC LPF tune function */
3681 rtl_writephy(tp, 0x1f, 0x0a43);
3682 rtl_writephy(tp, 0x13, 0x8012);
3683 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3684 rtl_writephy(tp, 0x1f, 0x0000);
3685
3686 /* Set rg_sel_sdm_rate */
3687 rtl_writephy(tp, 0x1f, 0x0c42);
3688 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3689 rtl_writephy(tp, 0x1f, 0x0000);
3690
3691 /* Channel estimation parameters */
3692 rtl_writephy(tp, 0x1f, 0x0a43);
3693 rtl_writephy(tp, 0x13, 0x80f3);
3694 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3695 rtl_writephy(tp, 0x13, 0x80f0);
3696 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3697 rtl_writephy(tp, 0x13, 0x80ef);
3698 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3699 rtl_writephy(tp, 0x13, 0x80f6);
3700 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3701 rtl_writephy(tp, 0x13, 0x80ec);
3702 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3703 rtl_writephy(tp, 0x13, 0x80ed);
3704 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3705 rtl_writephy(tp, 0x13, 0x80f2);
3706 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3707 rtl_writephy(tp, 0x13, 0x80f4);
3708 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3709 rtl_writephy(tp, 0x1f, 0x0a43);
3710 rtl_writephy(tp, 0x13, 0x8110);
3711 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3712 rtl_writephy(tp, 0x13, 0x810f);
3713 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3714 rtl_writephy(tp, 0x13, 0x8111);
3715 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3716 rtl_writephy(tp, 0x13, 0x8113);
3717 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3718 rtl_writephy(tp, 0x13, 0x8115);
3719 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3720 rtl_writephy(tp, 0x13, 0x810e);
3721 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3722 rtl_writephy(tp, 0x13, 0x810c);
3723 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3724 rtl_writephy(tp, 0x13, 0x810b);
3725 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3726 rtl_writephy(tp, 0x1f, 0x0a43);
3727 rtl_writephy(tp, 0x13, 0x80d1);
3728 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3729 rtl_writephy(tp, 0x13, 0x80cd);
3730 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3731 rtl_writephy(tp, 0x13, 0x80d3);
3732 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3733 rtl_writephy(tp, 0x13, 0x80d5);
3734 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3735 rtl_writephy(tp, 0x13, 0x80d7);
3736 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3737
3738 /* Force PWM-mode */
3739 rtl_writephy(tp, 0x1f, 0x0bcd);
3740 rtl_writephy(tp, 0x14, 0x5065);
3741 rtl_writephy(tp, 0x14, 0xd065);
3742 rtl_writephy(tp, 0x1f, 0x0bc8);
3743 rtl_writephy(tp, 0x12, 0x00ed);
3744 rtl_writephy(tp, 0x1f, 0x0bcd);
3745 rtl_writephy(tp, 0x14, 0x1065);
3746 rtl_writephy(tp, 0x14, 0x9065);
3747 rtl_writephy(tp, 0x14, 0x1065);
3748 rtl_writephy(tp, 0x1f, 0x0000);
3749
3750 /* Check ALDPS bit, disable it if enabled */
3751 rtl_writephy(tp, 0x1f, 0x0a43);
3752 if (rtl_readphy(tp, 0x10) & 0x0004)
3753 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3754
3755 rtl_writephy(tp, 0x1f, 0x0000);
3756}
3757
françois romieu4da19632011-01-03 15:07:55 +00003758static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003759{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003760 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003761 { 0x1f, 0x0003 },
3762 { 0x08, 0x441d },
3763 { 0x01, 0x9100 },
3764 { 0x1f, 0x0000 }
3765 };
3766
françois romieu4da19632011-01-03 15:07:55 +00003767 rtl_writephy(tp, 0x1f, 0x0000);
3768 rtl_patchphy(tp, 0x11, 1 << 12);
3769 rtl_patchphy(tp, 0x19, 1 << 13);
3770 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003771
françois romieu4da19632011-01-03 15:07:55 +00003772 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003773}
3774
Hayes Wang5a5e4442011-02-22 17:26:21 +08003775static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3776{
3777 static const struct phy_reg phy_reg_init[] = {
3778 { 0x1f, 0x0005 },
3779 { 0x1a, 0x0000 },
3780 { 0x1f, 0x0000 },
3781
3782 { 0x1f, 0x0004 },
3783 { 0x1c, 0x0000 },
3784 { 0x1f, 0x0000 },
3785
3786 { 0x1f, 0x0001 },
3787 { 0x15, 0x7701 },
3788 { 0x1f, 0x0000 }
3789 };
3790
3791 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003792 rtl_writephy(tp, 0x1f, 0x0000);
3793 rtl_writephy(tp, 0x18, 0x0310);
3794 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003795
François Romieu953a12c2011-04-24 17:38:48 +02003796 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003797
3798 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3799}
3800
Hayes Wang7e18dca2012-03-30 14:33:02 +08003801static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3802{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003803 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003804 rtl_writephy(tp, 0x1f, 0x0000);
3805 rtl_writephy(tp, 0x18, 0x0310);
3806 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003807
3808 rtl_apply_firmware(tp);
3809
3810 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003811 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003812 rtl_writephy(tp, 0x1f, 0x0004);
3813 rtl_writephy(tp, 0x10, 0x401f);
3814 rtl_writephy(tp, 0x19, 0x7030);
3815 rtl_writephy(tp, 0x1f, 0x0000);
3816}
3817
Hayes Wang5598bfe2012-07-02 17:23:21 +08003818static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3819{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003820 static const struct phy_reg phy_reg_init[] = {
3821 { 0x1f, 0x0004 },
3822 { 0x10, 0xc07f },
3823 { 0x19, 0x7030 },
3824 { 0x1f, 0x0000 }
3825 };
3826
3827 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003828 rtl_writephy(tp, 0x1f, 0x0000);
3829 rtl_writephy(tp, 0x18, 0x0310);
3830 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003831
3832 rtl_apply_firmware(tp);
3833
Francois Romieufdf6fc02012-07-06 22:40:38 +02003834 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003835 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3836
Francois Romieufdf6fc02012-07-06 22:40:38 +02003837 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003838}
3839
Francois Romieu5615d9f2007-08-17 17:50:46 +02003840static void rtl_hw_phy_config(struct net_device *dev)
3841{
3842 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003843
Francois Romieu5615d9f2007-08-17 17:50:46 +02003844 switch (tp->mac_version) {
3845 case RTL_GIGA_MAC_VER_01:
3846 break;
3847 case RTL_GIGA_MAC_VER_02:
3848 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003849 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003850 break;
3851 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003852 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003853 break;
françois romieu2e9558562009-08-10 19:44:19 +00003854 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003855 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003856 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003857 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003858 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003859 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003860 case RTL_GIGA_MAC_VER_07:
3861 case RTL_GIGA_MAC_VER_08:
3862 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003863 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003864 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003865 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003866 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003867 break;
3868 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003869 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003870 break;
3871 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003872 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003873 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003874 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003875 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003876 break;
3877 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003878 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003879 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003880 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003881 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003882 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003883 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003884 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003885 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003886 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003887 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003888 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003889 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003890 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003891 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003892 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003893 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003894 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003895 break;
3896 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003897 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003898 break;
3899 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003900 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003901 break;
françois romieue6de30d2011-01-03 15:08:37 +00003902 case RTL_GIGA_MAC_VER_28:
3903 rtl8168d_4_hw_phy_config(tp);
3904 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003905 case RTL_GIGA_MAC_VER_29:
3906 case RTL_GIGA_MAC_VER_30:
3907 rtl8105e_hw_phy_config(tp);
3908 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003909 case RTL_GIGA_MAC_VER_31:
3910 /* None. */
3911 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003912 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003913 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003914 rtl8168e_1_hw_phy_config(tp);
3915 break;
3916 case RTL_GIGA_MAC_VER_34:
3917 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003918 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003919 case RTL_GIGA_MAC_VER_35:
3920 rtl8168f_1_hw_phy_config(tp);
3921 break;
3922 case RTL_GIGA_MAC_VER_36:
3923 rtl8168f_2_hw_phy_config(tp);
3924 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003925
Hayes Wang7e18dca2012-03-30 14:33:02 +08003926 case RTL_GIGA_MAC_VER_37:
3927 rtl8402_hw_phy_config(tp);
3928 break;
3929
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003930 case RTL_GIGA_MAC_VER_38:
3931 rtl8411_hw_phy_config(tp);
3932 break;
3933
Hayes Wang5598bfe2012-07-02 17:23:21 +08003934 case RTL_GIGA_MAC_VER_39:
3935 rtl8106e_hw_phy_config(tp);
3936 break;
3937
Hayes Wangc5583862012-07-02 17:23:22 +08003938 case RTL_GIGA_MAC_VER_40:
3939 rtl8168g_1_hw_phy_config(tp);
3940 break;
hayeswang57538c42013-04-01 22:23:40 +00003941 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00003942 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08003943 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00003944 rtl8168g_2_hw_phy_config(tp);
3945 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003946 case RTL_GIGA_MAC_VER_45:
3947 case RTL_GIGA_MAC_VER_47:
3948 rtl8168h_1_hw_phy_config(tp);
3949 break;
3950 case RTL_GIGA_MAC_VER_46:
3951 case RTL_GIGA_MAC_VER_48:
3952 rtl8168h_2_hw_phy_config(tp);
3953 break;
Hayes Wangc5583862012-07-02 17:23:22 +08003954
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003955 case RTL_GIGA_MAC_VER_49:
3956 rtl8168ep_1_hw_phy_config(tp);
3957 break;
3958 case RTL_GIGA_MAC_VER_50:
3959 case RTL_GIGA_MAC_VER_51:
3960 rtl8168ep_2_hw_phy_config(tp);
3961 break;
3962
Hayes Wangc5583862012-07-02 17:23:22 +08003963 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02003964 default:
3965 break;
3966 }
3967}
3968
Francois Romieuda78dbf2012-01-26 14:18:23 +01003969static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3970{
Francois Romieuda78dbf2012-01-26 14:18:23 +01003971 if (!test_and_set_bit(flag, tp->wk.flags))
3972 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01003973}
3974
David S. Miller8decf862011-09-22 03:23:13 -04003975static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3976{
David S. Miller8decf862011-09-22 03:23:13 -04003977 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Heiner Kallweite3972862018-06-29 08:07:04 +02003978 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04003979}
3980
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003981static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
Francois Romieu5615d9f2007-08-17 17:50:46 +02003983 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003984
Marcus Sundberg773328942008-07-10 21:28:08 +02003985 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit7a67e112018-09-19 22:00:24 +02003986 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3987 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Heiner Kallweit49d17512018-06-28 20:36:15 +02003988 netif_dbg(tp, drv, dev,
3989 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02003990 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02003991 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003992
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02003993 /* We may have called phy_speed_down before */
3994 phy_speed_up(dev->phydev);
3995
Heiner Kallweitf75222b2018-07-17 22:51:41 +02003996 genphy_soft_reset(dev->phydev);
Heiner Kallweit10bc6a62018-09-20 22:47:09 +02003997
Alex Xu (Hello71)9003b362018-09-30 11:06:39 -04003998 /* It was reported that several chips end up with 10MBit/Half on a
Heiner Kallweit10bc6a62018-09-20 22:47:09 +02003999 * 1GBit link after resuming from S3. For whatever reason the PHY on
Alex Xu (Hello71)9003b362018-09-30 11:06:39 -04004000 * these chips doesn't properly start a renegotiation when soft-reset.
Heiner Kallweit10bc6a62018-09-20 22:47:09 +02004001 * Explicitly requesting a renegotiation fixes this.
4002 */
Alex Xu (Hello71)9003b362018-09-30 11:06:39 -04004003 if (dev->phydev->autoneg == AUTONEG_ENABLE)
Heiner Kallweit10bc6a62018-09-20 22:47:09 +02004004 phy_restart_aneg(dev->phydev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004005}
4006
Francois Romieu773d2022007-01-31 23:47:43 +01004007static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4008{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004009 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004010
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004011 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004012
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004013 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4014 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004015
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004016 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4017 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004018
françois romieu9ecb9aa2012-12-07 11:20:21 +00004019 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4020 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004021
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004022 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004023
Francois Romieuda78dbf2012-01-26 14:18:23 +01004024 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004025}
4026
4027static int rtl_set_mac_address(struct net_device *dev, void *p)
4028{
4029 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004030 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004031 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004032
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004033 ret = eth_mac_addr(dev, p);
4034 if (ret)
4035 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004036
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004037 pm_runtime_get_noresume(d);
4038
4039 if (pm_runtime_active(d))
4040 rtl_rar_set(tp, dev->dev_addr);
4041
4042 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004043
4044 return 0;
4045}
4046
Heiner Kallweite3972862018-06-29 08:07:04 +02004047static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004048{
Heiner Kallweit69b3c592018-07-17 22:51:53 +02004049 if (!netif_running(dev))
4050 return -ENODEV;
Heiner Kallweite3972862018-06-29 08:07:04 +02004051
Heiner Kallweit69b3c592018-07-17 22:51:53 +02004052 return phy_mii_ioctl(dev->phydev, ifr, cmd);
Francois Romieu8b4ab282008-11-19 22:05:25 -08004053}
4054
Bill Pembertonbaf63292012-12-03 09:23:28 -05004055static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004056{
4057 struct mdio_ops *ops = &tp->mdio_ops;
4058
4059 switch (tp->mac_version) {
4060 case RTL_GIGA_MAC_VER_27:
4061 ops->write = r8168dp_1_mdio_write;
4062 ops->read = r8168dp_1_mdio_read;
4063 break;
françois romieue6de30d2011-01-03 15:08:37 +00004064 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004065 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004066 ops->write = r8168dp_2_mdio_write;
4067 ops->read = r8168dp_2_mdio_read;
4068 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004069 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004070 ops->write = r8168g_mdio_write;
4071 ops->read = r8168g_mdio_read;
4072 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004073 default:
4074 ops->write = r8169_mdio_write;
4075 ops->read = r8169_mdio_read;
4076 break;
4077 }
4078}
4079
David S. Miller1805b2f2011-10-24 18:18:09 -04004080static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4081{
David S. Miller1805b2f2011-10-24 18:18:09 -04004082 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004083 case RTL_GIGA_MAC_VER_25:
4084 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004085 case RTL_GIGA_MAC_VER_29:
4086 case RTL_GIGA_MAC_VER_30:
4087 case RTL_GIGA_MAC_VER_32:
4088 case RTL_GIGA_MAC_VER_33:
4089 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004090 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004091 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004092 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4093 break;
4094 default:
4095 break;
4096 }
4097}
4098
4099static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4100{
Heiner Kallweit649f0832018-10-25 18:40:19 +02004101 struct phy_device *phydev;
4102
4103 if (!__rtl8169_get_wol(tp))
David S. Miller1805b2f2011-10-24 18:18:09 -04004104 return false;
4105
Heiner Kallweit649f0832018-10-25 18:40:19 +02004106 /* phydev may not be attached to netdevice */
4107 phydev = mdiobus_get_phy(tp->mii_bus, 0);
4108
4109 phy_speed_down(phydev, false);
David S. Miller1805b2f2011-10-24 18:18:09 -04004110 rtl_wol_suspend_quirk(tp);
4111
4112 return true;
4113}
4114
françois romieu065c27c2011-01-03 15:08:12 +00004115static void r8168_pll_power_down(struct rtl8169_private *tp)
4116{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004117 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004118 return;
4119
hayeswang01dc7fe2011-03-21 01:50:28 +00004120 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4121 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004122 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004123
David S. Miller1805b2f2011-10-24 18:18:09 -04004124 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004125 return;
françois romieu065c27c2011-01-03 15:08:12 +00004126
françois romieu065c27c2011-01-03 15:08:12 +00004127 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004128 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004129 case RTL_GIGA_MAC_VER_37:
4130 case RTL_GIGA_MAC_VER_39:
4131 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004132 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004133 case RTL_GIGA_MAC_VER_45:
4134 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004135 case RTL_GIGA_MAC_VER_47:
4136 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004137 case RTL_GIGA_MAC_VER_50:
4138 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004139 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004140 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004141 case RTL_GIGA_MAC_VER_40:
4142 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004143 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004144 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004145 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004146 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004147 break;
françois romieu065c27c2011-01-03 15:08:12 +00004148 }
4149}
4150
4151static void r8168_pll_power_up(struct rtl8169_private *tp)
4152{
françois romieu065c27c2011-01-03 15:08:12 +00004153 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004154 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004155 case RTL_GIGA_MAC_VER_37:
4156 case RTL_GIGA_MAC_VER_39:
4157 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004158 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004159 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004160 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004161 case RTL_GIGA_MAC_VER_45:
4162 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004163 case RTL_GIGA_MAC_VER_47:
4164 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004165 case RTL_GIGA_MAC_VER_50:
4166 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004167 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004168 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004169 case RTL_GIGA_MAC_VER_40:
4170 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004171 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004172 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004173 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004174 0x00000000, ERIAR_EXGMAC);
4175 break;
françois romieu065c27c2011-01-03 15:08:12 +00004176 }
4177
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02004178 phy_resume(tp->dev->phydev);
4179 /* give MAC/PHY some time to resume */
4180 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00004181}
4182
françois romieu065c27c2011-01-03 15:08:12 +00004183static void rtl_pll_power_down(struct rtl8169_private *tp)
4184{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004185 switch (tp->mac_version) {
4186 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4187 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4188 break;
4189 default:
4190 r8168_pll_power_down(tp);
4191 }
françois romieu065c27c2011-01-03 15:08:12 +00004192}
4193
4194static void rtl_pll_power_up(struct rtl8169_private *tp)
4195{
françois romieu065c27c2011-01-03 15:08:12 +00004196 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004197 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4198 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004199 break;
françois romieu065c27c2011-01-03 15:08:12 +00004200 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004201 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004202 }
4203}
4204
Hayes Wange542a222011-07-06 15:58:04 +08004205static void rtl_init_rxcfg(struct rtl8169_private *tp)
4206{
Hayes Wange542a222011-07-06 15:58:04 +08004207 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004208 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4209 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004210 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004211 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004212 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
Maciej S. Szmigiero511cfd52018-10-11 16:02:10 +02004213 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
4214 case RTL_GIGA_MAC_VER_38:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004215 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004216 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004217 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004218 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004219 break;
Hayes Wange542a222011-07-06 15:58:04 +08004220 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004221 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004222 break;
4223 }
4224}
4225
Hayes Wang92fc43b2011-07-06 15:58:03 +08004226static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4227{
Timo Teräs9fba0812013-01-15 21:01:24 +00004228 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004229}
4230
Francois Romieud58d46b2011-05-03 16:38:29 +02004231static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4232{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004233 if (tp->jumbo_ops.enable) {
4234 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4235 tp->jumbo_ops.enable(tp);
4236 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4237 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004238}
4239
4240static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4241{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004242 if (tp->jumbo_ops.disable) {
4243 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4244 tp->jumbo_ops.disable(tp);
4245 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4246 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004247}
4248
4249static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4250{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004251 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4252 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004253 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004254}
4255
4256static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4257{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004258 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4259 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004260 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004261}
4262
4263static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4264{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004265 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004266}
4267
4268static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4269{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004270 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004271}
4272
4273static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4274{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004275 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4276 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4277 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004278 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004279}
4280
4281static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4282{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004283 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4284 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4285 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004286 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004287}
4288
4289static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4290{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004291 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004292 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004293}
4294
4295static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4296{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004297 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004298 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004299}
4300
4301static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4302{
Francois Romieud58d46b2011-05-03 16:38:29 +02004303 r8168b_0_hw_jumbo_enable(tp);
4304
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004305 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004306}
4307
4308static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4309{
Francois Romieud58d46b2011-05-03 16:38:29 +02004310 r8168b_0_hw_jumbo_disable(tp);
4311
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004312 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004313}
4314
Bill Pembertonbaf63292012-12-03 09:23:28 -05004315static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004316{
4317 struct jumbo_ops *ops = &tp->jumbo_ops;
4318
4319 switch (tp->mac_version) {
4320 case RTL_GIGA_MAC_VER_11:
4321 ops->disable = r8168b_0_hw_jumbo_disable;
4322 ops->enable = r8168b_0_hw_jumbo_enable;
4323 break;
4324 case RTL_GIGA_MAC_VER_12:
4325 case RTL_GIGA_MAC_VER_17:
4326 ops->disable = r8168b_1_hw_jumbo_disable;
4327 ops->enable = r8168b_1_hw_jumbo_enable;
4328 break;
4329 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4330 case RTL_GIGA_MAC_VER_19:
4331 case RTL_GIGA_MAC_VER_20:
4332 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4333 case RTL_GIGA_MAC_VER_22:
4334 case RTL_GIGA_MAC_VER_23:
4335 case RTL_GIGA_MAC_VER_24:
4336 case RTL_GIGA_MAC_VER_25:
4337 case RTL_GIGA_MAC_VER_26:
4338 ops->disable = r8168c_hw_jumbo_disable;
4339 ops->enable = r8168c_hw_jumbo_enable;
4340 break;
4341 case RTL_GIGA_MAC_VER_27:
4342 case RTL_GIGA_MAC_VER_28:
4343 ops->disable = r8168dp_hw_jumbo_disable;
4344 ops->enable = r8168dp_hw_jumbo_enable;
4345 break;
4346 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4347 case RTL_GIGA_MAC_VER_32:
4348 case RTL_GIGA_MAC_VER_33:
4349 case RTL_GIGA_MAC_VER_34:
4350 ops->disable = r8168e_hw_jumbo_disable;
4351 ops->enable = r8168e_hw_jumbo_enable;
4352 break;
4353
4354 /*
4355 * No action needed for jumbo frames with 8169.
4356 * No jumbo for 810x at all.
4357 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004358 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004359 default:
4360 ops->disable = NULL;
4361 ops->enable = NULL;
4362 break;
4363 }
4364}
4365
Francois Romieuffc46952012-07-06 14:19:23 +02004366DECLARE_RTL_COND(rtl_chipcmd_cond)
4367{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004368 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004369}
4370
Francois Romieu6f43adc2011-04-29 15:05:51 +02004371static void rtl_hw_reset(struct rtl8169_private *tp)
4372{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004373 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004374
Francois Romieuffc46952012-07-06 14:19:23 +02004375 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004376}
4377
Francois Romieub6ffd972011-06-17 17:00:05 +02004378static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4379{
4380 struct rtl_fw *rtl_fw;
4381 const char *name;
4382 int rc = -ENOMEM;
4383
4384 name = rtl_lookup_firmware_name(tp);
4385 if (!name)
4386 goto out_no_firmware;
4387
4388 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4389 if (!rtl_fw)
4390 goto err_warn;
4391
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004392 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004393 if (rc < 0)
4394 goto err_free;
4395
Francois Romieufd112f22011-06-18 00:10:29 +02004396 rc = rtl_check_firmware(tp, rtl_fw);
4397 if (rc < 0)
4398 goto err_release_firmware;
4399
Francois Romieub6ffd972011-06-17 17:00:05 +02004400 tp->rtl_fw = rtl_fw;
4401out:
4402 return;
4403
Francois Romieufd112f22011-06-18 00:10:29 +02004404err_release_firmware:
4405 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004406err_free:
4407 kfree(rtl_fw);
4408err_warn:
4409 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4410 name, rc);
4411out_no_firmware:
4412 tp->rtl_fw = NULL;
4413 goto out;
4414}
4415
François Romieu953a12c2011-04-24 17:38:48 +02004416static void rtl_request_firmware(struct rtl8169_private *tp)
4417{
Francois Romieub6ffd972011-06-17 17:00:05 +02004418 if (IS_ERR(tp->rtl_fw))
4419 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004420}
4421
Hayes Wang92fc43b2011-07-06 15:58:03 +08004422static void rtl_rx_close(struct rtl8169_private *tp)
4423{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004424 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004425}
4426
Francois Romieuffc46952012-07-06 14:19:23 +02004427DECLARE_RTL_COND(rtl_npq_cond)
4428{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004429 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02004430}
4431
4432DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4433{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004434 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02004435}
4436
françois romieue6de30d2011-01-03 15:08:37 +00004437static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
4439 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004440 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Hayes Wang92fc43b2011-07-06 15:58:03 +08004442 rtl_rx_close(tp);
4443
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004444 switch (tp->mac_version) {
4445 case RTL_GIGA_MAC_VER_27:
4446 case RTL_GIGA_MAC_VER_28:
4447 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02004448 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004449 break;
4450 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4451 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004452 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004453 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004454 break;
4455 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004456 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004457 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004458 break;
françois romieue6de30d2011-01-03 15:08:37 +00004459 }
4460
Hayes Wang92fc43b2011-07-06 15:58:03 +08004461 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004464static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004465{
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004466 u32 val = TX_DMA_BURST << TxDMAShift |
4467 InterFrameGap << TxInterFrameGapShift;
4468
4469 if (tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
4470 tp->mac_version != RTL_GIGA_MAC_VER_39)
4471 val |= TXCFG_AUTO_FIFO;
4472
4473 RTL_W32(tp, TxConfig, val);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004474}
4475
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004476static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004478 /* Low hurts. Let's disable the filtering. */
4479 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004480}
4481
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004482static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004483{
4484 /*
4485 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4486 * register to be written before TxDescAddrLow to work.
4487 * Switching from MMIO to I/O access fixes the issue as well.
4488 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004489 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4490 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4491 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4492 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004493}
4494
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004495static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004496{
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004497 u32 val;
Francois Romieu6dccd162007-02-13 23:38:05 +01004498
Heiner Kallweit34bc0092018-10-12 23:23:57 +02004499 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4500 val = 0x000fff00;
4501 else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
4502 val = 0x00ffff00;
4503 else
4504 return;
4505
4506 if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
4507 val |= 0xff;
4508
4509 RTL_W32(tp, 0x7c, val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004510}
4511
Francois Romieue6b763e2012-03-08 09:35:39 +01004512static void rtl_set_rx_mode(struct net_device *dev)
4513{
4514 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01004515 u32 mc_filter[2]; /* Multicast hash filter */
4516 int rx_mode;
4517 u32 tmp = 0;
4518
4519 if (dev->flags & IFF_PROMISC) {
4520 /* Unconditionally log net taps. */
4521 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4522 rx_mode =
4523 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4524 AcceptAllPhys;
4525 mc_filter[1] = mc_filter[0] = 0xffffffff;
4526 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4527 (dev->flags & IFF_ALLMULTI)) {
4528 /* Too many to filter perfectly -- accept all multicasts. */
4529 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4530 mc_filter[1] = mc_filter[0] = 0xffffffff;
4531 } else {
4532 struct netdev_hw_addr *ha;
4533
4534 rx_mode = AcceptBroadcast | AcceptMyPhys;
4535 mc_filter[1] = mc_filter[0] = 0;
4536 netdev_for_each_mc_addr(ha, dev) {
4537 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4538 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4539 rx_mode |= AcceptMulticast;
4540 }
4541 }
4542
4543 if (dev->features & NETIF_F_RXALL)
4544 rx_mode |= (AcceptErr | AcceptRunt);
4545
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004546 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01004547
4548 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4549 u32 data = mc_filter[0];
4550
4551 mc_filter[0] = swab32(mc_filter[1]);
4552 mc_filter[1] = swab32(data);
4553 }
4554
Nathan Walp04817762012-11-01 12:08:47 +00004555 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4556 mc_filter[1] = mc_filter[0] = 0xffffffff;
4557
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004558 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4559 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004560
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004561 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004562}
4563
Heiner Kallweit52f85602018-05-19 10:29:33 +02004564static void rtl_hw_start(struct rtl8169_private *tp)
4565{
4566 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4567
4568 tp->hw_start(tp);
4569
4570 rtl_set_rx_max_size(tp);
4571 rtl_set_rx_tx_desc_registers(tp);
Heiner Kallweit52f85602018-05-19 10:29:33 +02004572 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4573
4574 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4575 RTL_R8(tp, IntrMask);
4576 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004577 rtl_init_rxcfg(tp);
Maciej S. Szmigierof74dd482018-09-07 20:15:22 +02004578 rtl_set_tx_config_registers(tp);
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004579
Heiner Kallweit52f85602018-05-19 10:29:33 +02004580 rtl_set_rx_mode(tp->dev);
4581 /* no early-rx interrupts */
4582 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
Heiner Kallweitfe716f82018-11-19 22:31:32 +01004583 rtl_irq_enable(tp);
Heiner Kallweit52f85602018-05-19 10:29:33 +02004584}
4585
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004586static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01004587{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004588 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004589 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004590
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004591 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004593 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004594
Francois Romieucecb5fd2011-04-01 10:21:07 +02004595 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4596 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004597 netif_dbg(tp, drv, tp->dev,
4598 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004599 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 }
4601
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004602 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02004603
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004604 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01004605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 /*
4607 * Undocumented corner. Supposedly:
4608 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4609 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004610 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004612 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01004613}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Francois Romieuffc46952012-07-06 14:19:23 +02004615DECLARE_RTL_COND(rtl_csiar_cond)
4616{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004617 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02004618}
4619
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004620static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004621{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004622 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4623
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004624 RTL_W32(tp, CSIDR, value);
4625 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004626 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004627
Francois Romieuffc46952012-07-06 14:19:23 +02004628 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004629}
4630
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004631static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004632{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004633 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4634
4635 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4636 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004637
Francois Romieuffc46952012-07-06 14:19:23 +02004638 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004639 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004640}
4641
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004642static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004643{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004644 struct pci_dev *pdev = tp->pci_dev;
4645 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004646
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004647 /* According to Realtek the value at config space address 0x070f
4648 * controls the L0s/L1 entrance latency. We try standard ECAM access
4649 * first and if it fails fall back to CSI.
4650 */
4651 if (pdev->cfg_size > 0x070f &&
4652 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4653 return;
4654
4655 netdev_notice_once(tp->dev,
4656 "No native access to PCI extended config space, falling back to CSI\n");
4657 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4658 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004659}
4660
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004661static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08004662{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004663 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02004664}
4665
4666struct ephy_info {
4667 unsigned int offset;
4668 u16 mask;
4669 u16 bits;
4670};
4671
Francois Romieufdf6fc02012-07-06 22:40:38 +02004672static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4673 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004674{
4675 u16 w;
4676
4677 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004678 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4679 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004680 e++;
4681 }
4682}
4683
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004684static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02004685{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004686 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004687 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004688}
4689
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004690static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004691{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004692 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004693 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004694}
4695
hayeswangb51ecea2014-07-09 14:52:51 +08004696static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
4697{
hayeswangb51ecea2014-07-09 14:52:51 +08004698 u8 data;
4699
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004700 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08004701
4702 if (enable)
4703 data |= Rdy_to_L23;
4704 else
4705 data &= ~Rdy_to_L23;
4706
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004707 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08004708}
4709
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004710static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4711{
4712 if (enable) {
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004713 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
Kai-Heng Feng94235462018-09-12 14:58:20 +08004714 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004715 } else {
4716 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4717 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4718 }
Kai-Heng Feng94235462018-09-12 14:58:20 +08004719
4720 udelay(10);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004721}
4722
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004723static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004724{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004725 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004726
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004727 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004728 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02004729
françois romieufaf1e782013-02-27 13:01:57 +00004730 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004731 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00004732 PCI_EXP_DEVCTL_NOSNOOP_EN);
4733 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004734}
4735
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004736static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004737{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004738 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004739
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004740 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004741
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004742 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004743}
4744
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004745static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004746{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004747 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02004748
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004749 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004750
françois romieufaf1e782013-02-27 13:01:57 +00004751 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004752 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02004753
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004754 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004755
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004756 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004757 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02004758}
4759
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004760static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004761{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004762 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004763 { 0x01, 0, 0x0001 },
4764 { 0x02, 0x0800, 0x1000 },
4765 { 0x03, 0, 0x0042 },
4766 { 0x06, 0x0080, 0x0000 },
4767 { 0x07, 0, 0x2000 }
4768 };
4769
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004770 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004771
Francois Romieufdf6fc02012-07-06 22:40:38 +02004772 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02004773
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004774 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004775}
4776
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004777static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004778{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004779 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004780
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004781 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02004782
françois romieufaf1e782013-02-27 13:01:57 +00004783 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004784 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02004785
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004786 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004787 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02004788}
4789
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004790static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004791{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004792 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004793
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004794 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004795
4796 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004797 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004798
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004799 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004800
françois romieufaf1e782013-02-27 13:01:57 +00004801 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004802 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004803
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004804 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004805 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004806}
4807
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004808static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004809{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004810 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004811 { 0x02, 0x0800, 0x1000 },
4812 { 0x03, 0, 0x0002 },
4813 { 0x06, 0x0080, 0x0000 }
4814 };
4815
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004816 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004817
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004818 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02004819
Francois Romieufdf6fc02012-07-06 22:40:38 +02004820 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02004821
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004822 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004823}
4824
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004825static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004826{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004827 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004828 { 0x01, 0, 0x0001 },
4829 { 0x03, 0x0400, 0x0220 }
4830 };
4831
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004832 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004833
Francois Romieufdf6fc02012-07-06 22:40:38 +02004834 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02004835
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004836 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004837}
4838
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004839static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004840{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004841 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004842}
4843
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004844static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004845{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004846 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004847
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004848 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004849}
4850
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004851static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004852{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004853 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004854
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004855 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004856
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004857 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004858
françois romieufaf1e782013-02-27 13:01:57 +00004859 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004860 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02004861
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004862 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004863 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02004864}
4865
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004866static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004867{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004868 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004869
françois romieufaf1e782013-02-27 13:01:57 +00004870 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004871 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00004872
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004873 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00004874
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004875 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004876}
4877
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004878static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004879{
4880 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004881 { 0x0b, 0x0000, 0x0048 },
4882 { 0x19, 0x0020, 0x0050 },
4883 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00004884 };
françois romieue6de30d2011-01-03 15:08:37 +00004885
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004886 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004887
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004888 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00004889
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004890 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00004891
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004892 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00004893
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004894 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004895}
4896
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004897static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004898{
Hayes Wang70090422011-07-06 15:58:06 +08004899 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004900 { 0x00, 0x0200, 0x0100 },
4901 { 0x00, 0x0000, 0x0004 },
4902 { 0x06, 0x0002, 0x0001 },
4903 { 0x06, 0x0000, 0x0030 },
4904 { 0x07, 0x0000, 0x2000 },
4905 { 0x00, 0x0000, 0x0020 },
4906 { 0x03, 0x5800, 0x2000 },
4907 { 0x03, 0x0000, 0x0001 },
4908 { 0x01, 0x0800, 0x1000 },
4909 { 0x07, 0x0000, 0x4000 },
4910 { 0x1e, 0x0000, 0x2000 },
4911 { 0x19, 0xffff, 0xfe6c },
4912 { 0x0a, 0x0000, 0x0040 }
4913 };
4914
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004915 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004916
Francois Romieufdf6fc02012-07-06 22:40:38 +02004917 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004918
françois romieufaf1e782013-02-27 13:01:57 +00004919 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004920 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00004921
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004922 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00004923
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004924 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004925
4926 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004927 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4928 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004929
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004930 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004931}
4932
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004933static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004934{
4935 static const struct ephy_info e_info_8168e_2[] = {
4936 { 0x09, 0x0000, 0x0080 },
4937 { 0x19, 0x0000, 0x0224 }
4938 };
4939
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004940 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08004941
Francois Romieufdf6fc02012-07-06 22:40:38 +02004942 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08004943
françois romieufaf1e782013-02-27 13:01:57 +00004944 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004945 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08004946
Francois Romieufdf6fc02012-07-06 22:40:38 +02004947 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4948 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4949 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4950 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4951 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4952 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004953 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4954 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08004955
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004956 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004957
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004958 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004959
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004960 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08004961
4962 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004963 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08004964
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004965 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4966 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4967 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02004968
4969 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08004970}
4971
Hayes Wang5f886e02012-03-30 14:33:03 +08004972static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08004973{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004974 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08004975
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004976 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08004977
Francois Romieufdf6fc02012-07-06 22:40:38 +02004978 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4979 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4980 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4981 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004982 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
4983 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
4984 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4985 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02004986 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4987 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08004988
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004989 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08004990
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004991 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00004992
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004993 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
4994 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4995 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4996 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08004997}
4998
Hayes Wang5f886e02012-03-30 14:33:03 +08004999static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5000{
Hayes Wang5f886e02012-03-30 14:33:03 +08005001 static const struct ephy_info e_info_8168f_1[] = {
5002 { 0x06, 0x00c0, 0x0020 },
5003 { 0x08, 0x0001, 0x0002 },
5004 { 0x09, 0x0000, 0x0080 },
5005 { 0x19, 0x0000, 0x0224 }
5006 };
5007
5008 rtl_hw_start_8168f(tp);
5009
Francois Romieufdf6fc02012-07-06 22:40:38 +02005010 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005011
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005012 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005013
5014 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005015 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005016}
5017
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005018static void rtl_hw_start_8411(struct rtl8169_private *tp)
5019{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005020 static const struct ephy_info e_info_8168f_1[] = {
5021 { 0x06, 0x00c0, 0x0020 },
5022 { 0x0f, 0xffff, 0x5200 },
5023 { 0x1e, 0x0000, 0x4000 },
5024 { 0x19, 0x0000, 0x0224 }
5025 };
5026
5027 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005028 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005029
Francois Romieufdf6fc02012-07-06 22:40:38 +02005030 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005031
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005032 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005033}
5034
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005035static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005036{
Hayes Wangc5583862012-07-02 17:23:22 +08005037 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5038 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5039 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5040 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5041
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005042 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08005043
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005044 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005045
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005046 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5047 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005048 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005049
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005050 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5051 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005052
5053 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5054 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5055
5056 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005057 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005058
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005059 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5060 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005061
5062 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005063}
5064
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005065static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5066{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005067 static const struct ephy_info e_info_8168g_1[] = {
5068 { 0x00, 0x0000, 0x0008 },
5069 { 0x0c, 0x37d0, 0x0820 },
5070 { 0x1e, 0x0000, 0x0001 },
5071 { 0x19, 0x8000, 0x0000 }
5072 };
5073
5074 rtl_hw_start_8168g(tp);
5075
5076 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005077 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005078 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005079 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005080}
5081
hayeswang57538c42013-04-01 22:23:40 +00005082static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5083{
hayeswang57538c42013-04-01 22:23:40 +00005084 static const struct ephy_info e_info_8168g_2[] = {
5085 { 0x00, 0x0000, 0x0008 },
5086 { 0x0c, 0x3df0, 0x0200 },
5087 { 0x19, 0xffff, 0xfc00 },
5088 { 0x1e, 0xffff, 0x20eb }
5089 };
5090
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005091 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005092
5093 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005094 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5095 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005096 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5097}
5098
hayeswang45dd95c2013-07-08 17:09:01 +08005099static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5100{
hayeswang45dd95c2013-07-08 17:09:01 +08005101 static const struct ephy_info e_info_8411_2[] = {
5102 { 0x00, 0x0000, 0x0008 },
5103 { 0x0c, 0x3df0, 0x0200 },
5104 { 0x0f, 0xffff, 0x5200 },
5105 { 0x19, 0x0020, 0x0000 },
5106 { 0x1e, 0x0000, 0x2000 }
5107 };
5108
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005109 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005110
5111 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005112 rtl_hw_aspm_clkreq_enable(tp, false);
hayeswang45dd95c2013-07-08 17:09:01 +08005113 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005114 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08005115}
5116
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005117static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5118{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005119 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005120 u32 data;
5121 static const struct ephy_info e_info_8168h_1[] = {
5122 { 0x1e, 0x0800, 0x0001 },
5123 { 0x1d, 0x0000, 0x0800 },
5124 { 0x05, 0xffff, 0x2089 },
5125 { 0x06, 0xffff, 0x5881 },
5126 { 0x04, 0xffff, 0x154a },
5127 { 0x01, 0xffff, 0x068b }
5128 };
5129
5130 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005131 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005132 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5133
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005134 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5135 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5136 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5137 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5138
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005139 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005140
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005141 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005142
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005143 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5144 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005145
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005146 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005147
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005148 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005149
5150 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5151
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005152 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5153 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005154
5155 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5156 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5157
5158 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005159 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005160
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005161 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5162 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005163
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005164 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005165
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005166 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005167
5168 rtl_pcie_state_l2l3_enable(tp, false);
5169
5170 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005171 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005172 rtl_writephy(tp, 0x1f, 0x0000);
5173 if (rg_saw_cnt > 0) {
5174 u16 sw_cnt_1ms_ini;
5175
5176 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5177 sw_cnt_1ms_ini &= 0x0fff;
5178 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005179 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005180 data |= sw_cnt_1ms_ini;
5181 r8168_mac_ocp_write(tp, 0xd412, data);
5182 }
5183
5184 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005185 data &= ~0xf0;
5186 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005187 r8168_mac_ocp_write(tp, 0xe056, data);
5188
5189 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005190 data &= ~0x6000;
5191 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005192 r8168_mac_ocp_write(tp, 0xe052, data);
5193
5194 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005195 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005196 data |= 0x017f;
5197 r8168_mac_ocp_write(tp, 0xe0d6, data);
5198
5199 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005200 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005201 data |= 0x047f;
5202 r8168_mac_ocp_write(tp, 0xd420, data);
5203
5204 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5205 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5206 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5207 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005208
5209 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005210}
5211
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005212static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5213{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005214 rtl8168ep_stop_cmac(tp);
5215
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005216 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5217 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5218 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5219 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5220
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005221 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005222
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005223 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005224
5225 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5226 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5227
5228 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5229
5230 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5231
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005232 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5233 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005234
5235 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5236 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5237
5238 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005239 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005240
5241 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5242
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005243 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005244
5245 rtl_pcie_state_l2l3_enable(tp, false);
5246}
5247
5248static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5249{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005250 static const struct ephy_info e_info_8168ep_1[] = {
5251 { 0x00, 0xffff, 0x10ab },
5252 { 0x06, 0xffff, 0xf030 },
5253 { 0x08, 0xffff, 0x2006 },
5254 { 0x0d, 0xffff, 0x1666 },
5255 { 0x0c, 0x3ff0, 0x0000 }
5256 };
5257
5258 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005259 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005260 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5261
5262 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005263
5264 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005265}
5266
5267static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5268{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005269 static const struct ephy_info e_info_8168ep_2[] = {
5270 { 0x00, 0xffff, 0x10a3 },
5271 { 0x19, 0xffff, 0xfc00 },
5272 { 0x1e, 0xffff, 0x20ea }
5273 };
5274
5275 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005276 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005277 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5278
5279 rtl_hw_start_8168ep(tp);
5280
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005281 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5282 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005283
5284 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005285}
5286
5287static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5288{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005289 u32 data;
5290 static const struct ephy_info e_info_8168ep_3[] = {
5291 { 0x00, 0xffff, 0x10a3 },
5292 { 0x19, 0xffff, 0x7c00 },
5293 { 0x1e, 0xffff, 0x20eb },
5294 { 0x0d, 0xffff, 0x1666 }
5295 };
5296
5297 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005298 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005299 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5300
5301 rtl_hw_start_8168ep(tp);
5302
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005303 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5304 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005305
5306 data = r8168_mac_ocp_read(tp, 0xd3e2);
5307 data &= 0xf000;
5308 data |= 0x0271;
5309 r8168_mac_ocp_write(tp, 0xd3e2, data);
5310
5311 data = r8168_mac_ocp_read(tp, 0xd3e4);
5312 data &= 0xff00;
5313 r8168_mac_ocp_write(tp, 0xd3e4, data);
5314
5315 data = r8168_mac_ocp_read(tp, 0xe860);
5316 data |= 0x0080;
5317 r8168_mac_ocp_write(tp, 0xe860, data);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005318
5319 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005320}
5321
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005322static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005323{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005324 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005325
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005326 tp->cp_cmd &= ~INTT_MASK;
5327 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005328 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02005329
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005330 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01005331
5332 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005333 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Heiner Kallweit559c3c02018-11-19 22:34:17 +01005334 tp->irq_mask |= RxFIFOOver;
5335 tp->irq_mask &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005336 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005337
Francois Romieu219a1e92008-06-28 11:58:39 +02005338 switch (tp->mac_version) {
5339 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005340 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005341 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005342
5343 case RTL_GIGA_MAC_VER_12:
5344 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005345 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005346 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005347
5348 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005349 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005350 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005351
5352 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005353 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005354 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005355
5356 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005357 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005358 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005359
Francois Romieu197ff762008-06-28 13:16:02 +02005360 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005361 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005362 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005363
Francois Romieu6fb07052008-06-29 11:54:28 +02005364 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005365 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005366 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005367
Francois Romieuef3386f2008-06-29 12:24:30 +02005368 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005369 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005370 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005371
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005372 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005373 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005374 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005375
Francois Romieu5b538df2008-07-20 16:22:45 +02005376 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005377 case RTL_GIGA_MAC_VER_26:
5378 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005379 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005380 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005381
françois romieue6de30d2011-01-03 15:08:37 +00005382 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005383 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005384 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005385
hayeswang4804b3b2011-03-21 01:50:29 +00005386 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005387 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005388 break;
5389
hayeswang01dc7fe2011-03-21 01:50:28 +00005390 case RTL_GIGA_MAC_VER_32:
5391 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005392 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005393 break;
5394 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005395 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005396 break;
françois romieue6de30d2011-01-03 15:08:37 +00005397
Hayes Wangc2218922011-09-06 16:55:18 +08005398 case RTL_GIGA_MAC_VER_35:
5399 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005400 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005401 break;
5402
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005403 case RTL_GIGA_MAC_VER_38:
5404 rtl_hw_start_8411(tp);
5405 break;
5406
Hayes Wangc5583862012-07-02 17:23:22 +08005407 case RTL_GIGA_MAC_VER_40:
5408 case RTL_GIGA_MAC_VER_41:
5409 rtl_hw_start_8168g_1(tp);
5410 break;
hayeswang57538c42013-04-01 22:23:40 +00005411 case RTL_GIGA_MAC_VER_42:
5412 rtl_hw_start_8168g_2(tp);
5413 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005414
hayeswang45dd95c2013-07-08 17:09:01 +08005415 case RTL_GIGA_MAC_VER_44:
5416 rtl_hw_start_8411_2(tp);
5417 break;
5418
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005419 case RTL_GIGA_MAC_VER_45:
5420 case RTL_GIGA_MAC_VER_46:
5421 rtl_hw_start_8168h_1(tp);
5422 break;
5423
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005424 case RTL_GIGA_MAC_VER_49:
5425 rtl_hw_start_8168ep_1(tp);
5426 break;
5427
5428 case RTL_GIGA_MAC_VER_50:
5429 rtl_hw_start_8168ep_2(tp);
5430 break;
5431
5432 case RTL_GIGA_MAC_VER_51:
5433 rtl_hw_start_8168ep_3(tp);
5434 break;
5435
Francois Romieu219a1e92008-06-28 11:58:39 +02005436 default:
Heiner Kallweit49d17512018-06-28 20:36:15 +02005437 netif_err(tp, drv, tp->dev,
5438 "unknown chipset (mac_version = %d)\n",
5439 tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005440 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005441 }
Francois Romieu07ce4062007-02-23 23:36:39 +01005442}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005444static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005445{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005446 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005447 { 0x01, 0, 0x6e65 },
5448 { 0x02, 0, 0x091f },
5449 { 0x03, 0, 0xc2f9 },
5450 { 0x06, 0, 0xafb5 },
5451 { 0x07, 0, 0x0e00 },
5452 { 0x19, 0, 0xec80 },
5453 { 0x01, 0, 0x2e65 },
5454 { 0x01, 0, 0x6e65 }
5455 };
5456 u8 cfg1;
5457
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005458 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005459
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005460 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005461
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005462 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005463
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005464 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02005465 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005466 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005467
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005468 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005469 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005470 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005471
Francois Romieufdf6fc02012-07-06 22:40:38 +02005472 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005473}
5474
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005475static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005476{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005477 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005478
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005479 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005480
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005481 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5482 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005483}
5484
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005485static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005486{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005487 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005488
Francois Romieufdf6fc02012-07-06 22:40:38 +02005489 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005490}
5491
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005492static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005493{
5494 static const struct ephy_info e_info_8105e_1[] = {
5495 { 0x07, 0, 0x4000 },
5496 { 0x19, 0, 0x0200 },
5497 { 0x19, 0, 0x0020 },
5498 { 0x1e, 0, 0x2000 },
5499 { 0x03, 0, 0x0001 },
5500 { 0x19, 0, 0x0100 },
5501 { 0x19, 0, 0x0004 },
5502 { 0x0a, 0, 0x0020 }
5503 };
5504
Francois Romieucecb5fd2011-04-01 10:21:07 +02005505 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005506 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005507
Francois Romieucecb5fd2011-04-01 10:21:07 +02005508 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005509 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005510
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005511 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5512 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005513
Francois Romieufdf6fc02012-07-06 22:40:38 +02005514 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08005515
5516 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005517}
5518
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005519static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005520{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005521 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005522 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005523}
5524
Hayes Wang7e18dca2012-03-30 14:33:02 +08005525static void rtl_hw_start_8402(struct rtl8169_private *tp)
5526{
Hayes Wang7e18dca2012-03-30 14:33:02 +08005527 static const struct ephy_info e_info_8402[] = {
5528 { 0x19, 0xffff, 0xff64 },
5529 { 0x1e, 0, 0x4000 }
5530 };
5531
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005532 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005533
5534 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005535 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005536
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005537 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005538
Francois Romieufdf6fc02012-07-06 22:40:38 +02005539 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005540
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005541 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005542
Francois Romieufdf6fc02012-07-06 22:40:38 +02005543 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5544 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005545 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5546 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005547 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5548 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005549 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005550
5551 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005552}
5553
Hayes Wang5598bfe2012-07-02 17:23:21 +08005554static void rtl_hw_start_8106(struct rtl8169_private *tp)
5555{
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005556 rtl_hw_aspm_clkreq_enable(tp, false);
5557
Hayes Wang5598bfe2012-07-02 17:23:21 +08005558 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005559 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005560
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005561 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5562 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5563 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005564
5565 rtl_pcie_state_l2l3_enable(tp, false);
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005566 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005567}
5568
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005569static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005570{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005571 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
Heiner Kallweit559c3c02018-11-19 22:34:17 +01005572 tp->irq_mask &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005573
Francois Romieucecb5fd2011-04-01 10:21:07 +02005574 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005575 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005576 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005577 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005578
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005579 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005580
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005581 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005582 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00005583
Francois Romieu2857ffb2008-08-02 21:08:49 +02005584 switch (tp->mac_version) {
5585 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005586 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005587 break;
5588
5589 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005590 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005591 break;
5592
5593 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005594 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005595 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005596
5597 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005598 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005599 break;
5600 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005601 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005602 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005603
5604 case RTL_GIGA_MAC_VER_37:
5605 rtl_hw_start_8402(tp);
5606 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005607
5608 case RTL_GIGA_MAC_VER_39:
5609 rtl_hw_start_8106(tp);
5610 break;
hayeswang58152cd2013-04-01 22:23:42 +00005611 case RTL_GIGA_MAC_VER_43:
5612 rtl_hw_start_8168g_2(tp);
5613 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005614 case RTL_GIGA_MAC_VER_47:
5615 case RTL_GIGA_MAC_VER_48:
5616 rtl_hw_start_8168h_1(tp);
5617 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005618 }
5619
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005620 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621}
5622
5623static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5624{
Francois Romieud58d46b2011-05-03 16:38:29 +02005625 struct rtl8169_private *tp = netdev_priv(dev);
5626
Francois Romieud58d46b2011-05-03 16:38:29 +02005627 if (new_mtu > ETH_DATA_LEN)
5628 rtl_hw_jumbo_enable(tp);
5629 else
5630 rtl_hw_jumbo_disable(tp);
5631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005633 netdev_update_features(dev);
5634
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636}
5637
5638static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5639{
Al Viro95e09182007-12-22 18:55:39 +00005640 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5642}
5643
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005644static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5645 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005647 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5648 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005649
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005650 kfree(*data_buff);
5651 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 rtl8169_make_unusable_by_asic(desc);
5653}
5654
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005655static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656{
5657 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5658
Alexander Duycka0750132014-12-11 15:02:17 -08005659 /* Force memory writes to complete before releasing descriptor */
5660 dma_wmb();
5661
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005662 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663}
5664
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005665static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005667 return (void *)ALIGN((long)data, 16);
5668}
5669
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005670static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5671 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005672{
5673 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005675 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02005676 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005678 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005679 if (!data)
5680 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005681
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005682 if (rtl8169_align(data) != data) {
5683 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005684 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005685 if (!data)
5686 return NULL;
5687 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005688
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005689 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005690 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005691 if (unlikely(dma_mapping_error(d, mapping))) {
5692 if (net_ratelimit())
5693 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005694 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
Heiner Kallweitd731af72018-04-17 23:26:41 +02005697 desc->addr = cpu_to_le64(mapping);
5698 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005699 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005700
5701err_out:
5702 kfree(data);
5703 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704}
5705
5706static void rtl8169_rx_clear(struct rtl8169_private *tp)
5707{
Francois Romieu07d3f512007-02-21 22:40:46 +01005708 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
5710 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005711 if (tp->Rx_databuff[i]) {
5712 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 tp->RxDescArray + i);
5714 }
5715 }
5716}
5717
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005718static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005720 desc->opts1 |= cpu_to_le32(RingEnd);
5721}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005722
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005723static int rtl8169_rx_fill(struct rtl8169_private *tp)
5724{
5725 unsigned int i;
5726
5727 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005728 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005729
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005730 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005731 if (!data) {
5732 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005733 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005734 }
5735 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005738 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5739 return 0;
5740
5741err_out:
5742 rtl8169_rx_clear(tp);
5743 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744}
5745
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005746static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 rtl8169_init_ring_indexes(tp);
5749
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005750 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5751 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005753 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754}
5755
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005756static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 struct TxDesc *desc)
5758{
5759 unsigned int len = tx_skb->len;
5760
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005761 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5762
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 desc->opts1 = 0x00;
5764 desc->opts2 = 0x00;
5765 desc->addr = 0x00;
5766 tx_skb->len = 0;
5767}
5768
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005769static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5770 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771{
5772 unsigned int i;
5773
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005774 for (i = 0; i < n; i++) {
5775 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 struct ring_info *tx_skb = tp->tx_skb + entry;
5777 unsigned int len = tx_skb->len;
5778
5779 if (len) {
5780 struct sk_buff *skb = tx_skb->skb;
5781
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005782 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 tp->TxDescArray + entry);
5784 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07005785 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 tx_skb->skb = NULL;
5787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 }
5789 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005790}
5791
5792static void rtl8169_tx_clear(struct rtl8169_private *tp)
5793{
5794 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 tp->cur_tx = tp->dirty_tx = 0;
Florian Westphald92060b2018-10-20 12:25:27 +02005796 netdev_reset_queue(tp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797}
5798
Francois Romieu4422bcd2012-01-26 11:23:32 +01005799static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800{
David Howellsc4028952006-11-22 14:57:56 +00005801 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005802 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
Francois Romieuda78dbf2012-01-26 14:18:23 +01005804 napi_disable(&tp->napi);
5805 netif_stop_queue(dev);
Paul E. McKenney16f11502018-11-05 17:07:39 -08005806 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
françois romieuc7c2c392011-12-04 20:30:52 +00005808 rtl8169_hw_reset(tp);
5809
Francois Romieu56de4142011-03-15 17:29:31 +01005810 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005811 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01005812
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005814 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
Francois Romieuda78dbf2012-01-26 14:18:23 +01005816 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005817 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005818 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819}
5820
5821static void rtl8169_tx_timeout(struct net_device *dev)
5822{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005823 struct rtl8169_private *tp = netdev_priv(dev);
5824
5825 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826}
5827
Heiner Kallweit734c1402018-11-22 21:56:48 +01005828static __le32 rtl8169_get_txd_opts1(u32 opts0, u32 len, unsigned int entry)
5829{
5830 u32 status = opts0 | len;
5831
5832 if (entry == NUM_TX_DESC - 1)
5833 status |= RingEnd;
5834
5835 return cpu_to_le32(status);
5836}
5837
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005839 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840{
5841 struct skb_shared_info *info = skb_shinfo(skb);
5842 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005843 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005844 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
5846 entry = tp->cur_tx;
5847 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005848 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01005850 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 void *addr;
5852
5853 entry = (entry + 1) % NUM_TX_DESC;
5854
5855 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005856 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005857 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005858 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005859 if (unlikely(dma_mapping_error(d, mapping))) {
5860 if (net_ratelimit())
5861 netif_err(tp, drv, tp->dev,
5862 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005863 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
Heiner Kallweit734c1402018-11-22 21:56:48 +01005866 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005867 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 txd->addr = cpu_to_le64(mapping);
5869
5870 tp->tx_skb[entry].len = len;
5871 }
5872
5873 if (cur_frag) {
5874 tp->tx_skb[entry].skb = skb;
5875 txd->opts1 |= cpu_to_le32(LastFrag);
5876 }
5877
5878 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005879
5880err_out:
5881 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5882 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883}
5884
françois romieub423e9a2013-05-18 01:24:46 +00005885static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5886{
5887 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5888}
5889
hayeswange9746042014-07-11 16:25:58 +08005890static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5891 struct net_device *dev);
5892/* r8169_csum_workaround()
5893 * The hw limites the value the transport offset. When the offset is out of the
5894 * range, calculate the checksum by sw.
5895 */
5896static void r8169_csum_workaround(struct rtl8169_private *tp,
5897 struct sk_buff *skb)
5898{
5899 if (skb_shinfo(skb)->gso_size) {
5900 netdev_features_t features = tp->dev->features;
5901 struct sk_buff *segs, *nskb;
5902
5903 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
5904 segs = skb_gso_segment(skb, features);
5905 if (IS_ERR(segs) || !segs)
5906 goto drop;
5907
5908 do {
5909 nskb = segs;
5910 segs = segs->next;
5911 nskb->next = NULL;
5912 rtl8169_start_xmit(nskb, tp->dev);
5913 } while (segs);
5914
Alexander Duyckeb781392015-05-01 10:34:44 -07005915 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08005916 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5917 if (skb_checksum_help(skb) < 0)
5918 goto drop;
5919
5920 rtl8169_start_xmit(skb, tp->dev);
5921 } else {
5922 struct net_device_stats *stats;
5923
5924drop:
5925 stats = &tp->dev->stats;
5926 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07005927 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08005928 }
5929}
5930
5931/* msdn_giant_send_check()
5932 * According to the document of microsoft, the TCP Pseudo Header excludes the
5933 * packet length for IPv6 TCP large packets.
5934 */
5935static int msdn_giant_send_check(struct sk_buff *skb)
5936{
5937 const struct ipv6hdr *ipv6h;
5938 struct tcphdr *th;
5939 int ret;
5940
5941 ret = skb_cow_head(skb, 0);
5942 if (ret)
5943 return ret;
5944
5945 ipv6h = ipv6_hdr(skb);
5946 th = tcp_hdr(skb);
5947
5948 th->check = 0;
5949 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
5950
5951 return ret;
5952}
5953
hayeswang5888d3f2014-07-11 16:25:56 +08005954static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
5955 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956{
Michał Mirosław350fb322011-04-08 06:35:56 +00005957 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958
Francois Romieu2b7b4312011-04-18 22:53:24 -07005959 if (mss) {
5960 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08005961 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5962 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5963 const struct iphdr *ip = ip_hdr(skb);
5964
5965 if (ip->protocol == IPPROTO_TCP)
5966 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5967 else if (ip->protocol == IPPROTO_UDP)
5968 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5969 else
5970 WARN_ON_ONCE(1);
5971 }
5972
5973 return true;
5974}
5975
5976static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5977 struct sk_buff *skb, u32 *opts)
5978{
hayeswangbdfa4ed2014-07-11 16:25:57 +08005979 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08005980 u32 mss = skb_shinfo(skb)->gso_size;
5981
5982 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08005983 if (transport_offset > GTTCPHO_MAX) {
5984 netif_warn(tp, tx_err, tp->dev,
5985 "Invalid transport offset 0x%x for TSO\n",
5986 transport_offset);
5987 return false;
5988 }
5989
Heiner Kallweit4ff36462018-05-02 21:40:02 +02005990 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08005991 case htons(ETH_P_IP):
5992 opts[0] |= TD1_GTSENV4;
5993 break;
5994
5995 case htons(ETH_P_IPV6):
5996 if (msdn_giant_send_check(skb))
5997 return false;
5998
5999 opts[0] |= TD1_GTSENV6;
6000 break;
6001
6002 default:
6003 WARN_ON_ONCE(1);
6004 break;
6005 }
6006
hayeswangbdfa4ed2014-07-11 16:25:57 +08006007 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006008 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006009 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006010 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
françois romieub423e9a2013-05-18 01:24:46 +00006012 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006013 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006014
hayeswange9746042014-07-11 16:25:58 +08006015 if (transport_offset > TCPHO_MAX) {
6016 netif_warn(tp, tx_err, tp->dev,
6017 "Invalid transport offset 0x%x\n",
6018 transport_offset);
6019 return false;
6020 }
6021
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006022 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006023 case htons(ETH_P_IP):
6024 opts[1] |= TD1_IPv4_CS;
6025 ip_protocol = ip_hdr(skb)->protocol;
6026 break;
6027
6028 case htons(ETH_P_IPV6):
6029 opts[1] |= TD1_IPv6_CS;
6030 ip_protocol = ipv6_hdr(skb)->nexthdr;
6031 break;
6032
6033 default:
6034 ip_protocol = IPPROTO_RAW;
6035 break;
6036 }
6037
6038 if (ip_protocol == IPPROTO_TCP)
6039 opts[1] |= TD1_TCP_CS;
6040 else if (ip_protocol == IPPROTO_UDP)
6041 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006042 else
6043 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006044
6045 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006046 } else {
6047 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006048 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 }
hayeswang5888d3f2014-07-11 16:25:56 +08006050
françois romieub423e9a2013-05-18 01:24:46 +00006051 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052}
6053
Heiner Kallweit76085c92018-11-22 22:03:08 +01006054static bool rtl_tx_slots_avail(struct rtl8169_private *tp,
6055 unsigned int nr_frags)
6056{
6057 unsigned int slots_avail = tp->dirty_tx + NUM_TX_DESC - tp->cur_tx;
6058
6059 /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
6060 return slots_avail > nr_frags;
6061}
6062
Stephen Hemminger613573252009-08-31 19:50:58 +00006063static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6064 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065{
6066 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006067 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006069 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 dma_addr_t mapping;
Heiner Kallweit734c1402018-11-22 21:56:48 +01006071 u32 opts[2], len;
Heiner Kallweit2e6eedb2018-11-25 14:31:54 +01006072 bool stop_queue;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006073 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006074
Heiner Kallweit76085c92018-11-22 22:03:08 +01006075 if (unlikely(!rtl_tx_slots_avail(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006076 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006077 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 }
6079
6080 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006081 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
françois romieub423e9a2013-05-18 01:24:46 +00006083 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6084 opts[0] = DescOwn;
6085
hayeswange9746042014-07-11 16:25:58 +08006086 if (!tp->tso_csum(tp, skb, opts)) {
6087 r8169_csum_workaround(tp, skb);
6088 return NETDEV_TX_OK;
6089 }
françois romieub423e9a2013-05-18 01:24:46 +00006090
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006091 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006092 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006093 if (unlikely(dma_mapping_error(d, mapping))) {
6094 if (net_ratelimit())
6095 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006096 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
6099 tp->tx_skb[entry].len = len;
6100 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
Francois Romieu2b7b4312011-04-18 22:53:24 -07006102 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006103 if (frags < 0)
6104 goto err_dma_1;
6105 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006106 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006107 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006108 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006109 tp->tx_skb[entry].skb = skb;
6110 }
6111
Francois Romieu2b7b4312011-04-18 22:53:24 -07006112 txd->opts2 = cpu_to_le32(opts[1]);
6113
Richard Cochran5047fb52012-03-10 07:29:42 +00006114 skb_tx_timestamp(skb);
6115
Alexander Duycka0750132014-12-11 15:02:17 -08006116 /* Force memory writes to complete before releasing descriptor */
6117 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118
Heiner Kallweit734c1402018-11-22 21:56:48 +01006119 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
Alexander Duycka0750132014-12-11 15:02:17 -08006121 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006122 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
Alexander Duycka0750132014-12-11 15:02:17 -08006124 tp->cur_tx += frags + 1;
6125
Heiner Kallweit2e6eedb2018-11-25 14:31:54 +01006126 stop_queue = !rtl_tx_slots_avail(tp, MAX_SKB_FRAGS);
6127 if (unlikely(stop_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 netif_stop_queue(dev);
Heiner Kallweit2e6eedb2018-11-25 14:31:54 +01006129
Heiner Kallweitbd7153b2018-11-26 20:24:16 +01006130 if (__netdev_sent_queue(dev, skb->len, skb->xmit_more))
Heiner Kallweit2e6eedb2018-11-25 14:31:54 +01006131 RTL_W8(tp, TxPoll, NPQ);
Heiner Kallweit2e6eedb2018-11-25 14:31:54 +01006132
6133 if (unlikely(stop_queue)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006134 /* Sync with rtl_tx:
6135 * - publish queue status and cur_tx ring index (write barrier)
6136 * - refresh dirty_tx ring index (read barrier).
6137 * May the current thread have a pessimistic view of the ring
6138 * status and forget to wake up queue, a racing rtl_tx thread
6139 * can't.
6140 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006141 smp_mb();
Heiner Kallweit76085c92018-11-22 22:03:08 +01006142 if (rtl_tx_slots_avail(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 netif_wake_queue(dev);
6144 }
6145
Stephen Hemminger613573252009-08-31 19:50:58 +00006146 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006148err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006149 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006150err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006151 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006152 dev->stats.tx_dropped++;
6153 return NETDEV_TX_OK;
6154
6155err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006157 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006158 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159}
6160
6161static void rtl8169_pcierr_interrupt(struct net_device *dev)
6162{
6163 struct rtl8169_private *tp = netdev_priv(dev);
6164 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 u16 pci_status, pci_cmd;
6166
6167 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6168 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6169
Joe Perchesbf82c182010-02-09 11:49:50 +00006170 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6171 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
6173 /*
6174 * The recovery sequence below admits a very elaborated explanation:
6175 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006176 * - I did not see what else could be done;
6177 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 *
6179 * Feel free to adjust to your needs.
6180 */
Francois Romieua27993f2006-12-18 00:04:19 +01006181 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006182 pci_cmd &= ~PCI_COMMAND_PARITY;
6183 else
6184 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6185
6186 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187
6188 pci_write_config_word(pdev, PCI_STATUS,
6189 pci_status & (PCI_STATUS_DETECTED_PARITY |
6190 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6191 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6192
6193 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006194 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006195 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006197 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 }
6200
françois romieue6de30d2011-01-03 15:08:37 +00006201 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006202
Francois Romieu98ddf982012-01-31 10:47:34 +01006203 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204}
6205
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01006206static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
6207 int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208{
Florian Westphald92060b2018-10-20 12:25:27 +02006209 unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 dirty_tx = tp->dirty_tx;
6212 smp_rmb();
6213 tx_left = tp->cur_tx - dirty_tx;
6214
6215 while (tx_left > 0) {
6216 unsigned int entry = dirty_tx % NUM_TX_DESC;
6217 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 u32 status;
6219
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6221 if (status & DescOwn)
6222 break;
6223
Alexander Duycka0750132014-12-11 15:02:17 -08006224 /* This barrier is needed to keep us from reading
6225 * any other fields out of the Tx descriptor until
6226 * we know the status of DescOwn
6227 */
6228 dma_rmb();
6229
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006230 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006231 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 if (status & LastFrag) {
Florian Westphald92060b2018-10-20 12:25:27 +02006233 pkts_compl++;
6234 bytes_compl += tx_skb->skb->len;
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01006235 napi_consume_skb(tx_skb->skb, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 tx_skb->skb = NULL;
6237 }
6238 dirty_tx++;
6239 tx_left--;
6240 }
6241
6242 if (tp->dirty_tx != dirty_tx) {
Florian Westphald92060b2018-10-20 12:25:27 +02006243 netdev_completed_queue(dev, pkts_compl, bytes_compl);
6244
6245 u64_stats_update_begin(&tp->tx_stats.syncp);
6246 tp->tx_stats.packets += pkts_compl;
6247 tp->tx_stats.bytes += bytes_compl;
6248 u64_stats_update_end(&tp->tx_stats.syncp);
6249
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006251 /* Sync with rtl8169_start_xmit:
6252 * - publish dirty_tx ring index (write barrier)
6253 * - refresh cur_tx ring index and queue status (read barrier)
6254 * May the current thread miss the stopped queue condition,
6255 * a racing xmit thread can only have a right view of the
6256 * ring status.
6257 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006258 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 if (netif_queue_stopped(dev) &&
Heiner Kallweit76085c92018-11-22 22:03:08 +01006260 rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 netif_wake_queue(dev);
6262 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006263 /*
6264 * 8168 hack: TxPoll requests are lost when the Tx packets are
6265 * too close. Let's kick an extra TxPoll request when a burst
6266 * of start_xmit activity is detected (if it is not detected,
6267 * it is slow enough). -- FR
6268 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006269 if (tp->cur_tx != dirty_tx)
6270 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 }
6272}
6273
Francois Romieu126fa4b2005-05-12 20:09:17 -04006274static inline int rtl8169_fragmented_frame(u32 status)
6275{
6276 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6277}
6278
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006279static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 u32 status = opts1 & RxProtoMask;
6282
6283 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006284 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 skb->ip_summed = CHECKSUM_UNNECESSARY;
6286 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006287 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288}
6289
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006290static struct sk_buff *rtl8169_try_rx_copy(void *data,
6291 struct rtl8169_private *tp,
6292 int pkt_size,
6293 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006295 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006296 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006298 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006299 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006300 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006301 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006302 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006303 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006304 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6305
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006306 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307}
6308
Francois Romieuda78dbf2012-01-26 14:18:23 +01006309static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310{
6311 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006312 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
Timo Teräs9fba0812013-01-15 21:01:24 +00006316 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006318 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 u32 status;
6320
Heiner Kallweit62028062018-04-17 23:30:29 +02006321 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 if (status & DescOwn)
6323 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006324
6325 /* This barrier is needed to keep us from reading
6326 * any other fields out of the Rx descriptor until
6327 * we know the status of DescOwn
6328 */
6329 dma_rmb();
6330
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006331 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006332 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6333 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006334 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006336 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006338 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006339 /* RxFOVF is a reserved bit on later chip versions */
6340 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6341 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006342 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006343 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006344 } else if (status & (RxRUNT | RxCRC) &&
6345 !(status & RxRWT) &&
6346 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006347 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006350 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006351 dma_addr_t addr;
6352 int pkt_size;
6353
6354process_pkt:
6355 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006356 if (likely(!(dev->features & NETIF_F_RXFCS)))
6357 pkt_size = (status & 0x00003fff) - 4;
6358 else
6359 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
Francois Romieu126fa4b2005-05-12 20:09:17 -04006361 /*
6362 * The driver does not support incoming fragmented
6363 * frames. They are seen as a symptom of over-mtu
6364 * sized frames.
6365 */
6366 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006367 dev->stats.rx_dropped++;
6368 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006369 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006370 }
6371
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006372 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6373 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006374 if (!skb) {
6375 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006376 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 }
6378
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006379 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 skb_put(skb, pkt_size);
6381 skb->protocol = eth_type_trans(skb, dev);
6382
Francois Romieu7a8fc772011-03-01 17:18:33 +01006383 rtl8169_rx_vlan_tag(desc, skb);
6384
françois romieu39174292015-11-11 23:35:18 +01006385 if (skb->pkt_type == PACKET_MULTICAST)
6386 dev->stats.multicast++;
6387
Francois Romieu56de4142011-03-15 17:29:31 +01006388 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389
Junchang Wang8027aa22012-03-04 23:30:32 +01006390 u64_stats_update_begin(&tp->rx_stats.syncp);
6391 tp->rx_stats.packets++;
6392 tp->rx_stats.bytes += pkt_size;
6393 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 }
françois romieuce11ff52013-01-24 13:30:06 +00006395release_descriptor:
6396 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006397 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 }
6399
6400 count = cur_rx - tp->cur_rx;
6401 tp->cur_rx = cur_rx;
6402
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 return count;
6404}
6405
Francois Romieu07d3f512007-02-21 22:40:46 +01006406static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006408 struct rtl8169_private *tp = dev_instance;
Heiner Kallweit05bbe552018-08-10 22:38:29 +02006409 u16 status = rtl_get_events(tp);
Heiner Kallweite7824102018-12-15 16:25:05 +01006410 u16 irq_mask = RTL_R16(tp, IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411
Heiner Kallweite7824102018-12-15 16:25:05 +01006412 if (status == 0xffff || !(status & irq_mask))
Heiner Kallweit05bbe552018-08-10 22:38:29 +02006413 return IRQ_NONE;
françois romieu811fd302011-12-04 20:30:45 +00006414
Heiner Kallweit38caff52018-10-18 22:19:28 +02006415 if (unlikely(status & SYSErr)) {
6416 rtl8169_pcierr_interrupt(tp->dev);
6417 goto out;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006418 }
6419
Heiner Kallweitee28b302018-12-09 22:05:11 +01006420 if (status & LinkChg && tp->dev->phydev)
Heiner Kallweit38caff52018-10-18 22:19:28 +02006421 phy_mac_interrupt(tp->dev->phydev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006422
Heiner Kallweit38caff52018-10-18 22:19:28 +02006423 if (unlikely(status & RxFIFOOver &&
6424 tp->mac_version == RTL_GIGA_MAC_VER_11)) {
6425 netif_stop_queue(tp->dev);
6426 /* XXX - Hack alert. See rtl_task(). */
6427 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6428 }
6429
6430 if (status & RTL_EVENT_NAPI) {
6431 rtl_irq_disable(tp);
6432 napi_schedule_irqoff(&tp->napi);
6433 }
6434out:
6435 rtl_ack_events(tp, status);
6436
6437 return IRQ_HANDLED;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006438}
6439
Francois Romieu4422bcd2012-01-26 11:23:32 +01006440static void rtl_task(struct work_struct *work)
6441{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006442 static const struct {
6443 int bitnr;
6444 void (*action)(struct rtl8169_private *);
6445 } rtl_work[] = {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006446 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01006447 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006448 struct rtl8169_private *tp =
6449 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006450 struct net_device *dev = tp->dev;
6451 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006452
Francois Romieuda78dbf2012-01-26 14:18:23 +01006453 rtl_lock_work(tp);
6454
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006455 if (!netif_running(dev) ||
6456 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006457 goto out_unlock;
6458
6459 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6460 bool pending;
6461
Francois Romieuda78dbf2012-01-26 14:18:23 +01006462 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006463 if (pending)
6464 rtl_work[i].action(tp);
6465 }
6466
6467out_unlock:
6468 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006469}
6470
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006471static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006473 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6474 struct net_device *dev = tp->dev;
Heiner Kallweit6b839b62018-10-18 19:56:01 +02006475 int work_done;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006476
Heiner Kallweit6b839b62018-10-18 19:56:01 +02006477 work_done = rtl_rx(dev, tp, (u32) budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006478
Heiner Kallweit5317d5c2018-11-22 22:02:00 +01006479 rtl_tx(dev, tp, budget);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006480
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006481 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08006482 napi_complete_done(napi, work_done);
Heiner Kallweitfe716f82018-11-19 22:31:32 +01006483 rtl_irq_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 }
6485
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006486 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006489static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02006490{
6491 struct rtl8169_private *tp = netdev_priv(dev);
6492
6493 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6494 return;
6495
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006496 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6497 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02006498}
6499
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006500static void r8169_phylink_handler(struct net_device *ndev)
6501{
6502 struct rtl8169_private *tp = netdev_priv(ndev);
6503
6504 if (netif_carrier_ok(ndev)) {
6505 rtl_link_chg_patch(tp);
6506 pm_request_resume(&tp->pci_dev->dev);
6507 } else {
6508 pm_runtime_idle(&tp->pci_dev->dev);
6509 }
6510
6511 if (net_ratelimit())
6512 phy_print_status(ndev->phydev);
6513}
6514
6515static int r8169_phy_connect(struct rtl8169_private *tp)
6516{
6517 struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
6518 phy_interface_t phy_mode;
6519 int ret;
6520
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02006521 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006522 PHY_INTERFACE_MODE_MII;
6523
6524 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6525 phy_mode);
6526 if (ret)
6527 return ret;
6528
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02006529 if (!tp->supports_gmii)
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006530 phy_set_max_speed(phydev, SPEED_100);
6531
6532 /* Ensure to advertise everything, incl. pause */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +01006533 linkmode_copy(phydev->advertising, phydev->supported);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006534
6535 phy_attached_info(phydev);
6536
6537 return 0;
6538}
6539
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540static void rtl8169_down(struct net_device *dev)
6541{
6542 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006544 phy_stop(dev->phydev);
6545
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006546 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006547 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548
Hayes Wang92fc43b2011-07-06 15:58:03 +08006549 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006550 /*
6551 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006552 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6553 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006554 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006555 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenney16f11502018-11-05 17:07:39 -08006558 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 rtl8169_tx_clear(tp);
6561
6562 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006563
6564 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565}
6566
6567static int rtl8169_close(struct net_device *dev)
6568{
6569 struct rtl8169_private *tp = netdev_priv(dev);
6570 struct pci_dev *pdev = tp->pci_dev;
6571
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006572 pm_runtime_get_sync(&pdev->dev);
6573
Francois Romieucecb5fd2011-04-01 10:21:07 +02006574 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006575 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006576
Francois Romieuda78dbf2012-01-26 14:18:23 +01006577 rtl_lock_work(tp);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006578 /* Clear all task flags */
6579 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006580
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006582 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
Lekensteyn4ea72442013-07-22 09:53:30 +02006584 cancel_work_sync(&tp->wk.work);
6585
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006586 phy_disconnect(dev->phydev);
6587
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006588 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006590 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6591 tp->RxPhyAddr);
6592 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6593 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 tp->TxDescArray = NULL;
6595 tp->RxDescArray = NULL;
6596
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006597 pm_runtime_put_sync(&pdev->dev);
6598
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 return 0;
6600}
6601
Francois Romieudc1c00c2012-03-08 10:06:18 +01006602#ifdef CONFIG_NET_POLL_CONTROLLER
6603static void rtl8169_netpoll(struct net_device *dev)
6604{
6605 struct rtl8169_private *tp = netdev_priv(dev);
6606
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03006607 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01006608}
6609#endif
6610
Francois Romieudf43ac72012-03-08 09:48:40 +01006611static int rtl_open(struct net_device *dev)
6612{
6613 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006614 struct pci_dev *pdev = tp->pci_dev;
6615 int retval = -ENOMEM;
6616
6617 pm_runtime_get_sync(&pdev->dev);
6618
6619 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006620 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006621 * dma_alloc_coherent provides more.
6622 */
6623 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6624 &tp->TxPhyAddr, GFP_KERNEL);
6625 if (!tp->TxDescArray)
6626 goto err_pm_runtime_put;
6627
6628 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6629 &tp->RxPhyAddr, GFP_KERNEL);
6630 if (!tp->RxDescArray)
6631 goto err_free_tx_0;
6632
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006633 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006634 if (retval < 0)
6635 goto err_free_rx_1;
6636
6637 INIT_WORK(&tp->wk.work, rtl_task);
6638
6639 smp_mb();
6640
6641 rtl_request_firmware(tp);
6642
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006643 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006644 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01006645 if (retval < 0)
6646 goto err_release_fw_2;
6647
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006648 retval = r8169_phy_connect(tp);
6649 if (retval)
6650 goto err_free_irq;
6651
Francois Romieudf43ac72012-03-08 09:48:40 +01006652 rtl_lock_work(tp);
6653
6654 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6655
6656 napi_enable(&tp->napi);
6657
6658 rtl8169_init_phy(dev, tp);
6659
Francois Romieudf43ac72012-03-08 09:48:40 +01006660 rtl_pll_power_up(tp);
6661
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006662 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006663
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006664 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006665 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6666
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006667 phy_start(dev->phydev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006668 netif_start_queue(dev);
6669
6670 rtl_unlock_work(tp);
6671
Heiner Kallweita92a0842018-01-08 21:39:13 +01006672 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006673out:
6674 return retval;
6675
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006676err_free_irq:
6677 pci_free_irq(pdev, 0, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006678err_release_fw_2:
6679 rtl_release_firmware(tp);
6680 rtl8169_rx_clear(tp);
6681err_free_rx_1:
6682 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6683 tp->RxPhyAddr);
6684 tp->RxDescArray = NULL;
6685err_free_tx_0:
6686 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6687 tp->TxPhyAddr);
6688 tp->TxDescArray = NULL;
6689err_pm_runtime_put:
6690 pm_runtime_put_noidle(&pdev->dev);
6691 goto out;
6692}
6693
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006694static void
Junchang Wang8027aa22012-03-04 23:30:32 +01006695rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696{
6697 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006698 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02006699 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01006700 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006702 pm_runtime_get_noresume(&pdev->dev);
6703
6704 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006705 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006706
Junchang Wang8027aa22012-03-04 23:30:32 +01006707 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006708 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006709 stats->rx_packets = tp->rx_stats.packets;
6710 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006711 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006712
Junchang Wang8027aa22012-03-04 23:30:32 +01006713 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006714 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006715 stats->tx_packets = tp->tx_stats.packets;
6716 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006717 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006718
6719 stats->rx_dropped = dev->stats.rx_dropped;
6720 stats->tx_dropped = dev->stats.tx_dropped;
6721 stats->rx_length_errors = dev->stats.rx_length_errors;
6722 stats->rx_errors = dev->stats.rx_errors;
6723 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6724 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6725 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02006726 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01006727
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006728 /*
6729 * Fetch additonal counter values missing in stats collected by driver
6730 * from tally counters.
6731 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006732 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006733 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006734
6735 /*
6736 * Subtract values fetched during initalization.
6737 * See rtl8169_init_counter_offsets for a description why we do that.
6738 */
Corinna Vinschen42020322015-09-10 10:47:35 +02006739 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006740 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02006741 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006742 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02006743 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006744 le16_to_cpu(tp->tc_offset.tx_aborted);
6745
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006746 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747}
6748
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006749static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006750{
françois romieu065c27c2011-01-03 15:08:12 +00006751 struct rtl8169_private *tp = netdev_priv(dev);
6752
Francois Romieu5d06a992006-02-23 00:47:58 +01006753 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006754 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006755
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006756 phy_stop(dev->phydev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006757 netif_device_detach(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006758
6759 rtl_lock_work(tp);
6760 napi_disable(&tp->napi);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006761 /* Clear all task flags */
6762 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6763
Francois Romieuda78dbf2012-01-26 14:18:23 +01006764 rtl_unlock_work(tp);
6765
6766 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006767}
Francois Romieu5d06a992006-02-23 00:47:58 +01006768
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006769#ifdef CONFIG_PM
6770
6771static int rtl8169_suspend(struct device *device)
6772{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006773 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006774 struct rtl8169_private *tp = netdev_priv(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006775
6776 rtl8169_net_suspend(dev);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006777 clk_disable_unprepare(tp->clk);
Francois Romieu1371fa62007-04-02 23:01:11 +02006778
Francois Romieu5d06a992006-02-23 00:47:58 +01006779 return 0;
6780}
6781
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006782static void __rtl8169_resume(struct net_device *dev)
6783{
françois romieu065c27c2011-01-03 15:08:12 +00006784 struct rtl8169_private *tp = netdev_priv(dev);
6785
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006786 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006787
6788 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02006789 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00006790
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006791 phy_start(tp->dev->phydev);
6792
Artem Savkovcff4c162012-04-03 10:29:11 +00006793 rtl_lock_work(tp);
6794 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006795 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006796 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006797
Francois Romieu98ddf982012-01-31 10:47:34 +01006798 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006799}
6800
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006801static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006802{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006803 struct net_device *dev = dev_get_drvdata(device);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006804 struct rtl8169_private *tp = netdev_priv(dev);
6805
6806 clk_prepare_enable(tp->clk);
Francois Romieu5d06a992006-02-23 00:47:58 +01006807
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006808 if (netif_running(dev))
6809 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006810
Francois Romieu5d06a992006-02-23 00:47:58 +01006811 return 0;
6812}
6813
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006814static int rtl8169_runtime_suspend(struct device *device)
6815{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006816 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006817 struct rtl8169_private *tp = netdev_priv(dev);
6818
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006819 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006820 return 0;
6821
Francois Romieuda78dbf2012-01-26 14:18:23 +01006822 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006823 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006824 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006825
6826 rtl8169_net_suspend(dev);
6827
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006828 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006829 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006830 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006831
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006832 return 0;
6833}
6834
6835static int rtl8169_runtime_resume(struct device *device)
6836{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006837 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006838 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08006839 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006840
6841 if (!tp->TxDescArray)
6842 return 0;
6843
Francois Romieuda78dbf2012-01-26 14:18:23 +01006844 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006845 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006846 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006847
6848 __rtl8169_resume(dev);
6849
6850 return 0;
6851}
6852
6853static int rtl8169_runtime_idle(struct device *device)
6854{
Heiner Kallweit0f07bd82018-11-19 22:32:18 +01006855 struct net_device *dev = dev_get_drvdata(device);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006856
Heiner Kallweita92a0842018-01-08 21:39:13 +01006857 if (!netif_running(dev) || !netif_carrier_ok(dev))
6858 pm_schedule_suspend(device, 10000);
6859
6860 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006861}
6862
Alexey Dobriyan47145212009-12-14 18:00:08 -08006863static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006864 .suspend = rtl8169_suspend,
6865 .resume = rtl8169_resume,
6866 .freeze = rtl8169_suspend,
6867 .thaw = rtl8169_resume,
6868 .poweroff = rtl8169_suspend,
6869 .restore = rtl8169_resume,
6870 .runtime_suspend = rtl8169_runtime_suspend,
6871 .runtime_resume = rtl8169_runtime_resume,
6872 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006873};
6874
6875#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6876
6877#else /* !CONFIG_PM */
6878
6879#define RTL8169_PM_OPS NULL
6880
6881#endif /* !CONFIG_PM */
6882
David S. Miller1805b2f2011-10-24 18:18:09 -04006883static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6884{
David S. Miller1805b2f2011-10-24 18:18:09 -04006885 /* WoL fails with 8168b when the receiver is disabled. */
6886 switch (tp->mac_version) {
6887 case RTL_GIGA_MAC_VER_11:
6888 case RTL_GIGA_MAC_VER_12:
6889 case RTL_GIGA_MAC_VER_17:
6890 pci_clear_master(tp->pci_dev);
6891
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006892 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006893 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006894 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04006895 break;
6896 default:
6897 break;
6898 }
6899}
6900
Francois Romieu1765f952008-09-13 17:21:40 +02006901static void rtl_shutdown(struct pci_dev *pdev)
6902{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006903 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006904 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006905
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006906 rtl8169_net_suspend(dev);
6907
Francois Romieucecb5fd2011-04-01 10:21:07 +02006908 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006909 rtl_rar_set(tp, dev->perm_addr);
6910
Hayes Wang92fc43b2011-07-06 15:58:03 +08006911 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006912
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006913 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02006914 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006915 rtl_wol_suspend_quirk(tp);
6916 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006917 }
6918
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006919 pci_wake_from_d3(pdev, true);
6920 pci_set_power_state(pdev, PCI_D3hot);
6921 }
6922}
Francois Romieu5d06a992006-02-23 00:47:58 +01006923
Bill Pembertonbaf63292012-12-03 09:23:28 -05006924static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01006925{
6926 struct net_device *dev = pci_get_drvdata(pdev);
6927 struct rtl8169_private *tp = netdev_priv(dev);
6928
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01006929 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01006930 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01006931
Devendra Nagaad1be8d2012-05-31 01:51:20 +00006932 netif_napi_del(&tp->napi);
6933
Francois Romieue27566e2012-03-08 09:54:01 +01006934 unregister_netdev(dev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006935 mdiobus_unregister(tp->mii_bus);
Francois Romieue27566e2012-03-08 09:54:01 +01006936
6937 rtl_release_firmware(tp);
6938
6939 if (pci_dev_run_wake(pdev))
6940 pm_runtime_get_noresume(&pdev->dev);
6941
6942 /* restore original MAC address */
6943 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01006944}
6945
Francois Romieufa9c3852012-03-08 10:01:50 +01006946static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01006947 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01006948 .ndo_stop = rtl8169_close,
6949 .ndo_get_stats64 = rtl8169_get_stats64,
6950 .ndo_start_xmit = rtl8169_start_xmit,
6951 .ndo_tx_timeout = rtl8169_tx_timeout,
6952 .ndo_validate_addr = eth_validate_addr,
6953 .ndo_change_mtu = rtl8169_change_mtu,
6954 .ndo_fix_features = rtl8169_fix_features,
6955 .ndo_set_features = rtl8169_set_features,
6956 .ndo_set_mac_address = rtl_set_mac_address,
6957 .ndo_do_ioctl = rtl8169_ioctl,
6958 .ndo_set_rx_mode = rtl_set_rx_mode,
6959#ifdef CONFIG_NET_POLL_CONTROLLER
6960 .ndo_poll_controller = rtl8169_netpoll,
6961#endif
6962
6963};
6964
Francois Romieu31fa8b12012-03-08 10:09:40 +01006965static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006966 void (*hw_start)(struct rtl8169_private *tp);
Heiner Kallweit559c3c02018-11-19 22:34:17 +01006967 u16 irq_mask;
Heiner Kallweit14967f92018-02-28 07:55:20 +01006968 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03006969 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006970} rtl_cfg_infos [] = {
6971 [RTL_CFG_0] = {
6972 .hw_start = rtl_hw_start_8169,
Heiner Kallweit559c3c02018-11-19 22:34:17 +01006973 .irq_mask = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01006974 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03006975 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01006976 },
6977 [RTL_CFG_1] = {
6978 .hw_start = rtl_hw_start_8168,
Heiner Kallweit559c3c02018-11-19 22:34:17 +01006979 .irq_mask = LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01006980 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03006981 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01006982 },
6983 [RTL_CFG_2] = {
6984 .hw_start = rtl_hw_start_8101,
Heiner Kallweit559c3c02018-11-19 22:34:17 +01006985 .irq_mask = LinkChg | RxOverflow | RxFIFOOver,
Francois Romieu50970832017-10-27 13:24:49 +03006986 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01006987 }
6988};
6989
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006990static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01006991{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006992 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01006993
Jian-Hong Pand49c88d2018-09-27 12:09:48 +08006994 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006995 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
6996 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
6997 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006998 flags = PCI_IRQ_LEGACY;
Jian-Hong Pand49c88d2018-09-27 12:09:48 +08006999 } else {
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007000 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007001 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007002
7003 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007004}
7005
Hayes Wangc5583862012-07-02 17:23:22 +08007006DECLARE_RTL_COND(rtl_link_list_ready_cond)
7007{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007008 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007009}
7010
7011DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7012{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007013 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007014}
7015
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007016static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
7017{
7018 struct rtl8169_private *tp = mii_bus->priv;
7019
7020 if (phyaddr > 0)
7021 return -ENODEV;
7022
7023 return rtl_readphy(tp, phyreg);
7024}
7025
7026static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
7027 int phyreg, u16 val)
7028{
7029 struct rtl8169_private *tp = mii_bus->priv;
7030
7031 if (phyaddr > 0)
7032 return -ENODEV;
7033
7034 rtl_writephy(tp, phyreg, val);
7035
7036 return 0;
7037}
7038
7039static int r8169_mdio_register(struct rtl8169_private *tp)
7040{
7041 struct pci_dev *pdev = tp->pci_dev;
7042 struct phy_device *phydev;
7043 struct mii_bus *new_bus;
7044 int ret;
7045
7046 new_bus = devm_mdiobus_alloc(&pdev->dev);
7047 if (!new_bus)
7048 return -ENOMEM;
7049
7050 new_bus->name = "r8169";
7051 new_bus->priv = tp;
7052 new_bus->parent = &pdev->dev;
7053 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
7054 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
7055 PCI_DEVID(pdev->bus->number, pdev->devfn));
7056
7057 new_bus->read = r8169_mdio_read_reg;
7058 new_bus->write = r8169_mdio_write_reg;
7059
7060 ret = mdiobus_register(new_bus);
7061 if (ret)
7062 return ret;
7063
7064 phydev = mdiobus_get_phy(new_bus, 0);
7065 if (!phydev) {
7066 mdiobus_unregister(new_bus);
7067 return -ENODEV;
7068 }
7069
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02007070 /* PHY will be woken up in rtl_open() */
7071 phy_suspend(phydev);
7072
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007073 tp->mii_bus = new_bus;
7074
7075 return 0;
7076}
7077
Bill Pembertonbaf63292012-12-03 09:23:28 -05007078static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007079{
Hayes Wangc5583862012-07-02 17:23:22 +08007080 u32 data;
7081
7082 tp->ocp_base = OCP_STD_PHY_BASE;
7083
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007084 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007085
7086 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7087 return;
7088
7089 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7090 return;
7091
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007092 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007093 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007094 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007095
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007096 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007097 data &= ~(1 << 14);
7098 r8168_mac_ocp_write(tp, 0xe8de, data);
7099
7100 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7101 return;
7102
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007103 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007104 data |= (1 << 15);
7105 r8168_mac_ocp_write(tp, 0xe8de, data);
7106
7107 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7108 return;
7109}
7110
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007111static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7112{
7113 rtl8168ep_stop_cmac(tp);
7114 rtl_hw_init_8168g(tp);
7115}
7116
Bill Pembertonbaf63292012-12-03 09:23:28 -05007117static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007118{
7119 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007120 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007121 rtl_hw_init_8168g(tp);
7122 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007123 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007124 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007125 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007126 default:
7127 break;
7128 }
7129}
7130
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007131/* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
7132static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
7133{
7134 switch (tp->mac_version) {
7135 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7136 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
7137 return false;
7138 default:
7139 return true;
7140 }
7141}
7142
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007143static int rtl_jumbo_max(struct rtl8169_private *tp)
7144{
7145 /* Non-GBit versions don't support jumbo frames */
7146 if (!tp->supports_gmii)
7147 return JUMBO_1K;
7148
7149 switch (tp->mac_version) {
7150 /* RTL8169 */
7151 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7152 return JUMBO_7K;
7153 /* RTL8168b */
7154 case RTL_GIGA_MAC_VER_11:
7155 case RTL_GIGA_MAC_VER_12:
7156 case RTL_GIGA_MAC_VER_17:
7157 return JUMBO_4K;
7158 /* RTL8168c */
7159 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
7160 return JUMBO_6K;
7161 default:
7162 return JUMBO_9K;
7163 }
7164}
7165
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02007166static void rtl_disable_clk(void *data)
7167{
7168 clk_disable_unprepare(data);
7169}
7170
hayeswang929a0312014-09-16 11:40:47 +08007171static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007172{
7173 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007174 struct rtl8169_private *tp;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007175 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007176 int chipset, region, i;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007177 int jumbo_max, rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007178
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007179 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7180 if (!dev)
7181 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007182
7183 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007184 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007185 tp = netdev_priv(dev);
7186 tp->dev = dev;
7187 tp->pci_dev = pdev;
7188 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02007189 tp->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007190
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02007191 /* Get the *optional* external "ether_clk" used on some boards */
7192 tp->clk = devm_clk_get(&pdev->dev, "ether_clk");
7193 if (IS_ERR(tp->clk)) {
7194 rc = PTR_ERR(tp->clk);
7195 if (rc == -ENOENT) {
7196 /* clk-core allows NULL (for suspend / resume) */
7197 tp->clk = NULL;
7198 } else if (rc == -EPROBE_DEFER) {
7199 return rc;
7200 } else {
7201 dev_err(&pdev->dev, "failed to get clk: %d\n", rc);
7202 return rc;
7203 }
7204 } else {
7205 rc = clk_prepare_enable(tp->clk);
7206 if (rc) {
7207 dev_err(&pdev->dev, "failed to enable clk: %d\n", rc);
7208 return rc;
7209 }
7210
7211 rc = devm_add_action_or_reset(&pdev->dev, rtl_disable_clk,
7212 tp->clk);
7213 if (rc)
7214 return rc;
7215 }
7216
Francois Romieu3b6cf252012-03-08 09:59:04 +01007217 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007218 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007219 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007220 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007221 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007222 }
7223
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007224 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007225 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007226
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007227 /* use first MMIO region */
7228 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7229 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007230 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007231 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007232 }
7233
7234 /* check for weird/broken PCI region reporting */
7235 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007236 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007237 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007238 }
7239
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007240 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007241 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007242 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007243 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007244 }
7245
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007246 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007247
Francois Romieu3b6cf252012-03-08 09:59:04 +01007248 /* Identify chip attached to board */
Heiner Kallweitb4cc2dc2018-11-22 21:58:48 +01007249 rtl8169_get_mac_version(tp);
7250 if (tp->mac_version == RTL_GIGA_MAC_NONE)
7251 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007252
Heiner Kallweite3972862018-06-29 08:07:04 +02007253 if (rtl_tbi_enabled(tp)) {
7254 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7255 return -ENODEV;
7256 }
7257
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007258 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007259
Heiner Kallweita0456792018-09-25 07:59:36 +02007260 if (sizeof(dma_addr_t) > 4 && (use_dac == 1 || (use_dac == -1 &&
7261 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
7262 !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007263
7264 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7265 if (!pci_is_pcie(pdev))
7266 tp->cp_cmd |= PCIDAC;
7267 dev->features |= NETIF_F_HIGHDMA;
7268 } else {
7269 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7270 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007271 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007272 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007273 }
7274 }
7275
Francois Romieu3b6cf252012-03-08 09:59:04 +01007276 rtl_init_rxcfg(tp);
7277
Heiner Kallweitde20e122018-09-25 07:58:00 +02007278 rtl8169_irq_mask_and_ack(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007279
Hayes Wangc5583862012-07-02 17:23:22 +08007280 rtl_hw_initialize(tp);
7281
Francois Romieu3b6cf252012-03-08 09:59:04 +01007282 rtl_hw_reset(tp);
7283
Francois Romieu3b6cf252012-03-08 09:59:04 +01007284 pci_set_master(pdev);
7285
Francois Romieu3b6cf252012-03-08 09:59:04 +01007286 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007287 rtl_init_jumbo_ops(tp);
7288
Francois Romieu3b6cf252012-03-08 09:59:04 +01007289 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007290
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007291 rc = rtl_alloc_irq(tp);
7292 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007293 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007294 return rc;
7295 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007296
Heiner Kallweit18041b52018-07-24 22:21:04 +02007297 tp->saved_wolopts = __rtl8169_get_wol(tp);
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01007298
Francois Romieu3b6cf252012-03-08 09:59:04 +01007299 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007300 u64_stats_init(&tp->rx_stats.syncp);
7301 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007302
7303 /* Get MAC address */
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007304 switch (tp->mac_version) {
Heiner Kallweit353af852018-05-02 21:39:59 +02007305 u8 mac_addr[ETH_ALEN] __aligned(4);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007306 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7307 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08007308 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
Heiner Kallweit353af852018-05-02 21:39:59 +02007309 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007310
Heiner Kallweit353af852018-05-02 21:39:59 +02007311 if (is_valid_ether_addr(mac_addr))
7312 rtl_rar_set(tp, mac_addr);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007313 break;
7314 default:
7315 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007316 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007317 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007318 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007319
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007320 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007321
Heiner Kallweit37621492018-04-17 23:20:03 +02007322 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007323
7324 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7325 * properly for all devices */
7326 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007327 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007328
7329 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007330 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7331 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007332 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7333 NETIF_F_HIGHDMA;
Heiner Kallweit2d0ec542018-07-02 22:49:35 +02007334 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007335
hayeswang929a0312014-09-16 11:40:47 +08007336 tp->cp_cmd |= RxChkSum | RxVlan;
7337
7338 /*
7339 * Pretend we are using VLANs; This bypasses a nasty bug where
7340 * Interrupts stop flowing on high load on 8110SCd controllers.
7341 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007342 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007343 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007344 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007345
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007346 if (rtl_chip_supports_csum_v2(tp)) {
hayeswang5888d3f2014-07-11 16:25:56 +08007347 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007348 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007349 } else {
7350 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007351 }
hayeswang5888d3f2014-07-11 16:25:56 +08007352
Francois Romieu3b6cf252012-03-08 09:59:04 +01007353 dev->hw_features |= NETIF_F_RXALL;
7354 dev->hw_features |= NETIF_F_RXFCS;
7355
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007356 /* MTU range: 60 - hw-specific max */
7357 dev->min_mtu = ETH_ZLEN;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007358 jumbo_max = rtl_jumbo_max(tp);
7359 dev->max_mtu = jumbo_max;
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007360
Francois Romieu3b6cf252012-03-08 09:59:04 +01007361 tp->hw_start = cfg->hw_start;
Heiner Kallweit559c3c02018-11-19 22:34:17 +01007362 tp->irq_mask = RTL_EVENT_NAPI | cfg->irq_mask;
Francois Romieu50970832017-10-27 13:24:49 +03007363 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007364
Francois Romieu3b6cf252012-03-08 09:59:04 +01007365 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7366
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007367 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7368 &tp->counters_phys_addr,
7369 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007370 if (!tp->counters)
7371 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02007372
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02007373 pci_set_drvdata(pdev, dev);
7374
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007375 rc = r8169_mdio_register(tp);
7376 if (rc)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007377 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007378
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02007379 /* chip gets powered up in rtl_open() */
7380 rtl_pll_power_down(tp);
7381
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007382 rc = register_netdev(dev);
7383 if (rc)
7384 goto err_mdio_unregister;
7385
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01007386 netif_info(tp, probe, dev, "%s, %pM, XID %03x, IRQ %d\n",
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02007387 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit55d2ad72018-11-19 22:41:35 +01007388 (RTL_R32(tp, TxConfig) >> 20) & 0xfcf,
Heiner Kallweit29274992018-02-28 20:43:38 +01007389 pci_irq_vector(pdev, 0));
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007390
7391 if (jumbo_max > JUMBO_1K)
7392 netif_info(tp, probe, dev,
7393 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
7394 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
7395 "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007396
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007397 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01007398 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007399
Heiner Kallweita92a0842018-01-08 21:39:13 +01007400 if (pci_dev_run_wake(pdev))
7401 pm_runtime_put_sync(&pdev->dev);
7402
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007403 return 0;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007404
7405err_mdio_unregister:
7406 mdiobus_unregister(tp->mii_bus);
7407 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007408}
7409
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410static struct pci_driver rtl8169_pci_driver = {
7411 .name = MODULENAME,
7412 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007413 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007414 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007415 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007416 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417};
7418
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007419module_pci_driver(rtl8169_pci_driver);