blob: ac306797590ecc8e8d633f65b21aaa1a53731ed0 [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>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
Heiner Kallweitf1e911d2018-07-17 22:51:26 +020018#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
Heiner Kallweit098b01a2018-08-10 22:37:31 +020022#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/ip.h>
24#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080030#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define MODULENAME "r8169"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
françois romieubca03d52011-01-03 15:07:31 +000035#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
36#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000037#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
38#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080039#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080040#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
41#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080042#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080043#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080044#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080045#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080046#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000047#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000048#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000049#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080050#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
51#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
52#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
53#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000054
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020055#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070056 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020057
Julien Ducourthial477206a2012-05-09 00:00:06 +020058#define TX_SLOTS_AVAIL(tp) \
59 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
60
61/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
62#define TX_FRAGS_READY_FOR(tp,nr_frags) \
63 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
66 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050067static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Michal Schmidtaee77e42012-09-09 13:55:26 +000069#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
71
72#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020073#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000075#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
77#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
78
79#define RTL8169_TX_TIMEOUT (6*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +020082#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
83#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
84#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
85#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
86#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
87#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +020090 RTL_GIGA_MAC_VER_01 = 0,
91 RTL_GIGA_MAC_VER_02,
92 RTL_GIGA_MAC_VER_03,
93 RTL_GIGA_MAC_VER_04,
94 RTL_GIGA_MAC_VER_05,
95 RTL_GIGA_MAC_VER_06,
96 RTL_GIGA_MAC_VER_07,
97 RTL_GIGA_MAC_VER_08,
98 RTL_GIGA_MAC_VER_09,
99 RTL_GIGA_MAC_VER_10,
100 RTL_GIGA_MAC_VER_11,
101 RTL_GIGA_MAC_VER_12,
102 RTL_GIGA_MAC_VER_13,
103 RTL_GIGA_MAC_VER_14,
104 RTL_GIGA_MAC_VER_15,
105 RTL_GIGA_MAC_VER_16,
106 RTL_GIGA_MAC_VER_17,
107 RTL_GIGA_MAC_VER_18,
108 RTL_GIGA_MAC_VER_19,
109 RTL_GIGA_MAC_VER_20,
110 RTL_GIGA_MAC_VER_21,
111 RTL_GIGA_MAC_VER_22,
112 RTL_GIGA_MAC_VER_23,
113 RTL_GIGA_MAC_VER_24,
114 RTL_GIGA_MAC_VER_25,
115 RTL_GIGA_MAC_VER_26,
116 RTL_GIGA_MAC_VER_27,
117 RTL_GIGA_MAC_VER_28,
118 RTL_GIGA_MAC_VER_29,
119 RTL_GIGA_MAC_VER_30,
120 RTL_GIGA_MAC_VER_31,
121 RTL_GIGA_MAC_VER_32,
122 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800123 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800124 RTL_GIGA_MAC_VER_35,
125 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800126 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800127 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800128 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800129 RTL_GIGA_MAC_VER_40,
130 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000131 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000132 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800133 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800134 RTL_GIGA_MAC_VER_45,
135 RTL_GIGA_MAC_VER_46,
136 RTL_GIGA_MAC_VER_47,
137 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800138 RTL_GIGA_MAC_VER_49,
139 RTL_GIGA_MAC_VER_50,
140 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200141 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
Francois Romieud58d46b2011-05-03 16:38:29 +0200144#define JUMBO_1K ETH_DATA_LEN
145#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
146#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
147#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
148#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
149
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800150static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 const char *name;
Francois Romieu85bffe62011-04-27 08:22:39 +0200152 const char *fw_name;
153} rtl_chip_infos[] = {
154 /* PCI devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200155 [RTL_GIGA_MAC_VER_01] = {"RTL8169" },
156 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
157 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
158 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
159 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
160 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
Francois Romieu85bffe62011-04-27 08:22:39 +0200161 /* PCI-E devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200162 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
163 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
164 [RTL_GIGA_MAC_VER_09] = {"RTL8102e" },
165 [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
166 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
167 [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
168 [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
169 [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
170 [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
171 [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
172 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
173 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
174 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
175 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
176 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
177 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
178 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
179 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
180 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
181 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
182 [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
183 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
184 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
185 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
186 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
187 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
188 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
189 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
190 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
191 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
192 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
193 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
194 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
195 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
196 [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
197 [RTL_GIGA_MAC_VER_42] = {"RTL8168g/8111g", FIRMWARE_8168G_3},
198 [RTL_GIGA_MAC_VER_43] = {"RTL8106e", FIRMWARE_8106E_2},
199 [RTL_GIGA_MAC_VER_44] = {"RTL8411", FIRMWARE_8411_2 },
200 [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
201 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
202 [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
203 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
204 [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
205 [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
206 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Francois Romieubcf0bf92006-07-26 23:14:13 +0200209enum cfg_version {
210 RTL_CFG_0 = 0x00,
211 RTL_CFG_1,
212 RTL_CFG_2
213};
214
Benoit Taine9baa3c32014-08-08 15:56:03 +0200215static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200216 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200217 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800218 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200219 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100220 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200221 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200222 { PCI_VENDOR_ID_DLINK, 0x4300,
223 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200224 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000225 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200226 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200227 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
228 { PCI_VENDOR_ID_LINKSYS, 0x1032,
229 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100230 { 0x0001, 0x8168,
231 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 {0,},
233};
234
235MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
236
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200237static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200238static struct {
239 u32 msg_enable;
240} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Francois Romieu07d3f512007-02-21 22:40:46 +0100242enum rtl_registers {
243 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100244 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100245 MAR0 = 8, /* Multicast filter. */
246 CounterAddrLow = 0x10,
247 CounterAddrHigh = 0x14,
248 TxDescStartAddrLow = 0x20,
249 TxDescStartAddrHigh = 0x24,
250 TxHDescStartAddrLow = 0x28,
251 TxHDescStartAddrHigh = 0x2c,
252 FLASH = 0x30,
253 ERSR = 0x36,
254 ChipCmd = 0x37,
255 TxPoll = 0x38,
256 IntrMask = 0x3c,
257 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700258
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800259 TxConfig = 0x40,
260#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
261#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
262
263 RxConfig = 0x44,
264#define RX128_INT_EN (1 << 15) /* 8111c and later */
265#define RX_MULTI_EN (1 << 14) /* 8111c only */
266#define RXCFG_FIFO_SHIFT 13
267 /* No threshold before first PCI xfer */
268#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000269#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800270#define RXCFG_DMA_SHIFT 8
271 /* Unlimited maximum PCI burst. */
272#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700273
Francois Romieu07d3f512007-02-21 22:40:46 +0100274 RxMissed = 0x4c,
275 Cfg9346 = 0x50,
276 Config0 = 0x51,
277 Config1 = 0x52,
278 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200279#define PME_SIGNAL (1 << 5) /* 8168c and later */
280
Francois Romieu07d3f512007-02-21 22:40:46 +0100281 Config3 = 0x54,
282 Config4 = 0x55,
283 Config5 = 0x56,
284 MultiIntr = 0x5c,
285 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100286 PHYstatus = 0x6c,
287 RxMaxSize = 0xda,
288 CPlusCmd = 0xe0,
289 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300290
291#define RTL_COALESCE_MASK 0x0f
292#define RTL_COALESCE_SHIFT 4
293#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
294#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
295
Francois Romieu07d3f512007-02-21 22:40:46 +0100296 RxDescAddrLow = 0xe4,
297 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000298 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
299
300#define NoEarlyTx 0x3f /* Max value : no early transmit. */
301
302 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
303
304#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800305#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000306
Francois Romieu07d3f512007-02-21 22:40:46 +0100307 FuncEvent = 0xf0,
308 FuncEventMask = 0xf4,
309 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800310 IBCR0 = 0xf8,
311 IBCR2 = 0xf9,
312 IBIMR0 = 0xfa,
313 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100314 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
Francois Romieuf162a5d2008-06-01 22:37:49 +0200317enum rtl8168_8101_registers {
318 CSIDR = 0x64,
319 CSIAR = 0x68,
320#define CSIAR_FLAG 0x80000000
321#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200322#define CSIAR_BYTE_ENABLE 0x0000f000
323#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000324 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200325 EPHYAR = 0x80,
326#define EPHYAR_FLAG 0x80000000
327#define EPHYAR_WRITE_CMD 0x80000000
328#define EPHYAR_REG_MASK 0x1f
329#define EPHYAR_REG_SHIFT 16
330#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800331 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800332#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800333#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200334 DBG_REG = 0xd1,
335#define FIX_NAK_1 (1 << 4)
336#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800337 TWSI = 0xd2,
338 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800339#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800340#define TX_EMPTY (1 << 5)
341#define RX_EMPTY (1 << 4)
342#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800343#define EN_NDP (1 << 3)
344#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800345#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000346 EFUSEAR = 0xdc,
347#define EFUSEAR_FLAG 0x80000000
348#define EFUSEAR_WRITE_CMD 0x80000000
349#define EFUSEAR_READ_CMD 0x00000000
350#define EFUSEAR_REG_MASK 0x03ff
351#define EFUSEAR_REG_SHIFT 8
352#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800353 MISC_1 = 0xf2,
354#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200355};
356
françois romieuc0e45c12011-01-03 15:08:04 +0000357enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800358 LED_FREQ = 0x1a,
359 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000360 ERIDR = 0x70,
361 ERIAR = 0x74,
362#define ERIAR_FLAG 0x80000000
363#define ERIAR_WRITE_CMD 0x80000000
364#define ERIAR_READ_CMD 0x00000000
365#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000366#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800367#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
368#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
369#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800370#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800371#define ERIAR_MASK_SHIFT 12
372#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
373#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800374#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800375#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800376#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000377 EPHY_RXER_NUM = 0x7c,
378 OCPDR = 0xb0, /* OCP GPHY access */
379#define OCPDR_WRITE_CMD 0x80000000
380#define OCPDR_READ_CMD 0x00000000
381#define OCPDR_REG_MASK 0x7f
382#define OCPDR_GPHY_REG_SHIFT 16
383#define OCPDR_DATA_MASK 0xffff
384 OCPAR = 0xb4,
385#define OCPAR_FLAG 0x80000000
386#define OCPAR_GPHY_WRITE_CMD 0x8000f060
387#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800388 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000389 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
390 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200391#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800392#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800393#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800394#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800395#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000396};
397
Francois Romieu07d3f512007-02-21 22:40:46 +0100398enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100400 SYSErr = 0x8000,
401 PCSTimeout = 0x4000,
402 SWInt = 0x0100,
403 TxDescUnavail = 0x0080,
404 RxFIFOOver = 0x0040,
405 LinkChg = 0x0020,
406 RxOverflow = 0x0010,
407 TxErr = 0x0008,
408 TxOK = 0x0004,
409 RxErr = 0x0002,
410 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400413 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200414 RxFOVF = (1 << 23),
415 RxRWT = (1 << 22),
416 RxRES = (1 << 21),
417 RxRUNT = (1 << 20),
418 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800421 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100422 CmdReset = 0x10,
423 CmdRxEnb = 0x08,
424 CmdTxEnb = 0x04,
425 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Francois Romieu275391a2007-02-23 23:50:28 +0100427 /* TXPoll register p.5 */
428 HPQ = 0x80, /* Poll cmd on the high prio queue */
429 NPQ = 0x40, /* Poll cmd on the low prio queue */
430 FSWInt = 0x01, /* Forced software interrupt */
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100433 Cfg9346_Lock = 0x00,
434 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100437 AcceptErr = 0x20,
438 AcceptRunt = 0x10,
439 AcceptBroadcast = 0x08,
440 AcceptMulticast = 0x04,
441 AcceptMyPhys = 0x02,
442 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200443#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /* TxConfigBits */
446 TxInterFrameGapShift = 24,
447 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
448
Francois Romieu5d06a992006-02-23 00:47:58 +0100449 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200450 LEDS1 = (1 << 7),
451 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200452 Speed_down = (1 << 4),
453 MEMMAP = (1 << 3),
454 IOMAP = (1 << 2),
455 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100456 PMEnable = (1 << 0), /* Power Management Enable */
457
Francois Romieu6dccd162007-02-13 23:38:05 +0100458 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000459 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000460 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100461 PCI_Clock_66MHz = 0x01,
462 PCI_Clock_33MHz = 0x00,
463
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100464 /* Config3 register p.25 */
465 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
466 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200467 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800468 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200469 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100470
Francois Romieud58d46b2011-05-03 16:38:29 +0200471 /* Config4 register */
472 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
473
Francois Romieu5d06a992006-02-23 00:47:58 +0100474 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100475 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
476 MWF = (1 << 5), /* Accept Multicast wakeup frame */
477 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200478 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100479 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100480 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000481 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200484 EnableBist = (1 << 15), // 8168 8101
485 Mac_dbgo_oe = (1 << 14), // 8168 8101
486 Normal_mode = (1 << 13), // unused
487 Force_half_dup = (1 << 12), // 8168 8101
488 Force_rxflow_en = (1 << 11), // 8168 8101
489 Force_txflow_en = (1 << 10), // 8168 8101
490 Cxpl_dbg_sel = (1 << 9), // 8168 8101
491 ASF = (1 << 8), // 8168 8101
492 PktCntrDisable = (1 << 7), // 8168 8101
493 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 RxVlan = (1 << 6),
495 RxChkSum = (1 << 5),
496 PCIDAC = (1 << 4),
497 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200498#define INTT_MASK GENMASK(1, 0)
Francois Romieu0e485152007-02-20 00:00:26 +0100499 INTT_0 = 0x0000, // 8168
500 INTT_1 = 0x0001, // 8168
501 INTT_2 = 0x0002, // 8168
502 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100505 TBI_Enable = 0x80,
506 TxFlowCtrl = 0x40,
507 RxFlowCtrl = 0x20,
508 _1000bpsF = 0x10,
509 _100bps = 0x08,
510 _10bps = 0x04,
511 LinkStatus = 0x02,
512 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100515 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200516
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200517 /* ResetCounterCommand */
518 CounterReset = 0x1,
519
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200520 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100521 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800522
523 /* magic enable v2 */
524 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525};
526
Francois Romieu2b7b4312011-04-18 22:53:24 -0700527enum rtl_desc_bit {
528 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
530 RingEnd = (1 << 30), /* End of descriptor ring */
531 FirstFrag = (1 << 29), /* First segment of a packet */
532 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700533};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Francois Romieu2b7b4312011-04-18 22:53:24 -0700535/* Generic case. */
536enum rtl_tx_desc_bit {
537 /* First doubleword. */
538 TD_LSO = (1 << 27), /* Large Send Offload */
539#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Francois Romieu2b7b4312011-04-18 22:53:24 -0700541 /* Second doubleword. */
542 TxVlanTag = (1 << 17), /* Add VLAN tag */
543};
544
545/* 8169, 8168b and 810x except 8102e. */
546enum rtl_tx_desc_bit_0 {
547 /* First doubleword. */
548#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
549 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
550 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
551 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
552};
553
554/* 8102e, 8168c and beyond. */
555enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800556 /* First doubleword. */
557 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800558 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800559#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800560#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800561
Francois Romieu2b7b4312011-04-18 22:53:24 -0700562 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800563#define TCPHO_SHIFT 18
564#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700565#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800566 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
567 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700568 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
569 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
570};
571
Francois Romieu2b7b4312011-04-18 22:53:24 -0700572enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* Rx private */
574 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500575 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577#define RxProtoUDP (PID1)
578#define RxProtoTCP (PID0)
579#define RxProtoIP (PID1 | PID0)
580#define RxProtoMask RxProtoIP
581
582 IPFail = (1 << 16), /* IP checksum failed */
583 UDPFail = (1 << 15), /* UDP/IP checksum failed */
584 TCPFail = (1 << 14), /* TCP/IP checksum failed */
585 RxVlanTag = (1 << 16), /* VLAN tag available */
586};
587
588#define RsvdMask 0x3fffc000
Heiner Kallweit12d42c52018-04-28 22:19:30 +0200589#define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200592 __le32 opts1;
593 __le32 opts2;
594 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595};
596
597struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200598 __le32 opts1;
599 __le32 opts2;
600 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601};
602
603struct ring_info {
604 struct sk_buff *skb;
605 u32 len;
606 u8 __pad[sizeof(void *) - sizeof(u32)];
607};
608
Ivan Vecera355423d2009-02-06 21:49:57 -0800609struct rtl8169_counters {
610 __le64 tx_packets;
611 __le64 rx_packets;
612 __le64 tx_errors;
613 __le32 rx_errors;
614 __le16 rx_missed;
615 __le16 align_errors;
616 __le32 tx_one_collision;
617 __le32 tx_multi_collision;
618 __le64 rx_unicast;
619 __le64 rx_broadcast;
620 __le32 rx_multicast;
621 __le16 tx_aborted;
622 __le16 tx_underun;
623};
624
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200625struct rtl8169_tc_offsets {
626 bool inited;
627 __le64 tx_errors;
628 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200629 __le16 tx_aborted;
630};
631
Francois Romieuda78dbf2012-01-26 14:18:23 +0100632enum rtl_flag {
Francois Romieu6c4a70c2012-01-31 10:56:44 +0100633 RTL_FLAG_TASK_ENABLED,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100634 RTL_FLAG_TASK_SLOW_PENDING,
635 RTL_FLAG_TASK_RESET_PENDING,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100636 RTL_FLAG_MAX
637};
638
Junchang Wang8027aa22012-03-04 23:30:32 +0100639struct rtl8169_stats {
640 u64 packets;
641 u64 bytes;
642 struct u64_stats_sync syncp;
643};
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645struct rtl8169_private {
646 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200647 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000648 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700649 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200650 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700651 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
653 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100655 struct rtl8169_stats rx_stats;
656 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
658 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
659 dma_addr_t TxPhyAddr;
660 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000661 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100664
665 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300666 const struct rtl_coalesce_info *coalesce_info;
françois romieuc0e45c12011-01-03 15:08:04 +0000667
668 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200669 void (*write)(struct rtl8169_private *, int, int);
670 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000671 } mdio_ops;
672
Francois Romieud58d46b2011-05-03 16:38:29 +0200673 struct jumbo_ops {
674 void (*enable)(struct rtl8169_private *);
675 void (*disable)(struct rtl8169_private *);
676 } jumbo_ops;
677
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200678 void (*hw_start)(struct rtl8169_private *tp);
hayeswang5888d3f2014-07-11 16:25:56 +0800679 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100680
681 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100682 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
683 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100684 struct work_struct work;
685 } wk;
686
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +0200687 unsigned supports_gmii:1;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +0200688 struct mii_bus *mii_bus;
Corinna Vinschen42020322015-09-10 10:47:35 +0200689 dma_addr_t counters_phys_addr;
690 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200691 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000692 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000693
Francois Romieub6ffd972011-06-17 17:00:05 +0200694 struct rtl_fw {
695 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200696
697#define RTL_VER_SIZE 32
698
699 char version[RTL_VER_SIZE];
700
701 struct rtl_fw_phy_action {
702 __le32 *code;
703 size_t size;
704 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200705 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300706#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800707
708 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709};
710
Ralf Baechle979b6c12005-06-13 14:30:40 -0700711MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700714MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200715module_param_named(debug, debug.msg_enable, int, 0);
716MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717MODULE_LICENSE("GPL");
françois romieubca03d52011-01-03 15:07:31 +0000718MODULE_FIRMWARE(FIRMWARE_8168D_1);
719MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000720MODULE_FIRMWARE(FIRMWARE_8168E_1);
721MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400722MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800723MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800724MODULE_FIRMWARE(FIRMWARE_8168F_1);
725MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800726MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800727MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800728MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800729MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000730MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000731MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000732MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800733MODULE_FIRMWARE(FIRMWARE_8168H_1);
734MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200735MODULE_FIRMWARE(FIRMWARE_8107E_1);
736MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100738static inline struct device *tp_to_dev(struct rtl8169_private *tp)
739{
740 return &tp->pci_dev->dev;
741}
742
Francois Romieuda78dbf2012-01-26 14:18:23 +0100743static void rtl_lock_work(struct rtl8169_private *tp)
744{
745 mutex_lock(&tp->wk.mutex);
746}
747
748static void rtl_unlock_work(struct rtl8169_private *tp)
749{
750 mutex_unlock(&tp->wk.mutex);
751}
752
Heiner Kallweitcb732002018-03-20 07:45:35 +0100753static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200754{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100755 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800756 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200757}
758
Francois Romieuffc46952012-07-06 14:19:23 +0200759struct rtl_cond {
760 bool (*check)(struct rtl8169_private *);
761 const char *msg;
762};
763
764static void rtl_udelay(unsigned int d)
765{
766 udelay(d);
767}
768
769static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
770 void (*delay)(unsigned int), unsigned int d, int n,
771 bool high)
772{
773 int i;
774
775 for (i = 0; i < n; i++) {
776 delay(d);
777 if (c->check(tp) == high)
778 return true;
779 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200780 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
781 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200782 return false;
783}
784
785static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
786 const struct rtl_cond *c,
787 unsigned int d, int n)
788{
789 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
790}
791
792static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
793 const struct rtl_cond *c,
794 unsigned int d, int n)
795{
796 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
797}
798
799static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
800 const struct rtl_cond *c,
801 unsigned int d, int n)
802{
803 return rtl_loop_wait(tp, c, msleep, d, n, true);
804}
805
806static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
807 const struct rtl_cond *c,
808 unsigned int d, int n)
809{
810 return rtl_loop_wait(tp, c, msleep, d, n, false);
811}
812
813#define DECLARE_RTL_COND(name) \
814static bool name ## _check(struct rtl8169_private *); \
815 \
816static const struct rtl_cond name = { \
817 .check = name ## _check, \
818 .msg = #name \
819}; \
820 \
821static bool name ## _check(struct rtl8169_private *tp)
822
Hayes Wangc5583862012-07-02 17:23:22 +0800823static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
824{
825 if (reg & 0xffff0001) {
826 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
827 return true;
828 }
829 return false;
830}
831
832DECLARE_RTL_COND(rtl_ocp_gphy_cond)
833{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200834 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800835}
836
837static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
838{
Hayes Wangc5583862012-07-02 17:23:22 +0800839 if (rtl_ocp_reg_failure(tp, reg))
840 return;
841
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200842 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800843
844 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
845}
846
847static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
848{
Hayes Wangc5583862012-07-02 17:23:22 +0800849 if (rtl_ocp_reg_failure(tp, reg))
850 return 0;
851
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200852 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800853
854 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200855 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +0800856}
857
Hayes Wangc5583862012-07-02 17:23:22 +0800858static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
859{
Hayes Wangc5583862012-07-02 17:23:22 +0800860 if (rtl_ocp_reg_failure(tp, reg))
861 return;
862
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200863 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800864}
865
866static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
867{
Hayes Wangc5583862012-07-02 17:23:22 +0800868 if (rtl_ocp_reg_failure(tp, reg))
869 return 0;
870
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200871 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800872
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200873 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800874}
875
876#define OCP_STD_PHY_BASE 0xa400
877
878static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
879{
880 if (reg == 0x1f) {
881 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
882 return;
883 }
884
885 if (tp->ocp_base != OCP_STD_PHY_BASE)
886 reg -= 0x10;
887
888 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
889}
890
891static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
892{
893 if (tp->ocp_base != OCP_STD_PHY_BASE)
894 reg -= 0x10;
895
896 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
897}
898
hayeswangeee37862013-04-01 22:23:38 +0000899static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
900{
901 if (reg == 0x1f) {
902 tp->ocp_base = value << 4;
903 return;
904 }
905
906 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
907}
908
909static int mac_mcu_read(struct rtl8169_private *tp, int reg)
910{
911 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
912}
913
Francois Romieuffc46952012-07-06 14:19:23 +0200914DECLARE_RTL_COND(rtl_phyar_cond)
915{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200916 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200917}
918
Francois Romieu24192212012-07-06 20:19:42 +0200919static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200921 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Francois Romieuffc46952012-07-06 14:19:23 +0200923 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700924 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700925 * According to hardware specs a 20us delay is required after write
926 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700927 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700928 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Francois Romieu24192212012-07-06 20:19:42 +0200931static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Francois Romieuffc46952012-07-06 14:19:23 +0200933 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200935 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Francois Romieuffc46952012-07-06 14:19:23 +0200937 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200938 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +0200939
Timo Teräs81a95f02010-06-09 17:31:48 -0700940 /*
941 * According to hardware specs a 20us delay is required after read
942 * complete indication, but before sending next command.
943 */
944 udelay(20);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return value;
947}
948
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800949DECLARE_RTL_COND(rtl_ocpar_cond)
950{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200951 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800952}
953
Francois Romieu24192212012-07-06 20:19:42 +0200954static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000955{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200956 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
957 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
958 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000959
Francois Romieuffc46952012-07-06 14:19:23 +0200960 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +0000961}
962
Francois Romieu24192212012-07-06 20:19:42 +0200963static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000964{
Francois Romieu24192212012-07-06 20:19:42 +0200965 r8168dp_1_mdio_access(tp, reg,
966 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000967}
968
Francois Romieu24192212012-07-06 20:19:42 +0200969static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000970{
Francois Romieu24192212012-07-06 20:19:42 +0200971 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +0000972
973 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200974 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
975 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000976
Francois Romieuffc46952012-07-06 14:19:23 +0200977 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200978 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +0000979}
980
françois romieue6de30d2011-01-03 15:08:37 +0000981#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
982
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200983static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000984{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200985 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000986}
987
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200988static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000989{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200990 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000991}
992
Francois Romieu24192212012-07-06 20:19:42 +0200993static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +0000994{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200995 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000996
Francois Romieu24192212012-07-06 20:19:42 +0200997 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +0000998
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200999 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001000}
1001
Francois Romieu24192212012-07-06 20:19:42 +02001002static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001003{
1004 int value;
1005
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001006 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001007
Francois Romieu24192212012-07-06 20:19:42 +02001008 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001009
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001010 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001011
1012 return value;
1013}
1014
françois romieu4da19632011-01-03 15:07:55 +00001015static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001016{
Francois Romieu24192212012-07-06 20:19:42 +02001017 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001018}
1019
françois romieu4da19632011-01-03 15:07:55 +00001020static int rtl_readphy(struct rtl8169_private *tp, int location)
1021{
Francois Romieu24192212012-07-06 20:19:42 +02001022 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001023}
1024
1025static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1026{
1027 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1028}
1029
Chun-Hao Lin76564422014-10-01 23:17:17 +08001030static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001031{
1032 int val;
1033
françois romieu4da19632011-01-03 15:07:55 +00001034 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001035 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001036}
1037
Francois Romieuffc46952012-07-06 14:19:23 +02001038DECLARE_RTL_COND(rtl_ephyar_cond)
1039{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001040 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001041}
1042
Francois Romieufdf6fc02012-07-06 22:40:38 +02001043static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001044{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001045 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001046 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1047
Francois Romieuffc46952012-07-06 14:19:23 +02001048 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1049
1050 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001051}
1052
Francois Romieufdf6fc02012-07-06 22:40:38 +02001053static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001054{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001055 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001056
Francois Romieuffc46952012-07-06 14:19:23 +02001057 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001058 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001059}
1060
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001061DECLARE_RTL_COND(rtl_eriar_cond)
1062{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001063 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001064}
1065
Francois Romieufdf6fc02012-07-06 22:40:38 +02001066static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1067 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001068{
Hayes Wang133ac402011-07-06 15:58:05 +08001069 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001070 RTL_W32(tp, ERIDR, val);
1071 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001072
Francois Romieuffc46952012-07-06 14:19:23 +02001073 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001074}
1075
Francois Romieufdf6fc02012-07-06 22:40:38 +02001076static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001077{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001078 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001079
Francois Romieuffc46952012-07-06 14:19:23 +02001080 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001081 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001082}
1083
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001084static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001085 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001086{
1087 u32 val;
1088
Francois Romieufdf6fc02012-07-06 22:40:38 +02001089 val = rtl_eri_read(tp, addr, type);
1090 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001091}
1092
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001093static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1094{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001095 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001096 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001097 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001098}
1099
1100static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1101{
1102 return rtl_eri_read(tp, reg, ERIAR_OOB);
1103}
1104
1105static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1106{
1107 switch (tp->mac_version) {
1108 case RTL_GIGA_MAC_VER_27:
1109 case RTL_GIGA_MAC_VER_28:
1110 case RTL_GIGA_MAC_VER_31:
1111 return r8168dp_ocp_read(tp, mask, reg);
1112 case RTL_GIGA_MAC_VER_49:
1113 case RTL_GIGA_MAC_VER_50:
1114 case RTL_GIGA_MAC_VER_51:
1115 return r8168ep_ocp_read(tp, mask, reg);
1116 default:
1117 BUG();
1118 return ~0;
1119 }
1120}
1121
1122static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1123 u32 data)
1124{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001125 RTL_W32(tp, OCPDR, data);
1126 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001127 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1128}
1129
1130static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1131 u32 data)
1132{
1133 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1134 data, ERIAR_OOB);
1135}
1136
1137static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1138{
1139 switch (tp->mac_version) {
1140 case RTL_GIGA_MAC_VER_27:
1141 case RTL_GIGA_MAC_VER_28:
1142 case RTL_GIGA_MAC_VER_31:
1143 r8168dp_ocp_write(tp, mask, reg, data);
1144 break;
1145 case RTL_GIGA_MAC_VER_49:
1146 case RTL_GIGA_MAC_VER_50:
1147 case RTL_GIGA_MAC_VER_51:
1148 r8168ep_ocp_write(tp, mask, reg, data);
1149 break;
1150 default:
1151 BUG();
1152 break;
1153 }
1154}
1155
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001156static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1157{
1158 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1159
1160 ocp_write(tp, 0x1, 0x30, 0x00000001);
1161}
1162
1163#define OOB_CMD_RESET 0x00
1164#define OOB_CMD_DRIVER_START 0x05
1165#define OOB_CMD_DRIVER_STOP 0x06
1166
1167static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1168{
1169 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1170}
1171
1172DECLARE_RTL_COND(rtl_ocp_read_cond)
1173{
1174 u16 reg;
1175
1176 reg = rtl8168_get_ocp_reg(tp);
1177
1178 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1179}
1180
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001181DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1182{
1183 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1184}
1185
1186DECLARE_RTL_COND(rtl_ocp_tx_cond)
1187{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001188 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001189}
1190
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001191static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1192{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001193 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001194 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001195 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1196 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001197}
1198
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001199static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001200{
1201 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001202 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1203}
1204
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001205static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1206{
1207 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1208 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1209 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1210}
1211
1212static void rtl8168_driver_start(struct rtl8169_private *tp)
1213{
1214 switch (tp->mac_version) {
1215 case RTL_GIGA_MAC_VER_27:
1216 case RTL_GIGA_MAC_VER_28:
1217 case RTL_GIGA_MAC_VER_31:
1218 rtl8168dp_driver_start(tp);
1219 break;
1220 case RTL_GIGA_MAC_VER_49:
1221 case RTL_GIGA_MAC_VER_50:
1222 case RTL_GIGA_MAC_VER_51:
1223 rtl8168ep_driver_start(tp);
1224 break;
1225 default:
1226 BUG();
1227 break;
1228 }
1229}
1230
1231static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1232{
1233 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1234 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1235}
1236
1237static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1238{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001239 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001240 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1241 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1242 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1243}
1244
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001245static void rtl8168_driver_stop(struct rtl8169_private *tp)
1246{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001247 switch (tp->mac_version) {
1248 case RTL_GIGA_MAC_VER_27:
1249 case RTL_GIGA_MAC_VER_28:
1250 case RTL_GIGA_MAC_VER_31:
1251 rtl8168dp_driver_stop(tp);
1252 break;
1253 case RTL_GIGA_MAC_VER_49:
1254 case RTL_GIGA_MAC_VER_50:
1255 case RTL_GIGA_MAC_VER_51:
1256 rtl8168ep_driver_stop(tp);
1257 break;
1258 default:
1259 BUG();
1260 break;
1261 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001262}
1263
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001264static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001265{
1266 u16 reg = rtl8168_get_ocp_reg(tp);
1267
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001268 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001269}
1270
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001271static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001272{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001273 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001274}
1275
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001276static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001277{
1278 switch (tp->mac_version) {
1279 case RTL_GIGA_MAC_VER_27:
1280 case RTL_GIGA_MAC_VER_28:
1281 case RTL_GIGA_MAC_VER_31:
1282 return r8168dp_check_dash(tp);
1283 case RTL_GIGA_MAC_VER_49:
1284 case RTL_GIGA_MAC_VER_50:
1285 case RTL_GIGA_MAC_VER_51:
1286 return r8168ep_check_dash(tp);
1287 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001288 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001289 }
1290}
1291
françois romieuc28aa382011-08-02 03:53:43 +00001292struct exgmac_reg {
1293 u16 addr;
1294 u16 mask;
1295 u32 val;
1296};
1297
Francois Romieufdf6fc02012-07-06 22:40:38 +02001298static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001299 const struct exgmac_reg *r, int len)
1300{
1301 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001302 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001303 r++;
1304 }
1305}
1306
Francois Romieuffc46952012-07-06 14:19:23 +02001307DECLARE_RTL_COND(rtl_efusear_cond)
1308{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001309 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001310}
1311
Francois Romieufdf6fc02012-07-06 22:40:38 +02001312static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001313{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001314 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001315
Francois Romieuffc46952012-07-06 14:19:23 +02001316 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001317 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001318}
1319
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001320static u16 rtl_get_events(struct rtl8169_private *tp)
1321{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001322 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001323}
1324
1325static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1326{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001327 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001328 mmiowb();
1329}
1330
1331static void rtl_irq_disable(struct rtl8169_private *tp)
1332{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001333 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001334 mmiowb();
1335}
1336
Francois Romieu3e990ff2012-01-26 12:50:01 +01001337static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1338{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001339 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001340}
1341
Francois Romieuda78dbf2012-01-26 14:18:23 +01001342#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1343#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1344#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1345
1346static void rtl_irq_enable_all(struct rtl8169_private *tp)
1347{
1348 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1349}
1350
françois romieu811fd302011-12-04 20:30:45 +00001351static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001353 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001354 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001355 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Hayes Wang70090422011-07-06 15:58:06 +08001358static void rtl_link_chg_patch(struct rtl8169_private *tp)
1359{
Hayes Wang70090422011-07-06 15:58:06 +08001360 struct net_device *dev = tp->dev;
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001361 struct phy_device *phydev = dev->phydev;
Hayes Wang70090422011-07-06 15:58:06 +08001362
1363 if (!netif_running(dev))
1364 return;
1365
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001366 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1367 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001368 if (phydev->speed == SPEED_1000) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001369 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1370 ERIAR_EXGMAC);
1371 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1372 ERIAR_EXGMAC);
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001373 } else if (phydev->speed == SPEED_100) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001374 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1375 ERIAR_EXGMAC);
1376 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1377 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001378 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001379 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1380 ERIAR_EXGMAC);
1381 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1382 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001383 }
1384 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001385 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001386 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001387 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001388 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001389 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1390 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001391 if (phydev->speed == SPEED_1000) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001392 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1393 ERIAR_EXGMAC);
1394 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1395 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001396 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001397 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1398 ERIAR_EXGMAC);
1399 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1400 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001401 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001402 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001403 if (phydev->speed == SPEED_10) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001404 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1405 ERIAR_EXGMAC);
1406 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1407 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001408 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001409 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1410 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001411 }
Hayes Wang70090422011-07-06 15:58:06 +08001412 }
1413}
1414
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001415#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1416
1417static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1418{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001419 u8 options;
1420 u32 wolopts = 0;
1421
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001422 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001423 if (!(options & PMEnable))
1424 return 0;
1425
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001426 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001427 if (options & LinkUp)
1428 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001429 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001430 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1431 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001432 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1433 wolopts |= WAKE_MAGIC;
1434 break;
1435 default:
1436 if (options & MagicPacket)
1437 wolopts |= WAKE_MAGIC;
1438 break;
1439 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001440
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001441 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001442 if (options & UWF)
1443 wolopts |= WAKE_UCAST;
1444 if (options & BWF)
1445 wolopts |= WAKE_BCAST;
1446 if (options & MWF)
1447 wolopts |= WAKE_MCAST;
1448
1449 return wolopts;
1450}
1451
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001452static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1453{
1454 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001455
Francois Romieuda78dbf2012-01-26 14:18:23 +01001456 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001457 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001458 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001459 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001460}
1461
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001462static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001463{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001464 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001465 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001466 u32 opt;
1467 u16 reg;
1468 u8 mask;
1469 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001470 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001471 { WAKE_UCAST, Config5, UWF },
1472 { WAKE_BCAST, Config5, BWF },
1473 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001474 { WAKE_ANY, Config5, LanWake },
1475 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001476 };
Francois Romieu851e6022012-04-17 11:10:11 +02001477 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001478
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001479 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001480
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001481 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001482 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1483 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001484 tmp = ARRAY_SIZE(cfg) - 1;
1485 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001486 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001487 0x0dc,
1488 ERIAR_MASK_0100,
1489 MagicPacket_v2,
1490 0x0000,
1491 ERIAR_EXGMAC);
1492 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001493 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001494 0x0dc,
1495 ERIAR_MASK_0100,
1496 0x0000,
1497 MagicPacket_v2,
1498 ERIAR_EXGMAC);
1499 break;
1500 default:
1501 tmp = ARRAY_SIZE(cfg);
1502 break;
1503 }
1504
1505 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001506 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001507 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001508 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001509 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001510 }
1511
Francois Romieu851e6022012-04-17 11:10:11 +02001512 switch (tp->mac_version) {
1513 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001514 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001515 if (wolopts)
1516 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001517 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001518 break;
1519 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001520 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001521 if (wolopts)
1522 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001523 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001524 break;
1525 }
1526
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001527 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001528}
1529
1530static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1531{
1532 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001533 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001534
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001535 if (wol->wolopts & ~WAKE_ANY)
1536 return -EINVAL;
1537
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001538 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001539
Francois Romieuda78dbf2012-01-26 14:18:23 +01001540 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001541
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001542 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001543
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001544 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001545 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001546
1547 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001548
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001549 device_set_wakeup_enable(d, tp->saved_wolopts);
françois romieuea809072010-11-08 13:23:58 +00001550
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001551 pm_runtime_put_noidle(d);
1552
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001553 return 0;
1554}
1555
Francois Romieu31bd2042011-04-26 18:58:59 +02001556static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1557{
Francois Romieu85bffe62011-04-27 08:22:39 +02001558 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001559}
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561static void rtl8169_get_drvinfo(struct net_device *dev,
1562 struct ethtool_drvinfo *info)
1563{
1564 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001565 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Rick Jones68aad782011-11-07 13:29:27 +00001567 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001568 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001569 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001570 if (!IS_ERR_OR_NULL(rtl_fw))
1571 strlcpy(info->fw_version, rtl_fw->version,
1572 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
1575static int rtl8169_get_regs_len(struct net_device *dev)
1576{
1577 return R8169_REGS_SIZE;
1578}
1579
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001580static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1581 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Francois Romieud58d46b2011-05-03 16:38:29 +02001583 struct rtl8169_private *tp = netdev_priv(dev);
1584
Francois Romieu2b7b4312011-04-18 22:53:24 -07001585 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001586 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Francois Romieud58d46b2011-05-03 16:38:29 +02001588 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001589 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001590 features &= ~NETIF_F_IP_CSUM;
1591
Michał Mirosław350fb322011-04-08 06:35:56 +00001592 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Heiner Kallweita3984572018-04-28 22:19:15 +02001595static int rtl8169_set_features(struct net_device *dev,
1596 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001599 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Heiner Kallweita3984572018-04-28 22:19:15 +02001601 rtl_lock_work(tp);
1602
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001603 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001604 if (features & NETIF_F_RXALL)
1605 rx_config |= (AcceptErr | AcceptRunt);
1606 else
1607 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001609 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001610
hayeswang929a0312014-09-16 11:40:47 +08001611 if (features & NETIF_F_RXCSUM)
1612 tp->cp_cmd |= RxChkSum;
1613 else
1614 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001615
hayeswang929a0312014-09-16 11:40:47 +08001616 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1617 tp->cp_cmd |= RxVlan;
1618 else
1619 tp->cp_cmd &= ~RxVlan;
1620
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001621 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1622 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Francois Romieuda78dbf2012-01-26 14:18:23 +01001624 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 return 0;
1627}
1628
Kirill Smelkov810f4892012-11-10 21:11:02 +04001629static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001631 return (skb_vlan_tag_present(skb)) ?
1632 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
Francois Romieu7a8fc772011-03-01 17:18:33 +01001635static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Francois Romieu7a8fc772011-03-01 17:18:33 +01001639 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001640 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1644 void *p)
1645{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001646 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001647 u32 __iomem *data = tp->mmio_addr;
1648 u32 *dw = p;
1649 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Francois Romieuda78dbf2012-01-26 14:18:23 +01001651 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001652 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1653 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001654 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001657static u32 rtl8169_get_msglevel(struct net_device *dev)
1658{
1659 struct rtl8169_private *tp = netdev_priv(dev);
1660
1661 return tp->msg_enable;
1662}
1663
1664static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1665{
1666 struct rtl8169_private *tp = netdev_priv(dev);
1667
1668 tp->msg_enable = value;
1669}
1670
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001671static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1672 "tx_packets",
1673 "rx_packets",
1674 "tx_errors",
1675 "rx_errors",
1676 "rx_missed",
1677 "align_errors",
1678 "tx_single_collisions",
1679 "tx_multi_collisions",
1680 "unicast",
1681 "broadcast",
1682 "multicast",
1683 "tx_aborted",
1684 "tx_underrun",
1685};
1686
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001687static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001688{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001689 switch (sset) {
1690 case ETH_SS_STATS:
1691 return ARRAY_SIZE(rtl8169_gstrings);
1692 default:
1693 return -EOPNOTSUPP;
1694 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001695}
1696
Corinna Vinschen42020322015-09-10 10:47:35 +02001697DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001698{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001699 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001700}
1701
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001702static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001703{
Corinna Vinschen42020322015-09-10 10:47:35 +02001704 dma_addr_t paddr = tp->counters_phys_addr;
1705 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001706
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001707 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1708 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001709 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001710 RTL_W32(tp, CounterAddrLow, cmd);
1711 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001712
Francois Romieua78e9362018-01-26 01:53:26 +01001713 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001714}
1715
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001716static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001717{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001718 /*
1719 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1720 * tally counters.
1721 */
1722 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1723 return true;
1724
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001725 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001726}
1727
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001728static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001729{
Ivan Vecera355423d2009-02-06 21:49:57 -08001730 /*
1731 * Some chips are unable to dump tally counters when the receiver
1732 * is disabled.
1733 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001734 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001735 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001736
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001737 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001738}
1739
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001740static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001741{
Corinna Vinschen42020322015-09-10 10:47:35 +02001742 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001743 bool ret = false;
1744
1745 /*
1746 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1747 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1748 * reset by a power cycle, while the counter values collected by the
1749 * driver are reset at every driver unload/load cycle.
1750 *
1751 * To make sure the HW values returned by @get_stats64 match the SW
1752 * values, we collect the initial values at first open(*) and use them
1753 * as offsets to normalize the values returned by @get_stats64.
1754 *
1755 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1756 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1757 * set at open time by rtl_hw_start.
1758 */
1759
1760 if (tp->tc_offset.inited)
1761 return true;
1762
1763 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001764 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001765 ret = true;
1766
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001767 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001768 ret = true;
1769
Corinna Vinschen42020322015-09-10 10:47:35 +02001770 tp->tc_offset.tx_errors = counters->tx_errors;
1771 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1772 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001773 tp->tc_offset.inited = true;
1774
1775 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001776}
1777
Ivan Vecera355423d2009-02-06 21:49:57 -08001778static void rtl8169_get_ethtool_stats(struct net_device *dev,
1779 struct ethtool_stats *stats, u64 *data)
1780{
1781 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001782 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02001783 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08001784
1785 ASSERT_RTNL();
1786
Chun-Hao Line0636232016-07-29 16:37:55 +08001787 pm_runtime_get_noresume(d);
1788
1789 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001790 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08001791
1792 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08001793
Corinna Vinschen42020322015-09-10 10:47:35 +02001794 data[0] = le64_to_cpu(counters->tx_packets);
1795 data[1] = le64_to_cpu(counters->rx_packets);
1796 data[2] = le64_to_cpu(counters->tx_errors);
1797 data[3] = le32_to_cpu(counters->rx_errors);
1798 data[4] = le16_to_cpu(counters->rx_missed);
1799 data[5] = le16_to_cpu(counters->align_errors);
1800 data[6] = le32_to_cpu(counters->tx_one_collision);
1801 data[7] = le32_to_cpu(counters->tx_multi_collision);
1802 data[8] = le64_to_cpu(counters->rx_unicast);
1803 data[9] = le64_to_cpu(counters->rx_broadcast);
1804 data[10] = le32_to_cpu(counters->rx_multicast);
1805 data[11] = le16_to_cpu(counters->tx_aborted);
1806 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08001807}
1808
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001809static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1810{
1811 switch(stringset) {
1812 case ETH_SS_STATS:
1813 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1814 break;
1815 }
1816}
1817
Francois Romieu50970832017-10-27 13:24:49 +03001818/*
1819 * Interrupt coalescing
1820 *
1821 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1822 * > 8169, 8168 and 810x line of chipsets
1823 *
1824 * 8169, 8168, and 8136(810x) serial chipsets support it.
1825 *
1826 * > 2 - the Tx timer unit at gigabit speed
1827 *
1828 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1829 * (0xe0) bit 1 and bit 0.
1830 *
1831 * For 8169
1832 * bit[1:0] \ speed 1000M 100M 10M
1833 * 0 0 320ns 2.56us 40.96us
1834 * 0 1 2.56us 20.48us 327.7us
1835 * 1 0 5.12us 40.96us 655.4us
1836 * 1 1 10.24us 81.92us 1.31ms
1837 *
1838 * For the other
1839 * bit[1:0] \ speed 1000M 100M 10M
1840 * 0 0 5us 2.56us 40.96us
1841 * 0 1 40us 20.48us 327.7us
1842 * 1 0 80us 40.96us 655.4us
1843 * 1 1 160us 81.92us 1.31ms
1844 */
1845
1846/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1847struct rtl_coalesce_scale {
1848 /* Rx / Tx */
1849 u32 nsecs[2];
1850};
1851
1852/* rx/tx scale factors for all CPlusCmd[0:1] cases */
1853struct rtl_coalesce_info {
1854 u32 speed;
1855 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1856};
1857
1858/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1859#define rxtx_x1822(r, t) { \
1860 {{(r), (t)}}, \
1861 {{(r)*8, (t)*8}}, \
1862 {{(r)*8*2, (t)*8*2}}, \
1863 {{(r)*8*2*2, (t)*8*2*2}}, \
1864}
1865static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1866 /* speed delays: rx00 tx00 */
1867 { SPEED_10, rxtx_x1822(40960, 40960) },
1868 { SPEED_100, rxtx_x1822( 2560, 2560) },
1869 { SPEED_1000, rxtx_x1822( 320, 320) },
1870 { 0 },
1871};
1872
1873static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1874 /* speed delays: rx00 tx00 */
1875 { SPEED_10, rxtx_x1822(40960, 40960) },
1876 { SPEED_100, rxtx_x1822( 2560, 2560) },
1877 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1878 { 0 },
1879};
1880#undef rxtx_x1822
1881
1882/* get rx/tx scale vector corresponding to current speed */
1883static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1884{
1885 struct rtl8169_private *tp = netdev_priv(dev);
1886 struct ethtool_link_ksettings ecmd;
1887 const struct rtl_coalesce_info *ci;
1888 int rc;
1889
Heiner Kallweit45772432018-07-17 22:51:44 +02001890 rc = phy_ethtool_get_link_ksettings(dev, &ecmd);
Francois Romieu50970832017-10-27 13:24:49 +03001891 if (rc < 0)
1892 return ERR_PTR(rc);
1893
1894 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
1895 if (ecmd.base.speed == ci->speed) {
1896 return ci;
1897 }
1898 }
1899
1900 return ERR_PTR(-ELNRNG);
1901}
1902
1903static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1904{
1905 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001906 const struct rtl_coalesce_info *ci;
1907 const struct rtl_coalesce_scale *scale;
1908 struct {
1909 u32 *max_frames;
1910 u32 *usecs;
1911 } coal_settings [] = {
1912 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1913 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1914 }, *p = coal_settings;
1915 int i;
1916 u16 w;
1917
1918 memset(ec, 0, sizeof(*ec));
1919
1920 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1921 ci = rtl_coalesce_info(dev);
1922 if (IS_ERR(ci))
1923 return PTR_ERR(ci);
1924
Heiner Kallweit0ae09742018-04-28 22:19:26 +02001925 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03001926
1927 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001928 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03001929 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1930 w >>= RTL_COALESCE_SHIFT;
1931 *p->usecs = w & RTL_COALESCE_MASK;
1932 }
1933
1934 for (i = 0; i < 2; i++) {
1935 p = coal_settings + i;
1936 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1937
1938 /*
1939 * ethtool_coalesce says it is illegal to set both usecs and
1940 * max_frames to 0.
1941 */
1942 if (!*p->usecs && !*p->max_frames)
1943 *p->max_frames = 1;
1944 }
1945
1946 return 0;
1947}
1948
1949/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1950static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1951 struct net_device *dev, u32 nsec, u16 *cp01)
1952{
1953 const struct rtl_coalesce_info *ci;
1954 u16 i;
1955
1956 ci = rtl_coalesce_info(dev);
1957 if (IS_ERR(ci))
1958 return ERR_CAST(ci);
1959
1960 for (i = 0; i < 4; i++) {
1961 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1962 ci->scalev[i].nsecs[1]);
1963 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1964 *cp01 = i;
1965 return &ci->scalev[i];
1966 }
1967 }
1968
1969 return ERR_PTR(-EINVAL);
1970}
1971
1972static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1973{
1974 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001975 const struct rtl_coalesce_scale *scale;
1976 struct {
1977 u32 frames;
1978 u32 usecs;
1979 } coal_settings [] = {
1980 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1981 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1982 }, *p = coal_settings;
1983 u16 w = 0, cp01;
1984 int i;
1985
1986 scale = rtl_coalesce_choose_scale(dev,
1987 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1988 if (IS_ERR(scale))
1989 return PTR_ERR(scale);
1990
1991 for (i = 0; i < 2; i++, p++) {
1992 u32 units;
1993
1994 /*
1995 * accept max_frames=1 we returned in rtl_get_coalesce.
1996 * accept it not only when usecs=0 because of e.g. the following scenario:
1997 *
1998 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
1999 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2000 * - then user does `ethtool -C eth0 rx-usecs 100`
2001 *
2002 * since ethtool sends to kernel whole ethtool_coalesce
2003 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2004 * we'll reject it below in `frames % 4 != 0`.
2005 */
2006 if (p->frames == 1) {
2007 p->frames = 0;
2008 }
2009
2010 units = p->usecs * 1000 / scale->nsecs[i];
2011 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2012 return -EINVAL;
2013
2014 w <<= RTL_COALESCE_SHIFT;
2015 w |= units;
2016 w <<= RTL_COALESCE_SHIFT;
2017 w |= p->frames >> 2;
2018 }
2019
2020 rtl_lock_work(tp);
2021
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002022 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002023
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02002024 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002025 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2026 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002027
2028 rtl_unlock_work(tp);
2029
2030 return 0;
2031}
2032
Jeff Garzik7282d492006-09-13 14:30:00 -04002033static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 .get_drvinfo = rtl8169_get_drvinfo,
2035 .get_regs_len = rtl8169_get_regs_len,
2036 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002037 .get_coalesce = rtl_get_coalesce,
2038 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002039 .get_msglevel = rtl8169_get_msglevel,
2040 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002042 .get_wol = rtl8169_get_wol,
2043 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002044 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002045 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002046 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002047 .get_ts_info = ethtool_op_get_ts_info,
Heiner Kallweitdd849572018-07-17 22:51:48 +02002048 .nway_reset = phy_ethtool_nway_reset,
Heiner Kallweit45772432018-07-17 22:51:44 +02002049 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2050 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051};
2052
Francois Romieu07d3f512007-02-21 22:40:46 +01002053static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Heiner Kallweit22148df2018-04-22 17:15:15 +02002054 u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Francois Romieu0e485152007-02-20 00:00:26 +01002056 /*
2057 * The driver currently handles the 8168Bf and the 8168Be identically
2058 * but they can be identified more specifically through the test below
2059 * if needed:
2060 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002061 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002062 *
2063 * Same thing for the 8101Eb and the 8101Ec:
2064 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002065 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002066 */
Francois Romieu37441002011-06-17 22:58:54 +02002067 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002069 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 int mac_version;
2071 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002072 /* 8168EP family. */
2073 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2074 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2075 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2076
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002077 /* 8168H family. */
2078 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2079 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2080
Hayes Wangc5583862012-07-02 17:23:22 +08002081 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002082 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002083 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002084 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2085 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2086
Hayes Wangc2218922011-09-06 16:55:18 +08002087 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002088 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002089 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2090 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2091
hayeswang01dc7fe2011-03-21 01:50:28 +00002092 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002093 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002094 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2095 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2096
Francois Romieu5b538df2008-07-20 16:22:45 +02002097 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002098 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002099 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002100
françois romieue6de30d2011-01-03 15:08:37 +00002101 /* 8168DP family. */
2102 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2103 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002104 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002105
Francois Romieuef808d52008-06-29 13:10:54 +02002106 /* 8168C family. */
Francois Romieuef3386f2008-06-29 12:24:30 +02002107 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002108 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002109 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002110 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2111 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002112 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieuef808d52008-06-29 13:10:54 +02002113 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002114
2115 /* 8168B family. */
2116 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002117 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2118 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2119
2120 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002121 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002122 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002123 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2124 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002125 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2126 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2127 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2128 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002129 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002130 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002131 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002132 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2133 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002134 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2135 /* FIXME: where did these entries come from ? -- FR */
2136 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2137 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2138
2139 /* 8110 family. */
2140 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2141 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2142 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2143 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2144 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2145 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2146
Jean Delvaref21b75e2009-05-26 20:54:48 -07002147 /* Catch-all */
2148 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002149 };
2150 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 u32 reg;
2152
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002153 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002154 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 p++;
2156 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002157
2158 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02002159 dev_notice(tp_to_dev(tp),
2160 "unknown MAC, using family default\n");
Francois Romieu5d320a22011-05-08 17:47:36 +02002161 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002162 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02002163 tp->mac_version = tp->supports_gmii ?
hayeswang58152cd2013-04-01 22:23:42 +00002164 RTL_GIGA_MAC_VER_42 :
2165 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002166 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02002167 tp->mac_version = tp->supports_gmii ?
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002168 RTL_GIGA_MAC_VER_45 :
2169 RTL_GIGA_MAC_VER_47;
2170 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02002171 tp->mac_version = tp->supports_gmii ?
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002172 RTL_GIGA_MAC_VER_46 :
2173 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
2177static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2178{
Heiner Kallweit49d17512018-06-28 20:36:15 +02002179 netif_dbg(tp, drv, tp->dev, "mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Francois Romieu867763c2007-08-17 18:21:58 +02002182struct phy_reg {
2183 u16 reg;
2184 u16 val;
2185};
2186
françois romieu4da19632011-01-03 15:07:55 +00002187static void rtl_writephy_batch(struct rtl8169_private *tp,
2188 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002189{
2190 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002191 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002192 regs++;
2193 }
2194}
2195
françois romieubca03d52011-01-03 15:07:31 +00002196#define PHY_READ 0x00000000
2197#define PHY_DATA_OR 0x10000000
2198#define PHY_DATA_AND 0x20000000
2199#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002200#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002201#define PHY_CLEAR_READCOUNT 0x70000000
2202#define PHY_WRITE 0x80000000
2203#define PHY_READCOUNT_EQ_SKIP 0x90000000
2204#define PHY_COMP_EQ_SKIPN 0xa0000000
2205#define PHY_COMP_NEQ_SKIPN 0xb0000000
2206#define PHY_WRITE_PREVIOUS 0xc0000000
2207#define PHY_SKIPN 0xd0000000
2208#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002209
Hayes Wang960aee62011-06-18 11:37:48 +02002210struct fw_info {
2211 u32 magic;
2212 char version[RTL_VER_SIZE];
2213 __le32 fw_start;
2214 __le32 fw_len;
2215 u8 chksum;
2216} __packed;
2217
Francois Romieu1c361ef2011-06-17 17:16:24 +02002218#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2219
2220static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002221{
Francois Romieub6ffd972011-06-17 17:00:05 +02002222 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002223 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002224 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2225 char *version = rtl_fw->version;
2226 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002227
Francois Romieu1c361ef2011-06-17 17:16:24 +02002228 if (fw->size < FW_OPCODE_SIZE)
2229 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002230
2231 if (!fw_info->magic) {
2232 size_t i, size, start;
2233 u8 checksum = 0;
2234
2235 if (fw->size < sizeof(*fw_info))
2236 goto out;
2237
2238 for (i = 0; i < fw->size; i++)
2239 checksum += fw->data[i];
2240 if (checksum != 0)
2241 goto out;
2242
2243 start = le32_to_cpu(fw_info->fw_start);
2244 if (start > fw->size)
2245 goto out;
2246
2247 size = le32_to_cpu(fw_info->fw_len);
2248 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2249 goto out;
2250
2251 memcpy(version, fw_info->version, RTL_VER_SIZE);
2252
2253 pa->code = (__le32 *)(fw->data + start);
2254 pa->size = size;
2255 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002256 if (fw->size % FW_OPCODE_SIZE)
2257 goto out;
2258
2259 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2260
2261 pa->code = (__le32 *)fw->data;
2262 pa->size = fw->size / FW_OPCODE_SIZE;
2263 }
2264 version[RTL_VER_SIZE - 1] = 0;
2265
2266 rc = true;
2267out:
2268 return rc;
2269}
2270
Francois Romieufd112f22011-06-18 00:10:29 +02002271static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2272 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002273{
Francois Romieufd112f22011-06-18 00:10:29 +02002274 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002275 size_t index;
2276
Francois Romieu1c361ef2011-06-17 17:16:24 +02002277 for (index = 0; index < pa->size; index++) {
2278 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002279 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002280
hayeswang42b82dc2011-01-10 02:07:25 +00002281 switch(action & 0xf0000000) {
2282 case PHY_READ:
2283 case PHY_DATA_OR:
2284 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002285 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002286 case PHY_CLEAR_READCOUNT:
2287 case PHY_WRITE:
2288 case PHY_WRITE_PREVIOUS:
2289 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002290 break;
2291
hayeswang42b82dc2011-01-10 02:07:25 +00002292 case PHY_BJMPN:
2293 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002294 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002295 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002296 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002297 }
2298 break;
2299 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002300 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002301 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002302 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002303 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002304 }
2305 break;
2306 case PHY_COMP_EQ_SKIPN:
2307 case PHY_COMP_NEQ_SKIPN:
2308 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002309 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002310 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002311 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002312 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002313 }
2314 break;
2315
hayeswang42b82dc2011-01-10 02:07:25 +00002316 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002317 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002318 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002319 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002320 }
2321 }
Francois Romieufd112f22011-06-18 00:10:29 +02002322 rc = true;
2323out:
2324 return rc;
2325}
françois romieubca03d52011-01-03 15:07:31 +00002326
Francois Romieufd112f22011-06-18 00:10:29 +02002327static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2328{
2329 struct net_device *dev = tp->dev;
2330 int rc = -EINVAL;
2331
2332 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002333 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002334 goto out;
2335 }
2336
2337 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2338 rc = 0;
2339out:
2340 return rc;
2341}
2342
2343static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2344{
2345 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002346 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002347 u32 predata, count;
2348 size_t index;
2349
2350 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002351 org.write = ops->write;
2352 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002353
Francois Romieu1c361ef2011-06-17 17:16:24 +02002354 for (index = 0; index < pa->size; ) {
2355 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002356 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002357 u32 regno = (action & 0x0fff0000) >> 16;
2358
2359 if (!action)
2360 break;
françois romieubca03d52011-01-03 15:07:31 +00002361
2362 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002363 case PHY_READ:
2364 predata = rtl_readphy(tp, regno);
2365 count++;
2366 index++;
françois romieubca03d52011-01-03 15:07:31 +00002367 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002368 case PHY_DATA_OR:
2369 predata |= data;
2370 index++;
2371 break;
2372 case PHY_DATA_AND:
2373 predata &= data;
2374 index++;
2375 break;
2376 case PHY_BJMPN:
2377 index -= regno;
2378 break;
hayeswangeee37862013-04-01 22:23:38 +00002379 case PHY_MDIO_CHG:
2380 if (data == 0) {
2381 ops->write = org.write;
2382 ops->read = org.read;
2383 } else if (data == 1) {
2384 ops->write = mac_mcu_write;
2385 ops->read = mac_mcu_read;
2386 }
2387
hayeswang42b82dc2011-01-10 02:07:25 +00002388 index++;
2389 break;
2390 case PHY_CLEAR_READCOUNT:
2391 count = 0;
2392 index++;
2393 break;
2394 case PHY_WRITE:
2395 rtl_writephy(tp, regno, data);
2396 index++;
2397 break;
2398 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002399 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002400 break;
2401 case PHY_COMP_EQ_SKIPN:
2402 if (predata == data)
2403 index += regno;
2404 index++;
2405 break;
2406 case PHY_COMP_NEQ_SKIPN:
2407 if (predata != data)
2408 index += regno;
2409 index++;
2410 break;
2411 case PHY_WRITE_PREVIOUS:
2412 rtl_writephy(tp, regno, predata);
2413 index++;
2414 break;
2415 case PHY_SKIPN:
2416 index += regno + 1;
2417 break;
2418 case PHY_DELAY_MS:
2419 mdelay(data);
2420 index++;
2421 break;
2422
françois romieubca03d52011-01-03 15:07:31 +00002423 default:
2424 BUG();
2425 }
2426 }
hayeswangeee37862013-04-01 22:23:38 +00002427
2428 ops->write = org.write;
2429 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002430}
2431
françois romieuf1e02ed2011-01-13 13:07:53 +00002432static void rtl_release_firmware(struct rtl8169_private *tp)
2433{
Francois Romieub6ffd972011-06-17 17:00:05 +02002434 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2435 release_firmware(tp->rtl_fw->fw);
2436 kfree(tp->rtl_fw);
2437 }
2438 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002439}
2440
François Romieu953a12c2011-04-24 17:38:48 +02002441static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002442{
Francois Romieub6ffd972011-06-17 17:00:05 +02002443 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002444
2445 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002446 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002447 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002448}
2449
2450static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2451{
2452 if (rtl_readphy(tp, reg) != val)
2453 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2454 else
2455 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002456}
2457
françois romieu4da19632011-01-03 15:07:55 +00002458static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002460 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002461 { 0x1f, 0x0001 },
2462 { 0x06, 0x006e },
2463 { 0x08, 0x0708 },
2464 { 0x15, 0x4000 },
2465 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
françois romieu0b9b5712009-08-10 19:44:56 +00002467 { 0x1f, 0x0001 },
2468 { 0x03, 0x00a1 },
2469 { 0x02, 0x0008 },
2470 { 0x01, 0x0120 },
2471 { 0x00, 0x1000 },
2472 { 0x04, 0x0800 },
2473 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
françois romieu0b9b5712009-08-10 19:44:56 +00002475 { 0x03, 0xff41 },
2476 { 0x02, 0xdf60 },
2477 { 0x01, 0x0140 },
2478 { 0x00, 0x0077 },
2479 { 0x04, 0x7800 },
2480 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
françois romieu0b9b5712009-08-10 19:44:56 +00002482 { 0x03, 0x802f },
2483 { 0x02, 0x4f02 },
2484 { 0x01, 0x0409 },
2485 { 0x00, 0xf0f9 },
2486 { 0x04, 0x9800 },
2487 { 0x04, 0x9000 },
2488
2489 { 0x03, 0xdf01 },
2490 { 0x02, 0xdf20 },
2491 { 0x01, 0xff95 },
2492 { 0x00, 0xba00 },
2493 { 0x04, 0xa800 },
2494 { 0x04, 0xa000 },
2495
2496 { 0x03, 0xff41 },
2497 { 0x02, 0xdf20 },
2498 { 0x01, 0x0140 },
2499 { 0x00, 0x00bb },
2500 { 0x04, 0xb800 },
2501 { 0x04, 0xb000 },
2502
2503 { 0x03, 0xdf41 },
2504 { 0x02, 0xdc60 },
2505 { 0x01, 0x6340 },
2506 { 0x00, 0x007d },
2507 { 0x04, 0xd800 },
2508 { 0x04, 0xd000 },
2509
2510 { 0x03, 0xdf01 },
2511 { 0x02, 0xdf20 },
2512 { 0x01, 0x100a },
2513 { 0x00, 0xa0ff },
2514 { 0x04, 0xf800 },
2515 { 0x04, 0xf000 },
2516
2517 { 0x1f, 0x0000 },
2518 { 0x0b, 0x0000 },
2519 { 0x00, 0x9200 }
2520 };
2521
françois romieu4da19632011-01-03 15:07:55 +00002522 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
françois romieu4da19632011-01-03 15:07:55 +00002525static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002526{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002527 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002528 { 0x1f, 0x0002 },
2529 { 0x01, 0x90d0 },
2530 { 0x1f, 0x0000 }
2531 };
2532
françois romieu4da19632011-01-03 15:07:55 +00002533 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002534}
2535
françois romieu4da19632011-01-03 15:07:55 +00002536static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002537{
2538 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002539
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002540 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2541 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002542 return;
2543
françois romieu4da19632011-01-03 15:07:55 +00002544 rtl_writephy(tp, 0x1f, 0x0001);
2545 rtl_writephy(tp, 0x10, 0xf01b);
2546 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002547}
2548
françois romieu4da19632011-01-03 15:07:55 +00002549static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002550{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002551 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002552 { 0x1f, 0x0001 },
2553 { 0x04, 0x0000 },
2554 { 0x03, 0x00a1 },
2555 { 0x02, 0x0008 },
2556 { 0x01, 0x0120 },
2557 { 0x00, 0x1000 },
2558 { 0x04, 0x0800 },
2559 { 0x04, 0x9000 },
2560 { 0x03, 0x802f },
2561 { 0x02, 0x4f02 },
2562 { 0x01, 0x0409 },
2563 { 0x00, 0xf099 },
2564 { 0x04, 0x9800 },
2565 { 0x04, 0xa000 },
2566 { 0x03, 0xdf01 },
2567 { 0x02, 0xdf20 },
2568 { 0x01, 0xff95 },
2569 { 0x00, 0xba00 },
2570 { 0x04, 0xa800 },
2571 { 0x04, 0xf000 },
2572 { 0x03, 0xdf01 },
2573 { 0x02, 0xdf20 },
2574 { 0x01, 0x101a },
2575 { 0x00, 0xa0ff },
2576 { 0x04, 0xf800 },
2577 { 0x04, 0x0000 },
2578 { 0x1f, 0x0000 },
2579
2580 { 0x1f, 0x0001 },
2581 { 0x10, 0xf41b },
2582 { 0x14, 0xfb54 },
2583 { 0x18, 0xf5c7 },
2584 { 0x1f, 0x0000 },
2585
2586 { 0x1f, 0x0001 },
2587 { 0x17, 0x0cc0 },
2588 { 0x1f, 0x0000 }
2589 };
2590
françois romieu4da19632011-01-03 15:07:55 +00002591 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002592
françois romieu4da19632011-01-03 15:07:55 +00002593 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002594}
2595
françois romieu4da19632011-01-03 15:07:55 +00002596static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002597{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002598 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002599 { 0x1f, 0x0001 },
2600 { 0x04, 0x0000 },
2601 { 0x03, 0x00a1 },
2602 { 0x02, 0x0008 },
2603 { 0x01, 0x0120 },
2604 { 0x00, 0x1000 },
2605 { 0x04, 0x0800 },
2606 { 0x04, 0x9000 },
2607 { 0x03, 0x802f },
2608 { 0x02, 0x4f02 },
2609 { 0x01, 0x0409 },
2610 { 0x00, 0xf099 },
2611 { 0x04, 0x9800 },
2612 { 0x04, 0xa000 },
2613 { 0x03, 0xdf01 },
2614 { 0x02, 0xdf20 },
2615 { 0x01, 0xff95 },
2616 { 0x00, 0xba00 },
2617 { 0x04, 0xa800 },
2618 { 0x04, 0xf000 },
2619 { 0x03, 0xdf01 },
2620 { 0x02, 0xdf20 },
2621 { 0x01, 0x101a },
2622 { 0x00, 0xa0ff },
2623 { 0x04, 0xf800 },
2624 { 0x04, 0x0000 },
2625 { 0x1f, 0x0000 },
2626
2627 { 0x1f, 0x0001 },
2628 { 0x0b, 0x8480 },
2629 { 0x1f, 0x0000 },
2630
2631 { 0x1f, 0x0001 },
2632 { 0x18, 0x67c7 },
2633 { 0x04, 0x2000 },
2634 { 0x03, 0x002f },
2635 { 0x02, 0x4360 },
2636 { 0x01, 0x0109 },
2637 { 0x00, 0x3022 },
2638 { 0x04, 0x2800 },
2639 { 0x1f, 0x0000 },
2640
2641 { 0x1f, 0x0001 },
2642 { 0x17, 0x0cc0 },
2643 { 0x1f, 0x0000 }
2644 };
2645
françois romieu4da19632011-01-03 15:07:55 +00002646 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002647}
2648
françois romieu4da19632011-01-03 15:07:55 +00002649static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002650{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002651 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002652 { 0x10, 0xf41b },
2653 { 0x1f, 0x0000 }
2654 };
2655
françois romieu4da19632011-01-03 15:07:55 +00002656 rtl_writephy(tp, 0x1f, 0x0001);
2657 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002658
françois romieu4da19632011-01-03 15:07:55 +00002659 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002660}
2661
françois romieu4da19632011-01-03 15:07:55 +00002662static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002663{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002664 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002665 { 0x1f, 0x0001 },
2666 { 0x10, 0xf41b },
2667 { 0x1f, 0x0000 }
2668 };
2669
françois romieu4da19632011-01-03 15:07:55 +00002670 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002671}
2672
françois romieu4da19632011-01-03 15:07:55 +00002673static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002674{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002675 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002676 { 0x1f, 0x0000 },
2677 { 0x1d, 0x0f00 },
2678 { 0x1f, 0x0002 },
2679 { 0x0c, 0x1ec8 },
2680 { 0x1f, 0x0000 }
2681 };
2682
françois romieu4da19632011-01-03 15:07:55 +00002683 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002684}
2685
françois romieu4da19632011-01-03 15:07:55 +00002686static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002687{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002688 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002689 { 0x1f, 0x0001 },
2690 { 0x1d, 0x3d98 },
2691 { 0x1f, 0x0000 }
2692 };
2693
françois romieu4da19632011-01-03 15:07:55 +00002694 rtl_writephy(tp, 0x1f, 0x0000);
2695 rtl_patchphy(tp, 0x14, 1 << 5);
2696 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002697
françois romieu4da19632011-01-03 15:07:55 +00002698 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002699}
2700
françois romieu4da19632011-01-03 15:07:55 +00002701static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002702{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002703 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002704 { 0x1f, 0x0001 },
2705 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002706 { 0x1f, 0x0002 },
2707 { 0x00, 0x88d4 },
2708 { 0x01, 0x82b1 },
2709 { 0x03, 0x7002 },
2710 { 0x08, 0x9e30 },
2711 { 0x09, 0x01f0 },
2712 { 0x0a, 0x5500 },
2713 { 0x0c, 0x00c8 },
2714 { 0x1f, 0x0003 },
2715 { 0x12, 0xc096 },
2716 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002717 { 0x1f, 0x0000 },
2718 { 0x1f, 0x0000 },
2719 { 0x09, 0x2000 },
2720 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002721 };
2722
françois romieu4da19632011-01-03 15:07:55 +00002723 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002724
françois romieu4da19632011-01-03 15:07:55 +00002725 rtl_patchphy(tp, 0x14, 1 << 5);
2726 rtl_patchphy(tp, 0x0d, 1 << 5);
2727 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002728}
2729
françois romieu4da19632011-01-03 15:07:55 +00002730static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002731{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002732 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002733 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002734 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002735 { 0x03, 0x802f },
2736 { 0x02, 0x4f02 },
2737 { 0x01, 0x0409 },
2738 { 0x00, 0xf099 },
2739 { 0x04, 0x9800 },
2740 { 0x04, 0x9000 },
2741 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002742 { 0x1f, 0x0002 },
2743 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002744 { 0x06, 0x0761 },
2745 { 0x1f, 0x0003 },
2746 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002747 { 0x1f, 0x0000 }
2748 };
2749
françois romieu4da19632011-01-03 15:07:55 +00002750 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002751
françois romieu4da19632011-01-03 15:07:55 +00002752 rtl_patchphy(tp, 0x16, 1 << 0);
2753 rtl_patchphy(tp, 0x14, 1 << 5);
2754 rtl_patchphy(tp, 0x0d, 1 << 5);
2755 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002756}
2757
françois romieu4da19632011-01-03 15:07:55 +00002758static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002759{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002760 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002761 { 0x1f, 0x0001 },
2762 { 0x12, 0x2300 },
2763 { 0x1d, 0x3d98 },
2764 { 0x1f, 0x0002 },
2765 { 0x0c, 0x7eb8 },
2766 { 0x06, 0x5461 },
2767 { 0x1f, 0x0003 },
2768 { 0x16, 0x0f0a },
2769 { 0x1f, 0x0000 }
2770 };
2771
françois romieu4da19632011-01-03 15:07:55 +00002772 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002773
françois romieu4da19632011-01-03 15:07:55 +00002774 rtl_patchphy(tp, 0x16, 1 << 0);
2775 rtl_patchphy(tp, 0x14, 1 << 5);
2776 rtl_patchphy(tp, 0x0d, 1 << 5);
2777 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002778}
2779
françois romieu4da19632011-01-03 15:07:55 +00002780static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002781{
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002783}
2784
françois romieubca03d52011-01-03 15:07:31 +00002785static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002786{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002787 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002788 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002789 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002790 { 0x06, 0x4064 },
2791 { 0x07, 0x2863 },
2792 { 0x08, 0x059c },
2793 { 0x09, 0x26b4 },
2794 { 0x0a, 0x6a19 },
2795 { 0x0b, 0xdcc8 },
2796 { 0x10, 0xf06d },
2797 { 0x14, 0x7f68 },
2798 { 0x18, 0x7fd9 },
2799 { 0x1c, 0xf0ff },
2800 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002801 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002802 { 0x12, 0xf49f },
2803 { 0x13, 0x070b },
2804 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002805 { 0x14, 0x94c0 },
2806
2807 /*
2808 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002809 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002810 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002811 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002812 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002813 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002814 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002815 { 0x06, 0x5561 },
2816
2817 /*
2818 * Can not link to 1Gbps with bad cable
2819 * Decrease SNR threshold form 21.07dB to 19.04dB
2820 */
2821 { 0x1f, 0x0001 },
2822 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002823
2824 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002825 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002826 };
2827
françois romieu4da19632011-01-03 15:07:55 +00002828 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002829
françois romieubca03d52011-01-03 15:07:31 +00002830 /*
2831 * Rx Error Issue
2832 * Fine Tune Switching regulator parameter
2833 */
françois romieu4da19632011-01-03 15:07:55 +00002834 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002835 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2836 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002837
Francois Romieufdf6fc02012-07-06 22:40:38 +02002838 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002839 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002840 { 0x1f, 0x0002 },
2841 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002842 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002843 { 0x05, 0x8330 },
2844 { 0x06, 0x669a },
2845 { 0x1f, 0x0002 }
2846 };
2847 int val;
2848
françois romieu4da19632011-01-03 15:07:55 +00002849 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002850
françois romieu4da19632011-01-03 15:07:55 +00002851 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002852
2853 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002854 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002855 0x0065, 0x0066, 0x0067, 0x0068,
2856 0x0069, 0x006a, 0x006b, 0x006c
2857 };
2858 int i;
2859
françois romieu4da19632011-01-03 15:07:55 +00002860 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002861
2862 val &= 0xff00;
2863 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002864 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002865 }
2866 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002867 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002868 { 0x1f, 0x0002 },
2869 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002870 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002871 { 0x05, 0x8330 },
2872 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002873 };
2874
françois romieu4da19632011-01-03 15:07:55 +00002875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002876 }
2877
françois romieubca03d52011-01-03 15:07:31 +00002878 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002879 rtl_writephy(tp, 0x1f, 0x0002);
2880 rtl_patchphy(tp, 0x0d, 0x0300);
2881 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002882
françois romieubca03d52011-01-03 15:07:31 +00002883 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002884 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002885 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2886 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002887
françois romieu4da19632011-01-03 15:07:55 +00002888 rtl_writephy(tp, 0x1f, 0x0005);
2889 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002890
2891 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002892
françois romieu4da19632011-01-03 15:07:55 +00002893 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002894}
2895
françois romieubca03d52011-01-03 15:07:31 +00002896static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002897{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002898 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002899 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002900 { 0x1f, 0x0001 },
2901 { 0x06, 0x4064 },
2902 { 0x07, 0x2863 },
2903 { 0x08, 0x059c },
2904 { 0x09, 0x26b4 },
2905 { 0x0a, 0x6a19 },
2906 { 0x0b, 0xdcc8 },
2907 { 0x10, 0xf06d },
2908 { 0x14, 0x7f68 },
2909 { 0x18, 0x7fd9 },
2910 { 0x1c, 0xf0ff },
2911 { 0x1d, 0x3d9c },
2912 { 0x1f, 0x0003 },
2913 { 0x12, 0xf49f },
2914 { 0x13, 0x070b },
2915 { 0x1a, 0x05ad },
2916 { 0x14, 0x94c0 },
2917
françois romieubca03d52011-01-03 15:07:31 +00002918 /*
2919 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002920 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002921 */
françois romieudaf9df62009-10-07 12:44:20 +00002922 { 0x1f, 0x0002 },
2923 { 0x06, 0x5561 },
2924 { 0x1f, 0x0005 },
2925 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002926 { 0x06, 0x5561 },
2927
2928 /*
2929 * Can not link to 1Gbps with bad cable
2930 * Decrease SNR threshold form 21.07dB to 19.04dB
2931 */
2932 { 0x1f, 0x0001 },
2933 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002934
2935 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002936 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002937 };
2938
françois romieu4da19632011-01-03 15:07:55 +00002939 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002940
Francois Romieufdf6fc02012-07-06 22:40:38 +02002941 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002942 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002943 { 0x1f, 0x0002 },
2944 { 0x05, 0x669a },
2945 { 0x1f, 0x0005 },
2946 { 0x05, 0x8330 },
2947 { 0x06, 0x669a },
2948
2949 { 0x1f, 0x0002 }
2950 };
2951 int val;
2952
françois romieu4da19632011-01-03 15:07:55 +00002953 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002954
françois romieu4da19632011-01-03 15:07:55 +00002955 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002956 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002957 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002958 0x0065, 0x0066, 0x0067, 0x0068,
2959 0x0069, 0x006a, 0x006b, 0x006c
2960 };
2961 int i;
2962
françois romieu4da19632011-01-03 15:07:55 +00002963 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002964
2965 val &= 0xff00;
2966 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002967 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002968 }
2969 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002970 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002971 { 0x1f, 0x0002 },
2972 { 0x05, 0x2642 },
2973 { 0x1f, 0x0005 },
2974 { 0x05, 0x8330 },
2975 { 0x06, 0x2642 }
2976 };
2977
françois romieu4da19632011-01-03 15:07:55 +00002978 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002979 }
2980
françois romieubca03d52011-01-03 15:07:31 +00002981 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002982 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002983 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2984 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002985
françois romieubca03d52011-01-03 15:07:31 +00002986 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002987 rtl_writephy(tp, 0x1f, 0x0002);
2988 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002989
françois romieu4da19632011-01-03 15:07:55 +00002990 rtl_writephy(tp, 0x1f, 0x0005);
2991 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002992
2993 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002994
françois romieu4da19632011-01-03 15:07:55 +00002995 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002996}
2997
françois romieu4da19632011-01-03 15:07:55 +00002998static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002999{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003000 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003001 { 0x1f, 0x0002 },
3002 { 0x10, 0x0008 },
3003 { 0x0d, 0x006c },
3004
3005 { 0x1f, 0x0000 },
3006 { 0x0d, 0xf880 },
3007
3008 { 0x1f, 0x0001 },
3009 { 0x17, 0x0cc0 },
3010
3011 { 0x1f, 0x0001 },
3012 { 0x0b, 0xa4d8 },
3013 { 0x09, 0x281c },
3014 { 0x07, 0x2883 },
3015 { 0x0a, 0x6b35 },
3016 { 0x1d, 0x3da4 },
3017 { 0x1c, 0xeffd },
3018 { 0x14, 0x7f52 },
3019 { 0x18, 0x7fc6 },
3020 { 0x08, 0x0601 },
3021 { 0x06, 0x4063 },
3022 { 0x10, 0xf074 },
3023 { 0x1f, 0x0003 },
3024 { 0x13, 0x0789 },
3025 { 0x12, 0xf4bd },
3026 { 0x1a, 0x04fd },
3027 { 0x14, 0x84b0 },
3028 { 0x1f, 0x0000 },
3029 { 0x00, 0x9200 },
3030
3031 { 0x1f, 0x0005 },
3032 { 0x01, 0x0340 },
3033 { 0x1f, 0x0001 },
3034 { 0x04, 0x4000 },
3035 { 0x03, 0x1d21 },
3036 { 0x02, 0x0c32 },
3037 { 0x01, 0x0200 },
3038 { 0x00, 0x5554 },
3039 { 0x04, 0x4800 },
3040 { 0x04, 0x4000 },
3041 { 0x04, 0xf000 },
3042 { 0x03, 0xdf01 },
3043 { 0x02, 0xdf20 },
3044 { 0x01, 0x101a },
3045 { 0x00, 0xa0ff },
3046 { 0x04, 0xf800 },
3047 { 0x04, 0xf000 },
3048 { 0x1f, 0x0000 },
3049
3050 { 0x1f, 0x0007 },
3051 { 0x1e, 0x0023 },
3052 { 0x16, 0x0000 },
3053 { 0x1f, 0x0000 }
3054 };
3055
françois romieu4da19632011-01-03 15:07:55 +00003056 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003057}
3058
françois romieue6de30d2011-01-03 15:08:37 +00003059static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3060{
3061 static const struct phy_reg phy_reg_init[] = {
3062 { 0x1f, 0x0001 },
3063 { 0x17, 0x0cc0 },
3064
3065 { 0x1f, 0x0007 },
3066 { 0x1e, 0x002d },
3067 { 0x18, 0x0040 },
3068 { 0x1f, 0x0000 }
3069 };
3070
3071 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3072 rtl_patchphy(tp, 0x0d, 1 << 5);
3073}
3074
Hayes Wang70090422011-07-06 15:58:06 +08003075static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003076{
3077 static const struct phy_reg phy_reg_init[] = {
3078 /* Enable Delay cap */
3079 { 0x1f, 0x0005 },
3080 { 0x05, 0x8b80 },
3081 { 0x06, 0xc896 },
3082 { 0x1f, 0x0000 },
3083
3084 /* Channel estimation fine tune */
3085 { 0x1f, 0x0001 },
3086 { 0x0b, 0x6c20 },
3087 { 0x07, 0x2872 },
3088 { 0x1c, 0xefff },
3089 { 0x1f, 0x0003 },
3090 { 0x14, 0x6420 },
3091 { 0x1f, 0x0000 },
3092
3093 /* Update PFM & 10M TX idle timer */
3094 { 0x1f, 0x0007 },
3095 { 0x1e, 0x002f },
3096 { 0x15, 0x1919 },
3097 { 0x1f, 0x0000 },
3098
3099 { 0x1f, 0x0007 },
3100 { 0x1e, 0x00ac },
3101 { 0x18, 0x0006 },
3102 { 0x1f, 0x0000 }
3103 };
3104
Francois Romieu15ecd032011-04-27 13:52:22 -07003105 rtl_apply_firmware(tp);
3106
hayeswang01dc7fe2011-03-21 01:50:28 +00003107 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3108
3109 /* DCO enable for 10M IDLE Power */
3110 rtl_writephy(tp, 0x1f, 0x0007);
3111 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003112 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003113 rtl_writephy(tp, 0x1f, 0x0000);
3114
3115 /* For impedance matching */
3116 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003117 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003118 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003119
3120 /* PHY auto speed down */
3121 rtl_writephy(tp, 0x1f, 0x0007);
3122 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003123 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003124 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003125 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003126
3127 rtl_writephy(tp, 0x1f, 0x0005);
3128 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003129 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003130 rtl_writephy(tp, 0x1f, 0x0000);
3131
3132 rtl_writephy(tp, 0x1f, 0x0005);
3133 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003134 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003135 rtl_writephy(tp, 0x1f, 0x0007);
3136 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003137 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003138 rtl_writephy(tp, 0x1f, 0x0006);
3139 rtl_writephy(tp, 0x00, 0x5a00);
3140 rtl_writephy(tp, 0x1f, 0x0000);
3141 rtl_writephy(tp, 0x0d, 0x0007);
3142 rtl_writephy(tp, 0x0e, 0x003c);
3143 rtl_writephy(tp, 0x0d, 0x4007);
3144 rtl_writephy(tp, 0x0e, 0x0000);
3145 rtl_writephy(tp, 0x0d, 0x0000);
3146}
3147
françois romieu9ecb9aa2012-12-07 11:20:21 +00003148static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3149{
3150 const u16 w[] = {
3151 addr[0] | (addr[1] << 8),
3152 addr[2] | (addr[3] << 8),
3153 addr[4] | (addr[5] << 8)
3154 };
3155 const struct exgmac_reg e[] = {
3156 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3157 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3158 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3159 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3160 };
3161
3162 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3163}
3164
Hayes Wang70090422011-07-06 15:58:06 +08003165static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3166{
3167 static const struct phy_reg phy_reg_init[] = {
3168 /* Enable Delay cap */
3169 { 0x1f, 0x0004 },
3170 { 0x1f, 0x0007 },
3171 { 0x1e, 0x00ac },
3172 { 0x18, 0x0006 },
3173 { 0x1f, 0x0002 },
3174 { 0x1f, 0x0000 },
3175 { 0x1f, 0x0000 },
3176
3177 /* Channel estimation fine tune */
3178 { 0x1f, 0x0003 },
3179 { 0x09, 0xa20f },
3180 { 0x1f, 0x0000 },
3181 { 0x1f, 0x0000 },
3182
3183 /* Green Setting */
3184 { 0x1f, 0x0005 },
3185 { 0x05, 0x8b5b },
3186 { 0x06, 0x9222 },
3187 { 0x05, 0x8b6d },
3188 { 0x06, 0x8000 },
3189 { 0x05, 0x8b76 },
3190 { 0x06, 0x8000 },
3191 { 0x1f, 0x0000 }
3192 };
3193
3194 rtl_apply_firmware(tp);
3195
3196 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3197
3198 /* For 4-corner performance improve */
3199 rtl_writephy(tp, 0x1f, 0x0005);
3200 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003201 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003202 rtl_writephy(tp, 0x1f, 0x0000);
3203
3204 /* PHY auto speed down */
3205 rtl_writephy(tp, 0x1f, 0x0004);
3206 rtl_writephy(tp, 0x1f, 0x0007);
3207 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003208 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003209 rtl_writephy(tp, 0x1f, 0x0002);
3210 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003211 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003212
3213 /* improve 10M EEE waveform */
3214 rtl_writephy(tp, 0x1f, 0x0005);
3215 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003216 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003217 rtl_writephy(tp, 0x1f, 0x0000);
3218
3219 /* Improve 2-pair detection performance */
3220 rtl_writephy(tp, 0x1f, 0x0005);
3221 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003222 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003223 rtl_writephy(tp, 0x1f, 0x0000);
3224
3225 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003226 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003227 rtl_writephy(tp, 0x1f, 0x0005);
3228 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003229 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003230 rtl_writephy(tp, 0x1f, 0x0004);
3231 rtl_writephy(tp, 0x1f, 0x0007);
3232 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003233 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003234 rtl_writephy(tp, 0x1f, 0x0002);
3235 rtl_writephy(tp, 0x1f, 0x0000);
3236 rtl_writephy(tp, 0x0d, 0x0007);
3237 rtl_writephy(tp, 0x0e, 0x003c);
3238 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003239 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003240 rtl_writephy(tp, 0x0d, 0x0000);
3241
3242 /* Green feature */
3243 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003244 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3245 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003246 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003247 rtl_writephy(tp, 0x1f, 0x0005);
3248 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3249 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003250
françois romieu9ecb9aa2012-12-07 11:20:21 +00003251 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3252 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003253}
3254
Hayes Wang5f886e02012-03-30 14:33:03 +08003255static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3256{
3257 /* For 4-corner performance improve */
3258 rtl_writephy(tp, 0x1f, 0x0005);
3259 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003260 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003261 rtl_writephy(tp, 0x1f, 0x0000);
3262
3263 /* PHY auto speed down */
3264 rtl_writephy(tp, 0x1f, 0x0007);
3265 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003266 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003267 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003268 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003269
3270 /* Improve 10M EEE waveform */
3271 rtl_writephy(tp, 0x1f, 0x0005);
3272 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003273 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003274 rtl_writephy(tp, 0x1f, 0x0000);
3275}
3276
Hayes Wangc2218922011-09-06 16:55:18 +08003277static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3278{
3279 static const struct phy_reg phy_reg_init[] = {
3280 /* Channel estimation fine tune */
3281 { 0x1f, 0x0003 },
3282 { 0x09, 0xa20f },
3283 { 0x1f, 0x0000 },
3284
3285 /* Modify green table for giga & fnet */
3286 { 0x1f, 0x0005 },
3287 { 0x05, 0x8b55 },
3288 { 0x06, 0x0000 },
3289 { 0x05, 0x8b5e },
3290 { 0x06, 0x0000 },
3291 { 0x05, 0x8b67 },
3292 { 0x06, 0x0000 },
3293 { 0x05, 0x8b70 },
3294 { 0x06, 0x0000 },
3295 { 0x1f, 0x0000 },
3296 { 0x1f, 0x0007 },
3297 { 0x1e, 0x0078 },
3298 { 0x17, 0x0000 },
3299 { 0x19, 0x00fb },
3300 { 0x1f, 0x0000 },
3301
3302 /* Modify green table for 10M */
3303 { 0x1f, 0x0005 },
3304 { 0x05, 0x8b79 },
3305 { 0x06, 0xaa00 },
3306 { 0x1f, 0x0000 },
3307
3308 /* Disable hiimpedance detection (RTCT) */
3309 { 0x1f, 0x0003 },
3310 { 0x01, 0x328a },
3311 { 0x1f, 0x0000 }
3312 };
3313
3314 rtl_apply_firmware(tp);
3315
3316 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3317
Hayes Wang5f886e02012-03-30 14:33:03 +08003318 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003319
3320 /* Improve 2-pair detection performance */
3321 rtl_writephy(tp, 0x1f, 0x0005);
3322 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003323 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003324 rtl_writephy(tp, 0x1f, 0x0000);
3325}
3326
3327static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3328{
3329 rtl_apply_firmware(tp);
3330
Hayes Wang5f886e02012-03-30 14:33:03 +08003331 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003332}
3333
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003334static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3335{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003336 static const struct phy_reg phy_reg_init[] = {
3337 /* Channel estimation fine tune */
3338 { 0x1f, 0x0003 },
3339 { 0x09, 0xa20f },
3340 { 0x1f, 0x0000 },
3341
3342 /* Modify green table for giga & fnet */
3343 { 0x1f, 0x0005 },
3344 { 0x05, 0x8b55 },
3345 { 0x06, 0x0000 },
3346 { 0x05, 0x8b5e },
3347 { 0x06, 0x0000 },
3348 { 0x05, 0x8b67 },
3349 { 0x06, 0x0000 },
3350 { 0x05, 0x8b70 },
3351 { 0x06, 0x0000 },
3352 { 0x1f, 0x0000 },
3353 { 0x1f, 0x0007 },
3354 { 0x1e, 0x0078 },
3355 { 0x17, 0x0000 },
3356 { 0x19, 0x00aa },
3357 { 0x1f, 0x0000 },
3358
3359 /* Modify green table for 10M */
3360 { 0x1f, 0x0005 },
3361 { 0x05, 0x8b79 },
3362 { 0x06, 0xaa00 },
3363 { 0x1f, 0x0000 },
3364
3365 /* Disable hiimpedance detection (RTCT) */
3366 { 0x1f, 0x0003 },
3367 { 0x01, 0x328a },
3368 { 0x1f, 0x0000 }
3369 };
3370
3371
3372 rtl_apply_firmware(tp);
3373
3374 rtl8168f_hw_phy_config(tp);
3375
3376 /* Improve 2-pair detection performance */
3377 rtl_writephy(tp, 0x1f, 0x0005);
3378 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003379 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003380 rtl_writephy(tp, 0x1f, 0x0000);
3381
3382 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3383
3384 /* Modify green table for giga */
3385 rtl_writephy(tp, 0x1f, 0x0005);
3386 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003387 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003388 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003389 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003390 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003391 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003392 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003393 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003394 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003395 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003396 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003397 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003398 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003399 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003400 rtl_writephy(tp, 0x1f, 0x0000);
3401
3402 /* uc same-seed solution */
3403 rtl_writephy(tp, 0x1f, 0x0005);
3404 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003405 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003406 rtl_writephy(tp, 0x1f, 0x0000);
3407
3408 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003409 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003410 rtl_writephy(tp, 0x1f, 0x0005);
3411 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003412 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003413 rtl_writephy(tp, 0x1f, 0x0004);
3414 rtl_writephy(tp, 0x1f, 0x0007);
3415 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003416 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003417 rtl_writephy(tp, 0x1f, 0x0000);
3418 rtl_writephy(tp, 0x0d, 0x0007);
3419 rtl_writephy(tp, 0x0e, 0x003c);
3420 rtl_writephy(tp, 0x0d, 0x4007);
3421 rtl_writephy(tp, 0x0e, 0x0000);
3422 rtl_writephy(tp, 0x0d, 0x0000);
3423
3424 /* Green feature */
3425 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003426 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3427 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003428 rtl_writephy(tp, 0x1f, 0x0000);
3429}
3430
Hayes Wangc5583862012-07-02 17:23:22 +08003431static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3432{
Hayes Wangc5583862012-07-02 17:23:22 +08003433 rtl_apply_firmware(tp);
3434
hayeswang41f44d12013-04-01 22:23:36 +00003435 rtl_writephy(tp, 0x1f, 0x0a46);
3436 if (rtl_readphy(tp, 0x10) & 0x0100) {
3437 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003438 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003439 } else {
3440 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003441 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003442 }
Hayes Wangc5583862012-07-02 17:23:22 +08003443
hayeswang41f44d12013-04-01 22:23:36 +00003444 rtl_writephy(tp, 0x1f, 0x0a46);
3445 if (rtl_readphy(tp, 0x13) & 0x0100) {
3446 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003447 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003448 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003449 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003450 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003451 }
Hayes Wangc5583862012-07-02 17:23:22 +08003452
hayeswang41f44d12013-04-01 22:23:36 +00003453 /* Enable PHY auto speed down */
3454 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003455 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003456
hayeswangfe7524c2013-04-01 22:23:37 +00003457 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003458 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003459 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003460 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003461 rtl_writephy(tp, 0x1f, 0x0a43);
3462 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003463 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3464 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003465
hayeswang41f44d12013-04-01 22:23:36 +00003466 /* EEE auto-fallback function */
3467 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003468 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003469
hayeswang41f44d12013-04-01 22:23:36 +00003470 /* Enable UC LPF tune function */
3471 rtl_writephy(tp, 0x1f, 0x0a43);
3472 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003473 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003474
3475 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003476 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003477
hayeswangfe7524c2013-04-01 22:23:37 +00003478 /* Improve SWR Efficiency */
3479 rtl_writephy(tp, 0x1f, 0x0bcd);
3480 rtl_writephy(tp, 0x14, 0x5065);
3481 rtl_writephy(tp, 0x14, 0xd065);
3482 rtl_writephy(tp, 0x1f, 0x0bc8);
3483 rtl_writephy(tp, 0x11, 0x5655);
3484 rtl_writephy(tp, 0x1f, 0x0bcd);
3485 rtl_writephy(tp, 0x14, 0x1065);
3486 rtl_writephy(tp, 0x14, 0x9065);
3487 rtl_writephy(tp, 0x14, 0x1065);
3488
David Chang1bac1072013-11-27 15:48:36 +08003489 /* Check ALDPS bit, disable it if enabled */
3490 rtl_writephy(tp, 0x1f, 0x0a43);
3491 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003492 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003493
hayeswang41f44d12013-04-01 22:23:36 +00003494 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003495}
3496
hayeswang57538c42013-04-01 22:23:40 +00003497static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3498{
3499 rtl_apply_firmware(tp);
3500}
3501
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003502static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3503{
3504 u16 dout_tapbin;
3505 u32 data;
3506
3507 rtl_apply_firmware(tp);
3508
3509 /* CHN EST parameters adjust - giga master */
3510 rtl_writephy(tp, 0x1f, 0x0a43);
3511 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003512 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003513 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003514 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003515 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003516 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003517 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003518 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003519 rtl_writephy(tp, 0x1f, 0x0000);
3520
3521 /* CHN EST parameters adjust - giga slave */
3522 rtl_writephy(tp, 0x1f, 0x0a43);
3523 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003524 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003525 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003526 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003527 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003528 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003529 rtl_writephy(tp, 0x1f, 0x0000);
3530
3531 /* CHN EST parameters adjust - fnet */
3532 rtl_writephy(tp, 0x1f, 0x0a43);
3533 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003534 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003535 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003536 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003537 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003538 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003539 rtl_writephy(tp, 0x1f, 0x0000);
3540
3541 /* enable R-tune & PGA-retune function */
3542 dout_tapbin = 0;
3543 rtl_writephy(tp, 0x1f, 0x0a46);
3544 data = rtl_readphy(tp, 0x13);
3545 data &= 3;
3546 data <<= 2;
3547 dout_tapbin |= data;
3548 data = rtl_readphy(tp, 0x12);
3549 data &= 0xc000;
3550 data >>= 14;
3551 dout_tapbin |= data;
3552 dout_tapbin = ~(dout_tapbin^0x08);
3553 dout_tapbin <<= 12;
3554 dout_tapbin &= 0xf000;
3555 rtl_writephy(tp, 0x1f, 0x0a43);
3556 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003557 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003558 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003559 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003560 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003561 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003562 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003563 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003564
3565 rtl_writephy(tp, 0x1f, 0x0a43);
3566 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003567 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003568 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003569 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003570 rtl_writephy(tp, 0x1f, 0x0000);
3571
3572 /* enable GPHY 10M */
3573 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003574 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003575 rtl_writephy(tp, 0x1f, 0x0000);
3576
3577 /* SAR ADC performance */
3578 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003579 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003580 rtl_writephy(tp, 0x1f, 0x0000);
3581
3582 rtl_writephy(tp, 0x1f, 0x0a43);
3583 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003584 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003585 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003586 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003587 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003588 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003589 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003590 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003591 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003592 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003593 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003594 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003595 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003596 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003597 rtl_writephy(tp, 0x1f, 0x0000);
3598
3599 /* disable phy pfm mode */
3600 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003601 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003602 rtl_writephy(tp, 0x1f, 0x0000);
3603
3604 /* Check ALDPS bit, disable it if enabled */
3605 rtl_writephy(tp, 0x1f, 0x0a43);
3606 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003607 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003608
3609 rtl_writephy(tp, 0x1f, 0x0000);
3610}
3611
3612static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3613{
3614 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3615 u16 rlen;
3616 u32 data;
3617
3618 rtl_apply_firmware(tp);
3619
3620 /* CHIN EST parameter update */
3621 rtl_writephy(tp, 0x1f, 0x0a43);
3622 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003623 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003624 rtl_writephy(tp, 0x1f, 0x0000);
3625
3626 /* enable R-tune & PGA-retune function */
3627 rtl_writephy(tp, 0x1f, 0x0a43);
3628 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003629 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003630 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003631 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003632 rtl_writephy(tp, 0x1f, 0x0000);
3633
3634 /* enable GPHY 10M */
3635 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003636 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003637 rtl_writephy(tp, 0x1f, 0x0000);
3638
3639 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3640 data = r8168_mac_ocp_read(tp, 0xdd02);
3641 ioffset_p3 = ((data & 0x80)>>7);
3642 ioffset_p3 <<= 3;
3643
3644 data = r8168_mac_ocp_read(tp, 0xdd00);
3645 ioffset_p3 |= ((data & (0xe000))>>13);
3646 ioffset_p2 = ((data & (0x1e00))>>9);
3647 ioffset_p1 = ((data & (0x01e0))>>5);
3648 ioffset_p0 = ((data & 0x0010)>>4);
3649 ioffset_p0 <<= 3;
3650 ioffset_p0 |= (data & (0x07));
3651 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3652
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003653 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003654 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003655 rtl_writephy(tp, 0x1f, 0x0bcf);
3656 rtl_writephy(tp, 0x16, data);
3657 rtl_writephy(tp, 0x1f, 0x0000);
3658 }
3659
3660 /* Modify rlen (TX LPF corner frequency) level */
3661 rtl_writephy(tp, 0x1f, 0x0bcd);
3662 data = rtl_readphy(tp, 0x16);
3663 data &= 0x000f;
3664 rlen = 0;
3665 if (data > 3)
3666 rlen = data - 3;
3667 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3668 rtl_writephy(tp, 0x17, data);
3669 rtl_writephy(tp, 0x1f, 0x0bcd);
3670 rtl_writephy(tp, 0x1f, 0x0000);
3671
3672 /* disable phy pfm mode */
3673 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003674 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003675 rtl_writephy(tp, 0x1f, 0x0000);
3676
3677 /* Check ALDPS bit, disable it if enabled */
3678 rtl_writephy(tp, 0x1f, 0x0a43);
3679 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003680 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003681
3682 rtl_writephy(tp, 0x1f, 0x0000);
3683}
3684
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003685static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3686{
3687 /* Enable PHY auto speed down */
3688 rtl_writephy(tp, 0x1f, 0x0a44);
3689 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3690 rtl_writephy(tp, 0x1f, 0x0000);
3691
3692 /* patch 10M & ALDPS */
3693 rtl_writephy(tp, 0x1f, 0x0bcc);
3694 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3695 rtl_writephy(tp, 0x1f, 0x0a44);
3696 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3697 rtl_writephy(tp, 0x1f, 0x0a43);
3698 rtl_writephy(tp, 0x13, 0x8084);
3699 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3700 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3701 rtl_writephy(tp, 0x1f, 0x0000);
3702
3703 /* Enable EEE auto-fallback function */
3704 rtl_writephy(tp, 0x1f, 0x0a4b);
3705 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3706 rtl_writephy(tp, 0x1f, 0x0000);
3707
3708 /* Enable UC LPF tune function */
3709 rtl_writephy(tp, 0x1f, 0x0a43);
3710 rtl_writephy(tp, 0x13, 0x8012);
3711 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3712 rtl_writephy(tp, 0x1f, 0x0000);
3713
3714 /* set rg_sel_sdm_rate */
3715 rtl_writephy(tp, 0x1f, 0x0c42);
3716 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3717 rtl_writephy(tp, 0x1f, 0x0000);
3718
3719 /* Check ALDPS bit, disable it if enabled */
3720 rtl_writephy(tp, 0x1f, 0x0a43);
3721 if (rtl_readphy(tp, 0x10) & 0x0004)
3722 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3723
3724 rtl_writephy(tp, 0x1f, 0x0000);
3725}
3726
3727static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3728{
3729 /* patch 10M & ALDPS */
3730 rtl_writephy(tp, 0x1f, 0x0bcc);
3731 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3732 rtl_writephy(tp, 0x1f, 0x0a44);
3733 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3734 rtl_writephy(tp, 0x1f, 0x0a43);
3735 rtl_writephy(tp, 0x13, 0x8084);
3736 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3737 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3738 rtl_writephy(tp, 0x1f, 0x0000);
3739
3740 /* Enable UC LPF tune function */
3741 rtl_writephy(tp, 0x1f, 0x0a43);
3742 rtl_writephy(tp, 0x13, 0x8012);
3743 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3744 rtl_writephy(tp, 0x1f, 0x0000);
3745
3746 /* Set rg_sel_sdm_rate */
3747 rtl_writephy(tp, 0x1f, 0x0c42);
3748 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3749 rtl_writephy(tp, 0x1f, 0x0000);
3750
3751 /* Channel estimation parameters */
3752 rtl_writephy(tp, 0x1f, 0x0a43);
3753 rtl_writephy(tp, 0x13, 0x80f3);
3754 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3755 rtl_writephy(tp, 0x13, 0x80f0);
3756 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3757 rtl_writephy(tp, 0x13, 0x80ef);
3758 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3759 rtl_writephy(tp, 0x13, 0x80f6);
3760 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3761 rtl_writephy(tp, 0x13, 0x80ec);
3762 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3763 rtl_writephy(tp, 0x13, 0x80ed);
3764 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3765 rtl_writephy(tp, 0x13, 0x80f2);
3766 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3767 rtl_writephy(tp, 0x13, 0x80f4);
3768 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3769 rtl_writephy(tp, 0x1f, 0x0a43);
3770 rtl_writephy(tp, 0x13, 0x8110);
3771 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3772 rtl_writephy(tp, 0x13, 0x810f);
3773 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3774 rtl_writephy(tp, 0x13, 0x8111);
3775 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3776 rtl_writephy(tp, 0x13, 0x8113);
3777 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3778 rtl_writephy(tp, 0x13, 0x8115);
3779 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3780 rtl_writephy(tp, 0x13, 0x810e);
3781 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3782 rtl_writephy(tp, 0x13, 0x810c);
3783 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3784 rtl_writephy(tp, 0x13, 0x810b);
3785 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3786 rtl_writephy(tp, 0x1f, 0x0a43);
3787 rtl_writephy(tp, 0x13, 0x80d1);
3788 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3789 rtl_writephy(tp, 0x13, 0x80cd);
3790 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3791 rtl_writephy(tp, 0x13, 0x80d3);
3792 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3793 rtl_writephy(tp, 0x13, 0x80d5);
3794 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3795 rtl_writephy(tp, 0x13, 0x80d7);
3796 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3797
3798 /* Force PWM-mode */
3799 rtl_writephy(tp, 0x1f, 0x0bcd);
3800 rtl_writephy(tp, 0x14, 0x5065);
3801 rtl_writephy(tp, 0x14, 0xd065);
3802 rtl_writephy(tp, 0x1f, 0x0bc8);
3803 rtl_writephy(tp, 0x12, 0x00ed);
3804 rtl_writephy(tp, 0x1f, 0x0bcd);
3805 rtl_writephy(tp, 0x14, 0x1065);
3806 rtl_writephy(tp, 0x14, 0x9065);
3807 rtl_writephy(tp, 0x14, 0x1065);
3808 rtl_writephy(tp, 0x1f, 0x0000);
3809
3810 /* Check ALDPS bit, disable it if enabled */
3811 rtl_writephy(tp, 0x1f, 0x0a43);
3812 if (rtl_readphy(tp, 0x10) & 0x0004)
3813 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3814
3815 rtl_writephy(tp, 0x1f, 0x0000);
3816}
3817
françois romieu4da19632011-01-03 15:07:55 +00003818static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003819{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003820 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003821 { 0x1f, 0x0003 },
3822 { 0x08, 0x441d },
3823 { 0x01, 0x9100 },
3824 { 0x1f, 0x0000 }
3825 };
3826
françois romieu4da19632011-01-03 15:07:55 +00003827 rtl_writephy(tp, 0x1f, 0x0000);
3828 rtl_patchphy(tp, 0x11, 1 << 12);
3829 rtl_patchphy(tp, 0x19, 1 << 13);
3830 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003831
françois romieu4da19632011-01-03 15:07:55 +00003832 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003833}
3834
Hayes Wang5a5e4442011-02-22 17:26:21 +08003835static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3836{
3837 static const struct phy_reg phy_reg_init[] = {
3838 { 0x1f, 0x0005 },
3839 { 0x1a, 0x0000 },
3840 { 0x1f, 0x0000 },
3841
3842 { 0x1f, 0x0004 },
3843 { 0x1c, 0x0000 },
3844 { 0x1f, 0x0000 },
3845
3846 { 0x1f, 0x0001 },
3847 { 0x15, 0x7701 },
3848 { 0x1f, 0x0000 }
3849 };
3850
3851 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003852 rtl_writephy(tp, 0x1f, 0x0000);
3853 rtl_writephy(tp, 0x18, 0x0310);
3854 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003855
François Romieu953a12c2011-04-24 17:38:48 +02003856 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003857
3858 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3859}
3860
Hayes Wang7e18dca2012-03-30 14:33:02 +08003861static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3862{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003863 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003864 rtl_writephy(tp, 0x1f, 0x0000);
3865 rtl_writephy(tp, 0x18, 0x0310);
3866 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003867
3868 rtl_apply_firmware(tp);
3869
3870 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003871 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003872 rtl_writephy(tp, 0x1f, 0x0004);
3873 rtl_writephy(tp, 0x10, 0x401f);
3874 rtl_writephy(tp, 0x19, 0x7030);
3875 rtl_writephy(tp, 0x1f, 0x0000);
3876}
3877
Hayes Wang5598bfe2012-07-02 17:23:21 +08003878static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3879{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003880 static const struct phy_reg phy_reg_init[] = {
3881 { 0x1f, 0x0004 },
3882 { 0x10, 0xc07f },
3883 { 0x19, 0x7030 },
3884 { 0x1f, 0x0000 }
3885 };
3886
3887 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003888 rtl_writephy(tp, 0x1f, 0x0000);
3889 rtl_writephy(tp, 0x18, 0x0310);
3890 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003891
3892 rtl_apply_firmware(tp);
3893
Francois Romieufdf6fc02012-07-06 22:40:38 +02003894 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003895 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3896
Francois Romieufdf6fc02012-07-06 22:40:38 +02003897 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003898}
3899
Francois Romieu5615d9f2007-08-17 17:50:46 +02003900static void rtl_hw_phy_config(struct net_device *dev)
3901{
3902 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003903
3904 rtl8169_print_mac_version(tp);
3905
3906 switch (tp->mac_version) {
3907 case RTL_GIGA_MAC_VER_01:
3908 break;
3909 case RTL_GIGA_MAC_VER_02:
3910 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003911 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003912 break;
3913 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003914 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003915 break;
françois romieu2e9558562009-08-10 19:44:19 +00003916 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003917 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003918 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003919 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003920 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003921 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003922 case RTL_GIGA_MAC_VER_07:
3923 case RTL_GIGA_MAC_VER_08:
3924 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003925 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003926 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003927 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003928 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003929 break;
3930 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003931 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003932 break;
3933 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003934 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003935 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003936 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003937 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003938 break;
3939 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003940 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003941 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003942 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003943 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003944 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003945 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003946 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003947 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003948 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003949 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003950 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003951 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003952 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003953 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003954 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003955 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003956 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003957 break;
3958 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003959 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003960 break;
3961 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003962 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003963 break;
françois romieue6de30d2011-01-03 15:08:37 +00003964 case RTL_GIGA_MAC_VER_28:
3965 rtl8168d_4_hw_phy_config(tp);
3966 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003967 case RTL_GIGA_MAC_VER_29:
3968 case RTL_GIGA_MAC_VER_30:
3969 rtl8105e_hw_phy_config(tp);
3970 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003971 case RTL_GIGA_MAC_VER_31:
3972 /* None. */
3973 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003974 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003975 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003976 rtl8168e_1_hw_phy_config(tp);
3977 break;
3978 case RTL_GIGA_MAC_VER_34:
3979 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003980 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003981 case RTL_GIGA_MAC_VER_35:
3982 rtl8168f_1_hw_phy_config(tp);
3983 break;
3984 case RTL_GIGA_MAC_VER_36:
3985 rtl8168f_2_hw_phy_config(tp);
3986 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003987
Hayes Wang7e18dca2012-03-30 14:33:02 +08003988 case RTL_GIGA_MAC_VER_37:
3989 rtl8402_hw_phy_config(tp);
3990 break;
3991
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003992 case RTL_GIGA_MAC_VER_38:
3993 rtl8411_hw_phy_config(tp);
3994 break;
3995
Hayes Wang5598bfe2012-07-02 17:23:21 +08003996 case RTL_GIGA_MAC_VER_39:
3997 rtl8106e_hw_phy_config(tp);
3998 break;
3999
Hayes Wangc5583862012-07-02 17:23:22 +08004000 case RTL_GIGA_MAC_VER_40:
4001 rtl8168g_1_hw_phy_config(tp);
4002 break;
hayeswang57538c42013-04-01 22:23:40 +00004003 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004004 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004005 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004006 rtl8168g_2_hw_phy_config(tp);
4007 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004008 case RTL_GIGA_MAC_VER_45:
4009 case RTL_GIGA_MAC_VER_47:
4010 rtl8168h_1_hw_phy_config(tp);
4011 break;
4012 case RTL_GIGA_MAC_VER_46:
4013 case RTL_GIGA_MAC_VER_48:
4014 rtl8168h_2_hw_phy_config(tp);
4015 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004016
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004017 case RTL_GIGA_MAC_VER_49:
4018 rtl8168ep_1_hw_phy_config(tp);
4019 break;
4020 case RTL_GIGA_MAC_VER_50:
4021 case RTL_GIGA_MAC_VER_51:
4022 rtl8168ep_2_hw_phy_config(tp);
4023 break;
4024
Hayes Wangc5583862012-07-02 17:23:22 +08004025 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004026 default:
4027 break;
4028 }
4029}
4030
Francois Romieuda78dbf2012-01-26 14:18:23 +01004031static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4032{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004033 if (!test_and_set_bit(flag, tp->wk.flags))
4034 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004035}
4036
David S. Miller8decf862011-09-22 03:23:13 -04004037static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4038{
David S. Miller8decf862011-09-22 03:23:13 -04004039 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Heiner Kallweite3972862018-06-29 08:07:04 +02004040 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004041}
4042
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004043static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004045 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004046
Marcus Sundberg773328942008-07-10 21:28:08 +02004047 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004048 netif_dbg(tp, drv, dev,
4049 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004050 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004051 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004052
Francois Romieu6dccd162007-02-13 23:38:05 +01004053 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4054
4055 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4056 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004057
Francois Romieubcf0bf92006-07-26 23:14:13 +02004058 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004059 netif_dbg(tp, drv, dev,
4060 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004061 RTL_W8(tp, 0x82, 0x01);
Heiner Kallweit49d17512018-06-28 20:36:15 +02004062 netif_dbg(tp, drv, dev,
4063 "Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004064 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004065 }
4066
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02004067 /* We may have called phy_speed_down before */
4068 phy_speed_up(dev->phydev);
4069
Heiner Kallweitf75222b2018-07-17 22:51:41 +02004070 genphy_soft_reset(dev->phydev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004071}
4072
Francois Romieu773d2022007-01-31 23:47:43 +01004073static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4074{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004075 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004076
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004077 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004078
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004079 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4080 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004081
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004082 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4083 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004084
françois romieu9ecb9aa2012-12-07 11:20:21 +00004085 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4086 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004087
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004088 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004089
Francois Romieuda78dbf2012-01-26 14:18:23 +01004090 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004091}
4092
4093static int rtl_set_mac_address(struct net_device *dev, void *p)
4094{
4095 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004096 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004097 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004098
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004099 ret = eth_mac_addr(dev, p);
4100 if (ret)
4101 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004102
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004103 pm_runtime_get_noresume(d);
4104
4105 if (pm_runtime_active(d))
4106 rtl_rar_set(tp, dev->dev_addr);
4107
4108 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004109
4110 return 0;
4111}
4112
Heiner Kallweite3972862018-06-29 08:07:04 +02004113static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004114{
Heiner Kallweit69b3c592018-07-17 22:51:53 +02004115 if (!netif_running(dev))
4116 return -ENODEV;
Heiner Kallweite3972862018-06-29 08:07:04 +02004117
Heiner Kallweit69b3c592018-07-17 22:51:53 +02004118 return phy_mii_ioctl(dev->phydev, ifr, cmd);
Francois Romieu8b4ab282008-11-19 22:05:25 -08004119}
4120
Bill Pembertonbaf63292012-12-03 09:23:28 -05004121static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004122{
4123 struct mdio_ops *ops = &tp->mdio_ops;
4124
4125 switch (tp->mac_version) {
4126 case RTL_GIGA_MAC_VER_27:
4127 ops->write = r8168dp_1_mdio_write;
4128 ops->read = r8168dp_1_mdio_read;
4129 break;
françois romieue6de30d2011-01-03 15:08:37 +00004130 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004131 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004132 ops->write = r8168dp_2_mdio_write;
4133 ops->read = r8168dp_2_mdio_read;
4134 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004135 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004136 ops->write = r8168g_mdio_write;
4137 ops->read = r8168g_mdio_read;
4138 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004139 default:
4140 ops->write = r8169_mdio_write;
4141 ops->read = r8169_mdio_read;
4142 break;
4143 }
4144}
4145
David S. Miller1805b2f2011-10-24 18:18:09 -04004146static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4147{
David S. Miller1805b2f2011-10-24 18:18:09 -04004148 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004149 case RTL_GIGA_MAC_VER_25:
4150 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004151 case RTL_GIGA_MAC_VER_29:
4152 case RTL_GIGA_MAC_VER_30:
4153 case RTL_GIGA_MAC_VER_32:
4154 case RTL_GIGA_MAC_VER_33:
4155 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004156 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004157 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004158 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4159 break;
4160 default:
4161 break;
4162 }
4163}
4164
4165static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4166{
Heiner Kallweit6fcf9b12018-07-04 21:11:29 +02004167 if (!netif_running(tp->dev) || !__rtl8169_get_wol(tp))
David S. Miller1805b2f2011-10-24 18:18:09 -04004168 return false;
4169
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02004170 phy_speed_down(tp->dev->phydev, false);
David S. Miller1805b2f2011-10-24 18:18:09 -04004171 rtl_wol_suspend_quirk(tp);
4172
4173 return true;
4174}
4175
françois romieu065c27c2011-01-03 15:08:12 +00004176static void r8168_pll_power_down(struct rtl8169_private *tp)
4177{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004178 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004179 return;
4180
hayeswang01dc7fe2011-03-21 01:50:28 +00004181 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4182 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004183 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004184
David S. Miller1805b2f2011-10-24 18:18:09 -04004185 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004186 return;
françois romieu065c27c2011-01-03 15:08:12 +00004187
françois romieu065c27c2011-01-03 15:08:12 +00004188 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004189 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004190 case RTL_GIGA_MAC_VER_37:
4191 case RTL_GIGA_MAC_VER_39:
4192 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004193 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004194 case RTL_GIGA_MAC_VER_45:
4195 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004196 case RTL_GIGA_MAC_VER_47:
4197 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004198 case RTL_GIGA_MAC_VER_50:
4199 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004200 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004201 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004202 case RTL_GIGA_MAC_VER_40:
4203 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004204 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004205 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004206 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004207 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004208 break;
françois romieu065c27c2011-01-03 15:08:12 +00004209 }
4210}
4211
4212static void r8168_pll_power_up(struct rtl8169_private *tp)
4213{
françois romieu065c27c2011-01-03 15:08:12 +00004214 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004215 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004216 case RTL_GIGA_MAC_VER_37:
4217 case RTL_GIGA_MAC_VER_39:
4218 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004219 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004220 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004221 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004222 case RTL_GIGA_MAC_VER_45:
4223 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004224 case RTL_GIGA_MAC_VER_47:
4225 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004226 case RTL_GIGA_MAC_VER_50:
4227 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004228 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004229 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004230 case RTL_GIGA_MAC_VER_40:
4231 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004232 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004233 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004234 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004235 0x00000000, ERIAR_EXGMAC);
4236 break;
françois romieu065c27c2011-01-03 15:08:12 +00004237 }
4238
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02004239 phy_resume(tp->dev->phydev);
4240 /* give MAC/PHY some time to resume */
4241 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00004242}
4243
françois romieu065c27c2011-01-03 15:08:12 +00004244static void rtl_pll_power_down(struct rtl8169_private *tp)
4245{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004246 switch (tp->mac_version) {
4247 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4248 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4249 break;
4250 default:
4251 r8168_pll_power_down(tp);
4252 }
françois romieu065c27c2011-01-03 15:08:12 +00004253}
4254
4255static void rtl_pll_power_up(struct rtl8169_private *tp)
4256{
françois romieu065c27c2011-01-03 15:08:12 +00004257 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004258 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4259 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004260 break;
françois romieu065c27c2011-01-03 15:08:12 +00004261 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004262 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004263 }
4264}
4265
Hayes Wange542a222011-07-06 15:58:04 +08004266static void rtl_init_rxcfg(struct rtl8169_private *tp)
4267{
Hayes Wange542a222011-07-06 15:58:04 +08004268 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004269 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4270 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004271 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004272 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004273 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004274 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004275 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004276 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004277 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004278 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004279 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004280 break;
Hayes Wange542a222011-07-06 15:58:04 +08004281 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004282 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004283 break;
4284 }
4285}
4286
Hayes Wang92fc43b2011-07-06 15:58:03 +08004287static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4288{
Timo Teräs9fba0812013-01-15 21:01:24 +00004289 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004290}
4291
Francois Romieud58d46b2011-05-03 16:38:29 +02004292static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4293{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004294 if (tp->jumbo_ops.enable) {
4295 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4296 tp->jumbo_ops.enable(tp);
4297 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4298 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004299}
4300
4301static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4302{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004303 if (tp->jumbo_ops.disable) {
4304 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4305 tp->jumbo_ops.disable(tp);
4306 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4307 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004308}
4309
4310static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4311{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004312 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4313 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004314 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004315}
4316
4317static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4318{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004319 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4320 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004321 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004322}
4323
4324static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4325{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004326 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004327}
4328
4329static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4330{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004331 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004332}
4333
4334static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4335{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004336 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4337 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4338 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004339 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004340}
4341
4342static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4343{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004344 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4345 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4346 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004347 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004348}
4349
4350static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4351{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004352 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004353 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004354}
4355
4356static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4357{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004358 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004359 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004360}
4361
4362static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4363{
Francois Romieud58d46b2011-05-03 16:38:29 +02004364 r8168b_0_hw_jumbo_enable(tp);
4365
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004366 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004367}
4368
4369static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4370{
Francois Romieud58d46b2011-05-03 16:38:29 +02004371 r8168b_0_hw_jumbo_disable(tp);
4372
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004373 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004374}
4375
Bill Pembertonbaf63292012-12-03 09:23:28 -05004376static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004377{
4378 struct jumbo_ops *ops = &tp->jumbo_ops;
4379
4380 switch (tp->mac_version) {
4381 case RTL_GIGA_MAC_VER_11:
4382 ops->disable = r8168b_0_hw_jumbo_disable;
4383 ops->enable = r8168b_0_hw_jumbo_enable;
4384 break;
4385 case RTL_GIGA_MAC_VER_12:
4386 case RTL_GIGA_MAC_VER_17:
4387 ops->disable = r8168b_1_hw_jumbo_disable;
4388 ops->enable = r8168b_1_hw_jumbo_enable;
4389 break;
4390 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4391 case RTL_GIGA_MAC_VER_19:
4392 case RTL_GIGA_MAC_VER_20:
4393 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4394 case RTL_GIGA_MAC_VER_22:
4395 case RTL_GIGA_MAC_VER_23:
4396 case RTL_GIGA_MAC_VER_24:
4397 case RTL_GIGA_MAC_VER_25:
4398 case RTL_GIGA_MAC_VER_26:
4399 ops->disable = r8168c_hw_jumbo_disable;
4400 ops->enable = r8168c_hw_jumbo_enable;
4401 break;
4402 case RTL_GIGA_MAC_VER_27:
4403 case RTL_GIGA_MAC_VER_28:
4404 ops->disable = r8168dp_hw_jumbo_disable;
4405 ops->enable = r8168dp_hw_jumbo_enable;
4406 break;
4407 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4408 case RTL_GIGA_MAC_VER_32:
4409 case RTL_GIGA_MAC_VER_33:
4410 case RTL_GIGA_MAC_VER_34:
4411 ops->disable = r8168e_hw_jumbo_disable;
4412 ops->enable = r8168e_hw_jumbo_enable;
4413 break;
4414
4415 /*
4416 * No action needed for jumbo frames with 8169.
4417 * No jumbo for 810x at all.
4418 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004419 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004420 default:
4421 ops->disable = NULL;
4422 ops->enable = NULL;
4423 break;
4424 }
4425}
4426
Francois Romieuffc46952012-07-06 14:19:23 +02004427DECLARE_RTL_COND(rtl_chipcmd_cond)
4428{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004429 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004430}
4431
Francois Romieu6f43adc2011-04-29 15:05:51 +02004432static void rtl_hw_reset(struct rtl8169_private *tp)
4433{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004434 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004435
Francois Romieuffc46952012-07-06 14:19:23 +02004436 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004437}
4438
Francois Romieub6ffd972011-06-17 17:00:05 +02004439static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4440{
4441 struct rtl_fw *rtl_fw;
4442 const char *name;
4443 int rc = -ENOMEM;
4444
4445 name = rtl_lookup_firmware_name(tp);
4446 if (!name)
4447 goto out_no_firmware;
4448
4449 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4450 if (!rtl_fw)
4451 goto err_warn;
4452
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004453 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004454 if (rc < 0)
4455 goto err_free;
4456
Francois Romieufd112f22011-06-18 00:10:29 +02004457 rc = rtl_check_firmware(tp, rtl_fw);
4458 if (rc < 0)
4459 goto err_release_firmware;
4460
Francois Romieub6ffd972011-06-17 17:00:05 +02004461 tp->rtl_fw = rtl_fw;
4462out:
4463 return;
4464
Francois Romieufd112f22011-06-18 00:10:29 +02004465err_release_firmware:
4466 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004467err_free:
4468 kfree(rtl_fw);
4469err_warn:
4470 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4471 name, rc);
4472out_no_firmware:
4473 tp->rtl_fw = NULL;
4474 goto out;
4475}
4476
François Romieu953a12c2011-04-24 17:38:48 +02004477static void rtl_request_firmware(struct rtl8169_private *tp)
4478{
Francois Romieub6ffd972011-06-17 17:00:05 +02004479 if (IS_ERR(tp->rtl_fw))
4480 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004481}
4482
Hayes Wang92fc43b2011-07-06 15:58:03 +08004483static void rtl_rx_close(struct rtl8169_private *tp)
4484{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004485 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004486}
4487
Francois Romieuffc46952012-07-06 14:19:23 +02004488DECLARE_RTL_COND(rtl_npq_cond)
4489{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004490 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02004491}
4492
4493DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4494{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004495 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02004496}
4497
françois romieue6de30d2011-01-03 15:08:37 +00004498static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499{
4500 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004501 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Hayes Wang92fc43b2011-07-06 15:58:03 +08004503 rtl_rx_close(tp);
4504
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004505 switch (tp->mac_version) {
4506 case RTL_GIGA_MAC_VER_27:
4507 case RTL_GIGA_MAC_VER_28:
4508 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02004509 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004510 break;
4511 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4512 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004513 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004514 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004515 break;
4516 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004517 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004518 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004519 break;
françois romieue6de30d2011-01-03 15:08:37 +00004520 }
4521
Hayes Wang92fc43b2011-07-06 15:58:03 +08004522 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523}
4524
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004525static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004526{
Francois Romieu9cb427b2006-11-02 00:10:16 +01004527 /* Set DMA burst size and Interframe Gap Time */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004528 RTL_W32(tp, TxConfig, (TX_DMA_BURST << TxDMAShift) |
Francois Romieu9cb427b2006-11-02 00:10:16 +01004529 (InterFrameGap << TxInterFrameGapShift));
4530}
4531
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004532static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004534 /* Low hurts. Let's disable the filtering. */
4535 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004536}
4537
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004538static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004539{
4540 /*
4541 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4542 * register to be written before TxDescAddrLow to work.
4543 * Switching from MMIO to I/O access fixes the issue as well.
4544 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004545 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4546 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4547 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4548 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004549}
4550
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004551static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004552{
Francois Romieu37441002011-06-17 22:58:54 +02004553 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004554 u32 mac_version;
4555 u32 clk;
4556 u32 val;
4557 } cfg2_info [] = {
4558 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4559 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4560 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4561 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004562 };
4563 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004564 unsigned int i;
4565 u32 clk;
4566
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004567 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004568 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004569 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004570 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004571 break;
4572 }
4573 }
4574}
4575
Francois Romieue6b763e2012-03-08 09:35:39 +01004576static void rtl_set_rx_mode(struct net_device *dev)
4577{
4578 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01004579 u32 mc_filter[2]; /* Multicast hash filter */
4580 int rx_mode;
4581 u32 tmp = 0;
4582
4583 if (dev->flags & IFF_PROMISC) {
4584 /* Unconditionally log net taps. */
4585 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4586 rx_mode =
4587 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4588 AcceptAllPhys;
4589 mc_filter[1] = mc_filter[0] = 0xffffffff;
4590 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4591 (dev->flags & IFF_ALLMULTI)) {
4592 /* Too many to filter perfectly -- accept all multicasts. */
4593 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4594 mc_filter[1] = mc_filter[0] = 0xffffffff;
4595 } else {
4596 struct netdev_hw_addr *ha;
4597
4598 rx_mode = AcceptBroadcast | AcceptMyPhys;
4599 mc_filter[1] = mc_filter[0] = 0;
4600 netdev_for_each_mc_addr(ha, dev) {
4601 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4602 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4603 rx_mode |= AcceptMulticast;
4604 }
4605 }
4606
4607 if (dev->features & NETIF_F_RXALL)
4608 rx_mode |= (AcceptErr | AcceptRunt);
4609
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004610 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01004611
4612 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4613 u32 data = mc_filter[0];
4614
4615 mc_filter[0] = swab32(mc_filter[1]);
4616 mc_filter[1] = swab32(data);
4617 }
4618
Nathan Walp04817762012-11-01 12:08:47 +00004619 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4620 mc_filter[1] = mc_filter[0] = 0xffffffff;
4621
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004622 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4623 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004624
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004625 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004626}
4627
Heiner Kallweit52f85602018-05-19 10:29:33 +02004628static void rtl_hw_start(struct rtl8169_private *tp)
4629{
4630 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4631
4632 tp->hw_start(tp);
4633
4634 rtl_set_rx_max_size(tp);
4635 rtl_set_rx_tx_desc_registers(tp);
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004636 rtl_set_tx_config_registers(tp);
Heiner Kallweit52f85602018-05-19 10:29:33 +02004637 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4638
4639 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4640 RTL_R8(tp, IntrMask);
4641 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004642 rtl_init_rxcfg(tp);
4643
Heiner Kallweit52f85602018-05-19 10:29:33 +02004644 rtl_set_rx_mode(tp->dev);
4645 /* no early-rx interrupts */
4646 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
4647 rtl_irq_enable_all(tp);
4648}
4649
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004650static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01004651{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004652 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004653 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004654
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004655 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004657 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004658
Francois Romieucecb5fd2011-04-01 10:21:07 +02004659 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4660 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004661 netif_dbg(tp, drv, tp->dev,
4662 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004663 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 }
4665
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004666 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02004667
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004668 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01004669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 /*
4671 * Undocumented corner. Supposedly:
4672 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4673 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004674 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004676 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01004677}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Francois Romieuffc46952012-07-06 14:19:23 +02004679DECLARE_RTL_COND(rtl_csiar_cond)
4680{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004681 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02004682}
4683
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004684static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004685{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004686 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4687
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004688 RTL_W32(tp, CSIDR, value);
4689 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004690 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004691
Francois Romieuffc46952012-07-06 14:19:23 +02004692 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004693}
4694
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004695static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004696{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004697 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4698
4699 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4700 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004701
Francois Romieuffc46952012-07-06 14:19:23 +02004702 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004703 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004704}
4705
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004706static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004707{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004708 struct pci_dev *pdev = tp->pci_dev;
4709 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004710
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004711 /* According to Realtek the value at config space address 0x070f
4712 * controls the L0s/L1 entrance latency. We try standard ECAM access
4713 * first and if it fails fall back to CSI.
4714 */
4715 if (pdev->cfg_size > 0x070f &&
4716 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4717 return;
4718
4719 netdev_notice_once(tp->dev,
4720 "No native access to PCI extended config space, falling back to CSI\n");
4721 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4722 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004723}
4724
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004725static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08004726{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004727 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02004728}
4729
4730struct ephy_info {
4731 unsigned int offset;
4732 u16 mask;
4733 u16 bits;
4734};
4735
Francois Romieufdf6fc02012-07-06 22:40:38 +02004736static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4737 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004738{
4739 u16 w;
4740
4741 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004742 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4743 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004744 e++;
4745 }
4746}
4747
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004748static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02004749{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004750 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004751 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004752}
4753
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004754static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004755{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004756 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004757 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004758}
4759
hayeswangb51ecea2014-07-09 14:52:51 +08004760static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
4761{
hayeswangb51ecea2014-07-09 14:52:51 +08004762 u8 data;
4763
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004764 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08004765
4766 if (enable)
4767 data |= Rdy_to_L23;
4768 else
4769 data &= ~Rdy_to_L23;
4770
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004771 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08004772}
4773
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004774static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4775{
4776 if (enable) {
4777 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
4778 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
4779 } else {
4780 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4781 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4782 }
4783}
4784
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004785static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004786{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004787 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004788
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004789 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004790 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02004791
françois romieufaf1e782013-02-27 13:01:57 +00004792 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004793 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00004794 PCI_EXP_DEVCTL_NOSNOOP_EN);
4795 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004796}
4797
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004798static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004799{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004800 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004801
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004802 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004803
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004804 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004805}
4806
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004807static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004808{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004809 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02004810
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004811 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004812
françois romieufaf1e782013-02-27 13:01:57 +00004813 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004814 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02004815
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004816 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004817
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004818 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004819 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02004820}
4821
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004822static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004823{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004824 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004825 { 0x01, 0, 0x0001 },
4826 { 0x02, 0x0800, 0x1000 },
4827 { 0x03, 0, 0x0042 },
4828 { 0x06, 0x0080, 0x0000 },
4829 { 0x07, 0, 0x2000 }
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_8168cp, ARRAY_SIZE(e_info_8168cp));
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_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004840{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004841 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004842
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004843 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +02004844
françois romieufaf1e782013-02-27 13:01:57 +00004845 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004846 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieuef3386f2008-06-29 12:24:30 +02004847
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004848 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004849 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieuef3386f2008-06-29 12:24:30 +02004850}
4851
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004852static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004853{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004854 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004855
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004856 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004857
4858 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004859 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004860
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004861 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004862
françois romieufaf1e782013-02-27 13:01:57 +00004863 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004864 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004865
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004866 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004867 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004868}
4869
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004870static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004871{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004872 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004873 { 0x02, 0x0800, 0x1000 },
4874 { 0x03, 0, 0x0002 },
4875 { 0x06, 0x0080, 0x0000 }
4876 };
4877
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004878 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004879
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004880 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02004881
Francois Romieufdf6fc02012-07-06 22:40:38 +02004882 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02004883
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004884 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004885}
4886
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004887static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004888{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004889 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004890 { 0x01, 0, 0x0001 },
4891 { 0x03, 0x0400, 0x0220 }
4892 };
4893
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004894 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004895
Francois Romieufdf6fc02012-07-06 22:40:38 +02004896 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02004897
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004898 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004899}
4900
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004901static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004902{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004903 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004904}
4905
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004906static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004907{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004908 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004909
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004910 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004911}
4912
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004913static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004914{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004915 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004916
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004917 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004918
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004919 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004920
françois romieufaf1e782013-02-27 13:01:57 +00004921 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004922 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02004923
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004924 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004925 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02004926}
4927
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004928static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004929{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004930 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004931
françois romieufaf1e782013-02-27 13:01:57 +00004932 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004933 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00004934
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004935 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00004936
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004937 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004938}
4939
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004940static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004941{
4942 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004943 { 0x0b, 0x0000, 0x0048 },
4944 { 0x19, 0x0020, 0x0050 },
4945 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00004946 };
françois romieue6de30d2011-01-03 15:08:37 +00004947
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004948 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004949
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004950 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00004951
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004952 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00004953
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004954 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00004955
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004956 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004957}
4958
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004959static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004960{
Hayes Wang70090422011-07-06 15:58:06 +08004961 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004962 { 0x00, 0x0200, 0x0100 },
4963 { 0x00, 0x0000, 0x0004 },
4964 { 0x06, 0x0002, 0x0001 },
4965 { 0x06, 0x0000, 0x0030 },
4966 { 0x07, 0x0000, 0x2000 },
4967 { 0x00, 0x0000, 0x0020 },
4968 { 0x03, 0x5800, 0x2000 },
4969 { 0x03, 0x0000, 0x0001 },
4970 { 0x01, 0x0800, 0x1000 },
4971 { 0x07, 0x0000, 0x4000 },
4972 { 0x1e, 0x0000, 0x2000 },
4973 { 0x19, 0xffff, 0xfe6c },
4974 { 0x0a, 0x0000, 0x0040 }
4975 };
4976
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004977 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004978
Francois Romieufdf6fc02012-07-06 22:40:38 +02004979 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004980
françois romieufaf1e782013-02-27 13:01:57 +00004981 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004982 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00004983
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004984 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00004985
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004986 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004987
4988 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004989 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4990 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004991
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004992 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004993}
4994
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004995static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08004996{
4997 static const struct ephy_info e_info_8168e_2[] = {
4998 { 0x09, 0x0000, 0x0080 },
4999 { 0x19, 0x0000, 0x0224 }
5000 };
5001
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005002 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005003
Francois Romieufdf6fc02012-07-06 22:40:38 +02005004 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005005
françois romieufaf1e782013-02-27 13:01:57 +00005006 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005007 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005008
Francois Romieufdf6fc02012-07-06 22:40:38 +02005009 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5010 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5011 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5012 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5013 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5014 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005015 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5016 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005017
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005018 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005019
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005020 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005021
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005022 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5023 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005024
5025 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005026 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005027
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005028 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5029 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5030 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02005031
5032 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08005033}
5034
Hayes Wang5f886e02012-03-30 14:33:03 +08005035static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005036{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005037 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005038
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005039 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005040
Francois Romieufdf6fc02012-07-06 22:40:38 +02005041 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5042 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5043 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5044 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005045 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5046 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5047 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5048 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005049 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5050 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005051
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005052 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005053
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005054 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005055
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005056 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5057 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5058 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5059 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5060 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005061}
5062
Hayes Wang5f886e02012-03-30 14:33:03 +08005063static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5064{
Hayes Wang5f886e02012-03-30 14:33:03 +08005065 static const struct ephy_info e_info_8168f_1[] = {
5066 { 0x06, 0x00c0, 0x0020 },
5067 { 0x08, 0x0001, 0x0002 },
5068 { 0x09, 0x0000, 0x0080 },
5069 { 0x19, 0x0000, 0x0224 }
5070 };
5071
5072 rtl_hw_start_8168f(tp);
5073
Francois Romieufdf6fc02012-07-06 22:40:38 +02005074 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005075
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005076 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005077
5078 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005079 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005080}
5081
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005082static void rtl_hw_start_8411(struct rtl8169_private *tp)
5083{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005084 static const struct ephy_info e_info_8168f_1[] = {
5085 { 0x06, 0x00c0, 0x0020 },
5086 { 0x0f, 0xffff, 0x5200 },
5087 { 0x1e, 0x0000, 0x4000 },
5088 { 0x19, 0x0000, 0x0224 }
5089 };
5090
5091 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005092 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005093
Francois Romieufdf6fc02012-07-06 22:40:38 +02005094 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005095
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005096 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005097}
5098
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005099static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005100{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005101 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
hayeswangbeb330a2013-04-01 22:23:39 +00005102
Hayes Wangc5583862012-07-02 17:23:22 +08005103 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5104 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5105 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5106 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5107
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005108 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08005109
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005110 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005111
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005112 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5113 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005114 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005115
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005116 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5117 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005118
5119 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5120 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5121
5122 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005123 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005124
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005125 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5126 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005127
5128 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005129}
5130
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005131static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5132{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005133 static const struct ephy_info e_info_8168g_1[] = {
5134 { 0x00, 0x0000, 0x0008 },
5135 { 0x0c, 0x37d0, 0x0820 },
5136 { 0x1e, 0x0000, 0x0001 },
5137 { 0x19, 0x8000, 0x0000 }
5138 };
5139
5140 rtl_hw_start_8168g(tp);
5141
5142 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005143 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005144 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005145 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005146}
5147
hayeswang57538c42013-04-01 22:23:40 +00005148static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5149{
hayeswang57538c42013-04-01 22:23:40 +00005150 static const struct ephy_info e_info_8168g_2[] = {
5151 { 0x00, 0x0000, 0x0008 },
5152 { 0x0c, 0x3df0, 0x0200 },
5153 { 0x19, 0xffff, 0xfc00 },
5154 { 0x1e, 0xffff, 0x20eb }
5155 };
5156
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005157 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005158
5159 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005160 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5161 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005162 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5163}
5164
hayeswang45dd95c2013-07-08 17:09:01 +08005165static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5166{
hayeswang45dd95c2013-07-08 17:09:01 +08005167 static const struct ephy_info e_info_8411_2[] = {
5168 { 0x00, 0x0000, 0x0008 },
5169 { 0x0c, 0x3df0, 0x0200 },
5170 { 0x0f, 0xffff, 0x5200 },
5171 { 0x19, 0x0020, 0x0000 },
5172 { 0x1e, 0x0000, 0x2000 }
5173 };
5174
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005175 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005176
5177 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005178 rtl_hw_aspm_clkreq_enable(tp, false);
hayeswang45dd95c2013-07-08 17:09:01 +08005179 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005180 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08005181}
5182
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005183static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5184{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005185 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005186 u32 data;
5187 static const struct ephy_info e_info_8168h_1[] = {
5188 { 0x1e, 0x0800, 0x0001 },
5189 { 0x1d, 0x0000, 0x0800 },
5190 { 0x05, 0xffff, 0x2089 },
5191 { 0x06, 0xffff, 0x5881 },
5192 { 0x04, 0xffff, 0x154a },
5193 { 0x01, 0xffff, 0x068b }
5194 };
5195
5196 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005197 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005198 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5199
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005200 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005201
5202 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5203 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5204 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5205 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5206
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005207 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005208
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005209 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005210
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005211 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5212 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005213
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005214 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005215
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005216 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005217
5218 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5219
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005220 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5221 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005222
5223 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5224 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5225
5226 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005227 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005228
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005229 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5230 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005231
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005232 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005233
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005234 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005235
5236 rtl_pcie_state_l2l3_enable(tp, false);
5237
5238 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005239 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005240 rtl_writephy(tp, 0x1f, 0x0000);
5241 if (rg_saw_cnt > 0) {
5242 u16 sw_cnt_1ms_ini;
5243
5244 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5245 sw_cnt_1ms_ini &= 0x0fff;
5246 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005247 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005248 data |= sw_cnt_1ms_ini;
5249 r8168_mac_ocp_write(tp, 0xd412, data);
5250 }
5251
5252 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005253 data &= ~0xf0;
5254 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005255 r8168_mac_ocp_write(tp, 0xe056, data);
5256
5257 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005258 data &= ~0x6000;
5259 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005260 r8168_mac_ocp_write(tp, 0xe052, data);
5261
5262 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005263 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005264 data |= 0x017f;
5265 r8168_mac_ocp_write(tp, 0xe0d6, data);
5266
5267 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005268 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005269 data |= 0x047f;
5270 r8168_mac_ocp_write(tp, 0xd420, data);
5271
5272 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5273 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5274 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5275 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005276
5277 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005278}
5279
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005280static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5281{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005282 rtl8168ep_stop_cmac(tp);
5283
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005284 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005285
5286 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5287 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5288 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5289 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5290
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005291 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005292
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005293 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005294
5295 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5296 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5297
5298 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5299
5300 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5301
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005302 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5303 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005304
5305 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5306 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5307
5308 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005309 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005310
5311 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5312
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005313 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005314
5315 rtl_pcie_state_l2l3_enable(tp, false);
5316}
5317
5318static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5319{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005320 static const struct ephy_info e_info_8168ep_1[] = {
5321 { 0x00, 0xffff, 0x10ab },
5322 { 0x06, 0xffff, 0xf030 },
5323 { 0x08, 0xffff, 0x2006 },
5324 { 0x0d, 0xffff, 0x1666 },
5325 { 0x0c, 0x3ff0, 0x0000 }
5326 };
5327
5328 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005329 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005330 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5331
5332 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005333
5334 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005335}
5336
5337static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5338{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005339 static const struct ephy_info e_info_8168ep_2[] = {
5340 { 0x00, 0xffff, 0x10a3 },
5341 { 0x19, 0xffff, 0xfc00 },
5342 { 0x1e, 0xffff, 0x20ea }
5343 };
5344
5345 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005346 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005347 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5348
5349 rtl_hw_start_8168ep(tp);
5350
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005351 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5352 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005353
5354 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005355}
5356
5357static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5358{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005359 u32 data;
5360 static const struct ephy_info e_info_8168ep_3[] = {
5361 { 0x00, 0xffff, 0x10a3 },
5362 { 0x19, 0xffff, 0x7c00 },
5363 { 0x1e, 0xffff, 0x20eb },
5364 { 0x0d, 0xffff, 0x1666 }
5365 };
5366
5367 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005368 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005369 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5370
5371 rtl_hw_start_8168ep(tp);
5372
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005373 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5374 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005375
5376 data = r8168_mac_ocp_read(tp, 0xd3e2);
5377 data &= 0xf000;
5378 data |= 0x0271;
5379 r8168_mac_ocp_write(tp, 0xd3e2, data);
5380
5381 data = r8168_mac_ocp_read(tp, 0xd3e4);
5382 data &= 0xff00;
5383 r8168_mac_ocp_write(tp, 0xd3e4, data);
5384
5385 data = r8168_mac_ocp_read(tp, 0xe860);
5386 data |= 0x0080;
5387 r8168_mac_ocp_write(tp, 0xe860, data);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005388
5389 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005390}
5391
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005392static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005393{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005394 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005395
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005396 tp->cp_cmd &= ~INTT_MASK;
5397 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005398 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02005399
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005400 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01005401
5402 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005403 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005404 tp->event_slow |= RxFIFOOver | PCSTimeout;
5405 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005406 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005407
Francois Romieu219a1e92008-06-28 11:58:39 +02005408 switch (tp->mac_version) {
5409 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005410 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005411 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005412
5413 case RTL_GIGA_MAC_VER_12:
5414 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005415 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005416 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005417
5418 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005419 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005420 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005421
5422 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005423 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005424 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005425
5426 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005427 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005428 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005429
Francois Romieu197ff762008-06-28 13:16:02 +02005430 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005431 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005432 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005433
Francois Romieu6fb07052008-06-29 11:54:28 +02005434 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005435 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005436 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005437
Francois Romieuef3386f2008-06-29 12:24:30 +02005438 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005439 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005440 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005441
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005442 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005443 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005444 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005445
Francois Romieu5b538df2008-07-20 16:22:45 +02005446 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005447 case RTL_GIGA_MAC_VER_26:
5448 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005449 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005450 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005451
françois romieue6de30d2011-01-03 15:08:37 +00005452 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005453 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005454 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005455
hayeswang4804b3b2011-03-21 01:50:29 +00005456 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005457 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005458 break;
5459
hayeswang01dc7fe2011-03-21 01:50:28 +00005460 case RTL_GIGA_MAC_VER_32:
5461 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005462 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005463 break;
5464 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005465 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005466 break;
françois romieue6de30d2011-01-03 15:08:37 +00005467
Hayes Wangc2218922011-09-06 16:55:18 +08005468 case RTL_GIGA_MAC_VER_35:
5469 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005470 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005471 break;
5472
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005473 case RTL_GIGA_MAC_VER_38:
5474 rtl_hw_start_8411(tp);
5475 break;
5476
Hayes Wangc5583862012-07-02 17:23:22 +08005477 case RTL_GIGA_MAC_VER_40:
5478 case RTL_GIGA_MAC_VER_41:
5479 rtl_hw_start_8168g_1(tp);
5480 break;
hayeswang57538c42013-04-01 22:23:40 +00005481 case RTL_GIGA_MAC_VER_42:
5482 rtl_hw_start_8168g_2(tp);
5483 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005484
hayeswang45dd95c2013-07-08 17:09:01 +08005485 case RTL_GIGA_MAC_VER_44:
5486 rtl_hw_start_8411_2(tp);
5487 break;
5488
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005489 case RTL_GIGA_MAC_VER_45:
5490 case RTL_GIGA_MAC_VER_46:
5491 rtl_hw_start_8168h_1(tp);
5492 break;
5493
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005494 case RTL_GIGA_MAC_VER_49:
5495 rtl_hw_start_8168ep_1(tp);
5496 break;
5497
5498 case RTL_GIGA_MAC_VER_50:
5499 rtl_hw_start_8168ep_2(tp);
5500 break;
5501
5502 case RTL_GIGA_MAC_VER_51:
5503 rtl_hw_start_8168ep_3(tp);
5504 break;
5505
Francois Romieu219a1e92008-06-28 11:58:39 +02005506 default:
Heiner Kallweit49d17512018-06-28 20:36:15 +02005507 netif_err(tp, drv, tp->dev,
5508 "unknown chipset (mac_version = %d)\n",
5509 tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005510 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005511 }
Francois Romieu07ce4062007-02-23 23:36:39 +01005512}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005514static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005515{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005516 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005517 { 0x01, 0, 0x6e65 },
5518 { 0x02, 0, 0x091f },
5519 { 0x03, 0, 0xc2f9 },
5520 { 0x06, 0, 0xafb5 },
5521 { 0x07, 0, 0x0e00 },
5522 { 0x19, 0, 0xec80 },
5523 { 0x01, 0, 0x2e65 },
5524 { 0x01, 0, 0x6e65 }
5525 };
5526 u8 cfg1;
5527
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005528 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005529
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005530 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005531
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005532 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005533
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005534 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02005535 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005536 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005537
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005538 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005539 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005540 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005541
Francois Romieufdf6fc02012-07-06 22:40:38 +02005542 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005543}
5544
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005545static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005546{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005547 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005548
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005549 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005550
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005551 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5552 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005553}
5554
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005555static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005556{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005557 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005558
Francois Romieufdf6fc02012-07-06 22:40:38 +02005559 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005560}
5561
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005562static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005563{
5564 static const struct ephy_info e_info_8105e_1[] = {
5565 { 0x07, 0, 0x4000 },
5566 { 0x19, 0, 0x0200 },
5567 { 0x19, 0, 0x0020 },
5568 { 0x1e, 0, 0x2000 },
5569 { 0x03, 0, 0x0001 },
5570 { 0x19, 0, 0x0100 },
5571 { 0x19, 0, 0x0004 },
5572 { 0x0a, 0, 0x0020 }
5573 };
5574
Francois Romieucecb5fd2011-04-01 10:21:07 +02005575 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005576 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005577
Francois Romieucecb5fd2011-04-01 10:21:07 +02005578 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005579 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005580
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005581 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5582 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005583
Francois Romieufdf6fc02012-07-06 22:40:38 +02005584 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08005585
5586 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005587}
5588
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005589static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005590{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005591 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005592 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005593}
5594
Hayes Wang7e18dca2012-03-30 14:33:02 +08005595static void rtl_hw_start_8402(struct rtl8169_private *tp)
5596{
Hayes Wang7e18dca2012-03-30 14:33:02 +08005597 static const struct ephy_info e_info_8402[] = {
5598 { 0x19, 0xffff, 0xff64 },
5599 { 0x1e, 0, 0x4000 }
5600 };
5601
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005602 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005603
5604 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005605 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005606
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005607 RTL_W32(tp, TxConfig, RTL_R32(tp, TxConfig) | TXCFG_AUTO_FIFO);
5608 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005609
Francois Romieufdf6fc02012-07-06 22:40:38 +02005610 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005611
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005612 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005613
Francois Romieufdf6fc02012-07-06 22:40:38 +02005614 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5615 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005616 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5617 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005618 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5619 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005620 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005621
5622 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005623}
5624
Hayes Wang5598bfe2012-07-02 17:23:21 +08005625static void rtl_hw_start_8106(struct rtl8169_private *tp)
5626{
Hayes Wang5598bfe2012-07-02 17:23:21 +08005627 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005628 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005629
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005630 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5631 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5632 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005633
5634 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005635}
5636
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005637static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005638{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005639 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5640 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005641
Francois Romieucecb5fd2011-04-01 10:21:07 +02005642 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005643 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005644 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005645 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005646
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005647 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005648
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005649 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005650 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00005651
Francois Romieu2857ffb2008-08-02 21:08:49 +02005652 switch (tp->mac_version) {
5653 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005654 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005655 break;
5656
5657 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005658 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005659 break;
5660
5661 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005662 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005663 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005664
5665 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005666 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005667 break;
5668 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005669 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005670 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005671
5672 case RTL_GIGA_MAC_VER_37:
5673 rtl_hw_start_8402(tp);
5674 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005675
5676 case RTL_GIGA_MAC_VER_39:
5677 rtl_hw_start_8106(tp);
5678 break;
hayeswang58152cd2013-04-01 22:23:42 +00005679 case RTL_GIGA_MAC_VER_43:
5680 rtl_hw_start_8168g_2(tp);
5681 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005682 case RTL_GIGA_MAC_VER_47:
5683 case RTL_GIGA_MAC_VER_48:
5684 rtl_hw_start_8168h_1(tp);
5685 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005686 }
5687
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005688 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689}
5690
5691static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5692{
Francois Romieud58d46b2011-05-03 16:38:29 +02005693 struct rtl8169_private *tp = netdev_priv(dev);
5694
Francois Romieud58d46b2011-05-03 16:38:29 +02005695 if (new_mtu > ETH_DATA_LEN)
5696 rtl_hw_jumbo_enable(tp);
5697 else
5698 rtl_hw_jumbo_disable(tp);
5699
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005701 netdev_update_features(dev);
5702
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704}
5705
5706static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5707{
Al Viro95e09182007-12-22 18:55:39 +00005708 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5710}
5711
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005712static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5713 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005715 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5716 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005717
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005718 kfree(*data_buff);
5719 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 rtl8169_make_unusable_by_asic(desc);
5721}
5722
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005723static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
5725 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5726
Alexander Duycka0750132014-12-11 15:02:17 -08005727 /* Force memory writes to complete before releasing descriptor */
5728 dma_wmb();
5729
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005730 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731}
5732
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005733static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005735 return (void *)ALIGN((long)data, 16);
5736}
5737
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005738static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5739 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005740{
5741 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005743 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02005744 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005746 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005747 if (!data)
5748 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005749
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005750 if (rtl8169_align(data) != data) {
5751 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005752 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005753 if (!data)
5754 return NULL;
5755 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005756
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005757 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005758 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005759 if (unlikely(dma_mapping_error(d, mapping))) {
5760 if (net_ratelimit())
5761 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005762 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
Heiner Kallweitd731af72018-04-17 23:26:41 +02005765 desc->addr = cpu_to_le64(mapping);
5766 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005767 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005768
5769err_out:
5770 kfree(data);
5771 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772}
5773
5774static void rtl8169_rx_clear(struct rtl8169_private *tp)
5775{
Francois Romieu07d3f512007-02-21 22:40:46 +01005776 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
5778 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005779 if (tp->Rx_databuff[i]) {
5780 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 tp->RxDescArray + i);
5782 }
5783 }
5784}
5785
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005786static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005788 desc->opts1 |= cpu_to_le32(RingEnd);
5789}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005790
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005791static int rtl8169_rx_fill(struct rtl8169_private *tp)
5792{
5793 unsigned int i;
5794
5795 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005796 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005797
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005798 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005799 if (!data) {
5800 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005801 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005802 }
5803 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005806 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5807 return 0;
5808
5809err_out:
5810 rtl8169_rx_clear(tp);
5811 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812}
5813
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005814static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 rtl8169_init_ring_indexes(tp);
5817
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005818 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5819 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005821 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822}
5823
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005824static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 struct TxDesc *desc)
5826{
5827 unsigned int len = tx_skb->len;
5828
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005829 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5830
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 desc->opts1 = 0x00;
5832 desc->opts2 = 0x00;
5833 desc->addr = 0x00;
5834 tx_skb->len = 0;
5835}
5836
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005837static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5838 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839{
5840 unsigned int i;
5841
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005842 for (i = 0; i < n; i++) {
5843 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 struct ring_info *tx_skb = tp->tx_skb + entry;
5845 unsigned int len = tx_skb->len;
5846
5847 if (len) {
5848 struct sk_buff *skb = tx_skb->skb;
5849
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005850 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 tp->TxDescArray + entry);
5852 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07005853 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 tx_skb->skb = NULL;
5855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 }
5857 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005858}
5859
5860static void rtl8169_tx_clear(struct rtl8169_private *tp)
5861{
5862 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 tp->cur_tx = tp->dirty_tx = 0;
5864}
5865
Francois Romieu4422bcd2012-01-26 11:23:32 +01005866static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867{
David Howellsc4028952006-11-22 14:57:56 +00005868 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005869 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
Francois Romieuda78dbf2012-01-26 14:18:23 +01005871 napi_disable(&tp->napi);
5872 netif_stop_queue(dev);
5873 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
françois romieuc7c2c392011-12-04 20:30:52 +00005875 rtl8169_hw_reset(tp);
5876
Francois Romieu56de4142011-03-15 17:29:31 +01005877 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005878 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01005879
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005881 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
Francois Romieuda78dbf2012-01-26 14:18:23 +01005883 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005884 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005885 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886}
5887
5888static void rtl8169_tx_timeout(struct net_device *dev)
5889{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005890 struct rtl8169_private *tp = netdev_priv(dev);
5891
5892 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893}
5894
5895static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005896 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897{
5898 struct skb_shared_info *info = skb_shinfo(skb);
5899 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005900 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005901 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
5903 entry = tp->cur_tx;
5904 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005905 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 dma_addr_t mapping;
5907 u32 status, len;
5908 void *addr;
5909
5910 entry = (entry + 1) % NUM_TX_DESC;
5911
5912 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005913 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005914 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005915 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005916 if (unlikely(dma_mapping_error(d, mapping))) {
5917 if (net_ratelimit())
5918 netif_err(tp, drv, tp->dev,
5919 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005920 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
Francois Romieucecb5fd2011-04-01 10:21:07 +02005923 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005924 status = opts[0] | len |
5925 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
5927 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005928 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 txd->addr = cpu_to_le64(mapping);
5930
5931 tp->tx_skb[entry].len = len;
5932 }
5933
5934 if (cur_frag) {
5935 tp->tx_skb[entry].skb = skb;
5936 txd->opts1 |= cpu_to_le32(LastFrag);
5937 }
5938
5939 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005940
5941err_out:
5942 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5943 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944}
5945
françois romieub423e9a2013-05-18 01:24:46 +00005946static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5947{
5948 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5949}
5950
hayeswange9746042014-07-11 16:25:58 +08005951static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5952 struct net_device *dev);
5953/* r8169_csum_workaround()
5954 * The hw limites the value the transport offset. When the offset is out of the
5955 * range, calculate the checksum by sw.
5956 */
5957static void r8169_csum_workaround(struct rtl8169_private *tp,
5958 struct sk_buff *skb)
5959{
5960 if (skb_shinfo(skb)->gso_size) {
5961 netdev_features_t features = tp->dev->features;
5962 struct sk_buff *segs, *nskb;
5963
5964 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
5965 segs = skb_gso_segment(skb, features);
5966 if (IS_ERR(segs) || !segs)
5967 goto drop;
5968
5969 do {
5970 nskb = segs;
5971 segs = segs->next;
5972 nskb->next = NULL;
5973 rtl8169_start_xmit(nskb, tp->dev);
5974 } while (segs);
5975
Alexander Duyckeb781392015-05-01 10:34:44 -07005976 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08005977 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5978 if (skb_checksum_help(skb) < 0)
5979 goto drop;
5980
5981 rtl8169_start_xmit(skb, tp->dev);
5982 } else {
5983 struct net_device_stats *stats;
5984
5985drop:
5986 stats = &tp->dev->stats;
5987 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07005988 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08005989 }
5990}
5991
5992/* msdn_giant_send_check()
5993 * According to the document of microsoft, the TCP Pseudo Header excludes the
5994 * packet length for IPv6 TCP large packets.
5995 */
5996static int msdn_giant_send_check(struct sk_buff *skb)
5997{
5998 const struct ipv6hdr *ipv6h;
5999 struct tcphdr *th;
6000 int ret;
6001
6002 ret = skb_cow_head(skb, 0);
6003 if (ret)
6004 return ret;
6005
6006 ipv6h = ipv6_hdr(skb);
6007 th = tcp_hdr(skb);
6008
6009 th->check = 0;
6010 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6011
6012 return ret;
6013}
6014
hayeswang5888d3f2014-07-11 16:25:56 +08006015static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6016 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017{
Michał Mirosław350fb322011-04-08 06:35:56 +00006018 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Francois Romieu2b7b4312011-04-18 22:53:24 -07006020 if (mss) {
6021 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006022 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6023 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6024 const struct iphdr *ip = ip_hdr(skb);
6025
6026 if (ip->protocol == IPPROTO_TCP)
6027 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6028 else if (ip->protocol == IPPROTO_UDP)
6029 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6030 else
6031 WARN_ON_ONCE(1);
6032 }
6033
6034 return true;
6035}
6036
6037static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6038 struct sk_buff *skb, u32 *opts)
6039{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006040 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006041 u32 mss = skb_shinfo(skb)->gso_size;
6042
6043 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006044 if (transport_offset > GTTCPHO_MAX) {
6045 netif_warn(tp, tx_err, tp->dev,
6046 "Invalid transport offset 0x%x for TSO\n",
6047 transport_offset);
6048 return false;
6049 }
6050
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006051 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006052 case htons(ETH_P_IP):
6053 opts[0] |= TD1_GTSENV4;
6054 break;
6055
6056 case htons(ETH_P_IPV6):
6057 if (msdn_giant_send_check(skb))
6058 return false;
6059
6060 opts[0] |= TD1_GTSENV6;
6061 break;
6062
6063 default:
6064 WARN_ON_ONCE(1);
6065 break;
6066 }
6067
hayeswangbdfa4ed2014-07-11 16:25:57 +08006068 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006069 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006070 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006071 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
françois romieub423e9a2013-05-18 01:24:46 +00006073 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006074 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006075
hayeswange9746042014-07-11 16:25:58 +08006076 if (transport_offset > TCPHO_MAX) {
6077 netif_warn(tp, tx_err, tp->dev,
6078 "Invalid transport offset 0x%x\n",
6079 transport_offset);
6080 return false;
6081 }
6082
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006083 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006084 case htons(ETH_P_IP):
6085 opts[1] |= TD1_IPv4_CS;
6086 ip_protocol = ip_hdr(skb)->protocol;
6087 break;
6088
6089 case htons(ETH_P_IPV6):
6090 opts[1] |= TD1_IPv6_CS;
6091 ip_protocol = ipv6_hdr(skb)->nexthdr;
6092 break;
6093
6094 default:
6095 ip_protocol = IPPROTO_RAW;
6096 break;
6097 }
6098
6099 if (ip_protocol == IPPROTO_TCP)
6100 opts[1] |= TD1_TCP_CS;
6101 else if (ip_protocol == IPPROTO_UDP)
6102 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006103 else
6104 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006105
6106 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006107 } else {
6108 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006109 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 }
hayeswang5888d3f2014-07-11 16:25:56 +08006111
françois romieub423e9a2013-05-18 01:24:46 +00006112 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113}
6114
Stephen Hemminger613573252009-08-31 19:50:58 +00006115static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6116 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117{
6118 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006119 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006121 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 dma_addr_t mapping;
6123 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006124 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006125 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006126
Julien Ducourthial477206a2012-05-09 00:00:06 +02006127 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006128 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006129 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 }
6131
6132 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006133 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
françois romieub423e9a2013-05-18 01:24:46 +00006135 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6136 opts[0] = DescOwn;
6137
hayeswange9746042014-07-11 16:25:58 +08006138 if (!tp->tso_csum(tp, skb, opts)) {
6139 r8169_csum_workaround(tp, skb);
6140 return NETDEV_TX_OK;
6141 }
françois romieub423e9a2013-05-18 01:24:46 +00006142
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006143 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006144 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006145 if (unlikely(dma_mapping_error(d, mapping))) {
6146 if (net_ratelimit())
6147 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006148 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
6151 tp->tx_skb[entry].len = len;
6152 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153
Francois Romieu2b7b4312011-04-18 22:53:24 -07006154 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006155 if (frags < 0)
6156 goto err_dma_1;
6157 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006158 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006159 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006160 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006161 tp->tx_skb[entry].skb = skb;
6162 }
6163
Francois Romieu2b7b4312011-04-18 22:53:24 -07006164 txd->opts2 = cpu_to_le32(opts[1]);
6165
Richard Cochran5047fb52012-03-10 07:29:42 +00006166 skb_tx_timestamp(skb);
6167
Alexander Duycka0750132014-12-11 15:02:17 -08006168 /* Force memory writes to complete before releasing descriptor */
6169 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170
Francois Romieucecb5fd2011-04-01 10:21:07 +02006171 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006172 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 txd->opts1 = cpu_to_le32(status);
6174
Alexander Duycka0750132014-12-11 15:02:17 -08006175 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006176 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177
Alexander Duycka0750132014-12-11 15:02:17 -08006178 tp->cur_tx += frags + 1;
6179
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006180 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
David S. Miller87cda7c2015-02-22 15:54:29 -05006182 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01006183
David S. Miller87cda7c2015-02-22 15:54:29 -05006184 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006185 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6186 * not miss a ring update when it notices a stopped queue.
6187 */
6188 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006190 /* Sync with rtl_tx:
6191 * - publish queue status and cur_tx ring index (write barrier)
6192 * - refresh dirty_tx ring index (read barrier).
6193 * May the current thread have a pessimistic view of the ring
6194 * status and forget to wake up queue, a racing rtl_tx thread
6195 * can't.
6196 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006197 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006198 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 netif_wake_queue(dev);
6200 }
6201
Stephen Hemminger613573252009-08-31 19:50:58 +00006202 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006204err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006205 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006206err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006207 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006208 dev->stats.tx_dropped++;
6209 return NETDEV_TX_OK;
6210
6211err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006213 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006214 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215}
6216
6217static void rtl8169_pcierr_interrupt(struct net_device *dev)
6218{
6219 struct rtl8169_private *tp = netdev_priv(dev);
6220 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 u16 pci_status, pci_cmd;
6222
6223 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6224 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6225
Joe Perchesbf82c182010-02-09 11:49:50 +00006226 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6227 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
6229 /*
6230 * The recovery sequence below admits a very elaborated explanation:
6231 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006232 * - I did not see what else could be done;
6233 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 *
6235 * Feel free to adjust to your needs.
6236 */
Francois Romieua27993f2006-12-18 00:04:19 +01006237 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006238 pci_cmd &= ~PCI_COMMAND_PARITY;
6239 else
6240 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6241
6242 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
6244 pci_write_config_word(pdev, PCI_STATUS,
6245 pci_status & (PCI_STATUS_DETECTED_PARITY |
6246 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6247 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6248
6249 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006250 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006251 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006253 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 }
6256
françois romieue6de30d2011-01-03 15:08:37 +00006257 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006258
Francois Romieu98ddf982012-01-31 10:47:34 +01006259 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260}
6261
Francois Romieuda78dbf2012-01-26 14:18:23 +01006262static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263{
6264 unsigned int dirty_tx, tx_left;
6265
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 dirty_tx = tp->dirty_tx;
6267 smp_rmb();
6268 tx_left = tp->cur_tx - dirty_tx;
6269
6270 while (tx_left > 0) {
6271 unsigned int entry = dirty_tx % NUM_TX_DESC;
6272 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 u32 status;
6274
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6276 if (status & DescOwn)
6277 break;
6278
Alexander Duycka0750132014-12-11 15:02:17 -08006279 /* This barrier is needed to keep us from reading
6280 * any other fields out of the Tx descriptor until
6281 * we know the status of DescOwn
6282 */
6283 dma_rmb();
6284
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006285 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006286 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05006288 u64_stats_update_begin(&tp->tx_stats.syncp);
6289 tp->tx_stats.packets++;
6290 tp->tx_stats.bytes += tx_skb->skb->len;
6291 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006292 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 tx_skb->skb = NULL;
6294 }
6295 dirty_tx++;
6296 tx_left--;
6297 }
6298
6299 if (tp->dirty_tx != dirty_tx) {
6300 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006301 /* Sync with rtl8169_start_xmit:
6302 * - publish dirty_tx ring index (write barrier)
6303 * - refresh cur_tx ring index and queue status (read barrier)
6304 * May the current thread miss the stopped queue condition,
6305 * a racing xmit thread can only have a right view of the
6306 * ring status.
6307 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006308 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006310 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 netif_wake_queue(dev);
6312 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006313 /*
6314 * 8168 hack: TxPoll requests are lost when the Tx packets are
6315 * too close. Let's kick an extra TxPoll request when a burst
6316 * of start_xmit activity is detected (if it is not detected,
6317 * it is slow enough). -- FR
6318 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006319 if (tp->cur_tx != dirty_tx)
6320 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 }
6322}
6323
Francois Romieu126fa4b2005-05-12 20:09:17 -04006324static inline int rtl8169_fragmented_frame(u32 status)
6325{
6326 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6327}
6328
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006329static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 u32 status = opts1 & RxProtoMask;
6332
6333 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006334 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 skb->ip_summed = CHECKSUM_UNNECESSARY;
6336 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006337 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338}
6339
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006340static struct sk_buff *rtl8169_try_rx_copy(void *data,
6341 struct rtl8169_private *tp,
6342 int pkt_size,
6343 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006345 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006346 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006348 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006349 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006350 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006351 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006352 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006353 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006354 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6355
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006356 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357}
6358
Francois Romieuda78dbf2012-01-26 14:18:23 +01006359static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360{
6361 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006362 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
Timo Teräs9fba0812013-01-15 21:01:24 +00006366 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006368 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 u32 status;
6370
Heiner Kallweit62028062018-04-17 23:30:29 +02006371 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 if (status & DescOwn)
6373 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006374
6375 /* This barrier is needed to keep us from reading
6376 * any other fields out of the Rx descriptor until
6377 * we know the status of DescOwn
6378 */
6379 dma_rmb();
6380
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006381 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006382 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6383 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006384 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006386 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006388 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006389 /* RxFOVF is a reserved bit on later chip versions */
6390 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6391 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006392 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006393 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006394 } else if (status & (RxRUNT | RxCRC) &&
6395 !(status & RxRWT) &&
6396 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006397 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006400 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006401 dma_addr_t addr;
6402 int pkt_size;
6403
6404process_pkt:
6405 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006406 if (likely(!(dev->features & NETIF_F_RXFCS)))
6407 pkt_size = (status & 0x00003fff) - 4;
6408 else
6409 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410
Francois Romieu126fa4b2005-05-12 20:09:17 -04006411 /*
6412 * The driver does not support incoming fragmented
6413 * frames. They are seen as a symptom of over-mtu
6414 * sized frames.
6415 */
6416 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006417 dev->stats.rx_dropped++;
6418 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006419 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006420 }
6421
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006422 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6423 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006424 if (!skb) {
6425 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006426 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 }
6428
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006429 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 skb_put(skb, pkt_size);
6431 skb->protocol = eth_type_trans(skb, dev);
6432
Francois Romieu7a8fc772011-03-01 17:18:33 +01006433 rtl8169_rx_vlan_tag(desc, skb);
6434
françois romieu39174292015-11-11 23:35:18 +01006435 if (skb->pkt_type == PACKET_MULTICAST)
6436 dev->stats.multicast++;
6437
Francois Romieu56de4142011-03-15 17:29:31 +01006438 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439
Junchang Wang8027aa22012-03-04 23:30:32 +01006440 u64_stats_update_begin(&tp->rx_stats.syncp);
6441 tp->rx_stats.packets++;
6442 tp->rx_stats.bytes += pkt_size;
6443 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 }
françois romieuce11ff52013-01-24 13:30:06 +00006445release_descriptor:
6446 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006447 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 }
6449
6450 count = cur_rx - tp->cur_rx;
6451 tp->cur_rx = cur_rx;
6452
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 return count;
6454}
6455
Francois Romieu07d3f512007-02-21 22:40:46 +01006456static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006458 struct rtl8169_private *tp = dev_instance;
Heiner Kallweit05bbe552018-08-10 22:38:29 +02006459 u16 status = rtl_get_events(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460
Heiner Kallweit05bbe552018-08-10 22:38:29 +02006461 if (status == 0xffff || !(status & (RTL_EVENT_NAPI | tp->event_slow)))
6462 return IRQ_NONE;
françois romieu811fd302011-12-04 20:30:45 +00006463
Heiner Kallweit05bbe552018-08-10 22:38:29 +02006464 rtl_irq_disable(tp);
6465 napi_schedule_irqoff(&tp->napi);
6466
6467 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468}
6469
Francois Romieuda78dbf2012-01-26 14:18:23 +01006470/*
6471 * Workqueue context.
6472 */
6473static void rtl_slow_event_work(struct rtl8169_private *tp)
6474{
6475 struct net_device *dev = tp->dev;
6476 u16 status;
6477
6478 status = rtl_get_events(tp) & tp->event_slow;
6479 rtl_ack_events(tp, status);
6480
6481 if (unlikely(status & RxFIFOOver)) {
6482 switch (tp->mac_version) {
6483 /* Work around for rx fifo overflow */
6484 case RTL_GIGA_MAC_VER_11:
6485 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006486 /* XXX - Hack alert. See rtl_task(). */
6487 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006488 default:
6489 break;
6490 }
6491 }
6492
6493 if (unlikely(status & SYSErr))
6494 rtl8169_pcierr_interrupt(dev);
6495
6496 if (status & LinkChg)
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006497 phy_mac_interrupt(dev->phydev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006498
françois romieu7dbb4912012-06-09 10:53:16 +00006499 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006500}
6501
Francois Romieu4422bcd2012-01-26 11:23:32 +01006502static void rtl_task(struct work_struct *work)
6503{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006504 static const struct {
6505 int bitnr;
6506 void (*action)(struct rtl8169_private *);
6507 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006508 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006509 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6510 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01006511 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006512 struct rtl8169_private *tp =
6513 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006514 struct net_device *dev = tp->dev;
6515 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006516
Francois Romieuda78dbf2012-01-26 14:18:23 +01006517 rtl_lock_work(tp);
6518
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006519 if (!netif_running(dev) ||
6520 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006521 goto out_unlock;
6522
6523 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6524 bool pending;
6525
Francois Romieuda78dbf2012-01-26 14:18:23 +01006526 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006527 if (pending)
6528 rtl_work[i].action(tp);
6529 }
6530
6531out_unlock:
6532 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006533}
6534
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006535static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006537 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6538 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006539 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6540 int work_done= 0;
6541 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542
Francois Romieuda78dbf2012-01-26 14:18:23 +01006543 status = rtl_get_events(tp);
6544 rtl_ack_events(tp, status & ~tp->event_slow);
6545
6546 if (status & RTL_EVENT_NAPI_RX)
6547 work_done = rtl_rx(dev, tp, (u32) budget);
6548
6549 if (status & RTL_EVENT_NAPI_TX)
6550 rtl_tx(dev, tp);
6551
6552 if (status & tp->event_slow) {
6553 enable_mask &= ~tp->event_slow;
6554
6555 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006558 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08006559 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00006560
Francois Romieuda78dbf2012-01-26 14:18:23 +01006561 rtl_irq_enable(tp, enable_mask);
6562 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 }
6564
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006565 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006568static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02006569{
6570 struct rtl8169_private *tp = netdev_priv(dev);
6571
6572 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6573 return;
6574
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006575 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6576 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02006577}
6578
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006579static void r8169_phylink_handler(struct net_device *ndev)
6580{
6581 struct rtl8169_private *tp = netdev_priv(ndev);
6582
6583 if (netif_carrier_ok(ndev)) {
6584 rtl_link_chg_patch(tp);
6585 pm_request_resume(&tp->pci_dev->dev);
6586 } else {
6587 pm_runtime_idle(&tp->pci_dev->dev);
6588 }
6589
6590 if (net_ratelimit())
6591 phy_print_status(ndev->phydev);
6592}
6593
6594static int r8169_phy_connect(struct rtl8169_private *tp)
6595{
6596 struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
6597 phy_interface_t phy_mode;
6598 int ret;
6599
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02006600 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006601 PHY_INTERFACE_MODE_MII;
6602
6603 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6604 phy_mode);
6605 if (ret)
6606 return ret;
6607
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02006608 if (!tp->supports_gmii)
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006609 phy_set_max_speed(phydev, SPEED_100);
6610
6611 /* Ensure to advertise everything, incl. pause */
6612 phydev->advertising = phydev->supported;
6613
6614 phy_attached_info(phydev);
6615
6616 return 0;
6617}
6618
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619static void rtl8169_down(struct net_device *dev)
6620{
6621 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006623 phy_stop(dev->phydev);
6624
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006625 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006626 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627
Hayes Wang92fc43b2011-07-06 15:58:03 +08006628 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006629 /*
6630 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006631 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6632 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006633 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006634 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006637 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 rtl8169_tx_clear(tp);
6640
6641 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006642
6643 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644}
6645
6646static int rtl8169_close(struct net_device *dev)
6647{
6648 struct rtl8169_private *tp = netdev_priv(dev);
6649 struct pci_dev *pdev = tp->pci_dev;
6650
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006651 pm_runtime_get_sync(&pdev->dev);
6652
Francois Romieucecb5fd2011-04-01 10:21:07 +02006653 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006654 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006655
Francois Romieuda78dbf2012-01-26 14:18:23 +01006656 rtl_lock_work(tp);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006657 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006658
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006660 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661
Lekensteyn4ea72442013-07-22 09:53:30 +02006662 cancel_work_sync(&tp->wk.work);
6663
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006664 phy_disconnect(dev->phydev);
6665
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006666 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006668 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6669 tp->RxPhyAddr);
6670 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6671 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 tp->TxDescArray = NULL;
6673 tp->RxDescArray = NULL;
6674
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006675 pm_runtime_put_sync(&pdev->dev);
6676
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 return 0;
6678}
6679
Francois Romieudc1c00c2012-03-08 10:06:18 +01006680#ifdef CONFIG_NET_POLL_CONTROLLER
6681static void rtl8169_netpoll(struct net_device *dev)
6682{
6683 struct rtl8169_private *tp = netdev_priv(dev);
6684
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03006685 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01006686}
6687#endif
6688
Francois Romieudf43ac72012-03-08 09:48:40 +01006689static int rtl_open(struct net_device *dev)
6690{
6691 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006692 struct pci_dev *pdev = tp->pci_dev;
6693 int retval = -ENOMEM;
6694
6695 pm_runtime_get_sync(&pdev->dev);
6696
6697 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006698 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006699 * dma_alloc_coherent provides more.
6700 */
6701 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6702 &tp->TxPhyAddr, GFP_KERNEL);
6703 if (!tp->TxDescArray)
6704 goto err_pm_runtime_put;
6705
6706 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6707 &tp->RxPhyAddr, GFP_KERNEL);
6708 if (!tp->RxDescArray)
6709 goto err_free_tx_0;
6710
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006711 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006712 if (retval < 0)
6713 goto err_free_rx_1;
6714
6715 INIT_WORK(&tp->wk.work, rtl_task);
6716
6717 smp_mb();
6718
6719 rtl_request_firmware(tp);
6720
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006721 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006722 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01006723 if (retval < 0)
6724 goto err_release_fw_2;
6725
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006726 retval = r8169_phy_connect(tp);
6727 if (retval)
6728 goto err_free_irq;
6729
Francois Romieudf43ac72012-03-08 09:48:40 +01006730 rtl_lock_work(tp);
6731
6732 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6733
6734 napi_enable(&tp->napi);
6735
6736 rtl8169_init_phy(dev, tp);
6737
Francois Romieudf43ac72012-03-08 09:48:40 +01006738 rtl_pll_power_up(tp);
6739
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006740 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006741
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006742 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006743 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6744
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006745 phy_start(dev->phydev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006746 netif_start_queue(dev);
6747
6748 rtl_unlock_work(tp);
6749
Heiner Kallweita92a0842018-01-08 21:39:13 +01006750 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006751out:
6752 return retval;
6753
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006754err_free_irq:
6755 pci_free_irq(pdev, 0, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006756err_release_fw_2:
6757 rtl_release_firmware(tp);
6758 rtl8169_rx_clear(tp);
6759err_free_rx_1:
6760 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6761 tp->RxPhyAddr);
6762 tp->RxDescArray = NULL;
6763err_free_tx_0:
6764 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6765 tp->TxPhyAddr);
6766 tp->TxDescArray = NULL;
6767err_pm_runtime_put:
6768 pm_runtime_put_noidle(&pdev->dev);
6769 goto out;
6770}
6771
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006772static void
Junchang Wang8027aa22012-03-04 23:30:32 +01006773rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774{
6775 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006776 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02006777 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01006778 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006780 pm_runtime_get_noresume(&pdev->dev);
6781
6782 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006783 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006784
Junchang Wang8027aa22012-03-04 23:30:32 +01006785 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006786 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006787 stats->rx_packets = tp->rx_stats.packets;
6788 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006789 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006790
Junchang Wang8027aa22012-03-04 23:30:32 +01006791 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006792 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006793 stats->tx_packets = tp->tx_stats.packets;
6794 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006795 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006796
6797 stats->rx_dropped = dev->stats.rx_dropped;
6798 stats->tx_dropped = dev->stats.tx_dropped;
6799 stats->rx_length_errors = dev->stats.rx_length_errors;
6800 stats->rx_errors = dev->stats.rx_errors;
6801 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6802 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6803 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02006804 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01006805
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006806 /*
6807 * Fetch additonal counter values missing in stats collected by driver
6808 * from tally counters.
6809 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006810 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006811 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006812
6813 /*
6814 * Subtract values fetched during initalization.
6815 * See rtl8169_init_counter_offsets for a description why we do that.
6816 */
Corinna Vinschen42020322015-09-10 10:47:35 +02006817 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006818 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02006819 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006820 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02006821 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006822 le16_to_cpu(tp->tc_offset.tx_aborted);
6823
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006824 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825}
6826
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006827static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006828{
françois romieu065c27c2011-01-03 15:08:12 +00006829 struct rtl8169_private *tp = netdev_priv(dev);
6830
Francois Romieu5d06a992006-02-23 00:47:58 +01006831 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006832 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006833
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006834 phy_stop(dev->phydev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006835 netif_device_detach(dev);
6836 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006837
6838 rtl_lock_work(tp);
6839 napi_disable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006840 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006841 rtl_unlock_work(tp);
6842
6843 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006844}
Francois Romieu5d06a992006-02-23 00:47:58 +01006845
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006846#ifdef CONFIG_PM
6847
6848static int rtl8169_suspend(struct device *device)
6849{
6850 struct pci_dev *pdev = to_pci_dev(device);
6851 struct net_device *dev = pci_get_drvdata(pdev);
6852
6853 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02006854
Francois Romieu5d06a992006-02-23 00:47:58 +01006855 return 0;
6856}
6857
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006858static void __rtl8169_resume(struct net_device *dev)
6859{
françois romieu065c27c2011-01-03 15:08:12 +00006860 struct rtl8169_private *tp = netdev_priv(dev);
6861
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006862 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006863
6864 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02006865 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00006866
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006867 phy_start(tp->dev->phydev);
6868
Artem Savkovcff4c162012-04-03 10:29:11 +00006869 rtl_lock_work(tp);
6870 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006871 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006872 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006873
Francois Romieu98ddf982012-01-31 10:47:34 +01006874 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006875}
6876
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006877static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006878{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006879 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006880 struct net_device *dev = pci_get_drvdata(pdev);
6881
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006882 if (netif_running(dev))
6883 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006884
Francois Romieu5d06a992006-02-23 00:47:58 +01006885 return 0;
6886}
6887
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006888static int rtl8169_runtime_suspend(struct device *device)
6889{
6890 struct pci_dev *pdev = to_pci_dev(device);
6891 struct net_device *dev = pci_get_drvdata(pdev);
6892 struct rtl8169_private *tp = netdev_priv(dev);
6893
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006894 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006895 return 0;
6896
Francois Romieuda78dbf2012-01-26 14:18:23 +01006897 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006898 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006899 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006900
6901 rtl8169_net_suspend(dev);
6902
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006903 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006904 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006905 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006906
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006907 return 0;
6908}
6909
6910static int rtl8169_runtime_resume(struct device *device)
6911{
6912 struct pci_dev *pdev = to_pci_dev(device);
6913 struct net_device *dev = pci_get_drvdata(pdev);
6914 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08006915 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006916
6917 if (!tp->TxDescArray)
6918 return 0;
6919
Francois Romieuda78dbf2012-01-26 14:18:23 +01006920 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006921 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006922 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006923
6924 __rtl8169_resume(dev);
6925
6926 return 0;
6927}
6928
6929static int rtl8169_runtime_idle(struct device *device)
6930{
6931 struct pci_dev *pdev = to_pci_dev(device);
6932 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006933
Heiner Kallweita92a0842018-01-08 21:39:13 +01006934 if (!netif_running(dev) || !netif_carrier_ok(dev))
6935 pm_schedule_suspend(device, 10000);
6936
6937 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006938}
6939
Alexey Dobriyan47145212009-12-14 18:00:08 -08006940static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006941 .suspend = rtl8169_suspend,
6942 .resume = rtl8169_resume,
6943 .freeze = rtl8169_suspend,
6944 .thaw = rtl8169_resume,
6945 .poweroff = rtl8169_suspend,
6946 .restore = rtl8169_resume,
6947 .runtime_suspend = rtl8169_runtime_suspend,
6948 .runtime_resume = rtl8169_runtime_resume,
6949 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006950};
6951
6952#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6953
6954#else /* !CONFIG_PM */
6955
6956#define RTL8169_PM_OPS NULL
6957
6958#endif /* !CONFIG_PM */
6959
David S. Miller1805b2f2011-10-24 18:18:09 -04006960static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6961{
David S. Miller1805b2f2011-10-24 18:18:09 -04006962 /* WoL fails with 8168b when the receiver is disabled. */
6963 switch (tp->mac_version) {
6964 case RTL_GIGA_MAC_VER_11:
6965 case RTL_GIGA_MAC_VER_12:
6966 case RTL_GIGA_MAC_VER_17:
6967 pci_clear_master(tp->pci_dev);
6968
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006969 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006970 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006971 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04006972 break;
6973 default:
6974 break;
6975 }
6976}
6977
Francois Romieu1765f952008-09-13 17:21:40 +02006978static void rtl_shutdown(struct pci_dev *pdev)
6979{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006980 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00006981 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02006982
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006983 rtl8169_net_suspend(dev);
6984
Francois Romieucecb5fd2011-04-01 10:21:07 +02006985 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08006986 rtl_rar_set(tp, dev->perm_addr);
6987
Hayes Wang92fc43b2011-07-06 15:58:03 +08006988 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00006989
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006990 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02006991 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04006992 rtl_wol_suspend_quirk(tp);
6993 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00006994 }
6995
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006996 pci_wake_from_d3(pdev, true);
6997 pci_set_power_state(pdev, PCI_D3hot);
6998 }
6999}
Francois Romieu5d06a992006-02-23 00:47:58 +01007000
Bill Pembertonbaf63292012-12-03 09:23:28 -05007001static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007002{
7003 struct net_device *dev = pci_get_drvdata(pdev);
7004 struct rtl8169_private *tp = netdev_priv(dev);
7005
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007006 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007007 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007008
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007009 netif_napi_del(&tp->napi);
7010
Francois Romieue27566e2012-03-08 09:54:01 +01007011 unregister_netdev(dev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007012 mdiobus_unregister(tp->mii_bus);
Francois Romieue27566e2012-03-08 09:54:01 +01007013
7014 rtl_release_firmware(tp);
7015
7016 if (pci_dev_run_wake(pdev))
7017 pm_runtime_get_noresume(&pdev->dev);
7018
7019 /* restore original MAC address */
7020 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007021}
7022
Francois Romieufa9c3852012-03-08 10:01:50 +01007023static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007024 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007025 .ndo_stop = rtl8169_close,
7026 .ndo_get_stats64 = rtl8169_get_stats64,
7027 .ndo_start_xmit = rtl8169_start_xmit,
7028 .ndo_tx_timeout = rtl8169_tx_timeout,
7029 .ndo_validate_addr = eth_validate_addr,
7030 .ndo_change_mtu = rtl8169_change_mtu,
7031 .ndo_fix_features = rtl8169_fix_features,
7032 .ndo_set_features = rtl8169_set_features,
7033 .ndo_set_mac_address = rtl_set_mac_address,
7034 .ndo_do_ioctl = rtl8169_ioctl,
7035 .ndo_set_rx_mode = rtl_set_rx_mode,
7036#ifdef CONFIG_NET_POLL_CONTROLLER
7037 .ndo_poll_controller = rtl8169_netpoll,
7038#endif
7039
7040};
7041
Francois Romieu31fa8b12012-03-08 10:09:40 +01007042static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007043 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007044 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007045 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007046 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007047 u8 default_ver;
7048} rtl_cfg_infos [] = {
7049 [RTL_CFG_0] = {
7050 .hw_start = rtl_hw_start_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007051 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007052 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007053 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007054 .default_ver = RTL_GIGA_MAC_VER_01,
7055 },
7056 [RTL_CFG_1] = {
7057 .hw_start = rtl_hw_start_8168,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007058 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007059 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007060 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007061 .default_ver = RTL_GIGA_MAC_VER_11,
7062 },
7063 [RTL_CFG_2] = {
7064 .hw_start = rtl_hw_start_8101,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007065 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7066 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03007067 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007068 .default_ver = RTL_GIGA_MAC_VER_13,
7069 }
7070};
7071
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007072static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01007073{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007074 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007075
Jian-Hong Pan7bb05b82018-08-17 13:07:35 +08007076 switch (tp->mac_version) {
7077 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007078 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7079 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7080 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007081 flags = PCI_IRQ_LEGACY;
Jian-Hong Pan7bb05b82018-08-17 13:07:35 +08007082 break;
7083 case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_40:
Heiner Kallweit7c53a722018-08-12 13:26:26 +02007084 /* This version was reported to have issues with resume
7085 * from suspend when using MSI-X
7086 */
7087 flags = PCI_IRQ_LEGACY | PCI_IRQ_MSI;
Jian-Hong Pan7bb05b82018-08-17 13:07:35 +08007088 break;
7089 default:
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007090 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007091 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007092
7093 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007094}
7095
Hayes Wangc5583862012-07-02 17:23:22 +08007096DECLARE_RTL_COND(rtl_link_list_ready_cond)
7097{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007098 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007099}
7100
7101DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7102{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007103 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007104}
7105
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007106static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
7107{
7108 struct rtl8169_private *tp = mii_bus->priv;
7109
7110 if (phyaddr > 0)
7111 return -ENODEV;
7112
7113 return rtl_readphy(tp, phyreg);
7114}
7115
7116static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
7117 int phyreg, u16 val)
7118{
7119 struct rtl8169_private *tp = mii_bus->priv;
7120
7121 if (phyaddr > 0)
7122 return -ENODEV;
7123
7124 rtl_writephy(tp, phyreg, val);
7125
7126 return 0;
7127}
7128
7129static int r8169_mdio_register(struct rtl8169_private *tp)
7130{
7131 struct pci_dev *pdev = tp->pci_dev;
7132 struct phy_device *phydev;
7133 struct mii_bus *new_bus;
7134 int ret;
7135
7136 new_bus = devm_mdiobus_alloc(&pdev->dev);
7137 if (!new_bus)
7138 return -ENOMEM;
7139
7140 new_bus->name = "r8169";
7141 new_bus->priv = tp;
7142 new_bus->parent = &pdev->dev;
7143 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
7144 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
7145 PCI_DEVID(pdev->bus->number, pdev->devfn));
7146
7147 new_bus->read = r8169_mdio_read_reg;
7148 new_bus->write = r8169_mdio_write_reg;
7149
7150 ret = mdiobus_register(new_bus);
7151 if (ret)
7152 return ret;
7153
7154 phydev = mdiobus_get_phy(new_bus, 0);
7155 if (!phydev) {
7156 mdiobus_unregister(new_bus);
7157 return -ENODEV;
7158 }
7159
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02007160 /* PHY will be woken up in rtl_open() */
7161 phy_suspend(phydev);
7162
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007163 tp->mii_bus = new_bus;
7164
7165 return 0;
7166}
7167
Bill Pembertonbaf63292012-12-03 09:23:28 -05007168static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007169{
Hayes Wangc5583862012-07-02 17:23:22 +08007170 u32 data;
7171
7172 tp->ocp_base = OCP_STD_PHY_BASE;
7173
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007174 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007175
7176 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7177 return;
7178
7179 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7180 return;
7181
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007182 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007183 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007184 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007185
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007186 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007187 data &= ~(1 << 14);
7188 r8168_mac_ocp_write(tp, 0xe8de, data);
7189
7190 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7191 return;
7192
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007193 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007194 data |= (1 << 15);
7195 r8168_mac_ocp_write(tp, 0xe8de, data);
7196
7197 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7198 return;
7199}
7200
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007201static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7202{
7203 rtl8168ep_stop_cmac(tp);
7204 rtl_hw_init_8168g(tp);
7205}
7206
Bill Pembertonbaf63292012-12-03 09:23:28 -05007207static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007208{
7209 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007210 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007211 rtl_hw_init_8168g(tp);
7212 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007213 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007214 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007215 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007216 default:
7217 break;
7218 }
7219}
7220
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007221/* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
7222static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
7223{
7224 switch (tp->mac_version) {
7225 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7226 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
7227 return false;
7228 default:
7229 return true;
7230 }
7231}
7232
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007233static int rtl_jumbo_max(struct rtl8169_private *tp)
7234{
7235 /* Non-GBit versions don't support jumbo frames */
7236 if (!tp->supports_gmii)
7237 return JUMBO_1K;
7238
7239 switch (tp->mac_version) {
7240 /* RTL8169 */
7241 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7242 return JUMBO_7K;
7243 /* RTL8168b */
7244 case RTL_GIGA_MAC_VER_11:
7245 case RTL_GIGA_MAC_VER_12:
7246 case RTL_GIGA_MAC_VER_17:
7247 return JUMBO_4K;
7248 /* RTL8168c */
7249 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
7250 return JUMBO_6K;
7251 default:
7252 return JUMBO_9K;
7253 }
7254}
7255
hayeswang929a0312014-09-16 11:40:47 +08007256static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007257{
7258 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007259 struct rtl8169_private *tp;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007260 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007261 int chipset, region, i;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007262 int jumbo_max, rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007263
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007264 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7265 if (!dev)
7266 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007267
7268 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007269 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007270 tp = netdev_priv(dev);
7271 tp->dev = dev;
7272 tp->pci_dev = pdev;
7273 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02007274 tp->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007275
Francois Romieu3b6cf252012-03-08 09:59:04 +01007276 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007277 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007278 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007279 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007280 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007281 }
7282
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007283 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007284 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007285
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007286 /* use first MMIO region */
7287 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7288 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007289 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007290 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007291 }
7292
7293 /* check for weird/broken PCI region reporting */
7294 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007295 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007296 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007297 }
7298
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007299 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007300 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007301 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007302 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007303 }
7304
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007305 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007306
7307 if (!pci_is_pcie(pdev))
Heiner Kallweit22148df2018-04-22 17:15:15 +02007308 dev_info(&pdev->dev, "not PCI Express\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007309
7310 /* Identify chip attached to board */
Heiner Kallweit22148df2018-04-22 17:15:15 +02007311 rtl8169_get_mac_version(tp, cfg->default_ver);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007312
Heiner Kallweite3972862018-06-29 08:07:04 +02007313 if (rtl_tbi_enabled(tp)) {
7314 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7315 return -ENODEV;
7316 }
7317
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007318 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007319
7320 if ((sizeof(dma_addr_t) > 4) &&
7321 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7322 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01007323 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7324 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007325
7326 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7327 if (!pci_is_pcie(pdev))
7328 tp->cp_cmd |= PCIDAC;
7329 dev->features |= NETIF_F_HIGHDMA;
7330 } else {
7331 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7332 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007333 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007334 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007335 }
7336 }
7337
Francois Romieu3b6cf252012-03-08 09:59:04 +01007338 rtl_init_rxcfg(tp);
7339
7340 rtl_irq_disable(tp);
7341
Hayes Wangc5583862012-07-02 17:23:22 +08007342 rtl_hw_initialize(tp);
7343
Francois Romieu3b6cf252012-03-08 09:59:04 +01007344 rtl_hw_reset(tp);
7345
7346 rtl_ack_events(tp, 0xffff);
7347
7348 pci_set_master(pdev);
7349
Francois Romieu3b6cf252012-03-08 09:59:04 +01007350 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007351 rtl_init_jumbo_ops(tp);
7352
7353 rtl8169_print_mac_version(tp);
7354
7355 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007356
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007357 rc = rtl_alloc_irq(tp);
7358 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007359 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007360 return rc;
7361 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007362
Heiner Kallweit18041b52018-07-24 22:21:04 +02007363 tp->saved_wolopts = __rtl8169_get_wol(tp);
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01007364
Francois Romieu3b6cf252012-03-08 09:59:04 +01007365 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007366 u64_stats_init(&tp->rx_stats.syncp);
7367 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007368
7369 /* Get MAC address */
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007370 switch (tp->mac_version) {
Heiner Kallweit353af852018-05-02 21:39:59 +02007371 u8 mac_addr[ETH_ALEN] __aligned(4);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007372 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7373 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08007374 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
Heiner Kallweit353af852018-05-02 21:39:59 +02007375 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007376
Heiner Kallweit353af852018-05-02 21:39:59 +02007377 if (is_valid_ether_addr(mac_addr))
7378 rtl_rar_set(tp, mac_addr);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007379 break;
7380 default:
7381 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007382 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007383 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007384 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007385
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007386 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007387 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007388
Heiner Kallweit37621492018-04-17 23:20:03 +02007389 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007390
7391 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7392 * properly for all devices */
7393 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007394 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007395
7396 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007397 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7398 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007399 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7400 NETIF_F_HIGHDMA;
Heiner Kallweit2d0ec542018-07-02 22:49:35 +02007401 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007402
hayeswang929a0312014-09-16 11:40:47 +08007403 tp->cp_cmd |= RxChkSum | RxVlan;
7404
7405 /*
7406 * Pretend we are using VLANs; This bypasses a nasty bug where
7407 * Interrupts stop flowing on high load on 8110SCd controllers.
7408 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007409 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007410 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007411 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007412
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007413 if (rtl_chip_supports_csum_v2(tp)) {
hayeswang5888d3f2014-07-11 16:25:56 +08007414 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007415 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007416 } else {
7417 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007418 }
hayeswang5888d3f2014-07-11 16:25:56 +08007419
Francois Romieu3b6cf252012-03-08 09:59:04 +01007420 dev->hw_features |= NETIF_F_RXALL;
7421 dev->hw_features |= NETIF_F_RXFCS;
7422
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007423 /* MTU range: 60 - hw-specific max */
7424 dev->min_mtu = ETH_ZLEN;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007425 jumbo_max = rtl_jumbo_max(tp);
7426 dev->max_mtu = jumbo_max;
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007427
Francois Romieu3b6cf252012-03-08 09:59:04 +01007428 tp->hw_start = cfg->hw_start;
7429 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03007430 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007431
Francois Romieu3b6cf252012-03-08 09:59:04 +01007432 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7433
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007434 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7435 &tp->counters_phys_addr,
7436 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007437 if (!tp->counters)
7438 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02007439
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02007440 pci_set_drvdata(pdev, dev);
7441
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007442 rc = r8169_mdio_register(tp);
7443 if (rc)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007444 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007445
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02007446 /* chip gets powered up in rtl_open() */
7447 rtl_pll_power_down(tp);
7448
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007449 rc = register_netdev(dev);
7450 if (rc)
7451 goto err_mdio_unregister;
7452
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02007453 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7454 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit90b989c2018-04-17 23:32:15 +02007455 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01007456 pci_irq_vector(pdev, 0));
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007457
7458 if (jumbo_max > JUMBO_1K)
7459 netif_info(tp, probe, dev,
7460 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
7461 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
7462 "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007463
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007464 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01007465 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007466
Heiner Kallweita92a0842018-01-08 21:39:13 +01007467 if (pci_dev_run_wake(pdev))
7468 pm_runtime_put_sync(&pdev->dev);
7469
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007470 return 0;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007471
7472err_mdio_unregister:
7473 mdiobus_unregister(tp->mii_bus);
7474 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007475}
7476
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477static struct pci_driver rtl8169_pci_driver = {
7478 .name = MODULENAME,
7479 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007480 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007481 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007482 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007483 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484};
7485
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007486module_pci_driver(rtl8169_pci_driver);