blob: 9a5e2969df6197cd3383e263b2336dca5faa1a2d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
Hans de Goedec2f6f3e2018-09-12 11:34:55 +020016#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/delay.h>
18#include <linux/ethtool.h>
Heiner Kallweitf1e911d2018-07-17 22:51:26 +020019#include <linux/phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/if_vlan.h>
21#include <linux/crc32.h>
22#include <linux/in.h>
Heiner Kallweit098b01a2018-08-10 22:37:31 +020023#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/ip.h>
25#include <linux/tcp.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000026#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000028#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000029#include <linux/firmware.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
hayeswange9746042014-07-11 16:25:58 +080031#include <linux/ipv6.h>
32#include <net/ip6_checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define MODULENAME "r8169"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
françois romieubca03d52011-01-03 15:07:31 +000036#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
37#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000038#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
39#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080040#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wangc2218922011-09-06 16:55:18 +080041#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
42#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080043#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
Hayes Wang7e18dca2012-03-30 14:33:02 +080044#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
Hayes Wangb3d7b2f2012-03-30 14:48:06 +080045#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
hayeswang45dd95c2013-07-08 17:09:01 +080046#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
Hayes Wang5598bfe2012-07-02 17:23:21 +080047#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
hayeswang58152cd2013-04-01 22:23:42 +000048#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
hayeswangbeb330a2013-04-01 22:23:39 +000049#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
hayeswang57538c42013-04-01 22:23:40 +000050#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +080051#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
52#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
53#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
54#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
françois romieubca03d52011-01-03 15:07:31 +000055
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020056#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d92005-09-30 16:54:02 -070057 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020058
Julien Ducourthial477206a2012-05-09 00:00:06 +020059#define TX_SLOTS_AVAIL(tp) \
60 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
61
62/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
63#define TX_FRAGS_READY_FOR(tp,nr_frags) \
64 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
67 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050068static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Michal Schmidtaee77e42012-09-09 13:55:26 +000070#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
72
73#define R8169_REGS_SIZE 256
Heiner Kallweit1d0254d2018-04-17 23:25:46 +020074#define R8169_RX_BUF_SIZE (SZ_16K - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
Timo Teräs9fba0812013-01-15 21:01:24 +000076#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
78#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
79
80#define RTL8169_TX_TIMEOUT (6*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* write/read MMIO register */
Andy Shevchenko1ef72862018-03-01 13:27:34 +020083#define RTL_W8(tp, reg, val8) writeb((val8), tp->mmio_addr + (reg))
84#define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
85#define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
86#define RTL_R8(tp, reg) readb(tp->mmio_addr + (reg))
87#define RTL_R16(tp, reg) readw(tp->mmio_addr + (reg))
88#define RTL_R32(tp, reg) readl(tp->mmio_addr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +020091 RTL_GIGA_MAC_VER_01 = 0,
92 RTL_GIGA_MAC_VER_02,
93 RTL_GIGA_MAC_VER_03,
94 RTL_GIGA_MAC_VER_04,
95 RTL_GIGA_MAC_VER_05,
96 RTL_GIGA_MAC_VER_06,
97 RTL_GIGA_MAC_VER_07,
98 RTL_GIGA_MAC_VER_08,
99 RTL_GIGA_MAC_VER_09,
100 RTL_GIGA_MAC_VER_10,
101 RTL_GIGA_MAC_VER_11,
102 RTL_GIGA_MAC_VER_12,
103 RTL_GIGA_MAC_VER_13,
104 RTL_GIGA_MAC_VER_14,
105 RTL_GIGA_MAC_VER_15,
106 RTL_GIGA_MAC_VER_16,
107 RTL_GIGA_MAC_VER_17,
108 RTL_GIGA_MAC_VER_18,
109 RTL_GIGA_MAC_VER_19,
110 RTL_GIGA_MAC_VER_20,
111 RTL_GIGA_MAC_VER_21,
112 RTL_GIGA_MAC_VER_22,
113 RTL_GIGA_MAC_VER_23,
114 RTL_GIGA_MAC_VER_24,
115 RTL_GIGA_MAC_VER_25,
116 RTL_GIGA_MAC_VER_26,
117 RTL_GIGA_MAC_VER_27,
118 RTL_GIGA_MAC_VER_28,
119 RTL_GIGA_MAC_VER_29,
120 RTL_GIGA_MAC_VER_30,
121 RTL_GIGA_MAC_VER_31,
122 RTL_GIGA_MAC_VER_32,
123 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800124 RTL_GIGA_MAC_VER_34,
Hayes Wangc2218922011-09-06 16:55:18 +0800125 RTL_GIGA_MAC_VER_35,
126 RTL_GIGA_MAC_VER_36,
Hayes Wang7e18dca2012-03-30 14:33:02 +0800127 RTL_GIGA_MAC_VER_37,
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800128 RTL_GIGA_MAC_VER_38,
Hayes Wang5598bfe2012-07-02 17:23:21 +0800129 RTL_GIGA_MAC_VER_39,
Hayes Wangc5583862012-07-02 17:23:22 +0800130 RTL_GIGA_MAC_VER_40,
131 RTL_GIGA_MAC_VER_41,
hayeswang57538c42013-04-01 22:23:40 +0000132 RTL_GIGA_MAC_VER_42,
hayeswang58152cd2013-04-01 22:23:42 +0000133 RTL_GIGA_MAC_VER_43,
hayeswang45dd95c2013-07-08 17:09:01 +0800134 RTL_GIGA_MAC_VER_44,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800135 RTL_GIGA_MAC_VER_45,
136 RTL_GIGA_MAC_VER_46,
137 RTL_GIGA_MAC_VER_47,
138 RTL_GIGA_MAC_VER_48,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800139 RTL_GIGA_MAC_VER_49,
140 RTL_GIGA_MAC_VER_50,
141 RTL_GIGA_MAC_VER_51,
Francois Romieu85bffe62011-04-27 08:22:39 +0200142 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Francois Romieud58d46b2011-05-03 16:38:29 +0200145#define JUMBO_1K ETH_DATA_LEN
146#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
147#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
148#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
149#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
150
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800151static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 const char *name;
Francois Romieu85bffe62011-04-27 08:22:39 +0200153 const char *fw_name;
154} rtl_chip_infos[] = {
155 /* PCI devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200156 [RTL_GIGA_MAC_VER_01] = {"RTL8169" },
157 [RTL_GIGA_MAC_VER_02] = {"RTL8169s" },
158 [RTL_GIGA_MAC_VER_03] = {"RTL8110s" },
159 [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb" },
160 [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc" },
161 [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc" },
Francois Romieu85bffe62011-04-27 08:22:39 +0200162 /* PCI-E devices. */
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +0200163 [RTL_GIGA_MAC_VER_07] = {"RTL8102e" },
164 [RTL_GIGA_MAC_VER_08] = {"RTL8102e" },
165 [RTL_GIGA_MAC_VER_09] = {"RTL8102e" },
166 [RTL_GIGA_MAC_VER_10] = {"RTL8101e" },
167 [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b" },
168 [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b" },
169 [RTL_GIGA_MAC_VER_13] = {"RTL8101e" },
170 [RTL_GIGA_MAC_VER_14] = {"RTL8100e" },
171 [RTL_GIGA_MAC_VER_15] = {"RTL8100e" },
172 [RTL_GIGA_MAC_VER_16] = {"RTL8101e" },
173 [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b" },
174 [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp" },
175 [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c" },
176 [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c" },
177 [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c" },
178 [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c" },
179 [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp" },
180 [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp" },
181 [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d", FIRMWARE_8168D_1},
182 [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d", FIRMWARE_8168D_2},
183 [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp" },
184 [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp" },
185 [RTL_GIGA_MAC_VER_29] = {"RTL8105e", FIRMWARE_8105E_1},
186 [RTL_GIGA_MAC_VER_30] = {"RTL8105e", FIRMWARE_8105E_1},
187 [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp" },
188 [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e", FIRMWARE_8168E_1},
189 [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e", FIRMWARE_8168E_2},
190 [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl", FIRMWARE_8168E_3},
191 [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f", FIRMWARE_8168F_1},
192 [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f", FIRMWARE_8168F_2},
193 [RTL_GIGA_MAC_VER_37] = {"RTL8402", FIRMWARE_8402_1 },
194 [RTL_GIGA_MAC_VER_38] = {"RTL8411", FIRMWARE_8411_1 },
195 [RTL_GIGA_MAC_VER_39] = {"RTL8106e", FIRMWARE_8106E_1},
196 [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g", FIRMWARE_8168G_2},
197 [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g" },
198 [RTL_GIGA_MAC_VER_42] = {"RTL8168g/8111g", FIRMWARE_8168G_3},
199 [RTL_GIGA_MAC_VER_43] = {"RTL8106e", FIRMWARE_8106E_2},
200 [RTL_GIGA_MAC_VER_44] = {"RTL8411", FIRMWARE_8411_2 },
201 [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h", FIRMWARE_8168H_1},
202 [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h", FIRMWARE_8168H_2},
203 [RTL_GIGA_MAC_VER_47] = {"RTL8107e", FIRMWARE_8107E_1},
204 [RTL_GIGA_MAC_VER_48] = {"RTL8107e", FIRMWARE_8107E_2},
205 [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep" },
206 [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep" },
207 [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Francois Romieubcf0bf92006-07-26 23:14:13 +0200210enum cfg_version {
211 RTL_CFG_0 = 0x00,
212 RTL_CFG_1,
213 RTL_CFG_2
214};
215
Benoit Taine9baa3c32014-08-08 15:56:03 +0200216static const struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200217 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200218 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Chun-Hao Lin610c9082016-12-27 16:29:43 +0800219 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
Francois Romieud81bf552006-09-20 21:31:20 +0200220 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100221 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Anthony Wong9fd0e092018-08-31 20:06:42 +0800222 { PCI_DEVICE(PCI_VENDOR_ID_NCUBE, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200223 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
Francois Romieu2a35cfa2012-08-31 23:06:17 +0200224 { PCI_VENDOR_ID_DLINK, 0x4300,
225 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200226 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000227 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200228 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200229 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
230 { PCI_VENDOR_ID_LINKSYS, 0x1032,
231 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100232 { 0x0001, 0x8168,
233 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 {0,},
235};
236
237MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
238
Ard Biesheuvel27896c82016-05-14 22:40:15 +0200239static int use_dac = -1;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200240static struct {
241 u32 msg_enable;
242} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Francois Romieu07d3f512007-02-21 22:40:46 +0100244enum rtl_registers {
245 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100246 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100247 MAR0 = 8, /* Multicast filter. */
248 CounterAddrLow = 0x10,
249 CounterAddrHigh = 0x14,
250 TxDescStartAddrLow = 0x20,
251 TxDescStartAddrHigh = 0x24,
252 TxHDescStartAddrLow = 0x28,
253 TxHDescStartAddrHigh = 0x2c,
254 FLASH = 0x30,
255 ERSR = 0x36,
256 ChipCmd = 0x37,
257 TxPoll = 0x38,
258 IntrMask = 0x3c,
259 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700260
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800261 TxConfig = 0x40,
262#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
263#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
264
265 RxConfig = 0x44,
266#define RX128_INT_EN (1 << 15) /* 8111c and later */
267#define RX_MULTI_EN (1 << 14) /* 8111c only */
268#define RXCFG_FIFO_SHIFT 13
269 /* No threshold before first PCI xfer */
270#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
hayeswangbeb330a2013-04-01 22:23:39 +0000271#define RX_EARLY_OFF (1 << 11)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800272#define RXCFG_DMA_SHIFT 8
273 /* Unlimited maximum PCI burst. */
274#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700275
Francois Romieu07d3f512007-02-21 22:40:46 +0100276 RxMissed = 0x4c,
277 Cfg9346 = 0x50,
278 Config0 = 0x51,
279 Config1 = 0x52,
280 Config2 = 0x53,
Francois Romieud387b422012-04-17 11:12:01 +0200281#define PME_SIGNAL (1 << 5) /* 8168c and later */
282
Francois Romieu07d3f512007-02-21 22:40:46 +0100283 Config3 = 0x54,
284 Config4 = 0x55,
285 Config5 = 0x56,
286 MultiIntr = 0x5c,
287 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100288 PHYstatus = 0x6c,
289 RxMaxSize = 0xda,
290 CPlusCmd = 0xe0,
291 IntrMitigate = 0xe2,
Francois Romieu50970832017-10-27 13:24:49 +0300292
293#define RTL_COALESCE_MASK 0x0f
294#define RTL_COALESCE_SHIFT 4
295#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
296#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
297
Francois Romieu07d3f512007-02-21 22:40:46 +0100298 RxDescAddrLow = 0xe4,
299 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000300 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
301
302#define NoEarlyTx 0x3f /* Max value : no early transmit. */
303
304 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
305
306#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800307#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000308
Francois Romieu07d3f512007-02-21 22:40:46 +0100309 FuncEvent = 0xf0,
310 FuncEventMask = 0xf4,
311 FuncPresetState = 0xf8,
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800312 IBCR0 = 0xf8,
313 IBCR2 = 0xf9,
314 IBIMR0 = 0xfa,
315 IBISR0 = 0xfb,
Francois Romieu07d3f512007-02-21 22:40:46 +0100316 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317};
318
Francois Romieuf162a5d2008-06-01 22:37:49 +0200319enum rtl8168_8101_registers {
320 CSIDR = 0x64,
321 CSIAR = 0x68,
322#define CSIAR_FLAG 0x80000000
323#define CSIAR_WRITE_CMD 0x80000000
Heiner Kallweitff1d7332018-05-02 21:39:56 +0200324#define CSIAR_BYTE_ENABLE 0x0000f000
325#define CSIAR_ADDR_MASK 0x00000fff
françois romieu065c27c2011-01-03 15:08:12 +0000326 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200327 EPHYAR = 0x80,
328#define EPHYAR_FLAG 0x80000000
329#define EPHYAR_WRITE_CMD 0x80000000
330#define EPHYAR_REG_MASK 0x1f
331#define EPHYAR_REG_SHIFT 16
332#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800333 DLLPR = 0xd0,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800334#define PFM_EN (1 << 6)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800335#define TX_10M_PS_EN (1 << 7)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200336 DBG_REG = 0xd1,
337#define FIX_NAK_1 (1 << 4)
338#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800339 TWSI = 0xd2,
340 MCU = 0xd3,
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800341#define NOW_IS_OOB (1 << 7)
Hayes Wangc5583862012-07-02 17:23:22 +0800342#define TX_EMPTY (1 << 5)
343#define RX_EMPTY (1 << 4)
344#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800345#define EN_NDP (1 << 3)
346#define EN_OOB_RESET (1 << 2)
Hayes Wangc5583862012-07-02 17:23:22 +0800347#define LINK_LIST_RDY (1 << 1)
françois romieudaf9df62009-10-07 12:44:20 +0000348 EFUSEAR = 0xdc,
349#define EFUSEAR_FLAG 0x80000000
350#define EFUSEAR_WRITE_CMD 0x80000000
351#define EFUSEAR_READ_CMD 0x00000000
352#define EFUSEAR_REG_MASK 0x03ff
353#define EFUSEAR_REG_SHIFT 8
354#define EFUSEAR_DATA_MASK 0xff
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800355 MISC_1 = 0xf2,
356#define PFM_D3COLD_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200357};
358
françois romieuc0e45c12011-01-03 15:08:04 +0000359enum rtl8168_registers {
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800360 LED_FREQ = 0x1a,
361 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000362 ERIDR = 0x70,
363 ERIAR = 0x74,
364#define ERIAR_FLAG 0x80000000
365#define ERIAR_WRITE_CMD 0x80000000
366#define ERIAR_READ_CMD 0x00000000
367#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000368#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800369#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
370#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
371#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800372#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800373#define ERIAR_MASK_SHIFT 12
374#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
375#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800376#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
Hayes Wangc5583862012-07-02 17:23:22 +0800377#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800378#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000379 EPHY_RXER_NUM = 0x7c,
380 OCPDR = 0xb0, /* OCP GPHY access */
381#define OCPDR_WRITE_CMD 0x80000000
382#define OCPDR_READ_CMD 0x00000000
383#define OCPDR_REG_MASK 0x7f
384#define OCPDR_GPHY_REG_SHIFT 16
385#define OCPDR_DATA_MASK 0xffff
386 OCPAR = 0xb4,
387#define OCPAR_FLAG 0x80000000
388#define OCPAR_GPHY_WRITE_CMD 0x8000f060
389#define OCPAR_GPHY_READ_CMD 0x0000f060
Hayes Wangc5583862012-07-02 17:23:22 +0800390 GPHY_OCP = 0xb8,
hayeswang01dc7fe2011-03-21 01:50:28 +0000391 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
392 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200393#define TXPLA_RST (1 << 29)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800394#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800395#define PWM_EN (1 << 22)
Hayes Wangc5583862012-07-02 17:23:22 +0800396#define RXDV_GATED_EN (1 << 19)
Hayes Wang5598bfe2012-07-02 17:23:21 +0800397#define EARLY_TALLY_EN (1 << 16)
françois romieuc0e45c12011-01-03 15:08:04 +0000398};
399
Francois Romieu07d3f512007-02-21 22:40:46 +0100400enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100402 SYSErr = 0x8000,
403 PCSTimeout = 0x4000,
404 SWInt = 0x0100,
405 TxDescUnavail = 0x0080,
406 RxFIFOOver = 0x0040,
407 LinkChg = 0x0020,
408 RxOverflow = 0x0010,
409 TxErr = 0x0008,
410 TxOK = 0x0004,
411 RxErr = 0x0002,
412 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400415 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200416 RxFOVF = (1 << 23),
417 RxRWT = (1 << 22),
418 RxRES = (1 << 21),
419 RxRUNT = (1 << 20),
420 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /* ChipCmdBits */
Hayes Wang4f6b00e52011-07-06 15:58:02 +0800423 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100424 CmdReset = 0x10,
425 CmdRxEnb = 0x08,
426 CmdTxEnb = 0x04,
427 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Francois Romieu275391a2007-02-23 23:50:28 +0100429 /* TXPoll register p.5 */
430 HPQ = 0x80, /* Poll cmd on the high prio queue */
431 NPQ = 0x40, /* Poll cmd on the low prio queue */
432 FSWInt = 0x01, /* Forced software interrupt */
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100435 Cfg9346_Lock = 0x00,
436 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100439 AcceptErr = 0x20,
440 AcceptRunt = 0x10,
441 AcceptBroadcast = 0x08,
442 AcceptMulticast = 0x04,
443 AcceptMyPhys = 0x02,
444 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200445#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* TxConfigBits */
448 TxInterFrameGapShift = 24,
449 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
450
Francois Romieu5d06a992006-02-23 00:47:58 +0100451 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200452 LEDS1 = (1 << 7),
453 LEDS0 = (1 << 6),
Francois Romieuf162a5d2008-06-01 22:37:49 +0200454 Speed_down = (1 << 4),
455 MEMMAP = (1 << 3),
456 IOMAP = (1 << 2),
457 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100458 PMEnable = (1 << 0), /* Power Management Enable */
459
Francois Romieu6dccd162007-02-13 23:38:05 +0100460 /* Config2 register p. 25 */
hayeswang57538c42013-04-01 22:23:40 +0000461 ClkReqEn = (1 << 7), /* Clock Request Enable */
françois romieu2ca6cf02011-12-15 08:37:43 +0000462 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
Francois Romieu6dccd162007-02-13 23:38:05 +0100463 PCI_Clock_66MHz = 0x01,
464 PCI_Clock_33MHz = 0x00,
465
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100466 /* Config3 register p.25 */
467 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
468 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieud58d46b2011-05-03 16:38:29 +0200469 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
hayeswangb51ecea2014-07-09 14:52:51 +0800470 Rdy_to_L23 = (1 << 1), /* L23 Enable */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200471 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100472
Francois Romieud58d46b2011-05-03 16:38:29 +0200473 /* Config4 register */
474 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
475
Francois Romieu5d06a992006-02-23 00:47:58 +0100476 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100477 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
478 MWF = (1 << 5), /* Accept Multicast wakeup frame */
479 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200480 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100481 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100482 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
hayeswang57538c42013-04-01 22:23:40 +0000483 ASPM_en = (1 << 0), /* ASPM enable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200486 EnableBist = (1 << 15), // 8168 8101
487 Mac_dbgo_oe = (1 << 14), // 8168 8101
488 Normal_mode = (1 << 13), // unused
489 Force_half_dup = (1 << 12), // 8168 8101
490 Force_rxflow_en = (1 << 11), // 8168 8101
491 Force_txflow_en = (1 << 10), // 8168 8101
492 Cxpl_dbg_sel = (1 << 9), // 8168 8101
493 ASF = (1 << 8), // 8168 8101
494 PktCntrDisable = (1 << 7), // 8168 8101
495 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 RxVlan = (1 << 6),
497 RxChkSum = (1 << 5),
498 PCIDAC = (1 << 4),
499 PCIMulRW = (1 << 3),
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +0200500#define INTT_MASK GENMASK(1, 0)
Francois Romieu0e485152007-02-20 00:00:26 +0100501 INTT_0 = 0x0000, // 8168
502 INTT_1 = 0x0001, // 8168
503 INTT_2 = 0x0002, // 8168
504 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100507 TBI_Enable = 0x80,
508 TxFlowCtrl = 0x40,
509 RxFlowCtrl = 0x20,
510 _1000bpsF = 0x10,
511 _100bps = 0x08,
512 _10bps = 0x04,
513 LinkStatus = 0x02,
514 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100517 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200518
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200519 /* ResetCounterCommand */
520 CounterReset = 0x1,
521
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200522 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100523 CounterDump = 0x8,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800524
525 /* magic enable v2 */
526 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527};
528
Francois Romieu2b7b4312011-04-18 22:53:24 -0700529enum rtl_desc_bit {
530 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
532 RingEnd = (1 << 30), /* End of descriptor ring */
533 FirstFrag = (1 << 29), /* First segment of a packet */
534 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700535};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Francois Romieu2b7b4312011-04-18 22:53:24 -0700537/* Generic case. */
538enum rtl_tx_desc_bit {
539 /* First doubleword. */
540 TD_LSO = (1 << 27), /* Large Send Offload */
541#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Francois Romieu2b7b4312011-04-18 22:53:24 -0700543 /* Second doubleword. */
544 TxVlanTag = (1 << 17), /* Add VLAN tag */
545};
546
547/* 8169, 8168b and 810x except 8102e. */
548enum rtl_tx_desc_bit_0 {
549 /* First doubleword. */
550#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
551 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
552 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
553 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
554};
555
556/* 8102e, 8168c and beyond. */
557enum rtl_tx_desc_bit_1 {
hayeswangbdfa4ed2014-07-11 16:25:57 +0800558 /* First doubleword. */
559 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
hayeswange9746042014-07-11 16:25:58 +0800560 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
hayeswangbdfa4ed2014-07-11 16:25:57 +0800561#define GTTCPHO_SHIFT 18
hayeswange9746042014-07-11 16:25:58 +0800562#define GTTCPHO_MAX 0x7fU
hayeswangbdfa4ed2014-07-11 16:25:57 +0800563
Francois Romieu2b7b4312011-04-18 22:53:24 -0700564 /* Second doubleword. */
hayeswange9746042014-07-11 16:25:58 +0800565#define TCPHO_SHIFT 18
566#define TCPHO_MAX 0x3ffU
Francois Romieu2b7b4312011-04-18 22:53:24 -0700567#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
hayeswange9746042014-07-11 16:25:58 +0800568 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
569 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700570 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
571 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
572};
573
Francois Romieu2b7b4312011-04-18 22:53:24 -0700574enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* Rx private */
576 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
Zhu Yanjun9b600472017-01-05 02:54:27 -0500577 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579#define RxProtoUDP (PID1)
580#define RxProtoTCP (PID0)
581#define RxProtoIP (PID1 | PID0)
582#define RxProtoMask RxProtoIP
583
584 IPFail = (1 << 16), /* IP checksum failed */
585 UDPFail = (1 << 15), /* UDP/IP checksum failed */
586 TCPFail = (1 << 14), /* TCP/IP checksum failed */
587 RxVlanTag = (1 << 16), /* VLAN tag available */
588};
589
590#define RsvdMask 0x3fffc000
Heiner Kallweit12d42c52018-04-28 22:19:30 +0200591#define CPCMD_QUIRK_MASK (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200594 __le32 opts1;
595 __le32 opts2;
596 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597};
598
599struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200600 __le32 opts1;
601 __le32 opts2;
602 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603};
604
605struct ring_info {
606 struct sk_buff *skb;
607 u32 len;
608 u8 __pad[sizeof(void *) - sizeof(u32)];
609};
610
Ivan Vecera355423d2009-02-06 21:49:57 -0800611struct rtl8169_counters {
612 __le64 tx_packets;
613 __le64 rx_packets;
614 __le64 tx_errors;
615 __le32 rx_errors;
616 __le16 rx_missed;
617 __le16 align_errors;
618 __le32 tx_one_collision;
619 __le32 tx_multi_collision;
620 __le64 rx_unicast;
621 __le64 rx_broadcast;
622 __le32 rx_multicast;
623 __le16 tx_aborted;
624 __le16 tx_underun;
625};
626
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200627struct rtl8169_tc_offsets {
628 bool inited;
629 __le64 tx_errors;
630 __le32 tx_multi_collision;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200631 __le16 tx_aborted;
632};
633
Francois Romieuda78dbf2012-01-26 14:18:23 +0100634enum rtl_flag {
Kai-Heng Feng6ad56902018-09-11 01:51:43 +0800635 RTL_FLAG_TASK_ENABLED = 0,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100636 RTL_FLAG_TASK_SLOW_PENDING,
637 RTL_FLAG_TASK_RESET_PENDING,
Francois Romieuda78dbf2012-01-26 14:18:23 +0100638 RTL_FLAG_MAX
639};
640
Junchang Wang8027aa22012-03-04 23:30:32 +0100641struct rtl8169_stats {
642 u64 packets;
643 u64 bytes;
644 struct u64_stats_sync syncp;
645};
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647struct rtl8169_private {
648 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200649 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000650 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700651 struct napi_struct napi;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200652 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700653 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
655 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 u32 dirty_tx;
Junchang Wang8027aa22012-03-04 23:30:32 +0100657 struct rtl8169_stats rx_stats;
658 struct rtl8169_stats tx_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
660 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
661 dma_addr_t TxPhyAddr;
662 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000663 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 u16 cp_cmd;
Francois Romieuda78dbf2012-01-26 14:18:23 +0100666
667 u16 event_slow;
Francois Romieu50970832017-10-27 13:24:49 +0300668 const struct rtl_coalesce_info *coalesce_info;
Hans de Goedec2f6f3e2018-09-12 11:34:55 +0200669 struct clk *clk;
françois romieuc0e45c12011-01-03 15:08:04 +0000670
671 struct mdio_ops {
Francois Romieu24192212012-07-06 20:19:42 +0200672 void (*write)(struct rtl8169_private *, int, int);
673 int (*read)(struct rtl8169_private *, int);
françois romieuc0e45c12011-01-03 15:08:04 +0000674 } mdio_ops;
675
Francois Romieud58d46b2011-05-03 16:38:29 +0200676 struct jumbo_ops {
677 void (*enable)(struct rtl8169_private *);
678 void (*disable)(struct rtl8169_private *);
679 } jumbo_ops;
680
Heiner Kallweit61cb5322018-04-17 23:27:38 +0200681 void (*hw_start)(struct rtl8169_private *tp);
hayeswang5888d3f2014-07-11 16:25:56 +0800682 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
Francois Romieu4422bcd2012-01-26 11:23:32 +0100683
684 struct {
Francois Romieuda78dbf2012-01-26 14:18:23 +0100685 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
686 struct mutex mutex;
Francois Romieu4422bcd2012-01-26 11:23:32 +0100687 struct work_struct work;
688 } wk;
689
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +0200690 unsigned supports_gmii:1;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +0200691 struct mii_bus *mii_bus;
Corinna Vinschen42020322015-09-10 10:47:35 +0200692 dma_addr_t counters_phys_addr;
693 struct rtl8169_counters *counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +0200694 struct rtl8169_tc_offsets tc_offset;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000695 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000696
Francois Romieub6ffd972011-06-17 17:00:05 +0200697 struct rtl_fw {
698 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200699
700#define RTL_VER_SIZE 32
701
702 char version[RTL_VER_SIZE];
703
704 struct rtl_fw_phy_action {
705 __le32 *code;
706 size_t size;
707 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200708 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300709#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Hayes Wangc5583862012-07-02 17:23:22 +0800710
711 u32 ocp_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712};
713
Ralf Baechle979b6c12005-06-13 14:30:40 -0700714MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700717MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200718module_param_named(debug, debug.msg_enable, int, 0);
719MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720MODULE_LICENSE("GPL");
françois romieubca03d52011-01-03 15:07:31 +0000721MODULE_FIRMWARE(FIRMWARE_8168D_1);
722MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000723MODULE_FIRMWARE(FIRMWARE_8168E_1);
724MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400725MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800726MODULE_FIRMWARE(FIRMWARE_8105E_1);
Hayes Wangc2218922011-09-06 16:55:18 +0800727MODULE_FIRMWARE(FIRMWARE_8168F_1);
728MODULE_FIRMWARE(FIRMWARE_8168F_2);
Hayes Wang7e18dca2012-03-30 14:33:02 +0800729MODULE_FIRMWARE(FIRMWARE_8402_1);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +0800730MODULE_FIRMWARE(FIRMWARE_8411_1);
hayeswang45dd95c2013-07-08 17:09:01 +0800731MODULE_FIRMWARE(FIRMWARE_8411_2);
Hayes Wang5598bfe2012-07-02 17:23:21 +0800732MODULE_FIRMWARE(FIRMWARE_8106E_1);
hayeswang58152cd2013-04-01 22:23:42 +0000733MODULE_FIRMWARE(FIRMWARE_8106E_2);
hayeswangbeb330a2013-04-01 22:23:39 +0000734MODULE_FIRMWARE(FIRMWARE_8168G_2);
hayeswang57538c42013-04-01 22:23:40 +0000735MODULE_FIRMWARE(FIRMWARE_8168G_3);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +0800736MODULE_FIRMWARE(FIRMWARE_8168H_1);
737MODULE_FIRMWARE(FIRMWARE_8168H_2);
Francois Romieua3bf5c42014-08-26 22:40:38 +0200738MODULE_FIRMWARE(FIRMWARE_8107E_1);
739MODULE_FIRMWARE(FIRMWARE_8107E_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Heiner Kallweit1e1205b2018-03-20 07:45:42 +0100741static inline struct device *tp_to_dev(struct rtl8169_private *tp)
742{
743 return &tp->pci_dev->dev;
744}
745
Francois Romieuda78dbf2012-01-26 14:18:23 +0100746static void rtl_lock_work(struct rtl8169_private *tp)
747{
748 mutex_lock(&tp->wk.mutex);
749}
750
751static void rtl_unlock_work(struct rtl8169_private *tp)
752{
753 mutex_unlock(&tp->wk.mutex);
754}
755
Heiner Kallweitcb732002018-03-20 07:45:35 +0100756static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
Francois Romieud58d46b2011-05-03 16:38:29 +0200757{
Heiner Kallweitcb732002018-03-20 07:45:35 +0100758 pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +0800759 PCI_EXP_DEVCTL_READRQ, force);
Francois Romieud58d46b2011-05-03 16:38:29 +0200760}
761
Francois Romieuffc46952012-07-06 14:19:23 +0200762struct rtl_cond {
763 bool (*check)(struct rtl8169_private *);
764 const char *msg;
765};
766
767static void rtl_udelay(unsigned int d)
768{
769 udelay(d);
770}
771
772static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
773 void (*delay)(unsigned int), unsigned int d, int n,
774 bool high)
775{
776 int i;
777
778 for (i = 0; i < n; i++) {
779 delay(d);
780 if (c->check(tp) == high)
781 return true;
782 }
Francois Romieu82e316e2012-07-11 23:39:51 +0200783 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
784 c->msg, !high, n, d);
Francois Romieuffc46952012-07-06 14:19:23 +0200785 return false;
786}
787
788static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
789 const struct rtl_cond *c,
790 unsigned int d, int n)
791{
792 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
793}
794
795static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
796 const struct rtl_cond *c,
797 unsigned int d, int n)
798{
799 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
800}
801
802static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
803 const struct rtl_cond *c,
804 unsigned int d, int n)
805{
806 return rtl_loop_wait(tp, c, msleep, d, n, true);
807}
808
809static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
810 const struct rtl_cond *c,
811 unsigned int d, int n)
812{
813 return rtl_loop_wait(tp, c, msleep, d, n, false);
814}
815
816#define DECLARE_RTL_COND(name) \
817static bool name ## _check(struct rtl8169_private *); \
818 \
819static const struct rtl_cond name = { \
820 .check = name ## _check, \
821 .msg = #name \
822}; \
823 \
824static bool name ## _check(struct rtl8169_private *tp)
825
Hayes Wangc5583862012-07-02 17:23:22 +0800826static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
827{
828 if (reg & 0xffff0001) {
829 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
830 return true;
831 }
832 return false;
833}
834
835DECLARE_RTL_COND(rtl_ocp_gphy_cond)
836{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200837 return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
Hayes Wangc5583862012-07-02 17:23:22 +0800838}
839
840static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
841{
Hayes Wangc5583862012-07-02 17:23:22 +0800842 if (rtl_ocp_reg_failure(tp, reg))
843 return;
844
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200845 RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800846
847 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
848}
849
850static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
851{
Hayes Wangc5583862012-07-02 17:23:22 +0800852 if (rtl_ocp_reg_failure(tp, reg))
853 return 0;
854
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200855 RTL_W32(tp, GPHY_OCP, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800856
857 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200858 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
Hayes Wangc5583862012-07-02 17:23:22 +0800859}
860
Hayes Wangc5583862012-07-02 17:23:22 +0800861static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
862{
Hayes Wangc5583862012-07-02 17:23:22 +0800863 if (rtl_ocp_reg_failure(tp, reg))
864 return;
865
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200866 RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
Hayes Wangc5583862012-07-02 17:23:22 +0800867}
868
869static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
870{
Hayes Wangc5583862012-07-02 17:23:22 +0800871 if (rtl_ocp_reg_failure(tp, reg))
872 return 0;
873
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200874 RTL_W32(tp, OCPDR, reg << 15);
Hayes Wangc5583862012-07-02 17:23:22 +0800875
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200876 return RTL_R32(tp, OCPDR);
Hayes Wangc5583862012-07-02 17:23:22 +0800877}
878
879#define OCP_STD_PHY_BASE 0xa400
880
881static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
882{
883 if (reg == 0x1f) {
884 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
885 return;
886 }
887
888 if (tp->ocp_base != OCP_STD_PHY_BASE)
889 reg -= 0x10;
890
891 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
892}
893
894static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
895{
896 if (tp->ocp_base != OCP_STD_PHY_BASE)
897 reg -= 0x10;
898
899 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
900}
901
hayeswangeee37862013-04-01 22:23:38 +0000902static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
903{
904 if (reg == 0x1f) {
905 tp->ocp_base = value << 4;
906 return;
907 }
908
909 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
910}
911
912static int mac_mcu_read(struct rtl8169_private *tp, int reg)
913{
914 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
915}
916
Francois Romieuffc46952012-07-06 14:19:23 +0200917DECLARE_RTL_COND(rtl_phyar_cond)
918{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200919 return RTL_R32(tp, PHYAR) & 0x80000000;
Francois Romieuffc46952012-07-06 14:19:23 +0200920}
921
Francois Romieu24192212012-07-06 20:19:42 +0200922static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200924 RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Francois Romieuffc46952012-07-06 14:19:23 +0200926 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
Timo Teräs024a07b2010-06-06 15:38:47 -0700927 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700928 * According to hardware specs a 20us delay is required after write
929 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700930 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700931 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Francois Romieu24192212012-07-06 20:19:42 +0200934static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Francois Romieuffc46952012-07-06 14:19:23 +0200936 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200938 RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Francois Romieuffc46952012-07-06 14:19:23 +0200940 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200941 RTL_R32(tp, PHYAR) & 0xffff : ~0;
Francois Romieuffc46952012-07-06 14:19:23 +0200942
Timo Teräs81a95f02010-06-09 17:31:48 -0700943 /*
944 * According to hardware specs a 20us delay is required after read
945 * complete indication, but before sending next command.
946 */
947 udelay(20);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return value;
950}
951
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800952DECLARE_RTL_COND(rtl_ocpar_cond)
953{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200954 return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +0800955}
956
Francois Romieu24192212012-07-06 20:19:42 +0200957static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
françois romieuc0e45c12011-01-03 15:08:04 +0000958{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200959 RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
960 RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
961 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000962
Francois Romieuffc46952012-07-06 14:19:23 +0200963 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
françois romieuc0e45c12011-01-03 15:08:04 +0000964}
965
Francois Romieu24192212012-07-06 20:19:42 +0200966static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieuc0e45c12011-01-03 15:08:04 +0000967{
Francois Romieu24192212012-07-06 20:19:42 +0200968 r8168dp_1_mdio_access(tp, reg,
969 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
françois romieuc0e45c12011-01-03 15:08:04 +0000970}
971
Francois Romieu24192212012-07-06 20:19:42 +0200972static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
françois romieuc0e45c12011-01-03 15:08:04 +0000973{
Francois Romieu24192212012-07-06 20:19:42 +0200974 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
françois romieuc0e45c12011-01-03 15:08:04 +0000975
976 mdelay(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200977 RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
978 RTL_W32(tp, EPHY_RXER_NUM, 0);
françois romieuc0e45c12011-01-03 15:08:04 +0000979
Francois Romieuffc46952012-07-06 14:19:23 +0200980 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200981 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
françois romieuc0e45c12011-01-03 15:08:04 +0000982}
983
françois romieue6de30d2011-01-03 15:08:37 +0000984#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
985
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200986static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000987{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200988 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000989}
990
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200991static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +0000992{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200993 RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
françois romieue6de30d2011-01-03 15:08:37 +0000994}
995
Francois Romieu24192212012-07-06 20:19:42 +0200996static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
françois romieue6de30d2011-01-03 15:08:37 +0000997{
Andy Shevchenko1ef72862018-03-01 13:27:34 +0200998 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +0000999
Francois Romieu24192212012-07-06 20:19:42 +02001000 r8169_mdio_write(tp, reg, value);
françois romieue6de30d2011-01-03 15:08:37 +00001001
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001002 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001003}
1004
Francois Romieu24192212012-07-06 20:19:42 +02001005static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
françois romieue6de30d2011-01-03 15:08:37 +00001006{
1007 int value;
1008
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001009 r8168dp_2_mdio_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001010
Francois Romieu24192212012-07-06 20:19:42 +02001011 value = r8169_mdio_read(tp, reg);
françois romieue6de30d2011-01-03 15:08:37 +00001012
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001013 r8168dp_2_mdio_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00001014
1015 return value;
1016}
1017
françois romieu4da19632011-01-03 15:07:55 +00001018static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +02001019{
Francois Romieu24192212012-07-06 20:19:42 +02001020 tp->mdio_ops.write(tp, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +02001021}
1022
françois romieu4da19632011-01-03 15:07:55 +00001023static int rtl_readphy(struct rtl8169_private *tp, int location)
1024{
Francois Romieu24192212012-07-06 20:19:42 +02001025 return tp->mdio_ops.read(tp, location);
françois romieu4da19632011-01-03 15:07:55 +00001026}
1027
1028static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1029{
1030 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1031}
1032
Chun-Hao Lin76564422014-10-01 23:17:17 +08001033static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +00001034{
1035 int val;
1036
françois romieu4da19632011-01-03 15:07:55 +00001037 val = rtl_readphy(tp, reg_addr);
Chun-Hao Lin76564422014-10-01 23:17:17 +08001038 rtl_writephy(tp, reg_addr, (val & ~m) | p);
françois romieudaf9df62009-10-07 12:44:20 +00001039}
1040
Francois Romieuffc46952012-07-06 14:19:23 +02001041DECLARE_RTL_COND(rtl_ephyar_cond)
1042{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001043 return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001044}
1045
Francois Romieufdf6fc02012-07-06 22:40:38 +02001046static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
Francois Romieudacf8152008-08-02 20:44:13 +02001047{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001048 RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
Francois Romieudacf8152008-08-02 20:44:13 +02001049 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1050
Francois Romieuffc46952012-07-06 14:19:23 +02001051 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1052
1053 udelay(10);
Francois Romieudacf8152008-08-02 20:44:13 +02001054}
1055
Francois Romieufdf6fc02012-07-06 22:40:38 +02001056static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
Francois Romieudacf8152008-08-02 20:44:13 +02001057{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001058 RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
Francois Romieudacf8152008-08-02 20:44:13 +02001059
Francois Romieuffc46952012-07-06 14:19:23 +02001060 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001061 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
Francois Romieudacf8152008-08-02 20:44:13 +02001062}
1063
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001064DECLARE_RTL_COND(rtl_eriar_cond)
1065{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001066 return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001067}
1068
Francois Romieufdf6fc02012-07-06 22:40:38 +02001069static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1070 u32 val, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001071{
Hayes Wang133ac402011-07-06 15:58:05 +08001072 BUG_ON((addr & 3) || (mask == 0));
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001073 RTL_W32(tp, ERIDR, val);
1074 RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001075
Francois Romieuffc46952012-07-06 14:19:23 +02001076 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
Hayes Wang133ac402011-07-06 15:58:05 +08001077}
1078
Francois Romieufdf6fc02012-07-06 22:40:38 +02001079static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001080{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001081 RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
Hayes Wang133ac402011-07-06 15:58:05 +08001082
Francois Romieuffc46952012-07-06 14:19:23 +02001083 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001084 RTL_R32(tp, ERIDR) : ~0;
Hayes Wang133ac402011-07-06 15:58:05 +08001085}
1086
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001087static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
Francois Romieufdf6fc02012-07-06 22:40:38 +02001088 u32 m, int type)
Hayes Wang133ac402011-07-06 15:58:05 +08001089{
1090 u32 val;
1091
Francois Romieufdf6fc02012-07-06 22:40:38 +02001092 val = rtl_eri_read(tp, addr, type);
1093 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
Hayes Wang133ac402011-07-06 15:58:05 +08001094}
1095
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001096static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1097{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001098 RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001099 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001100 RTL_R32(tp, OCPDR) : ~0;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001101}
1102
1103static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1104{
1105 return rtl_eri_read(tp, reg, ERIAR_OOB);
1106}
1107
1108static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1109{
1110 switch (tp->mac_version) {
1111 case RTL_GIGA_MAC_VER_27:
1112 case RTL_GIGA_MAC_VER_28:
1113 case RTL_GIGA_MAC_VER_31:
1114 return r8168dp_ocp_read(tp, mask, reg);
1115 case RTL_GIGA_MAC_VER_49:
1116 case RTL_GIGA_MAC_VER_50:
1117 case RTL_GIGA_MAC_VER_51:
1118 return r8168ep_ocp_read(tp, mask, reg);
1119 default:
1120 BUG();
1121 return ~0;
1122 }
1123}
1124
1125static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1126 u32 data)
1127{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001128 RTL_W32(tp, OCPDR, data);
1129 RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001130 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1131}
1132
1133static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1134 u32 data)
1135{
1136 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1137 data, ERIAR_OOB);
1138}
1139
1140static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1141{
1142 switch (tp->mac_version) {
1143 case RTL_GIGA_MAC_VER_27:
1144 case RTL_GIGA_MAC_VER_28:
1145 case RTL_GIGA_MAC_VER_31:
1146 r8168dp_ocp_write(tp, mask, reg, data);
1147 break;
1148 case RTL_GIGA_MAC_VER_49:
1149 case RTL_GIGA_MAC_VER_50:
1150 case RTL_GIGA_MAC_VER_51:
1151 r8168ep_ocp_write(tp, mask, reg, data);
1152 break;
1153 default:
1154 BUG();
1155 break;
1156 }
1157}
1158
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001159static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1160{
1161 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1162
1163 ocp_write(tp, 0x1, 0x30, 0x00000001);
1164}
1165
1166#define OOB_CMD_RESET 0x00
1167#define OOB_CMD_DRIVER_START 0x05
1168#define OOB_CMD_DRIVER_STOP 0x06
1169
1170static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1171{
1172 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1173}
1174
1175DECLARE_RTL_COND(rtl_ocp_read_cond)
1176{
1177 u16 reg;
1178
1179 reg = rtl8168_get_ocp_reg(tp);
1180
1181 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1182}
1183
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001184DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1185{
1186 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1187}
1188
1189DECLARE_RTL_COND(rtl_ocp_tx_cond)
1190{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001191 return RTL_R8(tp, IBISR0) & 0x20;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001192}
1193
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001194static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1195{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001196 RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
Chunhao Lin086ca232018-01-31 01:32:36 +08001197 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001198 RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1199 RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001200}
1201
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001202static void rtl8168dp_driver_start(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001203{
1204 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001205 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1206}
1207
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001208static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1209{
1210 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1211 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1212 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1213}
1214
1215static void rtl8168_driver_start(struct rtl8169_private *tp)
1216{
1217 switch (tp->mac_version) {
1218 case RTL_GIGA_MAC_VER_27:
1219 case RTL_GIGA_MAC_VER_28:
1220 case RTL_GIGA_MAC_VER_31:
1221 rtl8168dp_driver_start(tp);
1222 break;
1223 case RTL_GIGA_MAC_VER_49:
1224 case RTL_GIGA_MAC_VER_50:
1225 case RTL_GIGA_MAC_VER_51:
1226 rtl8168ep_driver_start(tp);
1227 break;
1228 default:
1229 BUG();
1230 break;
1231 }
1232}
1233
1234static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1235{
1236 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1237 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1238}
1239
1240static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1241{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08001242 rtl8168ep_stop_cmac(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001243 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1244 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1245 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1246}
1247
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001248static void rtl8168_driver_stop(struct rtl8169_private *tp)
1249{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001250 switch (tp->mac_version) {
1251 case RTL_GIGA_MAC_VER_27:
1252 case RTL_GIGA_MAC_VER_28:
1253 case RTL_GIGA_MAC_VER_31:
1254 rtl8168dp_driver_stop(tp);
1255 break;
1256 case RTL_GIGA_MAC_VER_49:
1257 case RTL_GIGA_MAC_VER_50:
1258 case RTL_GIGA_MAC_VER_51:
1259 rtl8168ep_driver_stop(tp);
1260 break;
1261 default:
1262 BUG();
1263 break;
1264 }
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001265}
1266
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001267static bool r8168dp_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001268{
1269 u16 reg = rtl8168_get_ocp_reg(tp);
1270
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001271 return !!(ocp_read(tp, 0x0f, reg) & 0x00008000);
Chun-Hao Lin2a9b4d92014-10-01 23:17:20 +08001272}
1273
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001274static bool r8168ep_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001275{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001276 return !!(ocp_read(tp, 0x0f, 0x128) & 0x00000001);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001277}
1278
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001279static bool r8168_check_dash(struct rtl8169_private *tp)
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001280{
1281 switch (tp->mac_version) {
1282 case RTL_GIGA_MAC_VER_27:
1283 case RTL_GIGA_MAC_VER_28:
1284 case RTL_GIGA_MAC_VER_31:
1285 return r8168dp_check_dash(tp);
1286 case RTL_GIGA_MAC_VER_49:
1287 case RTL_GIGA_MAC_VER_50:
1288 case RTL_GIGA_MAC_VER_51:
1289 return r8168ep_check_dash(tp);
1290 default:
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01001291 return false;
Chun-Hao Lin935e2212014-10-07 15:10:41 +08001292 }
1293}
1294
françois romieuc28aa382011-08-02 03:53:43 +00001295struct exgmac_reg {
1296 u16 addr;
1297 u16 mask;
1298 u32 val;
1299};
1300
Francois Romieufdf6fc02012-07-06 22:40:38 +02001301static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
françois romieuc28aa382011-08-02 03:53:43 +00001302 const struct exgmac_reg *r, int len)
1303{
1304 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001305 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
françois romieuc28aa382011-08-02 03:53:43 +00001306 r++;
1307 }
1308}
1309
Francois Romieuffc46952012-07-06 14:19:23 +02001310DECLARE_RTL_COND(rtl_efusear_cond)
1311{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001312 return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02001313}
1314
Francois Romieufdf6fc02012-07-06 22:40:38 +02001315static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
françois romieudaf9df62009-10-07 12:44:20 +00001316{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001317 RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
françois romieudaf9df62009-10-07 12:44:20 +00001318
Francois Romieuffc46952012-07-06 14:19:23 +02001319 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001320 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
françois romieudaf9df62009-10-07 12:44:20 +00001321}
1322
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001323static u16 rtl_get_events(struct rtl8169_private *tp)
1324{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001325 return RTL_R16(tp, IntrStatus);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001326}
1327
1328static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1329{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001330 RTL_W16(tp, IntrStatus, bits);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001331 mmiowb();
1332}
1333
1334static void rtl_irq_disable(struct rtl8169_private *tp)
1335{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001336 RTL_W16(tp, IntrMask, 0);
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001337 mmiowb();
1338}
1339
Francois Romieu3e990ff2012-01-26 12:50:01 +01001340static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1341{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001342 RTL_W16(tp, IntrMask, bits);
Francois Romieu3e990ff2012-01-26 12:50:01 +01001343}
1344
Francois Romieuda78dbf2012-01-26 14:18:23 +01001345#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1346#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1347#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1348
1349static void rtl_irq_enable_all(struct rtl8169_private *tp)
1350{
1351 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1352}
1353
françois romieu811fd302011-12-04 20:30:45 +00001354static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Francois Romieu9085cdfa2012-01-26 12:59:08 +01001356 rtl_irq_disable(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001357 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001358 RTL_R8(tp, ChipCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Hayes Wang70090422011-07-06 15:58:06 +08001361static void rtl_link_chg_patch(struct rtl8169_private *tp)
1362{
Hayes Wang70090422011-07-06 15:58:06 +08001363 struct net_device *dev = tp->dev;
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001364 struct phy_device *phydev = dev->phydev;
Hayes Wang70090422011-07-06 15:58:06 +08001365
1366 if (!netif_running(dev))
1367 return;
1368
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08001369 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1370 tp->mac_version == RTL_GIGA_MAC_VER_38) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001371 if (phydev->speed == SPEED_1000) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001372 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1373 ERIAR_EXGMAC);
1374 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1375 ERIAR_EXGMAC);
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001376 } else if (phydev->speed == SPEED_100) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001377 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1378 ERIAR_EXGMAC);
1379 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1380 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001381 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001382 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1383 ERIAR_EXGMAC);
1384 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1385 ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08001386 }
1387 /* Reset packet filter */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001388 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
Hayes Wang70090422011-07-06 15:58:06 +08001389 ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001390 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
Hayes Wang70090422011-07-06 15:58:06 +08001391 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001392 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1393 tp->mac_version == RTL_GIGA_MAC_VER_36) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001394 if (phydev->speed == SPEED_1000) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001395 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1396 ERIAR_EXGMAC);
1397 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1398 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001399 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001400 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1401 ERIAR_EXGMAC);
1402 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1403 ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08001404 }
Hayes Wang7e18dca2012-03-30 14:33:02 +08001405 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
Heiner Kallweit29a12b42018-07-17 22:52:14 +02001406 if (phydev->speed == SPEED_10) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001407 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1408 ERIAR_EXGMAC);
1409 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1410 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001411 } else {
Francois Romieufdf6fc02012-07-06 22:40:38 +02001412 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1413 ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08001414 }
Hayes Wang70090422011-07-06 15:58:06 +08001415 }
1416}
1417
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001418#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1419
1420static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1421{
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001422 u8 options;
1423 u32 wolopts = 0;
1424
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001425 options = RTL_R8(tp, Config1);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001426 if (!(options & PMEnable))
1427 return 0;
1428
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001429 options = RTL_R8(tp, Config3);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001430 if (options & LinkUp)
1431 wolopts |= WAKE_PHY;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001432 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001433 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1434 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001435 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1436 wolopts |= WAKE_MAGIC;
1437 break;
1438 default:
1439 if (options & MagicPacket)
1440 wolopts |= WAKE_MAGIC;
1441 break;
1442 }
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001443
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001444 options = RTL_R8(tp, Config5);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001445 if (options & UWF)
1446 wolopts |= WAKE_UCAST;
1447 if (options & BWF)
1448 wolopts |= WAKE_BCAST;
1449 if (options & MWF)
1450 wolopts |= WAKE_MCAST;
1451
1452 return wolopts;
1453}
1454
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001455static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1456{
1457 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001458
Francois Romieuda78dbf2012-01-26 14:18:23 +01001459 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001460 wol->supported = WAKE_ANY;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001461 wol->wolopts = tp->saved_wolopts;
Francois Romieuda78dbf2012-01-26 14:18:23 +01001462 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001463}
1464
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001465static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001466{
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001467 unsigned int i, tmp;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001468 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001469 u32 opt;
1470 u16 reg;
1471 u8 mask;
1472 } cfg[] = {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001473 { WAKE_PHY, Config3, LinkUp },
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001474 { WAKE_UCAST, Config5, UWF },
1475 { WAKE_BCAST, Config5, BWF },
1476 { WAKE_MCAST, Config5, MWF },
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001477 { WAKE_ANY, Config5, LanWake },
1478 { WAKE_MAGIC, Config3, MagicPacket }
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001479 };
Francois Romieu851e6022012-04-17 11:10:11 +02001480 u8 options;
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001481
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001482 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001483
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001484 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02001485 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1486 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001487 tmp = ARRAY_SIZE(cfg) - 1;
1488 if (wolopts & WAKE_MAGIC)
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001489 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001490 0x0dc,
1491 ERIAR_MASK_0100,
1492 MagicPacket_v2,
1493 0x0000,
1494 ERIAR_EXGMAC);
1495 else
Chun-Hao Lin706123d2014-10-01 23:17:18 +08001496 rtl_w0w1_eri(tp,
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08001497 0x0dc,
1498 ERIAR_MASK_0100,
1499 0x0000,
1500 MagicPacket_v2,
1501 ERIAR_EXGMAC);
1502 break;
1503 default:
1504 tmp = ARRAY_SIZE(cfg);
1505 break;
1506 }
1507
1508 for (i = 0; i < tmp; i++) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001509 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001510 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001511 options |= cfg[i].mask;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001512 RTL_W8(tp, cfg[i].reg, options);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001513 }
1514
Francois Romieu851e6022012-04-17 11:10:11 +02001515 switch (tp->mac_version) {
1516 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001517 options = RTL_R8(tp, Config1) & ~PMEnable;
Francois Romieu851e6022012-04-17 11:10:11 +02001518 if (wolopts)
1519 options |= PMEnable;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001520 RTL_W8(tp, Config1, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001521 break;
1522 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001523 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
Francois Romieud387b422012-04-17 11:12:01 +02001524 if (wolopts)
1525 options |= PME_SIGNAL;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001526 RTL_W8(tp, Config2, options);
Francois Romieu851e6022012-04-17 11:10:11 +02001527 break;
1528 }
1529
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001530 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001531}
1532
1533static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1534{
1535 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001536 struct device *d = tp_to_dev(tp);
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001537
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001538 if (wol->wolopts & ~WAKE_ANY)
1539 return -EINVAL;
1540
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001541 pm_runtime_get_noresume(d);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001542
Francois Romieuda78dbf2012-01-26 14:18:23 +01001543 rtl_lock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001544
Heiner Kallweit2f533f62018-06-25 20:34:41 +02001545 tp->saved_wolopts = wol->wolopts;
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001546
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001547 if (pm_runtime_active(d))
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001548 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001549
1550 rtl_unlock_work(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001551
Heiner Kallweit433f9d02018-06-24 18:39:06 +02001552 device_set_wakeup_enable(d, tp->saved_wolopts);
françois romieuea809072010-11-08 13:23:58 +00001553
Chun-Hao Lin5fa80a32016-07-29 16:37:54 +08001554 pm_runtime_put_noidle(d);
1555
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001556 return 0;
1557}
1558
Francois Romieu31bd2042011-04-26 18:58:59 +02001559static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1560{
Francois Romieu85bffe62011-04-27 08:22:39 +02001561 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001562}
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564static void rtl8169_get_drvinfo(struct net_device *dev,
1565 struct ethtool_drvinfo *info)
1566{
1567 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001568 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Rick Jones68aad782011-11-07 13:29:27 +00001570 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001571 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001572 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
Rick Jones8ac72d12011-11-22 14:06:26 +00001573 if (!IS_ERR_OR_NULL(rtl_fw))
1574 strlcpy(info->fw_version, rtl_fw->version,
1575 sizeof(info->fw_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578static int rtl8169_get_regs_len(struct net_device *dev)
1579{
1580 return R8169_REGS_SIZE;
1581}
1582
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001583static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1584 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Francois Romieud58d46b2011-05-03 16:38:29 +02001586 struct rtl8169_private *tp = netdev_priv(dev);
1587
Francois Romieu2b7b4312011-04-18 22:53:24 -07001588 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001589 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Francois Romieud58d46b2011-05-03 16:38:29 +02001591 if (dev->mtu > JUMBO_1K &&
Heiner Kallweit6ed0e082018-04-17 23:36:12 +02001592 tp->mac_version > RTL_GIGA_MAC_VER_06)
Francois Romieud58d46b2011-05-03 16:38:29 +02001593 features &= ~NETIF_F_IP_CSUM;
1594
Michał Mirosław350fb322011-04-08 06:35:56 +00001595 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597
Heiner Kallweita3984572018-04-28 22:19:15 +02001598static int rtl8169_set_features(struct net_device *dev,
1599 netdev_features_t features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct rtl8169_private *tp = netdev_priv(dev);
hayeswang929a0312014-09-16 11:40:47 +08001602 u32 rx_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Heiner Kallweita3984572018-04-28 22:19:15 +02001604 rtl_lock_work(tp);
1605
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001606 rx_config = RTL_R32(tp, RxConfig);
hayeswang929a0312014-09-16 11:40:47 +08001607 if (features & NETIF_F_RXALL)
1608 rx_config |= (AcceptErr | AcceptRunt);
1609 else
1610 rx_config &= ~(AcceptErr | AcceptRunt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001612 RTL_W32(tp, RxConfig, rx_config);
Michał Mirosław350fb322011-04-08 06:35:56 +00001613
hayeswang929a0312014-09-16 11:40:47 +08001614 if (features & NETIF_F_RXCSUM)
1615 tp->cp_cmd |= RxChkSum;
1616 else
1617 tp->cp_cmd &= ~RxChkSum;
Ben Greear6bbe0212012-02-10 15:04:33 +00001618
hayeswang929a0312014-09-16 11:40:47 +08001619 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1620 tp->cp_cmd |= RxVlan;
1621 else
1622 tp->cp_cmd &= ~RxVlan;
1623
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001624 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1625 RTL_R16(tp, CPlusCmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Francois Romieuda78dbf2012-01-26 14:18:23 +01001627 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 return 0;
1630}
1631
Kirill Smelkov810f4892012-11-10 21:11:02 +04001632static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Jiri Pirkodf8a39d2015-01-13 17:13:44 +01001634 return (skb_vlan_tag_present(skb)) ?
1635 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Francois Romieu7a8fc772011-03-01 17:18:33 +01001638static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Francois Romieu7a8fc772011-03-01 17:18:33 +01001642 if (opts2 & RxVlanTag)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001643 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1647 void *p)
1648{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001649 struct rtl8169_private *tp = netdev_priv(dev);
Peter Wu15edae92013-08-21 23:17:11 +02001650 u32 __iomem *data = tp->mmio_addr;
1651 u32 *dw = p;
1652 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Francois Romieuda78dbf2012-01-26 14:18:23 +01001654 rtl_lock_work(tp);
Peter Wu15edae92013-08-21 23:17:11 +02001655 for (i = 0; i < R8169_REGS_SIZE; i += 4)
1656 memcpy_fromio(dw++, data++, 4);
Francois Romieuda78dbf2012-01-26 14:18:23 +01001657 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001660static u32 rtl8169_get_msglevel(struct net_device *dev)
1661{
1662 struct rtl8169_private *tp = netdev_priv(dev);
1663
1664 return tp->msg_enable;
1665}
1666
1667static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1668{
1669 struct rtl8169_private *tp = netdev_priv(dev);
1670
1671 tp->msg_enable = value;
1672}
1673
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001674static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1675 "tx_packets",
1676 "rx_packets",
1677 "tx_errors",
1678 "rx_errors",
1679 "rx_missed",
1680 "align_errors",
1681 "tx_single_collisions",
1682 "tx_multi_collisions",
1683 "unicast",
1684 "broadcast",
1685 "multicast",
1686 "tx_aborted",
1687 "tx_underrun",
1688};
1689
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001690static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001691{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001692 switch (sset) {
1693 case ETH_SS_STATS:
1694 return ARRAY_SIZE(rtl8169_gstrings);
1695 default:
1696 return -EOPNOTSUPP;
1697 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001698}
1699
Corinna Vinschen42020322015-09-10 10:47:35 +02001700DECLARE_RTL_COND(rtl_counters_cond)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001701{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001702 return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001703}
1704
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001705static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001706{
Corinna Vinschen42020322015-09-10 10:47:35 +02001707 dma_addr_t paddr = tp->counters_phys_addr;
1708 u32 cmd;
Corinna Vinschen42020322015-09-10 10:47:35 +02001709
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001710 RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1711 RTL_R32(tp, CounterAddrHigh);
Corinna Vinschen42020322015-09-10 10:47:35 +02001712 cmd = (u64)paddr & DMA_BIT_MASK(32);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001713 RTL_W32(tp, CounterAddrLow, cmd);
1714 RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
Corinna Vinschen42020322015-09-10 10:47:35 +02001715
Francois Romieua78e9362018-01-26 01:53:26 +01001716 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001717}
1718
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001719static bool rtl8169_reset_counters(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001720{
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001721 /*
1722 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1723 * tally counters.
1724 */
1725 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1726 return true;
1727
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001728 return rtl8169_do_counters(tp, CounterReset);
Francois Romieuffc46952012-07-06 14:19:23 +02001729}
1730
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001731static bool rtl8169_update_counters(struct rtl8169_private *tp)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001732{
Ivan Vecera355423d2009-02-06 21:49:57 -08001733 /*
1734 * Some chips are unable to dump tally counters when the receiver
1735 * is disabled.
1736 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001737 if ((RTL_R8(tp, ChipCmd) & CmdRxEnb) == 0)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001738 return true;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001739
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001740 return rtl8169_do_counters(tp, CounterDump);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001741}
1742
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001743static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001744{
Corinna Vinschen42020322015-09-10 10:47:35 +02001745 struct rtl8169_counters *counters = tp->counters;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001746 bool ret = false;
1747
1748 /*
1749 * rtl8169_init_counter_offsets is called from rtl_open. On chip
1750 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1751 * reset by a power cycle, while the counter values collected by the
1752 * driver are reset at every driver unload/load cycle.
1753 *
1754 * To make sure the HW values returned by @get_stats64 match the SW
1755 * values, we collect the initial values at first open(*) and use them
1756 * as offsets to normalize the values returned by @get_stats64.
1757 *
1758 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1759 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1760 * set at open time by rtl_hw_start.
1761 */
1762
1763 if (tp->tc_offset.inited)
1764 return true;
1765
1766 /* If both, reset and update fail, propagate to caller. */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001767 if (rtl8169_reset_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001768 ret = true;
1769
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001770 if (rtl8169_update_counters(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001771 ret = true;
1772
Corinna Vinschen42020322015-09-10 10:47:35 +02001773 tp->tc_offset.tx_errors = counters->tx_errors;
1774 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1775 tp->tc_offset.tx_aborted = counters->tx_aborted;
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02001776 tp->tc_offset.inited = true;
1777
1778 return ret;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001779}
1780
Ivan Vecera355423d2009-02-06 21:49:57 -08001781static void rtl8169_get_ethtool_stats(struct net_device *dev,
1782 struct ethtool_stats *stats, u64 *data)
1783{
1784 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01001785 struct device *d = tp_to_dev(tp);
Corinna Vinschen42020322015-09-10 10:47:35 +02001786 struct rtl8169_counters *counters = tp->counters;
Ivan Vecera355423d2009-02-06 21:49:57 -08001787
1788 ASSERT_RTNL();
1789
Chun-Hao Line0636232016-07-29 16:37:55 +08001790 pm_runtime_get_noresume(d);
1791
1792 if (pm_runtime_active(d))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02001793 rtl8169_update_counters(tp);
Chun-Hao Line0636232016-07-29 16:37:55 +08001794
1795 pm_runtime_put_noidle(d);
Ivan Vecera355423d2009-02-06 21:49:57 -08001796
Corinna Vinschen42020322015-09-10 10:47:35 +02001797 data[0] = le64_to_cpu(counters->tx_packets);
1798 data[1] = le64_to_cpu(counters->rx_packets);
1799 data[2] = le64_to_cpu(counters->tx_errors);
1800 data[3] = le32_to_cpu(counters->rx_errors);
1801 data[4] = le16_to_cpu(counters->rx_missed);
1802 data[5] = le16_to_cpu(counters->align_errors);
1803 data[6] = le32_to_cpu(counters->tx_one_collision);
1804 data[7] = le32_to_cpu(counters->tx_multi_collision);
1805 data[8] = le64_to_cpu(counters->rx_unicast);
1806 data[9] = le64_to_cpu(counters->rx_broadcast);
1807 data[10] = le32_to_cpu(counters->rx_multicast);
1808 data[11] = le16_to_cpu(counters->tx_aborted);
1809 data[12] = le16_to_cpu(counters->tx_underun);
Ivan Vecera355423d2009-02-06 21:49:57 -08001810}
1811
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001812static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1813{
1814 switch(stringset) {
1815 case ETH_SS_STATS:
1816 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1817 break;
1818 }
1819}
1820
Francois Romieu50970832017-10-27 13:24:49 +03001821/*
1822 * Interrupt coalescing
1823 *
1824 * > 1 - the availability of the IntrMitigate (0xe2) register through the
1825 * > 8169, 8168 and 810x line of chipsets
1826 *
1827 * 8169, 8168, and 8136(810x) serial chipsets support it.
1828 *
1829 * > 2 - the Tx timer unit at gigabit speed
1830 *
1831 * The unit of the timer depends on both the speed and the setting of CPlusCmd
1832 * (0xe0) bit 1 and bit 0.
1833 *
1834 * For 8169
1835 * bit[1:0] \ speed 1000M 100M 10M
1836 * 0 0 320ns 2.56us 40.96us
1837 * 0 1 2.56us 20.48us 327.7us
1838 * 1 0 5.12us 40.96us 655.4us
1839 * 1 1 10.24us 81.92us 1.31ms
1840 *
1841 * For the other
1842 * bit[1:0] \ speed 1000M 100M 10M
1843 * 0 0 5us 2.56us 40.96us
1844 * 0 1 40us 20.48us 327.7us
1845 * 1 0 80us 40.96us 655.4us
1846 * 1 1 160us 81.92us 1.31ms
1847 */
1848
1849/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1850struct rtl_coalesce_scale {
1851 /* Rx / Tx */
1852 u32 nsecs[2];
1853};
1854
1855/* rx/tx scale factors for all CPlusCmd[0:1] cases */
1856struct rtl_coalesce_info {
1857 u32 speed;
1858 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
1859};
1860
1861/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1862#define rxtx_x1822(r, t) { \
1863 {{(r), (t)}}, \
1864 {{(r)*8, (t)*8}}, \
1865 {{(r)*8*2, (t)*8*2}}, \
1866 {{(r)*8*2*2, (t)*8*2*2}}, \
1867}
1868static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1869 /* speed delays: rx00 tx00 */
1870 { SPEED_10, rxtx_x1822(40960, 40960) },
1871 { SPEED_100, rxtx_x1822( 2560, 2560) },
1872 { SPEED_1000, rxtx_x1822( 320, 320) },
1873 { 0 },
1874};
1875
1876static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1877 /* speed delays: rx00 tx00 */
1878 { SPEED_10, rxtx_x1822(40960, 40960) },
1879 { SPEED_100, rxtx_x1822( 2560, 2560) },
1880 { SPEED_1000, rxtx_x1822( 5000, 5000) },
1881 { 0 },
1882};
1883#undef rxtx_x1822
1884
1885/* get rx/tx scale vector corresponding to current speed */
1886static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1887{
1888 struct rtl8169_private *tp = netdev_priv(dev);
1889 struct ethtool_link_ksettings ecmd;
1890 const struct rtl_coalesce_info *ci;
1891 int rc;
1892
Heiner Kallweit45772432018-07-17 22:51:44 +02001893 rc = phy_ethtool_get_link_ksettings(dev, &ecmd);
Francois Romieu50970832017-10-27 13:24:49 +03001894 if (rc < 0)
1895 return ERR_PTR(rc);
1896
1897 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
1898 if (ecmd.base.speed == ci->speed) {
1899 return ci;
1900 }
1901 }
1902
1903 return ERR_PTR(-ELNRNG);
1904}
1905
1906static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1907{
1908 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001909 const struct rtl_coalesce_info *ci;
1910 const struct rtl_coalesce_scale *scale;
1911 struct {
1912 u32 *max_frames;
1913 u32 *usecs;
1914 } coal_settings [] = {
1915 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1916 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1917 }, *p = coal_settings;
1918 int i;
1919 u16 w;
1920
1921 memset(ec, 0, sizeof(*ec));
1922
1923 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1924 ci = rtl_coalesce_info(dev);
1925 if (IS_ERR(ci))
1926 return PTR_ERR(ci);
1927
Heiner Kallweit0ae09742018-04-28 22:19:26 +02001928 scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
Francois Romieu50970832017-10-27 13:24:49 +03001929
1930 /* read IntrMitigate and adjust according to scale */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02001931 for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
Francois Romieu50970832017-10-27 13:24:49 +03001932 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1933 w >>= RTL_COALESCE_SHIFT;
1934 *p->usecs = w & RTL_COALESCE_MASK;
1935 }
1936
1937 for (i = 0; i < 2; i++) {
1938 p = coal_settings + i;
1939 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1940
1941 /*
1942 * ethtool_coalesce says it is illegal to set both usecs and
1943 * max_frames to 0.
1944 */
1945 if (!*p->usecs && !*p->max_frames)
1946 *p->max_frames = 1;
1947 }
1948
1949 return 0;
1950}
1951
1952/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1953static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1954 struct net_device *dev, u32 nsec, u16 *cp01)
1955{
1956 const struct rtl_coalesce_info *ci;
1957 u16 i;
1958
1959 ci = rtl_coalesce_info(dev);
1960 if (IS_ERR(ci))
1961 return ERR_CAST(ci);
1962
1963 for (i = 0; i < 4; i++) {
1964 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1965 ci->scalev[i].nsecs[1]);
1966 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1967 *cp01 = i;
1968 return &ci->scalev[i];
1969 }
1970 }
1971
1972 return ERR_PTR(-EINVAL);
1973}
1974
1975static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1976{
1977 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu50970832017-10-27 13:24:49 +03001978 const struct rtl_coalesce_scale *scale;
1979 struct {
1980 u32 frames;
1981 u32 usecs;
1982 } coal_settings [] = {
1983 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1984 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1985 }, *p = coal_settings;
1986 u16 w = 0, cp01;
1987 int i;
1988
1989 scale = rtl_coalesce_choose_scale(dev,
1990 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1991 if (IS_ERR(scale))
1992 return PTR_ERR(scale);
1993
1994 for (i = 0; i < 2; i++, p++) {
1995 u32 units;
1996
1997 /*
1998 * accept max_frames=1 we returned in rtl_get_coalesce.
1999 * accept it not only when usecs=0 because of e.g. the following scenario:
2000 *
2001 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2002 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2003 * - then user does `ethtool -C eth0 rx-usecs 100`
2004 *
2005 * since ethtool sends to kernel whole ethtool_coalesce
2006 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2007 * we'll reject it below in `frames % 4 != 0`.
2008 */
2009 if (p->frames == 1) {
2010 p->frames = 0;
2011 }
2012
2013 units = p->usecs * 1000 / scale->nsecs[i];
2014 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2015 return -EINVAL;
2016
2017 w <<= RTL_COALESCE_SHIFT;
2018 w |= units;
2019 w <<= RTL_COALESCE_SHIFT;
2020 w |= p->frames >> 2;
2021 }
2022
2023 rtl_lock_work(tp);
2024
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002025 RTL_W16(tp, IntrMitigate, swab16(w));
Francois Romieu50970832017-10-27 13:24:49 +03002026
Heiner Kallweit9a3c81f2018-04-28 22:19:21 +02002027 tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002028 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
2029 RTL_R16(tp, CPlusCmd);
Francois Romieu50970832017-10-27 13:24:49 +03002030
2031 rtl_unlock_work(tp);
2032
2033 return 0;
2034}
2035
Jeff Garzik7282d492006-09-13 14:30:00 -04002036static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 .get_drvinfo = rtl8169_get_drvinfo,
2038 .get_regs_len = rtl8169_get_regs_len,
2039 .get_link = ethtool_op_get_link,
Francois Romieu50970832017-10-27 13:24:49 +03002040 .get_coalesce = rtl_get_coalesce,
2041 .set_coalesce = rtl_set_coalesce,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002042 .get_msglevel = rtl8169_get_msglevel,
2043 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01002045 .get_wol = rtl8169_get_wol,
2046 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002047 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002048 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02002049 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Richard Cochrane1593bb2012-04-03 22:59:35 +00002050 .get_ts_info = ethtool_op_get_ts_info,
Heiner Kallweitdd849572018-07-17 22:51:48 +02002051 .nway_reset = phy_ethtool_nway_reset,
Heiner Kallweit45772432018-07-17 22:51:44 +02002052 .get_link_ksettings = phy_ethtool_get_link_ksettings,
2053 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054};
2055
Francois Romieu07d3f512007-02-21 22:40:46 +01002056static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Heiner Kallweit22148df2018-04-22 17:15:15 +02002057 u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Francois Romieu0e485152007-02-20 00:00:26 +01002059 /*
2060 * The driver currently handles the 8168Bf and the 8168Be identically
2061 * but they can be identified more specifically through the test below
2062 * if needed:
2063 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002064 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01002065 *
2066 * Same thing for the 8101Eb and the 8101Ec:
2067 *
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002068 * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01002069 */
Francois Romieu37441002011-06-17 22:58:54 +02002070 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002072 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 int mac_version;
2074 } mac_info[] = {
Chun-Hao Lin935e2212014-10-07 15:10:41 +08002075 /* 8168EP family. */
2076 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2077 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2078 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2079
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002080 /* 8168H family. */
2081 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2082 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2083
Hayes Wangc5583862012-07-02 17:23:22 +08002084 /* 8168G family. */
hayeswang45dd95c2013-07-08 17:09:01 +08002085 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
hayeswang57538c42013-04-01 22:23:40 +00002086 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
Hayes Wangc5583862012-07-02 17:23:22 +08002087 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2088 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2089
Hayes Wangc2218922011-09-06 16:55:18 +08002090 /* 8168F family. */
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08002091 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
Hayes Wangc2218922011-09-06 16:55:18 +08002092 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2093 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2094
hayeswang01dc7fe2011-03-21 01:50:28 +00002095 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08002096 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00002097 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2098 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2099
Francois Romieu5b538df2008-07-20 16:22:45 +02002100 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00002101 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00002102 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002103
françois romieue6de30d2011-01-03 15:08:37 +00002104 /* 8168DP family. */
2105 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2106 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00002107 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00002108
Francois Romieuef808d52008-06-29 13:10:54 +02002109 /* 8168C family. */
Francois Romieuef3386f2008-06-29 12:24:30 +02002110 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02002111 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002112 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002113 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2114 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02002115 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieuef808d52008-06-29 13:10:54 +02002116 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002117
2118 /* 8168B family. */
2119 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002120 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2121 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2122
2123 /* 8101 family. */
Hayes Wang5598bfe2012-07-02 17:23:21 +08002124 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
Hayes Wang7e18dca2012-03-30 14:33:02 +08002125 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08002126 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2127 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002128 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2129 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2130 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2131 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002132 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002133 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002134 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02002135 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2136 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002137 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2138 /* FIXME: where did these entries come from ? -- FR */
2139 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2140 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2141
2142 /* 8110 family. */
2143 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2144 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2145 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2146 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2147 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2148 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2149
Jean Delvaref21b75e2009-05-26 20:54:48 -07002150 /* Catch-all */
2151 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02002152 };
2153 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 u32 reg;
2155
Andy Shevchenko1ef72862018-03-01 13:27:34 +02002156 reg = RTL_R32(tp, TxConfig);
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002157 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 p++;
2159 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02002160
2161 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02002162 dev_notice(tp_to_dev(tp),
2163 "unknown MAC, using family default\n");
Francois Romieu5d320a22011-05-08 17:47:36 +02002164 tp->mac_version = default_version;
hayeswang58152cd2013-04-01 22:23:42 +00002165 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02002166 tp->mac_version = tp->supports_gmii ?
hayeswang58152cd2013-04-01 22:23:42 +00002167 RTL_GIGA_MAC_VER_42 :
2168 RTL_GIGA_MAC_VER_43;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002169 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02002170 tp->mac_version = tp->supports_gmii ?
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002171 RTL_GIGA_MAC_VER_45 :
2172 RTL_GIGA_MAC_VER_47;
2173 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02002174 tp->mac_version = tp->supports_gmii ?
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08002175 RTL_GIGA_MAC_VER_46 :
2176 RTL_GIGA_MAC_VER_48;
Francois Romieu5d320a22011-05-08 17:47:36 +02002177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
2180static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2181{
Heiner Kallweit49d17512018-06-28 20:36:15 +02002182 netif_dbg(tp, drv, tp->dev, "mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
Francois Romieu867763c2007-08-17 18:21:58 +02002185struct phy_reg {
2186 u16 reg;
2187 u16 val;
2188};
2189
françois romieu4da19632011-01-03 15:07:55 +00002190static void rtl_writephy_batch(struct rtl8169_private *tp,
2191 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02002192{
2193 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00002194 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02002195 regs++;
2196 }
2197}
2198
françois romieubca03d52011-01-03 15:07:31 +00002199#define PHY_READ 0x00000000
2200#define PHY_DATA_OR 0x10000000
2201#define PHY_DATA_AND 0x20000000
2202#define PHY_BJMPN 0x30000000
hayeswangeee37862013-04-01 22:23:38 +00002203#define PHY_MDIO_CHG 0x40000000
françois romieubca03d52011-01-03 15:07:31 +00002204#define PHY_CLEAR_READCOUNT 0x70000000
2205#define PHY_WRITE 0x80000000
2206#define PHY_READCOUNT_EQ_SKIP 0x90000000
2207#define PHY_COMP_EQ_SKIPN 0xa0000000
2208#define PHY_COMP_NEQ_SKIPN 0xb0000000
2209#define PHY_WRITE_PREVIOUS 0xc0000000
2210#define PHY_SKIPN 0xd0000000
2211#define PHY_DELAY_MS 0xe0000000
françois romieubca03d52011-01-03 15:07:31 +00002212
Hayes Wang960aee62011-06-18 11:37:48 +02002213struct fw_info {
2214 u32 magic;
2215 char version[RTL_VER_SIZE];
2216 __le32 fw_start;
2217 __le32 fw_len;
2218 u8 chksum;
2219} __packed;
2220
Francois Romieu1c361ef2011-06-17 17:16:24 +02002221#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2222
2223static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00002224{
Francois Romieub6ffd972011-06-17 17:00:05 +02002225 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02002226 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002227 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2228 char *version = rtl_fw->version;
2229 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00002230
Francois Romieu1c361ef2011-06-17 17:16:24 +02002231 if (fw->size < FW_OPCODE_SIZE)
2232 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02002233
2234 if (!fw_info->magic) {
2235 size_t i, size, start;
2236 u8 checksum = 0;
2237
2238 if (fw->size < sizeof(*fw_info))
2239 goto out;
2240
2241 for (i = 0; i < fw->size; i++)
2242 checksum += fw->data[i];
2243 if (checksum != 0)
2244 goto out;
2245
2246 start = le32_to_cpu(fw_info->fw_start);
2247 if (start > fw->size)
2248 goto out;
2249
2250 size = le32_to_cpu(fw_info->fw_len);
2251 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2252 goto out;
2253
2254 memcpy(version, fw_info->version, RTL_VER_SIZE);
2255
2256 pa->code = (__le32 *)(fw->data + start);
2257 pa->size = size;
2258 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02002259 if (fw->size % FW_OPCODE_SIZE)
2260 goto out;
2261
2262 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2263
2264 pa->code = (__le32 *)fw->data;
2265 pa->size = fw->size / FW_OPCODE_SIZE;
2266 }
2267 version[RTL_VER_SIZE - 1] = 0;
2268
2269 rc = true;
2270out:
2271 return rc;
2272}
2273
Francois Romieufd112f22011-06-18 00:10:29 +02002274static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2275 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02002276{
Francois Romieufd112f22011-06-18 00:10:29 +02002277 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02002278 size_t index;
2279
Francois Romieu1c361ef2011-06-17 17:16:24 +02002280 for (index = 0; index < pa->size; index++) {
2281 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00002282 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00002283
hayeswang42b82dc2011-01-10 02:07:25 +00002284 switch(action & 0xf0000000) {
2285 case PHY_READ:
2286 case PHY_DATA_OR:
2287 case PHY_DATA_AND:
hayeswangeee37862013-04-01 22:23:38 +00002288 case PHY_MDIO_CHG:
hayeswang42b82dc2011-01-10 02:07:25 +00002289 case PHY_CLEAR_READCOUNT:
2290 case PHY_WRITE:
2291 case PHY_WRITE_PREVIOUS:
2292 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00002293 break;
2294
hayeswang42b82dc2011-01-10 02:07:25 +00002295 case PHY_BJMPN:
2296 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02002297 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002298 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002299 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002300 }
2301 break;
2302 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002303 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002304 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002305 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002306 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002307 }
2308 break;
2309 case PHY_COMP_EQ_SKIPN:
2310 case PHY_COMP_NEQ_SKIPN:
2311 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02002312 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02002313 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002314 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002315 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00002316 }
2317 break;
2318
hayeswang42b82dc2011-01-10 02:07:25 +00002319 default:
Francois Romieufd112f22011-06-18 00:10:29 +02002320 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00002321 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02002322 goto out;
françois romieubca03d52011-01-03 15:07:31 +00002323 }
2324 }
Francois Romieufd112f22011-06-18 00:10:29 +02002325 rc = true;
2326out:
2327 return rc;
2328}
françois romieubca03d52011-01-03 15:07:31 +00002329
Francois Romieufd112f22011-06-18 00:10:29 +02002330static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2331{
2332 struct net_device *dev = tp->dev;
2333 int rc = -EINVAL;
2334
2335 if (!rtl_fw_format_ok(tp, rtl_fw)) {
Yannick Guerrini5c2d2b12015-02-24 13:03:51 +01002336 netif_err(tp, ifup, dev, "invalid firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02002337 goto out;
2338 }
2339
2340 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2341 rc = 0;
2342out:
2343 return rc;
2344}
2345
2346static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2347{
2348 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
hayeswangeee37862013-04-01 22:23:38 +00002349 struct mdio_ops org, *ops = &tp->mdio_ops;
Francois Romieufd112f22011-06-18 00:10:29 +02002350 u32 predata, count;
2351 size_t index;
2352
2353 predata = count = 0;
hayeswangeee37862013-04-01 22:23:38 +00002354 org.write = ops->write;
2355 org.read = ops->read;
hayeswang42b82dc2011-01-10 02:07:25 +00002356
Francois Romieu1c361ef2011-06-17 17:16:24 +02002357 for (index = 0; index < pa->size; ) {
2358 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002359 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002360 u32 regno = (action & 0x0fff0000) >> 16;
2361
2362 if (!action)
2363 break;
françois romieubca03d52011-01-03 15:07:31 +00002364
2365 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002366 case PHY_READ:
2367 predata = rtl_readphy(tp, regno);
2368 count++;
2369 index++;
françois romieubca03d52011-01-03 15:07:31 +00002370 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002371 case PHY_DATA_OR:
2372 predata |= data;
2373 index++;
2374 break;
2375 case PHY_DATA_AND:
2376 predata &= data;
2377 index++;
2378 break;
2379 case PHY_BJMPN:
2380 index -= regno;
2381 break;
hayeswangeee37862013-04-01 22:23:38 +00002382 case PHY_MDIO_CHG:
2383 if (data == 0) {
2384 ops->write = org.write;
2385 ops->read = org.read;
2386 } else if (data == 1) {
2387 ops->write = mac_mcu_write;
2388 ops->read = mac_mcu_read;
2389 }
2390
hayeswang42b82dc2011-01-10 02:07:25 +00002391 index++;
2392 break;
2393 case PHY_CLEAR_READCOUNT:
2394 count = 0;
2395 index++;
2396 break;
2397 case PHY_WRITE:
2398 rtl_writephy(tp, regno, data);
2399 index++;
2400 break;
2401 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002402 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002403 break;
2404 case PHY_COMP_EQ_SKIPN:
2405 if (predata == data)
2406 index += regno;
2407 index++;
2408 break;
2409 case PHY_COMP_NEQ_SKIPN:
2410 if (predata != data)
2411 index += regno;
2412 index++;
2413 break;
2414 case PHY_WRITE_PREVIOUS:
2415 rtl_writephy(tp, regno, predata);
2416 index++;
2417 break;
2418 case PHY_SKIPN:
2419 index += regno + 1;
2420 break;
2421 case PHY_DELAY_MS:
2422 mdelay(data);
2423 index++;
2424 break;
2425
françois romieubca03d52011-01-03 15:07:31 +00002426 default:
2427 BUG();
2428 }
2429 }
hayeswangeee37862013-04-01 22:23:38 +00002430
2431 ops->write = org.write;
2432 ops->read = org.read;
françois romieubca03d52011-01-03 15:07:31 +00002433}
2434
françois romieuf1e02ed2011-01-13 13:07:53 +00002435static void rtl_release_firmware(struct rtl8169_private *tp)
2436{
Francois Romieub6ffd972011-06-17 17:00:05 +02002437 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2438 release_firmware(tp->rtl_fw->fw);
2439 kfree(tp->rtl_fw);
2440 }
2441 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002442}
2443
François Romieu953a12c2011-04-24 17:38:48 +02002444static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002445{
Francois Romieub6ffd972011-06-17 17:00:05 +02002446 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002447
2448 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieueef63cc2013-02-08 23:43:20 +01002449 if (!IS_ERR_OR_NULL(rtl_fw))
Francois Romieub6ffd972011-06-17 17:00:05 +02002450 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002451}
2452
2453static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2454{
2455 if (rtl_readphy(tp, reg) != val)
2456 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2457 else
2458 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002459}
2460
françois romieu4da19632011-01-03 15:07:55 +00002461static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002463 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002464 { 0x1f, 0x0001 },
2465 { 0x06, 0x006e },
2466 { 0x08, 0x0708 },
2467 { 0x15, 0x4000 },
2468 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
françois romieu0b9b5712009-08-10 19:44:56 +00002470 { 0x1f, 0x0001 },
2471 { 0x03, 0x00a1 },
2472 { 0x02, 0x0008 },
2473 { 0x01, 0x0120 },
2474 { 0x00, 0x1000 },
2475 { 0x04, 0x0800 },
2476 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
françois romieu0b9b5712009-08-10 19:44:56 +00002478 { 0x03, 0xff41 },
2479 { 0x02, 0xdf60 },
2480 { 0x01, 0x0140 },
2481 { 0x00, 0x0077 },
2482 { 0x04, 0x7800 },
2483 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
françois romieu0b9b5712009-08-10 19:44:56 +00002485 { 0x03, 0x802f },
2486 { 0x02, 0x4f02 },
2487 { 0x01, 0x0409 },
2488 { 0x00, 0xf0f9 },
2489 { 0x04, 0x9800 },
2490 { 0x04, 0x9000 },
2491
2492 { 0x03, 0xdf01 },
2493 { 0x02, 0xdf20 },
2494 { 0x01, 0xff95 },
2495 { 0x00, 0xba00 },
2496 { 0x04, 0xa800 },
2497 { 0x04, 0xa000 },
2498
2499 { 0x03, 0xff41 },
2500 { 0x02, 0xdf20 },
2501 { 0x01, 0x0140 },
2502 { 0x00, 0x00bb },
2503 { 0x04, 0xb800 },
2504 { 0x04, 0xb000 },
2505
2506 { 0x03, 0xdf41 },
2507 { 0x02, 0xdc60 },
2508 { 0x01, 0x6340 },
2509 { 0x00, 0x007d },
2510 { 0x04, 0xd800 },
2511 { 0x04, 0xd000 },
2512
2513 { 0x03, 0xdf01 },
2514 { 0x02, 0xdf20 },
2515 { 0x01, 0x100a },
2516 { 0x00, 0xa0ff },
2517 { 0x04, 0xf800 },
2518 { 0x04, 0xf000 },
2519
2520 { 0x1f, 0x0000 },
2521 { 0x0b, 0x0000 },
2522 { 0x00, 0x9200 }
2523 };
2524
françois romieu4da19632011-01-03 15:07:55 +00002525 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
françois romieu4da19632011-01-03 15:07:55 +00002528static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002529{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002530 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002531 { 0x1f, 0x0002 },
2532 { 0x01, 0x90d0 },
2533 { 0x1f, 0x0000 }
2534 };
2535
françois romieu4da19632011-01-03 15:07:55 +00002536 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002537}
2538
françois romieu4da19632011-01-03 15:07:55 +00002539static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002540{
2541 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002542
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002543 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2544 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002545 return;
2546
françois romieu4da19632011-01-03 15:07:55 +00002547 rtl_writephy(tp, 0x1f, 0x0001);
2548 rtl_writephy(tp, 0x10, 0xf01b);
2549 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002550}
2551
françois romieu4da19632011-01-03 15:07:55 +00002552static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002553{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002554 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002555 { 0x1f, 0x0001 },
2556 { 0x04, 0x0000 },
2557 { 0x03, 0x00a1 },
2558 { 0x02, 0x0008 },
2559 { 0x01, 0x0120 },
2560 { 0x00, 0x1000 },
2561 { 0x04, 0x0800 },
2562 { 0x04, 0x9000 },
2563 { 0x03, 0x802f },
2564 { 0x02, 0x4f02 },
2565 { 0x01, 0x0409 },
2566 { 0x00, 0xf099 },
2567 { 0x04, 0x9800 },
2568 { 0x04, 0xa000 },
2569 { 0x03, 0xdf01 },
2570 { 0x02, 0xdf20 },
2571 { 0x01, 0xff95 },
2572 { 0x00, 0xba00 },
2573 { 0x04, 0xa800 },
2574 { 0x04, 0xf000 },
2575 { 0x03, 0xdf01 },
2576 { 0x02, 0xdf20 },
2577 { 0x01, 0x101a },
2578 { 0x00, 0xa0ff },
2579 { 0x04, 0xf800 },
2580 { 0x04, 0x0000 },
2581 { 0x1f, 0x0000 },
2582
2583 { 0x1f, 0x0001 },
2584 { 0x10, 0xf41b },
2585 { 0x14, 0xfb54 },
2586 { 0x18, 0xf5c7 },
2587 { 0x1f, 0x0000 },
2588
2589 { 0x1f, 0x0001 },
2590 { 0x17, 0x0cc0 },
2591 { 0x1f, 0x0000 }
2592 };
2593
françois romieu4da19632011-01-03 15:07:55 +00002594 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002595
françois romieu4da19632011-01-03 15:07:55 +00002596 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002597}
2598
françois romieu4da19632011-01-03 15:07:55 +00002599static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002600{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002601 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002602 { 0x1f, 0x0001 },
2603 { 0x04, 0x0000 },
2604 { 0x03, 0x00a1 },
2605 { 0x02, 0x0008 },
2606 { 0x01, 0x0120 },
2607 { 0x00, 0x1000 },
2608 { 0x04, 0x0800 },
2609 { 0x04, 0x9000 },
2610 { 0x03, 0x802f },
2611 { 0x02, 0x4f02 },
2612 { 0x01, 0x0409 },
2613 { 0x00, 0xf099 },
2614 { 0x04, 0x9800 },
2615 { 0x04, 0xa000 },
2616 { 0x03, 0xdf01 },
2617 { 0x02, 0xdf20 },
2618 { 0x01, 0xff95 },
2619 { 0x00, 0xba00 },
2620 { 0x04, 0xa800 },
2621 { 0x04, 0xf000 },
2622 { 0x03, 0xdf01 },
2623 { 0x02, 0xdf20 },
2624 { 0x01, 0x101a },
2625 { 0x00, 0xa0ff },
2626 { 0x04, 0xf800 },
2627 { 0x04, 0x0000 },
2628 { 0x1f, 0x0000 },
2629
2630 { 0x1f, 0x0001 },
2631 { 0x0b, 0x8480 },
2632 { 0x1f, 0x0000 },
2633
2634 { 0x1f, 0x0001 },
2635 { 0x18, 0x67c7 },
2636 { 0x04, 0x2000 },
2637 { 0x03, 0x002f },
2638 { 0x02, 0x4360 },
2639 { 0x01, 0x0109 },
2640 { 0x00, 0x3022 },
2641 { 0x04, 0x2800 },
2642 { 0x1f, 0x0000 },
2643
2644 { 0x1f, 0x0001 },
2645 { 0x17, 0x0cc0 },
2646 { 0x1f, 0x0000 }
2647 };
2648
françois romieu4da19632011-01-03 15:07:55 +00002649 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002650}
2651
françois romieu4da19632011-01-03 15:07:55 +00002652static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002653{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002654 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002655 { 0x10, 0xf41b },
2656 { 0x1f, 0x0000 }
2657 };
2658
françois romieu4da19632011-01-03 15:07:55 +00002659 rtl_writephy(tp, 0x1f, 0x0001);
2660 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002661
françois romieu4da19632011-01-03 15:07:55 +00002662 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002663}
2664
françois romieu4da19632011-01-03 15:07:55 +00002665static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002666{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002667 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002668 { 0x1f, 0x0001 },
2669 { 0x10, 0xf41b },
2670 { 0x1f, 0x0000 }
2671 };
2672
françois romieu4da19632011-01-03 15:07:55 +00002673 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002674}
2675
françois romieu4da19632011-01-03 15:07:55 +00002676static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002677{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002678 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002679 { 0x1f, 0x0000 },
2680 { 0x1d, 0x0f00 },
2681 { 0x1f, 0x0002 },
2682 { 0x0c, 0x1ec8 },
2683 { 0x1f, 0x0000 }
2684 };
2685
françois romieu4da19632011-01-03 15:07:55 +00002686 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002687}
2688
françois romieu4da19632011-01-03 15:07:55 +00002689static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002690{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002691 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002692 { 0x1f, 0x0001 },
2693 { 0x1d, 0x3d98 },
2694 { 0x1f, 0x0000 }
2695 };
2696
françois romieu4da19632011-01-03 15:07:55 +00002697 rtl_writephy(tp, 0x1f, 0x0000);
2698 rtl_patchphy(tp, 0x14, 1 << 5);
2699 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002700
françois romieu4da19632011-01-03 15:07:55 +00002701 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002702}
2703
françois romieu4da19632011-01-03 15:07:55 +00002704static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002705{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002706 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002707 { 0x1f, 0x0001 },
2708 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002709 { 0x1f, 0x0002 },
2710 { 0x00, 0x88d4 },
2711 { 0x01, 0x82b1 },
2712 { 0x03, 0x7002 },
2713 { 0x08, 0x9e30 },
2714 { 0x09, 0x01f0 },
2715 { 0x0a, 0x5500 },
2716 { 0x0c, 0x00c8 },
2717 { 0x1f, 0x0003 },
2718 { 0x12, 0xc096 },
2719 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002720 { 0x1f, 0x0000 },
2721 { 0x1f, 0x0000 },
2722 { 0x09, 0x2000 },
2723 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002724 };
2725
françois romieu4da19632011-01-03 15:07:55 +00002726 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002727
françois romieu4da19632011-01-03 15:07:55 +00002728 rtl_patchphy(tp, 0x14, 1 << 5);
2729 rtl_patchphy(tp, 0x0d, 1 << 5);
2730 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002731}
2732
françois romieu4da19632011-01-03 15:07:55 +00002733static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002734{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002735 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002736 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002737 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002738 { 0x03, 0x802f },
2739 { 0x02, 0x4f02 },
2740 { 0x01, 0x0409 },
2741 { 0x00, 0xf099 },
2742 { 0x04, 0x9800 },
2743 { 0x04, 0x9000 },
2744 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002745 { 0x1f, 0x0002 },
2746 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002747 { 0x06, 0x0761 },
2748 { 0x1f, 0x0003 },
2749 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002750 { 0x1f, 0x0000 }
2751 };
2752
françois romieu4da19632011-01-03 15:07:55 +00002753 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002754
françois romieu4da19632011-01-03 15:07:55 +00002755 rtl_patchphy(tp, 0x16, 1 << 0);
2756 rtl_patchphy(tp, 0x14, 1 << 5);
2757 rtl_patchphy(tp, 0x0d, 1 << 5);
2758 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002759}
2760
françois romieu4da19632011-01-03 15:07:55 +00002761static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002762{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002763 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002764 { 0x1f, 0x0001 },
2765 { 0x12, 0x2300 },
2766 { 0x1d, 0x3d98 },
2767 { 0x1f, 0x0002 },
2768 { 0x0c, 0x7eb8 },
2769 { 0x06, 0x5461 },
2770 { 0x1f, 0x0003 },
2771 { 0x16, 0x0f0a },
2772 { 0x1f, 0x0000 }
2773 };
2774
françois romieu4da19632011-01-03 15:07:55 +00002775 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002776
françois romieu4da19632011-01-03 15:07:55 +00002777 rtl_patchphy(tp, 0x16, 1 << 0);
2778 rtl_patchphy(tp, 0x14, 1 << 5);
2779 rtl_patchphy(tp, 0x0d, 1 << 5);
2780 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002781}
2782
françois romieu4da19632011-01-03 15:07:55 +00002783static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002784{
françois romieu4da19632011-01-03 15:07:55 +00002785 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002786}
2787
françois romieubca03d52011-01-03 15:07:31 +00002788static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002789{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002790 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002791 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002792 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002793 { 0x06, 0x4064 },
2794 { 0x07, 0x2863 },
2795 { 0x08, 0x059c },
2796 { 0x09, 0x26b4 },
2797 { 0x0a, 0x6a19 },
2798 { 0x0b, 0xdcc8 },
2799 { 0x10, 0xf06d },
2800 { 0x14, 0x7f68 },
2801 { 0x18, 0x7fd9 },
2802 { 0x1c, 0xf0ff },
2803 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002804 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002805 { 0x12, 0xf49f },
2806 { 0x13, 0x070b },
2807 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002808 { 0x14, 0x94c0 },
2809
2810 /*
2811 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002812 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002813 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002814 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002815 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002816 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002817 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002818 { 0x06, 0x5561 },
2819
2820 /*
2821 * Can not link to 1Gbps with bad cable
2822 * Decrease SNR threshold form 21.07dB to 19.04dB
2823 */
2824 { 0x1f, 0x0001 },
2825 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002826
2827 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002828 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002829 };
2830
françois romieu4da19632011-01-03 15:07:55 +00002831 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002832
françois romieubca03d52011-01-03 15:07:31 +00002833 /*
2834 * Rx Error Issue
2835 * Fine Tune Switching regulator parameter
2836 */
françois romieu4da19632011-01-03 15:07:55 +00002837 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002838 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2839 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002840
Francois Romieufdf6fc02012-07-06 22:40:38 +02002841 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002842 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002843 { 0x1f, 0x0002 },
2844 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002845 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002846 { 0x05, 0x8330 },
2847 { 0x06, 0x669a },
2848 { 0x1f, 0x0002 }
2849 };
2850 int val;
2851
françois romieu4da19632011-01-03 15:07:55 +00002852 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002853
françois romieu4da19632011-01-03 15:07:55 +00002854 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002855
2856 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002857 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002858 0x0065, 0x0066, 0x0067, 0x0068,
2859 0x0069, 0x006a, 0x006b, 0x006c
2860 };
2861 int i;
2862
françois romieu4da19632011-01-03 15:07:55 +00002863 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002864
2865 val &= 0xff00;
2866 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002867 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002868 }
2869 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002870 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002871 { 0x1f, 0x0002 },
2872 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002873 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002874 { 0x05, 0x8330 },
2875 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002876 };
2877
françois romieu4da19632011-01-03 15:07:55 +00002878 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002879 }
2880
françois romieubca03d52011-01-03 15:07:31 +00002881 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002882 rtl_writephy(tp, 0x1f, 0x0002);
2883 rtl_patchphy(tp, 0x0d, 0x0300);
2884 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002885
françois romieubca03d52011-01-03 15:07:31 +00002886 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002887 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002888 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2889 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002890
françois romieu4da19632011-01-03 15:07:55 +00002891 rtl_writephy(tp, 0x1f, 0x0005);
2892 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002893
2894 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002895
françois romieu4da19632011-01-03 15:07:55 +00002896 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002897}
2898
françois romieubca03d52011-01-03 15:07:31 +00002899static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002900{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002901 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002902 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002903 { 0x1f, 0x0001 },
2904 { 0x06, 0x4064 },
2905 { 0x07, 0x2863 },
2906 { 0x08, 0x059c },
2907 { 0x09, 0x26b4 },
2908 { 0x0a, 0x6a19 },
2909 { 0x0b, 0xdcc8 },
2910 { 0x10, 0xf06d },
2911 { 0x14, 0x7f68 },
2912 { 0x18, 0x7fd9 },
2913 { 0x1c, 0xf0ff },
2914 { 0x1d, 0x3d9c },
2915 { 0x1f, 0x0003 },
2916 { 0x12, 0xf49f },
2917 { 0x13, 0x070b },
2918 { 0x1a, 0x05ad },
2919 { 0x14, 0x94c0 },
2920
françois romieubca03d52011-01-03 15:07:31 +00002921 /*
2922 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002923 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002924 */
françois romieudaf9df62009-10-07 12:44:20 +00002925 { 0x1f, 0x0002 },
2926 { 0x06, 0x5561 },
2927 { 0x1f, 0x0005 },
2928 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002929 { 0x06, 0x5561 },
2930
2931 /*
2932 * Can not link to 1Gbps with bad cable
2933 * Decrease SNR threshold form 21.07dB to 19.04dB
2934 */
2935 { 0x1f, 0x0001 },
2936 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002937
2938 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002939 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002940 };
2941
françois romieu4da19632011-01-03 15:07:55 +00002942 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002943
Francois Romieufdf6fc02012-07-06 22:40:38 +02002944 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002945 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002946 { 0x1f, 0x0002 },
2947 { 0x05, 0x669a },
2948 { 0x1f, 0x0005 },
2949 { 0x05, 0x8330 },
2950 { 0x06, 0x669a },
2951
2952 { 0x1f, 0x0002 }
2953 };
2954 int val;
2955
françois romieu4da19632011-01-03 15:07:55 +00002956 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002957
françois romieu4da19632011-01-03 15:07:55 +00002958 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002959 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002960 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002961 0x0065, 0x0066, 0x0067, 0x0068,
2962 0x0069, 0x006a, 0x006b, 0x006c
2963 };
2964 int i;
2965
françois romieu4da19632011-01-03 15:07:55 +00002966 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002967
2968 val &= 0xff00;
2969 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002970 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002971 }
2972 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002973 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002974 { 0x1f, 0x0002 },
2975 { 0x05, 0x2642 },
2976 { 0x1f, 0x0005 },
2977 { 0x05, 0x8330 },
2978 { 0x06, 0x2642 }
2979 };
2980
françois romieu4da19632011-01-03 15:07:55 +00002981 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002982 }
2983
françois romieubca03d52011-01-03 15:07:31 +00002984 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002985 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08002986 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
2987 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002988
françois romieubca03d52011-01-03 15:07:31 +00002989 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002990 rtl_writephy(tp, 0x1f, 0x0002);
2991 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002992
françois romieu4da19632011-01-03 15:07:55 +00002993 rtl_writephy(tp, 0x1f, 0x0005);
2994 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002995
2996 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002997
françois romieu4da19632011-01-03 15:07:55 +00002998 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002999}
3000
françois romieu4da19632011-01-03 15:07:55 +00003001static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00003002{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003003 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00003004 { 0x1f, 0x0002 },
3005 { 0x10, 0x0008 },
3006 { 0x0d, 0x006c },
3007
3008 { 0x1f, 0x0000 },
3009 { 0x0d, 0xf880 },
3010
3011 { 0x1f, 0x0001 },
3012 { 0x17, 0x0cc0 },
3013
3014 { 0x1f, 0x0001 },
3015 { 0x0b, 0xa4d8 },
3016 { 0x09, 0x281c },
3017 { 0x07, 0x2883 },
3018 { 0x0a, 0x6b35 },
3019 { 0x1d, 0x3da4 },
3020 { 0x1c, 0xeffd },
3021 { 0x14, 0x7f52 },
3022 { 0x18, 0x7fc6 },
3023 { 0x08, 0x0601 },
3024 { 0x06, 0x4063 },
3025 { 0x10, 0xf074 },
3026 { 0x1f, 0x0003 },
3027 { 0x13, 0x0789 },
3028 { 0x12, 0xf4bd },
3029 { 0x1a, 0x04fd },
3030 { 0x14, 0x84b0 },
3031 { 0x1f, 0x0000 },
3032 { 0x00, 0x9200 },
3033
3034 { 0x1f, 0x0005 },
3035 { 0x01, 0x0340 },
3036 { 0x1f, 0x0001 },
3037 { 0x04, 0x4000 },
3038 { 0x03, 0x1d21 },
3039 { 0x02, 0x0c32 },
3040 { 0x01, 0x0200 },
3041 { 0x00, 0x5554 },
3042 { 0x04, 0x4800 },
3043 { 0x04, 0x4000 },
3044 { 0x04, 0xf000 },
3045 { 0x03, 0xdf01 },
3046 { 0x02, 0xdf20 },
3047 { 0x01, 0x101a },
3048 { 0x00, 0xa0ff },
3049 { 0x04, 0xf800 },
3050 { 0x04, 0xf000 },
3051 { 0x1f, 0x0000 },
3052
3053 { 0x1f, 0x0007 },
3054 { 0x1e, 0x0023 },
3055 { 0x16, 0x0000 },
3056 { 0x1f, 0x0000 }
3057 };
3058
françois romieu4da19632011-01-03 15:07:55 +00003059 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02003060}
3061
françois romieue6de30d2011-01-03 15:08:37 +00003062static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3063{
3064 static const struct phy_reg phy_reg_init[] = {
3065 { 0x1f, 0x0001 },
3066 { 0x17, 0x0cc0 },
3067
3068 { 0x1f, 0x0007 },
3069 { 0x1e, 0x002d },
3070 { 0x18, 0x0040 },
3071 { 0x1f, 0x0000 }
3072 };
3073
3074 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3075 rtl_patchphy(tp, 0x0d, 1 << 5);
3076}
3077
Hayes Wang70090422011-07-06 15:58:06 +08003078static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00003079{
3080 static const struct phy_reg phy_reg_init[] = {
3081 /* Enable Delay cap */
3082 { 0x1f, 0x0005 },
3083 { 0x05, 0x8b80 },
3084 { 0x06, 0xc896 },
3085 { 0x1f, 0x0000 },
3086
3087 /* Channel estimation fine tune */
3088 { 0x1f, 0x0001 },
3089 { 0x0b, 0x6c20 },
3090 { 0x07, 0x2872 },
3091 { 0x1c, 0xefff },
3092 { 0x1f, 0x0003 },
3093 { 0x14, 0x6420 },
3094 { 0x1f, 0x0000 },
3095
3096 /* Update PFM & 10M TX idle timer */
3097 { 0x1f, 0x0007 },
3098 { 0x1e, 0x002f },
3099 { 0x15, 0x1919 },
3100 { 0x1f, 0x0000 },
3101
3102 { 0x1f, 0x0007 },
3103 { 0x1e, 0x00ac },
3104 { 0x18, 0x0006 },
3105 { 0x1f, 0x0000 }
3106 };
3107
Francois Romieu15ecd032011-04-27 13:52:22 -07003108 rtl_apply_firmware(tp);
3109
hayeswang01dc7fe2011-03-21 01:50:28 +00003110 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3111
3112 /* DCO enable for 10M IDLE Power */
3113 rtl_writephy(tp, 0x1f, 0x0007);
3114 rtl_writephy(tp, 0x1e, 0x0023);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003115 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003116 rtl_writephy(tp, 0x1f, 0x0000);
3117
3118 /* For impedance matching */
3119 rtl_writephy(tp, 0x1f, 0x0002);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003120 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003121 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003122
3123 /* PHY auto speed down */
3124 rtl_writephy(tp, 0x1f, 0x0007);
3125 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003126 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003127 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003128 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003129
3130 rtl_writephy(tp, 0x1f, 0x0005);
3131 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003132 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003133 rtl_writephy(tp, 0x1f, 0x0000);
3134
3135 rtl_writephy(tp, 0x1f, 0x0005);
3136 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003137 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003138 rtl_writephy(tp, 0x1f, 0x0007);
3139 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003140 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
hayeswang01dc7fe2011-03-21 01:50:28 +00003141 rtl_writephy(tp, 0x1f, 0x0006);
3142 rtl_writephy(tp, 0x00, 0x5a00);
3143 rtl_writephy(tp, 0x1f, 0x0000);
3144 rtl_writephy(tp, 0x0d, 0x0007);
3145 rtl_writephy(tp, 0x0e, 0x003c);
3146 rtl_writephy(tp, 0x0d, 0x4007);
3147 rtl_writephy(tp, 0x0e, 0x0000);
3148 rtl_writephy(tp, 0x0d, 0x0000);
3149}
3150
françois romieu9ecb9aa2012-12-07 11:20:21 +00003151static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3152{
3153 const u16 w[] = {
3154 addr[0] | (addr[1] << 8),
3155 addr[2] | (addr[3] << 8),
3156 addr[4] | (addr[5] << 8)
3157 };
3158 const struct exgmac_reg e[] = {
3159 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3160 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3161 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3162 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3163 };
3164
3165 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3166}
3167
Hayes Wang70090422011-07-06 15:58:06 +08003168static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3169{
3170 static const struct phy_reg phy_reg_init[] = {
3171 /* Enable Delay cap */
3172 { 0x1f, 0x0004 },
3173 { 0x1f, 0x0007 },
3174 { 0x1e, 0x00ac },
3175 { 0x18, 0x0006 },
3176 { 0x1f, 0x0002 },
3177 { 0x1f, 0x0000 },
3178 { 0x1f, 0x0000 },
3179
3180 /* Channel estimation fine tune */
3181 { 0x1f, 0x0003 },
3182 { 0x09, 0xa20f },
3183 { 0x1f, 0x0000 },
3184 { 0x1f, 0x0000 },
3185
3186 /* Green Setting */
3187 { 0x1f, 0x0005 },
3188 { 0x05, 0x8b5b },
3189 { 0x06, 0x9222 },
3190 { 0x05, 0x8b6d },
3191 { 0x06, 0x8000 },
3192 { 0x05, 0x8b76 },
3193 { 0x06, 0x8000 },
3194 { 0x1f, 0x0000 }
3195 };
3196
3197 rtl_apply_firmware(tp);
3198
3199 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3200
3201 /* For 4-corner performance improve */
3202 rtl_writephy(tp, 0x1f, 0x0005);
3203 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003204 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003205 rtl_writephy(tp, 0x1f, 0x0000);
3206
3207 /* PHY auto speed down */
3208 rtl_writephy(tp, 0x1f, 0x0004);
3209 rtl_writephy(tp, 0x1f, 0x0007);
3210 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003211 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003212 rtl_writephy(tp, 0x1f, 0x0002);
3213 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003214 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003215
3216 /* improve 10M EEE waveform */
3217 rtl_writephy(tp, 0x1f, 0x0005);
3218 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003219 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003220 rtl_writephy(tp, 0x1f, 0x0000);
3221
3222 /* Improve 2-pair detection performance */
3223 rtl_writephy(tp, 0x1f, 0x0005);
3224 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003225 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003226 rtl_writephy(tp, 0x1f, 0x0000);
3227
3228 /* EEE setting */
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003229 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08003230 rtl_writephy(tp, 0x1f, 0x0005);
3231 rtl_writephy(tp, 0x05, 0x8b85);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003232 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003233 rtl_writephy(tp, 0x1f, 0x0004);
3234 rtl_writephy(tp, 0x1f, 0x0007);
3235 rtl_writephy(tp, 0x1e, 0x0020);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003236 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003237 rtl_writephy(tp, 0x1f, 0x0002);
3238 rtl_writephy(tp, 0x1f, 0x0000);
3239 rtl_writephy(tp, 0x0d, 0x0007);
3240 rtl_writephy(tp, 0x0e, 0x003c);
3241 rtl_writephy(tp, 0x0d, 0x4007);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003242 rtl_writephy(tp, 0x0e, 0x0006);
Hayes Wang70090422011-07-06 15:58:06 +08003243 rtl_writephy(tp, 0x0d, 0x0000);
3244
3245 /* Green feature */
3246 rtl_writephy(tp, 0x1f, 0x0003);
Heiner Kallweit1814d6a2017-11-19 11:09:58 +01003247 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3248 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
Hayes Wang70090422011-07-06 15:58:06 +08003249 rtl_writephy(tp, 0x1f, 0x0000);
Heiner Kallweitb399a392017-11-19 11:15:46 +01003250 rtl_writephy(tp, 0x1f, 0x0005);
3251 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3252 rtl_writephy(tp, 0x1f, 0x0000);
hayeswange0c07552012-10-23 20:24:03 +00003253
françois romieu9ecb9aa2012-12-07 11:20:21 +00003254 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3255 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
Hayes Wang70090422011-07-06 15:58:06 +08003256}
3257
Hayes Wang5f886e02012-03-30 14:33:03 +08003258static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3259{
3260 /* For 4-corner performance improve */
3261 rtl_writephy(tp, 0x1f, 0x0005);
3262 rtl_writephy(tp, 0x05, 0x8b80);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003263 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003264 rtl_writephy(tp, 0x1f, 0x0000);
3265
3266 /* PHY auto speed down */
3267 rtl_writephy(tp, 0x1f, 0x0007);
3268 rtl_writephy(tp, 0x1e, 0x002d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003269 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003270 rtl_writephy(tp, 0x1f, 0x0000);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003271 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003272
3273 /* Improve 10M EEE waveform */
3274 rtl_writephy(tp, 0x1f, 0x0005);
3275 rtl_writephy(tp, 0x05, 0x8b86);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003276 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
Hayes Wang5f886e02012-03-30 14:33:03 +08003277 rtl_writephy(tp, 0x1f, 0x0000);
3278}
3279
Hayes Wangc2218922011-09-06 16:55:18 +08003280static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3281{
3282 static const struct phy_reg phy_reg_init[] = {
3283 /* Channel estimation fine tune */
3284 { 0x1f, 0x0003 },
3285 { 0x09, 0xa20f },
3286 { 0x1f, 0x0000 },
3287
3288 /* Modify green table for giga & fnet */
3289 { 0x1f, 0x0005 },
3290 { 0x05, 0x8b55 },
3291 { 0x06, 0x0000 },
3292 { 0x05, 0x8b5e },
3293 { 0x06, 0x0000 },
3294 { 0x05, 0x8b67 },
3295 { 0x06, 0x0000 },
3296 { 0x05, 0x8b70 },
3297 { 0x06, 0x0000 },
3298 { 0x1f, 0x0000 },
3299 { 0x1f, 0x0007 },
3300 { 0x1e, 0x0078 },
3301 { 0x17, 0x0000 },
3302 { 0x19, 0x00fb },
3303 { 0x1f, 0x0000 },
3304
3305 /* Modify green table for 10M */
3306 { 0x1f, 0x0005 },
3307 { 0x05, 0x8b79 },
3308 { 0x06, 0xaa00 },
3309 { 0x1f, 0x0000 },
3310
3311 /* Disable hiimpedance detection (RTCT) */
3312 { 0x1f, 0x0003 },
3313 { 0x01, 0x328a },
3314 { 0x1f, 0x0000 }
3315 };
3316
3317 rtl_apply_firmware(tp);
3318
3319 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3320
Hayes Wang5f886e02012-03-30 14:33:03 +08003321 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003322
3323 /* Improve 2-pair detection performance */
3324 rtl_writephy(tp, 0x1f, 0x0005);
3325 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003326 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangc2218922011-09-06 16:55:18 +08003327 rtl_writephy(tp, 0x1f, 0x0000);
3328}
3329
3330static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3331{
3332 rtl_apply_firmware(tp);
3333
Hayes Wang5f886e02012-03-30 14:33:03 +08003334 rtl8168f_hw_phy_config(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08003335}
3336
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003337static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3338{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003339 static const struct phy_reg phy_reg_init[] = {
3340 /* Channel estimation fine tune */
3341 { 0x1f, 0x0003 },
3342 { 0x09, 0xa20f },
3343 { 0x1f, 0x0000 },
3344
3345 /* Modify green table for giga & fnet */
3346 { 0x1f, 0x0005 },
3347 { 0x05, 0x8b55 },
3348 { 0x06, 0x0000 },
3349 { 0x05, 0x8b5e },
3350 { 0x06, 0x0000 },
3351 { 0x05, 0x8b67 },
3352 { 0x06, 0x0000 },
3353 { 0x05, 0x8b70 },
3354 { 0x06, 0x0000 },
3355 { 0x1f, 0x0000 },
3356 { 0x1f, 0x0007 },
3357 { 0x1e, 0x0078 },
3358 { 0x17, 0x0000 },
3359 { 0x19, 0x00aa },
3360 { 0x1f, 0x0000 },
3361
3362 /* Modify green table for 10M */
3363 { 0x1f, 0x0005 },
3364 { 0x05, 0x8b79 },
3365 { 0x06, 0xaa00 },
3366 { 0x1f, 0x0000 },
3367
3368 /* Disable hiimpedance detection (RTCT) */
3369 { 0x1f, 0x0003 },
3370 { 0x01, 0x328a },
3371 { 0x1f, 0x0000 }
3372 };
3373
3374
3375 rtl_apply_firmware(tp);
3376
3377 rtl8168f_hw_phy_config(tp);
3378
3379 /* Improve 2-pair detection performance */
3380 rtl_writephy(tp, 0x1f, 0x0005);
3381 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003382 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003383 rtl_writephy(tp, 0x1f, 0x0000);
3384
3385 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3386
3387 /* Modify green table for giga */
3388 rtl_writephy(tp, 0x1f, 0x0005);
3389 rtl_writephy(tp, 0x05, 0x8b54);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003390 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003391 rtl_writephy(tp, 0x05, 0x8b5d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003392 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003393 rtl_writephy(tp, 0x05, 0x8a7c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003394 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003395 rtl_writephy(tp, 0x05, 0x8a7f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003396 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003397 rtl_writephy(tp, 0x05, 0x8a82);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003398 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003399 rtl_writephy(tp, 0x05, 0x8a85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003400 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003401 rtl_writephy(tp, 0x05, 0x8a88);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003402 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003403 rtl_writephy(tp, 0x1f, 0x0000);
3404
3405 /* uc same-seed solution */
3406 rtl_writephy(tp, 0x1f, 0x0005);
3407 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003408 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003409 rtl_writephy(tp, 0x1f, 0x0000);
3410
3411 /* eee setting */
Chun-Hao Lin706123d2014-10-01 23:17:18 +08003412 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003413 rtl_writephy(tp, 0x1f, 0x0005);
3414 rtl_writephy(tp, 0x05, 0x8b85);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003415 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003416 rtl_writephy(tp, 0x1f, 0x0004);
3417 rtl_writephy(tp, 0x1f, 0x0007);
3418 rtl_writephy(tp, 0x1e, 0x0020);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003419 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003420 rtl_writephy(tp, 0x1f, 0x0000);
3421 rtl_writephy(tp, 0x0d, 0x0007);
3422 rtl_writephy(tp, 0x0e, 0x003c);
3423 rtl_writephy(tp, 0x0d, 0x4007);
3424 rtl_writephy(tp, 0x0e, 0x0000);
3425 rtl_writephy(tp, 0x0d, 0x0000);
3426
3427 /* Green feature */
3428 rtl_writephy(tp, 0x1f, 0x0003);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003429 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3430 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003431 rtl_writephy(tp, 0x1f, 0x0000);
3432}
3433
Hayes Wangc5583862012-07-02 17:23:22 +08003434static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3435{
Hayes Wangc5583862012-07-02 17:23:22 +08003436 rtl_apply_firmware(tp);
3437
hayeswang41f44d12013-04-01 22:23:36 +00003438 rtl_writephy(tp, 0x1f, 0x0a46);
3439 if (rtl_readphy(tp, 0x10) & 0x0100) {
3440 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003441 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
hayeswang41f44d12013-04-01 22:23:36 +00003442 } else {
3443 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003444 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003445 }
Hayes Wangc5583862012-07-02 17:23:22 +08003446
hayeswang41f44d12013-04-01 22:23:36 +00003447 rtl_writephy(tp, 0x1f, 0x0a46);
3448 if (rtl_readphy(tp, 0x13) & 0x0100) {
3449 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003450 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003451 } else {
hayeswangfe7524c2013-04-01 22:23:37 +00003452 rtl_writephy(tp, 0x1f, 0x0c41);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003453 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
hayeswang41f44d12013-04-01 22:23:36 +00003454 }
Hayes Wangc5583862012-07-02 17:23:22 +08003455
hayeswang41f44d12013-04-01 22:23:36 +00003456 /* Enable PHY auto speed down */
3457 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003458 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003459
hayeswangfe7524c2013-04-01 22:23:37 +00003460 rtl_writephy(tp, 0x1f, 0x0bcc);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003461 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003462 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003463 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003464 rtl_writephy(tp, 0x1f, 0x0a43);
3465 rtl_writephy(tp, 0x13, 0x8084);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003466 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3467 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
hayeswangfe7524c2013-04-01 22:23:37 +00003468
hayeswang41f44d12013-04-01 22:23:36 +00003469 /* EEE auto-fallback function */
3470 rtl_writephy(tp, 0x1f, 0x0a4b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003471 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003472
hayeswang41f44d12013-04-01 22:23:36 +00003473 /* Enable UC LPF tune function */
3474 rtl_writephy(tp, 0x1f, 0x0a43);
3475 rtl_writephy(tp, 0x13, 0x8012);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003476 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
hayeswang41f44d12013-04-01 22:23:36 +00003477
3478 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003479 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
hayeswang41f44d12013-04-01 22:23:36 +00003480
hayeswangfe7524c2013-04-01 22:23:37 +00003481 /* Improve SWR Efficiency */
3482 rtl_writephy(tp, 0x1f, 0x0bcd);
3483 rtl_writephy(tp, 0x14, 0x5065);
3484 rtl_writephy(tp, 0x14, 0xd065);
3485 rtl_writephy(tp, 0x1f, 0x0bc8);
3486 rtl_writephy(tp, 0x11, 0x5655);
3487 rtl_writephy(tp, 0x1f, 0x0bcd);
3488 rtl_writephy(tp, 0x14, 0x1065);
3489 rtl_writephy(tp, 0x14, 0x9065);
3490 rtl_writephy(tp, 0x14, 0x1065);
3491
David Chang1bac1072013-11-27 15:48:36 +08003492 /* Check ALDPS bit, disable it if enabled */
3493 rtl_writephy(tp, 0x1f, 0x0a43);
3494 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003495 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
David Chang1bac1072013-11-27 15:48:36 +08003496
hayeswang41f44d12013-04-01 22:23:36 +00003497 rtl_writephy(tp, 0x1f, 0x0000);
Hayes Wangc5583862012-07-02 17:23:22 +08003498}
3499
hayeswang57538c42013-04-01 22:23:40 +00003500static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3501{
3502 rtl_apply_firmware(tp);
3503}
3504
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003505static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3506{
3507 u16 dout_tapbin;
3508 u32 data;
3509
3510 rtl_apply_firmware(tp);
3511
3512 /* CHN EST parameters adjust - giga master */
3513 rtl_writephy(tp, 0x1f, 0x0a43);
3514 rtl_writephy(tp, 0x13, 0x809b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003515 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003516 rtl_writephy(tp, 0x13, 0x80a2);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003517 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003518 rtl_writephy(tp, 0x13, 0x80a4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003519 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003520 rtl_writephy(tp, 0x13, 0x809c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003521 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003522 rtl_writephy(tp, 0x1f, 0x0000);
3523
3524 /* CHN EST parameters adjust - giga slave */
3525 rtl_writephy(tp, 0x1f, 0x0a43);
3526 rtl_writephy(tp, 0x13, 0x80ad);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003527 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003528 rtl_writephy(tp, 0x13, 0x80b4);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003529 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003530 rtl_writephy(tp, 0x13, 0x80ac);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003531 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003532 rtl_writephy(tp, 0x1f, 0x0000);
3533
3534 /* CHN EST parameters adjust - fnet */
3535 rtl_writephy(tp, 0x1f, 0x0a43);
3536 rtl_writephy(tp, 0x13, 0x808e);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003537 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003538 rtl_writephy(tp, 0x13, 0x8090);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003539 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003540 rtl_writephy(tp, 0x13, 0x8092);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003541 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003542 rtl_writephy(tp, 0x1f, 0x0000);
3543
3544 /* enable R-tune & PGA-retune function */
3545 dout_tapbin = 0;
3546 rtl_writephy(tp, 0x1f, 0x0a46);
3547 data = rtl_readphy(tp, 0x13);
3548 data &= 3;
3549 data <<= 2;
3550 dout_tapbin |= data;
3551 data = rtl_readphy(tp, 0x12);
3552 data &= 0xc000;
3553 data >>= 14;
3554 dout_tapbin |= data;
3555 dout_tapbin = ~(dout_tapbin^0x08);
3556 dout_tapbin <<= 12;
3557 dout_tapbin &= 0xf000;
3558 rtl_writephy(tp, 0x1f, 0x0a43);
3559 rtl_writephy(tp, 0x13, 0x827a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003560 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003561 rtl_writephy(tp, 0x13, 0x827b);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003562 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003563 rtl_writephy(tp, 0x13, 0x827c);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003564 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003565 rtl_writephy(tp, 0x13, 0x827d);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003566 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003567
3568 rtl_writephy(tp, 0x1f, 0x0a43);
3569 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003570 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003571 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003572 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003573 rtl_writephy(tp, 0x1f, 0x0000);
3574
3575 /* enable GPHY 10M */
3576 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003577 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003578 rtl_writephy(tp, 0x1f, 0x0000);
3579
3580 /* SAR ADC performance */
3581 rtl_writephy(tp, 0x1f, 0x0bca);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003582 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003583 rtl_writephy(tp, 0x1f, 0x0000);
3584
3585 rtl_writephy(tp, 0x1f, 0x0a43);
3586 rtl_writephy(tp, 0x13, 0x803f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003587 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003588 rtl_writephy(tp, 0x13, 0x8047);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003589 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003590 rtl_writephy(tp, 0x13, 0x804f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003591 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003592 rtl_writephy(tp, 0x13, 0x8057);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003593 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003594 rtl_writephy(tp, 0x13, 0x805f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003595 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003596 rtl_writephy(tp, 0x13, 0x8067);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003597 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003598 rtl_writephy(tp, 0x13, 0x806f);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003599 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003600 rtl_writephy(tp, 0x1f, 0x0000);
3601
3602 /* disable phy pfm mode */
3603 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003604 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003605 rtl_writephy(tp, 0x1f, 0x0000);
3606
3607 /* Check ALDPS bit, disable it if enabled */
3608 rtl_writephy(tp, 0x1f, 0x0a43);
3609 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003610 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003611
3612 rtl_writephy(tp, 0x1f, 0x0000);
3613}
3614
3615static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3616{
3617 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3618 u16 rlen;
3619 u32 data;
3620
3621 rtl_apply_firmware(tp);
3622
3623 /* CHIN EST parameter update */
3624 rtl_writephy(tp, 0x1f, 0x0a43);
3625 rtl_writephy(tp, 0x13, 0x808a);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003626 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003627 rtl_writephy(tp, 0x1f, 0x0000);
3628
3629 /* enable R-tune & PGA-retune function */
3630 rtl_writephy(tp, 0x1f, 0x0a43);
3631 rtl_writephy(tp, 0x13, 0x0811);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003632 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003633 rtl_writephy(tp, 0x1f, 0x0a42);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003634 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003635 rtl_writephy(tp, 0x1f, 0x0000);
3636
3637 /* enable GPHY 10M */
3638 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Lin76564422014-10-01 23:17:17 +08003639 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003640 rtl_writephy(tp, 0x1f, 0x0000);
3641
3642 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3643 data = r8168_mac_ocp_read(tp, 0xdd02);
3644 ioffset_p3 = ((data & 0x80)>>7);
3645 ioffset_p3 <<= 3;
3646
3647 data = r8168_mac_ocp_read(tp, 0xdd00);
3648 ioffset_p3 |= ((data & (0xe000))>>13);
3649 ioffset_p2 = ((data & (0x1e00))>>9);
3650 ioffset_p1 = ((data & (0x01e0))>>5);
3651 ioffset_p0 = ((data & 0x0010)>>4);
3652 ioffset_p0 <<= 3;
3653 ioffset_p0 |= (data & (0x07));
3654 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3655
Chun-Hao Lin05b96872014-10-01 23:17:12 +08003656 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
Chun-Hao Line2e27882015-12-24 21:15:26 +08003657 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003658 rtl_writephy(tp, 0x1f, 0x0bcf);
3659 rtl_writephy(tp, 0x16, data);
3660 rtl_writephy(tp, 0x1f, 0x0000);
3661 }
3662
3663 /* Modify rlen (TX LPF corner frequency) level */
3664 rtl_writephy(tp, 0x1f, 0x0bcd);
3665 data = rtl_readphy(tp, 0x16);
3666 data &= 0x000f;
3667 rlen = 0;
3668 if (data > 3)
3669 rlen = data - 3;
3670 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3671 rtl_writephy(tp, 0x17, data);
3672 rtl_writephy(tp, 0x1f, 0x0bcd);
3673 rtl_writephy(tp, 0x1f, 0x0000);
3674
3675 /* disable phy pfm mode */
3676 rtl_writephy(tp, 0x1f, 0x0a44);
Chun-Hao Linc832c35f2015-12-29 22:13:38 +08003677 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003678 rtl_writephy(tp, 0x1f, 0x0000);
3679
3680 /* Check ALDPS bit, disable it if enabled */
3681 rtl_writephy(tp, 0x1f, 0x0a43);
3682 if (rtl_readphy(tp, 0x10) & 0x0004)
Chun-Hao Lin76564422014-10-01 23:17:17 +08003683 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08003684
3685 rtl_writephy(tp, 0x1f, 0x0000);
3686}
3687
Chun-Hao Lin935e2212014-10-07 15:10:41 +08003688static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3689{
3690 /* Enable PHY auto speed down */
3691 rtl_writephy(tp, 0x1f, 0x0a44);
3692 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3693 rtl_writephy(tp, 0x1f, 0x0000);
3694
3695 /* patch 10M & ALDPS */
3696 rtl_writephy(tp, 0x1f, 0x0bcc);
3697 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3698 rtl_writephy(tp, 0x1f, 0x0a44);
3699 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3700 rtl_writephy(tp, 0x1f, 0x0a43);
3701 rtl_writephy(tp, 0x13, 0x8084);
3702 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3703 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3704 rtl_writephy(tp, 0x1f, 0x0000);
3705
3706 /* Enable EEE auto-fallback function */
3707 rtl_writephy(tp, 0x1f, 0x0a4b);
3708 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3709 rtl_writephy(tp, 0x1f, 0x0000);
3710
3711 /* Enable UC LPF tune function */
3712 rtl_writephy(tp, 0x1f, 0x0a43);
3713 rtl_writephy(tp, 0x13, 0x8012);
3714 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3715 rtl_writephy(tp, 0x1f, 0x0000);
3716
3717 /* set rg_sel_sdm_rate */
3718 rtl_writephy(tp, 0x1f, 0x0c42);
3719 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3720 rtl_writephy(tp, 0x1f, 0x0000);
3721
3722 /* Check ALDPS bit, disable it if enabled */
3723 rtl_writephy(tp, 0x1f, 0x0a43);
3724 if (rtl_readphy(tp, 0x10) & 0x0004)
3725 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3726
3727 rtl_writephy(tp, 0x1f, 0x0000);
3728}
3729
3730static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3731{
3732 /* patch 10M & ALDPS */
3733 rtl_writephy(tp, 0x1f, 0x0bcc);
3734 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
3735 rtl_writephy(tp, 0x1f, 0x0a44);
3736 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
3737 rtl_writephy(tp, 0x1f, 0x0a43);
3738 rtl_writephy(tp, 0x13, 0x8084);
3739 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
3740 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
3741 rtl_writephy(tp, 0x1f, 0x0000);
3742
3743 /* Enable UC LPF tune function */
3744 rtl_writephy(tp, 0x1f, 0x0a43);
3745 rtl_writephy(tp, 0x13, 0x8012);
3746 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3747 rtl_writephy(tp, 0x1f, 0x0000);
3748
3749 /* Set rg_sel_sdm_rate */
3750 rtl_writephy(tp, 0x1f, 0x0c42);
3751 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3752 rtl_writephy(tp, 0x1f, 0x0000);
3753
3754 /* Channel estimation parameters */
3755 rtl_writephy(tp, 0x1f, 0x0a43);
3756 rtl_writephy(tp, 0x13, 0x80f3);
3757 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3758 rtl_writephy(tp, 0x13, 0x80f0);
3759 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3760 rtl_writephy(tp, 0x13, 0x80ef);
3761 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3762 rtl_writephy(tp, 0x13, 0x80f6);
3763 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3764 rtl_writephy(tp, 0x13, 0x80ec);
3765 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3766 rtl_writephy(tp, 0x13, 0x80ed);
3767 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3768 rtl_writephy(tp, 0x13, 0x80f2);
3769 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3770 rtl_writephy(tp, 0x13, 0x80f4);
3771 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3772 rtl_writephy(tp, 0x1f, 0x0a43);
3773 rtl_writephy(tp, 0x13, 0x8110);
3774 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3775 rtl_writephy(tp, 0x13, 0x810f);
3776 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3777 rtl_writephy(tp, 0x13, 0x8111);
3778 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3779 rtl_writephy(tp, 0x13, 0x8113);
3780 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3781 rtl_writephy(tp, 0x13, 0x8115);
3782 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3783 rtl_writephy(tp, 0x13, 0x810e);
3784 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3785 rtl_writephy(tp, 0x13, 0x810c);
3786 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3787 rtl_writephy(tp, 0x13, 0x810b);
3788 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3789 rtl_writephy(tp, 0x1f, 0x0a43);
3790 rtl_writephy(tp, 0x13, 0x80d1);
3791 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3792 rtl_writephy(tp, 0x13, 0x80cd);
3793 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3794 rtl_writephy(tp, 0x13, 0x80d3);
3795 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3796 rtl_writephy(tp, 0x13, 0x80d5);
3797 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3798 rtl_writephy(tp, 0x13, 0x80d7);
3799 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3800
3801 /* Force PWM-mode */
3802 rtl_writephy(tp, 0x1f, 0x0bcd);
3803 rtl_writephy(tp, 0x14, 0x5065);
3804 rtl_writephy(tp, 0x14, 0xd065);
3805 rtl_writephy(tp, 0x1f, 0x0bc8);
3806 rtl_writephy(tp, 0x12, 0x00ed);
3807 rtl_writephy(tp, 0x1f, 0x0bcd);
3808 rtl_writephy(tp, 0x14, 0x1065);
3809 rtl_writephy(tp, 0x14, 0x9065);
3810 rtl_writephy(tp, 0x14, 0x1065);
3811 rtl_writephy(tp, 0x1f, 0x0000);
3812
3813 /* Check ALDPS bit, disable it if enabled */
3814 rtl_writephy(tp, 0x1f, 0x0a43);
3815 if (rtl_readphy(tp, 0x10) & 0x0004)
3816 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
3817
3818 rtl_writephy(tp, 0x1f, 0x0000);
3819}
3820
françois romieu4da19632011-01-03 15:07:55 +00003821static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003822{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003823 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003824 { 0x1f, 0x0003 },
3825 { 0x08, 0x441d },
3826 { 0x01, 0x9100 },
3827 { 0x1f, 0x0000 }
3828 };
3829
françois romieu4da19632011-01-03 15:07:55 +00003830 rtl_writephy(tp, 0x1f, 0x0000);
3831 rtl_patchphy(tp, 0x11, 1 << 12);
3832 rtl_patchphy(tp, 0x19, 1 << 13);
3833 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003834
françois romieu4da19632011-01-03 15:07:55 +00003835 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02003836}
3837
Hayes Wang5a5e4442011-02-22 17:26:21 +08003838static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3839{
3840 static const struct phy_reg phy_reg_init[] = {
3841 { 0x1f, 0x0005 },
3842 { 0x1a, 0x0000 },
3843 { 0x1f, 0x0000 },
3844
3845 { 0x1f, 0x0004 },
3846 { 0x1c, 0x0000 },
3847 { 0x1f, 0x0000 },
3848
3849 { 0x1f, 0x0001 },
3850 { 0x15, 0x7701 },
3851 { 0x1f, 0x0000 }
3852 };
3853
3854 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003855 rtl_writephy(tp, 0x1f, 0x0000);
3856 rtl_writephy(tp, 0x18, 0x0310);
3857 msleep(100);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003858
François Romieu953a12c2011-04-24 17:38:48 +02003859 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08003860
3861 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3862}
3863
Hayes Wang7e18dca2012-03-30 14:33:02 +08003864static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3865{
Hayes Wang7e18dca2012-03-30 14:33:02 +08003866 /* Disable ALDPS before setting firmware */
Francois Romieueef63cc2013-02-08 23:43:20 +01003867 rtl_writephy(tp, 0x1f, 0x0000);
3868 rtl_writephy(tp, 0x18, 0x0310);
3869 msleep(20);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003870
3871 rtl_apply_firmware(tp);
3872
3873 /* EEE setting */
Francois Romieufdf6fc02012-07-06 22:40:38 +02003874 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang7e18dca2012-03-30 14:33:02 +08003875 rtl_writephy(tp, 0x1f, 0x0004);
3876 rtl_writephy(tp, 0x10, 0x401f);
3877 rtl_writephy(tp, 0x19, 0x7030);
3878 rtl_writephy(tp, 0x1f, 0x0000);
3879}
3880
Hayes Wang5598bfe2012-07-02 17:23:21 +08003881static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3882{
Hayes Wang5598bfe2012-07-02 17:23:21 +08003883 static const struct phy_reg phy_reg_init[] = {
3884 { 0x1f, 0x0004 },
3885 { 0x10, 0xc07f },
3886 { 0x19, 0x7030 },
3887 { 0x1f, 0x0000 }
3888 };
3889
3890 /* Disable ALDPS before ram code */
Francois Romieueef63cc2013-02-08 23:43:20 +01003891 rtl_writephy(tp, 0x1f, 0x0000);
3892 rtl_writephy(tp, 0x18, 0x0310);
3893 msleep(100);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003894
3895 rtl_apply_firmware(tp);
3896
Francois Romieufdf6fc02012-07-06 22:40:38 +02003897 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003898 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3899
Francois Romieufdf6fc02012-07-06 22:40:38 +02003900 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Hayes Wang5598bfe2012-07-02 17:23:21 +08003901}
3902
Francois Romieu5615d9f2007-08-17 17:50:46 +02003903static void rtl_hw_phy_config(struct net_device *dev)
3904{
3905 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003906
3907 rtl8169_print_mac_version(tp);
3908
3909 switch (tp->mac_version) {
3910 case RTL_GIGA_MAC_VER_01:
3911 break;
3912 case RTL_GIGA_MAC_VER_02:
3913 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00003914 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003915 break;
3916 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00003917 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02003918 break;
françois romieu2e9558562009-08-10 19:44:19 +00003919 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00003920 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00003921 break;
françois romieu8c7006a2009-08-10 19:43:29 +00003922 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00003923 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00003924 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02003925 case RTL_GIGA_MAC_VER_07:
3926 case RTL_GIGA_MAC_VER_08:
3927 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00003928 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003929 break;
Francois Romieu236b8082008-05-30 16:11:48 +02003930 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00003931 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003932 break;
3933 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00003934 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003935 break;
3936 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00003937 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02003938 break;
Francois Romieu867763c2007-08-17 18:21:58 +02003939 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00003940 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003941 break;
3942 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00003943 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02003944 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02003945 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00003946 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02003947 break;
Francois Romieu197ff762008-06-28 13:16:02 +02003948 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00003949 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02003950 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02003951 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00003952 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02003953 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003954 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003955 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00003956 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02003957 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02003958 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00003959 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003960 break;
3961 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00003962 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00003963 break;
3964 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00003965 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02003966 break;
françois romieue6de30d2011-01-03 15:08:37 +00003967 case RTL_GIGA_MAC_VER_28:
3968 rtl8168d_4_hw_phy_config(tp);
3969 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08003970 case RTL_GIGA_MAC_VER_29:
3971 case RTL_GIGA_MAC_VER_30:
3972 rtl8105e_hw_phy_config(tp);
3973 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02003974 case RTL_GIGA_MAC_VER_31:
3975 /* None. */
3976 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00003977 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00003978 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003979 rtl8168e_1_hw_phy_config(tp);
3980 break;
3981 case RTL_GIGA_MAC_VER_34:
3982 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003983 break;
Hayes Wangc2218922011-09-06 16:55:18 +08003984 case RTL_GIGA_MAC_VER_35:
3985 rtl8168f_1_hw_phy_config(tp);
3986 break;
3987 case RTL_GIGA_MAC_VER_36:
3988 rtl8168f_2_hw_phy_config(tp);
3989 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003990
Hayes Wang7e18dca2012-03-30 14:33:02 +08003991 case RTL_GIGA_MAC_VER_37:
3992 rtl8402_hw_phy_config(tp);
3993 break;
3994
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08003995 case RTL_GIGA_MAC_VER_38:
3996 rtl8411_hw_phy_config(tp);
3997 break;
3998
Hayes Wang5598bfe2012-07-02 17:23:21 +08003999 case RTL_GIGA_MAC_VER_39:
4000 rtl8106e_hw_phy_config(tp);
4001 break;
4002
Hayes Wangc5583862012-07-02 17:23:22 +08004003 case RTL_GIGA_MAC_VER_40:
4004 rtl8168g_1_hw_phy_config(tp);
4005 break;
hayeswang57538c42013-04-01 22:23:40 +00004006 case RTL_GIGA_MAC_VER_42:
hayeswang58152cd2013-04-01 22:23:42 +00004007 case RTL_GIGA_MAC_VER_43:
hayeswang45dd95c2013-07-08 17:09:01 +08004008 case RTL_GIGA_MAC_VER_44:
hayeswang57538c42013-04-01 22:23:40 +00004009 rtl8168g_2_hw_phy_config(tp);
4010 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004011 case RTL_GIGA_MAC_VER_45:
4012 case RTL_GIGA_MAC_VER_47:
4013 rtl8168h_1_hw_phy_config(tp);
4014 break;
4015 case RTL_GIGA_MAC_VER_46:
4016 case RTL_GIGA_MAC_VER_48:
4017 rtl8168h_2_hw_phy_config(tp);
4018 break;
Hayes Wangc5583862012-07-02 17:23:22 +08004019
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004020 case RTL_GIGA_MAC_VER_49:
4021 rtl8168ep_1_hw_phy_config(tp);
4022 break;
4023 case RTL_GIGA_MAC_VER_50:
4024 case RTL_GIGA_MAC_VER_51:
4025 rtl8168ep_2_hw_phy_config(tp);
4026 break;
4027
Hayes Wangc5583862012-07-02 17:23:22 +08004028 case RTL_GIGA_MAC_VER_41:
Francois Romieu5615d9f2007-08-17 17:50:46 +02004029 default:
4030 break;
4031 }
4032}
4033
Francois Romieuda78dbf2012-01-26 14:18:23 +01004034static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4035{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004036 if (!test_and_set_bit(flag, tp->wk.flags))
4037 schedule_work(&tp->wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01004038}
4039
David S. Miller8decf862011-09-22 03:23:13 -04004040static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4041{
David S. Miller8decf862011-09-22 03:23:13 -04004042 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
Heiner Kallweite3972862018-06-29 08:07:04 +02004043 (RTL_R8(tp, PHYstatus) & TBI_Enable);
David S. Miller8decf862011-09-22 03:23:13 -04004044}
4045
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004046static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
Francois Romieu5615d9f2007-08-17 17:50:46 +02004048 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004049
Marcus Sundberg773328942008-07-10 21:28:08 +02004050 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004051 netif_dbg(tp, drv, dev,
4052 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004053 RTL_W8(tp, 0x82, 0x01);
Marcus Sundberg773328942008-07-10 21:28:08 +02004054 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004055
Francois Romieu6dccd162007-02-13 23:38:05 +01004056 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4057
4058 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4059 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004060
Francois Romieubcf0bf92006-07-26 23:14:13 +02004061 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004062 netif_dbg(tp, drv, dev,
4063 "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004064 RTL_W8(tp, 0x82, 0x01);
Heiner Kallweit49d17512018-06-28 20:36:15 +02004065 netif_dbg(tp, drv, dev,
4066 "Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00004067 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004068 }
4069
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02004070 /* We may have called phy_speed_down before */
4071 phy_speed_up(dev->phydev);
4072
Heiner Kallweitf75222b2018-07-17 22:51:41 +02004073 genphy_soft_reset(dev->phydev);
Heiner Kallweit10bc6a62018-09-20 22:47:09 +02004074
Alex Xu (Hello71)9003b362018-09-30 11:06:39 -04004075 /* It was reported that several chips end up with 10MBit/Half on a
Heiner Kallweit10bc6a62018-09-20 22:47:09 +02004076 * 1GBit link after resuming from S3. For whatever reason the PHY on
Alex Xu (Hello71)9003b362018-09-30 11:06:39 -04004077 * these chips doesn't properly start a renegotiation when soft-reset.
Heiner Kallweit10bc6a62018-09-20 22:47:09 +02004078 * Explicitly requesting a renegotiation fixes this.
4079 */
Alex Xu (Hello71)9003b362018-09-30 11:06:39 -04004080 if (dev->phydev->autoneg == AUTONEG_ENABLE)
Heiner Kallweit10bc6a62018-09-20 22:47:09 +02004081 phy_restart_aneg(dev->phydev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02004082}
4083
Francois Romieu773d2022007-01-31 23:47:43 +01004084static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4085{
Francois Romieuda78dbf2012-01-26 14:18:23 +01004086 rtl_lock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004087
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004088 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
françois romieu908ba2bf2010-04-26 11:42:58 +00004089
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004090 RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4091 RTL_R32(tp, MAC4);
françois romieu908ba2bf2010-04-26 11:42:58 +00004092
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004093 RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4094 RTL_R32(tp, MAC0);
françois romieu908ba2bf2010-04-26 11:42:58 +00004095
françois romieu9ecb9aa2012-12-07 11:20:21 +00004096 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4097 rtl_rar_exgmac_set(tp, addr);
françois romieuc28aa382011-08-02 03:53:43 +00004098
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004099 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Francois Romieu773d2022007-01-31 23:47:43 +01004100
Francois Romieuda78dbf2012-01-26 14:18:23 +01004101 rtl_unlock_work(tp);
Francois Romieu773d2022007-01-31 23:47:43 +01004102}
4103
4104static int rtl_set_mac_address(struct net_device *dev, void *p)
4105{
4106 struct rtl8169_private *tp = netdev_priv(dev);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004107 struct device *d = tp_to_dev(tp);
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004108 int ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004109
Heiner Kallweit1f7aa2b2018-03-20 07:45:33 +01004110 ret = eth_mac_addr(dev, p);
4111 if (ret)
4112 return ret;
Francois Romieu773d2022007-01-31 23:47:43 +01004113
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08004114 pm_runtime_get_noresume(d);
4115
4116 if (pm_runtime_active(d))
4117 rtl_rar_set(tp, dev->dev_addr);
4118
4119 pm_runtime_put_noidle(d);
Francois Romieu773d2022007-01-31 23:47:43 +01004120
4121 return 0;
4122}
4123
Heiner Kallweite3972862018-06-29 08:07:04 +02004124static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08004125{
Heiner Kallweit69b3c592018-07-17 22:51:53 +02004126 if (!netif_running(dev))
4127 return -ENODEV;
Heiner Kallweite3972862018-06-29 08:07:04 +02004128
Heiner Kallweit69b3c592018-07-17 22:51:53 +02004129 return phy_mii_ioctl(dev->phydev, ifr, cmd);
Francois Romieu8b4ab282008-11-19 22:05:25 -08004130}
4131
Bill Pembertonbaf63292012-12-03 09:23:28 -05004132static void rtl_init_mdio_ops(struct rtl8169_private *tp)
françois romieuc0e45c12011-01-03 15:08:04 +00004133{
4134 struct mdio_ops *ops = &tp->mdio_ops;
4135
4136 switch (tp->mac_version) {
4137 case RTL_GIGA_MAC_VER_27:
4138 ops->write = r8168dp_1_mdio_write;
4139 ops->read = r8168dp_1_mdio_read;
4140 break;
françois romieue6de30d2011-01-03 15:08:37 +00004141 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00004142 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00004143 ops->write = r8168dp_2_mdio_write;
4144 ops->read = r8168dp_2_mdio_read;
4145 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004146 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Hayes Wangc5583862012-07-02 17:23:22 +08004147 ops->write = r8168g_mdio_write;
4148 ops->read = r8168g_mdio_read;
4149 break;
françois romieuc0e45c12011-01-03 15:08:04 +00004150 default:
4151 ops->write = r8169_mdio_write;
4152 ops->read = r8169_mdio_read;
4153 break;
4154 }
4155}
4156
David S. Miller1805b2f2011-10-24 18:18:09 -04004157static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4158{
David S. Miller1805b2f2011-10-24 18:18:09 -04004159 switch (tp->mac_version) {
Cyril Bruleboisb00e69d2012-10-31 14:00:46 +00004160 case RTL_GIGA_MAC_VER_25:
4161 case RTL_GIGA_MAC_VER_26:
David S. Miller1805b2f2011-10-24 18:18:09 -04004162 case RTL_GIGA_MAC_VER_29:
4163 case RTL_GIGA_MAC_VER_30:
4164 case RTL_GIGA_MAC_VER_32:
4165 case RTL_GIGA_MAC_VER_33:
4166 case RTL_GIGA_MAC_VER_34:
Heiner Kallweit2a718832018-05-02 21:39:49 +02004167 case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004168 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
David S. Miller1805b2f2011-10-24 18:18:09 -04004169 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4170 break;
4171 default:
4172 break;
4173 }
4174}
4175
4176static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4177{
Heiner Kallweit6fcf9b12018-07-04 21:11:29 +02004178 if (!netif_running(tp->dev) || !__rtl8169_get_wol(tp))
David S. Miller1805b2f2011-10-24 18:18:09 -04004179 return false;
4180
Heiner Kallweit5b7ad4b2018-07-17 22:51:57 +02004181 phy_speed_down(tp->dev->phydev, false);
David S. Miller1805b2f2011-10-24 18:18:09 -04004182 rtl_wol_suspend_quirk(tp);
4183
4184 return true;
4185}
4186
françois romieu065c27c2011-01-03 15:08:12 +00004187static void r8168_pll_power_down(struct rtl8169_private *tp)
4188{
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01004189 if (r8168_check_dash(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004190 return;
4191
hayeswang01dc7fe2011-03-21 01:50:28 +00004192 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4193 tp->mac_version == RTL_GIGA_MAC_VER_33)
Francois Romieufdf6fc02012-07-06 22:40:38 +02004194 rtl_ephy_write(tp, 0x19, 0xff64);
hayeswang01dc7fe2011-03-21 01:50:28 +00004195
David S. Miller1805b2f2011-10-24 18:18:09 -04004196 if (rtl_wol_pll_power_down(tp))
françois romieu065c27c2011-01-03 15:08:12 +00004197 return;
françois romieu065c27c2011-01-03 15:08:12 +00004198
françois romieu065c27c2011-01-03 15:08:12 +00004199 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004200 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004201 case RTL_GIGA_MAC_VER_37:
4202 case RTL_GIGA_MAC_VER_39:
4203 case RTL_GIGA_MAC_VER_43:
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004204 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004205 case RTL_GIGA_MAC_VER_45:
4206 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004207 case RTL_GIGA_MAC_VER_47:
4208 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004209 case RTL_GIGA_MAC_VER_50:
4210 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004211 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004212 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004213 case RTL_GIGA_MAC_VER_40:
4214 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004215 case RTL_GIGA_MAC_VER_49:
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004216 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004217 0xfc000000, ERIAR_EXGMAC);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004218 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
hayeswangbeb330a2013-04-01 22:23:39 +00004219 break;
françois romieu065c27c2011-01-03 15:08:12 +00004220 }
4221}
4222
4223static void r8168_pll_power_up(struct rtl8169_private *tp)
4224{
françois romieu065c27c2011-01-03 15:08:12 +00004225 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004226 case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004227 case RTL_GIGA_MAC_VER_37:
4228 case RTL_GIGA_MAC_VER_39:
4229 case RTL_GIGA_MAC_VER_43:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004230 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
françois romieu065c27c2011-01-03 15:08:12 +00004231 break;
Chun-Hao Lin42fde732014-10-01 23:17:14 +08004232 case RTL_GIGA_MAC_VER_44:
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004233 case RTL_GIGA_MAC_VER_45:
4234 case RTL_GIGA_MAC_VER_46:
Heiner Kallweit73570bf2018-05-02 21:39:45 +02004235 case RTL_GIGA_MAC_VER_47:
4236 case RTL_GIGA_MAC_VER_48:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004237 case RTL_GIGA_MAC_VER_50:
4238 case RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004239 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08004240 break;
hayeswangbeb330a2013-04-01 22:23:39 +00004241 case RTL_GIGA_MAC_VER_40:
4242 case RTL_GIGA_MAC_VER_41:
Chun-Hao Lin935e2212014-10-07 15:10:41 +08004243 case RTL_GIGA_MAC_VER_49:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004244 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08004245 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
hayeswangbeb330a2013-04-01 22:23:39 +00004246 0x00000000, ERIAR_EXGMAC);
4247 break;
françois romieu065c27c2011-01-03 15:08:12 +00004248 }
4249
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02004250 phy_resume(tp->dev->phydev);
4251 /* give MAC/PHY some time to resume */
4252 msleep(20);
françois romieu065c27c2011-01-03 15:08:12 +00004253}
4254
françois romieu065c27c2011-01-03 15:08:12 +00004255static void rtl_pll_power_down(struct rtl8169_private *tp)
4256{
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004257 switch (tp->mac_version) {
4258 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4259 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4260 break;
4261 default:
4262 r8168_pll_power_down(tp);
4263 }
françois romieu065c27c2011-01-03 15:08:12 +00004264}
4265
4266static void rtl_pll_power_up(struct rtl8169_private *tp)
4267{
françois romieu065c27c2011-01-03 15:08:12 +00004268 switch (tp->mac_version) {
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004269 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4270 case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
françois romieu065c27c2011-01-03 15:08:12 +00004271 break;
françois romieu065c27c2011-01-03 15:08:12 +00004272 default:
Heiner Kallweit4f447d22018-05-02 21:39:47 +02004273 r8168_pll_power_up(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004274 }
4275}
4276
Hayes Wange542a222011-07-06 15:58:04 +08004277static void rtl_init_rxcfg(struct rtl8169_private *tp)
4278{
Hayes Wange542a222011-07-06 15:58:04 +08004279 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02004280 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4281 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004282 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004283 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004284 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
françois romieueb2dc352012-06-20 12:09:18 +00004285 case RTL_GIGA_MAC_VER_34:
françois romieu3ced8c92013-09-08 01:15:35 +02004286 case RTL_GIGA_MAC_VER_35:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004287 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004288 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02004289 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004290 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
hayeswangbeb330a2013-04-01 22:23:39 +00004291 break;
Hayes Wange542a222011-07-06 15:58:04 +08004292 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004293 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
Hayes Wange542a222011-07-06 15:58:04 +08004294 break;
4295 }
4296}
4297
Hayes Wang92fc43b2011-07-06 15:58:03 +08004298static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4299{
Timo Teräs9fba0812013-01-15 21:01:24 +00004300 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
Hayes Wang92fc43b2011-07-06 15:58:03 +08004301}
4302
Francois Romieud58d46b2011-05-03 16:38:29 +02004303static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4304{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004305 if (tp->jumbo_ops.enable) {
4306 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4307 tp->jumbo_ops.enable(tp);
4308 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4309 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004310}
4311
4312static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4313{
Heiner Kallweiteda40b82018-05-02 21:39:54 +02004314 if (tp->jumbo_ops.disable) {
4315 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4316 tp->jumbo_ops.disable(tp);
4317 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4318 }
Francois Romieud58d46b2011-05-03 16:38:29 +02004319}
4320
4321static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4322{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004323 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4324 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004325 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004326}
4327
4328static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4329{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004330 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4331 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004332 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004333}
4334
4335static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4336{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004337 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004338}
4339
4340static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4341{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004342 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
Francois Romieud58d46b2011-05-03 16:38:29 +02004343}
4344
4345static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4346{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004347 RTL_W8(tp, MaxTxPacketSize, 0x3f);
4348 RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4349 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
Heiner Kallweitcb732002018-03-20 07:45:35 +01004350 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004351}
4352
4353static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4354{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004355 RTL_W8(tp, MaxTxPacketSize, 0x0c);
4356 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4357 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004358 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieud58d46b2011-05-03 16:38:29 +02004359}
4360
4361static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4362{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004363 rtl_tx_performance_tweak(tp,
Rafał Miłeckif65d5392015-01-26 18:06:31 +01004364 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004365}
4366
4367static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4368{
Heiner Kallweitcb732002018-03-20 07:45:35 +01004369 rtl_tx_performance_tweak(tp,
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004370 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieud58d46b2011-05-03 16:38:29 +02004371}
4372
4373static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4374{
Francois Romieud58d46b2011-05-03 16:38:29 +02004375 r8168b_0_hw_jumbo_enable(tp);
4376
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004377 RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004378}
4379
4380static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4381{
Francois Romieud58d46b2011-05-03 16:38:29 +02004382 r8168b_0_hw_jumbo_disable(tp);
4383
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004384 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieud58d46b2011-05-03 16:38:29 +02004385}
4386
Bill Pembertonbaf63292012-12-03 09:23:28 -05004387static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
Francois Romieud58d46b2011-05-03 16:38:29 +02004388{
4389 struct jumbo_ops *ops = &tp->jumbo_ops;
4390
4391 switch (tp->mac_version) {
4392 case RTL_GIGA_MAC_VER_11:
4393 ops->disable = r8168b_0_hw_jumbo_disable;
4394 ops->enable = r8168b_0_hw_jumbo_enable;
4395 break;
4396 case RTL_GIGA_MAC_VER_12:
4397 case RTL_GIGA_MAC_VER_17:
4398 ops->disable = r8168b_1_hw_jumbo_disable;
4399 ops->enable = r8168b_1_hw_jumbo_enable;
4400 break;
4401 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4402 case RTL_GIGA_MAC_VER_19:
4403 case RTL_GIGA_MAC_VER_20:
4404 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4405 case RTL_GIGA_MAC_VER_22:
4406 case RTL_GIGA_MAC_VER_23:
4407 case RTL_GIGA_MAC_VER_24:
4408 case RTL_GIGA_MAC_VER_25:
4409 case RTL_GIGA_MAC_VER_26:
4410 ops->disable = r8168c_hw_jumbo_disable;
4411 ops->enable = r8168c_hw_jumbo_enable;
4412 break;
4413 case RTL_GIGA_MAC_VER_27:
4414 case RTL_GIGA_MAC_VER_28:
4415 ops->disable = r8168dp_hw_jumbo_disable;
4416 ops->enable = r8168dp_hw_jumbo_enable;
4417 break;
4418 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4419 case RTL_GIGA_MAC_VER_32:
4420 case RTL_GIGA_MAC_VER_33:
4421 case RTL_GIGA_MAC_VER_34:
4422 ops->disable = r8168e_hw_jumbo_disable;
4423 ops->enable = r8168e_hw_jumbo_enable;
4424 break;
4425
4426 /*
4427 * No action needed for jumbo frames with 8169.
4428 * No jumbo for 810x at all.
4429 */
Heiner Kallweit2a718832018-05-02 21:39:49 +02004430 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Francois Romieud58d46b2011-05-03 16:38:29 +02004431 default:
4432 ops->disable = NULL;
4433 ops->enable = NULL;
4434 break;
4435 }
4436}
4437
Francois Romieuffc46952012-07-06 14:19:23 +02004438DECLARE_RTL_COND(rtl_chipcmd_cond)
4439{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004440 return RTL_R8(tp, ChipCmd) & CmdReset;
Francois Romieuffc46952012-07-06 14:19:23 +02004441}
4442
Francois Romieu6f43adc2011-04-29 15:05:51 +02004443static void rtl_hw_reset(struct rtl8169_private *tp)
4444{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004445 RTL_W8(tp, ChipCmd, CmdReset);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004446
Francois Romieuffc46952012-07-06 14:19:23 +02004447 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02004448}
4449
Francois Romieub6ffd972011-06-17 17:00:05 +02004450static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
4451{
4452 struct rtl_fw *rtl_fw;
4453 const char *name;
4454 int rc = -ENOMEM;
4455
4456 name = rtl_lookup_firmware_name(tp);
4457 if (!name)
4458 goto out_no_firmware;
4459
4460 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4461 if (!rtl_fw)
4462 goto err_warn;
4463
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01004464 rc = request_firmware(&rtl_fw->fw, name, tp_to_dev(tp));
Francois Romieub6ffd972011-06-17 17:00:05 +02004465 if (rc < 0)
4466 goto err_free;
4467
Francois Romieufd112f22011-06-18 00:10:29 +02004468 rc = rtl_check_firmware(tp, rtl_fw);
4469 if (rc < 0)
4470 goto err_release_firmware;
4471
Francois Romieub6ffd972011-06-17 17:00:05 +02004472 tp->rtl_fw = rtl_fw;
4473out:
4474 return;
4475
Francois Romieufd112f22011-06-18 00:10:29 +02004476err_release_firmware:
4477 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02004478err_free:
4479 kfree(rtl_fw);
4480err_warn:
4481 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4482 name, rc);
4483out_no_firmware:
4484 tp->rtl_fw = NULL;
4485 goto out;
4486}
4487
François Romieu953a12c2011-04-24 17:38:48 +02004488static void rtl_request_firmware(struct rtl8169_private *tp)
4489{
Francois Romieub6ffd972011-06-17 17:00:05 +02004490 if (IS_ERR(tp->rtl_fw))
4491 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02004492}
4493
Hayes Wang92fc43b2011-07-06 15:58:03 +08004494static void rtl_rx_close(struct rtl8169_private *tp)
4495{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004496 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004497}
4498
Francois Romieuffc46952012-07-06 14:19:23 +02004499DECLARE_RTL_COND(rtl_npq_cond)
4500{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004501 return RTL_R8(tp, TxPoll) & NPQ;
Francois Romieuffc46952012-07-06 14:19:23 +02004502}
4503
4504DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4505{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004506 return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
Francois Romieuffc46952012-07-06 14:19:23 +02004507}
4508
françois romieue6de30d2011-01-03 15:08:37 +00004509static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
4511 /* Disable interrupts */
françois romieu811fd302011-12-04 20:30:45 +00004512 rtl8169_irq_mask_and_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Hayes Wang92fc43b2011-07-06 15:58:03 +08004514 rtl_rx_close(tp);
4515
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004516 switch (tp->mac_version) {
4517 case RTL_GIGA_MAC_VER_27:
4518 case RTL_GIGA_MAC_VER_28:
4519 case RTL_GIGA_MAC_VER_31:
Francois Romieuffc46952012-07-06 14:19:23 +02004520 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004521 break;
4522 case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4523 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004524 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Francois Romieuffc46952012-07-06 14:19:23 +02004525 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004526 break;
4527 default:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004528 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004529 udelay(100);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02004530 break;
françois romieue6de30d2011-01-03 15:08:37 +00004531 }
4532
Hayes Wang92fc43b2011-07-06 15:58:03 +08004533 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534}
4535
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004536static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004537{
Heiner Kallweitad5f97f2018-09-28 23:51:54 +02004538 u32 val = TX_DMA_BURST << TxDMAShift |
4539 InterFrameGap << TxInterFrameGapShift;
4540
4541 if (tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
4542 tp->mac_version != RTL_GIGA_MAC_VER_39)
4543 val |= TXCFG_AUTO_FIFO;
4544
4545 RTL_W32(tp, TxConfig, val);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004546}
4547
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004548static void rtl_set_rx_max_size(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Heiner Kallweit4fd48c42018-04-28 22:19:47 +02004550 /* Low hurts. Let's disable the filtering. */
4551 RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
Francois Romieu07ce4062007-02-23 23:36:39 +01004552}
4553
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004554static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
Francois Romieu7f796d832007-06-11 23:04:41 +02004555{
4556 /*
4557 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4558 * register to be written before TxDescAddrLow to work.
4559 * Switching from MMIO to I/O access fixes the issue as well.
4560 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004561 RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4562 RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4563 RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4564 RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004565}
4566
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004567static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
Francois Romieu6dccd162007-02-13 23:38:05 +01004568{
Francois Romieu37441002011-06-17 22:58:54 +02004569 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004570 u32 mac_version;
4571 u32 clk;
4572 u32 val;
4573 } cfg2_info [] = {
4574 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4575 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4576 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4577 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004578 };
4579 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004580 unsigned int i;
4581 u32 clk;
4582
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004583 clk = RTL_R8(tp, Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004584 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004585 if ((p->mac_version == mac_version) && (p->clk == clk)) {
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004586 RTL_W32(tp, 0x7c, p->val);
Francois Romieu6dccd162007-02-13 23:38:05 +01004587 break;
4588 }
4589 }
4590}
4591
Francois Romieue6b763e2012-03-08 09:35:39 +01004592static void rtl_set_rx_mode(struct net_device *dev)
4593{
4594 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieue6b763e2012-03-08 09:35:39 +01004595 u32 mc_filter[2]; /* Multicast hash filter */
4596 int rx_mode;
4597 u32 tmp = 0;
4598
4599 if (dev->flags & IFF_PROMISC) {
4600 /* Unconditionally log net taps. */
4601 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4602 rx_mode =
4603 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4604 AcceptAllPhys;
4605 mc_filter[1] = mc_filter[0] = 0xffffffff;
4606 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4607 (dev->flags & IFF_ALLMULTI)) {
4608 /* Too many to filter perfectly -- accept all multicasts. */
4609 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4610 mc_filter[1] = mc_filter[0] = 0xffffffff;
4611 } else {
4612 struct netdev_hw_addr *ha;
4613
4614 rx_mode = AcceptBroadcast | AcceptMyPhys;
4615 mc_filter[1] = mc_filter[0] = 0;
4616 netdev_for_each_mc_addr(ha, dev) {
4617 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4618 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4619 rx_mode |= AcceptMulticast;
4620 }
4621 }
4622
4623 if (dev->features & NETIF_F_RXALL)
4624 rx_mode |= (AcceptErr | AcceptRunt);
4625
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004626 tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Francois Romieue6b763e2012-03-08 09:35:39 +01004627
4628 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4629 u32 data = mc_filter[0];
4630
4631 mc_filter[0] = swab32(mc_filter[1]);
4632 mc_filter[1] = swab32(data);
4633 }
4634
Nathan Walp04817762012-11-01 12:08:47 +00004635 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4636 mc_filter[1] = mc_filter[0] = 0xffffffff;
4637
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004638 RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4639 RTL_W32(tp, MAR0 + 0, mc_filter[0]);
Francois Romieue6b763e2012-03-08 09:35:39 +01004640
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004641 RTL_W32(tp, RxConfig, tmp);
Francois Romieue6b763e2012-03-08 09:35:39 +01004642}
4643
Heiner Kallweit52f85602018-05-19 10:29:33 +02004644static void rtl_hw_start(struct rtl8169_private *tp)
4645{
4646 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
4647
4648 tp->hw_start(tp);
4649
4650 rtl_set_rx_max_size(tp);
4651 rtl_set_rx_tx_desc_registers(tp);
Heiner Kallweit52f85602018-05-19 10:29:33 +02004652 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
4653
4654 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4655 RTL_R8(tp, IntrMask);
4656 RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004657 rtl_init_rxcfg(tp);
Maciej S. Szmigierof74dd482018-09-07 20:15:22 +02004658 rtl_set_tx_config_registers(tp);
Azat Khuzhin05212ba2018-08-26 17:03:09 +03004659
Heiner Kallweit52f85602018-05-19 10:29:33 +02004660 rtl_set_rx_mode(tp->dev);
4661 /* no early-rx interrupts */
4662 RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
4663 rtl_irq_enable_all(tp);
4664}
4665
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004666static void rtl_hw_start_8169(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01004667{
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004668 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02004669 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004670
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004671 RTL_W8(tp, EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004673 tp->cp_cmd |= PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004674
Francois Romieucecb5fd2011-04-01 10:21:07 +02004675 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4676 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Heiner Kallweit49d17512018-06-28 20:36:15 +02004677 netif_dbg(tp, drv, tp->dev,
4678 "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004679 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 }
4681
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004682 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieubcf0bf92006-07-26 23:14:13 +02004683
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004684 rtl8169_set_magic_reg(tp, tp->mac_version);
Francois Romieu6dccd162007-02-13 23:38:05 +01004685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 /*
4687 * Undocumented corner. Supposedly:
4688 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4689 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004690 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004692 RTL_W32(tp, RxMissed, 0);
Francois Romieu07ce4062007-02-23 23:36:39 +01004693}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Francois Romieuffc46952012-07-06 14:19:23 +02004695DECLARE_RTL_COND(rtl_csiar_cond)
4696{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004697 return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
Francois Romieuffc46952012-07-06 14:19:23 +02004698}
4699
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004700static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004701{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004702 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4703
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004704 RTL_W32(tp, CSIDR, value);
4705 RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004706 CSIAR_BYTE_ENABLE | func << 16);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004707
Francois Romieuffc46952012-07-06 14:19:23 +02004708 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004709}
4710
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004711static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004712{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004713 u32 func = PCI_FUNC(tp->pci_dev->devfn);
4714
4715 RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4716 CSIAR_BYTE_ENABLE);
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004717
Francois Romieuffc46952012-07-06 14:19:23 +02004718 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004719 RTL_R32(tp, CSIDR) : ~0;
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004720}
4721
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004722static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
Hayes Wang7e18dca2012-03-30 14:33:02 +08004723{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004724 struct pci_dev *pdev = tp->pci_dev;
4725 u32 csi;
Hayes Wang7e18dca2012-03-30 14:33:02 +08004726
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004727 /* According to Realtek the value at config space address 0x070f
4728 * controls the L0s/L1 entrance latency. We try standard ECAM access
4729 * first and if it fails fall back to CSI.
4730 */
4731 if (pdev->cfg_size > 0x070f &&
4732 pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4733 return;
4734
4735 netdev_notice_once(tp->dev,
4736 "No native access to PCI extended config space, falling back to CSI\n");
4737 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4738 rtl_csi_write(tp, 0x070c, csi | val << 24);
Hayes Wang7e18dca2012-03-30 14:33:02 +08004739}
4740
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004741static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
hayeswang45dd95c2013-07-08 17:09:01 +08004742{
Heiner Kallweitff1d7332018-05-02 21:39:56 +02004743 rtl_csi_access_enable(tp, 0x27);
Francois Romieudacf8152008-08-02 20:44:13 +02004744}
4745
4746struct ephy_info {
4747 unsigned int offset;
4748 u16 mask;
4749 u16 bits;
4750};
4751
Francois Romieufdf6fc02012-07-06 22:40:38 +02004752static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
4753 int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004754{
4755 u16 w;
4756
4757 while (len-- > 0) {
Francois Romieufdf6fc02012-07-06 22:40:38 +02004758 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4759 rtl_ephy_write(tp, e->offset, w);
Francois Romieudacf8152008-08-02 20:44:13 +02004760 e++;
4761 }
4762}
4763
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004764static void rtl_disable_clock_request(struct rtl8169_private *tp)
Francois Romieub726e492008-06-28 12:22:59 +02004765{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004766 pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004767 PCI_EXP_LNKCTL_CLKREQ_EN);
Francois Romieub726e492008-06-28 12:22:59 +02004768}
4769
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004770static void rtl_enable_clock_request(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004771{
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004772 pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
Jiang Liu7d7903b2012-07-24 17:20:16 +08004773 PCI_EXP_LNKCTL_CLKREQ_EN);
françois romieue6de30d2011-01-03 15:08:37 +00004774}
4775
hayeswangb51ecea2014-07-09 14:52:51 +08004776static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
4777{
hayeswangb51ecea2014-07-09 14:52:51 +08004778 u8 data;
4779
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004780 data = RTL_R8(tp, Config3);
hayeswangb51ecea2014-07-09 14:52:51 +08004781
4782 if (enable)
4783 data |= Rdy_to_L23;
4784 else
4785 data &= ~Rdy_to_L23;
4786
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004787 RTL_W8(tp, Config3, data);
hayeswangb51ecea2014-07-09 14:52:51 +08004788}
4789
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004790static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4791{
4792 if (enable) {
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004793 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
Kai-Heng Feng94235462018-09-12 14:58:20 +08004794 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004795 } else {
4796 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4797 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4798 }
Kai-Heng Feng94235462018-09-12 14:58:20 +08004799
4800 udelay(10);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08004801}
4802
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004803static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004804{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004805 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004806
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004807 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004808 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieub726e492008-06-28 12:22:59 +02004809
françois romieufaf1e782013-02-27 13:01:57 +00004810 if (tp->dev->mtu <= ETH_DATA_LEN) {
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004811 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
françois romieufaf1e782013-02-27 13:01:57 +00004812 PCI_EXP_DEVCTL_NOSNOOP_EN);
4813 }
Francois Romieu219a1e92008-06-28 11:58:39 +02004814}
4815
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004816static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004817{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004818 rtl_hw_start_8168bb(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004819
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004820 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004821
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004822 RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004823}
4824
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004825static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004826{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004827 RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
Francois Romieub726e492008-06-28 12:22:59 +02004828
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004829 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieub726e492008-06-28 12:22:59 +02004830
françois romieufaf1e782013-02-27 13:01:57 +00004831 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004832 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieub726e492008-06-28 12:22:59 +02004833
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004834 rtl_disable_clock_request(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004835
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004836 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004837 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu219a1e92008-06-28 11:58:39 +02004838}
4839
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004840static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004841{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004842 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004843 { 0x01, 0, 0x0001 },
4844 { 0x02, 0x0800, 0x1000 },
4845 { 0x03, 0, 0x0042 },
4846 { 0x06, 0x0080, 0x0000 },
4847 { 0x07, 0, 0x2000 }
4848 };
4849
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004850 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004851
Francois Romieufdf6fc02012-07-06 22:40:38 +02004852 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
Francois Romieub726e492008-06-28 12:22:59 +02004853
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004854 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004855}
4856
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004857static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02004858{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004859 rtl_set_def_aspm_entry_latency(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02004860
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004861 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieuef3386f2008-06-29 12:24:30 +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 Romieuef3386f2008-06-29 12:24:30 +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 Romieuef3386f2008-06-29 12:24:30 +02004868}
4869
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004870static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004871{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004872 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004873
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004874 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004875
4876 /* Magic. */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004877 RTL_W8(tp, DBG_REG, 0x20);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004878
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004879 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004880
françois romieufaf1e782013-02-27 13:01:57 +00004881 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004882 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004883
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004884 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004885 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004886}
4887
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004888static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004889{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004890 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004891 { 0x02, 0x0800, 0x1000 },
4892 { 0x03, 0, 0x0002 },
4893 { 0x06, 0x0080, 0x0000 }
4894 };
4895
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004896 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004897
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004898 RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
Francois Romieub726e492008-06-28 12:22:59 +02004899
Francois Romieufdf6fc02012-07-06 22:40:38 +02004900 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
Francois Romieub726e492008-06-28 12:22:59 +02004901
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004902 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004903}
4904
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004905static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
Francois Romieu219a1e92008-06-28 11:58:39 +02004906{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004907 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004908 { 0x01, 0, 0x0001 },
4909 { 0x03, 0x0400, 0x0220 }
4910 };
4911
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004912 rtl_set_def_aspm_entry_latency(tp);
Francois Romieub726e492008-06-28 12:22:59 +02004913
Francois Romieufdf6fc02012-07-06 22:40:38 +02004914 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
Francois Romieub726e492008-06-28 12:22:59 +02004915
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004916 __rtl_hw_start_8168cp(tp);
Francois Romieu219a1e92008-06-28 11:58:39 +02004917}
4918
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004919static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02004920{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004921 rtl_hw_start_8168c_2(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02004922}
4923
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004924static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02004925{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004926 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004927
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004928 __rtl_hw_start_8168cp(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02004929}
4930
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004931static void rtl_hw_start_8168d(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02004932{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004933 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004934
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004935 rtl_disable_clock_request(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02004936
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004937 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004938
françois romieufaf1e782013-02-27 13:01:57 +00004939 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004940 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu5b538df2008-07-20 16:22:45 +02004941
Heiner Kallweit12d42c52018-04-28 22:19:30 +02004942 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Heiner Kallweit0ae09742018-04-28 22:19:26 +02004943 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu5b538df2008-07-20 16:22:45 +02004944}
4945
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004946static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
hayeswang4804b3b2011-03-21 01:50:29 +00004947{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004948 rtl_set_def_aspm_entry_latency(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004949
françois romieufaf1e782013-02-27 13:01:57 +00004950 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004951 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang4804b3b2011-03-21 01:50:29 +00004952
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004953 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang4804b3b2011-03-21 01:50:29 +00004954
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004955 rtl_disable_clock_request(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00004956}
4957
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004958static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
françois romieue6de30d2011-01-03 15:08:37 +00004959{
4960 static const struct ephy_info e_info_8168d_4[] = {
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004961 { 0x0b, 0x0000, 0x0048 },
4962 { 0x19, 0x0020, 0x0050 },
4963 { 0x0c, 0x0100, 0x0020 }
françois romieue6de30d2011-01-03 15:08:37 +00004964 };
françois romieue6de30d2011-01-03 15:08:37 +00004965
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004966 rtl_set_def_aspm_entry_latency(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004967
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02004968 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
françois romieue6de30d2011-01-03 15:08:37 +00004969
Andy Shevchenko1ef72862018-03-01 13:27:34 +02004970 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
françois romieue6de30d2011-01-03 15:08:37 +00004971
Chun-Hao Lin1016a4a2015-12-29 22:13:39 +08004972 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
françois romieue6de30d2011-01-03 15:08:37 +00004973
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01004974 rtl_enable_clock_request(tp);
françois romieue6de30d2011-01-03 15:08:37 +00004975}
4976
Hayes Wangbeb1fe12012-03-30 14:33:01 +08004977static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00004978{
Hayes Wang70090422011-07-06 15:58:06 +08004979 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004980 { 0x00, 0x0200, 0x0100 },
4981 { 0x00, 0x0000, 0x0004 },
4982 { 0x06, 0x0002, 0x0001 },
4983 { 0x06, 0x0000, 0x0030 },
4984 { 0x07, 0x0000, 0x2000 },
4985 { 0x00, 0x0000, 0x0020 },
4986 { 0x03, 0x5800, 0x2000 },
4987 { 0x03, 0x0000, 0x0001 },
4988 { 0x01, 0x0800, 0x1000 },
4989 { 0x07, 0x0000, 0x4000 },
4990 { 0x1e, 0x0000, 0x2000 },
4991 { 0x19, 0xffff, 0xfe6c },
4992 { 0x0a, 0x0000, 0x0040 }
4993 };
4994
Heiner Kallweitf37658d2018-06-23 09:51:28 +02004995 rtl_set_def_aspm_entry_latency(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00004996
Francois Romieufdf6fc02012-07-06 22:40:38 +02004997 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004998
françois romieufaf1e782013-02-27 13:01:57 +00004999 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005000 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
hayeswang01dc7fe2011-03-21 01:50:28 +00005001
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005002 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang01dc7fe2011-03-21 01:50:28 +00005003
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005004 rtl_disable_clock_request(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005005
5006 /* Reset tx FIFO pointer */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005007 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
5008 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00005009
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005010 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00005011}
5012
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005013static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
Hayes Wang70090422011-07-06 15:58:06 +08005014{
5015 static const struct ephy_info e_info_8168e_2[] = {
5016 { 0x09, 0x0000, 0x0080 },
5017 { 0x19, 0x0000, 0x0224 }
5018 };
5019
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005020 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005021
Francois Romieufdf6fc02012-07-06 22:40:38 +02005022 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
Hayes Wang70090422011-07-06 15:58:06 +08005023
françois romieufaf1e782013-02-27 13:01:57 +00005024 if (tp->dev->mtu <= ETH_DATA_LEN)
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005025 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang70090422011-07-06 15:58:06 +08005026
Francois Romieufdf6fc02012-07-06 22:40:38 +02005027 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5028 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5029 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5030 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5031 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5032 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005033 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5034 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang70090422011-07-06 15:58:06 +08005035
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005036 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08005037
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005038 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005039
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005040 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang70090422011-07-06 15:58:06 +08005041
5042 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005043 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang70090422011-07-06 15:58:06 +08005044
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005045 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5046 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5047 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Heiner Kallweitaa1e7d22018-06-23 09:53:00 +02005048
5049 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang70090422011-07-06 15:58:06 +08005050}
5051
Hayes Wang5f886e02012-03-30 14:33:03 +08005052static void rtl_hw_start_8168f(struct rtl8169_private *tp)
Hayes Wangc2218922011-09-06 16:55:18 +08005053{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005054 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005055
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005056 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc2218922011-09-06 16:55:18 +08005057
Francois Romieufdf6fc02012-07-06 22:40:38 +02005058 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5059 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5060 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
5061 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005062 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5063 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5064 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
5065 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005066 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
5067 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
Hayes Wangc2218922011-09-06 16:55:18 +08005068
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005069 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc2218922011-09-06 16:55:18 +08005070
Heiner Kallweit73c86ee2018-03-20 07:45:40 +01005071 rtl_disable_clock_request(tp);
Francois Romieu4521e1a92012-11-01 16:46:28 +00005072
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005073 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5074 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5075 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5076 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
Hayes Wangc2218922011-09-06 16:55:18 +08005077}
5078
Hayes Wang5f886e02012-03-30 14:33:03 +08005079static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5080{
Hayes Wang5f886e02012-03-30 14:33:03 +08005081 static const struct ephy_info e_info_8168f_1[] = {
5082 { 0x06, 0x00c0, 0x0020 },
5083 { 0x08, 0x0001, 0x0002 },
5084 { 0x09, 0x0000, 0x0080 },
5085 { 0x19, 0x0000, 0x0224 }
5086 };
5087
5088 rtl_hw_start_8168f(tp);
5089
Francois Romieufdf6fc02012-07-06 22:40:38 +02005090 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wang5f886e02012-03-30 14:33:03 +08005091
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005092 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
Hayes Wang5f886e02012-03-30 14:33:03 +08005093
5094 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005095 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wang5f886e02012-03-30 14:33:03 +08005096}
5097
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005098static void rtl_hw_start_8411(struct rtl8169_private *tp)
5099{
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005100 static const struct ephy_info e_info_8168f_1[] = {
5101 { 0x06, 0x00c0, 0x0020 },
5102 { 0x0f, 0xffff, 0x5200 },
5103 { 0x1e, 0x0000, 0x4000 },
5104 { 0x19, 0x0000, 0x0224 }
5105 };
5106
5107 rtl_hw_start_8168f(tp);
hayeswangb51ecea2014-07-09 14:52:51 +08005108 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005109
Francois Romieufdf6fc02012-07-06 22:40:38 +02005110 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005111
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005112 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005113}
5114
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005115static void rtl_hw_start_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08005116{
Hayes Wangc5583862012-07-02 17:23:22 +08005117 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
5118 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5119 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5120 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5121
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005122 rtl_set_def_aspm_entry_latency(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08005123
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005124 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wangc5583862012-07-02 17:23:22 +08005125
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005126 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5127 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
hayeswangbeb330a2013-04-01 22:23:39 +00005128 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
Hayes Wangc5583862012-07-02 17:23:22 +08005129
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005130 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5131 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Hayes Wangc5583862012-07-02 17:23:22 +08005132
5133 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5134 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5135
5136 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005137 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Hayes Wangc5583862012-07-02 17:23:22 +08005138
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005139 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5140 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005141
5142 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wangc5583862012-07-02 17:23:22 +08005143}
5144
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005145static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5146{
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005147 static const struct ephy_info e_info_8168g_1[] = {
5148 { 0x00, 0x0000, 0x0008 },
5149 { 0x0c, 0x37d0, 0x0820 },
5150 { 0x1e, 0x0000, 0x0001 },
5151 { 0x19, 0x8000, 0x0000 }
5152 };
5153
5154 rtl_hw_start_8168g(tp);
5155
5156 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005157 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005158 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005159 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005160}
5161
hayeswang57538c42013-04-01 22:23:40 +00005162static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5163{
hayeswang57538c42013-04-01 22:23:40 +00005164 static const struct ephy_info e_info_8168g_2[] = {
5165 { 0x00, 0x0000, 0x0008 },
5166 { 0x0c, 0x3df0, 0x0200 },
5167 { 0x19, 0xffff, 0xfc00 },
5168 { 0x1e, 0xffff, 0x20eb }
5169 };
5170
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005171 rtl_hw_start_8168g(tp);
hayeswang57538c42013-04-01 22:23:40 +00005172
5173 /* disable aspm and clock request before access ephy */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005174 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5175 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
hayeswang57538c42013-04-01 22:23:40 +00005176 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
5177}
5178
hayeswang45dd95c2013-07-08 17:09:01 +08005179static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5180{
hayeswang45dd95c2013-07-08 17:09:01 +08005181 static const struct ephy_info e_info_8411_2[] = {
5182 { 0x00, 0x0000, 0x0008 },
5183 { 0x0c, 0x3df0, 0x0200 },
5184 { 0x0f, 0xffff, 0x5200 },
5185 { 0x19, 0x0020, 0x0000 },
5186 { 0x1e, 0x0000, 0x2000 }
5187 };
5188
Chun-Hao Lin5fbea332014-12-10 21:28:38 +08005189 rtl_hw_start_8168g(tp);
hayeswang45dd95c2013-07-08 17:09:01 +08005190
5191 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005192 rtl_hw_aspm_clkreq_enable(tp, false);
hayeswang45dd95c2013-07-08 17:09:01 +08005193 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005194 rtl_hw_aspm_clkreq_enable(tp, true);
hayeswang45dd95c2013-07-08 17:09:01 +08005195}
5196
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005197static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5198{
Andrzej Hajda72521ea2015-09-24 16:00:24 +02005199 int rg_saw_cnt;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005200 u32 data;
5201 static const struct ephy_info e_info_8168h_1[] = {
5202 { 0x1e, 0x0800, 0x0001 },
5203 { 0x1d, 0x0000, 0x0800 },
5204 { 0x05, 0xffff, 0x2089 },
5205 { 0x06, 0xffff, 0x5881 },
5206 { 0x04, 0xffff, 0x154a },
5207 { 0x01, 0xffff, 0x068b }
5208 };
5209
5210 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005211 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005212 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
5213
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005214 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5215 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
5216 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
5217 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5218
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005219 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005220
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005221 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005222
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005223 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5224 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005225
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005226 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005227
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005228 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005229
5230 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5231
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005232 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5233 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005234
5235 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5236 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5237
5238 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005239 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005240
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005241 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5242 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005243
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005244 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005245
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005246 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005247
5248 rtl_pcie_state_l2l3_enable(tp, false);
5249
5250 rtl_writephy(tp, 0x1f, 0x0c42);
Chun-Hao Lin58493332015-12-24 21:15:27 +08005251 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005252 rtl_writephy(tp, 0x1f, 0x0000);
5253 if (rg_saw_cnt > 0) {
5254 u16 sw_cnt_1ms_ini;
5255
5256 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5257 sw_cnt_1ms_ini &= 0x0fff;
5258 data = r8168_mac_ocp_read(tp, 0xd412);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005259 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005260 data |= sw_cnt_1ms_ini;
5261 r8168_mac_ocp_write(tp, 0xd412, data);
5262 }
5263
5264 data = r8168_mac_ocp_read(tp, 0xe056);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005265 data &= ~0xf0;
5266 data |= 0x70;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005267 r8168_mac_ocp_write(tp, 0xe056, data);
5268
5269 data = r8168_mac_ocp_read(tp, 0xe052);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005270 data &= ~0x6000;
5271 data |= 0x8008;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005272 r8168_mac_ocp_write(tp, 0xe052, data);
5273
5274 data = r8168_mac_ocp_read(tp, 0xe0d6);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005275 data &= ~0x01ff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005276 data |= 0x017f;
5277 r8168_mac_ocp_write(tp, 0xe0d6, data);
5278
5279 data = r8168_mac_ocp_read(tp, 0xd420);
Chun-Hao Lina2cb7ec2016-02-05 02:28:00 +08005280 data &= ~0x0fff;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005281 data |= 0x047f;
5282 r8168_mac_ocp_write(tp, 0xd420, data);
5283
5284 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5285 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5286 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5287 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005288
5289 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005290}
5291
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005292static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5293{
Chun-Hao Lin003609d2014-12-02 16:48:31 +08005294 rtl8168ep_stop_cmac(tp);
5295
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005296 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
5297 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
5298 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
5299 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
5300
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005301 rtl_set_def_aspm_entry_latency(tp);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005302
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005303 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005304
5305 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5306 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
5307
5308 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
5309
5310 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
5311
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005312 RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5313 RTL_W8(tp, MaxTxPacketSize, EarlySize);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005314
5315 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5316 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5317
5318 /* Adjust EEE LED frequency */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005319 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005320
5321 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
5322
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005323 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005324
5325 rtl_pcie_state_l2l3_enable(tp, false);
5326}
5327
5328static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5329{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005330 static const struct ephy_info e_info_8168ep_1[] = {
5331 { 0x00, 0xffff, 0x10ab },
5332 { 0x06, 0xffff, 0xf030 },
5333 { 0x08, 0xffff, 0x2006 },
5334 { 0x0d, 0xffff, 0x1666 },
5335 { 0x0c, 0x3ff0, 0x0000 }
5336 };
5337
5338 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005339 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005340 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
5341
5342 rtl_hw_start_8168ep(tp);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005343
5344 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005345}
5346
5347static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5348{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005349 static const struct ephy_info e_info_8168ep_2[] = {
5350 { 0x00, 0xffff, 0x10a3 },
5351 { 0x19, 0xffff, 0xfc00 },
5352 { 0x1e, 0xffff, 0x20ea }
5353 };
5354
5355 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005356 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005357 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
5358
5359 rtl_hw_start_8168ep(tp);
5360
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005361 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5362 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005363
5364 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005365}
5366
5367static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5368{
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005369 u32 data;
5370 static const struct ephy_info e_info_8168ep_3[] = {
5371 { 0x00, 0xffff, 0x10a3 },
5372 { 0x19, 0xffff, 0x7c00 },
5373 { 0x1e, 0xffff, 0x20eb },
5374 { 0x0d, 0xffff, 0x1666 }
5375 };
5376
5377 /* disable aspm and clock request before access ephy */
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005378 rtl_hw_aspm_clkreq_enable(tp, false);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005379 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
5380
5381 rtl_hw_start_8168ep(tp);
5382
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005383 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5384 RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005385
5386 data = r8168_mac_ocp_read(tp, 0xd3e2);
5387 data &= 0xf000;
5388 data |= 0x0271;
5389 r8168_mac_ocp_write(tp, 0xd3e2, data);
5390
5391 data = r8168_mac_ocp_read(tp, 0xd3e4);
5392 data &= 0xff00;
5393 r8168_mac_ocp_write(tp, 0xd3e4, data);
5394
5395 data = r8168_mac_ocp_read(tp, 0xe860);
5396 data |= 0x0080;
5397 r8168_mac_ocp_write(tp, 0xe860, data);
Kai-Heng Fenga99790b2018-06-21 16:30:39 +08005398
5399 rtl_hw_aspm_clkreq_enable(tp, true);
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005400}
5401
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005402static void rtl_hw_start_8168(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005403{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005404 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02005405
Heiner Kallweit0ae09742018-04-28 22:19:26 +02005406 tp->cp_cmd &= ~INTT_MASK;
5407 tp->cp_cmd |= PktCntrDisable | INTT_1;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005408 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Francois Romieu2dd99532007-06-11 23:22:52 +02005409
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005410 RTL_W16(tp, IntrMitigate, 0x5151);
Francois Romieu0e485152007-02-20 00:00:26 +01005411
5412 /* Work around for RxFIFO overflow. */
françois romieu811fd302011-12-04 20:30:45 +00005413 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01005414 tp->event_slow |= RxFIFOOver | PCSTimeout;
5415 tp->event_slow &= ~RxOverflow;
Francois Romieu0e485152007-02-20 00:00:26 +01005416 }
Francois Romieu2dd99532007-06-11 23:22:52 +02005417
Francois Romieu219a1e92008-06-28 11:58:39 +02005418 switch (tp->mac_version) {
5419 case RTL_GIGA_MAC_VER_11:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005420 rtl_hw_start_8168bb(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005421 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005422
5423 case RTL_GIGA_MAC_VER_12:
5424 case RTL_GIGA_MAC_VER_17:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005425 rtl_hw_start_8168bef(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005426 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005427
5428 case RTL_GIGA_MAC_VER_18:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005429 rtl_hw_start_8168cp_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005430 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005431
5432 case RTL_GIGA_MAC_VER_19:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005433 rtl_hw_start_8168c_1(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005434 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005435
5436 case RTL_GIGA_MAC_VER_20:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005437 rtl_hw_start_8168c_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005438 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005439
Francois Romieu197ff762008-06-28 13:16:02 +02005440 case RTL_GIGA_MAC_VER_21:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005441 rtl_hw_start_8168c_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005442 break;
Francois Romieu197ff762008-06-28 13:16:02 +02005443
Francois Romieu6fb07052008-06-29 11:54:28 +02005444 case RTL_GIGA_MAC_VER_22:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005445 rtl_hw_start_8168c_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005446 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02005447
Francois Romieuef3386f2008-06-29 12:24:30 +02005448 case RTL_GIGA_MAC_VER_23:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005449 rtl_hw_start_8168cp_2(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005450 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02005451
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005452 case RTL_GIGA_MAC_VER_24:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005453 rtl_hw_start_8168cp_3(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005454 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02005455
Francois Romieu5b538df2008-07-20 16:22:45 +02005456 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00005457 case RTL_GIGA_MAC_VER_26:
5458 case RTL_GIGA_MAC_VER_27:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005459 rtl_hw_start_8168d(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005460 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02005461
françois romieue6de30d2011-01-03 15:08:37 +00005462 case RTL_GIGA_MAC_VER_28:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005463 rtl_hw_start_8168d_4(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005464 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02005465
hayeswang4804b3b2011-03-21 01:50:29 +00005466 case RTL_GIGA_MAC_VER_31:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005467 rtl_hw_start_8168dp(tp);
hayeswang4804b3b2011-03-21 01:50:29 +00005468 break;
5469
hayeswang01dc7fe2011-03-21 01:50:28 +00005470 case RTL_GIGA_MAC_VER_32:
5471 case RTL_GIGA_MAC_VER_33:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005472 rtl_hw_start_8168e_1(tp);
Hayes Wang70090422011-07-06 15:58:06 +08005473 break;
5474 case RTL_GIGA_MAC_VER_34:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005475 rtl_hw_start_8168e_2(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00005476 break;
françois romieue6de30d2011-01-03 15:08:37 +00005477
Hayes Wangc2218922011-09-06 16:55:18 +08005478 case RTL_GIGA_MAC_VER_35:
5479 case RTL_GIGA_MAC_VER_36:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005480 rtl_hw_start_8168f_1(tp);
Hayes Wangc2218922011-09-06 16:55:18 +08005481 break;
5482
Hayes Wangb3d7b2f2012-03-30 14:48:06 +08005483 case RTL_GIGA_MAC_VER_38:
5484 rtl_hw_start_8411(tp);
5485 break;
5486
Hayes Wangc5583862012-07-02 17:23:22 +08005487 case RTL_GIGA_MAC_VER_40:
5488 case RTL_GIGA_MAC_VER_41:
5489 rtl_hw_start_8168g_1(tp);
5490 break;
hayeswang57538c42013-04-01 22:23:40 +00005491 case RTL_GIGA_MAC_VER_42:
5492 rtl_hw_start_8168g_2(tp);
5493 break;
Hayes Wangc5583862012-07-02 17:23:22 +08005494
hayeswang45dd95c2013-07-08 17:09:01 +08005495 case RTL_GIGA_MAC_VER_44:
5496 rtl_hw_start_8411_2(tp);
5497 break;
5498
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005499 case RTL_GIGA_MAC_VER_45:
5500 case RTL_GIGA_MAC_VER_46:
5501 rtl_hw_start_8168h_1(tp);
5502 break;
5503
Chun-Hao Lin935e2212014-10-07 15:10:41 +08005504 case RTL_GIGA_MAC_VER_49:
5505 rtl_hw_start_8168ep_1(tp);
5506 break;
5507
5508 case RTL_GIGA_MAC_VER_50:
5509 rtl_hw_start_8168ep_2(tp);
5510 break;
5511
5512 case RTL_GIGA_MAC_VER_51:
5513 rtl_hw_start_8168ep_3(tp);
5514 break;
5515
Francois Romieu219a1e92008-06-28 11:58:39 +02005516 default:
Heiner Kallweit49d17512018-06-28 20:36:15 +02005517 netif_err(tp, drv, tp->dev,
5518 "unknown chipset (mac_version = %d)\n",
5519 tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00005520 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02005521 }
Francois Romieu07ce4062007-02-23 23:36:39 +01005522}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005524static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005525{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08005526 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02005527 { 0x01, 0, 0x6e65 },
5528 { 0x02, 0, 0x091f },
5529 { 0x03, 0, 0xc2f9 },
5530 { 0x06, 0, 0xafb5 },
5531 { 0x07, 0, 0x0e00 },
5532 { 0x19, 0, 0xec80 },
5533 { 0x01, 0, 0x2e65 },
5534 { 0x01, 0, 0x6e65 }
5535 };
5536 u8 cfg1;
5537
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005538 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005539
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005540 RTL_W8(tp, DBG_REG, FIX_NAK_1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005541
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005542 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005543
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005544 RTL_W8(tp, Config1,
Francois Romieu2857ffb2008-08-02 21:08:49 +02005545 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005546 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005547
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005548 cfg1 = RTL_R8(tp, Config1);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005549 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005550 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005551
Francois Romieufdf6fc02012-07-06 22:40:38 +02005552 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
Francois Romieu2857ffb2008-08-02 21:08:49 +02005553}
5554
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005555static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005556{
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005557 rtl_set_def_aspm_entry_latency(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005558
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005559 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005560
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005561 RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5562 RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005563}
5564
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005565static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02005566{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005567 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005568
Francois Romieufdf6fc02012-07-06 22:40:38 +02005569 rtl_ephy_write(tp, 0x03, 0xc2f9);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005570}
5571
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005572static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005573{
5574 static const struct ephy_info e_info_8105e_1[] = {
5575 { 0x07, 0, 0x4000 },
5576 { 0x19, 0, 0x0200 },
5577 { 0x19, 0, 0x0020 },
5578 { 0x1e, 0, 0x2000 },
5579 { 0x03, 0, 0x0001 },
5580 { 0x19, 0, 0x0100 },
5581 { 0x19, 0, 0x0004 },
5582 { 0x0a, 0, 0x0020 }
5583 };
5584
Francois Romieucecb5fd2011-04-01 10:21:07 +02005585 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005586 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005587
Francois Romieucecb5fd2011-04-01 10:21:07 +02005588 /* Disable Early Tally Counter */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005589 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005590
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005591 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5592 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005593
Francois Romieufdf6fc02012-07-06 22:40:38 +02005594 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
hayeswangb51ecea2014-07-09 14:52:51 +08005595
5596 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005597}
5598
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005599static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
Hayes Wang5a5e4442011-02-22 17:26:21 +08005600{
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005601 rtl_hw_start_8105e_1(tp);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005602 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005603}
5604
Hayes Wang7e18dca2012-03-30 14:33:02 +08005605static void rtl_hw_start_8402(struct rtl8169_private *tp)
5606{
Hayes Wang7e18dca2012-03-30 14:33:02 +08005607 static const struct ephy_info e_info_8402[] = {
5608 { 0x19, 0xffff, 0xff64 },
5609 { 0x1e, 0, 0x4000 }
5610 };
5611
Heiner Kallweitf37658d2018-06-23 09:51:28 +02005612 rtl_set_def_aspm_entry_latency(tp);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005613
5614 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005615 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005616
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005617 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005618
Francois Romieufdf6fc02012-07-06 22:40:38 +02005619 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
Hayes Wang7e18dca2012-03-30 14:33:02 +08005620
Heiner Kallweit8d98aa32018-04-16 21:38:27 +02005621 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005622
Francois Romieufdf6fc02012-07-06 22:40:38 +02005623 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
5624 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005625 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
5626 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
Francois Romieufdf6fc02012-07-06 22:40:38 +02005627 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5628 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
Chun-Hao Lin706123d2014-10-01 23:17:18 +08005629 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
hayeswangb51ecea2014-07-09 14:52:51 +08005630
5631 rtl_pcie_state_l2l3_enable(tp, false);
Hayes Wang7e18dca2012-03-30 14:33:02 +08005632}
5633
Hayes Wang5598bfe2012-07-02 17:23:21 +08005634static void rtl_hw_start_8106(struct rtl8169_private *tp)
5635{
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005636 rtl_hw_aspm_clkreq_enable(tp, false);
5637
Hayes Wang5598bfe2012-07-02 17:23:21 +08005638 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005639 RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005640
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005641 RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5642 RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5643 RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
hayeswangb51ecea2014-07-09 14:52:51 +08005644
5645 rtl_pcie_state_l2l3_enable(tp, false);
Kai-Heng Feng0866cd12018-09-12 14:58:21 +08005646 rtl_hw_aspm_clkreq_enable(tp, true);
Hayes Wang5598bfe2012-07-02 17:23:21 +08005647}
5648
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005649static void rtl_hw_start_8101(struct rtl8169_private *tp)
Francois Romieu07ce4062007-02-23 23:36:39 +01005650{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005651 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5652 tp->event_slow &= ~RxFIFOOver;
françois romieu811fd302011-12-04 20:30:45 +00005653
Francois Romieucecb5fd2011-04-01 10:21:07 +02005654 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
Jiang Liu7d7903b2012-07-24 17:20:16 +08005655 tp->mac_version == RTL_GIGA_MAC_VER_16)
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005656 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
Bjorn Helgaas8200bc72012-08-22 10:29:42 -06005657 PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieucdf1a602007-06-11 23:29:50 +02005658
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005659 RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
hayeswang1a964642013-04-01 22:23:41 +00005660
Heiner Kallweit12d42c52018-04-28 22:19:30 +02005661 tp->cp_cmd &= CPCMD_QUIRK_MASK;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005662 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
hayeswang1a964642013-04-01 22:23:41 +00005663
Francois Romieu2857ffb2008-08-02 21:08:49 +02005664 switch (tp->mac_version) {
5665 case RTL_GIGA_MAC_VER_07:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005666 rtl_hw_start_8102e_1(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005667 break;
5668
5669 case RTL_GIGA_MAC_VER_08:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005670 rtl_hw_start_8102e_3(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005671 break;
5672
5673 case RTL_GIGA_MAC_VER_09:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005674 rtl_hw_start_8102e_2(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02005675 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08005676
5677 case RTL_GIGA_MAC_VER_29:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005678 rtl_hw_start_8105e_1(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005679 break;
5680 case RTL_GIGA_MAC_VER_30:
Hayes Wangbeb1fe12012-03-30 14:33:01 +08005681 rtl_hw_start_8105e_2(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08005682 break;
Hayes Wang7e18dca2012-03-30 14:33:02 +08005683
5684 case RTL_GIGA_MAC_VER_37:
5685 rtl_hw_start_8402(tp);
5686 break;
Hayes Wang5598bfe2012-07-02 17:23:21 +08005687
5688 case RTL_GIGA_MAC_VER_39:
5689 rtl_hw_start_8106(tp);
5690 break;
hayeswang58152cd2013-04-01 22:23:42 +00005691 case RTL_GIGA_MAC_VER_43:
5692 rtl_hw_start_8168g_2(tp);
5693 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005694 case RTL_GIGA_MAC_VER_47:
5695 case RTL_GIGA_MAC_VER_48:
5696 rtl_hw_start_8168h_1(tp);
5697 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02005698 }
5699
Andy Shevchenko1ef72862018-03-01 13:27:34 +02005700 RTL_W16(tp, IntrMitigate, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701}
5702
5703static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5704{
Francois Romieud58d46b2011-05-03 16:38:29 +02005705 struct rtl8169_private *tp = netdev_priv(dev);
5706
Francois Romieud58d46b2011-05-03 16:38:29 +02005707 if (new_mtu > ETH_DATA_LEN)
5708 rtl_hw_jumbo_enable(tp);
5709 else
5710 rtl_hw_jumbo_disable(tp);
5711
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00005713 netdev_update_features(dev);
5714
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
5717
5718static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5719{
Al Viro95e09182007-12-22 18:55:39 +00005720 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5722}
5723
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005724static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5725 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726{
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005727 dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5728 R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005729
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005730 kfree(*data_buff);
5731 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 rtl8169_make_unusable_by_asic(desc);
5733}
5734
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005735static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736{
5737 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5738
Alexander Duycka0750132014-12-11 15:02:17 -08005739 /* Force memory writes to complete before releasing descriptor */
5740 dma_wmb();
5741
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005742 desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743}
5744
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005745static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005747 return (void *)ALIGN((long)data, 16);
5748}
5749
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005750static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5751 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005752{
5753 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 dma_addr_t mapping;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005755 struct device *d = tp_to_dev(tp);
Heiner Kallweitd3b404c2018-04-17 23:22:14 +02005756 int node = dev_to_node(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005758 data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005759 if (!data)
5760 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01005761
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005762 if (rtl8169_align(data) != data) {
5763 kfree(data);
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005764 data = kmalloc_node(R8169_RX_BUF_SIZE + 15, GFP_KERNEL, node);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005765 if (!data)
5766 return NULL;
5767 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005768
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005769 mapping = dma_map_single(d, rtl8169_align(data), R8169_RX_BUF_SIZE,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00005770 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005771 if (unlikely(dma_mapping_error(d, mapping))) {
5772 if (net_ratelimit())
5773 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005774 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776
Heiner Kallweitd731af72018-04-17 23:26:41 +02005777 desc->addr = cpu_to_le64(mapping);
5778 rtl8169_mark_to_asic(desc);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005779 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005780
5781err_out:
5782 kfree(data);
5783 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784}
5785
5786static void rtl8169_rx_clear(struct rtl8169_private *tp)
5787{
Francois Romieu07d3f512007-02-21 22:40:46 +01005788 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
5790 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005791 if (tp->Rx_databuff[i]) {
5792 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 tp->RxDescArray + i);
5794 }
5795 }
5796}
5797
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005798static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005800 desc->opts1 |= cpu_to_le32(RingEnd);
5801}
Francois Romieu5b0384f2006-08-16 16:00:01 +02005802
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005803static int rtl8169_rx_fill(struct rtl8169_private *tp)
5804{
5805 unsigned int i;
5806
5807 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005808 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02005809
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005810 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005811 if (!data) {
5812 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005813 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005814 }
5815 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005818 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5819 return 0;
5820
5821err_out:
5822 rtl8169_rx_clear(tp);
5823 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824}
5825
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005826static int rtl8169_init_ring(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 rtl8169_init_ring_indexes(tp);
5829
Heiner Kallweitb1127e62018-04-17 23:23:35 +02005830 memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5831 memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00005833 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834}
5835
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005836static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 struct TxDesc *desc)
5838{
5839 unsigned int len = tx_skb->len;
5840
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005841 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5842
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 desc->opts1 = 0x00;
5844 desc->opts2 = 0x00;
5845 desc->addr = 0x00;
5846 tx_skb->len = 0;
5847}
5848
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005849static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5850 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851{
5852 unsigned int i;
5853
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005854 for (i = 0; i < n; i++) {
5855 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 struct ring_info *tx_skb = tp->tx_skb + entry;
5857 unsigned int len = tx_skb->len;
5858
5859 if (len) {
5860 struct sk_buff *skb = tx_skb->skb;
5861
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005862 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 tp->TxDescArray + entry);
5864 if (skb) {
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07005865 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 tx_skb->skb = NULL;
5867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 }
5869 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005870}
5871
5872static void rtl8169_tx_clear(struct rtl8169_private *tp)
5873{
5874 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 tp->cur_tx = tp->dirty_tx = 0;
5876}
5877
Francois Romieu4422bcd2012-01-26 11:23:32 +01005878static void rtl_reset_work(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879{
David Howellsc4028952006-11-22 14:57:56 +00005880 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005881 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
Francois Romieuda78dbf2012-01-26 14:18:23 +01005883 napi_disable(&tp->napi);
5884 netif_stop_queue(dev);
5885 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
françois romieuc7c2c392011-12-04 20:30:52 +00005887 rtl8169_hw_reset(tp);
5888
Francois Romieu56de4142011-03-15 17:29:31 +01005889 for (i = 0; i < NUM_RX_DESC; i++)
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02005890 rtl8169_mark_to_asic(tp->RxDescArray + i);
Francois Romieu56de4142011-03-15 17:29:31 +01005891
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 rtl8169_tx_clear(tp);
françois romieuc7c2c392011-12-04 20:30:52 +00005893 rtl8169_init_ring_indexes(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Francois Romieuda78dbf2012-01-26 14:18:23 +01005895 napi_enable(&tp->napi);
Heiner Kallweit61cb5322018-04-17 23:27:38 +02005896 rtl_hw_start(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005897 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898}
5899
5900static void rtl8169_tx_timeout(struct net_device *dev)
5901{
Francois Romieuda78dbf2012-01-26 14:18:23 +01005902 struct rtl8169_private *tp = netdev_priv(dev);
5903
5904 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905}
5906
5907static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005908 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909{
5910 struct skb_shared_info *info = skb_shinfo(skb);
5911 unsigned int cur_frag, entry;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08005912 struct TxDesc *uninitialized_var(txd);
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01005913 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
5915 entry = tp->cur_tx;
5916 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00005917 const skb_frag_t *frag = info->frags + cur_frag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 dma_addr_t mapping;
5919 u32 status, len;
5920 void *addr;
5921
5922 entry = (entry + 1) % NUM_TX_DESC;
5923
5924 txd = tp->TxDescArray + entry;
Eric Dumazet9e903e02011-10-18 21:00:24 +00005925 len = skb_frag_size(frag);
Ian Campbell929f6182011-08-31 00:47:06 +00005926 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005927 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005928 if (unlikely(dma_mapping_error(d, mapping))) {
5929 if (net_ratelimit())
5930 netif_err(tp, drv, tp->dev,
5931 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005932 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
Francois Romieucecb5fd2011-04-01 10:21:07 +02005935 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005936 status = opts[0] | len |
5937 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
5939 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005940 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 txd->addr = cpu_to_le64(mapping);
5942
5943 tp->tx_skb[entry].len = len;
5944 }
5945
5946 if (cur_frag) {
5947 tp->tx_skb[entry].skb = skb;
5948 txd->opts1 |= cpu_to_le32(LastFrag);
5949 }
5950
5951 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005952
5953err_out:
5954 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5955 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956}
5957
françois romieub423e9a2013-05-18 01:24:46 +00005958static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5959{
5960 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5961}
5962
hayeswange9746042014-07-11 16:25:58 +08005963static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5964 struct net_device *dev);
5965/* r8169_csum_workaround()
5966 * The hw limites the value the transport offset. When the offset is out of the
5967 * range, calculate the checksum by sw.
5968 */
5969static void r8169_csum_workaround(struct rtl8169_private *tp,
5970 struct sk_buff *skb)
5971{
5972 if (skb_shinfo(skb)->gso_size) {
5973 netdev_features_t features = tp->dev->features;
5974 struct sk_buff *segs, *nskb;
5975
5976 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
5977 segs = skb_gso_segment(skb, features);
5978 if (IS_ERR(segs) || !segs)
5979 goto drop;
5980
5981 do {
5982 nskb = segs;
5983 segs = segs->next;
5984 nskb->next = NULL;
5985 rtl8169_start_xmit(nskb, tp->dev);
5986 } while (segs);
5987
Alexander Duyckeb781392015-05-01 10:34:44 -07005988 dev_consume_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08005989 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5990 if (skb_checksum_help(skb) < 0)
5991 goto drop;
5992
5993 rtl8169_start_xmit(skb, tp->dev);
5994 } else {
5995 struct net_device_stats *stats;
5996
5997drop:
5998 stats = &tp->dev->stats;
5999 stats->tx_dropped++;
Alexander Duyckeb781392015-05-01 10:34:44 -07006000 dev_kfree_skb_any(skb);
hayeswange9746042014-07-11 16:25:58 +08006001 }
6002}
6003
6004/* msdn_giant_send_check()
6005 * According to the document of microsoft, the TCP Pseudo Header excludes the
6006 * packet length for IPv6 TCP large packets.
6007 */
6008static int msdn_giant_send_check(struct sk_buff *skb)
6009{
6010 const struct ipv6hdr *ipv6h;
6011 struct tcphdr *th;
6012 int ret;
6013
6014 ret = skb_cow_head(skb, 0);
6015 if (ret)
6016 return ret;
6017
6018 ipv6h = ipv6_hdr(skb);
6019 th = tcp_hdr(skb);
6020
6021 th->check = 0;
6022 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
6023
6024 return ret;
6025}
6026
hayeswang5888d3f2014-07-11 16:25:56 +08006027static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
6028 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029{
Michał Mirosław350fb322011-04-08 06:35:56 +00006030 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
Francois Romieu2b7b4312011-04-18 22:53:24 -07006032 if (mss) {
6033 opts[0] |= TD_LSO;
hayeswang5888d3f2014-07-11 16:25:56 +08006034 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
6035 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
6036 const struct iphdr *ip = ip_hdr(skb);
6037
6038 if (ip->protocol == IPPROTO_TCP)
6039 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
6040 else if (ip->protocol == IPPROTO_UDP)
6041 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
6042 else
6043 WARN_ON_ONCE(1);
6044 }
6045
6046 return true;
6047}
6048
6049static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
6050 struct sk_buff *skb, u32 *opts)
6051{
hayeswangbdfa4ed2014-07-11 16:25:57 +08006052 u32 transport_offset = (u32)skb_transport_offset(skb);
hayeswang5888d3f2014-07-11 16:25:56 +08006053 u32 mss = skb_shinfo(skb)->gso_size;
6054
6055 if (mss) {
hayeswange9746042014-07-11 16:25:58 +08006056 if (transport_offset > GTTCPHO_MAX) {
6057 netif_warn(tp, tx_err, tp->dev,
6058 "Invalid transport offset 0x%x for TSO\n",
6059 transport_offset);
6060 return false;
6061 }
6062
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006063 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006064 case htons(ETH_P_IP):
6065 opts[0] |= TD1_GTSENV4;
6066 break;
6067
6068 case htons(ETH_P_IPV6):
6069 if (msdn_giant_send_check(skb))
6070 return false;
6071
6072 opts[0] |= TD1_GTSENV6;
6073 break;
6074
6075 default:
6076 WARN_ON_ONCE(1);
6077 break;
6078 }
6079
hayeswangbdfa4ed2014-07-11 16:25:57 +08006080 opts[0] |= transport_offset << GTTCPHO_SHIFT;
hayeswang5888d3f2014-07-11 16:25:56 +08006081 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006082 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
hayeswange9746042014-07-11 16:25:58 +08006083 u8 ip_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
françois romieub423e9a2013-05-18 01:24:46 +00006085 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006086 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
françois romieub423e9a2013-05-18 01:24:46 +00006087
hayeswange9746042014-07-11 16:25:58 +08006088 if (transport_offset > TCPHO_MAX) {
6089 netif_warn(tp, tx_err, tp->dev,
6090 "Invalid transport offset 0x%x\n",
6091 transport_offset);
6092 return false;
6093 }
6094
Heiner Kallweit4ff36462018-05-02 21:40:02 +02006095 switch (vlan_get_protocol(skb)) {
hayeswange9746042014-07-11 16:25:58 +08006096 case htons(ETH_P_IP):
6097 opts[1] |= TD1_IPv4_CS;
6098 ip_protocol = ip_hdr(skb)->protocol;
6099 break;
6100
6101 case htons(ETH_P_IPV6):
6102 opts[1] |= TD1_IPv6_CS;
6103 ip_protocol = ipv6_hdr(skb)->nexthdr;
6104 break;
6105
6106 default:
6107 ip_protocol = IPPROTO_RAW;
6108 break;
6109 }
6110
6111 if (ip_protocol == IPPROTO_TCP)
6112 opts[1] |= TD1_TCP_CS;
6113 else if (ip_protocol == IPPROTO_UDP)
6114 opts[1] |= TD1_UDP_CS;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006115 else
6116 WARN_ON_ONCE(1);
hayeswange9746042014-07-11 16:25:58 +08006117
6118 opts[1] |= transport_offset << TCPHO_SHIFT;
françois romieub423e9a2013-05-18 01:24:46 +00006119 } else {
6120 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
Alexander Duyck207c5f42014-12-03 08:18:04 -08006121 return !eth_skb_pad(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 }
hayeswang5888d3f2014-07-11 16:25:56 +08006123
françois romieub423e9a2013-05-18 01:24:46 +00006124 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125}
6126
Stephen Hemminger613573252009-08-31 19:50:58 +00006127static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
6128 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129{
6130 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006131 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 struct TxDesc *txd = tp->TxDescArray + entry;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006133 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 dma_addr_t mapping;
6135 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07006136 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006137 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02006138
Julien Ducourthial477206a2012-05-09 00:00:06 +02006139 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006140 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006141 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 }
6143
6144 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006145 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
françois romieub423e9a2013-05-18 01:24:46 +00006147 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
6148 opts[0] = DescOwn;
6149
hayeswange9746042014-07-11 16:25:58 +08006150 if (!tp->tso_csum(tp, skb, opts)) {
6151 r8169_csum_workaround(tp, skb);
6152 return NETDEV_TX_OK;
6153 }
françois romieub423e9a2013-05-18 01:24:46 +00006154
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006155 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006156 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006157 if (unlikely(dma_mapping_error(d, mapping))) {
6158 if (net_ratelimit())
6159 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006160 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00006161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
6163 tp->tx_skb[entry].len = len;
6164 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
Francois Romieu2b7b4312011-04-18 22:53:24 -07006166 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006167 if (frags < 0)
6168 goto err_dma_1;
6169 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07006170 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006171 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07006172 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006173 tp->tx_skb[entry].skb = skb;
6174 }
6175
Francois Romieu2b7b4312011-04-18 22:53:24 -07006176 txd->opts2 = cpu_to_le32(opts[1]);
6177
Richard Cochran5047fb52012-03-10 07:29:42 +00006178 skb_tx_timestamp(skb);
6179
Alexander Duycka0750132014-12-11 15:02:17 -08006180 /* Force memory writes to complete before releasing descriptor */
6181 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
Francois Romieucecb5fd2011-04-01 10:21:07 +02006183 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07006184 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 txd->opts1 = cpu_to_le32(status);
6186
Alexander Duycka0750132014-12-11 15:02:17 -08006187 /* Force all memory writes to complete before notifying device */
David Dillow4c020a92010-03-03 16:33:10 +00006188 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
Alexander Duycka0750132014-12-11 15:02:17 -08006190 tp->cur_tx += frags + 1;
6191
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006192 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
David S. Miller87cda7c2015-02-22 15:54:29 -05006194 mmiowb();
Francois Romieuda78dbf2012-01-26 14:18:23 +01006195
David S. Miller87cda7c2015-02-22 15:54:29 -05006196 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Francois Romieuae1f23f2012-01-31 00:00:19 +01006197 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6198 * not miss a ring update when it notices a stopped queue.
6199 */
6200 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 netif_stop_queue(dev);
Francois Romieuae1f23f2012-01-31 00:00:19 +01006202 /* Sync with rtl_tx:
6203 * - publish queue status and cur_tx ring index (write barrier)
6204 * - refresh dirty_tx ring index (read barrier).
6205 * May the current thread have a pessimistic view of the ring
6206 * status and forget to wake up queue, a racing rtl_tx thread
6207 * can't.
6208 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006209 smp_mb();
Julien Ducourthial477206a2012-05-09 00:00:06 +02006210 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 netif_wake_queue(dev);
6212 }
6213
Stephen Hemminger613573252009-08-31 19:50:58 +00006214 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006216err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006217 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006218err_dma_0:
Eric W. Biederman989c9ba2014-03-11 14:16:14 -07006219 dev_kfree_skb_any(skb);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00006220 dev->stats.tx_dropped++;
6221 return NETDEV_TX_OK;
6222
6223err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006225 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00006226 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227}
6228
6229static void rtl8169_pcierr_interrupt(struct net_device *dev)
6230{
6231 struct rtl8169_private *tp = netdev_priv(dev);
6232 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 u16 pci_status, pci_cmd;
6234
6235 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6236 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6237
Joe Perchesbf82c182010-02-09 11:49:50 +00006238 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6239 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
6241 /*
6242 * The recovery sequence below admits a very elaborated explanation:
6243 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01006244 * - I did not see what else could be done;
6245 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 *
6247 * Feel free to adjust to your needs.
6248 */
Francois Romieua27993f2006-12-18 00:04:19 +01006249 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01006250 pci_cmd &= ~PCI_COMMAND_PARITY;
6251 else
6252 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6253
6254 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
6256 pci_write_config_word(pdev, PCI_STATUS,
6257 pci_status & (PCI_STATUS_DETECTED_PARITY |
6258 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6259 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6260
6261 /* The infamous DAC f*ckup only happens at boot time */
Timo Teräs9fba0812013-01-15 21:01:24 +00006262 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006263 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 tp->cp_cmd &= ~PCIDAC;
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006265 RTL_W16(tp, CPlusCmd, tp->cp_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 }
6268
françois romieue6de30d2011-01-03 15:08:37 +00006269 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01006270
Francois Romieu98ddf982012-01-31 10:47:34 +01006271 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272}
6273
Francois Romieuda78dbf2012-01-26 14:18:23 +01006274static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275{
6276 unsigned int dirty_tx, tx_left;
6277
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 dirty_tx = tp->dirty_tx;
6279 smp_rmb();
6280 tx_left = tp->cur_tx - dirty_tx;
6281
6282 while (tx_left > 0) {
6283 unsigned int entry = dirty_tx % NUM_TX_DESC;
6284 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 u32 status;
6286
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6288 if (status & DescOwn)
6289 break;
6290
Alexander Duycka0750132014-12-11 15:02:17 -08006291 /* This barrier is needed to keep us from reading
6292 * any other fields out of the Tx descriptor until
6293 * we know the status of DescOwn
6294 */
6295 dma_rmb();
6296
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006297 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006298 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 if (status & LastFrag) {
David S. Miller87cda7c2015-02-22 15:54:29 -05006300 u64_stats_update_begin(&tp->tx_stats.syncp);
6301 tp->tx_stats.packets++;
6302 tp->tx_stats.bytes += tx_skb->skb->len;
6303 u64_stats_update_end(&tp->tx_stats.syncp);
Florian Fainelli7a4b813c2017-08-24 18:34:44 -07006304 dev_consume_skb_any(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 tx_skb->skb = NULL;
6306 }
6307 dirty_tx++;
6308 tx_left--;
6309 }
6310
6311 if (tp->dirty_tx != dirty_tx) {
6312 tp->dirty_tx = dirty_tx;
Francois Romieuae1f23f2012-01-31 00:00:19 +01006313 /* Sync with rtl8169_start_xmit:
6314 * - publish dirty_tx ring index (write barrier)
6315 * - refresh cur_tx ring index and queue status (read barrier)
6316 * May the current thread miss the stopped queue condition,
6317 * a racing xmit thread can only have a right view of the
6318 * ring status.
6319 */
Francois Romieu1e874e02012-01-27 15:05:38 +01006320 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 if (netif_queue_stopped(dev) &&
Julien Ducourthial477206a2012-05-09 00:00:06 +02006322 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 netif_wake_queue(dev);
6324 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02006325 /*
6326 * 8168 hack: TxPoll requests are lost when the Tx packets are
6327 * too close. Let's kick an extra TxPoll request when a burst
6328 * of start_xmit activity is detected (if it is not detected,
6329 * it is slow enough). -- FR
6330 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006331 if (tp->cur_tx != dirty_tx)
6332 RTL_W8(tp, TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 }
6334}
6335
Francois Romieu126fa4b2005-05-12 20:09:17 -04006336static inline int rtl8169_fragmented_frame(u32 status)
6337{
6338 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6339}
6340
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006341static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 u32 status = opts1 & RxProtoMask;
6344
6345 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00006346 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 skb->ip_summed = CHECKSUM_UNNECESSARY;
6348 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07006349 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350}
6351
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006352static struct sk_buff *rtl8169_try_rx_copy(void *data,
6353 struct rtl8169_private *tp,
6354 int pkt_size,
6355 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02006357 struct sk_buff *skb;
Heiner Kallweit1e1205b2018-03-20 07:45:42 +01006358 struct device *d = tp_to_dev(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006360 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006361 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006362 prefetch(data);
Alexander Duycke2338f82014-12-09 19:41:09 -08006363 skb = napi_alloc_skb(&tp->napi, pkt_size);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006364 if (skb)
Heiner Kallweit8a67aa82018-04-17 23:19:07 +02006365 skb_copy_to_linear_data(skb, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00006366 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6367
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006368 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369}
6370
Francois Romieuda78dbf2012-01-26 14:18:23 +01006371static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372{
6373 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006374 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 cur_rx = tp->cur_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
Timo Teräs9fba0812013-01-15 21:01:24 +00006378 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006380 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 u32 status;
6382
Heiner Kallweit62028062018-04-17 23:30:29 +02006383 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 if (status & DescOwn)
6385 break;
Alexander Duycka0750132014-12-11 15:02:17 -08006386
6387 /* This barrier is needed to keep us from reading
6388 * any other fields out of the Rx descriptor until
6389 * we know the status of DescOwn
6390 */
6391 dma_rmb();
6392
Richard Dawe4dcb7d32005-05-27 21:12:00 +02006393 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00006394 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6395 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006396 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02006398 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02006400 dev->stats.rx_crc_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006401 /* RxFOVF is a reserved bit on later chip versions */
6402 if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6403 status & RxFOVF) {
Francois Romieuda78dbf2012-01-26 14:18:23 +01006404 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Francois Romieucebf8cc2007-10-18 12:06:54 +02006405 dev->stats.rx_fifo_errors++;
Heiner Kallweit62028062018-04-17 23:30:29 +02006406 } else if (status & (RxRUNT | RxCRC) &&
6407 !(status & RxRWT) &&
6408 dev->features & NETIF_F_RXALL) {
Ben Greear6bbe0212012-02-10 15:04:33 +00006409 goto process_pkt;
Heiner Kallweit62028062018-04-17 23:30:29 +02006410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006412 struct sk_buff *skb;
Ben Greear6bbe0212012-02-10 15:04:33 +00006413 dma_addr_t addr;
6414 int pkt_size;
6415
6416process_pkt:
6417 addr = le64_to_cpu(desc->addr);
Ben Greear79d0c1d2012-02-10 15:04:34 +00006418 if (likely(!(dev->features & NETIF_F_RXFCS)))
6419 pkt_size = (status & 0x00003fff) - 4;
6420 else
6421 pkt_size = status & 0x00003fff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422
Francois Romieu126fa4b2005-05-12 20:09:17 -04006423 /*
6424 * The driver does not support incoming fragmented
6425 * frames. They are seen as a symptom of over-mtu
6426 * sized frames.
6427 */
6428 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02006429 dev->stats.rx_dropped++;
6430 dev->stats.rx_length_errors++;
françois romieuce11ff52013-01-24 13:30:06 +00006431 goto release_descriptor;
Francois Romieu126fa4b2005-05-12 20:09:17 -04006432 }
6433
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006434 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6435 tp, pkt_size, addr);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00006436 if (!skb) {
6437 dev->stats.rx_dropped++;
françois romieuce11ff52013-01-24 13:30:06 +00006438 goto release_descriptor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 }
6440
Eric Dumazetadea1ac72010-09-05 20:04:05 -07006441 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 skb_put(skb, pkt_size);
6443 skb->protocol = eth_type_trans(skb, dev);
6444
Francois Romieu7a8fc772011-03-01 17:18:33 +01006445 rtl8169_rx_vlan_tag(desc, skb);
6446
françois romieu39174292015-11-11 23:35:18 +01006447 if (skb->pkt_type == PACKET_MULTICAST)
6448 dev->stats.multicast++;
6449
Francois Romieu56de4142011-03-15 17:29:31 +01006450 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
Junchang Wang8027aa22012-03-04 23:30:32 +01006452 u64_stats_update_begin(&tp->rx_stats.syncp);
6453 tp->rx_stats.packets++;
6454 tp->rx_stats.bytes += pkt_size;
6455 u64_stats_update_end(&tp->rx_stats.syncp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 }
françois romieuce11ff52013-01-24 13:30:06 +00006457release_descriptor:
6458 desc->opts2 = 0;
Heiner Kallweit1d0254d2018-04-17 23:25:46 +02006459 rtl8169_mark_to_asic(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 }
6461
6462 count = cur_rx - tp->cur_rx;
6463 tp->cur_rx = cur_rx;
6464
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 return count;
6466}
6467
Francois Romieu07d3f512007-02-21 22:40:46 +01006468static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469{
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006470 struct rtl8169_private *tp = dev_instance;
Heiner Kallweit05bbe552018-08-10 22:38:29 +02006471 u16 status = rtl_get_events(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472
Heiner Kallweit05bbe552018-08-10 22:38:29 +02006473 if (status == 0xffff || !(status & (RTL_EVENT_NAPI | tp->event_slow)))
6474 return IRQ_NONE;
françois romieu811fd302011-12-04 20:30:45 +00006475
Heiner Kallweit05bbe552018-08-10 22:38:29 +02006476 rtl_irq_disable(tp);
6477 napi_schedule_irqoff(&tp->napi);
6478
6479 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480}
6481
Francois Romieuda78dbf2012-01-26 14:18:23 +01006482/*
6483 * Workqueue context.
6484 */
6485static void rtl_slow_event_work(struct rtl8169_private *tp)
6486{
6487 struct net_device *dev = tp->dev;
6488 u16 status;
6489
6490 status = rtl_get_events(tp) & tp->event_slow;
6491 rtl_ack_events(tp, status);
6492
6493 if (unlikely(status & RxFIFOOver)) {
6494 switch (tp->mac_version) {
6495 /* Work around for rx fifo overflow */
6496 case RTL_GIGA_MAC_VER_11:
6497 netif_stop_queue(dev);
Francois Romieu934714d2012-01-31 11:09:21 +01006498 /* XXX - Hack alert. See rtl_task(). */
6499 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006500 default:
6501 break;
6502 }
6503 }
6504
6505 if (unlikely(status & SYSErr))
6506 rtl8169_pcierr_interrupt(dev);
6507
6508 if (status & LinkChg)
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006509 phy_mac_interrupt(dev->phydev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006510
françois romieu7dbb4912012-06-09 10:53:16 +00006511 rtl_irq_enable_all(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006512}
6513
Francois Romieu4422bcd2012-01-26 11:23:32 +01006514static void rtl_task(struct work_struct *work)
6515{
Francois Romieuda78dbf2012-01-26 14:18:23 +01006516 static const struct {
6517 int bitnr;
6518 void (*action)(struct rtl8169_private *);
6519 } rtl_work[] = {
Francois Romieu934714d2012-01-31 11:09:21 +01006520 /* XXX - keep rtl_slow_event_work() as first element. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006521 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
6522 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
Francois Romieuda78dbf2012-01-26 14:18:23 +01006523 };
Francois Romieu4422bcd2012-01-26 11:23:32 +01006524 struct rtl8169_private *tp =
6525 container_of(work, struct rtl8169_private, wk.work);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006526 struct net_device *dev = tp->dev;
6527 int i;
Francois Romieu4422bcd2012-01-26 11:23:32 +01006528
Francois Romieuda78dbf2012-01-26 14:18:23 +01006529 rtl_lock_work(tp);
6530
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006531 if (!netif_running(dev) ||
6532 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
Francois Romieuda78dbf2012-01-26 14:18:23 +01006533 goto out_unlock;
6534
6535 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6536 bool pending;
6537
Francois Romieuda78dbf2012-01-26 14:18:23 +01006538 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006539 if (pending)
6540 rtl_work[i].action(tp);
6541 }
6542
6543out_unlock:
6544 rtl_unlock_work(tp);
Francois Romieu4422bcd2012-01-26 11:23:32 +01006545}
6546
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006547static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006549 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6550 struct net_device *dev = tp->dev;
Francois Romieuda78dbf2012-01-26 14:18:23 +01006551 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
6552 int work_done= 0;
6553 u16 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
Francois Romieuda78dbf2012-01-26 14:18:23 +01006555 status = rtl_get_events(tp);
6556 rtl_ack_events(tp, status & ~tp->event_slow);
6557
6558 if (status & RTL_EVENT_NAPI_RX)
6559 work_done = rtl_rx(dev, tp, (u32) budget);
6560
6561 if (status & RTL_EVENT_NAPI_TX)
6562 rtl_tx(dev, tp);
6563
6564 if (status & tp->event_slow) {
6565 enable_mask &= ~tp->event_slow;
6566
6567 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
6568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006570 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08006571 napi_complete_done(napi, work_done);
David Dillowf11a3772009-05-22 15:29:34 +00006572
Francois Romieuda78dbf2012-01-26 14:18:23 +01006573 rtl_irq_enable(tp, enable_mask);
6574 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 }
6576
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006577 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006580static void rtl8169_rx_missed(struct net_device *dev)
Francois Romieu523a6092008-09-10 22:28:56 +02006581{
6582 struct rtl8169_private *tp = netdev_priv(dev);
6583
6584 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6585 return;
6586
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006587 dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6588 RTL_W32(tp, RxMissed, 0);
Francois Romieu523a6092008-09-10 22:28:56 +02006589}
6590
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006591static void r8169_phylink_handler(struct net_device *ndev)
6592{
6593 struct rtl8169_private *tp = netdev_priv(ndev);
6594
6595 if (netif_carrier_ok(ndev)) {
6596 rtl_link_chg_patch(tp);
6597 pm_request_resume(&tp->pci_dev->dev);
6598 } else {
6599 pm_runtime_idle(&tp->pci_dev->dev);
6600 }
6601
6602 if (net_ratelimit())
6603 phy_print_status(ndev->phydev);
6604}
6605
6606static int r8169_phy_connect(struct rtl8169_private *tp)
6607{
6608 struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
6609 phy_interface_t phy_mode;
6610 int ret;
6611
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02006612 phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006613 PHY_INTERFACE_MODE_MII;
6614
6615 ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6616 phy_mode);
6617 if (ret)
6618 return ret;
6619
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02006620 if (!tp->supports_gmii)
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006621 phy_set_max_speed(phydev, SPEED_100);
6622
6623 /* Ensure to advertise everything, incl. pause */
6624 phydev->advertising = phydev->supported;
6625
6626 phy_attached_info(phydev);
6627
6628 return 0;
6629}
6630
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631static void rtl8169_down(struct net_device *dev)
6632{
6633 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006635 phy_stop(dev->phydev);
6636
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01006637 napi_disable(&tp->napi);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006638 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
Hayes Wang92fc43b2011-07-06 15:58:03 +08006640 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006641 /*
6642 * At this point device interrupts can not be enabled in any function,
Francois Romieu209e5ac2012-01-26 09:59:50 +01006643 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6644 * and napi is disabled (rtl8169_poll).
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00006645 */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006646 rtl8169_rx_missed(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 /* Give a racing hard_start_xmit a few cycles to complete. */
Francois Romieuda78dbf2012-01-26 14:18:23 +01006649 synchronize_sched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 rtl8169_tx_clear(tp);
6652
6653 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00006654
6655 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656}
6657
6658static int rtl8169_close(struct net_device *dev)
6659{
6660 struct rtl8169_private *tp = netdev_priv(dev);
6661 struct pci_dev *pdev = tp->pci_dev;
6662
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006663 pm_runtime_get_sync(&pdev->dev);
6664
Francois Romieucecb5fd2011-04-01 10:21:07 +02006665 /* Update counters before going down */
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006666 rtl8169_update_counters(tp);
Ivan Vecera355423d2009-02-06 21:49:57 -08006667
Francois Romieuda78dbf2012-01-26 14:18:23 +01006668 rtl_lock_work(tp);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006669 /* Clear all task flags */
6670 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006671
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 rtl8169_down(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006673 rtl_unlock_work(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
Lekensteyn4ea72442013-07-22 09:53:30 +02006675 cancel_work_sync(&tp->wk.work);
6676
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006677 phy_disconnect(dev->phydev);
6678
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006679 pci_free_irq(pdev, 0, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00006681 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6682 tp->RxPhyAddr);
6683 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6684 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 tp->TxDescArray = NULL;
6686 tp->RxDescArray = NULL;
6687
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006688 pm_runtime_put_sync(&pdev->dev);
6689
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 return 0;
6691}
6692
Francois Romieudc1c00c2012-03-08 10:06:18 +01006693#ifdef CONFIG_NET_POLL_CONTROLLER
6694static void rtl8169_netpoll(struct net_device *dev)
6695{
6696 struct rtl8169_private *tp = netdev_priv(dev);
6697
Ville Syrjälä6d8b8342018-06-20 15:01:53 +03006698 rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
Francois Romieudc1c00c2012-03-08 10:06:18 +01006699}
6700#endif
6701
Francois Romieudf43ac72012-03-08 09:48:40 +01006702static int rtl_open(struct net_device *dev)
6703{
6704 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006705 struct pci_dev *pdev = tp->pci_dev;
6706 int retval = -ENOMEM;
6707
6708 pm_runtime_get_sync(&pdev->dev);
6709
6710 /*
Jiri Kosinae75d6602012-04-08 21:48:52 +02006711 * Rx and Tx descriptors needs 256 bytes alignment.
Francois Romieudf43ac72012-03-08 09:48:40 +01006712 * dma_alloc_coherent provides more.
6713 */
6714 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6715 &tp->TxPhyAddr, GFP_KERNEL);
6716 if (!tp->TxDescArray)
6717 goto err_pm_runtime_put;
6718
6719 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6720 &tp->RxPhyAddr, GFP_KERNEL);
6721 if (!tp->RxDescArray)
6722 goto err_free_tx_0;
6723
Heiner Kallweitb1127e62018-04-17 23:23:35 +02006724 retval = rtl8169_init_ring(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006725 if (retval < 0)
6726 goto err_free_rx_1;
6727
6728 INIT_WORK(&tp->wk.work, rtl_task);
6729
6730 smp_mb();
6731
6732 rtl_request_firmware(tp);
6733
Heiner Kallweitebcd5da2018-04-17 23:29:20 +02006734 retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01006735 dev->name);
Francois Romieudf43ac72012-03-08 09:48:40 +01006736 if (retval < 0)
6737 goto err_release_fw_2;
6738
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006739 retval = r8169_phy_connect(tp);
6740 if (retval)
6741 goto err_free_irq;
6742
Francois Romieudf43ac72012-03-08 09:48:40 +01006743 rtl_lock_work(tp);
6744
6745 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6746
6747 napi_enable(&tp->napi);
6748
6749 rtl8169_init_phy(dev, tp);
6750
Francois Romieudf43ac72012-03-08 09:48:40 +01006751 rtl_pll_power_up(tp);
6752
Heiner Kallweit61cb5322018-04-17 23:27:38 +02006753 rtl_hw_start(tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006754
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006755 if (!rtl8169_init_counter_offsets(tp))
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006756 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6757
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006758 phy_start(dev->phydev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006759 netif_start_queue(dev);
6760
6761 rtl_unlock_work(tp);
6762
Heiner Kallweita92a0842018-01-08 21:39:13 +01006763 pm_runtime_put_sync(&pdev->dev);
Francois Romieudf43ac72012-03-08 09:48:40 +01006764out:
6765 return retval;
6766
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006767err_free_irq:
6768 pci_free_irq(pdev, 0, tp);
Francois Romieudf43ac72012-03-08 09:48:40 +01006769err_release_fw_2:
6770 rtl_release_firmware(tp);
6771 rtl8169_rx_clear(tp);
6772err_free_rx_1:
6773 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6774 tp->RxPhyAddr);
6775 tp->RxDescArray = NULL;
6776err_free_tx_0:
6777 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6778 tp->TxPhyAddr);
6779 tp->TxDescArray = NULL;
6780err_pm_runtime_put:
6781 pm_runtime_put_noidle(&pdev->dev);
6782 goto out;
6783}
6784
stephen hemmingerbc1f4472017-01-06 19:12:52 -08006785static void
Junchang Wang8027aa22012-03-04 23:30:32 +01006786rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787{
6788 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006789 struct pci_dev *pdev = tp->pci_dev;
Corinna Vinschen42020322015-09-10 10:47:35 +02006790 struct rtl8169_counters *counters = tp->counters;
Junchang Wang8027aa22012-03-04 23:30:32 +01006791 unsigned int start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006793 pm_runtime_get_noresume(&pdev->dev);
6794
6795 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006796 rtl8169_rx_missed(dev);
Francois Romieu5b0384f2006-08-16 16:00:01 +02006797
Junchang Wang8027aa22012-03-04 23:30:32 +01006798 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006799 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006800 stats->rx_packets = tp->rx_stats.packets;
6801 stats->rx_bytes = tp->rx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006802 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006803
Junchang Wang8027aa22012-03-04 23:30:32 +01006804 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -07006805 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
Junchang Wang8027aa22012-03-04 23:30:32 +01006806 stats->tx_packets = tp->tx_stats.packets;
6807 stats->tx_bytes = tp->tx_stats.bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -07006808 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
Junchang Wang8027aa22012-03-04 23:30:32 +01006809
6810 stats->rx_dropped = dev->stats.rx_dropped;
6811 stats->tx_dropped = dev->stats.tx_dropped;
6812 stats->rx_length_errors = dev->stats.rx_length_errors;
6813 stats->rx_errors = dev->stats.rx_errors;
6814 stats->rx_crc_errors = dev->stats.rx_crc_errors;
6815 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
6816 stats->rx_missed_errors = dev->stats.rx_missed_errors;
Corinna Vinschend7d2d892015-08-27 17:11:48 +02006817 stats->multicast = dev->stats.multicast;
Junchang Wang8027aa22012-03-04 23:30:32 +01006818
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006819 /*
6820 * Fetch additonal counter values missing in stats collected by driver
6821 * from tally counters.
6822 */
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006823 if (pm_runtime_active(&pdev->dev))
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006824 rtl8169_update_counters(tp);
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006825
6826 /*
6827 * Subtract values fetched during initalization.
6828 * See rtl8169_init_counter_offsets for a description why we do that.
6829 */
Corinna Vinschen42020322015-09-10 10:47:35 +02006830 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006831 le64_to_cpu(tp->tc_offset.tx_errors);
Corinna Vinschen42020322015-09-10 10:47:35 +02006832 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006833 le32_to_cpu(tp->tc_offset.tx_multi_collision);
Corinna Vinschen42020322015-09-10 10:47:35 +02006834 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
Corinna Vinschen6e85d5a2015-08-24 12:52:39 +02006835 le16_to_cpu(tp->tc_offset.tx_aborted);
6836
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006837 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838}
6839
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006840static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01006841{
françois romieu065c27c2011-01-03 15:08:12 +00006842 struct rtl8169_private *tp = netdev_priv(dev);
6843
Francois Romieu5d06a992006-02-23 00:47:58 +01006844 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006845 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01006846
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006847 phy_stop(dev->phydev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006848 netif_device_detach(dev);
6849 netif_stop_queue(dev);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006850
6851 rtl_lock_work(tp);
6852 napi_disable(&tp->napi);
Kai-Heng Feng6ad56902018-09-11 01:51:43 +08006853 /* Clear all task flags */
6854 bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6855
Francois Romieuda78dbf2012-01-26 14:18:23 +01006856 rtl_unlock_work(tp);
6857
6858 rtl_pll_power_down(tp);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006859}
Francois Romieu5d06a992006-02-23 00:47:58 +01006860
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006861#ifdef CONFIG_PM
6862
6863static int rtl8169_suspend(struct device *device)
6864{
6865 struct pci_dev *pdev = to_pci_dev(device);
6866 struct net_device *dev = pci_get_drvdata(pdev);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006867 struct rtl8169_private *tp = netdev_priv(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006868
6869 rtl8169_net_suspend(dev);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006870 clk_disable_unprepare(tp->clk);
Francois Romieu1371fa62007-04-02 23:01:11 +02006871
Francois Romieu5d06a992006-02-23 00:47:58 +01006872 return 0;
6873}
6874
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006875static void __rtl8169_resume(struct net_device *dev)
6876{
françois romieu065c27c2011-01-03 15:08:12 +00006877 struct rtl8169_private *tp = netdev_priv(dev);
6878
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006879 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00006880
6881 rtl_pll_power_up(tp);
Heiner Kallweit92bad852018-06-24 18:37:36 +02006882 rtl8169_init_phy(dev, tp);
françois romieu065c27c2011-01-03 15:08:12 +00006883
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02006884 phy_start(tp->dev->phydev);
6885
Artem Savkovcff4c162012-04-03 10:29:11 +00006886 rtl_lock_work(tp);
6887 napi_enable(&tp->napi);
Francois Romieu6c4a70c2012-01-31 10:56:44 +01006888 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
Artem Savkovcff4c162012-04-03 10:29:11 +00006889 rtl_unlock_work(tp);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006890
Francois Romieu98ddf982012-01-31 10:47:34 +01006891 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006892}
6893
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006894static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01006895{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006896 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01006897 struct net_device *dev = pci_get_drvdata(pdev);
Hans de Goedeac8bd9e2018-09-26 22:12:39 +02006898 struct rtl8169_private *tp = netdev_priv(dev);
6899
6900 clk_prepare_enable(tp->clk);
Francois Romieu5d06a992006-02-23 00:47:58 +01006901
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006902 if (netif_running(dev))
6903 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01006904
Francois Romieu5d06a992006-02-23 00:47:58 +01006905 return 0;
6906}
6907
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006908static int rtl8169_runtime_suspend(struct device *device)
6909{
6910 struct pci_dev *pdev = to_pci_dev(device);
6911 struct net_device *dev = pci_get_drvdata(pdev);
6912 struct rtl8169_private *tp = netdev_priv(dev);
6913
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02006914 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006915 return 0;
6916
Francois Romieuda78dbf2012-01-26 14:18:23 +01006917 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006918 __rtl8169_set_wol(tp, WAKE_ANY);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006919 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006920
6921 rtl8169_net_suspend(dev);
6922
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006923 /* Update counters before going runtime suspend */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006924 rtl8169_rx_missed(dev);
Heiner Kallweite71c9ce2018-04-17 23:28:28 +02006925 rtl8169_update_counters(tp);
Chun-Hao Linf09cf4b2016-02-24 14:18:42 +08006926
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006927 return 0;
6928}
6929
6930static int rtl8169_runtime_resume(struct device *device)
6931{
6932 struct pci_dev *pdev = to_pci_dev(device);
6933 struct net_device *dev = pci_get_drvdata(pdev);
6934 struct rtl8169_private *tp = netdev_priv(dev);
Chun-Hao Linf51d4a12016-07-29 16:37:56 +08006935 rtl_rar_set(tp, dev->dev_addr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006936
6937 if (!tp->TxDescArray)
6938 return 0;
6939
Francois Romieuda78dbf2012-01-26 14:18:23 +01006940 rtl_lock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006941 __rtl8169_set_wol(tp, tp->saved_wolopts);
Francois Romieuda78dbf2012-01-26 14:18:23 +01006942 rtl_unlock_work(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006943
6944 __rtl8169_resume(dev);
6945
6946 return 0;
6947}
6948
6949static int rtl8169_runtime_idle(struct device *device)
6950{
6951 struct pci_dev *pdev = to_pci_dev(device);
6952 struct net_device *dev = pci_get_drvdata(pdev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006953
Heiner Kallweita92a0842018-01-08 21:39:13 +01006954 if (!netif_running(dev) || !netif_carrier_ok(dev))
6955 pm_schedule_suspend(device, 10000);
6956
6957 return -EBUSY;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00006958}
6959
Alexey Dobriyan47145212009-12-14 18:00:08 -08006960static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02006961 .suspend = rtl8169_suspend,
6962 .resume = rtl8169_resume,
6963 .freeze = rtl8169_suspend,
6964 .thaw = rtl8169_resume,
6965 .poweroff = rtl8169_suspend,
6966 .restore = rtl8169_resume,
6967 .runtime_suspend = rtl8169_runtime_suspend,
6968 .runtime_resume = rtl8169_runtime_resume,
6969 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00006970};
6971
6972#define RTL8169_PM_OPS (&rtl8169_pm_ops)
6973
6974#else /* !CONFIG_PM */
6975
6976#define RTL8169_PM_OPS NULL
6977
6978#endif /* !CONFIG_PM */
6979
David S. Miller1805b2f2011-10-24 18:18:09 -04006980static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6981{
David S. Miller1805b2f2011-10-24 18:18:09 -04006982 /* WoL fails with 8168b when the receiver is disabled. */
6983 switch (tp->mac_version) {
6984 case RTL_GIGA_MAC_VER_11:
6985 case RTL_GIGA_MAC_VER_12:
6986 case RTL_GIGA_MAC_VER_17:
6987 pci_clear_master(tp->pci_dev);
6988
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006989 RTL_W8(tp, ChipCmd, CmdRxEnb);
David S. Miller1805b2f2011-10-24 18:18:09 -04006990 /* PCI commit */
Andy Shevchenko1ef72862018-03-01 13:27:34 +02006991 RTL_R8(tp, ChipCmd);
David S. Miller1805b2f2011-10-24 18:18:09 -04006992 break;
6993 default:
6994 break;
6995 }
6996}
6997
Francois Romieu1765f952008-09-13 17:21:40 +02006998static void rtl_shutdown(struct pci_dev *pdev)
6999{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007000 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00007001 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu1765f952008-09-13 17:21:40 +02007002
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007003 rtl8169_net_suspend(dev);
7004
Francois Romieucecb5fd2011-04-01 10:21:07 +02007005 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08007006 rtl_rar_set(tp, dev->perm_addr);
7007
Hayes Wang92fc43b2011-07-06 15:58:03 +08007008 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00007009
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007010 if (system_state == SYSTEM_POWER_OFF) {
Heiner Kallweit433f9d02018-06-24 18:39:06 +02007011 if (tp->saved_wolopts) {
David S. Miller1805b2f2011-10-24 18:18:09 -04007012 rtl_wol_suspend_quirk(tp);
7013 rtl_wol_shutdown_quirk(tp);
françois romieuca52efd2009-07-24 12:34:19 +00007014 }
7015
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007016 pci_wake_from_d3(pdev, true);
7017 pci_set_power_state(pdev, PCI_D3hot);
7018 }
7019}
Francois Romieu5d06a992006-02-23 00:47:58 +01007020
Bill Pembertonbaf63292012-12-03 09:23:28 -05007021static void rtl_remove_one(struct pci_dev *pdev)
Francois Romieue27566e2012-03-08 09:54:01 +01007022{
7023 struct net_device *dev = pci_get_drvdata(pdev);
7024 struct rtl8169_private *tp = netdev_priv(dev);
7025
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007026 if (r8168_check_dash(tp))
Francois Romieue27566e2012-03-08 09:54:01 +01007027 rtl8168_driver_stop(tp);
Francois Romieue27566e2012-03-08 09:54:01 +01007028
Devendra Nagaad1be8d2012-05-31 01:51:20 +00007029 netif_napi_del(&tp->napi);
7030
Francois Romieue27566e2012-03-08 09:54:01 +01007031 unregister_netdev(dev);
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007032 mdiobus_unregister(tp->mii_bus);
Francois Romieue27566e2012-03-08 09:54:01 +01007033
7034 rtl_release_firmware(tp);
7035
7036 if (pci_dev_run_wake(pdev))
7037 pm_runtime_get_noresume(&pdev->dev);
7038
7039 /* restore original MAC address */
7040 rtl_rar_set(tp, dev->perm_addr);
Francois Romieue27566e2012-03-08 09:54:01 +01007041}
7042
Francois Romieufa9c3852012-03-08 10:01:50 +01007043static const struct net_device_ops rtl_netdev_ops = {
Francois Romieudf43ac72012-03-08 09:48:40 +01007044 .ndo_open = rtl_open,
Francois Romieufa9c3852012-03-08 10:01:50 +01007045 .ndo_stop = rtl8169_close,
7046 .ndo_get_stats64 = rtl8169_get_stats64,
7047 .ndo_start_xmit = rtl8169_start_xmit,
7048 .ndo_tx_timeout = rtl8169_tx_timeout,
7049 .ndo_validate_addr = eth_validate_addr,
7050 .ndo_change_mtu = rtl8169_change_mtu,
7051 .ndo_fix_features = rtl8169_fix_features,
7052 .ndo_set_features = rtl8169_set_features,
7053 .ndo_set_mac_address = rtl_set_mac_address,
7054 .ndo_do_ioctl = rtl8169_ioctl,
7055 .ndo_set_rx_mode = rtl_set_rx_mode,
7056#ifdef CONFIG_NET_POLL_CONTROLLER
7057 .ndo_poll_controller = rtl8169_netpoll,
7058#endif
7059
7060};
7061
Francois Romieu31fa8b12012-03-08 10:09:40 +01007062static const struct rtl_cfg_info {
Heiner Kallweit61cb5322018-04-17 23:27:38 +02007063 void (*hw_start)(struct rtl8169_private *tp);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007064 u16 event_slow;
Heiner Kallweit14967f92018-02-28 07:55:20 +01007065 unsigned int has_gmii:1;
Francois Romieu50970832017-10-27 13:24:49 +03007066 const struct rtl_coalesce_info *coalesce_info;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007067 u8 default_ver;
7068} rtl_cfg_infos [] = {
7069 [RTL_CFG_0] = {
7070 .hw_start = rtl_hw_start_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007071 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007072 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007073 .coalesce_info = rtl_coalesce_info_8169,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007074 .default_ver = RTL_GIGA_MAC_VER_01,
7075 },
7076 [RTL_CFG_1] = {
7077 .hw_start = rtl_hw_start_8168,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007078 .event_slow = SYSErr | LinkChg | RxOverflow,
Heiner Kallweit14967f92018-02-28 07:55:20 +01007079 .has_gmii = 1,
Francois Romieu50970832017-10-27 13:24:49 +03007080 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007081 .default_ver = RTL_GIGA_MAC_VER_11,
7082 },
7083 [RTL_CFG_2] = {
7084 .hw_start = rtl_hw_start_8101,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007085 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
7086 PCSTimeout,
Francois Romieu50970832017-10-27 13:24:49 +03007087 .coalesce_info = rtl_coalesce_info_8168_8136,
Francois Romieu31fa8b12012-03-08 10:09:40 +01007088 .default_ver = RTL_GIGA_MAC_VER_13,
7089 }
7090};
7091
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007092static int rtl_alloc_irq(struct rtl8169_private *tp)
Francois Romieu31fa8b12012-03-08 10:09:40 +01007093{
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007094 unsigned int flags;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007095
Jian-Hong Pan7bb05b82018-08-17 13:07:35 +08007096 switch (tp->mac_version) {
7097 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007098 RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
7099 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
7100 RTL_W8(tp, Cfg9346, Cfg9346_Lock);
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007101 flags = PCI_IRQ_LEGACY;
Jian-Hong Pan7bb05b82018-08-17 13:07:35 +08007102 break;
7103 case RTL_GIGA_MAC_VER_39 ... RTL_GIGA_MAC_VER_40:
Heiner Kallweit7c53a722018-08-12 13:26:26 +02007104 /* This version was reported to have issues with resume
7105 * from suspend when using MSI-X
7106 */
7107 flags = PCI_IRQ_LEGACY | PCI_IRQ_MSI;
Jian-Hong Pan7bb05b82018-08-17 13:07:35 +08007108 break;
7109 default:
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007110 flags = PCI_IRQ_ALL_TYPES;
Francois Romieu31fa8b12012-03-08 10:09:40 +01007111 }
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007112
7113 return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
Francois Romieu31fa8b12012-03-08 10:09:40 +01007114}
7115
Hayes Wangc5583862012-07-02 17:23:22 +08007116DECLARE_RTL_COND(rtl_link_list_ready_cond)
7117{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007118 return RTL_R8(tp, MCU) & LINK_LIST_RDY;
Hayes Wangc5583862012-07-02 17:23:22 +08007119}
7120
7121DECLARE_RTL_COND(rtl_rxtx_empty_cond)
7122{
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007123 return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
Hayes Wangc5583862012-07-02 17:23:22 +08007124}
7125
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007126static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
7127{
7128 struct rtl8169_private *tp = mii_bus->priv;
7129
7130 if (phyaddr > 0)
7131 return -ENODEV;
7132
7133 return rtl_readphy(tp, phyreg);
7134}
7135
7136static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
7137 int phyreg, u16 val)
7138{
7139 struct rtl8169_private *tp = mii_bus->priv;
7140
7141 if (phyaddr > 0)
7142 return -ENODEV;
7143
7144 rtl_writephy(tp, phyreg, val);
7145
7146 return 0;
7147}
7148
7149static int r8169_mdio_register(struct rtl8169_private *tp)
7150{
7151 struct pci_dev *pdev = tp->pci_dev;
7152 struct phy_device *phydev;
7153 struct mii_bus *new_bus;
7154 int ret;
7155
7156 new_bus = devm_mdiobus_alloc(&pdev->dev);
7157 if (!new_bus)
7158 return -ENOMEM;
7159
7160 new_bus->name = "r8169";
7161 new_bus->priv = tp;
7162 new_bus->parent = &pdev->dev;
7163 new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
7164 snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
7165 PCI_DEVID(pdev->bus->number, pdev->devfn));
7166
7167 new_bus->read = r8169_mdio_read_reg;
7168 new_bus->write = r8169_mdio_write_reg;
7169
7170 ret = mdiobus_register(new_bus);
7171 if (ret)
7172 return ret;
7173
7174 phydev = mdiobus_get_phy(new_bus, 0);
7175 if (!phydev) {
7176 mdiobus_unregister(new_bus);
7177 return -ENODEV;
7178 }
7179
Heiner Kallweit242cd9b2018-07-17 22:51:33 +02007180 /* PHY will be woken up in rtl_open() */
7181 phy_suspend(phydev);
7182
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007183 tp->mii_bus = new_bus;
7184
7185 return 0;
7186}
7187
Bill Pembertonbaf63292012-12-03 09:23:28 -05007188static void rtl_hw_init_8168g(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007189{
Hayes Wangc5583862012-07-02 17:23:22 +08007190 u32 data;
7191
7192 tp->ocp_base = OCP_STD_PHY_BASE;
7193
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007194 RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
Hayes Wangc5583862012-07-02 17:23:22 +08007195
7196 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
7197 return;
7198
7199 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
7200 return;
7201
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007202 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
Hayes Wangc5583862012-07-02 17:23:22 +08007203 msleep(1);
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007204 RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
Hayes Wangc5583862012-07-02 17:23:22 +08007205
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007206 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007207 data &= ~(1 << 14);
7208 r8168_mac_ocp_write(tp, 0xe8de, data);
7209
7210 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7211 return;
7212
Hayes Wang5f8bcce2012-07-10 08:47:05 +02007213 data = r8168_mac_ocp_read(tp, 0xe8de);
Hayes Wangc5583862012-07-02 17:23:22 +08007214 data |= (1 << 15);
7215 r8168_mac_ocp_write(tp, 0xe8de, data);
7216
7217 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
7218 return;
7219}
7220
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007221static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
7222{
7223 rtl8168ep_stop_cmac(tp);
7224 rtl_hw_init_8168g(tp);
7225}
7226
Bill Pembertonbaf63292012-12-03 09:23:28 -05007227static void rtl_hw_initialize(struct rtl8169_private *tp)
Hayes Wangc5583862012-07-02 17:23:22 +08007228{
7229 switch (tp->mac_version) {
Heiner Kallweit2a718832018-05-02 21:39:49 +02007230 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007231 rtl_hw_init_8168g(tp);
7232 break;
Heiner Kallweit2a718832018-05-02 21:39:49 +02007233 case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin003609d2014-12-02 16:48:31 +08007234 rtl_hw_init_8168ep(tp);
Hayes Wangc5583862012-07-02 17:23:22 +08007235 break;
Hayes Wangc5583862012-07-02 17:23:22 +08007236 default:
7237 break;
7238 }
7239}
7240
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007241/* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
7242static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
7243{
7244 switch (tp->mac_version) {
7245 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7246 case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
7247 return false;
7248 default:
7249 return true;
7250 }
7251}
7252
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007253static int rtl_jumbo_max(struct rtl8169_private *tp)
7254{
7255 /* Non-GBit versions don't support jumbo frames */
7256 if (!tp->supports_gmii)
7257 return JUMBO_1K;
7258
7259 switch (tp->mac_version) {
7260 /* RTL8169 */
7261 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7262 return JUMBO_7K;
7263 /* RTL8168b */
7264 case RTL_GIGA_MAC_VER_11:
7265 case RTL_GIGA_MAC_VER_12:
7266 case RTL_GIGA_MAC_VER_17:
7267 return JUMBO_4K;
7268 /* RTL8168c */
7269 case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
7270 return JUMBO_6K;
7271 default:
7272 return JUMBO_9K;
7273 }
7274}
7275
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02007276static void rtl_disable_clk(void *data)
7277{
7278 clk_disable_unprepare(data);
7279}
7280
hayeswang929a0312014-09-16 11:40:47 +08007281static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Francois Romieu3b6cf252012-03-08 09:59:04 +01007282{
7283 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007284 struct rtl8169_private *tp;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007285 struct net_device *dev;
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007286 int chipset, region, i;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007287 int jumbo_max, rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007288
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007289 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7290 if (!dev)
7291 return -ENOMEM;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007292
7293 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieufa9c3852012-03-08 10:01:50 +01007294 dev->netdev_ops = &rtl_netdev_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007295 tp = netdev_priv(dev);
7296 tp->dev = dev;
7297 tp->pci_dev = pdev;
7298 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Heiner Kallweitf7ffa9a2018-07-17 22:52:09 +02007299 tp->supports_gmii = cfg->has_gmii;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007300
Hans de Goedec2f6f3e2018-09-12 11:34:55 +02007301 /* Get the *optional* external "ether_clk" used on some boards */
7302 tp->clk = devm_clk_get(&pdev->dev, "ether_clk");
7303 if (IS_ERR(tp->clk)) {
7304 rc = PTR_ERR(tp->clk);
7305 if (rc == -ENOENT) {
7306 /* clk-core allows NULL (for suspend / resume) */
7307 tp->clk = NULL;
7308 } else if (rc == -EPROBE_DEFER) {
7309 return rc;
7310 } else {
7311 dev_err(&pdev->dev, "failed to get clk: %d\n", rc);
7312 return rc;
7313 }
7314 } else {
7315 rc = clk_prepare_enable(tp->clk);
7316 if (rc) {
7317 dev_err(&pdev->dev, "failed to enable clk: %d\n", rc);
7318 return rc;
7319 }
7320
7321 rc = devm_add_action_or_reset(&pdev->dev, rtl_disable_clk,
7322 tp->clk);
7323 if (rc)
7324 return rc;
7325 }
7326
Francois Romieu3b6cf252012-03-08 09:59:04 +01007327 /* enable device (incl. PCI PM wakeup and hotplug setup) */
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007328 rc = pcim_enable_device(pdev);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007329 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007330 dev_err(&pdev->dev, "enable failure\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007331 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007332 }
7333
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007334 if (pcim_set_mwi(pdev) < 0)
Heiner Kallweit22148df2018-04-22 17:15:15 +02007335 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007336
Heiner Kallweitc8d48d92018-04-17 23:34:22 +02007337 /* use first MMIO region */
7338 region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7339 if (region < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007340 dev_err(&pdev->dev, "no MMIO resource found\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007341 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007342 }
7343
7344 /* check for weird/broken PCI region reporting */
7345 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007346 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007347 return -ENODEV;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007348 }
7349
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007350 rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007351 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007352 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007353 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007354 }
7355
Andy Shevchenko93a00d42018-03-01 13:27:35 +02007356 tp->mmio_addr = pcim_iomap_table(pdev)[region];
Francois Romieu3b6cf252012-03-08 09:59:04 +01007357
7358 if (!pci_is_pcie(pdev))
Heiner Kallweit22148df2018-04-22 17:15:15 +02007359 dev_info(&pdev->dev, "not PCI Express\n");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007360
7361 /* Identify chip attached to board */
Heiner Kallweit22148df2018-04-22 17:15:15 +02007362 rtl8169_get_mac_version(tp, cfg->default_ver);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007363
Heiner Kallweite3972862018-06-29 08:07:04 +02007364 if (rtl_tbi_enabled(tp)) {
7365 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7366 return -ENODEV;
7367 }
7368
Heiner Kallweit0ae09742018-04-28 22:19:26 +02007369 tp->cp_cmd = RTL_R16(tp, CPlusCmd);
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007370
7371 if ((sizeof(dma_addr_t) > 4) &&
7372 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
7373 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
Ard Biesheuvelf0076432016-10-14 14:40:33 +01007374 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
7375 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007376
7377 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
7378 if (!pci_is_pcie(pdev))
7379 tp->cp_cmd |= PCIDAC;
7380 dev->features |= NETIF_F_HIGHDMA;
7381 } else {
7382 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7383 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007384 dev_err(&pdev->dev, "DMA configuration failed\n");
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007385 return rc;
Ard Biesheuvel27896c82016-05-14 22:40:15 +02007386 }
7387 }
7388
Francois Romieu3b6cf252012-03-08 09:59:04 +01007389 rtl_init_rxcfg(tp);
7390
7391 rtl_irq_disable(tp);
7392
Hayes Wangc5583862012-07-02 17:23:22 +08007393 rtl_hw_initialize(tp);
7394
Francois Romieu3b6cf252012-03-08 09:59:04 +01007395 rtl_hw_reset(tp);
7396
7397 rtl_ack_events(tp, 0xffff);
7398
7399 pci_set_master(pdev);
7400
Francois Romieu3b6cf252012-03-08 09:59:04 +01007401 rtl_init_mdio_ops(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007402 rtl_init_jumbo_ops(tp);
7403
7404 rtl8169_print_mac_version(tp);
7405
7406 chipset = tp->mac_version;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007407
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007408 rc = rtl_alloc_irq(tp);
7409 if (rc < 0) {
Heiner Kallweit22148df2018-04-22 17:15:15 +02007410 dev_err(&pdev->dev, "Can't allocate interrupt\n");
Heiner Kallweit6c6aa152018-02-24 16:53:23 +01007411 return rc;
7412 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007413
Heiner Kallweit18041b52018-07-24 22:21:04 +02007414 tp->saved_wolopts = __rtl8169_get_wol(tp);
Heiner Kallweit7edf6d32018-02-22 21:22:40 +01007415
Francois Romieu3b6cf252012-03-08 09:59:04 +01007416 mutex_init(&tp->wk.mutex);
Kyle McMartin340fea32014-02-24 20:12:28 -05007417 u64_stats_init(&tp->rx_stats.syncp);
7418 u64_stats_init(&tp->tx_stats.syncp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007419
7420 /* Get MAC address */
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007421 switch (tp->mac_version) {
Heiner Kallweit353af852018-05-02 21:39:59 +02007422 u8 mac_addr[ETH_ALEN] __aligned(4);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007423 case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
7424 case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
Chun-Hao Lin05b96872014-10-01 23:17:12 +08007425 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
Heiner Kallweit353af852018-05-02 21:39:59 +02007426 *(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007427
Heiner Kallweit353af852018-05-02 21:39:59 +02007428 if (is_valid_ether_addr(mac_addr))
7429 rtl_rar_set(tp, mac_addr);
Heiner Kallweitb2d43e62018-05-02 21:39:52 +02007430 break;
7431 default:
7432 break;
Chun-Hao Lin6e1d0b82014-08-20 01:54:04 +08007433 }
Francois Romieu3b6cf252012-03-08 09:59:04 +01007434 for (i = 0; i < ETH_ALEN; i++)
Andy Shevchenko1ef72862018-03-01 13:27:34 +02007435 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007436
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00007437 dev->ethtool_ops = &rtl8169_ethtool_ops;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007438 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007439
Heiner Kallweit37621492018-04-17 23:20:03 +02007440 netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007441
7442 /* don't enable SG, IP_CSUM and TSO by default - it might not work
7443 * properly for all devices */
7444 dev->features |= NETIF_F_RXCSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00007445 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007446
7447 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
Patrick McHardyf6469682013-04-19 02:04:27 +00007448 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7449 NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007450 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7451 NETIF_F_HIGHDMA;
Heiner Kallweit2d0ec542018-07-02 22:49:35 +02007452 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007453
hayeswang929a0312014-09-16 11:40:47 +08007454 tp->cp_cmd |= RxChkSum | RxVlan;
7455
7456 /*
7457 * Pretend we are using VLANs; This bypasses a nasty bug where
7458 * Interrupts stop flowing on high load on 8110SCd controllers.
7459 */
Francois Romieu3b6cf252012-03-08 09:59:04 +01007460 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
hayeswang929a0312014-09-16 11:40:47 +08007461 /* Disallow toggling */
Patrick McHardyf6469682013-04-19 02:04:27 +00007462 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007463
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007464 if (rtl_chip_supports_csum_v2(tp)) {
hayeswang5888d3f2014-07-11 16:25:56 +08007465 tp->tso_csum = rtl8169_tso_csum_v2;
hayeswange9746042014-07-11 16:25:58 +08007466 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
Heiner Kallweiteb88f5f2018-08-10 22:39:29 +02007467 } else {
7468 tp->tso_csum = rtl8169_tso_csum_v1;
Heiner Kallweita4328dd2018-04-17 23:33:03 +02007469 }
hayeswang5888d3f2014-07-11 16:25:56 +08007470
Francois Romieu3b6cf252012-03-08 09:59:04 +01007471 dev->hw_features |= NETIF_F_RXALL;
7472 dev->hw_features |= NETIF_F_RXFCS;
7473
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007474 /* MTU range: 60 - hw-specific max */
7475 dev->min_mtu = ETH_ZLEN;
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007476 jumbo_max = rtl_jumbo_max(tp);
7477 dev->max_mtu = jumbo_max;
Jarod Wilsonc7315a92016-10-17 15:54:09 -04007478
Francois Romieu3b6cf252012-03-08 09:59:04 +01007479 tp->hw_start = cfg->hw_start;
7480 tp->event_slow = cfg->event_slow;
Francois Romieu50970832017-10-27 13:24:49 +03007481 tp->coalesce_info = cfg->coalesce_info;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007482
Francois Romieu3b6cf252012-03-08 09:59:04 +01007483 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
7484
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007485 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7486 &tp->counters_phys_addr,
7487 GFP_KERNEL);
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007488 if (!tp->counters)
7489 return -ENOMEM;
Corinna Vinschen42020322015-09-10 10:47:35 +02007490
Heiner Kallweit19c9ea32018-03-26 19:19:30 +02007491 pci_set_drvdata(pdev, dev);
7492
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007493 rc = r8169_mdio_register(tp);
7494 if (rc)
Heiner Kallweit4cf964a2017-12-12 07:41:06 +01007495 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007496
Heiner Kallweit07df5bd2018-07-17 21:21:37 +02007497 /* chip gets powered up in rtl_open() */
7498 rtl_pll_power_down(tp);
7499
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007500 rc = register_netdev(dev);
7501 if (rc)
7502 goto err_mdio_unregister;
7503
Heiner Kallweit2d6c5a62018-04-17 23:31:21 +02007504 netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
7505 rtl_chip_infos[chipset].name, dev->dev_addr,
Heiner Kallweit90b989c2018-04-17 23:32:15 +02007506 (u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
Heiner Kallweit29274992018-02-28 20:43:38 +01007507 pci_irq_vector(pdev, 0));
Heiner Kallweitabe8b2f2018-08-10 22:40:37 +02007508
7509 if (jumbo_max > JUMBO_1K)
7510 netif_info(tp, probe, dev,
7511 "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
7512 jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
7513 "ok" : "ko");
Francois Romieu3b6cf252012-03-08 09:59:04 +01007514
Heiner Kallweit9dbe7892018-02-22 21:37:48 +01007515 if (r8168_check_dash(tp))
Francois Romieu3b6cf252012-03-08 09:59:04 +01007516 rtl8168_driver_start(tp);
Francois Romieu3b6cf252012-03-08 09:59:04 +01007517
Heiner Kallweita92a0842018-01-08 21:39:13 +01007518 if (pci_dev_run_wake(pdev))
7519 pm_runtime_put_sync(&pdev->dev);
7520
Heiner Kallweit4c45d242017-12-12 07:41:02 +01007521 return 0;
Heiner Kallweitf1e911d2018-07-17 22:51:26 +02007522
7523err_mdio_unregister:
7524 mdiobus_unregister(tp->mii_bus);
7525 return rc;
Francois Romieu3b6cf252012-03-08 09:59:04 +01007526}
7527
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528static struct pci_driver rtl8169_pci_driver = {
7529 .name = MODULENAME,
7530 .id_table = rtl8169_pci_tbl,
Francois Romieu3b6cf252012-03-08 09:59:04 +01007531 .probe = rtl_init_one,
Bill Pembertonbaf63292012-12-03 09:23:28 -05007532 .remove = rtl_remove_one,
Francois Romieu1765f952008-09-13 17:21:40 +02007533 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00007534 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535};
7536
Devendra Naga3eeb7da2012-10-26 09:27:42 +00007537module_pci_driver(rtl8169_pci_driver);