]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/net/ethernet/realtek/r8169.c
r8169: disable WOL per default
[thirdparty/kernel/stable.git] / drivers / net / ethernet / realtek / r8169.c
CommitLineData
1da177e4 1/*
07d3f51f
FR
2 * 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.
1da177e4
LT
9 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
a6b7a407 24#include <linux/interrupt.h>
1da177e4 25#include <linux/dma-mapping.h>
e1759441 26#include <linux/pm_runtime.h>
bca03d5f 27#include <linux/firmware.h>
ba04c7c9 28#include <linux/pci-aspm.h>
70c71606 29#include <linux/prefetch.h>
e974604b 30#include <linux/ipv6.h>
31#include <net/ip6_checksum.h>
1da177e4
LT
32
33#include <asm/io.h>
34#include <asm/irq.h>
35
865c652d 36#define RTL8169_VERSION "2.3LK-NAPI"
1da177e4
LT
37#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
bca03d5f 40#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
01dc7fec 42#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
70090424 44#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
c2218925
HW
45#define FIRMWARE_8168F_1 "rtl_nic/rtl8168f-1.fw"
46#define FIRMWARE_8168F_2 "rtl_nic/rtl8168f-2.fw"
5a5e4443 47#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
7e18dca1 48#define FIRMWARE_8402_1 "rtl_nic/rtl8402-1.fw"
b3d7b2f2 49#define FIRMWARE_8411_1 "rtl_nic/rtl8411-1.fw"
45dd95c4 50#define FIRMWARE_8411_2 "rtl_nic/rtl8411-2.fw"
5598bfe5 51#define FIRMWARE_8106E_1 "rtl_nic/rtl8106e-1.fw"
58152cd4 52#define FIRMWARE_8106E_2 "rtl_nic/rtl8106e-2.fw"
beb330a4 53#define FIRMWARE_8168G_2 "rtl_nic/rtl8168g-2.fw"
57538c4a 54#define FIRMWARE_8168G_3 "rtl_nic/rtl8168g-3.fw"
6e1d0b89
CHL
55#define FIRMWARE_8168H_1 "rtl_nic/rtl8168h-1.fw"
56#define FIRMWARE_8168H_2 "rtl_nic/rtl8168h-2.fw"
57#define FIRMWARE_8107E_1 "rtl_nic/rtl8107e-1.fw"
58#define FIRMWARE_8107E_2 "rtl_nic/rtl8107e-2.fw"
bca03d5f 59
1da177e4
LT
60#ifdef RTL8169_DEBUG
61#define assert(expr) \
5b0384f4
FR
62 if (!(expr)) { \
63 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
b39d66a8 64 #expr,__FILE__,__func__,__LINE__); \
5b0384f4 65 }
06fa7358
JP
66#define dprintk(fmt, args...) \
67 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
1da177e4
LT
68#else
69#define assert(expr) do {} while (0)
70#define dprintk(fmt, args...) do {} while (0)
71#endif /* RTL8169_DEBUG */
72
b57b7e5a 73#define R8169_MSG_DEFAULT \
f0e837d9 74 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
b57b7e5a 75
477206a0
JD
76#define TX_SLOTS_AVAIL(tp) \
77 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx)
78
79/* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
80#define TX_FRAGS_READY_FOR(tp,nr_frags) \
81 (TX_SLOTS_AVAIL(tp) >= (nr_frags + 1))
1da177e4 82
1da177e4
LT
83/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
84 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
f71e1309 85static const int multicast_filter_limit = 32;
1da177e4 86
9c14ceaf 87#define MAX_READ_REQUEST_SHIFT 12
aee77e4a 88#define TX_DMA_BURST 7 /* Maximum PCI burst, '7' is unlimited */
1da177e4
LT
89#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
90
91#define R8169_REGS_SIZE 256
92#define R8169_NAPI_WEIGHT 64
93#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
9fba0812 94#define NUM_RX_DESC 256U /* Number of Rx descriptor registers */
1da177e4
LT
95#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
96#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
97
98#define RTL8169_TX_TIMEOUT (6*HZ)
99#define RTL8169_PHY_TIMEOUT (10*HZ)
100
101/* write/read MMIO register */
102#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
103#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
104#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
105#define RTL_R8(reg) readb (ioaddr + (reg))
106#define RTL_R16(reg) readw (ioaddr + (reg))
06f555f3 107#define RTL_R32(reg) readl (ioaddr + (reg))
1da177e4
LT
108
109enum mac_version {
85bffe6c
FR
110 RTL_GIGA_MAC_VER_01 = 0,
111 RTL_GIGA_MAC_VER_02,
112 RTL_GIGA_MAC_VER_03,
113 RTL_GIGA_MAC_VER_04,
114 RTL_GIGA_MAC_VER_05,
115 RTL_GIGA_MAC_VER_06,
116 RTL_GIGA_MAC_VER_07,
117 RTL_GIGA_MAC_VER_08,
118 RTL_GIGA_MAC_VER_09,
119 RTL_GIGA_MAC_VER_10,
120 RTL_GIGA_MAC_VER_11,
121 RTL_GIGA_MAC_VER_12,
122 RTL_GIGA_MAC_VER_13,
123 RTL_GIGA_MAC_VER_14,
124 RTL_GIGA_MAC_VER_15,
125 RTL_GIGA_MAC_VER_16,
126 RTL_GIGA_MAC_VER_17,
127 RTL_GIGA_MAC_VER_18,
128 RTL_GIGA_MAC_VER_19,
129 RTL_GIGA_MAC_VER_20,
130 RTL_GIGA_MAC_VER_21,
131 RTL_GIGA_MAC_VER_22,
132 RTL_GIGA_MAC_VER_23,
133 RTL_GIGA_MAC_VER_24,
134 RTL_GIGA_MAC_VER_25,
135 RTL_GIGA_MAC_VER_26,
136 RTL_GIGA_MAC_VER_27,
137 RTL_GIGA_MAC_VER_28,
138 RTL_GIGA_MAC_VER_29,
139 RTL_GIGA_MAC_VER_30,
140 RTL_GIGA_MAC_VER_31,
141 RTL_GIGA_MAC_VER_32,
142 RTL_GIGA_MAC_VER_33,
70090424 143 RTL_GIGA_MAC_VER_34,
c2218925
HW
144 RTL_GIGA_MAC_VER_35,
145 RTL_GIGA_MAC_VER_36,
7e18dca1 146 RTL_GIGA_MAC_VER_37,
b3d7b2f2 147 RTL_GIGA_MAC_VER_38,
5598bfe5 148 RTL_GIGA_MAC_VER_39,
c558386b
HW
149 RTL_GIGA_MAC_VER_40,
150 RTL_GIGA_MAC_VER_41,
57538c4a 151 RTL_GIGA_MAC_VER_42,
58152cd4 152 RTL_GIGA_MAC_VER_43,
45dd95c4 153 RTL_GIGA_MAC_VER_44,
6e1d0b89
CHL
154 RTL_GIGA_MAC_VER_45,
155 RTL_GIGA_MAC_VER_46,
156 RTL_GIGA_MAC_VER_47,
157 RTL_GIGA_MAC_VER_48,
935e2218
CHL
158 RTL_GIGA_MAC_VER_49,
159 RTL_GIGA_MAC_VER_50,
160 RTL_GIGA_MAC_VER_51,
85bffe6c 161 RTL_GIGA_MAC_NONE = 0xff,
1da177e4
LT
162};
163
2b7b4318
FR
164enum rtl_tx_desc_version {
165 RTL_TD_0 = 0,
166 RTL_TD_1 = 1,
167};
168
d58d46b5
FR
169#define JUMBO_1K ETH_DATA_LEN
170#define JUMBO_4K (4*1024 - ETH_HLEN - 2)
171#define JUMBO_6K (6*1024 - ETH_HLEN - 2)
172#define JUMBO_7K (7*1024 - ETH_HLEN - 2)
173#define JUMBO_9K (9*1024 - ETH_HLEN - 2)
174
175#define _R(NAME,TD,FW,SZ,B) { \
176 .name = NAME, \
177 .txd_version = TD, \
178 .fw_name = FW, \
179 .jumbo_max = SZ, \
180 .jumbo_tx_csum = B \
181}
1da177e4 182
3c6bee1d 183static const struct {
1da177e4 184 const char *name;
2b7b4318 185 enum rtl_tx_desc_version txd_version;
953a12cc 186 const char *fw_name;
d58d46b5
FR
187 u16 jumbo_max;
188 bool jumbo_tx_csum;
85bffe6c
FR
189} rtl_chip_infos[] = {
190 /* PCI devices. */
191 [RTL_GIGA_MAC_VER_01] =
d58d46b5 192 _R("RTL8169", RTL_TD_0, NULL, JUMBO_7K, true),
85bffe6c 193 [RTL_GIGA_MAC_VER_02] =
d58d46b5 194 _R("RTL8169s", RTL_TD_0, NULL, JUMBO_7K, true),
85bffe6c 195 [RTL_GIGA_MAC_VER_03] =
d58d46b5 196 _R("RTL8110s", RTL_TD_0, NULL, JUMBO_7K, true),
85bffe6c 197 [RTL_GIGA_MAC_VER_04] =
d58d46b5 198 _R("RTL8169sb/8110sb", RTL_TD_0, NULL, JUMBO_7K, true),
85bffe6c 199 [RTL_GIGA_MAC_VER_05] =
d58d46b5 200 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
85bffe6c 201 [RTL_GIGA_MAC_VER_06] =
d58d46b5 202 _R("RTL8169sc/8110sc", RTL_TD_0, NULL, JUMBO_7K, true),
85bffe6c
FR
203 /* PCI-E devices. */
204 [RTL_GIGA_MAC_VER_07] =
d58d46b5 205 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
85bffe6c 206 [RTL_GIGA_MAC_VER_08] =
d58d46b5 207 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
85bffe6c 208 [RTL_GIGA_MAC_VER_09] =
d58d46b5 209 _R("RTL8102e", RTL_TD_1, NULL, JUMBO_1K, true),
85bffe6c 210 [RTL_GIGA_MAC_VER_10] =
d58d46b5 211 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
85bffe6c 212 [RTL_GIGA_MAC_VER_11] =
d58d46b5 213 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
85bffe6c 214 [RTL_GIGA_MAC_VER_12] =
d58d46b5 215 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
85bffe6c 216 [RTL_GIGA_MAC_VER_13] =
d58d46b5 217 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
85bffe6c 218 [RTL_GIGA_MAC_VER_14] =
d58d46b5 219 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
85bffe6c 220 [RTL_GIGA_MAC_VER_15] =
d58d46b5 221 _R("RTL8100e", RTL_TD_0, NULL, JUMBO_1K, true),
85bffe6c 222 [RTL_GIGA_MAC_VER_16] =
d58d46b5 223 _R("RTL8101e", RTL_TD_0, NULL, JUMBO_1K, true),
85bffe6c 224 [RTL_GIGA_MAC_VER_17] =
f75761b6 225 _R("RTL8168b/8111b", RTL_TD_0, NULL, JUMBO_4K, false),
85bffe6c 226 [RTL_GIGA_MAC_VER_18] =
d58d46b5 227 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
85bffe6c 228 [RTL_GIGA_MAC_VER_19] =
d58d46b5 229 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
85bffe6c 230 [RTL_GIGA_MAC_VER_20] =
d58d46b5 231 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
85bffe6c 232 [RTL_GIGA_MAC_VER_21] =
d58d46b5 233 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
85bffe6c 234 [RTL_GIGA_MAC_VER_22] =
d58d46b5 235 _R("RTL8168c/8111c", RTL_TD_1, NULL, JUMBO_6K, false),
85bffe6c 236 [RTL_GIGA_MAC_VER_23] =
d58d46b5 237 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
85bffe6c 238 [RTL_GIGA_MAC_VER_24] =
d58d46b5 239 _R("RTL8168cp/8111cp", RTL_TD_1, NULL, JUMBO_6K, false),
85bffe6c 240 [RTL_GIGA_MAC_VER_25] =
d58d46b5
FR
241 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1,
242 JUMBO_9K, false),
85bffe6c 243 [RTL_GIGA_MAC_VER_26] =
d58d46b5
FR
244 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2,
245 JUMBO_9K, false),
85bffe6c 246 [RTL_GIGA_MAC_VER_27] =
d58d46b5 247 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
85bffe6c 248 [RTL_GIGA_MAC_VER_28] =
d58d46b5 249 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
85bffe6c 250 [RTL_GIGA_MAC_VER_29] =
d58d46b5
FR
251 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
252 JUMBO_1K, true),
85bffe6c 253 [RTL_GIGA_MAC_VER_30] =
d58d46b5
FR
254 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1,
255 JUMBO_1K, true),
85bffe6c 256 [RTL_GIGA_MAC_VER_31] =
d58d46b5 257 _R("RTL8168dp/8111dp", RTL_TD_1, NULL, JUMBO_9K, false),
85bffe6c 258 [RTL_GIGA_MAC_VER_32] =
d58d46b5
FR
259 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1,
260 JUMBO_9K, false),
85bffe6c 261 [RTL_GIGA_MAC_VER_33] =
d58d46b5
FR
262 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2,
263 JUMBO_9K, false),
70090424 264 [RTL_GIGA_MAC_VER_34] =
d58d46b5
FR
265 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3,
266 JUMBO_9K, false),
c2218925 267 [RTL_GIGA_MAC_VER_35] =
d58d46b5
FR
268 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_1,
269 JUMBO_9K, false),
c2218925 270 [RTL_GIGA_MAC_VER_36] =
d58d46b5
FR
271 _R("RTL8168f/8111f", RTL_TD_1, FIRMWARE_8168F_2,
272 JUMBO_9K, false),
7e18dca1
HW
273 [RTL_GIGA_MAC_VER_37] =
274 _R("RTL8402", RTL_TD_1, FIRMWARE_8402_1,
275 JUMBO_1K, true),
b3d7b2f2
HW
276 [RTL_GIGA_MAC_VER_38] =
277 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_1,
278 JUMBO_9K, false),
5598bfe5
HW
279 [RTL_GIGA_MAC_VER_39] =
280 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_1,
281 JUMBO_1K, true),
c558386b 282 [RTL_GIGA_MAC_VER_40] =
beb330a4 283 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_2,
c558386b
HW
284 JUMBO_9K, false),
285 [RTL_GIGA_MAC_VER_41] =
286 _R("RTL8168g/8111g", RTL_TD_1, NULL, JUMBO_9K, false),
57538c4a 287 [RTL_GIGA_MAC_VER_42] =
288 _R("RTL8168g/8111g", RTL_TD_1, FIRMWARE_8168G_3,
289 JUMBO_9K, false),
58152cd4 290 [RTL_GIGA_MAC_VER_43] =
291 _R("RTL8106e", RTL_TD_1, FIRMWARE_8106E_2,
292 JUMBO_1K, true),
45dd95c4 293 [RTL_GIGA_MAC_VER_44] =
294 _R("RTL8411", RTL_TD_1, FIRMWARE_8411_2,
295 JUMBO_9K, false),
6e1d0b89
CHL
296 [RTL_GIGA_MAC_VER_45] =
297 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_1,
298 JUMBO_9K, false),
299 [RTL_GIGA_MAC_VER_46] =
300 _R("RTL8168h/8111h", RTL_TD_1, FIRMWARE_8168H_2,
301 JUMBO_9K, false),
302 [RTL_GIGA_MAC_VER_47] =
303 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_1,
304 JUMBO_1K, false),
305 [RTL_GIGA_MAC_VER_48] =
306 _R("RTL8107e", RTL_TD_1, FIRMWARE_8107E_2,
307 JUMBO_1K, false),
935e2218
CHL
308 [RTL_GIGA_MAC_VER_49] =
309 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
310 JUMBO_9K, false),
311 [RTL_GIGA_MAC_VER_50] =
312 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
313 JUMBO_9K, false),
314 [RTL_GIGA_MAC_VER_51] =
315 _R("RTL8168ep/8111ep", RTL_TD_1, NULL,
316 JUMBO_9K, false),
953a12cc 317};
85bffe6c 318#undef _R
953a12cc 319
bcf0bf90
FR
320enum cfg_version {
321 RTL_CFG_0 = 0x00,
322 RTL_CFG_1,
323 RTL_CFG_2
324};
325
9baa3c34 326static const struct pci_device_id rtl8169_pci_tbl[] = {
bcf0bf90 327 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
d2eed8cf 328 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
610c9087 329 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8161), 0, 0, RTL_CFG_1 },
d81bf551 330 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
07ce4064 331 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
bcf0bf90 332 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
2a35cfa5
FR
333 { PCI_VENDOR_ID_DLINK, 0x4300,
334 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
bcf0bf90 335 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
93a3aa25 336 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
bc1660b5 337 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
bcf0bf90
FR
338 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
339 { PCI_VENDOR_ID_LINKSYS, 0x1032,
340 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
11d2e282
CM
341 { 0x0001, 0x8168,
342 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
1da177e4
LT
343 {0,},
344};
345
346MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
347
6f0333b8 348static int rx_buf_sz = 16383;
27896c83 349static int use_dac = -1;
b57b7e5a
SH
350static struct {
351 u32 msg_enable;
352} debug = { -1 };
1da177e4 353
07d3f51f
FR
354enum rtl_registers {
355 MAC0 = 0, /* Ethernet hardware address. */
773d2021 356 MAC4 = 4,
07d3f51f
FR
357 MAR0 = 8, /* Multicast filter. */
358 CounterAddrLow = 0x10,
359 CounterAddrHigh = 0x14,
360 TxDescStartAddrLow = 0x20,
361 TxDescStartAddrHigh = 0x24,
362 TxHDescStartAddrLow = 0x28,
363 TxHDescStartAddrHigh = 0x2c,
364 FLASH = 0x30,
365 ERSR = 0x36,
366 ChipCmd = 0x37,
367 TxPoll = 0x38,
368 IntrMask = 0x3c,
369 IntrStatus = 0x3e,
4f6b00e5 370
07d3f51f 371 TxConfig = 0x40,
4f6b00e5
HW
372#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
373#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
2b7b4318 374
4f6b00e5
HW
375 RxConfig = 0x44,
376#define RX128_INT_EN (1 << 15) /* 8111c and later */
377#define RX_MULTI_EN (1 << 14) /* 8111c only */
378#define RXCFG_FIFO_SHIFT 13
379 /* No threshold before first PCI xfer */
380#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
beb330a4 381#define RX_EARLY_OFF (1 << 11)
4f6b00e5
HW
382#define RXCFG_DMA_SHIFT 8
383 /* Unlimited maximum PCI burst. */
384#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
2b7b4318 385
07d3f51f
FR
386 RxMissed = 0x4c,
387 Cfg9346 = 0x50,
388 Config0 = 0x51,
389 Config1 = 0x52,
390 Config2 = 0x53,
d387b427
FR
391#define PME_SIGNAL (1 << 5) /* 8168c and later */
392
07d3f51f
FR
393 Config3 = 0x54,
394 Config4 = 0x55,
395 Config5 = 0x56,
396 MultiIntr = 0x5c,
397 PHYAR = 0x60,
07d3f51f
FR
398 PHYstatus = 0x6c,
399 RxMaxSize = 0xda,
400 CPlusCmd = 0xe0,
401 IntrMitigate = 0xe2,
50970831
FR
402
403#define RTL_COALESCE_MASK 0x0f
404#define RTL_COALESCE_SHIFT 4
405#define RTL_COALESCE_T_MAX (RTL_COALESCE_MASK)
406#define RTL_COALESCE_FRAME_MAX (RTL_COALESCE_MASK << 2)
407
07d3f51f
FR
408 RxDescAddrLow = 0xe4,
409 RxDescAddrHigh = 0xe8,
f0298f81 410 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
411
412#define NoEarlyTx 0x3f /* Max value : no early transmit. */
413
414 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
415
416#define TxPacketMax (8064 >> 7)
3090bd9a 417#define EarlySize 0x27
f0298f81 418
07d3f51f
FR
419 FuncEvent = 0xf0,
420 FuncEventMask = 0xf4,
421 FuncPresetState = 0xf8,
935e2218
CHL
422 IBCR0 = 0xf8,
423 IBCR2 = 0xf9,
424 IBIMR0 = 0xfa,
425 IBISR0 = 0xfb,
07d3f51f 426 FuncForceEvent = 0xfc,
1da177e4
LT
427};
428
f162a5d1
FR
429enum rtl8110_registers {
430 TBICSR = 0x64,
431 TBI_ANAR = 0x68,
432 TBI_LPAR = 0x6a,
433};
434
435enum rtl8168_8101_registers {
436 CSIDR = 0x64,
437 CSIAR = 0x68,
438#define CSIAR_FLAG 0x80000000
439#define CSIAR_WRITE_CMD 0x80000000
440#define CSIAR_BYTE_ENABLE 0x0f
441#define CSIAR_BYTE_ENABLE_SHIFT 12
442#define CSIAR_ADDR_MASK 0x0fff
7e18dca1
HW
443#define CSIAR_FUNC_CARD 0x00000000
444#define CSIAR_FUNC_SDIO 0x00010000
445#define CSIAR_FUNC_NIC 0x00020000
45dd95c4 446#define CSIAR_FUNC_NIC2 0x00010000
065c27c1 447 PMCH = 0x6f,
f162a5d1
FR
448 EPHYAR = 0x80,
449#define EPHYAR_FLAG 0x80000000
450#define EPHYAR_WRITE_CMD 0x80000000
451#define EPHYAR_REG_MASK 0x1f
452#define EPHYAR_REG_SHIFT 16
453#define EPHYAR_DATA_MASK 0xffff
5a5e4443 454 DLLPR = 0xd0,
4f6b00e5 455#define PFM_EN (1 << 6)
6e1d0b89 456#define TX_10M_PS_EN (1 << 7)
f162a5d1
FR
457 DBG_REG = 0xd1,
458#define FIX_NAK_1 (1 << 4)
459#define FIX_NAK_2 (1 << 3)
5a5e4443
HW
460 TWSI = 0xd2,
461 MCU = 0xd3,
4f6b00e5 462#define NOW_IS_OOB (1 << 7)
c558386b
HW
463#define TX_EMPTY (1 << 5)
464#define RX_EMPTY (1 << 4)
465#define RXTX_EMPTY (TX_EMPTY | RX_EMPTY)
5a5e4443
HW
466#define EN_NDP (1 << 3)
467#define EN_OOB_RESET (1 << 2)
c558386b 468#define LINK_LIST_RDY (1 << 1)
daf9df6d 469 EFUSEAR = 0xdc,
470#define EFUSEAR_FLAG 0x80000000
471#define EFUSEAR_WRITE_CMD 0x80000000
472#define EFUSEAR_READ_CMD 0x00000000
473#define EFUSEAR_REG_MASK 0x03ff
474#define EFUSEAR_REG_SHIFT 8
475#define EFUSEAR_DATA_MASK 0xff
6e1d0b89
CHL
476 MISC_1 = 0xf2,
477#define PFM_D3COLD_EN (1 << 6)
f162a5d1
FR
478};
479
c0e45c1c 480enum rtl8168_registers {
4f6b00e5
HW
481 LED_FREQ = 0x1a,
482 EEE_LED = 0x1b,
b646d900 483 ERIDR = 0x70,
484 ERIAR = 0x74,
485#define ERIAR_FLAG 0x80000000
486#define ERIAR_WRITE_CMD 0x80000000
487#define ERIAR_READ_CMD 0x00000000
488#define ERIAR_ADDR_BYTE_ALIGN 4
b646d900 489#define ERIAR_TYPE_SHIFT 16
4f6b00e5
HW
490#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
491#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
492#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
935e2218 493#define ERIAR_OOB (0x02 << ERIAR_TYPE_SHIFT)
4f6b00e5
HW
494#define ERIAR_MASK_SHIFT 12
495#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
496#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
6e1d0b89 497#define ERIAR_MASK_0100 (0x4 << ERIAR_MASK_SHIFT)
c558386b 498#define ERIAR_MASK_0101 (0x5 << ERIAR_MASK_SHIFT)
4f6b00e5 499#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
c0e45c1c 500 EPHY_RXER_NUM = 0x7c,
501 OCPDR = 0xb0, /* OCP GPHY access */
502#define OCPDR_WRITE_CMD 0x80000000
503#define OCPDR_READ_CMD 0x00000000
504#define OCPDR_REG_MASK 0x7f
505#define OCPDR_GPHY_REG_SHIFT 16
506#define OCPDR_DATA_MASK 0xffff
507 OCPAR = 0xb4,
508#define OCPAR_FLAG 0x80000000
509#define OCPAR_GPHY_WRITE_CMD 0x8000f060
510#define OCPAR_GPHY_READ_CMD 0x0000f060
c558386b 511 GPHY_OCP = 0xb8,
01dc7fec 512 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
513 MISC = 0xf0, /* 8168e only. */
cecb5fd7 514#define TXPLA_RST (1 << 29)
5598bfe5 515#define DISABLE_LAN_EN (1 << 23) /* Enable GPIO pin */
4f6b00e5 516#define PWM_EN (1 << 22)
c558386b 517#define RXDV_GATED_EN (1 << 19)
5598bfe5 518#define EARLY_TALLY_EN (1 << 16)
c0e45c1c 519};
520
07d3f51f 521enum rtl_register_content {
1da177e4 522 /* InterruptStatusBits */
07d3f51f
FR
523 SYSErr = 0x8000,
524 PCSTimeout = 0x4000,
525 SWInt = 0x0100,
526 TxDescUnavail = 0x0080,
527 RxFIFOOver = 0x0040,
528 LinkChg = 0x0020,
529 RxOverflow = 0x0010,
530 TxErr = 0x0008,
531 TxOK = 0x0004,
532 RxErr = 0x0002,
533 RxOK = 0x0001,
1da177e4
LT
534
535 /* RxStatusDesc */
e03f33af 536 RxBOVF = (1 << 24),
9dccf611
FR
537 RxFOVF = (1 << 23),
538 RxRWT = (1 << 22),
539 RxRES = (1 << 21),
540 RxRUNT = (1 << 20),
541 RxCRC = (1 << 19),
1da177e4
LT
542
543 /* ChipCmdBits */
4f6b00e5 544 StopReq = 0x80,
07d3f51f
FR
545 CmdReset = 0x10,
546 CmdRxEnb = 0x08,
547 CmdTxEnb = 0x04,
548 RxBufEmpty = 0x01,
1da177e4 549
275391a4
FR
550 /* TXPoll register p.5 */
551 HPQ = 0x80, /* Poll cmd on the high prio queue */
552 NPQ = 0x40, /* Poll cmd on the low prio queue */
553 FSWInt = 0x01, /* Forced software interrupt */
554
1da177e4 555 /* Cfg9346Bits */
07d3f51f
FR
556 Cfg9346_Lock = 0x00,
557 Cfg9346_Unlock = 0xc0,
1da177e4
LT
558
559 /* rx_mode_bits */
07d3f51f
FR
560 AcceptErr = 0x20,
561 AcceptRunt = 0x10,
562 AcceptBroadcast = 0x08,
563 AcceptMulticast = 0x04,
564 AcceptMyPhys = 0x02,
565 AcceptAllPhys = 0x01,
1687b566 566#define RX_CONFIG_ACCEPT_MASK 0x3f
1da177e4 567
1da177e4
LT
568 /* TxConfigBits */
569 TxInterFrameGapShift = 24,
570 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
571
5d06a99f 572 /* Config1 register p.24 */
f162a5d1
FR
573 LEDS1 = (1 << 7),
574 LEDS0 = (1 << 6),
f162a5d1
FR
575 Speed_down = (1 << 4),
576 MEMMAP = (1 << 3),
577 IOMAP = (1 << 2),
578 VPD = (1 << 1),
5d06a99f
FR
579 PMEnable = (1 << 0), /* Power Management Enable */
580
6dccd16b 581 /* Config2 register p. 25 */
57538c4a 582 ClkReqEn = (1 << 7), /* Clock Request Enable */
2ca6cf06 583 MSIEnable = (1 << 5), /* 8169 only. Reserved in the 8168. */
6dccd16b
FR
584 PCI_Clock_66MHz = 0x01,
585 PCI_Clock_33MHz = 0x00,
586
61a4dcc2
FR
587 /* Config3 register p.25 */
588 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
589 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
d58d46b5 590 Jumbo_En0 = (1 << 2), /* 8168 only. Reserved in the 8168b */
b51ecea8 591 Rdy_to_L23 = (1 << 1), /* L23 Enable */
f162a5d1 592 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
61a4dcc2 593
d58d46b5
FR
594 /* Config4 register */
595 Jumbo_En1 = (1 << 1), /* 8168 only. Reserved in the 8168b */
596
5d06a99f 597 /* Config5 register p.27 */
61a4dcc2
FR
598 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
599 MWF = (1 << 5), /* Accept Multicast wakeup frame */
600 UWF = (1 << 4), /* Accept Unicast wakeup frame */
cecb5fd7 601 Spi_en = (1 << 3),
61a4dcc2 602 LanWake = (1 << 1), /* LanWake enable/disable */
5d06a99f 603 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
57538c4a 604 ASPM_en = (1 << 0), /* ASPM enable */
5d06a99f 605
1da177e4
LT
606 /* TBICSR p.28 */
607 TBIReset = 0x80000000,
608 TBILoopback = 0x40000000,
609 TBINwEnable = 0x20000000,
610 TBINwRestart = 0x10000000,
611 TBILinkOk = 0x02000000,
612 TBINwComplete = 0x01000000,
613
614 /* CPlusCmd p.31 */
f162a5d1
FR
615 EnableBist = (1 << 15), // 8168 8101
616 Mac_dbgo_oe = (1 << 14), // 8168 8101
617 Normal_mode = (1 << 13), // unused
618 Force_half_dup = (1 << 12), // 8168 8101
619 Force_rxflow_en = (1 << 11), // 8168 8101
620 Force_txflow_en = (1 << 10), // 8168 8101
621 Cxpl_dbg_sel = (1 << 9), // 8168 8101
622 ASF = (1 << 8), // 8168 8101
623 PktCntrDisable = (1 << 7), // 8168 8101
624 Mac_dbgo_sel = 0x001c, // 8168
1da177e4
LT
625 RxVlan = (1 << 6),
626 RxChkSum = (1 << 5),
627 PCIDAC = (1 << 4),
628 PCIMulRW = (1 << 3),
0e485150
FR
629 INTT_0 = 0x0000, // 8168
630 INTT_1 = 0x0001, // 8168
631 INTT_2 = 0x0002, // 8168
632 INTT_3 = 0x0003, // 8168
1da177e4
LT
633
634 /* rtl8169_PHYstatus */
07d3f51f
FR
635 TBI_Enable = 0x80,
636 TxFlowCtrl = 0x40,
637 RxFlowCtrl = 0x20,
638 _1000bpsF = 0x10,
639 _100bps = 0x08,
640 _10bps = 0x04,
641 LinkStatus = 0x02,
642 FullDup = 0x01,
1da177e4 643
1da177e4 644 /* _TBICSRBit */
07d3f51f 645 TBILinkOK = 0x02000000,
d4a3a0fc 646
6e85d5ad
CV
647 /* ResetCounterCommand */
648 CounterReset = 0x1,
649
d4a3a0fc 650 /* DumpCounterCommand */
07d3f51f 651 CounterDump = 0x8,
6e1d0b89
CHL
652
653 /* magic enable v2 */
654 MagicPacket_v2 = (1 << 16), /* Wake up when receives a Magic Packet */
1da177e4
LT
655};
656
2b7b4318
FR
657enum rtl_desc_bit {
658 /* First doubleword. */
1da177e4
LT
659 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
660 RingEnd = (1 << 30), /* End of descriptor ring */
661 FirstFrag = (1 << 29), /* First segment of a packet */
662 LastFrag = (1 << 28), /* Final segment of a packet */
2b7b4318
FR
663};
664
665/* Generic case. */
666enum rtl_tx_desc_bit {
667 /* First doubleword. */
668 TD_LSO = (1 << 27), /* Large Send Offload */
669#define TD_MSS_MAX 0x07ffu /* MSS value */
1da177e4 670
2b7b4318
FR
671 /* Second doubleword. */
672 TxVlanTag = (1 << 17), /* Add VLAN tag */
673};
674
675/* 8169, 8168b and 810x except 8102e. */
676enum rtl_tx_desc_bit_0 {
677 /* First doubleword. */
678#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
679 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
680 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
681 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
682};
683
684/* 8102e, 8168c and beyond. */
685enum rtl_tx_desc_bit_1 {
bdfa4ed6 686 /* First doubleword. */
687 TD1_GTSENV4 = (1 << 26), /* Giant Send for IPv4 */
e974604b 688 TD1_GTSENV6 = (1 << 25), /* Giant Send for IPv6 */
bdfa4ed6 689#define GTTCPHO_SHIFT 18
e974604b 690#define GTTCPHO_MAX 0x7fU
bdfa4ed6 691
2b7b4318 692 /* Second doubleword. */
e974604b 693#define TCPHO_SHIFT 18
694#define TCPHO_MAX 0x3ffU
2b7b4318 695#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
e974604b 696 TD1_IPv6_CS = (1 << 28), /* Calculate IPv6 checksum */
697 TD1_IPv4_CS = (1 << 29), /* Calculate IPv4 checksum */
2b7b4318
FR
698 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
699 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
700};
1da177e4 701
2b7b4318 702enum rtl_rx_desc_bit {
1da177e4
LT
703 /* Rx private */
704 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
9b60047a 705 PID0 = (1 << 17), /* Protocol ID bit 0/2 */
1da177e4
LT
706
707#define RxProtoUDP (PID1)
708#define RxProtoTCP (PID0)
709#define RxProtoIP (PID1 | PID0)
710#define RxProtoMask RxProtoIP
711
712 IPFail = (1 << 16), /* IP checksum failed */
713 UDPFail = (1 << 15), /* UDP/IP checksum failed */
714 TCPFail = (1 << 14), /* TCP/IP checksum failed */
715 RxVlanTag = (1 << 16), /* VLAN tag available */
716};
717
718#define RsvdMask 0x3fffc000
719
720struct TxDesc {
6cccd6e7
REB
721 __le32 opts1;
722 __le32 opts2;
723 __le64 addr;
1da177e4
LT
724};
725
726struct RxDesc {
6cccd6e7
REB
727 __le32 opts1;
728 __le32 opts2;
729 __le64 addr;
1da177e4
LT
730};
731
732struct ring_info {
733 struct sk_buff *skb;
734 u32 len;
735 u8 __pad[sizeof(void *) - sizeof(u32)];
736};
737
f23e7fda 738enum features {
022ddbca
HK
739 RTL_FEATURE_MSI = (1 << 0),
740 RTL_FEATURE_GMII = (1 << 1),
f23e7fda
FR
741};
742
355423d0
IV
743struct rtl8169_counters {
744 __le64 tx_packets;
745 __le64 rx_packets;
746 __le64 tx_errors;
747 __le32 rx_errors;
748 __le16 rx_missed;
749 __le16 align_errors;
750 __le32 tx_one_collision;
751 __le32 tx_multi_collision;
752 __le64 rx_unicast;
753 __le64 rx_broadcast;
754 __le32 rx_multicast;
755 __le16 tx_aborted;
756 __le16 tx_underun;
757};
758
6e85d5ad
CV
759struct rtl8169_tc_offsets {
760 bool inited;
761 __le64 tx_errors;
762 __le32 tx_multi_collision;
6e85d5ad
CV
763 __le16 tx_aborted;
764};
765
da78dbff 766enum rtl_flag {
6c4a70c5 767 RTL_FLAG_TASK_ENABLED,
da78dbff
FR
768 RTL_FLAG_TASK_SLOW_PENDING,
769 RTL_FLAG_TASK_RESET_PENDING,
770 RTL_FLAG_TASK_PHY_PENDING,
771 RTL_FLAG_MAX
772};
773
8027aa24
JW
774struct rtl8169_stats {
775 u64 packets;
776 u64 bytes;
777 struct u64_stats_sync syncp;
778};
779
1da177e4
LT
780struct rtl8169_private {
781 void __iomem *mmio_addr; /* memory map physical address */
cecb5fd7 782 struct pci_dev *pci_dev;
c4028958 783 struct net_device *dev;
bea3348e 784 struct napi_struct napi;
b57b7e5a 785 u32 msg_enable;
2b7b4318
FR
786 u16 txd_version;
787 u16 mac_version;
1da177e4
LT
788 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
789 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
1da177e4 790 u32 dirty_tx;
8027aa24
JW
791 struct rtl8169_stats rx_stats;
792 struct rtl8169_stats tx_stats;
1da177e4
LT
793 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
794 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
795 dma_addr_t TxPhyAddr;
796 dma_addr_t RxPhyAddr;
6f0333b8 797 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
1da177e4 798 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
1da177e4
LT
799 struct timer_list timer;
800 u16 cp_cmd;
da78dbff
FR
801
802 u16 event_slow;
50970831 803 const struct rtl_coalesce_info *coalesce_info;
c0e45c1c 804
805 struct mdio_ops {
24192210
FR
806 void (*write)(struct rtl8169_private *, int, int);
807 int (*read)(struct rtl8169_private *, int);
c0e45c1c 808 } mdio_ops;
809
065c27c1 810 struct pll_power_ops {
811 void (*down)(struct rtl8169_private *);
812 void (*up)(struct rtl8169_private *);
813 } pll_power_ops;
814
d58d46b5
FR
815 struct jumbo_ops {
816 void (*enable)(struct rtl8169_private *);
817 void (*disable)(struct rtl8169_private *);
818 } jumbo_ops;
819
beb1fe18 820 struct csi_ops {
52989f0e
FR
821 void (*write)(struct rtl8169_private *, int, int);
822 u32 (*read)(struct rtl8169_private *, int);
beb1fe18
HW
823 } csi_ops;
824
54405cde 825 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
6fa1ba61
PR
826 int (*get_link_ksettings)(struct net_device *,
827 struct ethtool_link_ksettings *);
4da19633 828 void (*phy_reset_enable)(struct rtl8169_private *tp);
07ce4064 829 void (*hw_start)(struct net_device *);
4da19633 830 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
1da177e4 831 unsigned int (*link_ok)(void __iomem *);
8b4ab28d 832 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
5888d3fc 833 bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
4422bcd4
FR
834
835 struct {
da78dbff
FR
836 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
837 struct mutex mutex;
4422bcd4
FR
838 struct work_struct work;
839 } wk;
840
f23e7fda 841 unsigned features;
ccdffb9a
FR
842
843 struct mii_if_info mii;
42020320
CV
844 dma_addr_t counters_phys_addr;
845 struct rtl8169_counters *counters;
6e85d5ad 846 struct rtl8169_tc_offsets tc_offset;
e1759441 847 u32 saved_wolopts;
e03f33af 848 u32 opts1_mask;
f1e02ed1 849
b6ffd97f
FR
850 struct rtl_fw {
851 const struct firmware *fw;
1c361efb
FR
852
853#define RTL_VER_SIZE 32
854
855 char version[RTL_VER_SIZE];
856
857 struct rtl_fw_phy_action {
858 __le32 *code;
859 size_t size;
860 } phy_action;
b6ffd97f 861 } *rtl_fw;
497888cf 862#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
c558386b
HW
863
864 u32 ocp_base;
1da177e4
LT
865};
866
979b6c13 867MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
1da177e4 868MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
1da177e4 869module_param(use_dac, int, 0);
4300e8c7 870MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
b57b7e5a
SH
871module_param_named(debug, debug.msg_enable, int, 0);
872MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
1da177e4
LT
873MODULE_LICENSE("GPL");
874MODULE_VERSION(RTL8169_VERSION);
bca03d5f 875MODULE_FIRMWARE(FIRMWARE_8168D_1);
876MODULE_FIRMWARE(FIRMWARE_8168D_2);
01dc7fec 877MODULE_FIRMWARE(FIRMWARE_8168E_1);
878MODULE_FIRMWARE(FIRMWARE_8168E_2);
bbb8af75 879MODULE_FIRMWARE(FIRMWARE_8168E_3);
5a5e4443 880MODULE_FIRMWARE(FIRMWARE_8105E_1);
c2218925
HW
881MODULE_FIRMWARE(FIRMWARE_8168F_1);
882MODULE_FIRMWARE(FIRMWARE_8168F_2);
7e18dca1 883MODULE_FIRMWARE(FIRMWARE_8402_1);
b3d7b2f2 884MODULE_FIRMWARE(FIRMWARE_8411_1);
45dd95c4 885MODULE_FIRMWARE(FIRMWARE_8411_2);
5598bfe5 886MODULE_FIRMWARE(FIRMWARE_8106E_1);
58152cd4 887MODULE_FIRMWARE(FIRMWARE_8106E_2);
beb330a4 888MODULE_FIRMWARE(FIRMWARE_8168G_2);
57538c4a 889MODULE_FIRMWARE(FIRMWARE_8168G_3);
6e1d0b89
CHL
890MODULE_FIRMWARE(FIRMWARE_8168H_1);
891MODULE_FIRMWARE(FIRMWARE_8168H_2);
a3bf5c42
FR
892MODULE_FIRMWARE(FIRMWARE_8107E_1);
893MODULE_FIRMWARE(FIRMWARE_8107E_2);
1da177e4 894
da78dbff
FR
895static void rtl_lock_work(struct rtl8169_private *tp)
896{
897 mutex_lock(&tp->wk.mutex);
898}
899
900static void rtl_unlock_work(struct rtl8169_private *tp)
901{
902 mutex_unlock(&tp->wk.mutex);
903}
904
d58d46b5
FR
905static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
906{
7d7903b2
JL
907 pcie_capability_clear_and_set_word(pdev, PCI_EXP_DEVCTL,
908 PCI_EXP_DEVCTL_READRQ, force);
d58d46b5
FR
909}
910
ffc46952
FR
911struct rtl_cond {
912 bool (*check)(struct rtl8169_private *);
913 const char *msg;
914};
915
916static void rtl_udelay(unsigned int d)
917{
918 udelay(d);
919}
920
921static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
922 void (*delay)(unsigned int), unsigned int d, int n,
923 bool high)
924{
925 int i;
926
927 for (i = 0; i < n; i++) {
928 delay(d);
929 if (c->check(tp) == high)
930 return true;
931 }
82e316ef
FR
932 netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
933 c->msg, !high, n, d);
ffc46952
FR
934 return false;
935}
936
937static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
938 const struct rtl_cond *c,
939 unsigned int d, int n)
940{
941 return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
942}
943
944static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
945 const struct rtl_cond *c,
946 unsigned int d, int n)
947{
948 return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
949}
950
951static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
952 const struct rtl_cond *c,
953 unsigned int d, int n)
954{
955 return rtl_loop_wait(tp, c, msleep, d, n, true);
956}
957
958static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
959 const struct rtl_cond *c,
960 unsigned int d, int n)
961{
962 return rtl_loop_wait(tp, c, msleep, d, n, false);
963}
964
965#define DECLARE_RTL_COND(name) \
966static bool name ## _check(struct rtl8169_private *); \
967 \
968static const struct rtl_cond name = { \
969 .check = name ## _check, \
970 .msg = #name \
971}; \
972 \
973static bool name ## _check(struct rtl8169_private *tp)
974
c558386b
HW
975static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
976{
977 if (reg & 0xffff0001) {
978 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
979 return true;
980 }
981 return false;
982}
983
984DECLARE_RTL_COND(rtl_ocp_gphy_cond)
985{
986 void __iomem *ioaddr = tp->mmio_addr;
987
988 return RTL_R32(GPHY_OCP) & OCPAR_FLAG;
989}
990
991static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
992{
993 void __iomem *ioaddr = tp->mmio_addr;
994
995 if (rtl_ocp_reg_failure(tp, reg))
996 return;
997
998 RTL_W32(GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
999
1000 rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
1001}
1002
1003static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
1004{
1005 void __iomem *ioaddr = tp->mmio_addr;
1006
1007 if (rtl_ocp_reg_failure(tp, reg))
1008 return 0;
1009
1010 RTL_W32(GPHY_OCP, reg << 15);
1011
1012 return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
1013 (RTL_R32(GPHY_OCP) & 0xffff) : ~0;
1014}
1015
c558386b
HW
1016static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
1017{
1018 void __iomem *ioaddr = tp->mmio_addr;
1019
1020 if (rtl_ocp_reg_failure(tp, reg))
1021 return;
1022
1023 RTL_W32(OCPDR, OCPAR_FLAG | (reg << 15) | data);
c558386b
HW
1024}
1025
1026static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
1027{
1028 void __iomem *ioaddr = tp->mmio_addr;
1029
1030 if (rtl_ocp_reg_failure(tp, reg))
1031 return 0;
1032
1033 RTL_W32(OCPDR, reg << 15);
1034
3a83ad12 1035 return RTL_R32(OCPDR);
c558386b
HW
1036}
1037
1038#define OCP_STD_PHY_BASE 0xa400
1039
1040static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
1041{
1042 if (reg == 0x1f) {
1043 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
1044 return;
1045 }
1046
1047 if (tp->ocp_base != OCP_STD_PHY_BASE)
1048 reg -= 0x10;
1049
1050 r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
1051}
1052
1053static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
1054{
1055 if (tp->ocp_base != OCP_STD_PHY_BASE)
1056 reg -= 0x10;
1057
1058 return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
1059}
1060
eee3786f 1061static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
1062{
1063 if (reg == 0x1f) {
1064 tp->ocp_base = value << 4;
1065 return;
1066 }
1067
1068 r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
1069}
1070
1071static int mac_mcu_read(struct rtl8169_private *tp, int reg)
1072{
1073 return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
1074}
1075
ffc46952
FR
1076DECLARE_RTL_COND(rtl_phyar_cond)
1077{
1078 void __iomem *ioaddr = tp->mmio_addr;
1079
1080 return RTL_R32(PHYAR) & 0x80000000;
1081}
1082
24192210 1083static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
1da177e4 1084{
24192210 1085 void __iomem *ioaddr = tp->mmio_addr;
1da177e4 1086
24192210 1087 RTL_W32(PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
1da177e4 1088
ffc46952 1089 rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
024a07ba 1090 /*
81a95f04
TT
1091 * According to hardware specs a 20us delay is required after write
1092 * complete indication, but before sending next command.
024a07ba 1093 */
81a95f04 1094 udelay(20);
1da177e4
LT
1095}
1096
24192210 1097static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
1da177e4 1098{
24192210 1099 void __iomem *ioaddr = tp->mmio_addr;
ffc46952 1100 int value;
1da177e4 1101
24192210 1102 RTL_W32(PHYAR, 0x0 | (reg & 0x1f) << 16);
1da177e4 1103
ffc46952
FR
1104 value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
1105 RTL_R32(PHYAR) & 0xffff : ~0;
1106
81a95f04
TT
1107 /*
1108 * According to hardware specs a 20us delay is required after read
1109 * complete indication, but before sending next command.
1110 */
1111 udelay(20);
1112
1da177e4
LT
1113 return value;
1114}
1115
935e2218
CHL
1116DECLARE_RTL_COND(rtl_ocpar_cond)
1117{
1118 void __iomem *ioaddr = tp->mmio_addr;
1119
1120 return RTL_R32(OCPAR) & OCPAR_FLAG;
1121}
1122
24192210 1123static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
c0e45c1c 1124{
24192210 1125 void __iomem *ioaddr = tp->mmio_addr;
c0e45c1c 1126
24192210 1127 RTL_W32(OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
c0e45c1c 1128 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
1129 RTL_W32(EPHY_RXER_NUM, 0);
1130
ffc46952 1131 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
c0e45c1c 1132}
1133
24192210 1134static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
c0e45c1c 1135{
24192210
FR
1136 r8168dp_1_mdio_access(tp, reg,
1137 OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
c0e45c1c 1138}
1139
24192210 1140static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
c0e45c1c 1141{
24192210 1142 void __iomem *ioaddr = tp->mmio_addr;
c0e45c1c 1143
24192210 1144 r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
c0e45c1c 1145
1146 mdelay(1);
1147 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
1148 RTL_W32(EPHY_RXER_NUM, 0);
1149
ffc46952
FR
1150 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
1151 RTL_R32(OCPDR) & OCPDR_DATA_MASK : ~0;
c0e45c1c 1152}
1153
e6de30d6 1154#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
1155
1156static void r8168dp_2_mdio_start(void __iomem *ioaddr)
1157{
1158 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
1159}
1160
1161static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
1162{
1163 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
1164}
1165
24192210 1166static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
e6de30d6 1167{
24192210
FR
1168 void __iomem *ioaddr = tp->mmio_addr;
1169
e6de30d6 1170 r8168dp_2_mdio_start(ioaddr);
1171
24192210 1172 r8169_mdio_write(tp, reg, value);
e6de30d6 1173
1174 r8168dp_2_mdio_stop(ioaddr);
1175}
1176
24192210 1177static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
e6de30d6 1178{
24192210 1179 void __iomem *ioaddr = tp->mmio_addr;
e6de30d6 1180 int value;
1181
1182 r8168dp_2_mdio_start(ioaddr);
1183
24192210 1184 value = r8169_mdio_read(tp, reg);
e6de30d6 1185
1186 r8168dp_2_mdio_stop(ioaddr);
1187
1188 return value;
1189}
1190
4da19633 1191static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
dacf8154 1192{
24192210 1193 tp->mdio_ops.write(tp, location, val);
dacf8154
FR
1194}
1195
4da19633 1196static int rtl_readphy(struct rtl8169_private *tp, int location)
1197{
24192210 1198 return tp->mdio_ops.read(tp, location);
4da19633 1199}
1200
1201static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1202{
1203 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1204}
1205
76564428 1206static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
daf9df6d 1207{
1208 int val;
1209
4da19633 1210 val = rtl_readphy(tp, reg_addr);
76564428 1211 rtl_writephy(tp, reg_addr, (val & ~m) | p);
daf9df6d 1212}
1213
ccdffb9a
FR
1214static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
1215 int val)
1216{
1217 struct rtl8169_private *tp = netdev_priv(dev);
ccdffb9a 1218
4da19633 1219 rtl_writephy(tp, location, val);
ccdffb9a
FR
1220}
1221
1222static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
1223{
1224 struct rtl8169_private *tp = netdev_priv(dev);
ccdffb9a 1225
4da19633 1226 return rtl_readphy(tp, location);
ccdffb9a
FR
1227}
1228
ffc46952
FR
1229DECLARE_RTL_COND(rtl_ephyar_cond)
1230{
1231 void __iomem *ioaddr = tp->mmio_addr;
1232
1233 return RTL_R32(EPHYAR) & EPHYAR_FLAG;
1234}
1235
fdf6fc06 1236static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
dacf8154 1237{
fdf6fc06 1238 void __iomem *ioaddr = tp->mmio_addr;
dacf8154
FR
1239
1240 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1241 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1242
ffc46952
FR
1243 rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1244
1245 udelay(10);
dacf8154
FR
1246}
1247
fdf6fc06 1248static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
dacf8154 1249{
fdf6fc06 1250 void __iomem *ioaddr = tp->mmio_addr;
dacf8154
FR
1251
1252 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1253
ffc46952
FR
1254 return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1255 RTL_R32(EPHYAR) & EPHYAR_DATA_MASK : ~0;
dacf8154
FR
1256}
1257
935e2218
CHL
1258DECLARE_RTL_COND(rtl_eriar_cond)
1259{
1260 void __iomem *ioaddr = tp->mmio_addr;
1261
1262 return RTL_R32(ERIAR) & ERIAR_FLAG;
1263}
1264
fdf6fc06
FR
1265static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1266 u32 val, int type)
133ac40a 1267{
fdf6fc06 1268 void __iomem *ioaddr = tp->mmio_addr;
133ac40a
HW
1269
1270 BUG_ON((addr & 3) || (mask == 0));
1271 RTL_W32(ERIDR, val);
1272 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1273
ffc46952 1274 rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
133ac40a
HW
1275}
1276
fdf6fc06 1277static u32 rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
133ac40a 1278{
fdf6fc06 1279 void __iomem *ioaddr = tp->mmio_addr;
133ac40a
HW
1280
1281 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1282
ffc46952
FR
1283 return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1284 RTL_R32(ERIDR) : ~0;
133ac40a
HW
1285}
1286
706123d0 1287static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
fdf6fc06 1288 u32 m, int type)
133ac40a
HW
1289{
1290 u32 val;
1291
fdf6fc06
FR
1292 val = rtl_eri_read(tp, addr, type);
1293 rtl_eri_write(tp, addr, mask, (val & ~m) | p, type);
133ac40a
HW
1294}
1295
935e2218
CHL
1296static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1297{
1298 void __iomem *ioaddr = tp->mmio_addr;
1299
1300 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1301 return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1302 RTL_R32(OCPDR) : ~0;
1303}
1304
1305static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1306{
1307 return rtl_eri_read(tp, reg, ERIAR_OOB);
1308}
1309
1310static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1311{
1312 switch (tp->mac_version) {
1313 case RTL_GIGA_MAC_VER_27:
1314 case RTL_GIGA_MAC_VER_28:
1315 case RTL_GIGA_MAC_VER_31:
1316 return r8168dp_ocp_read(tp, mask, reg);
1317 case RTL_GIGA_MAC_VER_49:
1318 case RTL_GIGA_MAC_VER_50:
1319 case RTL_GIGA_MAC_VER_51:
1320 return r8168ep_ocp_read(tp, mask, reg);
1321 default:
1322 BUG();
1323 return ~0;
1324 }
1325}
1326
1327static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1328 u32 data)
1329{
1330 void __iomem *ioaddr = tp->mmio_addr;
1331
1332 RTL_W32(OCPDR, data);
1333 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1334 rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1335}
1336
1337static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1338 u32 data)
1339{
1340 rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1341 data, ERIAR_OOB);
1342}
1343
1344static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
1345{
1346 switch (tp->mac_version) {
1347 case RTL_GIGA_MAC_VER_27:
1348 case RTL_GIGA_MAC_VER_28:
1349 case RTL_GIGA_MAC_VER_31:
1350 r8168dp_ocp_write(tp, mask, reg, data);
1351 break;
1352 case RTL_GIGA_MAC_VER_49:
1353 case RTL_GIGA_MAC_VER_50:
1354 case RTL_GIGA_MAC_VER_51:
1355 r8168ep_ocp_write(tp, mask, reg, data);
1356 break;
1357 default:
1358 BUG();
1359 break;
1360 }
1361}
1362
2a9b4d96
CHL
1363static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
1364{
1365 rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd, ERIAR_EXGMAC);
1366
1367 ocp_write(tp, 0x1, 0x30, 0x00000001);
1368}
1369
1370#define OOB_CMD_RESET 0x00
1371#define OOB_CMD_DRIVER_START 0x05
1372#define OOB_CMD_DRIVER_STOP 0x06
1373
1374static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1375{
1376 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1377}
1378
1379DECLARE_RTL_COND(rtl_ocp_read_cond)
1380{
1381 u16 reg;
1382
1383 reg = rtl8168_get_ocp_reg(tp);
1384
1385 return ocp_read(tp, 0x0f, reg) & 0x00000800;
1386}
1387
935e2218 1388DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
2a9b4d96 1389{
935e2218
CHL
1390 return ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1391}
1392
1393DECLARE_RTL_COND(rtl_ocp_tx_cond)
1394{
1395 void __iomem *ioaddr = tp->mmio_addr;
1396
086ca23d 1397 return RTL_R8(IBISR0) & 0x20;
935e2218 1398}
2a9b4d96 1399
003609da
CHL
1400static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1401{
1402 void __iomem *ioaddr = tp->mmio_addr;
1403
1404 RTL_W8(IBCR2, RTL_R8(IBCR2) & ~0x01);
086ca23d 1405 rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
003609da
CHL
1406 RTL_W8(IBISR0, RTL_R8(IBISR0) | 0x20);
1407 RTL_W8(IBCR0, RTL_R8(IBCR0) & ~0x01);
1408}
1409
935e2218
CHL
1410static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1411{
1412 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
2a9b4d96
CHL
1413 rtl_msleep_loop_wait_high(tp, &rtl_ocp_read_cond, 10, 10);
1414}
1415
935e2218 1416static void rtl8168ep_driver_start(struct rtl8169_private *tp)
2a9b4d96 1417{
935e2218
CHL
1418 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1419 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1420 rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1421}
1422
1423static void rtl8168_driver_start(struct rtl8169_private *tp)
1424{
1425 switch (tp->mac_version) {
1426 case RTL_GIGA_MAC_VER_27:
1427 case RTL_GIGA_MAC_VER_28:
1428 case RTL_GIGA_MAC_VER_31:
1429 rtl8168dp_driver_start(tp);
1430 break;
1431 case RTL_GIGA_MAC_VER_49:
1432 case RTL_GIGA_MAC_VER_50:
1433 case RTL_GIGA_MAC_VER_51:
1434 rtl8168ep_driver_start(tp);
1435 break;
1436 default:
1437 BUG();
1438 break;
1439 }
1440}
2a9b4d96 1441
935e2218
CHL
1442static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1443{
1444 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
2a9b4d96
CHL
1445 rtl_msleep_loop_wait_low(tp, &rtl_ocp_read_cond, 10, 10);
1446}
1447
935e2218
CHL
1448static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1449{
003609da 1450 rtl8168ep_stop_cmac(tp);
935e2218
CHL
1451 ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1452 ocp_write(tp, 0x01, 0x30, ocp_read(tp, 0x01, 0x30) | 0x01);
1453 rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1454}
1455
1456static void rtl8168_driver_stop(struct rtl8169_private *tp)
1457{
1458 switch (tp->mac_version) {
1459 case RTL_GIGA_MAC_VER_27:
1460 case RTL_GIGA_MAC_VER_28:
1461 case RTL_GIGA_MAC_VER_31:
1462 rtl8168dp_driver_stop(tp);
1463 break;
1464 case RTL_GIGA_MAC_VER_49:
1465 case RTL_GIGA_MAC_VER_50:
1466 case RTL_GIGA_MAC_VER_51:
1467 rtl8168ep_driver_stop(tp);
1468 break;
1469 default:
1470 BUG();
1471 break;
1472 }
1473}
1474
1475static int r8168dp_check_dash(struct rtl8169_private *tp)
2a9b4d96
CHL
1476{
1477 u16 reg = rtl8168_get_ocp_reg(tp);
1478
1479 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
1480}
1481
935e2218
CHL
1482static int r8168ep_check_dash(struct rtl8169_private *tp)
1483{
1484 return (ocp_read(tp, 0x0f, 0x128) & 0x00000001) ? 1 : 0;
1485}
1486
1487static int r8168_check_dash(struct rtl8169_private *tp)
1488{
1489 switch (tp->mac_version) {
1490 case RTL_GIGA_MAC_VER_27:
1491 case RTL_GIGA_MAC_VER_28:
1492 case RTL_GIGA_MAC_VER_31:
1493 return r8168dp_check_dash(tp);
1494 case RTL_GIGA_MAC_VER_49:
1495 case RTL_GIGA_MAC_VER_50:
1496 case RTL_GIGA_MAC_VER_51:
1497 return r8168ep_check_dash(tp);
1498 default:
1499 return 0;
1500 }
1501}
1502
c28aa385 1503struct exgmac_reg {
1504 u16 addr;
1505 u16 mask;
1506 u32 val;
1507};
1508
fdf6fc06 1509static void rtl_write_exgmac_batch(struct rtl8169_private *tp,
c28aa385 1510 const struct exgmac_reg *r, int len)
1511{
1512 while (len-- > 0) {
fdf6fc06 1513 rtl_eri_write(tp, r->addr, r->mask, r->val, ERIAR_EXGMAC);
c28aa385 1514 r++;
1515 }
1516}
1517
ffc46952
FR
1518DECLARE_RTL_COND(rtl_efusear_cond)
1519{
1520 void __iomem *ioaddr = tp->mmio_addr;
1521
1522 return RTL_R32(EFUSEAR) & EFUSEAR_FLAG;
1523}
1524
fdf6fc06 1525static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
daf9df6d 1526{
fdf6fc06 1527 void __iomem *ioaddr = tp->mmio_addr;
daf9df6d 1528
1529 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1530
ffc46952
FR
1531 return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1532 RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
daf9df6d 1533}
1534
9085cdfa
FR
1535static u16 rtl_get_events(struct rtl8169_private *tp)
1536{
1537 void __iomem *ioaddr = tp->mmio_addr;
1538
1539 return RTL_R16(IntrStatus);
1540}
1541
1542static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1543{
1544 void __iomem *ioaddr = tp->mmio_addr;
1545
1546 RTL_W16(IntrStatus, bits);
1547 mmiowb();
1548}
1549
1550static void rtl_irq_disable(struct rtl8169_private *tp)
1551{
1552 void __iomem *ioaddr = tp->mmio_addr;
1553
1554 RTL_W16(IntrMask, 0);
1555 mmiowb();
1556}
1557
3e990ff5
FR
1558static void rtl_irq_enable(struct rtl8169_private *tp, u16 bits)
1559{
1560 void __iomem *ioaddr = tp->mmio_addr;
1561
1562 RTL_W16(IntrMask, bits);
1563}
1564
da78dbff
FR
1565#define RTL_EVENT_NAPI_RX (RxOK | RxErr)
1566#define RTL_EVENT_NAPI_TX (TxOK | TxErr)
1567#define RTL_EVENT_NAPI (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1568
1569static void rtl_irq_enable_all(struct rtl8169_private *tp)
1570{
1571 rtl_irq_enable(tp, RTL_EVENT_NAPI | tp->event_slow);
1572}
1573
811fd301 1574static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1da177e4 1575{
811fd301 1576 void __iomem *ioaddr = tp->mmio_addr;
1da177e4 1577
9085cdfa 1578 rtl_irq_disable(tp);
da78dbff 1579 rtl_ack_events(tp, RTL_EVENT_NAPI | tp->event_slow);
811fd301 1580 RTL_R8(ChipCmd);
1da177e4
LT
1581}
1582
4da19633 1583static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1da177e4 1584{
4da19633 1585 void __iomem *ioaddr = tp->mmio_addr;
1586
1da177e4
LT
1587 return RTL_R32(TBICSR) & TBIReset;
1588}
1589
4da19633 1590static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1da177e4 1591{
4da19633 1592 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1da177e4
LT
1593}
1594
1595static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1596{
1597 return RTL_R32(TBICSR) & TBILinkOk;
1598}
1599
1600static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1601{
1602 return RTL_R8(PHYstatus) & LinkStatus;
1603}
1604
4da19633 1605static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1da177e4 1606{
4da19633 1607 void __iomem *ioaddr = tp->mmio_addr;
1608
1da177e4
LT
1609 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1610}
1611
4da19633 1612static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1da177e4
LT
1613{
1614 unsigned int val;
1615
4da19633 1616 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1617 rtl_writephy(tp, MII_BMCR, val & 0xffff);
1da177e4
LT
1618}
1619
70090424
HW
1620static void rtl_link_chg_patch(struct rtl8169_private *tp)
1621{
1622 void __iomem *ioaddr = tp->mmio_addr;
1623 struct net_device *dev = tp->dev;
1624
1625 if (!netif_running(dev))
1626 return;
1627
b3d7b2f2
HW
1628 if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1629 tp->mac_version == RTL_GIGA_MAC_VER_38) {
70090424 1630 if (RTL_R8(PHYstatus) & _1000bpsF) {
fdf6fc06
FR
1631 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1632 ERIAR_EXGMAC);
1633 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1634 ERIAR_EXGMAC);
70090424 1635 } else if (RTL_R8(PHYstatus) & _100bps) {
fdf6fc06
FR
1636 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1637 ERIAR_EXGMAC);
1638 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1639 ERIAR_EXGMAC);
70090424 1640 } else {
fdf6fc06
FR
1641 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1642 ERIAR_EXGMAC);
1643 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1644 ERIAR_EXGMAC);
70090424
HW
1645 }
1646 /* Reset packet filter */
706123d0 1647 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
70090424 1648 ERIAR_EXGMAC);
706123d0 1649 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
70090424 1650 ERIAR_EXGMAC);
c2218925
HW
1651 } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1652 tp->mac_version == RTL_GIGA_MAC_VER_36) {
1653 if (RTL_R8(PHYstatus) & _1000bpsF) {
fdf6fc06
FR
1654 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011,
1655 ERIAR_EXGMAC);
1656 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005,
1657 ERIAR_EXGMAC);
c2218925 1658 } else {
fdf6fc06
FR
1659 rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f,
1660 ERIAR_EXGMAC);
1661 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f,
1662 ERIAR_EXGMAC);
c2218925 1663 }
7e18dca1
HW
1664 } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1665 if (RTL_R8(PHYstatus) & _10bps) {
fdf6fc06
FR
1666 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02,
1667 ERIAR_EXGMAC);
1668 rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060,
1669 ERIAR_EXGMAC);
7e18dca1 1670 } else {
fdf6fc06
FR
1671 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000,
1672 ERIAR_EXGMAC);
7e18dca1 1673 }
70090424
HW
1674 }
1675}
1676
ef4d5fcc
HK
1677static void rtl8169_check_link_status(struct net_device *dev,
1678 struct rtl8169_private *tp,
1679 void __iomem *ioaddr)
1da177e4 1680{
1da177e4 1681 if (tp->link_ok(ioaddr)) {
70090424 1682 rtl_link_chg_patch(tp);
e1759441 1683 /* This is to cancel a scheduled suspend if there's one. */
ef4d5fcc 1684 pm_request_resume(&tp->pci_dev->dev);
1da177e4 1685 netif_carrier_on(dev);
1519e57f
FR
1686 if (net_ratelimit())
1687 netif_info(tp, ifup, dev, "link up\n");
b57b7e5a 1688 } else {
1da177e4 1689 netif_carrier_off(dev);
bf82c189 1690 netif_info(tp, ifdown, dev, "link down\n");
a92a0849 1691 pm_runtime_idle(&tp->pci_dev->dev);
b57b7e5a 1692 }
1da177e4
LT
1693}
1694
e1759441
RW
1695#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1696
1697static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
61a4dcc2 1698{
61a4dcc2
FR
1699 void __iomem *ioaddr = tp->mmio_addr;
1700 u8 options;
e1759441 1701 u32 wolopts = 0;
61a4dcc2
FR
1702
1703 options = RTL_R8(Config1);
1704 if (!(options & PMEnable))
e1759441 1705 return 0;
61a4dcc2
FR
1706
1707 options = RTL_R8(Config3);
1708 if (options & LinkUp)
e1759441 1709 wolopts |= WAKE_PHY;
6e1d0b89 1710 switch (tp->mac_version) {
ac85bcdb
CHL
1711 case RTL_GIGA_MAC_VER_34:
1712 case RTL_GIGA_MAC_VER_35:
1713 case RTL_GIGA_MAC_VER_36:
1714 case RTL_GIGA_MAC_VER_37:
1715 case RTL_GIGA_MAC_VER_38:
1716 case RTL_GIGA_MAC_VER_40:
1717 case RTL_GIGA_MAC_VER_41:
1718 case RTL_GIGA_MAC_VER_42:
1719 case RTL_GIGA_MAC_VER_43:
1720 case RTL_GIGA_MAC_VER_44:
6e1d0b89
CHL
1721 case RTL_GIGA_MAC_VER_45:
1722 case RTL_GIGA_MAC_VER_46:
ac85bcdb
CHL
1723 case RTL_GIGA_MAC_VER_47:
1724 case RTL_GIGA_MAC_VER_48:
935e2218
CHL
1725 case RTL_GIGA_MAC_VER_49:
1726 case RTL_GIGA_MAC_VER_50:
1727 case RTL_GIGA_MAC_VER_51:
6e1d0b89
CHL
1728 if (rtl_eri_read(tp, 0xdc, ERIAR_EXGMAC) & MagicPacket_v2)
1729 wolopts |= WAKE_MAGIC;
1730 break;
1731 default:
1732 if (options & MagicPacket)
1733 wolopts |= WAKE_MAGIC;
1734 break;
1735 }
61a4dcc2
FR
1736
1737 options = RTL_R8(Config5);
1738 if (options & UWF)
e1759441 1739 wolopts |= WAKE_UCAST;
61a4dcc2 1740 if (options & BWF)
e1759441 1741 wolopts |= WAKE_BCAST;
61a4dcc2 1742 if (options & MWF)
e1759441 1743 wolopts |= WAKE_MCAST;
61a4dcc2 1744
e1759441 1745 return wolopts;
61a4dcc2
FR
1746}
1747
e1759441 1748static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
61a4dcc2
FR
1749{
1750 struct rtl8169_private *tp = netdev_priv(dev);
5fa80a32
CHL
1751 struct device *d = &tp->pci_dev->dev;
1752
1753 pm_runtime_get_noresume(d);
e1759441 1754
da78dbff 1755 rtl_lock_work(tp);
e1759441
RW
1756
1757 wol->supported = WAKE_ANY;
5fa80a32
CHL
1758 if (pm_runtime_active(d))
1759 wol->wolopts = __rtl8169_get_wol(tp);
1760 else
1761 wol->wolopts = tp->saved_wolopts;
e1759441 1762
da78dbff 1763 rtl_unlock_work(tp);
5fa80a32
CHL
1764
1765 pm_runtime_put_noidle(d);
e1759441
RW
1766}
1767
1768static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1769{
61a4dcc2 1770 void __iomem *ioaddr = tp->mmio_addr;
6e1d0b89 1771 unsigned int i, tmp;
350f7596 1772 static const struct {
61a4dcc2
FR
1773 u32 opt;
1774 u16 reg;
1775 u8 mask;
1776 } cfg[] = {
61a4dcc2 1777 { WAKE_PHY, Config3, LinkUp },
61a4dcc2
FR
1778 { WAKE_UCAST, Config5, UWF },
1779 { WAKE_BCAST, Config5, BWF },
1780 { WAKE_MCAST, Config5, MWF },
6e1d0b89
CHL
1781 { WAKE_ANY, Config5, LanWake },
1782 { WAKE_MAGIC, Config3, MagicPacket }
61a4dcc2 1783 };
851e6022 1784 u8 options;
61a4dcc2 1785
61a4dcc2
FR
1786 RTL_W8(Cfg9346, Cfg9346_Unlock);
1787
6e1d0b89 1788 switch (tp->mac_version) {
ac85bcdb
CHL
1789 case RTL_GIGA_MAC_VER_34:
1790 case RTL_GIGA_MAC_VER_35:
1791 case RTL_GIGA_MAC_VER_36:
1792 case RTL_GIGA_MAC_VER_37:
1793 case RTL_GIGA_MAC_VER_38:
1794 case RTL_GIGA_MAC_VER_40:
1795 case RTL_GIGA_MAC_VER_41:
1796 case RTL_GIGA_MAC_VER_42:
1797 case RTL_GIGA_MAC_VER_43:
1798 case RTL_GIGA_MAC_VER_44:
6e1d0b89
CHL
1799 case RTL_GIGA_MAC_VER_45:
1800 case RTL_GIGA_MAC_VER_46:
ac85bcdb
CHL
1801 case RTL_GIGA_MAC_VER_47:
1802 case RTL_GIGA_MAC_VER_48:
935e2218
CHL
1803 case RTL_GIGA_MAC_VER_49:
1804 case RTL_GIGA_MAC_VER_50:
1805 case RTL_GIGA_MAC_VER_51:
6e1d0b89
CHL
1806 tmp = ARRAY_SIZE(cfg) - 1;
1807 if (wolopts & WAKE_MAGIC)
706123d0 1808 rtl_w0w1_eri(tp,
6e1d0b89
CHL
1809 0x0dc,
1810 ERIAR_MASK_0100,
1811 MagicPacket_v2,
1812 0x0000,
1813 ERIAR_EXGMAC);
1814 else
706123d0 1815 rtl_w0w1_eri(tp,
6e1d0b89
CHL
1816 0x0dc,
1817 ERIAR_MASK_0100,
1818 0x0000,
1819 MagicPacket_v2,
1820 ERIAR_EXGMAC);
1821 break;
1822 default:
1823 tmp = ARRAY_SIZE(cfg);
1824 break;
1825 }
1826
1827 for (i = 0; i < tmp; i++) {
851e6022 1828 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
e1759441 1829 if (wolopts & cfg[i].opt)
61a4dcc2
FR
1830 options |= cfg[i].mask;
1831 RTL_W8(cfg[i].reg, options);
1832 }
1833
851e6022
FR
1834 switch (tp->mac_version) {
1835 case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1836 options = RTL_R8(Config1) & ~PMEnable;
1837 if (wolopts)
1838 options |= PMEnable;
1839 RTL_W8(Config1, options);
1840 break;
1841 default:
d387b427
FR
1842 options = RTL_R8(Config2) & ~PME_SIGNAL;
1843 if (wolopts)
1844 options |= PME_SIGNAL;
1845 RTL_W8(Config2, options);
851e6022
FR
1846 break;
1847 }
1848
61a4dcc2 1849 RTL_W8(Cfg9346, Cfg9346_Lock);
e1759441
RW
1850}
1851
1852static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1853{
1854 struct rtl8169_private *tp = netdev_priv(dev);
5fa80a32
CHL
1855 struct device *d = &tp->pci_dev->dev;
1856
1857 pm_runtime_get_noresume(d);
e1759441 1858
da78dbff 1859 rtl_lock_work(tp);
61a4dcc2 1860
5fa80a32
CHL
1861 if (pm_runtime_active(d))
1862 __rtl8169_set_wol(tp, wol->wolopts);
1863 else
1864 tp->saved_wolopts = wol->wolopts;
da78dbff
FR
1865
1866 rtl_unlock_work(tp);
61a4dcc2 1867
ea80907f 1868 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1869
5fa80a32
CHL
1870 pm_runtime_put_noidle(d);
1871
61a4dcc2
FR
1872 return 0;
1873}
1874
31bd204f
FR
1875static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1876{
85bffe6c 1877 return rtl_chip_infos[tp->mac_version].fw_name;
31bd204f
FR
1878}
1879
1da177e4
LT
1880static void rtl8169_get_drvinfo(struct net_device *dev,
1881 struct ethtool_drvinfo *info)
1882{
1883 struct rtl8169_private *tp = netdev_priv(dev);
b6ffd97f 1884 struct rtl_fw *rtl_fw = tp->rtl_fw;
1da177e4 1885
68aad78c
RJ
1886 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1887 strlcpy(info->version, RTL8169_VERSION, sizeof(info->version));
1888 strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1c361efb 1889 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
8ac72d16
RJ
1890 if (!IS_ERR_OR_NULL(rtl_fw))
1891 strlcpy(info->fw_version, rtl_fw->version,
1892 sizeof(info->fw_version));
1da177e4
LT
1893}
1894
1895static int rtl8169_get_regs_len(struct net_device *dev)
1896{
1897 return R8169_REGS_SIZE;
1898}
1899
1900static int rtl8169_set_speed_tbi(struct net_device *dev,
54405cde 1901 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1da177e4
LT
1902{
1903 struct rtl8169_private *tp = netdev_priv(dev);
1904 void __iomem *ioaddr = tp->mmio_addr;
1905 int ret = 0;
1906 u32 reg;
1907
1908 reg = RTL_R32(TBICSR);
1909 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1910 (duplex == DUPLEX_FULL)) {
1911 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1912 } else if (autoneg == AUTONEG_ENABLE)
1913 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1914 else {
bf82c189
JP
1915 netif_warn(tp, link, dev,
1916 "incorrect speed setting refused in TBI mode\n");
1da177e4
LT
1917 ret = -EOPNOTSUPP;
1918 }
1919
1920 return ret;
1921}
1922
1923static int rtl8169_set_speed_xmii(struct net_device *dev,
54405cde 1924 u8 autoneg, u16 speed, u8 duplex, u32 adv)
1da177e4
LT
1925{
1926 struct rtl8169_private *tp = netdev_priv(dev);
3577aa1b 1927 int giga_ctrl, bmcr;
54405cde 1928 int rc = -EINVAL;
1da177e4 1929
716b50a3 1930 rtl_writephy(tp, 0x1f, 0x0000);
1da177e4
LT
1931
1932 if (autoneg == AUTONEG_ENABLE) {
3577aa1b 1933 int auto_nego;
1934
4da19633 1935 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
54405cde
ON
1936 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1937 ADVERTISE_100HALF | ADVERTISE_100FULL);
1938
1939 if (adv & ADVERTISED_10baseT_Half)
1940 auto_nego |= ADVERTISE_10HALF;
1941 if (adv & ADVERTISED_10baseT_Full)
1942 auto_nego |= ADVERTISE_10FULL;
1943 if (adv & ADVERTISED_100baseT_Half)
1944 auto_nego |= ADVERTISE_100HALF;
1945 if (adv & ADVERTISED_100baseT_Full)
1946 auto_nego |= ADVERTISE_100FULL;
1947
3577aa1b 1948 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1da177e4 1949
4da19633 1950 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
3577aa1b 1951 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
bcf0bf90 1952
3577aa1b 1953 /* The 8100e/8101e/8102e do Fast Ethernet only. */
826e6cbd 1954 if (tp->mii.supports_gmii) {
54405cde
ON
1955 if (adv & ADVERTISED_1000baseT_Half)
1956 giga_ctrl |= ADVERTISE_1000HALF;
1957 if (adv & ADVERTISED_1000baseT_Full)
1958 giga_ctrl |= ADVERTISE_1000FULL;
1959 } else if (adv & (ADVERTISED_1000baseT_Half |
1960 ADVERTISED_1000baseT_Full)) {
bf82c189
JP
1961 netif_info(tp, link, dev,
1962 "PHY does not support 1000Mbps\n");
54405cde 1963 goto out;
bcf0bf90 1964 }
1da177e4 1965
3577aa1b 1966 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1967
4da19633 1968 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1969 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
3577aa1b 1970 } else {
3577aa1b 1971 if (speed == SPEED_10)
1972 bmcr = 0;
1973 else if (speed == SPEED_100)
1974 bmcr = BMCR_SPEED100;
1975 else
54405cde 1976 goto out;
3577aa1b 1977
1978 if (duplex == DUPLEX_FULL)
1979 bmcr |= BMCR_FULLDPLX;
2584fbc3
RS
1980 }
1981
4da19633 1982 rtl_writephy(tp, MII_BMCR, bmcr);
3577aa1b 1983
cecb5fd7
FR
1984 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1985 tp->mac_version == RTL_GIGA_MAC_VER_03) {
3577aa1b 1986 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
4da19633 1987 rtl_writephy(tp, 0x17, 0x2138);
1988 rtl_writephy(tp, 0x0e, 0x0260);
3577aa1b 1989 } else {
4da19633 1990 rtl_writephy(tp, 0x17, 0x2108);
1991 rtl_writephy(tp, 0x0e, 0x0000);
3577aa1b 1992 }
1993 }
1994
54405cde
ON
1995 rc = 0;
1996out:
1997 return rc;
1da177e4
LT
1998}
1999
2000static int rtl8169_set_speed(struct net_device *dev,
54405cde 2001 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1da177e4
LT
2002{
2003 struct rtl8169_private *tp = netdev_priv(dev);
2004 int ret;
2005
54405cde 2006 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
4876cc1e
FR
2007 if (ret < 0)
2008 goto out;
1da177e4 2009
4876cc1e 2010 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
c4556975
CHL
2011 (advertising & ADVERTISED_1000baseT_Full) &&
2012 !pci_is_pcie(tp->pci_dev)) {
1da177e4 2013 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
4876cc1e
FR
2014 }
2015out:
1da177e4
LT
2016 return ret;
2017}
2018
c8f44aff
MM
2019static netdev_features_t rtl8169_fix_features(struct net_device *dev,
2020 netdev_features_t features)
1da177e4 2021{
d58d46b5
FR
2022 struct rtl8169_private *tp = netdev_priv(dev);
2023
2b7b4318 2024 if (dev->mtu > TD_MSS_MAX)
350fb32a 2025 features &= ~NETIF_F_ALL_TSO;
1da177e4 2026
d58d46b5
FR
2027 if (dev->mtu > JUMBO_1K &&
2028 !rtl_chip_infos[tp->mac_version].jumbo_tx_csum)
2029 features &= ~NETIF_F_IP_CSUM;
2030
350fb32a 2031 return features;
1da177e4
LT
2032}
2033
da78dbff
FR
2034static void __rtl8169_set_features(struct net_device *dev,
2035 netdev_features_t features)
1da177e4
LT
2036{
2037 struct rtl8169_private *tp = netdev_priv(dev);
da78dbff 2038 void __iomem *ioaddr = tp->mmio_addr;
929a031d 2039 u32 rx_config;
1da177e4 2040
929a031d 2041 rx_config = RTL_R32(RxConfig);
2042 if (features & NETIF_F_RXALL)
2043 rx_config |= (AcceptErr | AcceptRunt);
2044 else
2045 rx_config &= ~(AcceptErr | AcceptRunt);
1da177e4 2046
929a031d 2047 RTL_W32(RxConfig, rx_config);
350fb32a 2048
929a031d 2049 if (features & NETIF_F_RXCSUM)
2050 tp->cp_cmd |= RxChkSum;
2051 else
2052 tp->cp_cmd &= ~RxChkSum;
6bbe021d 2053
929a031d 2054 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2055 tp->cp_cmd |= RxVlan;
2056 else
2057 tp->cp_cmd &= ~RxVlan;
2058
2059 tp->cp_cmd |= RTL_R16(CPlusCmd) & ~(RxVlan | RxChkSum);
2060
2061 RTL_W16(CPlusCmd, tp->cp_cmd);
2062 RTL_R16(CPlusCmd);
da78dbff 2063}
1da177e4 2064
da78dbff
FR
2065static int rtl8169_set_features(struct net_device *dev,
2066 netdev_features_t features)
2067{
2068 struct rtl8169_private *tp = netdev_priv(dev);
2069
929a031d 2070 features &= NETIF_F_RXALL | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
2071
da78dbff 2072 rtl_lock_work(tp);
85911d71 2073 if (features ^ dev->features)
929a031d 2074 __rtl8169_set_features(dev, features);
da78dbff 2075 rtl_unlock_work(tp);
1da177e4
LT
2076
2077 return 0;
2078}
2079
da78dbff 2080
810f4893 2081static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1da177e4 2082{
df8a39de
JP
2083 return (skb_vlan_tag_present(skb)) ?
2084 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
1da177e4
LT
2085}
2086
7a8fc77b 2087static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1da177e4
LT
2088{
2089 u32 opts2 = le32_to_cpu(desc->opts2);
1da177e4 2090
7a8fc77b 2091 if (opts2 & RxVlanTag)
86a9bad3 2092 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1da177e4
LT
2093}
2094
6fa1ba61
PR
2095static int rtl8169_get_link_ksettings_tbi(struct net_device *dev,
2096 struct ethtool_link_ksettings *cmd)
1da177e4
LT
2097{
2098 struct rtl8169_private *tp = netdev_priv(dev);
2099 void __iomem *ioaddr = tp->mmio_addr;
2100 u32 status;
6fa1ba61 2101 u32 supported, advertising;
1da177e4 2102
6fa1ba61 2103 supported =
1da177e4 2104 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
6fa1ba61 2105 cmd->base.port = PORT_FIBRE;
1da177e4
LT
2106
2107 status = RTL_R32(TBICSR);
6fa1ba61
PR
2108 advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
2109 cmd->base.autoneg = !!(status & TBINwEnable);
1da177e4 2110
6fa1ba61
PR
2111 cmd->base.speed = SPEED_1000;
2112 cmd->base.duplex = DUPLEX_FULL; /* Always set */
2113
2114 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2115 supported);
2116 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2117 advertising);
ccdffb9a
FR
2118
2119 return 0;
1da177e4
LT
2120}
2121
6fa1ba61
PR
2122static int rtl8169_get_link_ksettings_xmii(struct net_device *dev,
2123 struct ethtool_link_ksettings *cmd)
1da177e4
LT
2124{
2125 struct rtl8169_private *tp = netdev_priv(dev);
ccdffb9a 2126
82c01a84 2127 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
2128
2129 return 0;
1da177e4
LT
2130}
2131
6fa1ba61
PR
2132static int rtl8169_get_link_ksettings(struct net_device *dev,
2133 struct ethtool_link_ksettings *cmd)
1da177e4
LT
2134{
2135 struct rtl8169_private *tp = netdev_priv(dev);
ccdffb9a 2136 int rc;
1da177e4 2137
da78dbff 2138 rtl_lock_work(tp);
6fa1ba61 2139 rc = tp->get_link_ksettings(dev, cmd);
da78dbff 2140 rtl_unlock_work(tp);
1da177e4 2141
ccdffb9a 2142 return rc;
1da177e4
LT
2143}
2144
9e77d7a5
TJ
2145static int rtl8169_set_link_ksettings(struct net_device *dev,
2146 const struct ethtool_link_ksettings *cmd)
2147{
2148 struct rtl8169_private *tp = netdev_priv(dev);
2149 int rc;
2150 u32 advertising;
2151
2152 if (!ethtool_convert_link_mode_to_legacy_u32(&advertising,
2153 cmd->link_modes.advertising))
2154 return -EINVAL;
2155
2156 del_timer_sync(&tp->timer);
2157
2158 rtl_lock_work(tp);
2159 rc = rtl8169_set_speed(dev, cmd->base.autoneg, cmd->base.speed,
2160 cmd->base.duplex, advertising);
2161 rtl_unlock_work(tp);
2162
2163 return rc;
2164}
2165
1da177e4
LT
2166static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2167 void *p)
2168{
5b0384f4 2169 struct rtl8169_private *tp = netdev_priv(dev);
15edae91
PW
2170 u32 __iomem *data = tp->mmio_addr;
2171 u32 *dw = p;
2172 int i;
1da177e4 2173
da78dbff 2174 rtl_lock_work(tp);
15edae91
PW
2175 for (i = 0; i < R8169_REGS_SIZE; i += 4)
2176 memcpy_fromio(dw++, data++, 4);
da78dbff 2177 rtl_unlock_work(tp);
1da177e4
LT
2178}
2179
b57b7e5a
SH
2180static u32 rtl8169_get_msglevel(struct net_device *dev)
2181{
2182 struct rtl8169_private *tp = netdev_priv(dev);
2183
2184 return tp->msg_enable;
2185}
2186
2187static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
2188{
2189 struct rtl8169_private *tp = netdev_priv(dev);
2190
2191 tp->msg_enable = value;
2192}
2193
d4a3a0fc
SH
2194static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
2195 "tx_packets",
2196 "rx_packets",
2197 "tx_errors",
2198 "rx_errors",
2199 "rx_missed",
2200 "align_errors",
2201 "tx_single_collisions",
2202 "tx_multi_collisions",
2203 "unicast",
2204 "broadcast",
2205 "multicast",
2206 "tx_aborted",
2207 "tx_underrun",
2208};
2209
b9f2c044 2210static int rtl8169_get_sset_count(struct net_device *dev, int sset)
d4a3a0fc 2211{
b9f2c044
JG
2212 switch (sset) {
2213 case ETH_SS_STATS:
2214 return ARRAY_SIZE(rtl8169_gstrings);
2215 default:
2216 return -EOPNOTSUPP;
2217 }
d4a3a0fc
SH
2218}
2219
42020320 2220DECLARE_RTL_COND(rtl_counters_cond)
6e85d5ad 2221{
6e85d5ad 2222 void __iomem *ioaddr = tp->mmio_addr;
6e85d5ad 2223
42020320 2224 return RTL_R32(CounterAddrLow) & (CounterReset | CounterDump);
6e85d5ad
CV
2225}
2226
42020320 2227static bool rtl8169_do_counters(struct net_device *dev, u32 counter_cmd)
6e85d5ad
CV
2228{
2229 struct rtl8169_private *tp = netdev_priv(dev);
2230 void __iomem *ioaddr = tp->mmio_addr;
42020320
CV
2231 dma_addr_t paddr = tp->counters_phys_addr;
2232 u32 cmd;
6e85d5ad 2233
42020320 2234 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
a78e9366 2235 RTL_R32(CounterAddrHigh);
42020320
CV
2236 cmd = (u64)paddr & DMA_BIT_MASK(32);
2237 RTL_W32(CounterAddrLow, cmd);
2238 RTL_W32(CounterAddrLow, cmd | counter_cmd);
6e85d5ad 2239
a78e9366 2240 return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
6e85d5ad
CV
2241}
2242
2243static bool rtl8169_reset_counters(struct net_device *dev)
2244{
2245 struct rtl8169_private *tp = netdev_priv(dev);
6e85d5ad
CV
2246
2247 /*
2248 * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
2249 * tally counters.
2250 */
2251 if (tp->mac_version < RTL_GIGA_MAC_VER_19)
2252 return true;
2253
42020320 2254 return rtl8169_do_counters(dev, CounterReset);
ffc46952
FR
2255}
2256
6e85d5ad 2257static bool rtl8169_update_counters(struct net_device *dev)
d4a3a0fc
SH
2258{
2259 struct rtl8169_private *tp = netdev_priv(dev);
2260 void __iomem *ioaddr = tp->mmio_addr;
d4a3a0fc 2261
355423d0
IV
2262 /*
2263 * Some chips are unable to dump tally counters when the receiver
2264 * is disabled.
2265 */
2266 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
6e85d5ad 2267 return true;
d4a3a0fc 2268
42020320 2269 return rtl8169_do_counters(dev, CounterDump);
6e85d5ad
CV
2270}
2271
2272static bool rtl8169_init_counter_offsets(struct net_device *dev)
2273{
2274 struct rtl8169_private *tp = netdev_priv(dev);
42020320 2275 struct rtl8169_counters *counters = tp->counters;
6e85d5ad
CV
2276 bool ret = false;
2277
2278 /*
2279 * rtl8169_init_counter_offsets is called from rtl_open. On chip
2280 * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
2281 * reset by a power cycle, while the counter values collected by the
2282 * driver are reset at every driver unload/load cycle.
2283 *
2284 * To make sure the HW values returned by @get_stats64 match the SW
2285 * values, we collect the initial values at first open(*) and use them
2286 * as offsets to normalize the values returned by @get_stats64.
2287 *
2288 * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
2289 * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
2290 * set at open time by rtl_hw_start.
2291 */
2292
2293 if (tp->tc_offset.inited)
2294 return true;
2295
2296 /* If both, reset and update fail, propagate to caller. */
2297 if (rtl8169_reset_counters(dev))
2298 ret = true;
2299
2300 if (rtl8169_update_counters(dev))
2301 ret = true;
2302
42020320
CV
2303 tp->tc_offset.tx_errors = counters->tx_errors;
2304 tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
2305 tp->tc_offset.tx_aborted = counters->tx_aborted;
6e85d5ad
CV
2306 tp->tc_offset.inited = true;
2307
2308 return ret;
d4a3a0fc
SH
2309}
2310
355423d0
IV
2311static void rtl8169_get_ethtool_stats(struct net_device *dev,
2312 struct ethtool_stats *stats, u64 *data)
2313{
2314 struct rtl8169_private *tp = netdev_priv(dev);
e0636236 2315 struct device *d = &tp->pci_dev->dev;
42020320 2316 struct rtl8169_counters *counters = tp->counters;
355423d0
IV
2317
2318 ASSERT_RTNL();
2319
e0636236
CHL
2320 pm_runtime_get_noresume(d);
2321
2322 if (pm_runtime_active(d))
2323 rtl8169_update_counters(dev);
2324
2325 pm_runtime_put_noidle(d);
355423d0 2326
42020320
CV
2327 data[0] = le64_to_cpu(counters->tx_packets);
2328 data[1] = le64_to_cpu(counters->rx_packets);
2329 data[2] = le64_to_cpu(counters->tx_errors);
2330 data[3] = le32_to_cpu(counters->rx_errors);
2331 data[4] = le16_to_cpu(counters->rx_missed);
2332 data[5] = le16_to_cpu(counters->align_errors);
2333 data[6] = le32_to_cpu(counters->tx_one_collision);
2334 data[7] = le32_to_cpu(counters->tx_multi_collision);
2335 data[8] = le64_to_cpu(counters->rx_unicast);
2336 data[9] = le64_to_cpu(counters->rx_broadcast);
2337 data[10] = le32_to_cpu(counters->rx_multicast);
2338 data[11] = le16_to_cpu(counters->tx_aborted);
2339 data[12] = le16_to_cpu(counters->tx_underun);
355423d0
IV
2340}
2341
d4a3a0fc
SH
2342static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2343{
2344 switch(stringset) {
2345 case ETH_SS_STATS:
2346 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
2347 break;
2348 }
2349}
2350
f0903ea3
FF
2351static int rtl8169_nway_reset(struct net_device *dev)
2352{
2353 struct rtl8169_private *tp = netdev_priv(dev);
2354
2355 return mii_nway_restart(&tp->mii);
2356}
2357
50970831
FR
2358/*
2359 * Interrupt coalescing
2360 *
2361 * > 1 - the availability of the IntrMitigate (0xe2) register through the
2362 * > 8169, 8168 and 810x line of chipsets
2363 *
2364 * 8169, 8168, and 8136(810x) serial chipsets support it.
2365 *
2366 * > 2 - the Tx timer unit at gigabit speed
2367 *
2368 * The unit of the timer depends on both the speed and the setting of CPlusCmd
2369 * (0xe0) bit 1 and bit 0.
2370 *
2371 * For 8169
2372 * bit[1:0] \ speed 1000M 100M 10M
2373 * 0 0 320ns 2.56us 40.96us
2374 * 0 1 2.56us 20.48us 327.7us
2375 * 1 0 5.12us 40.96us 655.4us
2376 * 1 1 10.24us 81.92us 1.31ms
2377 *
2378 * For the other
2379 * bit[1:0] \ speed 1000M 100M 10M
2380 * 0 0 5us 2.56us 40.96us
2381 * 0 1 40us 20.48us 327.7us
2382 * 1 0 80us 40.96us 655.4us
2383 * 1 1 160us 81.92us 1.31ms
2384 */
2385
2386/* rx/tx scale factors for one particular CPlusCmd[0:1] value */
2387struct rtl_coalesce_scale {
2388 /* Rx / Tx */
2389 u32 nsecs[2];
2390};
2391
2392/* rx/tx scale factors for all CPlusCmd[0:1] cases */
2393struct rtl_coalesce_info {
2394 u32 speed;
2395 struct rtl_coalesce_scale scalev[4]; /* each CPlusCmd[0:1] case */
2396};
2397
2398/* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
2399#define rxtx_x1822(r, t) { \
2400 {{(r), (t)}}, \
2401 {{(r)*8, (t)*8}}, \
2402 {{(r)*8*2, (t)*8*2}}, \
2403 {{(r)*8*2*2, (t)*8*2*2}}, \
2404}
2405static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
2406 /* speed delays: rx00 tx00 */
2407 { SPEED_10, rxtx_x1822(40960, 40960) },
2408 { SPEED_100, rxtx_x1822( 2560, 2560) },
2409 { SPEED_1000, rxtx_x1822( 320, 320) },
2410 { 0 },
2411};
2412
2413static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
2414 /* speed delays: rx00 tx00 */
2415 { SPEED_10, rxtx_x1822(40960, 40960) },
2416 { SPEED_100, rxtx_x1822( 2560, 2560) },
2417 { SPEED_1000, rxtx_x1822( 5000, 5000) },
2418 { 0 },
2419};
2420#undef rxtx_x1822
2421
2422/* get rx/tx scale vector corresponding to current speed */
2423static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
2424{
2425 struct rtl8169_private *tp = netdev_priv(dev);
2426 struct ethtool_link_ksettings ecmd;
2427 const struct rtl_coalesce_info *ci;
2428 int rc;
2429
2430 rc = rtl8169_get_link_ksettings(dev, &ecmd);
2431 if (rc < 0)
2432 return ERR_PTR(rc);
2433
2434 for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
2435 if (ecmd.base.speed == ci->speed) {
2436 return ci;
2437 }
2438 }
2439
2440 return ERR_PTR(-ELNRNG);
2441}
2442
2443static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2444{
2445 struct rtl8169_private *tp = netdev_priv(dev);
2446 void __iomem *ioaddr = tp->mmio_addr;
2447 const struct rtl_coalesce_info *ci;
2448 const struct rtl_coalesce_scale *scale;
2449 struct {
2450 u32 *max_frames;
2451 u32 *usecs;
2452 } coal_settings [] = {
2453 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
2454 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
2455 }, *p = coal_settings;
2456 int i;
2457 u16 w;
2458
2459 memset(ec, 0, sizeof(*ec));
2460
2461 /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
2462 ci = rtl_coalesce_info(dev);
2463 if (IS_ERR(ci))
2464 return PTR_ERR(ci);
2465
2466 scale = &ci->scalev[RTL_R16(CPlusCmd) & 3];
2467
2468 /* read IntrMitigate and adjust according to scale */
2469 for (w = RTL_R16(IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
2470 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
2471 w >>= RTL_COALESCE_SHIFT;
2472 *p->usecs = w & RTL_COALESCE_MASK;
2473 }
2474
2475 for (i = 0; i < 2; i++) {
2476 p = coal_settings + i;
2477 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
2478
2479 /*
2480 * ethtool_coalesce says it is illegal to set both usecs and
2481 * max_frames to 0.
2482 */
2483 if (!*p->usecs && !*p->max_frames)
2484 *p->max_frames = 1;
2485 }
2486
2487 return 0;
2488}
2489
2490/* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
2491static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
2492 struct net_device *dev, u32 nsec, u16 *cp01)
2493{
2494 const struct rtl_coalesce_info *ci;
2495 u16 i;
2496
2497 ci = rtl_coalesce_info(dev);
2498 if (IS_ERR(ci))
2499 return ERR_CAST(ci);
2500
2501 for (i = 0; i < 4; i++) {
2502 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
2503 ci->scalev[i].nsecs[1]);
2504 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
2505 *cp01 = i;
2506 return &ci->scalev[i];
2507 }
2508 }
2509
2510 return ERR_PTR(-EINVAL);
2511}
2512
2513static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
2514{
2515 struct rtl8169_private *tp = netdev_priv(dev);
2516 void __iomem *ioaddr = tp->mmio_addr;
2517 const struct rtl_coalesce_scale *scale;
2518 struct {
2519 u32 frames;
2520 u32 usecs;
2521 } coal_settings [] = {
2522 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
2523 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
2524 }, *p = coal_settings;
2525 u16 w = 0, cp01;
2526 int i;
2527
2528 scale = rtl_coalesce_choose_scale(dev,
2529 max(p[0].usecs, p[1].usecs) * 1000, &cp01);
2530 if (IS_ERR(scale))
2531 return PTR_ERR(scale);
2532
2533 for (i = 0; i < 2; i++, p++) {
2534 u32 units;
2535
2536 /*
2537 * accept max_frames=1 we returned in rtl_get_coalesce.
2538 * accept it not only when usecs=0 because of e.g. the following scenario:
2539 *
2540 * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
2541 * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
2542 * - then user does `ethtool -C eth0 rx-usecs 100`
2543 *
2544 * since ethtool sends to kernel whole ethtool_coalesce
2545 * settings, if we do not handle rx_usecs=!0, rx_frames=1
2546 * we'll reject it below in `frames % 4 != 0`.
2547 */
2548 if (p->frames == 1) {
2549 p->frames = 0;
2550 }
2551
2552 units = p->usecs * 1000 / scale->nsecs[i];
2553 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
2554 return -EINVAL;
2555
2556 w <<= RTL_COALESCE_SHIFT;
2557 w |= units;
2558 w <<= RTL_COALESCE_SHIFT;
2559 w |= p->frames >> 2;
2560 }
2561
2562 rtl_lock_work(tp);
2563
2564 RTL_W16(IntrMitigate, swab16(w));
2565
2566 tp->cp_cmd = (tp->cp_cmd & ~3) | cp01;
2567 RTL_W16(CPlusCmd, tp->cp_cmd);
2568 RTL_R16(CPlusCmd);
2569
2570 rtl_unlock_work(tp);
2571
2572 return 0;
2573}
2574
7282d491 2575static const struct ethtool_ops rtl8169_ethtool_ops = {
1da177e4
LT
2576 .get_drvinfo = rtl8169_get_drvinfo,
2577 .get_regs_len = rtl8169_get_regs_len,
2578 .get_link = ethtool_op_get_link,
50970831
FR
2579 .get_coalesce = rtl_get_coalesce,
2580 .set_coalesce = rtl_set_coalesce,
b57b7e5a
SH
2581 .get_msglevel = rtl8169_get_msglevel,
2582 .set_msglevel = rtl8169_set_msglevel,
1da177e4 2583 .get_regs = rtl8169_get_regs,
61a4dcc2
FR
2584 .get_wol = rtl8169_get_wol,
2585 .set_wol = rtl8169_set_wol,
d4a3a0fc 2586 .get_strings = rtl8169_get_strings,
b9f2c044 2587 .get_sset_count = rtl8169_get_sset_count,
d4a3a0fc 2588 .get_ethtool_stats = rtl8169_get_ethtool_stats,
e1593bb1 2589 .get_ts_info = ethtool_op_get_ts_info,
f0903ea3 2590 .nway_reset = rtl8169_nway_reset,
6fa1ba61 2591 .get_link_ksettings = rtl8169_get_link_ksettings,
9e77d7a5 2592 .set_link_ksettings = rtl8169_set_link_ksettings,
1da177e4
LT
2593};
2594
07d3f51f 2595static void rtl8169_get_mac_version(struct rtl8169_private *tp,
5d320a20 2596 struct net_device *dev, u8 default_version)
1da177e4 2597{
5d320a20 2598 void __iomem *ioaddr = tp->mmio_addr;
0e485150
FR
2599 /*
2600 * The driver currently handles the 8168Bf and the 8168Be identically
2601 * but they can be identified more specifically through the test below
2602 * if needed:
2603 *
2604 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
0127215c
FR
2605 *
2606 * Same thing for the 8101Eb and the 8101Ec:
2607 *
2608 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
0e485150 2609 */
3744100e 2610 static const struct rtl_mac_info {
1da177e4 2611 u32 mask;
e3cf0cc0 2612 u32 val;
1da177e4
LT
2613 int mac_version;
2614 } mac_info[] = {
935e2218
CHL
2615 /* 8168EP family. */
2616 { 0x7cf00000, 0x50200000, RTL_GIGA_MAC_VER_51 },
2617 { 0x7cf00000, 0x50100000, RTL_GIGA_MAC_VER_50 },
2618 { 0x7cf00000, 0x50000000, RTL_GIGA_MAC_VER_49 },
2619
6e1d0b89
CHL
2620 /* 8168H family. */
2621 { 0x7cf00000, 0x54100000, RTL_GIGA_MAC_VER_46 },
2622 { 0x7cf00000, 0x54000000, RTL_GIGA_MAC_VER_45 },
2623
c558386b 2624 /* 8168G family. */
45dd95c4 2625 { 0x7cf00000, 0x5c800000, RTL_GIGA_MAC_VER_44 },
57538c4a 2626 { 0x7cf00000, 0x50900000, RTL_GIGA_MAC_VER_42 },
c558386b
HW
2627 { 0x7cf00000, 0x4c100000, RTL_GIGA_MAC_VER_41 },
2628 { 0x7cf00000, 0x4c000000, RTL_GIGA_MAC_VER_40 },
2629
c2218925 2630 /* 8168F family. */
b3d7b2f2 2631 { 0x7c800000, 0x48800000, RTL_GIGA_MAC_VER_38 },
c2218925
HW
2632 { 0x7cf00000, 0x48100000, RTL_GIGA_MAC_VER_36 },
2633 { 0x7cf00000, 0x48000000, RTL_GIGA_MAC_VER_35 },
2634
01dc7fec 2635 /* 8168E family. */
70090424 2636 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
01dc7fec 2637 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
2638 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
2639 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
2640
5b538df9 2641 /* 8168D family. */
daf9df6d 2642 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
2643 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
daf9df6d 2644 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
5b538df9 2645
e6de30d6 2646 /* 8168DP family. */
2647 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
2648 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
4804b3b3 2649 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
e6de30d6 2650
ef808d50 2651 /* 8168C family. */
17c99297 2652 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
ef3386f0 2653 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
ef808d50 2654 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
7f3e3d3a 2655 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
e3cf0cc0
FR
2656 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
2657 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
197ff761 2658 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
6fb07058 2659 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
ef808d50 2660 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
e3cf0cc0
FR
2661
2662 /* 8168B family. */
2663 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
2664 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
2665 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
2666 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
2667
2668 /* 8101 family. */
5598bfe5
HW
2669 { 0x7cf00000, 0x44900000, RTL_GIGA_MAC_VER_39 },
2670 { 0x7c800000, 0x44800000, RTL_GIGA_MAC_VER_39 },
7e18dca1 2671 { 0x7c800000, 0x44000000, RTL_GIGA_MAC_VER_37 },
36a0e6c2 2672 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
5a5e4443
HW
2673 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
2674 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
2675 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
2857ffb7
FR
2676 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
2677 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
2678 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
2679 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
2680 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
2681 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
e3cf0cc0 2682 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
2857ffb7 2683 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
e3cf0cc0 2684 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
2857ffb7
FR
2685 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
2686 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
e3cf0cc0
FR
2687 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
2688 /* FIXME: where did these entries come from ? -- FR */
2689 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
2690 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
2691
2692 /* 8110 family. */
2693 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
2694 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
2695 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
2696 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
2697 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
2698 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
2699
f21b75e9
JD
2700 /* Catch-all */
2701 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
3744100e
FR
2702 };
2703 const struct rtl_mac_info *p = mac_info;
1da177e4
LT
2704 u32 reg;
2705
e3cf0cc0
FR
2706 reg = RTL_R32(TxConfig);
2707 while ((reg & p->mask) != p->val)
1da177e4
LT
2708 p++;
2709 tp->mac_version = p->mac_version;
5d320a20
FR
2710
2711 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2712 netif_notice(tp, probe, dev,
2713 "unknown MAC, using family default\n");
2714 tp->mac_version = default_version;
58152cd4 2715 } else if (tp->mac_version == RTL_GIGA_MAC_VER_42) {
2716 tp->mac_version = tp->mii.supports_gmii ?
2717 RTL_GIGA_MAC_VER_42 :
2718 RTL_GIGA_MAC_VER_43;
6e1d0b89
CHL
2719 } else if (tp->mac_version == RTL_GIGA_MAC_VER_45) {
2720 tp->mac_version = tp->mii.supports_gmii ?
2721 RTL_GIGA_MAC_VER_45 :
2722 RTL_GIGA_MAC_VER_47;
2723 } else if (tp->mac_version == RTL_GIGA_MAC_VER_46) {
2724 tp->mac_version = tp->mii.supports_gmii ?
2725 RTL_GIGA_MAC_VER_46 :
2726 RTL_GIGA_MAC_VER_48;
5d320a20 2727 }
1da177e4
LT
2728}
2729
2730static void rtl8169_print_mac_version(struct rtl8169_private *tp)
2731{
bcf0bf90 2732 dprintk("mac_version = 0x%02x\n", tp->mac_version);
1da177e4
LT
2733}
2734
867763c1
FR
2735struct phy_reg {
2736 u16 reg;
2737 u16 val;
2738};
2739
4da19633 2740static void rtl_writephy_batch(struct rtl8169_private *tp,
2741 const struct phy_reg *regs, int len)
867763c1
FR
2742{
2743 while (len-- > 0) {
4da19633 2744 rtl_writephy(tp, regs->reg, regs->val);
867763c1
FR
2745 regs++;
2746 }
2747}
2748
bca03d5f 2749#define PHY_READ 0x00000000
2750#define PHY_DATA_OR 0x10000000
2751#define PHY_DATA_AND 0x20000000
2752#define PHY_BJMPN 0x30000000
eee3786f 2753#define PHY_MDIO_CHG 0x40000000
bca03d5f 2754#define PHY_CLEAR_READCOUNT 0x70000000
2755#define PHY_WRITE 0x80000000
2756#define PHY_READCOUNT_EQ_SKIP 0x90000000
2757#define PHY_COMP_EQ_SKIPN 0xa0000000
2758#define PHY_COMP_NEQ_SKIPN 0xb0000000
2759#define PHY_WRITE_PREVIOUS 0xc0000000
2760#define PHY_SKIPN 0xd0000000
2761#define PHY_DELAY_MS 0xe0000000
bca03d5f 2762
960aee6c
HW
2763struct fw_info {
2764 u32 magic;
2765 char version[RTL_VER_SIZE];
2766 __le32 fw_start;
2767 __le32 fw_len;
2768 u8 chksum;
2769} __packed;
2770
1c361efb
FR
2771#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2772
2773static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
bca03d5f 2774{
b6ffd97f 2775 const struct firmware *fw = rtl_fw->fw;
960aee6c 2776 struct fw_info *fw_info = (struct fw_info *)fw->data;
1c361efb
FR
2777 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2778 char *version = rtl_fw->version;
2779 bool rc = false;
2780
2781 if (fw->size < FW_OPCODE_SIZE)
2782 goto out;
960aee6c
HW
2783
2784 if (!fw_info->magic) {
2785 size_t i, size, start;
2786 u8 checksum = 0;
2787
2788 if (fw->size < sizeof(*fw_info))
2789 goto out;
2790
2791 for (i = 0; i < fw->size; i++)
2792 checksum += fw->data[i];
2793 if (checksum != 0)
2794 goto out;
2795
2796 start = le32_to_cpu(fw_info->fw_start);
2797 if (start > fw->size)
2798 goto out;
2799
2800 size = le32_to_cpu(fw_info->fw_len);
2801 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2802 goto out;
2803
2804 memcpy(version, fw_info->version, RTL_VER_SIZE);
2805
2806 pa->code = (__le32 *)(fw->data + start);
2807 pa->size = size;
2808 } else {
1c361efb
FR
2809 if (fw->size % FW_OPCODE_SIZE)
2810 goto out;
2811
2812 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
2813
2814 pa->code = (__le32 *)fw->data;
2815 pa->size = fw->size / FW_OPCODE_SIZE;
2816 }
2817 version[RTL_VER_SIZE - 1] = 0;
2818
2819 rc = true;
2820out:
2821 return rc;
2822}
2823
fd112f2e
FR
2824static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2825 struct rtl_fw_phy_action *pa)
1c361efb 2826{
fd112f2e 2827 bool rc = false;
1c361efb 2828 size_t index;
bca03d5f 2829
1c361efb
FR
2830 for (index = 0; index < pa->size; index++) {
2831 u32 action = le32_to_cpu(pa->code[index]);
42b82dc1 2832 u32 regno = (action & 0x0fff0000) >> 16;
bca03d5f 2833
42b82dc1 2834 switch(action & 0xf0000000) {
2835 case PHY_READ:
2836 case PHY_DATA_OR:
2837 case PHY_DATA_AND:
eee3786f 2838 case PHY_MDIO_CHG:
42b82dc1 2839 case PHY_CLEAR_READCOUNT:
2840 case PHY_WRITE:
2841 case PHY_WRITE_PREVIOUS:
2842 case PHY_DELAY_MS:
2843 break;
2844
2845 case PHY_BJMPN:
2846 if (regno > index) {
fd112f2e 2847 netif_err(tp, ifup, tp->dev,
cecb5fd7 2848 "Out of range of firmware\n");
fd112f2e 2849 goto out;
42b82dc1 2850 }
2851 break;
2852 case PHY_READCOUNT_EQ_SKIP:
1c361efb 2853 if (index + 2 >= pa->size) {
fd112f2e 2854 netif_err(tp, ifup, tp->dev,
cecb5fd7 2855 "Out of range of firmware\n");
fd112f2e 2856 goto out;
42b82dc1 2857 }
2858 break;
2859 case PHY_COMP_EQ_SKIPN:
2860 case PHY_COMP_NEQ_SKIPN:
2861 case PHY_SKIPN:
1c361efb 2862 if (index + 1 + regno >= pa->size) {
fd112f2e 2863 netif_err(tp, ifup, tp->dev,
cecb5fd7 2864 "Out of range of firmware\n");
fd112f2e 2865 goto out;
42b82dc1 2866 }
bca03d5f 2867 break;
2868
42b82dc1 2869 default:
fd112f2e 2870 netif_err(tp, ifup, tp->dev,
42b82dc1 2871 "Invalid action 0x%08x\n", action);
fd112f2e 2872 goto out;
bca03d5f 2873 }
2874 }
fd112f2e
FR
2875 rc = true;
2876out:
2877 return rc;
2878}
bca03d5f 2879
fd112f2e
FR
2880static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2881{
2882 struct net_device *dev = tp->dev;
2883 int rc = -EINVAL;
2884
2885 if (!rtl_fw_format_ok(tp, rtl_fw)) {
5c2d2b14 2886 netif_err(tp, ifup, dev, "invalid firmware\n");
fd112f2e
FR
2887 goto out;
2888 }
2889
2890 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2891 rc = 0;
2892out:
2893 return rc;
2894}
2895
2896static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2897{
2898 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
eee3786f 2899 struct mdio_ops org, *ops = &tp->mdio_ops;
fd112f2e
FR
2900 u32 predata, count;
2901 size_t index;
2902
2903 predata = count = 0;
eee3786f 2904 org.write = ops->write;
2905 org.read = ops->read;
42b82dc1 2906
1c361efb
FR
2907 for (index = 0; index < pa->size; ) {
2908 u32 action = le32_to_cpu(pa->code[index]);
bca03d5f 2909 u32 data = action & 0x0000ffff;
42b82dc1 2910 u32 regno = (action & 0x0fff0000) >> 16;
2911
2912 if (!action)
2913 break;
bca03d5f 2914
2915 switch(action & 0xf0000000) {
42b82dc1 2916 case PHY_READ:
2917 predata = rtl_readphy(tp, regno);
2918 count++;
2919 index++;
2920 break;
2921 case PHY_DATA_OR:
2922 predata |= data;
2923 index++;
2924 break;
2925 case PHY_DATA_AND:
2926 predata &= data;
2927 index++;
2928 break;
2929 case PHY_BJMPN:
2930 index -= regno;
2931 break;
eee3786f 2932 case PHY_MDIO_CHG:
2933 if (data == 0) {
2934 ops->write = org.write;
2935 ops->read = org.read;
2936 } else if (data == 1) {
2937 ops->write = mac_mcu_write;
2938 ops->read = mac_mcu_read;
2939 }
2940
42b82dc1 2941 index++;
2942 break;
2943 case PHY_CLEAR_READCOUNT:
2944 count = 0;
2945 index++;
2946 break;
bca03d5f 2947 case PHY_WRITE:
42b82dc1 2948 rtl_writephy(tp, regno, data);
2949 index++;
2950 break;
2951 case PHY_READCOUNT_EQ_SKIP:
cecb5fd7 2952 index += (count == data) ? 2 : 1;
bca03d5f 2953 break;
42b82dc1 2954 case PHY_COMP_EQ_SKIPN:
2955 if (predata == data)
2956 index += regno;
2957 index++;
2958 break;
2959 case PHY_COMP_NEQ_SKIPN:
2960 if (predata != data)
2961 index += regno;
2962 index++;
2963 break;
2964 case PHY_WRITE_PREVIOUS:
2965 rtl_writephy(tp, regno, predata);
2966 index++;
2967 break;
2968 case PHY_SKIPN:
2969 index += regno + 1;
2970 break;
2971 case PHY_DELAY_MS:
2972 mdelay(data);
2973 index++;
2974 break;
2975
bca03d5f 2976 default:
2977 BUG();
2978 }
2979 }
eee3786f 2980
2981 ops->write = org.write;
2982 ops->read = org.read;
bca03d5f 2983}
2984
f1e02ed1 2985static void rtl_release_firmware(struct rtl8169_private *tp)
2986{
b6ffd97f
FR
2987 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2988 release_firmware(tp->rtl_fw->fw);
2989 kfree(tp->rtl_fw);
2990 }
2991 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
f1e02ed1 2992}
2993
953a12cc 2994static void rtl_apply_firmware(struct rtl8169_private *tp)
f1e02ed1 2995{
b6ffd97f 2996 struct rtl_fw *rtl_fw = tp->rtl_fw;
f1e02ed1 2997
2998 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
eef63cc1 2999 if (!IS_ERR_OR_NULL(rtl_fw))
b6ffd97f 3000 rtl_phy_write_fw(tp, rtl_fw);
953a12cc
FR
3001}
3002
3003static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
3004{
3005 if (rtl_readphy(tp, reg) != val)
3006 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
3007 else
3008 rtl_apply_firmware(tp);
f1e02ed1 3009}
3010
4da19633 3011static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
1da177e4 3012{
350f7596 3013 static const struct phy_reg phy_reg_init[] = {
0b9b571d 3014 { 0x1f, 0x0001 },
3015 { 0x06, 0x006e },
3016 { 0x08, 0x0708 },
3017 { 0x15, 0x4000 },
3018 { 0x18, 0x65c7 },
1da177e4 3019
0b9b571d 3020 { 0x1f, 0x0001 },
3021 { 0x03, 0x00a1 },
3022 { 0x02, 0x0008 },
3023 { 0x01, 0x0120 },
3024 { 0x00, 0x1000 },
3025 { 0x04, 0x0800 },
3026 { 0x04, 0x0000 },
1da177e4 3027
0b9b571d 3028 { 0x03, 0xff41 },
3029 { 0x02, 0xdf60 },
3030 { 0x01, 0x0140 },
3031 { 0x00, 0x0077 },
3032 { 0x04, 0x7800 },
3033 { 0x04, 0x7000 },
3034
3035 { 0x03, 0x802f },
3036 { 0x02, 0x4f02 },
3037 { 0x01, 0x0409 },
3038 { 0x00, 0xf0f9 },
3039 { 0x04, 0x9800 },
3040 { 0x04, 0x9000 },
3041
3042 { 0x03, 0xdf01 },
3043 { 0x02, 0xdf20 },
3044 { 0x01, 0xff95 },
3045 { 0x00, 0xba00 },
3046 { 0x04, 0xa800 },
3047 { 0x04, 0xa000 },
3048
3049 { 0x03, 0xff41 },
3050 { 0x02, 0xdf20 },
3051 { 0x01, 0x0140 },
3052 { 0x00, 0x00bb },
3053 { 0x04, 0xb800 },
3054 { 0x04, 0xb000 },
3055
3056 { 0x03, 0xdf41 },
3057 { 0x02, 0xdc60 },
3058 { 0x01, 0x6340 },
3059 { 0x00, 0x007d },
3060 { 0x04, 0xd800 },
3061 { 0x04, 0xd000 },
3062
3063 { 0x03, 0xdf01 },
3064 { 0x02, 0xdf20 },
3065 { 0x01, 0x100a },
3066 { 0x00, 0xa0ff },
3067 { 0x04, 0xf800 },
3068 { 0x04, 0xf000 },
3069
3070 { 0x1f, 0x0000 },
3071 { 0x0b, 0x0000 },
3072 { 0x00, 0x9200 }
3073 };
1da177e4 3074
4da19633 3075 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1da177e4
LT
3076}
3077
4da19633 3078static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
5615d9f1 3079{
350f7596 3080 static const struct phy_reg phy_reg_init[] = {
a441d7b6
FR
3081 { 0x1f, 0x0002 },
3082 { 0x01, 0x90d0 },
3083 { 0x1f, 0x0000 }
3084 };
3085
4da19633 3086 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
5615d9f1
FR
3087}
3088
4da19633 3089static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2e955856 3090{
3091 struct pci_dev *pdev = tp->pci_dev;
2e955856 3092
ccbae55e
SS
3093 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
3094 (pdev->subsystem_device != 0xe000))
2e955856 3095 return;
3096
4da19633 3097 rtl_writephy(tp, 0x1f, 0x0001);
3098 rtl_writephy(tp, 0x10, 0xf01b);
3099 rtl_writephy(tp, 0x1f, 0x0000);
2e955856 3100}
3101
4da19633 3102static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2e955856 3103{
350f7596 3104 static const struct phy_reg phy_reg_init[] = {
2e955856 3105 { 0x1f, 0x0001 },
3106 { 0x04, 0x0000 },
3107 { 0x03, 0x00a1 },
3108 { 0x02, 0x0008 },
3109 { 0x01, 0x0120 },
3110 { 0x00, 0x1000 },
3111 { 0x04, 0x0800 },
3112 { 0x04, 0x9000 },
3113 { 0x03, 0x802f },
3114 { 0x02, 0x4f02 },
3115 { 0x01, 0x0409 },
3116 { 0x00, 0xf099 },
3117 { 0x04, 0x9800 },
3118 { 0x04, 0xa000 },
3119 { 0x03, 0xdf01 },
3120 { 0x02, 0xdf20 },
3121 { 0x01, 0xff95 },
3122 { 0x00, 0xba00 },
3123 { 0x04, 0xa800 },
3124 { 0x04, 0xf000 },
3125 { 0x03, 0xdf01 },
3126 { 0x02, 0xdf20 },
3127 { 0x01, 0x101a },
3128 { 0x00, 0xa0ff },
3129 { 0x04, 0xf800 },
3130 { 0x04, 0x0000 },
3131 { 0x1f, 0x0000 },
3132
3133 { 0x1f, 0x0001 },
3134 { 0x10, 0xf41b },
3135 { 0x14, 0xfb54 },
3136 { 0x18, 0xf5c7 },
3137 { 0x1f, 0x0000 },
3138
3139 { 0x1f, 0x0001 },
3140 { 0x17, 0x0cc0 },
3141 { 0x1f, 0x0000 }
3142 };
3143
4da19633 3144 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2e955856 3145
4da19633 3146 rtl8169scd_hw_phy_config_quirk(tp);
2e955856 3147}
3148
4da19633 3149static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
8c7006aa 3150{
350f7596 3151 static const struct phy_reg phy_reg_init[] = {
8c7006aa 3152 { 0x1f, 0x0001 },
3153 { 0x04, 0x0000 },
3154 { 0x03, 0x00a1 },
3155 { 0x02, 0x0008 },
3156 { 0x01, 0x0120 },
3157 { 0x00, 0x1000 },
3158 { 0x04, 0x0800 },
3159 { 0x04, 0x9000 },
3160 { 0x03, 0x802f },
3161 { 0x02, 0x4f02 },
3162 { 0x01, 0x0409 },
3163 { 0x00, 0xf099 },
3164 { 0x04, 0x9800 },
3165 { 0x04, 0xa000 },
3166 { 0x03, 0xdf01 },
3167 { 0x02, 0xdf20 },
3168 { 0x01, 0xff95 },
3169 { 0x00, 0xba00 },
3170 { 0x04, 0xa800 },
3171 { 0x04, 0xf000 },
3172 { 0x03, 0xdf01 },
3173 { 0x02, 0xdf20 },
3174 { 0x01, 0x101a },
3175 { 0x00, 0xa0ff },
3176 { 0x04, 0xf800 },
3177 { 0x04, 0x0000 },
3178 { 0x1f, 0x0000 },
3179
3180 { 0x1f, 0x0001 },
3181 { 0x0b, 0x8480 },
3182 { 0x1f, 0x0000 },
3183
3184 { 0x1f, 0x0001 },
3185 { 0x18, 0x67c7 },
3186 { 0x04, 0x2000 },
3187 { 0x03, 0x002f },
3188 { 0x02, 0x4360 },
3189 { 0x01, 0x0109 },
3190 { 0x00, 0x3022 },
3191 { 0x04, 0x2800 },
3192 { 0x1f, 0x0000 },
3193
3194 { 0x1f, 0x0001 },
3195 { 0x17, 0x0cc0 },
3196 { 0x1f, 0x0000 }
3197 };
3198
4da19633 3199 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
8c7006aa 3200}
3201
4da19633 3202static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
236b8082 3203{
350f7596 3204 static const struct phy_reg phy_reg_init[] = {
236b8082
FR
3205 { 0x10, 0xf41b },
3206 { 0x1f, 0x0000 }
3207 };
3208
4da19633 3209 rtl_writephy(tp, 0x1f, 0x0001);
3210 rtl_patchphy(tp, 0x16, 1 << 0);
236b8082 3211
4da19633 3212 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
236b8082
FR
3213}
3214
4da19633 3215static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
236b8082 3216{
350f7596 3217 static const struct phy_reg phy_reg_init[] = {
236b8082
FR
3218 { 0x1f, 0x0001 },
3219 { 0x10, 0xf41b },
3220 { 0x1f, 0x0000 }
3221 };
3222
4da19633 3223 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
236b8082
FR
3224}
3225
4da19633 3226static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
867763c1 3227{
350f7596 3228 static const struct phy_reg phy_reg_init[] = {
867763c1
FR
3229 { 0x1f, 0x0000 },
3230 { 0x1d, 0x0f00 },
3231 { 0x1f, 0x0002 },
3232 { 0x0c, 0x1ec8 },
3233 { 0x1f, 0x0000 }
3234 };
3235
4da19633 3236 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
867763c1
FR
3237}
3238
4da19633 3239static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
ef3386f0 3240{
350f7596 3241 static const struct phy_reg phy_reg_init[] = {
ef3386f0
FR
3242 { 0x1f, 0x0001 },
3243 { 0x1d, 0x3d98 },
3244 { 0x1f, 0x0000 }
3245 };
3246
4da19633 3247 rtl_writephy(tp, 0x1f, 0x0000);
3248 rtl_patchphy(tp, 0x14, 1 << 5);
3249 rtl_patchphy(tp, 0x0d, 1 << 5);
ef3386f0 3250
4da19633 3251 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
ef3386f0
FR
3252}
3253
4da19633 3254static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
867763c1 3255{
350f7596 3256 static const struct phy_reg phy_reg_init[] = {
a3f80671
FR
3257 { 0x1f, 0x0001 },
3258 { 0x12, 0x2300 },
867763c1
FR
3259 { 0x1f, 0x0002 },
3260 { 0x00, 0x88d4 },
3261 { 0x01, 0x82b1 },
3262 { 0x03, 0x7002 },
3263 { 0x08, 0x9e30 },
3264 { 0x09, 0x01f0 },
3265 { 0x0a, 0x5500 },
3266 { 0x0c, 0x00c8 },
3267 { 0x1f, 0x0003 },
3268 { 0x12, 0xc096 },
3269 { 0x16, 0x000a },
f50d4275
FR
3270 { 0x1f, 0x0000 },
3271 { 0x1f, 0x0000 },
3272 { 0x09, 0x2000 },
3273 { 0x09, 0x0000 }
867763c1
FR
3274 };
3275
4da19633 3276 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
f50d4275 3277
4da19633 3278 rtl_patchphy(tp, 0x14, 1 << 5);
3279 rtl_patchphy(tp, 0x0d, 1 << 5);
3280 rtl_writephy(tp, 0x1f, 0x0000);
867763c1
FR
3281}
3282
4da19633 3283static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
7da97ec9 3284{
350f7596 3285 static const struct phy_reg phy_reg_init[] = {
f50d4275 3286 { 0x1f, 0x0001 },
7da97ec9 3287 { 0x12, 0x2300 },
f50d4275
FR
3288 { 0x03, 0x802f },
3289 { 0x02, 0x4f02 },
3290 { 0x01, 0x0409 },
3291 { 0x00, 0xf099 },
3292 { 0x04, 0x9800 },
3293 { 0x04, 0x9000 },
3294 { 0x1d, 0x3d98 },
7da97ec9
FR
3295 { 0x1f, 0x0002 },
3296 { 0x0c, 0x7eb8 },
f50d4275
FR
3297 { 0x06, 0x0761 },
3298 { 0x1f, 0x0003 },
3299 { 0x16, 0x0f0a },
7da97ec9
FR
3300 { 0x1f, 0x0000 }
3301 };
3302
4da19633 3303 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
f50d4275 3304
4da19633 3305 rtl_patchphy(tp, 0x16, 1 << 0);
3306 rtl_patchphy(tp, 0x14, 1 << 5);
3307 rtl_patchphy(tp, 0x0d, 1 << 5);
3308 rtl_writephy(tp, 0x1f, 0x0000);
7da97ec9
FR
3309}
3310
4da19633 3311static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
197ff761 3312{
350f7596 3313 static const struct phy_reg phy_reg_init[] = {
197ff761
FR
3314 { 0x1f, 0x0001 },
3315 { 0x12, 0x2300 },
3316 { 0x1d, 0x3d98 },
3317 { 0x1f, 0x0002 },
3318 { 0x0c, 0x7eb8 },
3319 { 0x06, 0x5461 },
3320 { 0x1f, 0x0003 },
3321 { 0x16, 0x0f0a },
3322 { 0x1f, 0x0000 }
3323 };
3324
4da19633 3325 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
197ff761 3326
4da19633 3327 rtl_patchphy(tp, 0x16, 1 << 0);
3328 rtl_patchphy(tp, 0x14, 1 << 5);
3329 rtl_patchphy(tp, 0x0d, 1 << 5);
3330 rtl_writephy(tp, 0x1f, 0x0000);
197ff761
FR
3331}
3332
4da19633 3333static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
6fb07058 3334{
4da19633 3335 rtl8168c_3_hw_phy_config(tp);
6fb07058
FR
3336}
3337
bca03d5f 3338static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
5b538df9 3339{
350f7596 3340 static const struct phy_reg phy_reg_init_0[] = {
bca03d5f 3341 /* Channel Estimation */
5b538df9 3342 { 0x1f, 0x0001 },
daf9df6d 3343 { 0x06, 0x4064 },
3344 { 0x07, 0x2863 },
3345 { 0x08, 0x059c },
3346 { 0x09, 0x26b4 },
3347 { 0x0a, 0x6a19 },
3348 { 0x0b, 0xdcc8 },
3349 { 0x10, 0xf06d },
3350 { 0x14, 0x7f68 },
3351 { 0x18, 0x7fd9 },
3352 { 0x1c, 0xf0ff },
3353 { 0x1d, 0x3d9c },
5b538df9 3354 { 0x1f, 0x0003 },
daf9df6d 3355 { 0x12, 0xf49f },
3356 { 0x13, 0x070b },
3357 { 0x1a, 0x05ad },
bca03d5f 3358 { 0x14, 0x94c0 },
3359
3360 /*
3361 * Tx Error Issue
cecb5fd7 3362 * Enhance line driver power
bca03d5f 3363 */
5b538df9 3364 { 0x1f, 0x0002 },
daf9df6d 3365 { 0x06, 0x5561 },
3366 { 0x1f, 0x0005 },
3367 { 0x05, 0x8332 },
bca03d5f 3368 { 0x06, 0x5561 },
3369
3370 /*
3371 * Can not link to 1Gbps with bad cable
3372 * Decrease SNR threshold form 21.07dB to 19.04dB
3373 */
3374 { 0x1f, 0x0001 },
3375 { 0x17, 0x0cc0 },
daf9df6d 3376
5b538df9 3377 { 0x1f, 0x0000 },
bca03d5f 3378 { 0x0d, 0xf880 }
daf9df6d 3379 };
3380
4da19633 3381 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
daf9df6d 3382
bca03d5f 3383 /*
3384 * Rx Error Issue
3385 * Fine Tune Switching regulator parameter
3386 */
4da19633 3387 rtl_writephy(tp, 0x1f, 0x0002);
76564428
CHL
3388 rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
3389 rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
daf9df6d 3390
fdf6fc06 3391 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
350f7596 3392 static const struct phy_reg phy_reg_init[] = {
daf9df6d 3393 { 0x1f, 0x0002 },
3394 { 0x05, 0x669a },
3395 { 0x1f, 0x0005 },
3396 { 0x05, 0x8330 },
3397 { 0x06, 0x669a },
3398 { 0x1f, 0x0002 }
3399 };
3400 int val;
3401
4da19633 3402 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
daf9df6d 3403
4da19633 3404 val = rtl_readphy(tp, 0x0d);
daf9df6d 3405
3406 if ((val & 0x00ff) != 0x006c) {
350f7596 3407 static const u32 set[] = {
daf9df6d 3408 0x0065, 0x0066, 0x0067, 0x0068,
3409 0x0069, 0x006a, 0x006b, 0x006c
3410 };
3411 int i;
3412
4da19633 3413 rtl_writephy(tp, 0x1f, 0x0002);
daf9df6d 3414
3415 val &= 0xff00;
3416 for (i = 0; i < ARRAY_SIZE(set); i++)
4da19633 3417 rtl_writephy(tp, 0x0d, val | set[i]);
daf9df6d 3418 }
3419 } else {
350f7596 3420 static const struct phy_reg phy_reg_init[] = {
daf9df6d 3421 { 0x1f, 0x0002 },
3422 { 0x05, 0x6662 },
3423 { 0x1f, 0x0005 },
3424 { 0x05, 0x8330 },
3425 { 0x06, 0x6662 }
3426 };
3427
4da19633 3428 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
daf9df6d 3429 }
3430
bca03d5f 3431 /* RSET couple improve */
4da19633 3432 rtl_writephy(tp, 0x1f, 0x0002);
3433 rtl_patchphy(tp, 0x0d, 0x0300);
3434 rtl_patchphy(tp, 0x0f, 0x0010);
daf9df6d 3435
bca03d5f 3436 /* Fine tune PLL performance */
4da19633 3437 rtl_writephy(tp, 0x1f, 0x0002);
76564428
CHL
3438 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3439 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
daf9df6d 3440
4da19633 3441 rtl_writephy(tp, 0x1f, 0x0005);
3442 rtl_writephy(tp, 0x05, 0x001b);
953a12cc
FR
3443
3444 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
bca03d5f 3445
4da19633 3446 rtl_writephy(tp, 0x1f, 0x0000);
daf9df6d 3447}
3448
bca03d5f 3449static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
daf9df6d 3450{
350f7596 3451 static const struct phy_reg phy_reg_init_0[] = {
bca03d5f 3452 /* Channel Estimation */
daf9df6d 3453 { 0x1f, 0x0001 },
3454 { 0x06, 0x4064 },
3455 { 0x07, 0x2863 },
3456 { 0x08, 0x059c },
3457 { 0x09, 0x26b4 },
3458 { 0x0a, 0x6a19 },
3459 { 0x0b, 0xdcc8 },
3460 { 0x10, 0xf06d },
3461 { 0x14, 0x7f68 },
3462 { 0x18, 0x7fd9 },
3463 { 0x1c, 0xf0ff },
3464 { 0x1d, 0x3d9c },
3465 { 0x1f, 0x0003 },
3466 { 0x12, 0xf49f },
3467 { 0x13, 0x070b },
3468 { 0x1a, 0x05ad },
3469 { 0x14, 0x94c0 },
3470
bca03d5f 3471 /*
3472 * Tx Error Issue
cecb5fd7 3473 * Enhance line driver power
bca03d5f 3474 */
daf9df6d 3475 { 0x1f, 0x0002 },
3476 { 0x06, 0x5561 },
3477 { 0x1f, 0x0005 },
3478 { 0x05, 0x8332 },
bca03d5f 3479 { 0x06, 0x5561 },
3480
3481 /*
3482 * Can not link to 1Gbps with bad cable
3483 * Decrease SNR threshold form 21.07dB to 19.04dB
3484 */
3485 { 0x1f, 0x0001 },
3486 { 0x17, 0x0cc0 },
daf9df6d 3487
3488 { 0x1f, 0x0000 },
bca03d5f 3489 { 0x0d, 0xf880 }
5b538df9
FR
3490 };
3491
4da19633 3492 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
5b538df9 3493
fdf6fc06 3494 if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
350f7596 3495 static const struct phy_reg phy_reg_init[] = {
daf9df6d 3496 { 0x1f, 0x0002 },
3497 { 0x05, 0x669a },
5b538df9 3498 { 0x1f, 0x0005 },
daf9df6d 3499 { 0x05, 0x8330 },
3500 { 0x06, 0x669a },
3501
3502 { 0x1f, 0x0002 }
3503 };
3504 int val;
3505
4da19633 3506 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
daf9df6d 3507
4da19633 3508 val = rtl_readphy(tp, 0x0d);
daf9df6d 3509 if ((val & 0x00ff) != 0x006c) {
b6bc7650 3510 static const u32 set[] = {
daf9df6d 3511 0x0065, 0x0066, 0x0067, 0x0068,
3512 0x0069, 0x006a, 0x006b, 0x006c
3513 };
3514 int i;
3515
4da19633 3516 rtl_writephy(tp, 0x1f, 0x0002);
daf9df6d 3517
3518 val &= 0xff00;
3519 for (i = 0; i < ARRAY_SIZE(set); i++)
4da19633 3520 rtl_writephy(tp, 0x0d, val | set[i]);
daf9df6d 3521 }
3522 } else {
350f7596 3523 static const struct phy_reg phy_reg_init[] = {
daf9df6d 3524 { 0x1f, 0x0002 },
3525 { 0x05, 0x2642 },
5b538df9 3526 { 0x1f, 0x0005 },
daf9df6d 3527 { 0x05, 0x8330 },
3528 { 0x06, 0x2642 }
5b538df9
FR
3529 };
3530
4da19633 3531 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
5b538df9
FR
3532 }
3533
bca03d5f 3534 /* Fine tune PLL performance */
4da19633 3535 rtl_writephy(tp, 0x1f, 0x0002);
76564428
CHL
3536 rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3537 rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
daf9df6d 3538
bca03d5f 3539 /* Switching regulator Slew rate */
4da19633 3540 rtl_writephy(tp, 0x1f, 0x0002);
3541 rtl_patchphy(tp, 0x0f, 0x0017);
daf9df6d 3542
4da19633 3543 rtl_writephy(tp, 0x1f, 0x0005);
3544 rtl_writephy(tp, 0x05, 0x001b);
953a12cc
FR
3545
3546 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
bca03d5f 3547
4da19633 3548 rtl_writephy(tp, 0x1f, 0x0000);
daf9df6d 3549}
3550
4da19633 3551static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
daf9df6d 3552{
350f7596 3553 static const struct phy_reg phy_reg_init[] = {
daf9df6d 3554 { 0x1f, 0x0002 },
3555 { 0x10, 0x0008 },
3556 { 0x0d, 0x006c },
3557
3558 { 0x1f, 0x0000 },
3559 { 0x0d, 0xf880 },
3560
3561 { 0x1f, 0x0001 },
3562 { 0x17, 0x0cc0 },
3563
3564 { 0x1f, 0x0001 },
3565 { 0x0b, 0xa4d8 },
3566 { 0x09, 0x281c },
3567 { 0x07, 0x2883 },
3568 { 0x0a, 0x6b35 },
3569 { 0x1d, 0x3da4 },
3570 { 0x1c, 0xeffd },
3571 { 0x14, 0x7f52 },
3572 { 0x18, 0x7fc6 },
3573 { 0x08, 0x0601 },
3574 { 0x06, 0x4063 },
3575 { 0x10, 0xf074 },
3576 { 0x1f, 0x0003 },
3577 { 0x13, 0x0789 },
3578 { 0x12, 0xf4bd },
3579 { 0x1a, 0x04fd },
3580 { 0x14, 0x84b0 },
3581 { 0x1f, 0x0000 },
3582 { 0x00, 0x9200 },
3583
3584 { 0x1f, 0x0005 },
3585 { 0x01, 0x0340 },
3586 { 0x1f, 0x0001 },
3587 { 0x04, 0x4000 },
3588 { 0x03, 0x1d21 },
3589 { 0x02, 0x0c32 },
3590 { 0x01, 0x0200 },
3591 { 0x00, 0x5554 },
3592 { 0x04, 0x4800 },
3593 { 0x04, 0x4000 },
3594 { 0x04, 0xf000 },
3595 { 0x03, 0xdf01 },
3596 { 0x02, 0xdf20 },
3597 { 0x01, 0x101a },
3598 { 0x00, 0xa0ff },
3599 { 0x04, 0xf800 },
3600 { 0x04, 0xf000 },
3601 { 0x1f, 0x0000 },
3602
3603 { 0x1f, 0x0007 },
3604 { 0x1e, 0x0023 },
3605 { 0x16, 0x0000 },
3606 { 0x1f, 0x0000 }
3607 };
3608
4da19633 3609 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
5b538df9
FR
3610}
3611
e6de30d6 3612static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3613{
3614 static const struct phy_reg phy_reg_init[] = {
3615 { 0x1f, 0x0001 },
3616 { 0x17, 0x0cc0 },
3617
3618 { 0x1f, 0x0007 },
3619 { 0x1e, 0x002d },
3620 { 0x18, 0x0040 },
3621 { 0x1f, 0x0000 }
3622 };
3623
3624 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3625 rtl_patchphy(tp, 0x0d, 1 << 5);
3626}
3627
70090424 3628static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
01dc7fec 3629{
3630 static const struct phy_reg phy_reg_init[] = {
3631 /* Enable Delay cap */
3632 { 0x1f, 0x0005 },
3633 { 0x05, 0x8b80 },
3634 { 0x06, 0xc896 },
3635 { 0x1f, 0x0000 },
3636
3637 /* Channel estimation fine tune */
3638 { 0x1f, 0x0001 },
3639 { 0x0b, 0x6c20 },
3640 { 0x07, 0x2872 },
3641 { 0x1c, 0xefff },
3642 { 0x1f, 0x0003 },
3643 { 0x14, 0x6420 },
3644 { 0x1f, 0x0000 },
3645
3646 /* Update PFM & 10M TX idle timer */
3647 { 0x1f, 0x0007 },
3648 { 0x1e, 0x002f },
3649 { 0x15, 0x1919 },
3650 { 0x1f, 0x0000 },
3651
3652 { 0x1f, 0x0007 },
3653 { 0x1e, 0x00ac },
3654 { 0x18, 0x0006 },
3655 { 0x1f, 0x0000 }
3656 };
3657
15ecd039
FR
3658 rtl_apply_firmware(tp);
3659
01dc7fec 3660 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3661
3662 /* DCO enable for 10M IDLE Power */
3663 rtl_writephy(tp, 0x1f, 0x0007);
3664 rtl_writephy(tp, 0x1e, 0x0023);
76564428 3665 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
01dc7fec 3666 rtl_writephy(tp, 0x1f, 0x0000);
3667
3668 /* For impedance matching */
3669 rtl_writephy(tp, 0x1f, 0x0002);
76564428 3670 rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
cecb5fd7 3671 rtl_writephy(tp, 0x1f, 0x0000);
01dc7fec 3672
3673 /* PHY auto speed down */
3674 rtl_writephy(tp, 0x1f, 0x0007);
3675 rtl_writephy(tp, 0x1e, 0x002d);
76564428 3676 rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
01dc7fec 3677 rtl_writephy(tp, 0x1f, 0x0000);
76564428 3678 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
01dc7fec 3679
3680 rtl_writephy(tp, 0x1f, 0x0005);
3681 rtl_writephy(tp, 0x05, 0x8b86);
76564428 3682 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
01dc7fec 3683 rtl_writephy(tp, 0x1f, 0x0000);
3684
3685 rtl_writephy(tp, 0x1f, 0x0005);
3686 rtl_writephy(tp, 0x05, 0x8b85);
76564428 3687 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
01dc7fec 3688 rtl_writephy(tp, 0x1f, 0x0007);
3689 rtl_writephy(tp, 0x1e, 0x0020);
76564428 3690 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
01dc7fec 3691 rtl_writephy(tp, 0x1f, 0x0006);
3692 rtl_writephy(tp, 0x00, 0x5a00);
3693 rtl_writephy(tp, 0x1f, 0x0000);
3694 rtl_writephy(tp, 0x0d, 0x0007);
3695 rtl_writephy(tp, 0x0e, 0x003c);
3696 rtl_writephy(tp, 0x0d, 0x4007);
3697 rtl_writephy(tp, 0x0e, 0x0000);
3698 rtl_writephy(tp, 0x0d, 0x0000);
3699}
3700
9ecb9aab 3701static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3702{
3703 const u16 w[] = {
3704 addr[0] | (addr[1] << 8),
3705 addr[2] | (addr[3] << 8),
3706 addr[4] | (addr[5] << 8)
3707 };
3708 const struct exgmac_reg e[] = {
3709 { .addr = 0xe0, ERIAR_MASK_1111, .val = w[0] | (w[1] << 16) },
3710 { .addr = 0xe4, ERIAR_MASK_1111, .val = w[2] },
3711 { .addr = 0xf0, ERIAR_MASK_1111, .val = w[0] << 16 },
3712 { .addr = 0xf4, ERIAR_MASK_1111, .val = w[1] | (w[2] << 16) }
3713 };
3714
3715 rtl_write_exgmac_batch(tp, e, ARRAY_SIZE(e));
3716}
3717
70090424
HW
3718static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3719{
3720 static const struct phy_reg phy_reg_init[] = {
3721 /* Enable Delay cap */
3722 { 0x1f, 0x0004 },
3723 { 0x1f, 0x0007 },
3724 { 0x1e, 0x00ac },
3725 { 0x18, 0x0006 },
3726 { 0x1f, 0x0002 },
3727 { 0x1f, 0x0000 },
3728 { 0x1f, 0x0000 },
3729
3730 /* Channel estimation fine tune */
3731 { 0x1f, 0x0003 },
3732 { 0x09, 0xa20f },
3733 { 0x1f, 0x0000 },
3734 { 0x1f, 0x0000 },
3735
3736 /* Green Setting */
3737 { 0x1f, 0x0005 },
3738 { 0x05, 0x8b5b },
3739 { 0x06, 0x9222 },
3740 { 0x05, 0x8b6d },
3741 { 0x06, 0x8000 },
3742 { 0x05, 0x8b76 },
3743 { 0x06, 0x8000 },
3744 { 0x1f, 0x0000 }
3745 };
3746
3747 rtl_apply_firmware(tp);
3748
3749 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3750
3751 /* For 4-corner performance improve */
3752 rtl_writephy(tp, 0x1f, 0x0005);
3753 rtl_writephy(tp, 0x05, 0x8b80);
76564428 3754 rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
70090424
HW
3755 rtl_writephy(tp, 0x1f, 0x0000);
3756
3757 /* PHY auto speed down */
3758 rtl_writephy(tp, 0x1f, 0x0004);
3759 rtl_writephy(tp, 0x1f, 0x0007);
3760 rtl_writephy(tp, 0x1e, 0x002d);
76564428 3761 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
70090424
HW
3762 rtl_writephy(tp, 0x1f, 0x0002);
3763 rtl_writephy(tp, 0x1f, 0x0000);
76564428 3764 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
70090424
HW
3765
3766 /* improve 10M EEE waveform */
3767 rtl_writephy(tp, 0x1f, 0x0005);
3768 rtl_writephy(tp, 0x05, 0x8b86);
76564428 3769 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
70090424
HW
3770 rtl_writephy(tp, 0x1f, 0x0000);
3771
3772 /* Improve 2-pair detection performance */
3773 rtl_writephy(tp, 0x1f, 0x0005);
3774 rtl_writephy(tp, 0x05, 0x8b85);
76564428 3775 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
70090424
HW
3776 rtl_writephy(tp, 0x1f, 0x0000);
3777
3778 /* EEE setting */
1814d6a8 3779 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_1111, 0x0003, 0x0000, ERIAR_EXGMAC);
70090424
HW
3780 rtl_writephy(tp, 0x1f, 0x0005);
3781 rtl_writephy(tp, 0x05, 0x8b85);
1814d6a8 3782 rtl_w0w1_phy(tp, 0x06, 0x2000, 0x0000);
70090424
HW
3783 rtl_writephy(tp, 0x1f, 0x0004);
3784 rtl_writephy(tp, 0x1f, 0x0007);
3785 rtl_writephy(tp, 0x1e, 0x0020);
1814d6a8 3786 rtl_w0w1_phy(tp, 0x15, 0x0100, 0x0000);
70090424
HW
3787 rtl_writephy(tp, 0x1f, 0x0002);
3788 rtl_writephy(tp, 0x1f, 0x0000);
3789 rtl_writephy(tp, 0x0d, 0x0007);
3790 rtl_writephy(tp, 0x0e, 0x003c);
3791 rtl_writephy(tp, 0x0d, 0x4007);
1814d6a8 3792 rtl_writephy(tp, 0x0e, 0x0006);
70090424
HW
3793 rtl_writephy(tp, 0x0d, 0x0000);
3794
3795 /* Green feature */
3796 rtl_writephy(tp, 0x1f, 0x0003);
1814d6a8
HK
3797 rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3798 rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
70090424 3799 rtl_writephy(tp, 0x1f, 0x0000);
b399a394
HK
3800 rtl_writephy(tp, 0x1f, 0x0005);
3801 rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3802 rtl_writephy(tp, 0x1f, 0x0000);
e0c07557 3803
9ecb9aab 3804 /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3805 rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
70090424
HW
3806}
3807
5f886e08
HW
3808static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3809{
3810 /* For 4-corner performance improve */
3811 rtl_writephy(tp, 0x1f, 0x0005);
3812 rtl_writephy(tp, 0x05, 0x8b80);
76564428 3813 rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
5f886e08
HW
3814 rtl_writephy(tp, 0x1f, 0x0000);
3815
3816 /* PHY auto speed down */
3817 rtl_writephy(tp, 0x1f, 0x0007);
3818 rtl_writephy(tp, 0x1e, 0x002d);
76564428 3819 rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
5f886e08 3820 rtl_writephy(tp, 0x1f, 0x0000);
76564428 3821 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
5f886e08
HW
3822
3823 /* Improve 10M EEE waveform */
3824 rtl_writephy(tp, 0x1f, 0x0005);
3825 rtl_writephy(tp, 0x05, 0x8b86);
76564428 3826 rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
5f886e08
HW
3827 rtl_writephy(tp, 0x1f, 0x0000);
3828}
3829
c2218925
HW
3830static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3831{
3832 static const struct phy_reg phy_reg_init[] = {
3833 /* Channel estimation fine tune */
3834 { 0x1f, 0x0003 },
3835 { 0x09, 0xa20f },
3836 { 0x1f, 0x0000 },
3837
3838 /* Modify green table for giga & fnet */
3839 { 0x1f, 0x0005 },
3840 { 0x05, 0x8b55 },
3841 { 0x06, 0x0000 },
3842 { 0x05, 0x8b5e },
3843 { 0x06, 0x0000 },
3844 { 0x05, 0x8b67 },
3845 { 0x06, 0x0000 },
3846 { 0x05, 0x8b70 },
3847 { 0x06, 0x0000 },
3848 { 0x1f, 0x0000 },
3849 { 0x1f, 0x0007 },
3850 { 0x1e, 0x0078 },
3851 { 0x17, 0x0000 },
3852 { 0x19, 0x00fb },
3853 { 0x1f, 0x0000 },
3854
3855 /* Modify green table for 10M */
3856 { 0x1f, 0x0005 },
3857 { 0x05, 0x8b79 },
3858 { 0x06, 0xaa00 },
3859 { 0x1f, 0x0000 },
3860
3861 /* Disable hiimpedance detection (RTCT) */
3862 { 0x1f, 0x0003 },
3863 { 0x01, 0x328a },
3864 { 0x1f, 0x0000 }
3865 };
3866
3867 rtl_apply_firmware(tp);
3868
3869 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3870
5f886e08 3871 rtl8168f_hw_phy_config(tp);
c2218925
HW
3872
3873 /* Improve 2-pair detection performance */
3874 rtl_writephy(tp, 0x1f, 0x0005);
3875 rtl_writephy(tp, 0x05, 0x8b85);
76564428 3876 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
c2218925
HW
3877 rtl_writephy(tp, 0x1f, 0x0000);
3878}
3879
3880static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3881{
3882 rtl_apply_firmware(tp);
3883
5f886e08 3884 rtl8168f_hw_phy_config(tp);
c2218925
HW
3885}
3886
b3d7b2f2
HW
3887static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3888{
b3d7b2f2
HW
3889 static const struct phy_reg phy_reg_init[] = {
3890 /* Channel estimation fine tune */
3891 { 0x1f, 0x0003 },
3892 { 0x09, 0xa20f },
3893 { 0x1f, 0x0000 },
3894
3895 /* Modify green table for giga & fnet */
3896 { 0x1f, 0x0005 },
3897 { 0x05, 0x8b55 },
3898 { 0x06, 0x0000 },
3899 { 0x05, 0x8b5e },
3900 { 0x06, 0x0000 },
3901 { 0x05, 0x8b67 },
3902 { 0x06, 0x0000 },
3903 { 0x05, 0x8b70 },
3904 { 0x06, 0x0000 },
3905 { 0x1f, 0x0000 },
3906 { 0x1f, 0x0007 },
3907 { 0x1e, 0x0078 },
3908 { 0x17, 0x0000 },
3909 { 0x19, 0x00aa },
3910 { 0x1f, 0x0000 },
3911
3912 /* Modify green table for 10M */
3913 { 0x1f, 0x0005 },
3914 { 0x05, 0x8b79 },
3915 { 0x06, 0xaa00 },
3916 { 0x1f, 0x0000 },
3917
3918 /* Disable hiimpedance detection (RTCT) */
3919 { 0x1f, 0x0003 },
3920 { 0x01, 0x328a },
3921 { 0x1f, 0x0000 }
3922 };
3923
3924
3925 rtl_apply_firmware(tp);
3926
3927 rtl8168f_hw_phy_config(tp);
3928
3929 /* Improve 2-pair detection performance */
3930 rtl_writephy(tp, 0x1f, 0x0005);
3931 rtl_writephy(tp, 0x05, 0x8b85);
76564428 3932 rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
b3d7b2f2
HW
3933 rtl_writephy(tp, 0x1f, 0x0000);
3934
3935 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
3936
3937 /* Modify green table for giga */
3938 rtl_writephy(tp, 0x1f, 0x0005);
3939 rtl_writephy(tp, 0x05, 0x8b54);
76564428 3940 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
b3d7b2f2 3941 rtl_writephy(tp, 0x05, 0x8b5d);
76564428 3942 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
b3d7b2f2 3943 rtl_writephy(tp, 0x05, 0x8a7c);
76564428 3944 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
b3d7b2f2 3945 rtl_writephy(tp, 0x05, 0x8a7f);
76564428 3946 rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
b3d7b2f2 3947 rtl_writephy(tp, 0x05, 0x8a82);
76564428 3948 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
b3d7b2f2 3949 rtl_writephy(tp, 0x05, 0x8a85);
76564428 3950 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
b3d7b2f2 3951 rtl_writephy(tp, 0x05, 0x8a88);
76564428 3952 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
b3d7b2f2
HW
3953 rtl_writephy(tp, 0x1f, 0x0000);
3954
3955 /* uc same-seed solution */
3956 rtl_writephy(tp, 0x1f, 0x0005);
3957 rtl_writephy(tp, 0x05, 0x8b85);
76564428 3958 rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
b3d7b2f2
HW
3959 rtl_writephy(tp, 0x1f, 0x0000);
3960
3961 /* eee setting */
706123d0 3962 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x00, 0x03, ERIAR_EXGMAC);
b3d7b2f2
HW
3963 rtl_writephy(tp, 0x1f, 0x0005);
3964 rtl_writephy(tp, 0x05, 0x8b85);
76564428 3965 rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
b3d7b2f2
HW
3966 rtl_writephy(tp, 0x1f, 0x0004);
3967 rtl_writephy(tp, 0x1f, 0x0007);
3968 rtl_writephy(tp, 0x1e, 0x0020);
76564428 3969 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0100);
b3d7b2f2
HW
3970 rtl_writephy(tp, 0x1f, 0x0000);
3971 rtl_writephy(tp, 0x0d, 0x0007);
3972 rtl_writephy(tp, 0x0e, 0x003c);
3973 rtl_writephy(tp, 0x0d, 0x4007);
3974 rtl_writephy(tp, 0x0e, 0x0000);
3975 rtl_writephy(tp, 0x0d, 0x0000);
3976
3977 /* Green feature */
3978 rtl_writephy(tp, 0x1f, 0x0003);
76564428
CHL
3979 rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3980 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
b3d7b2f2
HW
3981 rtl_writephy(tp, 0x1f, 0x0000);
3982}
3983
c558386b
HW
3984static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3985{
c558386b
HW
3986 rtl_apply_firmware(tp);
3987
41f44d13 3988 rtl_writephy(tp, 0x1f, 0x0a46);
3989 if (rtl_readphy(tp, 0x10) & 0x0100) {
3990 rtl_writephy(tp, 0x1f, 0x0bcc);
76564428 3991 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
41f44d13 3992 } else {
3993 rtl_writephy(tp, 0x1f, 0x0bcc);
76564428 3994 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
41f44d13 3995 }
c558386b 3996
41f44d13 3997 rtl_writephy(tp, 0x1f, 0x0a46);
3998 if (rtl_readphy(tp, 0x13) & 0x0100) {
3999 rtl_writephy(tp, 0x1f, 0x0c41);
76564428 4000 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
41f44d13 4001 } else {
fe7524c0 4002 rtl_writephy(tp, 0x1f, 0x0c41);
76564428 4003 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
41f44d13 4004 }
c558386b 4005
41f44d13 4006 /* Enable PHY auto speed down */
4007 rtl_writephy(tp, 0x1f, 0x0a44);
76564428 4008 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
c558386b 4009
fe7524c0 4010 rtl_writephy(tp, 0x1f, 0x0bcc);
76564428 4011 rtl_w0w1_phy(tp, 0x14, 0x0100, 0x0000);
fe7524c0 4012 rtl_writephy(tp, 0x1f, 0x0a44);
76564428 4013 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
fe7524c0 4014 rtl_writephy(tp, 0x1f, 0x0a43);
4015 rtl_writephy(tp, 0x13, 0x8084);
76564428
CHL
4016 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4017 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
fe7524c0 4018
41f44d13 4019 /* EEE auto-fallback function */
4020 rtl_writephy(tp, 0x1f, 0x0a4b);
76564428 4021 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
c558386b 4022
41f44d13 4023 /* Enable UC LPF tune function */
4024 rtl_writephy(tp, 0x1f, 0x0a43);
4025 rtl_writephy(tp, 0x13, 0x8012);
76564428 4026 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
41f44d13 4027
4028 rtl_writephy(tp, 0x1f, 0x0c42);
76564428 4029 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
41f44d13 4030
fe7524c0 4031 /* Improve SWR Efficiency */
4032 rtl_writephy(tp, 0x1f, 0x0bcd);
4033 rtl_writephy(tp, 0x14, 0x5065);
4034 rtl_writephy(tp, 0x14, 0xd065);
4035 rtl_writephy(tp, 0x1f, 0x0bc8);
4036 rtl_writephy(tp, 0x11, 0x5655);
4037 rtl_writephy(tp, 0x1f, 0x0bcd);
4038 rtl_writephy(tp, 0x14, 0x1065);
4039 rtl_writephy(tp, 0x14, 0x9065);
4040 rtl_writephy(tp, 0x14, 0x1065);
4041
1bac1072
DC
4042 /* Check ALDPS bit, disable it if enabled */
4043 rtl_writephy(tp, 0x1f, 0x0a43);
4044 if (rtl_readphy(tp, 0x10) & 0x0004)
76564428 4045 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
1bac1072 4046
41f44d13 4047 rtl_writephy(tp, 0x1f, 0x0000);
c558386b
HW
4048}
4049
57538c4a 4050static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
4051{
4052 rtl_apply_firmware(tp);
4053}
4054
6e1d0b89
CHL
4055static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
4056{
4057 u16 dout_tapbin;
4058 u32 data;
4059
4060 rtl_apply_firmware(tp);
4061
4062 /* CHN EST parameters adjust - giga master */
4063 rtl_writephy(tp, 0x1f, 0x0a43);
4064 rtl_writephy(tp, 0x13, 0x809b);
76564428 4065 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
6e1d0b89 4066 rtl_writephy(tp, 0x13, 0x80a2);
76564428 4067 rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
6e1d0b89 4068 rtl_writephy(tp, 0x13, 0x80a4);
76564428 4069 rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
6e1d0b89 4070 rtl_writephy(tp, 0x13, 0x809c);
76564428 4071 rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
6e1d0b89
CHL
4072 rtl_writephy(tp, 0x1f, 0x0000);
4073
4074 /* CHN EST parameters adjust - giga slave */
4075 rtl_writephy(tp, 0x1f, 0x0a43);
4076 rtl_writephy(tp, 0x13, 0x80ad);
76564428 4077 rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
6e1d0b89 4078 rtl_writephy(tp, 0x13, 0x80b4);
76564428 4079 rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
6e1d0b89 4080 rtl_writephy(tp, 0x13, 0x80ac);
76564428 4081 rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
6e1d0b89
CHL
4082 rtl_writephy(tp, 0x1f, 0x0000);
4083
4084 /* CHN EST parameters adjust - fnet */
4085 rtl_writephy(tp, 0x1f, 0x0a43);
4086 rtl_writephy(tp, 0x13, 0x808e);
76564428 4087 rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
6e1d0b89 4088 rtl_writephy(tp, 0x13, 0x8090);
76564428 4089 rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
6e1d0b89 4090 rtl_writephy(tp, 0x13, 0x8092);
76564428 4091 rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
6e1d0b89
CHL
4092 rtl_writephy(tp, 0x1f, 0x0000);
4093
4094 /* enable R-tune & PGA-retune function */
4095 dout_tapbin = 0;
4096 rtl_writephy(tp, 0x1f, 0x0a46);
4097 data = rtl_readphy(tp, 0x13);
4098 data &= 3;
4099 data <<= 2;
4100 dout_tapbin |= data;
4101 data = rtl_readphy(tp, 0x12);
4102 data &= 0xc000;
4103 data >>= 14;
4104 dout_tapbin |= data;
4105 dout_tapbin = ~(dout_tapbin^0x08);
4106 dout_tapbin <<= 12;
4107 dout_tapbin &= 0xf000;
4108 rtl_writephy(tp, 0x1f, 0x0a43);
4109 rtl_writephy(tp, 0x13, 0x827a);
76564428 4110 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
6e1d0b89 4111 rtl_writephy(tp, 0x13, 0x827b);
76564428 4112 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
6e1d0b89 4113 rtl_writephy(tp, 0x13, 0x827c);
76564428 4114 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
6e1d0b89 4115 rtl_writephy(tp, 0x13, 0x827d);
76564428 4116 rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
6e1d0b89
CHL
4117
4118 rtl_writephy(tp, 0x1f, 0x0a43);
4119 rtl_writephy(tp, 0x13, 0x0811);
76564428 4120 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
6e1d0b89 4121 rtl_writephy(tp, 0x1f, 0x0a42);
76564428 4122 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
6e1d0b89
CHL
4123 rtl_writephy(tp, 0x1f, 0x0000);
4124
4125 /* enable GPHY 10M */
4126 rtl_writephy(tp, 0x1f, 0x0a44);
76564428 4127 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
6e1d0b89
CHL
4128 rtl_writephy(tp, 0x1f, 0x0000);
4129
4130 /* SAR ADC performance */
4131 rtl_writephy(tp, 0x1f, 0x0bca);
76564428 4132 rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
6e1d0b89
CHL
4133 rtl_writephy(tp, 0x1f, 0x0000);
4134
4135 rtl_writephy(tp, 0x1f, 0x0a43);
4136 rtl_writephy(tp, 0x13, 0x803f);
76564428 4137 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
6e1d0b89 4138 rtl_writephy(tp, 0x13, 0x8047);
76564428 4139 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
6e1d0b89 4140 rtl_writephy(tp, 0x13, 0x804f);
76564428 4141 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
6e1d0b89 4142 rtl_writephy(tp, 0x13, 0x8057);
76564428 4143 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
6e1d0b89 4144 rtl_writephy(tp, 0x13, 0x805f);
76564428 4145 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
6e1d0b89 4146 rtl_writephy(tp, 0x13, 0x8067);
76564428 4147 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
6e1d0b89 4148 rtl_writephy(tp, 0x13, 0x806f);
76564428 4149 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
6e1d0b89
CHL
4150 rtl_writephy(tp, 0x1f, 0x0000);
4151
4152 /* disable phy pfm mode */
4153 rtl_writephy(tp, 0x1f, 0x0a44);
c832c35f 4154 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
6e1d0b89
CHL
4155 rtl_writephy(tp, 0x1f, 0x0000);
4156
4157 /* Check ALDPS bit, disable it if enabled */
4158 rtl_writephy(tp, 0x1f, 0x0a43);
4159 if (rtl_readphy(tp, 0x10) & 0x0004)
76564428 4160 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
6e1d0b89
CHL
4161
4162 rtl_writephy(tp, 0x1f, 0x0000);
4163}
4164
4165static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
4166{
4167 u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
4168 u16 rlen;
4169 u32 data;
4170
4171 rtl_apply_firmware(tp);
4172
4173 /* CHIN EST parameter update */
4174 rtl_writephy(tp, 0x1f, 0x0a43);
4175 rtl_writephy(tp, 0x13, 0x808a);
76564428 4176 rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
6e1d0b89
CHL
4177 rtl_writephy(tp, 0x1f, 0x0000);
4178
4179 /* enable R-tune & PGA-retune function */
4180 rtl_writephy(tp, 0x1f, 0x0a43);
4181 rtl_writephy(tp, 0x13, 0x0811);
76564428 4182 rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
6e1d0b89 4183 rtl_writephy(tp, 0x1f, 0x0a42);
76564428 4184 rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
6e1d0b89
CHL
4185 rtl_writephy(tp, 0x1f, 0x0000);
4186
4187 /* enable GPHY 10M */
4188 rtl_writephy(tp, 0x1f, 0x0a44);
76564428 4189 rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
6e1d0b89
CHL
4190 rtl_writephy(tp, 0x1f, 0x0000);
4191
4192 r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
4193 data = r8168_mac_ocp_read(tp, 0xdd02);
4194 ioffset_p3 = ((data & 0x80)>>7);
4195 ioffset_p3 <<= 3;
4196
4197 data = r8168_mac_ocp_read(tp, 0xdd00);
4198 ioffset_p3 |= ((data & (0xe000))>>13);
4199 ioffset_p2 = ((data & (0x1e00))>>9);
4200 ioffset_p1 = ((data & (0x01e0))>>5);
4201 ioffset_p0 = ((data & 0x0010)>>4);
4202 ioffset_p0 <<= 3;
4203 ioffset_p0 |= (data & (0x07));
4204 data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
4205
05b9687b 4206 if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
e2e2788e 4207 (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
6e1d0b89
CHL
4208 rtl_writephy(tp, 0x1f, 0x0bcf);
4209 rtl_writephy(tp, 0x16, data);
4210 rtl_writephy(tp, 0x1f, 0x0000);
4211 }
4212
4213 /* Modify rlen (TX LPF corner frequency) level */
4214 rtl_writephy(tp, 0x1f, 0x0bcd);
4215 data = rtl_readphy(tp, 0x16);
4216 data &= 0x000f;
4217 rlen = 0;
4218 if (data > 3)
4219 rlen = data - 3;
4220 data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
4221 rtl_writephy(tp, 0x17, data);
4222 rtl_writephy(tp, 0x1f, 0x0bcd);
4223 rtl_writephy(tp, 0x1f, 0x0000);
4224
4225 /* disable phy pfm mode */
4226 rtl_writephy(tp, 0x1f, 0x0a44);
c832c35f 4227 rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
6e1d0b89
CHL
4228 rtl_writephy(tp, 0x1f, 0x0000);
4229
4230 /* Check ALDPS bit, disable it if enabled */
4231 rtl_writephy(tp, 0x1f, 0x0a43);
4232 if (rtl_readphy(tp, 0x10) & 0x0004)
76564428 4233 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
6e1d0b89
CHL
4234
4235 rtl_writephy(tp, 0x1f, 0x0000);
4236}
4237
935e2218
CHL
4238static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
4239{
4240 /* Enable PHY auto speed down */
4241 rtl_writephy(tp, 0x1f, 0x0a44);
4242 rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
4243 rtl_writephy(tp, 0x1f, 0x0000);
4244
4245 /* patch 10M & ALDPS */
4246 rtl_writephy(tp, 0x1f, 0x0bcc);
4247 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4248 rtl_writephy(tp, 0x1f, 0x0a44);
4249 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4250 rtl_writephy(tp, 0x1f, 0x0a43);
4251 rtl_writephy(tp, 0x13, 0x8084);
4252 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4253 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4254 rtl_writephy(tp, 0x1f, 0x0000);
4255
4256 /* Enable EEE auto-fallback function */
4257 rtl_writephy(tp, 0x1f, 0x0a4b);
4258 rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
4259 rtl_writephy(tp, 0x1f, 0x0000);
4260
4261 /* Enable UC LPF tune function */
4262 rtl_writephy(tp, 0x1f, 0x0a43);
4263 rtl_writephy(tp, 0x13, 0x8012);
4264 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4265 rtl_writephy(tp, 0x1f, 0x0000);
4266
4267 /* set rg_sel_sdm_rate */
4268 rtl_writephy(tp, 0x1f, 0x0c42);
4269 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4270 rtl_writephy(tp, 0x1f, 0x0000);
4271
4272 /* Check ALDPS bit, disable it if enabled */
4273 rtl_writephy(tp, 0x1f, 0x0a43);
4274 if (rtl_readphy(tp, 0x10) & 0x0004)
4275 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4276
4277 rtl_writephy(tp, 0x1f, 0x0000);
4278}
4279
4280static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
4281{
4282 /* patch 10M & ALDPS */
4283 rtl_writephy(tp, 0x1f, 0x0bcc);
4284 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x0100);
4285 rtl_writephy(tp, 0x1f, 0x0a44);
4286 rtl_w0w1_phy(tp, 0x11, 0x00c0, 0x0000);
4287 rtl_writephy(tp, 0x1f, 0x0a43);
4288 rtl_writephy(tp, 0x13, 0x8084);
4289 rtl_w0w1_phy(tp, 0x14, 0x0000, 0x6000);
4290 rtl_w0w1_phy(tp, 0x10, 0x1003, 0x0000);
4291 rtl_writephy(tp, 0x1f, 0x0000);
4292
4293 /* Enable UC LPF tune function */
4294 rtl_writephy(tp, 0x1f, 0x0a43);
4295 rtl_writephy(tp, 0x13, 0x8012);
4296 rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
4297 rtl_writephy(tp, 0x1f, 0x0000);
4298
4299 /* Set rg_sel_sdm_rate */
4300 rtl_writephy(tp, 0x1f, 0x0c42);
4301 rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
4302 rtl_writephy(tp, 0x1f, 0x0000);
4303
4304 /* Channel estimation parameters */
4305 rtl_writephy(tp, 0x1f, 0x0a43);
4306 rtl_writephy(tp, 0x13, 0x80f3);
4307 rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
4308 rtl_writephy(tp, 0x13, 0x80f0);
4309 rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
4310 rtl_writephy(tp, 0x13, 0x80ef);
4311 rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
4312 rtl_writephy(tp, 0x13, 0x80f6);
4313 rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
4314 rtl_writephy(tp, 0x13, 0x80ec);
4315 rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
4316 rtl_writephy(tp, 0x13, 0x80ed);
4317 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4318 rtl_writephy(tp, 0x13, 0x80f2);
4319 rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
4320 rtl_writephy(tp, 0x13, 0x80f4);
4321 rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
4322 rtl_writephy(tp, 0x1f, 0x0a43);
4323 rtl_writephy(tp, 0x13, 0x8110);
4324 rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
4325 rtl_writephy(tp, 0x13, 0x810f);
4326 rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
4327 rtl_writephy(tp, 0x13, 0x8111);
4328 rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
4329 rtl_writephy(tp, 0x13, 0x8113);
4330 rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
4331 rtl_writephy(tp, 0x13, 0x8115);
4332 rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
4333 rtl_writephy(tp, 0x13, 0x810e);
4334 rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
4335 rtl_writephy(tp, 0x13, 0x810c);
4336 rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
4337 rtl_writephy(tp, 0x13, 0x810b);
4338 rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
4339 rtl_writephy(tp, 0x1f, 0x0a43);
4340 rtl_writephy(tp, 0x13, 0x80d1);
4341 rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
4342 rtl_writephy(tp, 0x13, 0x80cd);
4343 rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
4344 rtl_writephy(tp, 0x13, 0x80d3);
4345 rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
4346 rtl_writephy(tp, 0x13, 0x80d5);
4347 rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
4348 rtl_writephy(tp, 0x13, 0x80d7);
4349 rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
4350
4351 /* Force PWM-mode */
4352 rtl_writephy(tp, 0x1f, 0x0bcd);
4353 rtl_writephy(tp, 0x14, 0x5065);
4354 rtl_writephy(tp, 0x14, 0xd065);
4355 rtl_writephy(tp, 0x1f, 0x0bc8);
4356 rtl_writephy(tp, 0x12, 0x00ed);
4357 rtl_writephy(tp, 0x1f, 0x0bcd);
4358 rtl_writephy(tp, 0x14, 0x1065);
4359 rtl_writephy(tp, 0x14, 0x9065);
4360 rtl_writephy(tp, 0x14, 0x1065);
4361 rtl_writephy(tp, 0x1f, 0x0000);
4362
4363 /* Check ALDPS bit, disable it if enabled */
4364 rtl_writephy(tp, 0x1f, 0x0a43);
4365 if (rtl_readphy(tp, 0x10) & 0x0004)
4366 rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0004);
4367
4368 rtl_writephy(tp, 0x1f, 0x0000);
4369}
4370
4da19633 4371static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
2857ffb7 4372{
350f7596 4373 static const struct phy_reg phy_reg_init[] = {
2857ffb7
FR
4374 { 0x1f, 0x0003 },
4375 { 0x08, 0x441d },
4376 { 0x01, 0x9100 },
4377 { 0x1f, 0x0000 }
4378 };
4379
4da19633 4380 rtl_writephy(tp, 0x1f, 0x0000);
4381 rtl_patchphy(tp, 0x11, 1 << 12);
4382 rtl_patchphy(tp, 0x19, 1 << 13);
4383 rtl_patchphy(tp, 0x10, 1 << 15);
2857ffb7 4384
4da19633 4385 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2857ffb7
FR
4386}
4387
5a5e4443
HW
4388static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
4389{
4390 static const struct phy_reg phy_reg_init[] = {
4391 { 0x1f, 0x0005 },
4392 { 0x1a, 0x0000 },
4393 { 0x1f, 0x0000 },
4394
4395 { 0x1f, 0x0004 },
4396 { 0x1c, 0x0000 },
4397 { 0x1f, 0x0000 },
4398
4399 { 0x1f, 0x0001 },
4400 { 0x15, 0x7701 },
4401 { 0x1f, 0x0000 }
4402 };
4403
4404 /* Disable ALDPS before ram code */
eef63cc1
FR
4405 rtl_writephy(tp, 0x1f, 0x0000);
4406 rtl_writephy(tp, 0x18, 0x0310);
4407 msleep(100);
5a5e4443 4408
953a12cc 4409 rtl_apply_firmware(tp);
5a5e4443
HW
4410
4411 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4412}
4413
7e18dca1
HW
4414static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
4415{
7e18dca1 4416 /* Disable ALDPS before setting firmware */
eef63cc1
FR
4417 rtl_writephy(tp, 0x1f, 0x0000);
4418 rtl_writephy(tp, 0x18, 0x0310);
4419 msleep(20);
7e18dca1
HW
4420
4421 rtl_apply_firmware(tp);
4422
4423 /* EEE setting */
fdf6fc06 4424 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
7e18dca1
HW
4425 rtl_writephy(tp, 0x1f, 0x0004);
4426 rtl_writephy(tp, 0x10, 0x401f);
4427 rtl_writephy(tp, 0x19, 0x7030);
4428 rtl_writephy(tp, 0x1f, 0x0000);
4429}
4430
5598bfe5
HW
4431static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
4432{
5598bfe5
HW
4433 static const struct phy_reg phy_reg_init[] = {
4434 { 0x1f, 0x0004 },
4435 { 0x10, 0xc07f },
4436 { 0x19, 0x7030 },
4437 { 0x1f, 0x0000 }
4438 };
4439
4440 /* Disable ALDPS before ram code */
eef63cc1
FR
4441 rtl_writephy(tp, 0x1f, 0x0000);
4442 rtl_writephy(tp, 0x18, 0x0310);
4443 msleep(100);
5598bfe5
HW
4444
4445 rtl_apply_firmware(tp);
4446
fdf6fc06 4447 rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5598bfe5
HW
4448 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
4449
fdf6fc06 4450 rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
5598bfe5
HW
4451}
4452
5615d9f1
FR
4453static void rtl_hw_phy_config(struct net_device *dev)
4454{
4455 struct rtl8169_private *tp = netdev_priv(dev);
5615d9f1
FR
4456
4457 rtl8169_print_mac_version(tp);
4458
4459 switch (tp->mac_version) {
4460 case RTL_GIGA_MAC_VER_01:
4461 break;
4462 case RTL_GIGA_MAC_VER_02:
4463 case RTL_GIGA_MAC_VER_03:
4da19633 4464 rtl8169s_hw_phy_config(tp);
5615d9f1
FR
4465 break;
4466 case RTL_GIGA_MAC_VER_04:
4da19633 4467 rtl8169sb_hw_phy_config(tp);
5615d9f1 4468 break;
2e955856 4469 case RTL_GIGA_MAC_VER_05:
4da19633 4470 rtl8169scd_hw_phy_config(tp);
2e955856 4471 break;
8c7006aa 4472 case RTL_GIGA_MAC_VER_06:
4da19633 4473 rtl8169sce_hw_phy_config(tp);
8c7006aa 4474 break;
2857ffb7
FR
4475 case RTL_GIGA_MAC_VER_07:
4476 case RTL_GIGA_MAC_VER_08:
4477 case RTL_GIGA_MAC_VER_09:
4da19633 4478 rtl8102e_hw_phy_config(tp);
2857ffb7 4479 break;
236b8082 4480 case RTL_GIGA_MAC_VER_11:
4da19633 4481 rtl8168bb_hw_phy_config(tp);
236b8082
FR
4482 break;
4483 case RTL_GIGA_MAC_VER_12:
4da19633 4484 rtl8168bef_hw_phy_config(tp);
236b8082
FR
4485 break;
4486 case RTL_GIGA_MAC_VER_17:
4da19633 4487 rtl8168bef_hw_phy_config(tp);
236b8082 4488 break;
867763c1 4489 case RTL_GIGA_MAC_VER_18:
4da19633 4490 rtl8168cp_1_hw_phy_config(tp);
867763c1
FR
4491 break;
4492 case RTL_GIGA_MAC_VER_19:
4da19633 4493 rtl8168c_1_hw_phy_config(tp);
867763c1 4494 break;
7da97ec9 4495 case RTL_GIGA_MAC_VER_20:
4da19633 4496 rtl8168c_2_hw_phy_config(tp);
7da97ec9 4497 break;
197ff761 4498 case RTL_GIGA_MAC_VER_21:
4da19633 4499 rtl8168c_3_hw_phy_config(tp);
197ff761 4500 break;
6fb07058 4501 case RTL_GIGA_MAC_VER_22:
4da19633 4502 rtl8168c_4_hw_phy_config(tp);
6fb07058 4503 break;
ef3386f0 4504 case RTL_GIGA_MAC_VER_23:
7f3e3d3a 4505 case RTL_GIGA_MAC_VER_24:
4da19633 4506 rtl8168cp_2_hw_phy_config(tp);
ef3386f0 4507 break;
5b538df9 4508 case RTL_GIGA_MAC_VER_25:
bca03d5f 4509 rtl8168d_1_hw_phy_config(tp);
daf9df6d 4510 break;
4511 case RTL_GIGA_MAC_VER_26:
bca03d5f 4512 rtl8168d_2_hw_phy_config(tp);
daf9df6d 4513 break;
4514 case RTL_GIGA_MAC_VER_27:
4da19633 4515 rtl8168d_3_hw_phy_config(tp);
5b538df9 4516 break;
e6de30d6 4517 case RTL_GIGA_MAC_VER_28:
4518 rtl8168d_4_hw_phy_config(tp);
4519 break;
5a5e4443
HW
4520 case RTL_GIGA_MAC_VER_29:
4521 case RTL_GIGA_MAC_VER_30:
4522 rtl8105e_hw_phy_config(tp);
4523 break;
cecb5fd7
FR
4524 case RTL_GIGA_MAC_VER_31:
4525 /* None. */
4526 break;
01dc7fec 4527 case RTL_GIGA_MAC_VER_32:
01dc7fec 4528 case RTL_GIGA_MAC_VER_33:
70090424
HW
4529 rtl8168e_1_hw_phy_config(tp);
4530 break;
4531 case RTL_GIGA_MAC_VER_34:
4532 rtl8168e_2_hw_phy_config(tp);
01dc7fec 4533 break;
c2218925
HW
4534 case RTL_GIGA_MAC_VER_35:
4535 rtl8168f_1_hw_phy_config(tp);
4536 break;
4537 case RTL_GIGA_MAC_VER_36:
4538 rtl8168f_2_hw_phy_config(tp);
4539 break;
ef3386f0 4540
7e18dca1
HW
4541 case RTL_GIGA_MAC_VER_37:
4542 rtl8402_hw_phy_config(tp);
4543 break;
4544
b3d7b2f2
HW
4545 case RTL_GIGA_MAC_VER_38:
4546 rtl8411_hw_phy_config(tp);
4547 break;
4548
5598bfe5
HW
4549 case RTL_GIGA_MAC_VER_39:
4550 rtl8106e_hw_phy_config(tp);
4551 break;
4552
c558386b
HW
4553 case RTL_GIGA_MAC_VER_40:
4554 rtl8168g_1_hw_phy_config(tp);
4555 break;
57538c4a 4556 case RTL_GIGA_MAC_VER_42:
58152cd4 4557 case RTL_GIGA_MAC_VER_43:
45dd95c4 4558 case RTL_GIGA_MAC_VER_44:
57538c4a 4559 rtl8168g_2_hw_phy_config(tp);
4560 break;
6e1d0b89
CHL
4561 case RTL_GIGA_MAC_VER_45:
4562 case RTL_GIGA_MAC_VER_47:
4563 rtl8168h_1_hw_phy_config(tp);
4564 break;
4565 case RTL_GIGA_MAC_VER_46:
4566 case RTL_GIGA_MAC_VER_48:
4567 rtl8168h_2_hw_phy_config(tp);
4568 break;
c558386b 4569
935e2218
CHL
4570 case RTL_GIGA_MAC_VER_49:
4571 rtl8168ep_1_hw_phy_config(tp);
4572 break;
4573 case RTL_GIGA_MAC_VER_50:
4574 case RTL_GIGA_MAC_VER_51:
4575 rtl8168ep_2_hw_phy_config(tp);
4576 break;
4577
c558386b 4578 case RTL_GIGA_MAC_VER_41:
5615d9f1
FR
4579 default:
4580 break;
4581 }
4582}
4583
da78dbff 4584static void rtl_phy_work(struct rtl8169_private *tp)
1da177e4 4585{
1da177e4
LT
4586 struct timer_list *timer = &tp->timer;
4587 void __iomem *ioaddr = tp->mmio_addr;
4588 unsigned long timeout = RTL8169_PHY_TIMEOUT;
4589
bcf0bf90 4590 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
1da177e4 4591
4da19633 4592 if (tp->phy_reset_pending(tp)) {
5b0384f4 4593 /*
1da177e4
LT
4594 * A busy loop could burn quite a few cycles on nowadays CPU.
4595 * Let's delay the execution of the timer for a few ticks.
4596 */
4597 timeout = HZ/10;
4598 goto out_mod_timer;
4599 }
4600
4601 if (tp->link_ok(ioaddr))
da78dbff 4602 return;
1da177e4 4603
9bb8eeb5 4604 netif_dbg(tp, link, tp->dev, "PHY reset until link up\n");
1da177e4 4605
4da19633 4606 tp->phy_reset_enable(tp);
1da177e4
LT
4607
4608out_mod_timer:
4609 mod_timer(timer, jiffies + timeout);
da78dbff
FR
4610}
4611
4612static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
4613{
da78dbff
FR
4614 if (!test_and_set_bit(flag, tp->wk.flags))
4615 schedule_work(&tp->wk.work);
da78dbff
FR
4616}
4617
9de36ccf 4618static void rtl8169_phy_timer(struct timer_list *t)
da78dbff 4619{
9de36ccf 4620 struct rtl8169_private *tp = from_timer(tp, t, timer);
da78dbff 4621
98ddf986 4622 rtl_schedule_task(tp, RTL_FLAG_TASK_PHY_PENDING);
1da177e4
LT
4623}
4624
ffc46952
FR
4625DECLARE_RTL_COND(rtl_phy_reset_cond)
4626{
4627 return tp->phy_reset_pending(tp);
4628}
4629
bf793295
FR
4630static void rtl8169_phy_reset(struct net_device *dev,
4631 struct rtl8169_private *tp)
4632{
4da19633 4633 tp->phy_reset_enable(tp);
ffc46952 4634 rtl_msleep_loop_wait_low(tp, &rtl_phy_reset_cond, 1, 100);
bf793295
FR
4635}
4636
2544bfc0
FR
4637static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4638{
4639 void __iomem *ioaddr = tp->mmio_addr;
4640
4641 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4642 (RTL_R8(PHYstatus) & TBI_Enable);
4643}
4644
4ff96fa6
FR
4645static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
4646{
4647 void __iomem *ioaddr = tp->mmio_addr;
4ff96fa6 4648
5615d9f1 4649 rtl_hw_phy_config(dev);
4ff96fa6 4650
77332894
MS
4651 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4652 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4653 RTL_W8(0x82, 0x01);
4654 }
4ff96fa6 4655
6dccd16b
FR
4656 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4657
4658 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
4659 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4ff96fa6 4660
bcf0bf90 4661 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
4ff96fa6
FR
4662 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4663 RTL_W8(0x82, 0x01);
4664 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
4da19633 4665 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
4ff96fa6
FR
4666 }
4667
bf793295
FR
4668 rtl8169_phy_reset(dev, tp);
4669
54405cde 4670 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
cecb5fd7
FR
4671 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4672 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4673 (tp->mii.supports_gmii ?
4674 ADVERTISED_1000baseT_Half |
4675 ADVERTISED_1000baseT_Full : 0));
4ff96fa6 4676
2544bfc0 4677 if (rtl_tbi_enabled(tp))
bf82c189 4678 netif_info(tp, link, dev, "TBI auto-negotiating\n");
4ff96fa6
FR
4679}
4680
773d2021
FR
4681static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4682{
4683 void __iomem *ioaddr = tp->mmio_addr;
773d2021 4684
da78dbff 4685 rtl_lock_work(tp);
773d2021
FR
4686
4687 RTL_W8(Cfg9346, Cfg9346_Unlock);
908ba2bf 4688
9ecb9aab 4689 RTL_W32(MAC4, addr[4] | addr[5] << 8);
908ba2bf 4690 RTL_R32(MAC4);
4691
9ecb9aab 4692 RTL_W32(MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
908ba2bf 4693 RTL_R32(MAC0);
4694
9ecb9aab 4695 if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4696 rtl_rar_exgmac_set(tp, addr);
c28aa385 4697
773d2021
FR
4698 RTL_W8(Cfg9346, Cfg9346_Lock);
4699
da78dbff 4700 rtl_unlock_work(tp);
773d2021
FR
4701}
4702
4703static int rtl_set_mac_address(struct net_device *dev, void *p)
4704{
4705 struct rtl8169_private *tp = netdev_priv(dev);
f51d4a10 4706 struct device *d = &tp->pci_dev->dev;
773d2021
FR
4707 struct sockaddr *addr = p;
4708
4709 if (!is_valid_ether_addr(addr->sa_data))
4710 return -EADDRNOTAVAIL;
4711
4712 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
4713
f51d4a10
CHL
4714 pm_runtime_get_noresume(d);
4715
4716 if (pm_runtime_active(d))
4717 rtl_rar_set(tp, dev->dev_addr);
4718
4719 pm_runtime_put_noidle(d);
773d2021
FR
4720
4721 return 0;
4722}
4723
5f787a1a
FR
4724static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4725{
4726 struct rtl8169_private *tp = netdev_priv(dev);
4727 struct mii_ioctl_data *data = if_mii(ifr);
4728
8b4ab28d
FR
4729 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
4730}
5f787a1a 4731
cecb5fd7
FR
4732static int rtl_xmii_ioctl(struct rtl8169_private *tp,
4733 struct mii_ioctl_data *data, int cmd)
8b4ab28d 4734{
5f787a1a
FR
4735 switch (cmd) {
4736 case SIOCGMIIPHY:
4737 data->phy_id = 32; /* Internal PHY */
4738 return 0;
4739
4740 case SIOCGMIIREG:
4da19633 4741 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
5f787a1a
FR
4742 return 0;
4743
4744 case SIOCSMIIREG:
4da19633 4745 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
5f787a1a
FR
4746 return 0;
4747 }
4748 return -EOPNOTSUPP;
4749}
4750
8b4ab28d
FR
4751static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
4752{
4753 return -EOPNOTSUPP;
4754}
4755
baf63293 4756static void rtl_init_mdio_ops(struct rtl8169_private *tp)
c0e45c1c 4757{
4758 struct mdio_ops *ops = &tp->mdio_ops;
4759
4760 switch (tp->mac_version) {
4761 case RTL_GIGA_MAC_VER_27:
4762 ops->write = r8168dp_1_mdio_write;
4763 ops->read = r8168dp_1_mdio_read;
4764 break;
e6de30d6 4765 case RTL_GIGA_MAC_VER_28:
4804b3b3 4766 case RTL_GIGA_MAC_VER_31:
e6de30d6 4767 ops->write = r8168dp_2_mdio_write;
4768 ops->read = r8168dp_2_mdio_read;
4769 break;
c558386b
HW
4770 case RTL_GIGA_MAC_VER_40:
4771 case RTL_GIGA_MAC_VER_41:
57538c4a 4772 case RTL_GIGA_MAC_VER_42:
58152cd4 4773 case RTL_GIGA_MAC_VER_43:
45dd95c4 4774 case RTL_GIGA_MAC_VER_44:
6e1d0b89
CHL
4775 case RTL_GIGA_MAC_VER_45:
4776 case RTL_GIGA_MAC_VER_46:
4777 case RTL_GIGA_MAC_VER_47:
4778 case RTL_GIGA_MAC_VER_48:
935e2218
CHL
4779 case RTL_GIGA_MAC_VER_49:
4780 case RTL_GIGA_MAC_VER_50:
4781 case RTL_GIGA_MAC_VER_51:
c558386b
HW
4782 ops->write = r8168g_mdio_write;
4783 ops->read = r8168g_mdio_read;
4784 break;
c0e45c1c 4785 default:
4786 ops->write = r8169_mdio_write;
4787 ops->read = r8169_mdio_read;
4788 break;
4789 }
4790}
4791
e2409d83 4792static void rtl_speed_down(struct rtl8169_private *tp)
4793{
4794 u32 adv;
4795 int lpa;
4796
4797 rtl_writephy(tp, 0x1f, 0x0000);
4798 lpa = rtl_readphy(tp, MII_LPA);
4799
4800 if (lpa & (LPA_10HALF | LPA_10FULL))
4801 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full;
4802 else if (lpa & (LPA_100HALF | LPA_100FULL))
4803 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4804 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
4805 else
4806 adv = ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
4807 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
4808 (tp->mii.supports_gmii ?
4809 ADVERTISED_1000baseT_Half |
4810 ADVERTISED_1000baseT_Full : 0);
4811
4812 rtl8169_set_speed(tp->dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
4813 adv);
4814}
4815
649b3b8c 4816static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4817{
4818 void __iomem *ioaddr = tp->mmio_addr;
4819
4820 switch (tp->mac_version) {
b00e69de
CB
4821 case RTL_GIGA_MAC_VER_25:
4822 case RTL_GIGA_MAC_VER_26:
649b3b8c 4823 case RTL_GIGA_MAC_VER_29:
4824 case RTL_GIGA_MAC_VER_30:
4825 case RTL_GIGA_MAC_VER_32:
4826 case RTL_GIGA_MAC_VER_33:
4827 case RTL_GIGA_MAC_VER_34:
7e18dca1 4828 case RTL_GIGA_MAC_VER_37:
b3d7b2f2 4829 case RTL_GIGA_MAC_VER_38:
5598bfe5 4830 case RTL_GIGA_MAC_VER_39:
c558386b
HW
4831 case RTL_GIGA_MAC_VER_40:
4832 case RTL_GIGA_MAC_VER_41:
57538c4a 4833 case RTL_GIGA_MAC_VER_42:
58152cd4 4834 case RTL_GIGA_MAC_VER_43:
45dd95c4 4835 case RTL_GIGA_MAC_VER_44:
6e1d0b89
CHL
4836 case RTL_GIGA_MAC_VER_45:
4837 case RTL_GIGA_MAC_VER_46:
4838 case RTL_GIGA_MAC_VER_47:
4839 case RTL_GIGA_MAC_VER_48:
935e2218
CHL
4840 case RTL_GIGA_MAC_VER_49:
4841 case RTL_GIGA_MAC_VER_50:
4842 case RTL_GIGA_MAC_VER_51:
649b3b8c 4843 RTL_W32(RxConfig, RTL_R32(RxConfig) |
4844 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4845 break;
4846 default:
4847 break;
4848 }
4849}
4850
4851static bool rtl_wol_pll_power_down(struct rtl8169_private *tp)
4852{
4853 if (!(__rtl8169_get_wol(tp) & WAKE_ANY))
4854 return false;
4855
e2409d83 4856 rtl_speed_down(tp);
649b3b8c 4857 rtl_wol_suspend_quirk(tp);
4858
4859 return true;
4860}
4861
065c27c1 4862static void r810x_phy_power_down(struct rtl8169_private *tp)
4863{
4864 rtl_writephy(tp, 0x1f, 0x0000);
4865 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4866}
4867
4868static void r810x_phy_power_up(struct rtl8169_private *tp)
4869{
4870 rtl_writephy(tp, 0x1f, 0x0000);
4871 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4872}
4873
4874static void r810x_pll_power_down(struct rtl8169_private *tp)
4875{
0004299a
HW
4876 void __iomem *ioaddr = tp->mmio_addr;
4877
649b3b8c 4878 if (rtl_wol_pll_power_down(tp))
065c27c1 4879 return;
065c27c1 4880
4881 r810x_phy_power_down(tp);
0004299a
HW
4882
4883 switch (tp->mac_version) {
4884 case RTL_GIGA_MAC_VER_07:
4885 case RTL_GIGA_MAC_VER_08:
4886 case RTL_GIGA_MAC_VER_09:
4887 case RTL_GIGA_MAC_VER_10:
4888 case RTL_GIGA_MAC_VER_13:
4889 case RTL_GIGA_MAC_VER_16:
4890 break;
4891 default:
4892 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
4893 break;
4894 }
065c27c1 4895}
4896
4897static void r810x_pll_power_up(struct rtl8169_private *tp)
4898{
0004299a
HW
4899 void __iomem *ioaddr = tp->mmio_addr;
4900
065c27c1 4901 r810x_phy_power_up(tp);
0004299a
HW
4902
4903 switch (tp->mac_version) {
4904 case RTL_GIGA_MAC_VER_07:
4905 case RTL_GIGA_MAC_VER_08:
4906 case RTL_GIGA_MAC_VER_09:
4907 case RTL_GIGA_MAC_VER_10:
4908 case RTL_GIGA_MAC_VER_13:
4909 case RTL_GIGA_MAC_VER_16:
4910 break;
6e1d0b89
CHL
4911 case RTL_GIGA_MAC_VER_47:
4912 case RTL_GIGA_MAC_VER_48:
05b9687b 4913 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
6e1d0b89 4914 break;
0004299a
HW
4915 default:
4916 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
4917 break;
4918 }
065c27c1 4919}
4920
4921static void r8168_phy_power_up(struct rtl8169_private *tp)
4922{
4923 rtl_writephy(tp, 0x1f, 0x0000);
01dc7fec 4924 switch (tp->mac_version) {
4925 case RTL_GIGA_MAC_VER_11:
4926 case RTL_GIGA_MAC_VER_12:
4927 case RTL_GIGA_MAC_VER_17:
4928 case RTL_GIGA_MAC_VER_18:
4929 case RTL_GIGA_MAC_VER_19:
4930 case RTL_GIGA_MAC_VER_20:
4931 case RTL_GIGA_MAC_VER_21:
4932 case RTL_GIGA_MAC_VER_22:
4933 case RTL_GIGA_MAC_VER_23:
4934 case RTL_GIGA_MAC_VER_24:
4935 case RTL_GIGA_MAC_VER_25:
4936 case RTL_GIGA_MAC_VER_26:
4937 case RTL_GIGA_MAC_VER_27:
4938 case RTL_GIGA_MAC_VER_28:
4939 case RTL_GIGA_MAC_VER_31:
4940 rtl_writephy(tp, 0x0e, 0x0000);
4941 break;
4942 default:
4943 break;
4944 }
065c27c1 4945 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
4946}
4947
4948static void r8168_phy_power_down(struct rtl8169_private *tp)
4949{
4950 rtl_writephy(tp, 0x1f, 0x0000);
01dc7fec 4951 switch (tp->mac_version) {
4952 case RTL_GIGA_MAC_VER_32:
4953 case RTL_GIGA_MAC_VER_33:
beb330a4 4954 case RTL_GIGA_MAC_VER_40:
4955 case RTL_GIGA_MAC_VER_41:
01dc7fec 4956 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
4957 break;
4958
4959 case RTL_GIGA_MAC_VER_11:
4960 case RTL_GIGA_MAC_VER_12:
4961 case RTL_GIGA_MAC_VER_17:
4962 case RTL_GIGA_MAC_VER_18:
4963 case RTL_GIGA_MAC_VER_19:
4964 case RTL_GIGA_MAC_VER_20:
4965 case RTL_GIGA_MAC_VER_21:
4966 case RTL_GIGA_MAC_VER_22:
4967 case RTL_GIGA_MAC_VER_23:
4968 case RTL_GIGA_MAC_VER_24:
4969 case RTL_GIGA_MAC_VER_25:
4970 case RTL_GIGA_MAC_VER_26:
4971 case RTL_GIGA_MAC_VER_27:
4972 case RTL_GIGA_MAC_VER_28:
4973 case RTL_GIGA_MAC_VER_31:
4974 rtl_writephy(tp, 0x0e, 0x0200);
4975 default:
4976 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
4977 break;
4978 }
065c27c1 4979}
4980
4981static void r8168_pll_power_down(struct rtl8169_private *tp)
4982{
4983 void __iomem *ioaddr = tp->mmio_addr;
4984
cecb5fd7
FR
4985 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4986 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
935e2218
CHL
4987 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
4988 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
4989 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
4990 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
2f8c040c 4991 r8168_check_dash(tp)) {
065c27c1 4992 return;
5d2e1957 4993 }
065c27c1 4994
cecb5fd7
FR
4995 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
4996 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
065c27c1 4997 (RTL_R16(CPlusCmd) & ASF)) {
4998 return;
4999 }
5000
01dc7fec 5001 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
5002 tp->mac_version == RTL_GIGA_MAC_VER_33)
fdf6fc06 5003 rtl_ephy_write(tp, 0x19, 0xff64);
01dc7fec 5004
649b3b8c 5005 if (rtl_wol_pll_power_down(tp))
065c27c1 5006 return;
065c27c1 5007
5008 r8168_phy_power_down(tp);
5009
5010 switch (tp->mac_version) {
5011 case RTL_GIGA_MAC_VER_25:
5012 case RTL_GIGA_MAC_VER_26:
5d2e1957
HW
5013 case RTL_GIGA_MAC_VER_27:
5014 case RTL_GIGA_MAC_VER_28:
4804b3b3 5015 case RTL_GIGA_MAC_VER_31:
01dc7fec 5016 case RTL_GIGA_MAC_VER_32:
5017 case RTL_GIGA_MAC_VER_33:
42fde737 5018 case RTL_GIGA_MAC_VER_44:
6e1d0b89
CHL
5019 case RTL_GIGA_MAC_VER_45:
5020 case RTL_GIGA_MAC_VER_46:
935e2218
CHL
5021 case RTL_GIGA_MAC_VER_50:
5022 case RTL_GIGA_MAC_VER_51:
065c27c1 5023 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
5024 break;
beb330a4 5025 case RTL_GIGA_MAC_VER_40:
5026 case RTL_GIGA_MAC_VER_41:
935e2218 5027 case RTL_GIGA_MAC_VER_49:
706123d0 5028 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0x00000000,
beb330a4 5029 0xfc000000, ERIAR_EXGMAC);
b8e5e6ad 5030 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
beb330a4 5031 break;
065c27c1 5032 }
5033}
5034
5035static void r8168_pll_power_up(struct rtl8169_private *tp)
5036{
5037 void __iomem *ioaddr = tp->mmio_addr;
5038
065c27c1 5039 switch (tp->mac_version) {
5040 case RTL_GIGA_MAC_VER_25:
5041 case RTL_GIGA_MAC_VER_26:
5d2e1957
HW
5042 case RTL_GIGA_MAC_VER_27:
5043 case RTL_GIGA_MAC_VER_28:
4804b3b3 5044 case RTL_GIGA_MAC_VER_31:
01dc7fec 5045 case RTL_GIGA_MAC_VER_32:
5046 case RTL_GIGA_MAC_VER_33:
065c27c1 5047 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
5048 break;
42fde737 5049 case RTL_GIGA_MAC_VER_44:
6e1d0b89
CHL
5050 case RTL_GIGA_MAC_VER_45:
5051 case RTL_GIGA_MAC_VER_46:
935e2218
CHL
5052 case RTL_GIGA_MAC_VER_50:
5053 case RTL_GIGA_MAC_VER_51:
05b9687b 5054 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
6e1d0b89 5055 break;
beb330a4 5056 case RTL_GIGA_MAC_VER_40:
5057 case RTL_GIGA_MAC_VER_41:
935e2218 5058 case RTL_GIGA_MAC_VER_49:
b8e5e6ad 5059 RTL_W8(PMCH, RTL_R8(PMCH) | 0xc0);
706123d0 5060 rtl_w0w1_eri(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000,
beb330a4 5061 0x00000000, ERIAR_EXGMAC);
5062 break;
065c27c1 5063 }
5064
5065 r8168_phy_power_up(tp);
5066}
5067
d58d46b5
FR
5068static void rtl_generic_op(struct rtl8169_private *tp,
5069 void (*op)(struct rtl8169_private *))
065c27c1 5070{
5071 if (op)
5072 op(tp);
5073}
5074
5075static void rtl_pll_power_down(struct rtl8169_private *tp)
5076{
d58d46b5 5077 rtl_generic_op(tp, tp->pll_power_ops.down);
065c27c1 5078}
5079
5080static void rtl_pll_power_up(struct rtl8169_private *tp)
5081{
d58d46b5 5082 rtl_generic_op(tp, tp->pll_power_ops.up);
065c27c1 5083}
5084
baf63293 5085static void rtl_init_pll_power_ops(struct rtl8169_private *tp)
065c27c1 5086{
5087 struct pll_power_ops *ops = &tp->pll_power_ops;
5088
5089 switch (tp->mac_version) {
5090 case RTL_GIGA_MAC_VER_07:
5091 case RTL_GIGA_MAC_VER_08:
5092 case RTL_GIGA_MAC_VER_09:
5093 case RTL_GIGA_MAC_VER_10:
5094 case RTL_GIGA_MAC_VER_16:
5a5e4443
HW
5095 case RTL_GIGA_MAC_VER_29:
5096 case RTL_GIGA_MAC_VER_30:
7e18dca1 5097 case RTL_GIGA_MAC_VER_37:
5598bfe5 5098 case RTL_GIGA_MAC_VER_39:
58152cd4 5099 case RTL_GIGA_MAC_VER_43:
6e1d0b89
CHL
5100 case RTL_GIGA_MAC_VER_47:
5101 case RTL_GIGA_MAC_VER_48:
065c27c1 5102 ops->down = r810x_pll_power_down;
5103 ops->up = r810x_pll_power_up;
5104 break;
5105
5106 case RTL_GIGA_MAC_VER_11:
5107 case RTL_GIGA_MAC_VER_12:
5108 case RTL_GIGA_MAC_VER_17:
5109 case RTL_GIGA_MAC_VER_18:
5110 case RTL_GIGA_MAC_VER_19:
5111 case RTL_GIGA_MAC_VER_20:
5112 case RTL_GIGA_MAC_VER_21:
5113 case RTL_GIGA_MAC_VER_22:
5114 case RTL_GIGA_MAC_VER_23:
5115 case RTL_GIGA_MAC_VER_24:
5116 case RTL_GIGA_MAC_VER_25:
5117 case RTL_GIGA_MAC_VER_26:
5118 case RTL_GIGA_MAC_VER_27:
e6de30d6 5119 case RTL_GIGA_MAC_VER_28:
4804b3b3 5120 case RTL_GIGA_MAC_VER_31:
01dc7fec 5121 case RTL_GIGA_MAC_VER_32:
5122 case RTL_GIGA_MAC_VER_33:
70090424 5123 case RTL_GIGA_MAC_VER_34:
c2218925
HW
5124 case RTL_GIGA_MAC_VER_35:
5125 case RTL_GIGA_MAC_VER_36:
b3d7b2f2 5126 case RTL_GIGA_MAC_VER_38:
c558386b
HW
5127 case RTL_GIGA_MAC_VER_40:
5128 case RTL_GIGA_MAC_VER_41:
57538c4a 5129 case RTL_GIGA_MAC_VER_42:
45dd95c4 5130 case RTL_GIGA_MAC_VER_44:
6e1d0b89
CHL
5131 case RTL_GIGA_MAC_VER_45:
5132 case RTL_GIGA_MAC_VER_46:
935e2218
CHL
5133 case RTL_GIGA_MAC_VER_49:
5134 case RTL_GIGA_MAC_VER_50:
5135 case RTL_GIGA_MAC_VER_51:
065c27c1 5136 ops->down = r8168_pll_power_down;
5137 ops->up = r8168_pll_power_up;
5138 break;
5139
5140 default:
5141 ops->down = NULL;
5142 ops->up = NULL;
5143 break;
5144 }
5145}
5146
e542a226
HW
5147static void rtl_init_rxcfg(struct rtl8169_private *tp)
5148{
5149 void __iomem *ioaddr = tp->mmio_addr;
5150
5151 switch (tp->mac_version) {
5152 case RTL_GIGA_MAC_VER_01:
5153 case RTL_GIGA_MAC_VER_02:
5154 case RTL_GIGA_MAC_VER_03:
5155 case RTL_GIGA_MAC_VER_04:
5156 case RTL_GIGA_MAC_VER_05:
5157 case RTL_GIGA_MAC_VER_06:
5158 case RTL_GIGA_MAC_VER_10:
5159 case RTL_GIGA_MAC_VER_11:
5160 case RTL_GIGA_MAC_VER_12:
5161 case RTL_GIGA_MAC_VER_13:
5162 case RTL_GIGA_MAC_VER_14:
5163 case RTL_GIGA_MAC_VER_15:
5164 case RTL_GIGA_MAC_VER_16:
5165 case RTL_GIGA_MAC_VER_17:
5166 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
5167 break;
5168 case RTL_GIGA_MAC_VER_18:
5169 case RTL_GIGA_MAC_VER_19:
5170 case RTL_GIGA_MAC_VER_20:
5171 case RTL_GIGA_MAC_VER_21:
5172 case RTL_GIGA_MAC_VER_22:
5173 case RTL_GIGA_MAC_VER_23:
5174 case RTL_GIGA_MAC_VER_24:
eb2dc35d 5175 case RTL_GIGA_MAC_VER_34:
3ced8c95 5176 case RTL_GIGA_MAC_VER_35:
e542a226
HW
5177 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
5178 break;
beb330a4 5179 case RTL_GIGA_MAC_VER_40:
5180 case RTL_GIGA_MAC_VER_41:
57538c4a 5181 case RTL_GIGA_MAC_VER_42:
58152cd4 5182 case RTL_GIGA_MAC_VER_43:
45dd95c4 5183 case RTL_GIGA_MAC_VER_44:
6e1d0b89
CHL
5184 case RTL_GIGA_MAC_VER_45:
5185 case RTL_GIGA_MAC_VER_46:
5186 case RTL_GIGA_MAC_VER_47:
5187 case RTL_GIGA_MAC_VER_48:
935e2218
CHL
5188 case RTL_GIGA_MAC_VER_49:
5189 case RTL_GIGA_MAC_VER_50:
5190 case RTL_GIGA_MAC_VER_51:
7ebc4822 5191 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
beb330a4 5192 break;
e542a226
HW
5193 default:
5194 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
5195 break;
5196 }
5197}
5198
92fc43b4
HW
5199static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
5200{
9fba0812 5201 tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
92fc43b4
HW
5202}
5203
d58d46b5
FR
5204static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
5205{
9c5028e9 5206 void __iomem *ioaddr = tp->mmio_addr;
5207
5208 RTL_W8(Cfg9346, Cfg9346_Unlock);
d58d46b5 5209 rtl_generic_op(tp, tp->jumbo_ops.enable);
9c5028e9 5210 RTL_W8(Cfg9346, Cfg9346_Lock);
d58d46b5
FR
5211}
5212
5213static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
5214{
9c5028e9 5215 void __iomem *ioaddr = tp->mmio_addr;
5216
5217 RTL_W8(Cfg9346, Cfg9346_Unlock);
d58d46b5 5218 rtl_generic_op(tp, tp->jumbo_ops.disable);
9c5028e9 5219 RTL_W8(Cfg9346, Cfg9346_Lock);
d58d46b5
FR
5220}
5221
5222static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
5223{
5224 void __iomem *ioaddr = tp->mmio_addr;
5225
5226 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5227 RTL_W8(Config4, RTL_R8(Config4) | Jumbo_En1);
f65d539c 5228 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
d58d46b5
FR
5229}
5230
5231static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
5232{
5233 void __iomem *ioaddr = tp->mmio_addr;
5234
5235 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5236 RTL_W8(Config4, RTL_R8(Config4) & ~Jumbo_En1);
5237 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
5238}
5239
5240static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
5241{
5242 void __iomem *ioaddr = tp->mmio_addr;
5243
5244 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5245}
5246
5247static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
5248{
5249 void __iomem *ioaddr = tp->mmio_addr;
5250
5251 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5252}
5253
5254static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
5255{
5256 void __iomem *ioaddr = tp->mmio_addr;
d58d46b5
FR
5257
5258 RTL_W8(MaxTxPacketSize, 0x3f);
5259 RTL_W8(Config3, RTL_R8(Config3) | Jumbo_En0);
5260 RTL_W8(Config4, RTL_R8(Config4) | 0x01);
f65d539c 5261 rtl_tx_performance_tweak(tp->pci_dev, PCI_EXP_DEVCTL_READRQ_512B);
d58d46b5
FR
5262}
5263
5264static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
5265{
5266 void __iomem *ioaddr = tp->mmio_addr;
d58d46b5
FR
5267
5268 RTL_W8(MaxTxPacketSize, 0x0c);
5269 RTL_W8(Config3, RTL_R8(Config3) & ~Jumbo_En0);
5270 RTL_W8(Config4, RTL_R8(Config4) & ~0x01);
4512ff9f 5271 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
d58d46b5
FR
5272}
5273
5274static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
5275{
5276 rtl_tx_performance_tweak(tp->pci_dev,
f65d539c 5277 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
d58d46b5
FR
5278}
5279
5280static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
5281{
5282 rtl_tx_performance_tweak(tp->pci_dev,
5283 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
5284}
5285
5286static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
5287{
5288 void __iomem *ioaddr = tp->mmio_addr;
5289
5290 r8168b_0_hw_jumbo_enable(tp);
5291
5292 RTL_W8(Config4, RTL_R8(Config4) | (1 << 0));
5293}
5294
5295static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
5296{
5297 void __iomem *ioaddr = tp->mmio_addr;
5298
5299 r8168b_0_hw_jumbo_disable(tp);
5300
5301 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
5302}
5303
baf63293 5304static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
d58d46b5
FR
5305{
5306 struct jumbo_ops *ops = &tp->jumbo_ops;
5307
5308 switch (tp->mac_version) {
5309 case RTL_GIGA_MAC_VER_11:
5310 ops->disable = r8168b_0_hw_jumbo_disable;
5311 ops->enable = r8168b_0_hw_jumbo_enable;
5312 break;
5313 case RTL_GIGA_MAC_VER_12:
5314 case RTL_GIGA_MAC_VER_17:
5315 ops->disable = r8168b_1_hw_jumbo_disable;
5316 ops->enable = r8168b_1_hw_jumbo_enable;
5317 break;
5318 case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
5319 case RTL_GIGA_MAC_VER_19:
5320 case RTL_GIGA_MAC_VER_20:
5321 case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
5322 case RTL_GIGA_MAC_VER_22:
5323 case RTL_GIGA_MAC_VER_23:
5324 case RTL_GIGA_MAC_VER_24:
5325 case RTL_GIGA_MAC_VER_25:
5326 case RTL_GIGA_MAC_VER_26:
5327 ops->disable = r8168c_hw_jumbo_disable;
5328 ops->enable = r8168c_hw_jumbo_enable;
5329 break;
5330 case RTL_GIGA_MAC_VER_27:
5331 case RTL_GIGA_MAC_VER_28:
5332 ops->disable = r8168dp_hw_jumbo_disable;
5333 ops->enable = r8168dp_hw_jumbo_enable;
5334 break;
5335 case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
5336 case RTL_GIGA_MAC_VER_32:
5337 case RTL_GIGA_MAC_VER_33:
5338 case RTL_GIGA_MAC_VER_34:
5339 ops->disable = r8168e_hw_jumbo_disable;
5340 ops->enable = r8168e_hw_jumbo_enable;
5341 break;
5342
5343 /*
5344 * No action needed for jumbo frames with 8169.
5345 * No jumbo for 810x at all.
5346 */
c558386b
HW
5347 case RTL_GIGA_MAC_VER_40:
5348 case RTL_GIGA_MAC_VER_41:
57538c4a 5349 case RTL_GIGA_MAC_VER_42:
58152cd4 5350 case RTL_GIGA_MAC_VER_43:
45dd95c4 5351 case RTL_GIGA_MAC_VER_44:
6e1d0b89
CHL
5352 case RTL_GIGA_MAC_VER_45:
5353 case RTL_GIGA_MAC_VER_46:
5354 case RTL_GIGA_MAC_VER_47:
5355 case RTL_GIGA_MAC_VER_48:
935e2218
CHL
5356 case RTL_GIGA_MAC_VER_49:
5357 case RTL_GIGA_MAC_VER_50:
5358 case RTL_GIGA_MAC_VER_51:
d58d46b5
FR
5359 default:
5360 ops->disable = NULL;
5361 ops->enable = NULL;
5362 break;
5363 }
5364}
5365
ffc46952
FR
5366DECLARE_RTL_COND(rtl_chipcmd_cond)
5367{
5368 void __iomem *ioaddr = tp->mmio_addr;
5369
5370 return RTL_R8(ChipCmd) & CmdReset;
5371}
5372
6f43adc8
FR
5373static void rtl_hw_reset(struct rtl8169_private *tp)
5374{
5375 void __iomem *ioaddr = tp->mmio_addr;
6f43adc8 5376
6f43adc8
FR
5377 RTL_W8(ChipCmd, CmdReset);
5378
ffc46952 5379 rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
6f43adc8
FR
5380}
5381
b6ffd97f 5382static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
953a12cc 5383{
b6ffd97f
FR
5384 struct rtl_fw *rtl_fw;
5385 const char *name;
5386 int rc = -ENOMEM;
953a12cc 5387
b6ffd97f
FR
5388 name = rtl_lookup_firmware_name(tp);
5389 if (!name)
5390 goto out_no_firmware;
953a12cc 5391
b6ffd97f
FR
5392 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
5393 if (!rtl_fw)
5394 goto err_warn;
31bd204f 5395
b6ffd97f
FR
5396 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
5397 if (rc < 0)
5398 goto err_free;
5399
fd112f2e
FR
5400 rc = rtl_check_firmware(tp, rtl_fw);
5401 if (rc < 0)
5402 goto err_release_firmware;
5403
b6ffd97f
FR
5404 tp->rtl_fw = rtl_fw;
5405out:
5406 return;
5407
fd112f2e
FR
5408err_release_firmware:
5409 release_firmware(rtl_fw->fw);
b6ffd97f
FR
5410err_free:
5411 kfree(rtl_fw);
5412err_warn:
5413 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
5414 name, rc);
5415out_no_firmware:
5416 tp->rtl_fw = NULL;
5417 goto out;
5418}
5419
5420static void rtl_request_firmware(struct rtl8169_private *tp)
5421{
5422 if (IS_ERR(tp->rtl_fw))
5423 rtl_request_uncached_firmware(tp);
953a12cc
FR
5424}
5425
92fc43b4
HW
5426static void rtl_rx_close(struct rtl8169_private *tp)
5427{
5428 void __iomem *ioaddr = tp->mmio_addr;
92fc43b4 5429
1687b566 5430 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
92fc43b4
HW
5431}
5432
ffc46952
FR
5433DECLARE_RTL_COND(rtl_npq_cond)
5434{
5435 void __iomem *ioaddr = tp->mmio_addr;
5436
5437 return RTL_R8(TxPoll) & NPQ;
5438}
5439
5440DECLARE_RTL_COND(rtl_txcfg_empty_cond)
5441{
5442 void __iomem *ioaddr = tp->mmio_addr;
5443
5444 return RTL_R32(TxConfig) & TXCFG_EMPTY;
5445}
5446
e6de30d6 5447static void rtl8169_hw_reset(struct rtl8169_private *tp)
1da177e4 5448{
e6de30d6 5449 void __iomem *ioaddr = tp->mmio_addr;
5450
1da177e4 5451 /* Disable interrupts */
811fd301 5452 rtl8169_irq_mask_and_ack(tp);
1da177e4 5453
92fc43b4
HW
5454 rtl_rx_close(tp);
5455
5d2e1957 5456 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
4804b3b3 5457 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
5458 tp->mac_version == RTL_GIGA_MAC_VER_31) {
ffc46952 5459 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
c2218925 5460 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
6e1d0b89
CHL
5461 tp->mac_version == RTL_GIGA_MAC_VER_35 ||
5462 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
5463 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
5464 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
5465 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
5466 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
5467 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
5468 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
5469 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
5470 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
5471 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
5472 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
935e2218
CHL
5473 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
5474 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
5475 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
5476 tp->mac_version == RTL_GIGA_MAC_VER_51) {
c2b0c1e7 5477 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
ffc46952 5478 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
92fc43b4
HW
5479 } else {
5480 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
5481 udelay(100);
e6de30d6 5482 }
5483
92fc43b4 5484 rtl_hw_reset(tp);
1da177e4
LT
5485}
5486
7f796d83 5487static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
9cb427b6
FR
5488{
5489 void __iomem *ioaddr = tp->mmio_addr;
9cb427b6
FR
5490
5491 /* Set DMA burst size and Interframe Gap Time */
5492 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
5493 (InterFrameGap << TxInterFrameGapShift));
5494}
5495
07ce4064 5496static void rtl_hw_start(struct net_device *dev)
1da177e4
LT
5497{
5498 struct rtl8169_private *tp = netdev_priv(dev);
1da177e4 5499
07ce4064
FR
5500 tp->hw_start(dev);
5501
da78dbff 5502 rtl_irq_enable_all(tp);
07ce4064
FR
5503}
5504
7f796d83
FR
5505static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
5506 void __iomem *ioaddr)
5507{
5508 /*
5509 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
5510 * register to be written before TxDescAddrLow to work.
5511 * Switching from MMIO to I/O access fixes the issue as well.
5512 */
5513 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
284901a9 5514 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
7f796d83 5515 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
284901a9 5516 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
7f796d83
FR
5517}
5518
5519static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
5520{
5521 u16 cmd;
5522
5523 cmd = RTL_R16(CPlusCmd);
5524 RTL_W16(CPlusCmd, cmd);
5525 return cmd;
5526}
5527
fdd7b4c3 5528static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
7f796d83
FR
5529{
5530 /* Low hurts. Let's disable the filtering. */
207d6e87 5531 RTL_W16(RxMaxSize, rx_buf_sz + 1);
7f796d83
FR
5532}
5533
6dccd16b
FR
5534static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
5535{
3744100e 5536 static const struct rtl_cfg2_info {
6dccd16b
FR
5537 u32 mac_version;
5538 u32 clk;
5539 u32 val;
5540 } cfg2_info [] = {
5541 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
5542 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
5543 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
5544 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3744100e
FR
5545 };
5546 const struct rtl_cfg2_info *p = cfg2_info;
6dccd16b
FR
5547 unsigned int i;
5548 u32 clk;
5549
5550 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
cadf1855 5551 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
6dccd16b
FR
5552 if ((p->mac_version == mac_version) && (p->clk == clk)) {
5553 RTL_W32(0x7c, p->val);
5554 break;
5555 }
5556 }
5557}
5558
e6b763ea
FR
5559static void rtl_set_rx_mode(struct net_device *dev)
5560{
5561 struct rtl8169_private *tp = netdev_priv(dev);
5562 void __iomem *ioaddr = tp->mmio_addr;
5563 u32 mc_filter[2]; /* Multicast hash filter */
5564 int rx_mode;
5565 u32 tmp = 0;
5566
5567 if (dev->flags & IFF_PROMISC) {
5568 /* Unconditionally log net taps. */
5569 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5570 rx_mode =
5571 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5572 AcceptAllPhys;
5573 mc_filter[1] = mc_filter[0] = 0xffffffff;
5574 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5575 (dev->flags & IFF_ALLMULTI)) {
5576 /* Too many to filter perfectly -- accept all multicasts. */
5577 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5578 mc_filter[1] = mc_filter[0] = 0xffffffff;
5579 } else {
5580 struct netdev_hw_addr *ha;
5581
5582 rx_mode = AcceptBroadcast | AcceptMyPhys;
5583 mc_filter[1] = mc_filter[0] = 0;
5584 netdev_for_each_mc_addr(ha, dev) {
5585 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5586 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5587 rx_mode |= AcceptMulticast;
5588 }
5589 }
5590
5591 if (dev->features & NETIF_F_RXALL)
5592 rx_mode |= (AcceptErr | AcceptRunt);
5593
5594 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
5595
5596 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5597 u32 data = mc_filter[0];
5598
5599 mc_filter[0] = swab32(mc_filter[1]);
5600 mc_filter[1] = swab32(data);
5601 }
5602
0481776b
NW
5603 if (tp->mac_version == RTL_GIGA_MAC_VER_35)
5604 mc_filter[1] = mc_filter[0] = 0xffffffff;
5605
e6b763ea
FR
5606 RTL_W32(MAR0 + 4, mc_filter[1]);
5607 RTL_W32(MAR0 + 0, mc_filter[0]);
5608
5609 RTL_W32(RxConfig, tmp);
5610}
5611
07ce4064
FR
5612static void rtl_hw_start_8169(struct net_device *dev)
5613{
5614 struct rtl8169_private *tp = netdev_priv(dev);
5615 void __iomem *ioaddr = tp->mmio_addr;
5616 struct pci_dev *pdev = tp->pci_dev;
07ce4064 5617
9cb427b6
FR
5618 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
5619 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
5620 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
5621 }
5622
1da177e4 5623 RTL_W8(Cfg9346, Cfg9346_Unlock);
cecb5fd7
FR
5624 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5625 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5626 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5627 tp->mac_version == RTL_GIGA_MAC_VER_04)
9cb427b6
FR
5628 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5629
e542a226
HW
5630 rtl_init_rxcfg(tp);
5631
f0298f81 5632 RTL_W8(EarlyTxThres, NoEarlyTx);
1da177e4 5633
6f0333b8 5634 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
1da177e4 5635
cecb5fd7
FR
5636 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
5637 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5638 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
5639 tp->mac_version == RTL_GIGA_MAC_VER_04)
c946b304 5640 rtl_set_rx_tx_config_registers(tp);
1da177e4 5641
7f796d83 5642 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
1da177e4 5643
cecb5fd7
FR
5644 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
5645 tp->mac_version == RTL_GIGA_MAC_VER_03) {
05b9687b 5646 dprintk("Set MAC Reg C+CR Offset 0xe0. "
1da177e4 5647 "Bit-3 and bit-14 MUST be 1\n");
bcf0bf90 5648 tp->cp_cmd |= (1 << 14);
1da177e4
LT
5649 }
5650
bcf0bf90
FR
5651 RTL_W16(CPlusCmd, tp->cp_cmd);
5652
6dccd16b
FR
5653 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
5654
1da177e4
LT
5655 /*
5656 * Undocumented corner. Supposedly:
5657 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
5658 */
5659 RTL_W16(IntrMitigate, 0x0000);
5660
7f796d83 5661 rtl_set_rx_tx_desc_registers(tp, ioaddr);
9cb427b6 5662
cecb5fd7
FR
5663 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
5664 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
5665 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
5666 tp->mac_version != RTL_GIGA_MAC_VER_04) {
c946b304
FR
5667 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
5668 rtl_set_rx_tx_config_registers(tp);
5669 }
5670
1da177e4 5671 RTL_W8(Cfg9346, Cfg9346_Lock);
b518fa8e
FR
5672
5673 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
5674 RTL_R8(IntrMask);
1da177e4
LT
5675
5676 RTL_W32(RxMissed, 0);
5677
07ce4064 5678 rtl_set_rx_mode(dev);
1da177e4
LT
5679
5680 /* no early-rx interrupts */
05b9687b 5681 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
07ce4064 5682}
1da177e4 5683
beb1fe18
HW
5684static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
5685{
5686 if (tp->csi_ops.write)
52989f0e 5687 tp->csi_ops.write(tp, addr, value);
beb1fe18
HW
5688}
5689
5690static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
5691{
52989f0e 5692 return tp->csi_ops.read ? tp->csi_ops.read(tp, addr) : ~0;
beb1fe18
HW
5693}
5694
5695static void rtl_csi_access_enable(struct rtl8169_private *tp, u32 bits)
dacf8154
FR
5696{
5697 u32 csi;
5698
beb1fe18
HW
5699 csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
5700 rtl_csi_write(tp, 0x070c, csi | bits);
5701}
5702
5703static void rtl_csi_access_enable_1(struct rtl8169_private *tp)
5704{
5705 rtl_csi_access_enable(tp, 0x17000000);
650e8d5d 5706}
5707
beb1fe18 5708static void rtl_csi_access_enable_2(struct rtl8169_private *tp)
e6de30d6 5709{
beb1fe18 5710 rtl_csi_access_enable(tp, 0x27000000);
e6de30d6 5711}
5712
ffc46952
FR
5713DECLARE_RTL_COND(rtl_csiar_cond)
5714{
5715 void __iomem *ioaddr = tp->mmio_addr;
5716
5717 return RTL_R32(CSIAR) & CSIAR_FLAG;
5718}
5719
52989f0e 5720static void r8169_csi_write(struct rtl8169_private *tp, int addr, int value)
650e8d5d 5721{
52989f0e 5722 void __iomem *ioaddr = tp->mmio_addr;
beb1fe18
HW
5723
5724 RTL_W32(CSIDR, value);
5725 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5726 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5727
ffc46952 5728 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
beb1fe18
HW
5729}
5730
52989f0e 5731static u32 r8169_csi_read(struct rtl8169_private *tp, int addr)
beb1fe18 5732{
52989f0e 5733 void __iomem *ioaddr = tp->mmio_addr;
beb1fe18
HW
5734
5735 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
5736 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5737
ffc46952
FR
5738 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5739 RTL_R32(CSIDR) : ~0;
beb1fe18
HW
5740}
5741
52989f0e 5742static void r8402_csi_write(struct rtl8169_private *tp, int addr, int value)
7e18dca1 5743{
52989f0e 5744 void __iomem *ioaddr = tp->mmio_addr;
7e18dca1
HW
5745
5746 RTL_W32(CSIDR, value);
5747 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5748 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5749 CSIAR_FUNC_NIC);
5750
ffc46952 5751 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
7e18dca1
HW
5752}
5753
52989f0e 5754static u32 r8402_csi_read(struct rtl8169_private *tp, int addr)
7e18dca1 5755{
52989f0e 5756 void __iomem *ioaddr = tp->mmio_addr;
7e18dca1
HW
5757
5758 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC |
5759 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5760
ffc46952
FR
5761 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5762 RTL_R32(CSIDR) : ~0;
7e18dca1
HW
5763}
5764
45dd95c4 5765static void r8411_csi_write(struct rtl8169_private *tp, int addr, int value)
5766{
5767 void __iomem *ioaddr = tp->mmio_addr;
5768
5769 RTL_W32(CSIDR, value);
5770 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
5771 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT |
5772 CSIAR_FUNC_NIC2);
5773
5774 rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
5775}
5776
5777static u32 r8411_csi_read(struct rtl8169_private *tp, int addr)
5778{
5779 void __iomem *ioaddr = tp->mmio_addr;
5780
5781 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) | CSIAR_FUNC_NIC2 |
5782 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
5783
5784 return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
5785 RTL_R32(CSIDR) : ~0;
5786}
5787
baf63293 5788static void rtl_init_csi_ops(struct rtl8169_private *tp)
beb1fe18
HW
5789{
5790 struct csi_ops *ops = &tp->csi_ops;
5791
5792 switch (tp->mac_version) {
5793 case RTL_GIGA_MAC_VER_01:
5794 case RTL_GIGA_MAC_VER_02:
5795 case RTL_GIGA_MAC_VER_03:
5796 case RTL_GIGA_MAC_VER_04:
5797 case RTL_GIGA_MAC_VER_05:
5798 case RTL_GIGA_MAC_VER_06:
5799 case RTL_GIGA_MAC_VER_10:
5800 case RTL_GIGA_MAC_VER_11:
5801 case RTL_GIGA_MAC_VER_12:
5802 case RTL_GIGA_MAC_VER_13:
5803 case RTL_GIGA_MAC_VER_14:
5804 case RTL_GIGA_MAC_VER_15:
5805 case RTL_GIGA_MAC_VER_16:
5806 case RTL_GIGA_MAC_VER_17:
5807 ops->write = NULL;
5808 ops->read = NULL;
5809 break;
5810
7e18dca1 5811 case RTL_GIGA_MAC_VER_37:
b3d7b2f2 5812 case RTL_GIGA_MAC_VER_38:
7e18dca1
HW
5813 ops->write = r8402_csi_write;
5814 ops->read = r8402_csi_read;
5815 break;
5816
45dd95c4 5817 case RTL_GIGA_MAC_VER_44:
5818 ops->write = r8411_csi_write;
5819 ops->read = r8411_csi_read;
5820 break;
5821
beb1fe18
HW
5822 default:
5823 ops->write = r8169_csi_write;
5824 ops->read = r8169_csi_read;
5825 break;
5826 }
dacf8154
FR
5827}
5828
5829struct ephy_info {
5830 unsigned int offset;
5831 u16 mask;
5832 u16 bits;
5833};
5834
fdf6fc06
FR
5835static void rtl_ephy_init(struct rtl8169_private *tp, const struct ephy_info *e,
5836 int len)
dacf8154
FR
5837{
5838 u16 w;
5839
5840 while (len-- > 0) {
fdf6fc06
FR
5841 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
5842 rtl_ephy_write(tp, e->offset, w);
dacf8154
FR
5843 e++;
5844 }
5845}
5846
b726e493
FR
5847static void rtl_disable_clock_request(struct pci_dev *pdev)
5848{
7d7903b2
JL
5849 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL,
5850 PCI_EXP_LNKCTL_CLKREQ_EN);
b726e493
FR
5851}
5852
e6de30d6 5853static void rtl_enable_clock_request(struct pci_dev *pdev)
5854{
7d7903b2
JL
5855 pcie_capability_set_word(pdev, PCI_EXP_LNKCTL,
5856 PCI_EXP_LNKCTL_CLKREQ_EN);
e6de30d6 5857}
5858
b51ecea8 5859static void rtl_pcie_state_l2l3_enable(struct rtl8169_private *tp, bool enable)
5860{
5861 void __iomem *ioaddr = tp->mmio_addr;
5862 u8 data;
5863
5864 data = RTL_R8(Config3);
5865
5866 if (enable)
5867 data |= Rdy_to_L23;
5868 else
5869 data &= ~Rdy_to_L23;
5870
5871 RTL_W8(Config3, data);
5872}
5873
b726e493
FR
5874#define R8168_CPCMD_QUIRK_MASK (\
5875 EnableBist | \
5876 Mac_dbgo_oe | \
5877 Force_half_dup | \
5878 Force_rxflow_en | \
5879 Force_txflow_en | \
5880 Cxpl_dbg_sel | \
5881 ASF | \
5882 PktCntrDisable | \
5883 Mac_dbgo_sel)
5884
beb1fe18 5885static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
219a1e9d 5886{
beb1fe18
HW
5887 void __iomem *ioaddr = tp->mmio_addr;
5888 struct pci_dev *pdev = tp->pci_dev;
5889
b726e493
FR
5890 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5891
5892 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5893
faf1e785 5894 if (tp->dev->mtu <= ETH_DATA_LEN) {
5895 rtl_tx_performance_tweak(pdev, (0x5 << MAX_READ_REQUEST_SHIFT) |
5896 PCI_EXP_DEVCTL_NOSNOOP_EN);
5897 }
219a1e9d
FR
5898}
5899
beb1fe18 5900static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
219a1e9d 5901{
beb1fe18
HW
5902 void __iomem *ioaddr = tp->mmio_addr;
5903
5904 rtl_hw_start_8168bb(tp);
b726e493 5905
f0298f81 5906 RTL_W8(MaxTxPacketSize, TxPacketMax);
b726e493
FR
5907
5908 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
219a1e9d
FR
5909}
5910
beb1fe18 5911static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
219a1e9d 5912{
beb1fe18
HW
5913 void __iomem *ioaddr = tp->mmio_addr;
5914 struct pci_dev *pdev = tp->pci_dev;
5915
b726e493
FR
5916 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
5917
5918 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5919
faf1e785 5920 if (tp->dev->mtu <= ETH_DATA_LEN)
5921 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
b726e493
FR
5922
5923 rtl_disable_clock_request(pdev);
5924
5925 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
219a1e9d
FR
5926}
5927
beb1fe18 5928static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
219a1e9d 5929{
350f7596 5930 static const struct ephy_info e_info_8168cp[] = {
b726e493
FR
5931 { 0x01, 0, 0x0001 },
5932 { 0x02, 0x0800, 0x1000 },
5933 { 0x03, 0, 0x0042 },
5934 { 0x06, 0x0080, 0x0000 },
5935 { 0x07, 0, 0x2000 }
5936 };
5937
beb1fe18 5938 rtl_csi_access_enable_2(tp);
b726e493 5939
fdf6fc06 5940 rtl_ephy_init(tp, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
b726e493 5941
beb1fe18 5942 __rtl_hw_start_8168cp(tp);
219a1e9d
FR
5943}
5944
beb1fe18 5945static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
ef3386f0 5946{
beb1fe18
HW
5947 void __iomem *ioaddr = tp->mmio_addr;
5948 struct pci_dev *pdev = tp->pci_dev;
5949
5950 rtl_csi_access_enable_2(tp);
ef3386f0
FR
5951
5952 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5953
faf1e785 5954 if (tp->dev->mtu <= ETH_DATA_LEN)
5955 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
ef3386f0
FR
5956
5957 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5958}
5959
beb1fe18 5960static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
7f3e3d3a 5961{
beb1fe18
HW
5962 void __iomem *ioaddr = tp->mmio_addr;
5963 struct pci_dev *pdev = tp->pci_dev;
5964
5965 rtl_csi_access_enable_2(tp);
7f3e3d3a
FR
5966
5967 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
5968
5969 /* Magic. */
5970 RTL_W8(DBG_REG, 0x20);
5971
f0298f81 5972 RTL_W8(MaxTxPacketSize, TxPacketMax);
7f3e3d3a 5973
faf1e785 5974 if (tp->dev->mtu <= ETH_DATA_LEN)
5975 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
7f3e3d3a
FR
5976
5977 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
5978}
5979
beb1fe18 5980static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
219a1e9d 5981{
beb1fe18 5982 void __iomem *ioaddr = tp->mmio_addr;
350f7596 5983 static const struct ephy_info e_info_8168c_1[] = {
b726e493
FR
5984 { 0x02, 0x0800, 0x1000 },
5985 { 0x03, 0, 0x0002 },
5986 { 0x06, 0x0080, 0x0000 }
5987 };
5988
beb1fe18 5989 rtl_csi_access_enable_2(tp);
b726e493
FR
5990
5991 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
5992
fdf6fc06 5993 rtl_ephy_init(tp, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
b726e493 5994
beb1fe18 5995 __rtl_hw_start_8168cp(tp);
219a1e9d
FR
5996}
5997
beb1fe18 5998static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
219a1e9d 5999{
350f7596 6000 static const struct ephy_info e_info_8168c_2[] = {
b726e493
FR
6001 { 0x01, 0, 0x0001 },
6002 { 0x03, 0x0400, 0x0220 }
6003 };
6004
beb1fe18 6005 rtl_csi_access_enable_2(tp);
b726e493 6006
fdf6fc06 6007 rtl_ephy_init(tp, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
b726e493 6008
beb1fe18 6009 __rtl_hw_start_8168cp(tp);
219a1e9d
FR
6010}
6011
beb1fe18 6012static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
197ff761 6013{
beb1fe18 6014 rtl_hw_start_8168c_2(tp);
197ff761
FR
6015}
6016
beb1fe18 6017static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
6fb07058 6018{
beb1fe18 6019 rtl_csi_access_enable_2(tp);
6fb07058 6020
beb1fe18 6021 __rtl_hw_start_8168cp(tp);
6fb07058
FR
6022}
6023
beb1fe18 6024static void rtl_hw_start_8168d(struct rtl8169_private *tp)
5b538df9 6025{
beb1fe18
HW
6026 void __iomem *ioaddr = tp->mmio_addr;
6027 struct pci_dev *pdev = tp->pci_dev;
6028
6029 rtl_csi_access_enable_2(tp);
5b538df9
FR
6030
6031 rtl_disable_clock_request(pdev);
6032
f0298f81 6033 RTL_W8(MaxTxPacketSize, TxPacketMax);
5b538df9 6034
faf1e785 6035 if (tp->dev->mtu <= ETH_DATA_LEN)
6036 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
5b538df9
FR
6037
6038 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
6039}
6040
beb1fe18 6041static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
4804b3b3 6042{
beb1fe18
HW
6043 void __iomem *ioaddr = tp->mmio_addr;
6044 struct pci_dev *pdev = tp->pci_dev;
6045
6046 rtl_csi_access_enable_1(tp);
4804b3b3 6047
faf1e785 6048 if (tp->dev->mtu <= ETH_DATA_LEN)
6049 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4804b3b3 6050
6051 RTL_W8(MaxTxPacketSize, TxPacketMax);
6052
6053 rtl_disable_clock_request(pdev);
6054}
6055
beb1fe18 6056static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
e6de30d6 6057{
beb1fe18
HW
6058 void __iomem *ioaddr = tp->mmio_addr;
6059 struct pci_dev *pdev = tp->pci_dev;
e6de30d6 6060 static const struct ephy_info e_info_8168d_4[] = {
1016a4a1
CHL
6061 { 0x0b, 0x0000, 0x0048 },
6062 { 0x19, 0x0020, 0x0050 },
6063 { 0x0c, 0x0100, 0x0020 }
e6de30d6 6064 };
e6de30d6 6065
beb1fe18 6066 rtl_csi_access_enable_1(tp);
e6de30d6 6067
6068 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6069
6070 RTL_W8(MaxTxPacketSize, TxPacketMax);
6071
1016a4a1 6072 rtl_ephy_init(tp, e_info_8168d_4, ARRAY_SIZE(e_info_8168d_4));
e6de30d6 6073
6074 rtl_enable_clock_request(pdev);
6075}
6076
beb1fe18 6077static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
01dc7fec 6078{
beb1fe18
HW
6079 void __iomem *ioaddr = tp->mmio_addr;
6080 struct pci_dev *pdev = tp->pci_dev;
70090424 6081 static const struct ephy_info e_info_8168e_1[] = {
01dc7fec 6082 { 0x00, 0x0200, 0x0100 },
6083 { 0x00, 0x0000, 0x0004 },
6084 { 0x06, 0x0002, 0x0001 },
6085 { 0x06, 0x0000, 0x0030 },
6086 { 0x07, 0x0000, 0x2000 },
6087 { 0x00, 0x0000, 0x0020 },
6088 { 0x03, 0x5800, 0x2000 },
6089 { 0x03, 0x0000, 0x0001 },
6090 { 0x01, 0x0800, 0x1000 },
6091 { 0x07, 0x0000, 0x4000 },
6092 { 0x1e, 0x0000, 0x2000 },
6093 { 0x19, 0xffff, 0xfe6c },
6094 { 0x0a, 0x0000, 0x0040 }
6095 };
6096
beb1fe18 6097 rtl_csi_access_enable_2(tp);
01dc7fec 6098
fdf6fc06 6099 rtl_ephy_init(tp, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
01dc7fec 6100
faf1e785 6101 if (tp->dev->mtu <= ETH_DATA_LEN)
6102 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
01dc7fec 6103
6104 RTL_W8(MaxTxPacketSize, TxPacketMax);
6105
6106 rtl_disable_clock_request(pdev);
6107
6108 /* Reset tx FIFO pointer */
cecb5fd7
FR
6109 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
6110 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
01dc7fec 6111
cecb5fd7 6112 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
01dc7fec 6113}
6114
beb1fe18 6115static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
70090424 6116{
beb1fe18
HW
6117 void __iomem *ioaddr = tp->mmio_addr;
6118 struct pci_dev *pdev = tp->pci_dev;
70090424
HW
6119 static const struct ephy_info e_info_8168e_2[] = {
6120 { 0x09, 0x0000, 0x0080 },
6121 { 0x19, 0x0000, 0x0224 }
6122 };
6123
beb1fe18 6124 rtl_csi_access_enable_1(tp);
70090424 6125
fdf6fc06 6126 rtl_ephy_init(tp, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
70090424 6127
faf1e785 6128 if (tp->dev->mtu <= ETH_DATA_LEN)
6129 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
70090424 6130
fdf6fc06
FR
6131 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6132 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6133 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6134 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6135 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6136 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
706123d0
CHL
6137 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6138 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
70090424 6139
3090bd9a 6140 RTL_W8(MaxTxPacketSize, EarlySize);
70090424 6141
4521e1a9
FR
6142 rtl_disable_clock_request(pdev);
6143
70090424
HW
6144 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6145 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6146
6147 /* Adjust EEE LED frequency */
6148 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6149
6150 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
6151 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4521e1a9 6152 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
70090424
HW
6153}
6154
5f886e08 6155static void rtl_hw_start_8168f(struct rtl8169_private *tp)
c2218925 6156{
beb1fe18
HW
6157 void __iomem *ioaddr = tp->mmio_addr;
6158 struct pci_dev *pdev = tp->pci_dev;
c2218925 6159
5f886e08 6160 rtl_csi_access_enable_2(tp);
c2218925
HW
6161
6162 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6163
fdf6fc06
FR
6164 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6165 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6166 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
6167 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
706123d0
CHL
6168 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6169 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6170 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
6171 rtl_w0w1_eri(tp, 0x1d0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
fdf6fc06
FR
6172 rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
6173 rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060, ERIAR_EXGMAC);
c2218925
HW
6174
6175 RTL_W8(MaxTxPacketSize, EarlySize);
6176
4521e1a9
FR
6177 rtl_disable_clock_request(pdev);
6178
c2218925
HW
6179 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6180 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
c2218925 6181 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4521e1a9
FR
6182 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
6183 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
c2218925
HW
6184}
6185
5f886e08
HW
6186static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
6187{
6188 void __iomem *ioaddr = tp->mmio_addr;
6189 static const struct ephy_info e_info_8168f_1[] = {
6190 { 0x06, 0x00c0, 0x0020 },
6191 { 0x08, 0x0001, 0x0002 },
6192 { 0x09, 0x0000, 0x0080 },
6193 { 0x19, 0x0000, 0x0224 }
6194 };
6195
6196 rtl_hw_start_8168f(tp);
6197
fdf6fc06 6198 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
5f886e08 6199
706123d0 6200 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00, ERIAR_EXGMAC);
5f886e08
HW
6201
6202 /* Adjust EEE LED frequency */
6203 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6204}
6205
b3d7b2f2
HW
6206static void rtl_hw_start_8411(struct rtl8169_private *tp)
6207{
b3d7b2f2
HW
6208 static const struct ephy_info e_info_8168f_1[] = {
6209 { 0x06, 0x00c0, 0x0020 },
6210 { 0x0f, 0xffff, 0x5200 },
6211 { 0x1e, 0x0000, 0x4000 },
6212 { 0x19, 0x0000, 0x0224 }
6213 };
6214
6215 rtl_hw_start_8168f(tp);
b51ecea8 6216 rtl_pcie_state_l2l3_enable(tp, false);
b3d7b2f2 6217
fdf6fc06 6218 rtl_ephy_init(tp, e_info_8168f_1, ARRAY_SIZE(e_info_8168f_1));
b3d7b2f2 6219
706123d0 6220 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0x0000, ERIAR_EXGMAC);
b3d7b2f2
HW
6221}
6222
5fbea337 6223static void rtl_hw_start_8168g(struct rtl8169_private *tp)
c558386b
HW
6224{
6225 void __iomem *ioaddr = tp->mmio_addr;
6226 struct pci_dev *pdev = tp->pci_dev;
6227
beb330a4 6228 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6229
c558386b
HW
6230 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x080002, ERIAR_EXGMAC);
6231 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6232 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6233 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6234
6235 rtl_csi_access_enable_1(tp);
6236
6237 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6238
706123d0
CHL
6239 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6240 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
beb330a4 6241 rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f, ERIAR_EXGMAC);
c558386b 6242
4521e1a9 6243 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
c558386b
HW
6244 RTL_W8(MaxTxPacketSize, EarlySize);
6245
6246 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6247 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6248
6249 /* Adjust EEE LED frequency */
6250 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6251
706123d0
CHL
6252 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6253 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
b51ecea8 6254
6255 rtl_pcie_state_l2l3_enable(tp, false);
c558386b
HW
6256}
6257
5fbea337
CHL
6258static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
6259{
6260 void __iomem *ioaddr = tp->mmio_addr;
6261 static const struct ephy_info e_info_8168g_1[] = {
6262 { 0x00, 0x0000, 0x0008 },
6263 { 0x0c, 0x37d0, 0x0820 },
6264 { 0x1e, 0x0000, 0x0001 },
6265 { 0x19, 0x8000, 0x0000 }
6266 };
6267
6268 rtl_hw_start_8168g(tp);
6269
6270 /* disable aspm and clock request before access ephy */
6271 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6272 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6273 rtl_ephy_init(tp, e_info_8168g_1, ARRAY_SIZE(e_info_8168g_1));
6274}
6275
57538c4a 6276static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
6277{
6278 void __iomem *ioaddr = tp->mmio_addr;
6279 static const struct ephy_info e_info_8168g_2[] = {
6280 { 0x00, 0x0000, 0x0008 },
6281 { 0x0c, 0x3df0, 0x0200 },
6282 { 0x19, 0xffff, 0xfc00 },
6283 { 0x1e, 0xffff, 0x20eb }
6284 };
6285
5fbea337 6286 rtl_hw_start_8168g(tp);
57538c4a 6287
6288 /* disable aspm and clock request before access ephy */
6289 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6290 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6291 rtl_ephy_init(tp, e_info_8168g_2, ARRAY_SIZE(e_info_8168g_2));
6292}
6293
45dd95c4 6294static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
6295{
6296 void __iomem *ioaddr = tp->mmio_addr;
6297 static const struct ephy_info e_info_8411_2[] = {
6298 { 0x00, 0x0000, 0x0008 },
6299 { 0x0c, 0x3df0, 0x0200 },
6300 { 0x0f, 0xffff, 0x5200 },
6301 { 0x19, 0x0020, 0x0000 },
6302 { 0x1e, 0x0000, 0x2000 }
6303 };
6304
5fbea337 6305 rtl_hw_start_8168g(tp);
45dd95c4 6306
6307 /* disable aspm and clock request before access ephy */
6308 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6309 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6310 rtl_ephy_init(tp, e_info_8411_2, ARRAY_SIZE(e_info_8411_2));
6311}
6312
6e1d0b89
CHL
6313static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
6314{
6315 void __iomem *ioaddr = tp->mmio_addr;
6316 struct pci_dev *pdev = tp->pci_dev;
72521ea0 6317 int rg_saw_cnt;
6e1d0b89
CHL
6318 u32 data;
6319 static const struct ephy_info e_info_8168h_1[] = {
6320 { 0x1e, 0x0800, 0x0001 },
6321 { 0x1d, 0x0000, 0x0800 },
6322 { 0x05, 0xffff, 0x2089 },
6323 { 0x06, 0xffff, 0x5881 },
6324 { 0x04, 0xffff, 0x154a },
6325 { 0x01, 0xffff, 0x068b }
6326 };
6327
6328 /* disable aspm and clock request before access ephy */
6329 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6330 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6331 rtl_ephy_init(tp, e_info_8168h_1, ARRAY_SIZE(e_info_8168h_1));
6332
6333 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6334
6335 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6336 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x38, ERIAR_EXGMAC);
6337 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x48, ERIAR_EXGMAC);
6338 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6339
6340 rtl_csi_access_enable_1(tp);
6341
6342 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6343
706123d0
CHL
6344 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6345 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6e1d0b89 6346
706123d0 6347 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_1111, 0x0010, 0x00, ERIAR_EXGMAC);
6e1d0b89 6348
706123d0 6349 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f00, 0x00, ERIAR_EXGMAC);
6e1d0b89
CHL
6350
6351 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6352
6e1d0b89
CHL
6353 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6354 RTL_W8(MaxTxPacketSize, EarlySize);
6355
6356 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6357 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6358
6359 /* Adjust EEE LED frequency */
6360 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6361
6362 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
69f3dc37 6363 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
6e1d0b89
CHL
6364
6365 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6366
706123d0 6367 rtl_w0w1_eri(tp, 0x1b0, ERIAR_MASK_0011, 0x0000, 0x1000, ERIAR_EXGMAC);
6e1d0b89
CHL
6368
6369 rtl_pcie_state_l2l3_enable(tp, false);
6370
6371 rtl_writephy(tp, 0x1f, 0x0c42);
58493333 6372 rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
6e1d0b89
CHL
6373 rtl_writephy(tp, 0x1f, 0x0000);
6374 if (rg_saw_cnt > 0) {
6375 u16 sw_cnt_1ms_ini;
6376
6377 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
6378 sw_cnt_1ms_ini &= 0x0fff;
6379 data = r8168_mac_ocp_read(tp, 0xd412);
a2cb7ec0 6380 data &= ~0x0fff;
6e1d0b89
CHL
6381 data |= sw_cnt_1ms_ini;
6382 r8168_mac_ocp_write(tp, 0xd412, data);
6383 }
6384
6385 data = r8168_mac_ocp_read(tp, 0xe056);
a2cb7ec0
CHL
6386 data &= ~0xf0;
6387 data |= 0x70;
6e1d0b89
CHL
6388 r8168_mac_ocp_write(tp, 0xe056, data);
6389
6390 data = r8168_mac_ocp_read(tp, 0xe052);
a2cb7ec0
CHL
6391 data &= ~0x6000;
6392 data |= 0x8008;
6e1d0b89
CHL
6393 r8168_mac_ocp_write(tp, 0xe052, data);
6394
6395 data = r8168_mac_ocp_read(tp, 0xe0d6);
a2cb7ec0 6396 data &= ~0x01ff;
6e1d0b89
CHL
6397 data |= 0x017f;
6398 r8168_mac_ocp_write(tp, 0xe0d6, data);
6399
6400 data = r8168_mac_ocp_read(tp, 0xd420);
a2cb7ec0 6401 data &= ~0x0fff;
6e1d0b89
CHL
6402 data |= 0x047f;
6403 r8168_mac_ocp_write(tp, 0xd420, data);
6404
6405 r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
6406 r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
6407 r8168_mac_ocp_write(tp, 0xc094, 0x0000);
6408 r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
6409}
6410
935e2218
CHL
6411static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
6412{
6413 void __iomem *ioaddr = tp->mmio_addr;
6414 struct pci_dev *pdev = tp->pci_dev;
6415
003609da
CHL
6416 rtl8168ep_stop_cmac(tp);
6417
935e2218
CHL
6418 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6419
6420 rtl_eri_write(tp, 0xc8, ERIAR_MASK_0101, 0x00080002, ERIAR_EXGMAC);
6421 rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, 0x2f, ERIAR_EXGMAC);
6422 rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, 0x5f, ERIAR_EXGMAC);
6423 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
6424
6425 rtl_csi_access_enable_1(tp);
6426
6427 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6428
6429 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6430 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
6431
6432 rtl_w0w1_eri(tp, 0xd4, ERIAR_MASK_1111, 0x1f80, 0x00, ERIAR_EXGMAC);
6433
6434 rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87, ERIAR_EXGMAC);
6435
935e2218
CHL
6436 RTL_W32(MISC, RTL_R32(MISC) & ~RXDV_GATED_EN);
6437 RTL_W8(MaxTxPacketSize, EarlySize);
6438
6439 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6440 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6441
6442 /* Adjust EEE LED frequency */
6443 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
6444
6445 rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06, ERIAR_EXGMAC);
6446
6447 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~TX_10M_PS_EN);
6448
6449 rtl_pcie_state_l2l3_enable(tp, false);
6450}
6451
6452static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
6453{
6454 void __iomem *ioaddr = tp->mmio_addr;
6455 static const struct ephy_info e_info_8168ep_1[] = {
6456 { 0x00, 0xffff, 0x10ab },
6457 { 0x06, 0xffff, 0xf030 },
6458 { 0x08, 0xffff, 0x2006 },
6459 { 0x0d, 0xffff, 0x1666 },
6460 { 0x0c, 0x3ff0, 0x0000 }
6461 };
6462
6463 /* disable aspm and clock request before access ephy */
6464 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6465 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6466 rtl_ephy_init(tp, e_info_8168ep_1, ARRAY_SIZE(e_info_8168ep_1));
6467
6468 rtl_hw_start_8168ep(tp);
6469}
6470
6471static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
6472{
6473 void __iomem *ioaddr = tp->mmio_addr;
6474 static const struct ephy_info e_info_8168ep_2[] = {
6475 { 0x00, 0xffff, 0x10a3 },
6476 { 0x19, 0xffff, 0xfc00 },
6477 { 0x1e, 0xffff, 0x20ea }
6478 };
6479
6480 /* disable aspm and clock request before access ephy */
6481 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6482 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6483 rtl_ephy_init(tp, e_info_8168ep_2, ARRAY_SIZE(e_info_8168ep_2));
6484
6485 rtl_hw_start_8168ep(tp);
6486
6487 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
69f3dc37 6488 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
935e2218
CHL
6489}
6490
6491static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
6492{
6493 void __iomem *ioaddr = tp->mmio_addr;
6494 u32 data;
6495 static const struct ephy_info e_info_8168ep_3[] = {
6496 { 0x00, 0xffff, 0x10a3 },
6497 { 0x19, 0xffff, 0x7c00 },
6498 { 0x1e, 0xffff, 0x20eb },
6499 { 0x0d, 0xffff, 0x1666 }
6500 };
6501
6502 /* disable aspm and clock request before access ephy */
6503 RTL_W8(Config2, RTL_R8(Config2) & ~ClkReqEn);
6504 RTL_W8(Config5, RTL_R8(Config5) & ~ASPM_en);
6505 rtl_ephy_init(tp, e_info_8168ep_3, ARRAY_SIZE(e_info_8168ep_3));
6506
6507 rtl_hw_start_8168ep(tp);
6508
6509 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
69f3dc37 6510 RTL_W8(MISC_1, RTL_R8(MISC_1) & ~PFM_D3COLD_EN);
935e2218
CHL
6511
6512 data = r8168_mac_ocp_read(tp, 0xd3e2);
6513 data &= 0xf000;
6514 data |= 0x0271;
6515 r8168_mac_ocp_write(tp, 0xd3e2, data);
6516
6517 data = r8168_mac_ocp_read(tp, 0xd3e4);
6518 data &= 0xff00;
6519 r8168_mac_ocp_write(tp, 0xd3e4, data);
6520
6521 data = r8168_mac_ocp_read(tp, 0xe860);
6522 data |= 0x0080;
6523 r8168_mac_ocp_write(tp, 0xe860, data);
6524}
6525
07ce4064
FR
6526static void rtl_hw_start_8168(struct net_device *dev)
6527{
2dd99530
FR
6528 struct rtl8169_private *tp = netdev_priv(dev);
6529 void __iomem *ioaddr = tp->mmio_addr;
6530
6531 RTL_W8(Cfg9346, Cfg9346_Unlock);
6532
f0298f81 6533 RTL_W8(MaxTxPacketSize, TxPacketMax);
2dd99530 6534
6f0333b8 6535 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
2dd99530 6536
0e485150 6537 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
2dd99530
FR
6538
6539 RTL_W16(CPlusCmd, tp->cp_cmd);
6540
0e485150 6541 RTL_W16(IntrMitigate, 0x5151);
2dd99530 6542
0e485150 6543 /* Work around for RxFIFO overflow. */
811fd301 6544 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
da78dbff
FR
6545 tp->event_slow |= RxFIFOOver | PCSTimeout;
6546 tp->event_slow &= ~RxOverflow;
0e485150
FR
6547 }
6548
6549 rtl_set_rx_tx_desc_registers(tp, ioaddr);
2dd99530 6550
1a964649 6551 rtl_set_rx_tx_config_registers(tp);
2dd99530
FR
6552
6553 RTL_R8(IntrMask);
6554
219a1e9d
FR
6555 switch (tp->mac_version) {
6556 case RTL_GIGA_MAC_VER_11:
beb1fe18 6557 rtl_hw_start_8168bb(tp);
4804b3b3 6558 break;
219a1e9d
FR
6559
6560 case RTL_GIGA_MAC_VER_12:
6561 case RTL_GIGA_MAC_VER_17:
beb1fe18 6562 rtl_hw_start_8168bef(tp);
4804b3b3 6563 break;
219a1e9d
FR
6564
6565 case RTL_GIGA_MAC_VER_18:
beb1fe18 6566 rtl_hw_start_8168cp_1(tp);
4804b3b3 6567 break;
219a1e9d
FR
6568
6569 case RTL_GIGA_MAC_VER_19:
beb1fe18 6570 rtl_hw_start_8168c_1(tp);
4804b3b3 6571 break;
219a1e9d
FR
6572
6573 case RTL_GIGA_MAC_VER_20:
beb1fe18 6574 rtl_hw_start_8168c_2(tp);
4804b3b3 6575 break;
219a1e9d 6576
197ff761 6577 case RTL_GIGA_MAC_VER_21:
beb1fe18 6578 rtl_hw_start_8168c_3(tp);
4804b3b3 6579 break;
197ff761 6580
6fb07058 6581 case RTL_GIGA_MAC_VER_22:
beb1fe18 6582 rtl_hw_start_8168c_4(tp);
4804b3b3 6583 break;
6fb07058 6584
ef3386f0 6585 case RTL_GIGA_MAC_VER_23:
beb1fe18 6586 rtl_hw_start_8168cp_2(tp);
4804b3b3 6587 break;
ef3386f0 6588
7f3e3d3a 6589 case RTL_GIGA_MAC_VER_24:
beb1fe18 6590 rtl_hw_start_8168cp_3(tp);
4804b3b3 6591 break;
7f3e3d3a 6592
5b538df9 6593 case RTL_GIGA_MAC_VER_25:
daf9df6d 6594 case RTL_GIGA_MAC_VER_26:
6595 case RTL_GIGA_MAC_VER_27:
beb1fe18 6596 rtl_hw_start_8168d(tp);
4804b3b3 6597 break;
5b538df9 6598
e6de30d6 6599 case RTL_GIGA_MAC_VER_28:
beb1fe18 6600 rtl_hw_start_8168d_4(tp);
4804b3b3 6601 break;
cecb5fd7 6602
4804b3b3 6603 case RTL_GIGA_MAC_VER_31:
beb1fe18 6604 rtl_hw_start_8168dp(tp);
4804b3b3 6605 break;
6606
01dc7fec 6607 case RTL_GIGA_MAC_VER_32:
6608 case RTL_GIGA_MAC_VER_33:
beb1fe18 6609 rtl_hw_start_8168e_1(tp);
70090424
HW
6610 break;
6611 case RTL_GIGA_MAC_VER_34:
beb1fe18 6612 rtl_hw_start_8168e_2(tp);
01dc7fec 6613 break;
e6de30d6 6614
c2218925
HW
6615 case RTL_GIGA_MAC_VER_35:
6616 case RTL_GIGA_MAC_VER_36:
beb1fe18 6617 rtl_hw_start_8168f_1(tp);
c2218925
HW
6618 break;
6619
b3d7b2f2
HW
6620 case RTL_GIGA_MAC_VER_38:
6621 rtl_hw_start_8411(tp);
6622 break;
6623
c558386b
HW
6624 case RTL_GIGA_MAC_VER_40:
6625 case RTL_GIGA_MAC_VER_41:
6626 rtl_hw_start_8168g_1(tp);
6627 break;
57538c4a 6628 case RTL_GIGA_MAC_VER_42:
6629 rtl_hw_start_8168g_2(tp);
6630 break;
c558386b 6631
45dd95c4 6632 case RTL_GIGA_MAC_VER_44:
6633 rtl_hw_start_8411_2(tp);
6634 break;
6635
6e1d0b89
CHL
6636 case RTL_GIGA_MAC_VER_45:
6637 case RTL_GIGA_MAC_VER_46:
6638 rtl_hw_start_8168h_1(tp);
6639 break;
6640
935e2218
CHL
6641 case RTL_GIGA_MAC_VER_49:
6642 rtl_hw_start_8168ep_1(tp);
6643 break;
6644
6645 case RTL_GIGA_MAC_VER_50:
6646 rtl_hw_start_8168ep_2(tp);
6647 break;
6648
6649 case RTL_GIGA_MAC_VER_51:
6650 rtl_hw_start_8168ep_3(tp);
6651 break;
6652
219a1e9d
FR
6653 default:
6654 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
6655 dev->name, tp->mac_version);
4804b3b3 6656 break;
219a1e9d 6657 }
2dd99530 6658
1a964649 6659 RTL_W8(Cfg9346, Cfg9346_Lock);
6660
0e485150
FR
6661 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
6662
1a964649 6663 rtl_set_rx_mode(dev);
b8363901 6664
05b9687b 6665 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
07ce4064 6666}
1da177e4 6667
2857ffb7
FR
6668#define R810X_CPCMD_QUIRK_MASK (\
6669 EnableBist | \
6670 Mac_dbgo_oe | \
6671 Force_half_dup | \
5edcc537 6672 Force_rxflow_en | \
2857ffb7
FR
6673 Force_txflow_en | \
6674 Cxpl_dbg_sel | \
6675 ASF | \
6676 PktCntrDisable | \
d24e9aaf 6677 Mac_dbgo_sel)
2857ffb7 6678
beb1fe18 6679static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
2857ffb7 6680{
beb1fe18
HW
6681 void __iomem *ioaddr = tp->mmio_addr;
6682 struct pci_dev *pdev = tp->pci_dev;
350f7596 6683 static const struct ephy_info e_info_8102e_1[] = {
2857ffb7
FR
6684 { 0x01, 0, 0x6e65 },
6685 { 0x02, 0, 0x091f },
6686 { 0x03, 0, 0xc2f9 },
6687 { 0x06, 0, 0xafb5 },
6688 { 0x07, 0, 0x0e00 },
6689 { 0x19, 0, 0xec80 },
6690 { 0x01, 0, 0x2e65 },
6691 { 0x01, 0, 0x6e65 }
6692 };
6693 u8 cfg1;
6694
beb1fe18 6695 rtl_csi_access_enable_2(tp);
2857ffb7
FR
6696
6697 RTL_W8(DBG_REG, FIX_NAK_1);
6698
6699 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6700
6701 RTL_W8(Config1,
6702 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
6703 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
6704
6705 cfg1 = RTL_R8(Config1);
6706 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
6707 RTL_W8(Config1, cfg1 & ~LEDS0);
6708
fdf6fc06 6709 rtl_ephy_init(tp, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
2857ffb7
FR
6710}
6711
beb1fe18 6712static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
2857ffb7 6713{
beb1fe18
HW
6714 void __iomem *ioaddr = tp->mmio_addr;
6715 struct pci_dev *pdev = tp->pci_dev;
6716
6717 rtl_csi_access_enable_2(tp);
2857ffb7
FR
6718
6719 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
6720
6721 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
6722 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
2857ffb7
FR
6723}
6724
beb1fe18 6725static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
2857ffb7 6726{
beb1fe18 6727 rtl_hw_start_8102e_2(tp);
2857ffb7 6728
fdf6fc06 6729 rtl_ephy_write(tp, 0x03, 0xc2f9);
2857ffb7
FR
6730}
6731
beb1fe18 6732static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5a5e4443 6733{
beb1fe18 6734 void __iomem *ioaddr = tp->mmio_addr;
5a5e4443
HW
6735 static const struct ephy_info e_info_8105e_1[] = {
6736 { 0x07, 0, 0x4000 },
6737 { 0x19, 0, 0x0200 },
6738 { 0x19, 0, 0x0020 },
6739 { 0x1e, 0, 0x2000 },
6740 { 0x03, 0, 0x0001 },
6741 { 0x19, 0, 0x0100 },
6742 { 0x19, 0, 0x0004 },
6743 { 0x0a, 0, 0x0020 }
6744 };
6745
cecb5fd7 6746 /* Force LAN exit from ASPM if Rx/Tx are not idle */
5a5e4443
HW
6747 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6748
cecb5fd7 6749 /* Disable Early Tally Counter */
5a5e4443
HW
6750 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
6751
6752 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4f6b00e5 6753 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
5a5e4443 6754
fdf6fc06 6755 rtl_ephy_init(tp, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
b51ecea8 6756
6757 rtl_pcie_state_l2l3_enable(tp, false);
5a5e4443
HW
6758}
6759
beb1fe18 6760static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5a5e4443 6761{
beb1fe18 6762 rtl_hw_start_8105e_1(tp);
fdf6fc06 6763 rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
5a5e4443
HW
6764}
6765
7e18dca1
HW
6766static void rtl_hw_start_8402(struct rtl8169_private *tp)
6767{
6768 void __iomem *ioaddr = tp->mmio_addr;
6769 static const struct ephy_info e_info_8402[] = {
6770 { 0x19, 0xffff, 0xff64 },
6771 { 0x1e, 0, 0x4000 }
6772 };
6773
6774 rtl_csi_access_enable_2(tp);
6775
6776 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6777 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6778
6779 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
6780 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
6781
fdf6fc06 6782 rtl_ephy_init(tp, e_info_8402, ARRAY_SIZE(e_info_8402));
7e18dca1
HW
6783
6784 rtl_tx_performance_tweak(tp->pci_dev, 0x5 << MAX_READ_REQUEST_SHIFT);
6785
fdf6fc06
FR
6786 rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, 0x00000002, ERIAR_EXGMAC);
6787 rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, 0x00000006, ERIAR_EXGMAC);
706123d0
CHL
6788 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x00, 0x01, ERIAR_EXGMAC);
6789 rtl_w0w1_eri(tp, 0xdc, ERIAR_MASK_0001, 0x01, 0x00, ERIAR_EXGMAC);
fdf6fc06
FR
6790 rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
6791 rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
706123d0 6792 rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00, ERIAR_EXGMAC);
b51ecea8 6793
6794 rtl_pcie_state_l2l3_enable(tp, false);
7e18dca1
HW
6795}
6796
5598bfe5
HW
6797static void rtl_hw_start_8106(struct rtl8169_private *tp)
6798{
6799 void __iomem *ioaddr = tp->mmio_addr;
6800
6801 /* Force LAN exit from ASPM if Rx/Tx are not idle */
6802 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
6803
4521e1a9 6804 RTL_W32(MISC, (RTL_R32(MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5598bfe5
HW
6805 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
6806 RTL_W8(DLLPR, RTL_R8(DLLPR) & ~PFM_EN);
b51ecea8 6807
6808 rtl_pcie_state_l2l3_enable(tp, false);
5598bfe5
HW
6809}
6810
07ce4064
FR
6811static void rtl_hw_start_8101(struct net_device *dev)
6812{
cdf1a608
FR
6813 struct rtl8169_private *tp = netdev_priv(dev);
6814 void __iomem *ioaddr = tp->mmio_addr;
6815 struct pci_dev *pdev = tp->pci_dev;
6816
da78dbff
FR
6817 if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
6818 tp->event_slow &= ~RxFIFOOver;
811fd301 6819
cecb5fd7 6820 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
7d7903b2 6821 tp->mac_version == RTL_GIGA_MAC_VER_16)
8200bc72
BH
6822 pcie_capability_set_word(pdev, PCI_EXP_DEVCTL,
6823 PCI_EXP_DEVCTL_NOSNOOP_EN);
cdf1a608 6824
d24e9aaf
HW
6825 RTL_W8(Cfg9346, Cfg9346_Unlock);
6826
1a964649 6827 RTL_W8(MaxTxPacketSize, TxPacketMax);
6828
6829 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
6830
6831 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
6832 RTL_W16(CPlusCmd, tp->cp_cmd);
6833
6834 rtl_set_rx_tx_desc_registers(tp, ioaddr);
6835
6836 rtl_set_rx_tx_config_registers(tp);
6837
2857ffb7
FR
6838 switch (tp->mac_version) {
6839 case RTL_GIGA_MAC_VER_07:
beb1fe18 6840 rtl_hw_start_8102e_1(tp);
2857ffb7
FR
6841 break;
6842
6843 case RTL_GIGA_MAC_VER_08:
beb1fe18 6844 rtl_hw_start_8102e_3(tp);
2857ffb7
FR
6845 break;
6846
6847 case RTL_GIGA_MAC_VER_09:
beb1fe18 6848 rtl_hw_start_8102e_2(tp);
2857ffb7 6849 break;
5a5e4443
HW
6850
6851 case RTL_GIGA_MAC_VER_29:
beb1fe18 6852 rtl_hw_start_8105e_1(tp);
5a5e4443
HW
6853 break;
6854 case RTL_GIGA_MAC_VER_30:
beb1fe18 6855 rtl_hw_start_8105e_2(tp);
5a5e4443 6856 break;
7e18dca1
HW
6857
6858 case RTL_GIGA_MAC_VER_37:
6859 rtl_hw_start_8402(tp);
6860 break;
5598bfe5
HW
6861
6862 case RTL_GIGA_MAC_VER_39:
6863 rtl_hw_start_8106(tp);
6864 break;
58152cd4 6865 case RTL_GIGA_MAC_VER_43:
6866 rtl_hw_start_8168g_2(tp);
6867 break;
6e1d0b89
CHL
6868 case RTL_GIGA_MAC_VER_47:
6869 case RTL_GIGA_MAC_VER_48:
6870 rtl_hw_start_8168h_1(tp);
6871 break;
cdf1a608
FR
6872 }
6873
d24e9aaf 6874 RTL_W8(Cfg9346, Cfg9346_Lock);
cdf1a608 6875
cdf1a608
FR
6876 RTL_W16(IntrMitigate, 0x0000);
6877
cdf1a608 6878 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
cdf1a608 6879
cdf1a608
FR
6880 rtl_set_rx_mode(dev);
6881
1a964649 6882 RTL_R8(IntrMask);
6883
cdf1a608 6884 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
1da177e4
LT
6885}
6886
6887static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
6888{
d58d46b5
FR
6889 struct rtl8169_private *tp = netdev_priv(dev);
6890
d58d46b5
FR
6891 if (new_mtu > ETH_DATA_LEN)
6892 rtl_hw_jumbo_enable(tp);
6893 else
6894 rtl_hw_jumbo_disable(tp);
6895
1da177e4 6896 dev->mtu = new_mtu;
350fb32a
MM
6897 netdev_update_features(dev);
6898
323bb685 6899 return 0;
1da177e4
LT
6900}
6901
6902static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
6903{
95e0918d 6904 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
1da177e4
LT
6905 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
6906}
6907
6f0333b8
ED
6908static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
6909 void **data_buff, struct RxDesc *desc)
1da177e4 6910{
48addcc9 6911 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
231aee63 6912 DMA_FROM_DEVICE);
48addcc9 6913
6f0333b8
ED
6914 kfree(*data_buff);
6915 *data_buff = NULL;
1da177e4
LT
6916 rtl8169_make_unusable_by_asic(desc);
6917}
6918
6919static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
6920{
6921 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
6922
a0750138
AD
6923 /* Force memory writes to complete before releasing descriptor */
6924 dma_wmb();
6925
1da177e4
LT
6926 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
6927}
6928
6929static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
6930 u32 rx_buf_sz)
6931{
6932 desc->addr = cpu_to_le64(mapping);
1da177e4
LT
6933 rtl8169_mark_to_asic(desc, rx_buf_sz);
6934}
6935
6f0333b8
ED
6936static inline void *rtl8169_align(void *data)
6937{
6938 return (void *)ALIGN((long)data, 16);
6939}
6940
0ecbe1ca
SG
6941static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
6942 struct RxDesc *desc)
1da177e4 6943{
6f0333b8 6944 void *data;
1da177e4 6945 dma_addr_t mapping;
48addcc9 6946 struct device *d = &tp->pci_dev->dev;
0ecbe1ca 6947 struct net_device *dev = tp->dev;
6f0333b8 6948 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
1da177e4 6949
6f0333b8
ED
6950 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
6951 if (!data)
6952 return NULL;
e9f63f30 6953
6f0333b8
ED
6954 if (rtl8169_align(data) != data) {
6955 kfree(data);
6956 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
6957 if (!data)
6958 return NULL;
6959 }
3eafe507 6960
48addcc9 6961 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
231aee63 6962 DMA_FROM_DEVICE);
d827d86b
SG
6963 if (unlikely(dma_mapping_error(d, mapping))) {
6964 if (net_ratelimit())
6965 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
3eafe507 6966 goto err_out;
d827d86b 6967 }
1da177e4
LT
6968
6969 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
6f0333b8 6970 return data;
3eafe507
SG
6971
6972err_out:
6973 kfree(data);
6974 return NULL;
1da177e4
LT
6975}
6976
6977static void rtl8169_rx_clear(struct rtl8169_private *tp)
6978{
07d3f51f 6979 unsigned int i;
1da177e4
LT
6980
6981 for (i = 0; i < NUM_RX_DESC; i++) {
6f0333b8
ED
6982 if (tp->Rx_databuff[i]) {
6983 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
1da177e4
LT
6984 tp->RxDescArray + i);
6985 }
6986 }
6987}
6988
0ecbe1ca 6989static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
1da177e4 6990{
0ecbe1ca
SG
6991 desc->opts1 |= cpu_to_le32(RingEnd);
6992}
5b0384f4 6993
0ecbe1ca
SG
6994static int rtl8169_rx_fill(struct rtl8169_private *tp)
6995{
6996 unsigned int i;
1da177e4 6997
0ecbe1ca
SG
6998 for (i = 0; i < NUM_RX_DESC; i++) {
6999 void *data;
4ae47c2d 7000
6f0333b8 7001 if (tp->Rx_databuff[i])
1da177e4 7002 continue;
bcf0bf90 7003
0ecbe1ca 7004 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
6f0333b8
ED
7005 if (!data) {
7006 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
0ecbe1ca 7007 goto err_out;
6f0333b8
ED
7008 }
7009 tp->Rx_databuff[i] = data;
1da177e4 7010 }
1da177e4 7011
0ecbe1ca
SG
7012 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
7013 return 0;
7014
7015err_out:
7016 rtl8169_rx_clear(tp);
7017 return -ENOMEM;
1da177e4
LT
7018}
7019
1da177e4
LT
7020static int rtl8169_init_ring(struct net_device *dev)
7021{
7022 struct rtl8169_private *tp = netdev_priv(dev);
7023
7024 rtl8169_init_ring_indexes(tp);
7025
7026 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
6f0333b8 7027 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
1da177e4 7028
0ecbe1ca 7029 return rtl8169_rx_fill(tp);
1da177e4
LT
7030}
7031
48addcc9 7032static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
1da177e4
LT
7033 struct TxDesc *desc)
7034{
7035 unsigned int len = tx_skb->len;
7036
48addcc9
SG
7037 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
7038
1da177e4
LT
7039 desc->opts1 = 0x00;
7040 desc->opts2 = 0x00;
7041 desc->addr = 0x00;
7042 tx_skb->len = 0;
7043}
7044
3eafe507
SG
7045static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
7046 unsigned int n)
1da177e4
LT
7047{
7048 unsigned int i;
7049
3eafe507
SG
7050 for (i = 0; i < n; i++) {
7051 unsigned int entry = (start + i) % NUM_TX_DESC;
1da177e4
LT
7052 struct ring_info *tx_skb = tp->tx_skb + entry;
7053 unsigned int len = tx_skb->len;
7054
7055 if (len) {
7056 struct sk_buff *skb = tx_skb->skb;
7057
48addcc9 7058 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
1da177e4
LT
7059 tp->TxDescArray + entry);
7060 if (skb) {
7a4b813c 7061 dev_consume_skb_any(skb);
1da177e4
LT
7062 tx_skb->skb = NULL;
7063 }
1da177e4
LT
7064 }
7065 }
3eafe507
SG
7066}
7067
7068static void rtl8169_tx_clear(struct rtl8169_private *tp)
7069{
7070 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
1da177e4
LT
7071 tp->cur_tx = tp->dirty_tx = 0;
7072}
7073
4422bcd4 7074static void rtl_reset_work(struct rtl8169_private *tp)
1da177e4 7075{
c4028958 7076 struct net_device *dev = tp->dev;
56de414c 7077 int i;
1da177e4 7078
da78dbff
FR
7079 napi_disable(&tp->napi);
7080 netif_stop_queue(dev);
7081 synchronize_sched();
1da177e4 7082
c7c2c39b 7083 rtl8169_hw_reset(tp);
7084
56de414c
FR
7085 for (i = 0; i < NUM_RX_DESC; i++)
7086 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
7087
1da177e4 7088 rtl8169_tx_clear(tp);
c7c2c39b 7089 rtl8169_init_ring_indexes(tp);
1da177e4 7090
da78dbff 7091 napi_enable(&tp->napi);
56de414c
FR
7092 rtl_hw_start(dev);
7093 netif_wake_queue(dev);
7094 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
1da177e4
LT
7095}
7096
7097static void rtl8169_tx_timeout(struct net_device *dev)
7098{
da78dbff
FR
7099 struct rtl8169_private *tp = netdev_priv(dev);
7100
7101 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
1da177e4
LT
7102}
7103
7104static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
2b7b4318 7105 u32 *opts)
1da177e4
LT
7106{
7107 struct skb_shared_info *info = skb_shinfo(skb);
7108 unsigned int cur_frag, entry;
6e1d0b89 7109 struct TxDesc *uninitialized_var(txd);
48addcc9 7110 struct device *d = &tp->pci_dev->dev;
1da177e4
LT
7111
7112 entry = tp->cur_tx;
7113 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
9e903e08 7114 const skb_frag_t *frag = info->frags + cur_frag;
1da177e4
LT
7115 dma_addr_t mapping;
7116 u32 status, len;
7117 void *addr;
7118
7119 entry = (entry + 1) % NUM_TX_DESC;
7120
7121 txd = tp->TxDescArray + entry;
9e903e08 7122 len = skb_frag_size(frag);
929f6189 7123 addr = skb_frag_address(frag);
48addcc9 7124 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
d827d86b
SG
7125 if (unlikely(dma_mapping_error(d, mapping))) {
7126 if (net_ratelimit())
7127 netif_err(tp, drv, tp->dev,
7128 "Failed to map TX fragments DMA!\n");
3eafe507 7129 goto err_out;
d827d86b 7130 }
1da177e4 7131
cecb5fd7 7132 /* Anti gcc 2.95.3 bugware (sic) */
2b7b4318
FR
7133 status = opts[0] | len |
7134 (RingEnd * !((entry + 1) % NUM_TX_DESC));
1da177e4
LT
7135
7136 txd->opts1 = cpu_to_le32(status);
2b7b4318 7137 txd->opts2 = cpu_to_le32(opts[1]);
1da177e4
LT
7138 txd->addr = cpu_to_le64(mapping);
7139
7140 tp->tx_skb[entry].len = len;
7141 }
7142
7143 if (cur_frag) {
7144 tp->tx_skb[entry].skb = skb;
7145 txd->opts1 |= cpu_to_le32(LastFrag);
7146 }
7147
7148 return cur_frag;
3eafe507
SG
7149
7150err_out:
7151 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
7152 return -EIO;
1da177e4
LT
7153}
7154
b423e9ae 7155static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
7156{
7157 return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
7158}
7159
e974604b 7160static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7161 struct net_device *dev);
7162/* r8169_csum_workaround()
7163 * The hw limites the value the transport offset. When the offset is out of the
7164 * range, calculate the checksum by sw.
7165 */
7166static void r8169_csum_workaround(struct rtl8169_private *tp,
7167 struct sk_buff *skb)
7168{
7169 if (skb_shinfo(skb)->gso_size) {
7170 netdev_features_t features = tp->dev->features;
7171 struct sk_buff *segs, *nskb;
7172
7173 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
7174 segs = skb_gso_segment(skb, features);
7175 if (IS_ERR(segs) || !segs)
7176 goto drop;
7177
7178 do {
7179 nskb = segs;
7180 segs = segs->next;
7181 nskb->next = NULL;
7182 rtl8169_start_xmit(nskb, tp->dev);
7183 } while (segs);
7184
eb781397 7185 dev_consume_skb_any(skb);
e974604b 7186 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7187 if (skb_checksum_help(skb) < 0)
7188 goto drop;
7189
7190 rtl8169_start_xmit(skb, tp->dev);
7191 } else {
7192 struct net_device_stats *stats;
7193
7194drop:
7195 stats = &tp->dev->stats;
7196 stats->tx_dropped++;
eb781397 7197 dev_kfree_skb_any(skb);
e974604b 7198 }
7199}
7200
7201/* msdn_giant_send_check()
7202 * According to the document of microsoft, the TCP Pseudo Header excludes the
7203 * packet length for IPv6 TCP large packets.
7204 */
7205static int msdn_giant_send_check(struct sk_buff *skb)
7206{
7207 const struct ipv6hdr *ipv6h;
7208 struct tcphdr *th;
7209 int ret;
7210
7211 ret = skb_cow_head(skb, 0);
7212 if (ret)
7213 return ret;
7214
7215 ipv6h = ipv6_hdr(skb);
7216 th = tcp_hdr(skb);
7217
7218 th->check = 0;
7219 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
7220
7221 return ret;
7222}
7223
7224static inline __be16 get_protocol(struct sk_buff *skb)
7225{
7226 __be16 protocol;
7227
7228 if (skb->protocol == htons(ETH_P_8021Q))
7229 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
7230 else
7231 protocol = skb->protocol;
7232
7233 return protocol;
7234}
7235
5888d3fc 7236static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
7237 struct sk_buff *skb, u32 *opts)
1da177e4 7238{
350fb32a
MM
7239 u32 mss = skb_shinfo(skb)->gso_size;
7240
2b7b4318
FR
7241 if (mss) {
7242 opts[0] |= TD_LSO;
5888d3fc 7243 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
7244 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
7245 const struct iphdr *ip = ip_hdr(skb);
7246
7247 if (ip->protocol == IPPROTO_TCP)
7248 opts[0] |= TD0_IP_CS | TD0_TCP_CS;
7249 else if (ip->protocol == IPPROTO_UDP)
7250 opts[0] |= TD0_IP_CS | TD0_UDP_CS;
7251 else
7252 WARN_ON_ONCE(1);
7253 }
7254
7255 return true;
7256}
7257
7258static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
7259 struct sk_buff *skb, u32 *opts)
7260{
bdfa4ed6 7261 u32 transport_offset = (u32)skb_transport_offset(skb);
5888d3fc 7262 u32 mss = skb_shinfo(skb)->gso_size;
7263
7264 if (mss) {
e974604b 7265 if (transport_offset > GTTCPHO_MAX) {
7266 netif_warn(tp, tx_err, tp->dev,
7267 "Invalid transport offset 0x%x for TSO\n",
7268 transport_offset);
7269 return false;
7270 }
7271
7272 switch (get_protocol(skb)) {
7273 case htons(ETH_P_IP):
7274 opts[0] |= TD1_GTSENV4;
7275 break;
7276
7277 case htons(ETH_P_IPV6):
7278 if (msdn_giant_send_check(skb))
7279 return false;
7280
7281 opts[0] |= TD1_GTSENV6;
7282 break;
7283
7284 default:
7285 WARN_ON_ONCE(1);
7286 break;
7287 }
7288
bdfa4ed6 7289 opts[0] |= transport_offset << GTTCPHO_SHIFT;
5888d3fc 7290 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
2b7b4318 7291 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
e974604b 7292 u8 ip_protocol;
1da177e4 7293
b423e9ae 7294 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
207c5f44 7295 return !(skb_checksum_help(skb) || eth_skb_pad(skb));
b423e9ae 7296
e974604b 7297 if (transport_offset > TCPHO_MAX) {
7298 netif_warn(tp, tx_err, tp->dev,
7299 "Invalid transport offset 0x%x\n",
7300 transport_offset);
7301 return false;
7302 }
7303
7304 switch (get_protocol(skb)) {
7305 case htons(ETH_P_IP):
7306 opts[1] |= TD1_IPv4_CS;
7307 ip_protocol = ip_hdr(skb)->protocol;
7308 break;
7309
7310 case htons(ETH_P_IPV6):
7311 opts[1] |= TD1_IPv6_CS;
7312 ip_protocol = ipv6_hdr(skb)->nexthdr;
7313 break;
7314
7315 default:
7316 ip_protocol = IPPROTO_RAW;
7317 break;
7318 }
7319
7320 if (ip_protocol == IPPROTO_TCP)
7321 opts[1] |= TD1_TCP_CS;
7322 else if (ip_protocol == IPPROTO_UDP)
7323 opts[1] |= TD1_UDP_CS;
2b7b4318
FR
7324 else
7325 WARN_ON_ONCE(1);
e974604b 7326
7327 opts[1] |= transport_offset << TCPHO_SHIFT;
b423e9ae 7328 } else {
7329 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
207c5f44 7330 return !eth_skb_pad(skb);
1da177e4 7331 }
5888d3fc 7332
b423e9ae 7333 return true;
1da177e4
LT
7334}
7335
61357325
SH
7336static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
7337 struct net_device *dev)
1da177e4
LT
7338{
7339 struct rtl8169_private *tp = netdev_priv(dev);
3eafe507 7340 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
1da177e4
LT
7341 struct TxDesc *txd = tp->TxDescArray + entry;
7342 void __iomem *ioaddr = tp->mmio_addr;
48addcc9 7343 struct device *d = &tp->pci_dev->dev;
1da177e4
LT
7344 dma_addr_t mapping;
7345 u32 status, len;
2b7b4318 7346 u32 opts[2];
3eafe507 7347 int frags;
5b0384f4 7348
477206a0 7349 if (unlikely(!TX_FRAGS_READY_FOR(tp, skb_shinfo(skb)->nr_frags))) {
bf82c189 7350 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
3eafe507 7351 goto err_stop_0;
1da177e4
LT
7352 }
7353
7354 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
3eafe507
SG
7355 goto err_stop_0;
7356
b423e9ae 7357 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
7358 opts[0] = DescOwn;
7359
e974604b 7360 if (!tp->tso_csum(tp, skb, opts)) {
7361 r8169_csum_workaround(tp, skb);
7362 return NETDEV_TX_OK;
7363 }
b423e9ae 7364
3eafe507 7365 len = skb_headlen(skb);
48addcc9 7366 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
d827d86b
SG
7367 if (unlikely(dma_mapping_error(d, mapping))) {
7368 if (net_ratelimit())
7369 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
3eafe507 7370 goto err_dma_0;
d827d86b 7371 }
3eafe507
SG
7372
7373 tp->tx_skb[entry].len = len;
7374 txd->addr = cpu_to_le64(mapping);
1da177e4 7375
2b7b4318 7376 frags = rtl8169_xmit_frags(tp, skb, opts);
3eafe507
SG
7377 if (frags < 0)
7378 goto err_dma_1;
7379 else if (frags)
2b7b4318 7380 opts[0] |= FirstFrag;
3eafe507 7381 else {
2b7b4318 7382 opts[0] |= FirstFrag | LastFrag;
1da177e4
LT
7383 tp->tx_skb[entry].skb = skb;
7384 }
7385
2b7b4318
FR
7386 txd->opts2 = cpu_to_le32(opts[1]);
7387
5047fb5d
RC
7388 skb_tx_timestamp(skb);
7389
a0750138
AD
7390 /* Force memory writes to complete before releasing descriptor */
7391 dma_wmb();
1da177e4 7392
cecb5fd7 7393 /* Anti gcc 2.95.3 bugware (sic) */
2b7b4318 7394 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
1da177e4
LT
7395 txd->opts1 = cpu_to_le32(status);
7396
a0750138 7397 /* Force all memory writes to complete before notifying device */
4c020a96 7398 wmb();
1da177e4 7399
a0750138
AD
7400 tp->cur_tx += frags + 1;
7401
87cda7cb 7402 RTL_W8(TxPoll, NPQ);
1da177e4 7403
87cda7cb 7404 mmiowb();
da78dbff 7405
87cda7cb 7406 if (!TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
ae1f23fb
FR
7407 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
7408 * not miss a ring update when it notices a stopped queue.
7409 */
7410 smp_wmb();
1da177e4 7411 netif_stop_queue(dev);
ae1f23fb
FR
7412 /* Sync with rtl_tx:
7413 * - publish queue status and cur_tx ring index (write barrier)
7414 * - refresh dirty_tx ring index (read barrier).
7415 * May the current thread have a pessimistic view of the ring
7416 * status and forget to wake up queue, a racing rtl_tx thread
7417 * can't.
7418 */
1e874e04 7419 smp_mb();
477206a0 7420 if (TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS))
1da177e4
LT
7421 netif_wake_queue(dev);
7422 }
7423
61357325 7424 return NETDEV_TX_OK;
1da177e4 7425
3eafe507 7426err_dma_1:
48addcc9 7427 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
3eafe507 7428err_dma_0:
989c9ba1 7429 dev_kfree_skb_any(skb);
3eafe507
SG
7430 dev->stats.tx_dropped++;
7431 return NETDEV_TX_OK;
7432
7433err_stop_0:
1da177e4 7434 netif_stop_queue(dev);
cebf8cc7 7435 dev->stats.tx_dropped++;
61357325 7436 return NETDEV_TX_BUSY;
1da177e4
LT
7437}
7438
7439static void rtl8169_pcierr_interrupt(struct net_device *dev)
7440{
7441 struct rtl8169_private *tp = netdev_priv(dev);
7442 struct pci_dev *pdev = tp->pci_dev;
1da177e4
LT
7443 u16 pci_status, pci_cmd;
7444
7445 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
7446 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
7447
bf82c189
JP
7448 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
7449 pci_cmd, pci_status);
1da177e4
LT
7450
7451 /*
7452 * The recovery sequence below admits a very elaborated explanation:
7453 * - it seems to work;
d03902b8
FR
7454 * - I did not see what else could be done;
7455 * - it makes iop3xx happy.
1da177e4
LT
7456 *
7457 * Feel free to adjust to your needs.
7458 */
a27993f3 7459 if (pdev->broken_parity_status)
d03902b8
FR
7460 pci_cmd &= ~PCI_COMMAND_PARITY;
7461 else
7462 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
7463
7464 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
1da177e4
LT
7465
7466 pci_write_config_word(pdev, PCI_STATUS,
7467 pci_status & (PCI_STATUS_DETECTED_PARITY |
7468 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
7469 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
7470
7471 /* The infamous DAC f*ckup only happens at boot time */
9fba0812 7472 if ((tp->cp_cmd & PCIDAC) && !tp->cur_rx) {
e6de30d6 7473 void __iomem *ioaddr = tp->mmio_addr;
7474
bf82c189 7475 netif_info(tp, intr, dev, "disabling PCI DAC\n");
1da177e4
LT
7476 tp->cp_cmd &= ~PCIDAC;
7477 RTL_W16(CPlusCmd, tp->cp_cmd);
7478 dev->features &= ~NETIF_F_HIGHDMA;
1da177e4
LT
7479 }
7480
e6de30d6 7481 rtl8169_hw_reset(tp);
d03902b8 7482
98ddf986 7483 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
1da177e4
LT
7484}
7485
da78dbff 7486static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp)
1da177e4
LT
7487{
7488 unsigned int dirty_tx, tx_left;
7489
1da177e4
LT
7490 dirty_tx = tp->dirty_tx;
7491 smp_rmb();
7492 tx_left = tp->cur_tx - dirty_tx;
7493
7494 while (tx_left > 0) {
7495 unsigned int entry = dirty_tx % NUM_TX_DESC;
7496 struct ring_info *tx_skb = tp->tx_skb + entry;
1da177e4
LT
7497 u32 status;
7498
1da177e4
LT
7499 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
7500 if (status & DescOwn)
7501 break;
7502
a0750138
AD
7503 /* This barrier is needed to keep us from reading
7504 * any other fields out of the Tx descriptor until
7505 * we know the status of DescOwn
7506 */
7507 dma_rmb();
7508
48addcc9
SG
7509 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
7510 tp->TxDescArray + entry);
1da177e4 7511 if (status & LastFrag) {
87cda7cb
DM
7512 u64_stats_update_begin(&tp->tx_stats.syncp);
7513 tp->tx_stats.packets++;
7514 tp->tx_stats.bytes += tx_skb->skb->len;
7515 u64_stats_update_end(&tp->tx_stats.syncp);
7a4b813c 7516 dev_consume_skb_any(tx_skb->skb);
1da177e4
LT
7517 tx_skb->skb = NULL;
7518 }
7519 dirty_tx++;
7520 tx_left--;
7521 }
7522
7523 if (tp->dirty_tx != dirty_tx) {
7524 tp->dirty_tx = dirty_tx;
ae1f23fb
FR
7525 /* Sync with rtl8169_start_xmit:
7526 * - publish dirty_tx ring index (write barrier)
7527 * - refresh cur_tx ring index and queue status (read barrier)
7528 * May the current thread miss the stopped queue condition,
7529 * a racing xmit thread can only have a right view of the
7530 * ring status.
7531 */
1e874e04 7532 smp_mb();
1da177e4 7533 if (netif_queue_stopped(dev) &&
477206a0 7534 TX_FRAGS_READY_FOR(tp, MAX_SKB_FRAGS)) {
1da177e4
LT
7535 netif_wake_queue(dev);
7536 }
d78ae2dc
FR
7537 /*
7538 * 8168 hack: TxPoll requests are lost when the Tx packets are
7539 * too close. Let's kick an extra TxPoll request when a burst
7540 * of start_xmit activity is detected (if it is not detected,
7541 * it is slow enough). -- FR
7542 */
da78dbff
FR
7543 if (tp->cur_tx != dirty_tx) {
7544 void __iomem *ioaddr = tp->mmio_addr;
7545
d78ae2dc 7546 RTL_W8(TxPoll, NPQ);
da78dbff 7547 }
1da177e4
LT
7548 }
7549}
7550
126fa4b9
FR
7551static inline int rtl8169_fragmented_frame(u32 status)
7552{
7553 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
7554}
7555
adea1ac7 7556static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
1da177e4 7557{
1da177e4
LT
7558 u32 status = opts1 & RxProtoMask;
7559
7560 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
d5d3ebe3 7561 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
1da177e4
LT
7562 skb->ip_summed = CHECKSUM_UNNECESSARY;
7563 else
bc8acf2c 7564 skb_checksum_none_assert(skb);
1da177e4
LT
7565}
7566
6f0333b8
ED
7567static struct sk_buff *rtl8169_try_rx_copy(void *data,
7568 struct rtl8169_private *tp,
7569 int pkt_size,
7570 dma_addr_t addr)
1da177e4 7571{
b449655f 7572 struct sk_buff *skb;
48addcc9 7573 struct device *d = &tp->pci_dev->dev;
b449655f 7574
6f0333b8 7575 data = rtl8169_align(data);
48addcc9 7576 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
6f0333b8 7577 prefetch(data);
e2338f86 7578 skb = napi_alloc_skb(&tp->napi, pkt_size);
6f0333b8
ED
7579 if (skb)
7580 memcpy(skb->data, data, pkt_size);
48addcc9
SG
7581 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
7582
6f0333b8 7583 return skb;
1da177e4
LT
7584}
7585
da78dbff 7586static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
1da177e4
LT
7587{
7588 unsigned int cur_rx, rx_left;
6f0333b8 7589 unsigned int count;
1da177e4 7590
1da177e4 7591 cur_rx = tp->cur_rx;
1da177e4 7592
9fba0812 7593 for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
1da177e4 7594 unsigned int entry = cur_rx % NUM_RX_DESC;
126fa4b9 7595 struct RxDesc *desc = tp->RxDescArray + entry;
1da177e4
LT
7596 u32 status;
7597
e03f33af 7598 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
1da177e4
LT
7599 if (status & DescOwn)
7600 break;
a0750138
AD
7601
7602 /* This barrier is needed to keep us from reading
7603 * any other fields out of the Rx descriptor until
7604 * we know the status of DescOwn
7605 */
7606 dma_rmb();
7607
4dcb7d33 7608 if (unlikely(status & RxRES)) {
bf82c189
JP
7609 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
7610 status);
cebf8cc7 7611 dev->stats.rx_errors++;
1da177e4 7612 if (status & (RxRWT | RxRUNT))
cebf8cc7 7613 dev->stats.rx_length_errors++;
1da177e4 7614 if (status & RxCRC)
cebf8cc7 7615 dev->stats.rx_crc_errors++;
9dccf611 7616 if (status & RxFOVF) {
da78dbff 7617 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
cebf8cc7 7618 dev->stats.rx_fifo_errors++;
9dccf611 7619 }
6bbe021d
BG
7620 if ((status & (RxRUNT | RxCRC)) &&
7621 !(status & (RxRWT | RxFOVF)) &&
7622 (dev->features & NETIF_F_RXALL))
7623 goto process_pkt;
1da177e4 7624 } else {
6f0333b8 7625 struct sk_buff *skb;
6bbe021d
BG
7626 dma_addr_t addr;
7627 int pkt_size;
7628
7629process_pkt:
7630 addr = le64_to_cpu(desc->addr);
79d0c1d2
BG
7631 if (likely(!(dev->features & NETIF_F_RXFCS)))
7632 pkt_size = (status & 0x00003fff) - 4;
7633 else
7634 pkt_size = status & 0x00003fff;
1da177e4 7635
126fa4b9
FR
7636 /*
7637 * The driver does not support incoming fragmented
7638 * frames. They are seen as a symptom of over-mtu
7639 * sized frames.
7640 */
7641 if (unlikely(rtl8169_fragmented_frame(status))) {
cebf8cc7
FR
7642 dev->stats.rx_dropped++;
7643 dev->stats.rx_length_errors++;
ce11ff5e 7644 goto release_descriptor;
126fa4b9
FR
7645 }
7646
6f0333b8
ED
7647 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
7648 tp, pkt_size, addr);
6f0333b8
ED
7649 if (!skb) {
7650 dev->stats.rx_dropped++;
ce11ff5e 7651 goto release_descriptor;
1da177e4
LT
7652 }
7653
adea1ac7 7654 rtl8169_rx_csum(skb, status);
1da177e4
LT
7655 skb_put(skb, pkt_size);
7656 skb->protocol = eth_type_trans(skb, dev);
7657
7a8fc77b
FR
7658 rtl8169_rx_vlan_tag(desc, skb);
7659
39174291 7660 if (skb->pkt_type == PACKET_MULTICAST)
7661 dev->stats.multicast++;
7662
56de414c 7663 napi_gro_receive(&tp->napi, skb);
1da177e4 7664
8027aa24
JW
7665 u64_stats_update_begin(&tp->rx_stats.syncp);
7666 tp->rx_stats.packets++;
7667 tp->rx_stats.bytes += pkt_size;
7668 u64_stats_update_end(&tp->rx_stats.syncp);
1da177e4 7669 }
ce11ff5e 7670release_descriptor:
7671 desc->opts2 = 0;
ce11ff5e 7672 rtl8169_mark_to_asic(desc, rx_buf_sz);
1da177e4
LT
7673 }
7674
7675 count = cur_rx - tp->cur_rx;
7676 tp->cur_rx = cur_rx;
7677
1da177e4
LT
7678 return count;
7679}
7680
07d3f51f 7681static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
1da177e4 7682{
07d3f51f 7683 struct net_device *dev = dev_instance;
1da177e4 7684 struct rtl8169_private *tp = netdev_priv(dev);
1da177e4 7685 int handled = 0;
9085cdfa 7686 u16 status;
1da177e4 7687
9085cdfa 7688 status = rtl_get_events(tp);
da78dbff
FR
7689 if (status && status != 0xffff) {
7690 status &= RTL_EVENT_NAPI | tp->event_slow;
7691 if (status) {
7692 handled = 1;
1da177e4 7693
da78dbff
FR
7694 rtl_irq_disable(tp);
7695 napi_schedule(&tp->napi);
f11a377b 7696 }
da78dbff
FR
7697 }
7698 return IRQ_RETVAL(handled);
7699}
1da177e4 7700
da78dbff
FR
7701/*
7702 * Workqueue context.
7703 */
7704static void rtl_slow_event_work(struct rtl8169_private *tp)
7705{
7706 struct net_device *dev = tp->dev;
7707 u16 status;
7708
7709 status = rtl_get_events(tp) & tp->event_slow;
7710 rtl_ack_events(tp, status);
1da177e4 7711
da78dbff
FR
7712 if (unlikely(status & RxFIFOOver)) {
7713 switch (tp->mac_version) {
7714 /* Work around for rx fifo overflow */
7715 case RTL_GIGA_MAC_VER_11:
7716 netif_stop_queue(dev);
934714d0
FR
7717 /* XXX - Hack alert. See rtl_task(). */
7718 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
da78dbff 7719 default:
f11a377b
DD
7720 break;
7721 }
da78dbff 7722 }
1da177e4 7723
da78dbff
FR
7724 if (unlikely(status & SYSErr))
7725 rtl8169_pcierr_interrupt(dev);
0e485150 7726
da78dbff 7727 if (status & LinkChg)
ef4d5fcc 7728 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
1da177e4 7729
7dbb4918 7730 rtl_irq_enable_all(tp);
1da177e4
LT
7731}
7732
4422bcd4
FR
7733static void rtl_task(struct work_struct *work)
7734{
da78dbff
FR
7735 static const struct {
7736 int bitnr;
7737 void (*action)(struct rtl8169_private *);
7738 } rtl_work[] = {
934714d0 7739 /* XXX - keep rtl_slow_event_work() as first element. */
da78dbff
FR
7740 { RTL_FLAG_TASK_SLOW_PENDING, rtl_slow_event_work },
7741 { RTL_FLAG_TASK_RESET_PENDING, rtl_reset_work },
7742 { RTL_FLAG_TASK_PHY_PENDING, rtl_phy_work }
7743 };
4422bcd4
FR
7744 struct rtl8169_private *tp =
7745 container_of(work, struct rtl8169_private, wk.work);
da78dbff
FR
7746 struct net_device *dev = tp->dev;
7747 int i;
7748
7749 rtl_lock_work(tp);
7750
6c4a70c5
FR
7751 if (!netif_running(dev) ||
7752 !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
da78dbff
FR
7753 goto out_unlock;
7754
7755 for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
7756 bool pending;
7757
da78dbff 7758 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
da78dbff
FR
7759 if (pending)
7760 rtl_work[i].action(tp);
7761 }
4422bcd4 7762
da78dbff
FR
7763out_unlock:
7764 rtl_unlock_work(tp);
4422bcd4
FR
7765}
7766
bea3348e 7767static int rtl8169_poll(struct napi_struct *napi, int budget)
1da177e4 7768{
bea3348e
SH
7769 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
7770 struct net_device *dev = tp->dev;
da78dbff
FR
7771 u16 enable_mask = RTL_EVENT_NAPI | tp->event_slow;
7772 int work_done= 0;
7773 u16 status;
7774
7775 status = rtl_get_events(tp);
7776 rtl_ack_events(tp, status & ~tp->event_slow);
7777
7778 if (status & RTL_EVENT_NAPI_RX)
7779 work_done = rtl_rx(dev, tp, (u32) budget);
7780
7781 if (status & RTL_EVENT_NAPI_TX)
7782 rtl_tx(dev, tp);
1da177e4 7783
da78dbff
FR
7784 if (status & tp->event_slow) {
7785 enable_mask &= ~tp->event_slow;
7786
7787 rtl_schedule_task(tp, RTL_FLAG_TASK_SLOW_PENDING);
7788 }
1da177e4 7789
bea3348e 7790 if (work_done < budget) {
6ad20165 7791 napi_complete_done(napi, work_done);
f11a377b 7792
da78dbff
FR
7793 rtl_irq_enable(tp, enable_mask);
7794 mmiowb();
1da177e4
LT
7795 }
7796
bea3348e 7797 return work_done;
1da177e4 7798}
1da177e4 7799
523a6094
FR
7800static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
7801{
7802 struct rtl8169_private *tp = netdev_priv(dev);
7803
7804 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
7805 return;
7806
7807 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
7808 RTL_W32(RxMissed, 0);
7809}
7810
1da177e4
LT
7811static void rtl8169_down(struct net_device *dev)
7812{
7813 struct rtl8169_private *tp = netdev_priv(dev);
7814 void __iomem *ioaddr = tp->mmio_addr;
1da177e4 7815
4876cc1e 7816 del_timer_sync(&tp->timer);
1da177e4 7817
93dd79e8 7818 napi_disable(&tp->napi);
da78dbff 7819 netif_stop_queue(dev);
1da177e4 7820
92fc43b4 7821 rtl8169_hw_reset(tp);
323bb685
SG
7822 /*
7823 * At this point device interrupts can not be enabled in any function,
209e5ac8
FR
7824 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
7825 * and napi is disabled (rtl8169_poll).
323bb685 7826 */
523a6094 7827 rtl8169_rx_missed(dev, ioaddr);
1da177e4 7828
1da177e4 7829 /* Give a racing hard_start_xmit a few cycles to complete. */
da78dbff 7830 synchronize_sched();
1da177e4 7831
1da177e4
LT
7832 rtl8169_tx_clear(tp);
7833
7834 rtl8169_rx_clear(tp);
065c27c1 7835
7836 rtl_pll_power_down(tp);
1da177e4
LT
7837}
7838
7839static int rtl8169_close(struct net_device *dev)
7840{
7841 struct rtl8169_private *tp = netdev_priv(dev);
7842 struct pci_dev *pdev = tp->pci_dev;
7843
e1759441
RW
7844 pm_runtime_get_sync(&pdev->dev);
7845
cecb5fd7 7846 /* Update counters before going down */
355423d0
IV
7847 rtl8169_update_counters(dev);
7848
da78dbff 7849 rtl_lock_work(tp);
6c4a70c5 7850 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
da78dbff 7851
1da177e4 7852 rtl8169_down(dev);
da78dbff 7853 rtl_unlock_work(tp);
1da177e4 7854
4ea72445
L
7855 cancel_work_sync(&tp->wk.work);
7856
92a7c4e7 7857 free_irq(pdev->irq, dev);
1da177e4 7858
82553bb6
SG
7859 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7860 tp->RxPhyAddr);
7861 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7862 tp->TxPhyAddr);
1da177e4
LT
7863 tp->TxDescArray = NULL;
7864 tp->RxDescArray = NULL;
7865
e1759441
RW
7866 pm_runtime_put_sync(&pdev->dev);
7867
1da177e4
LT
7868 return 0;
7869}
7870
dc1c00ce
FR
7871#ifdef CONFIG_NET_POLL_CONTROLLER
7872static void rtl8169_netpoll(struct net_device *dev)
7873{
7874 struct rtl8169_private *tp = netdev_priv(dev);
7875
7876 rtl8169_interrupt(tp->pci_dev->irq, dev);
7877}
7878#endif
7879
df43ac78
FR
7880static int rtl_open(struct net_device *dev)
7881{
7882 struct rtl8169_private *tp = netdev_priv(dev);
7883 void __iomem *ioaddr = tp->mmio_addr;
7884 struct pci_dev *pdev = tp->pci_dev;
7885 int retval = -ENOMEM;
7886
7887 pm_runtime_get_sync(&pdev->dev);
7888
7889 /*
e75d6606 7890 * Rx and Tx descriptors needs 256 bytes alignment.
df43ac78
FR
7891 * dma_alloc_coherent provides more.
7892 */
7893 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
7894 &tp->TxPhyAddr, GFP_KERNEL);
7895 if (!tp->TxDescArray)
7896 goto err_pm_runtime_put;
7897
7898 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
7899 &tp->RxPhyAddr, GFP_KERNEL);
7900 if (!tp->RxDescArray)
7901 goto err_free_tx_0;
7902
7903 retval = rtl8169_init_ring(dev);
7904 if (retval < 0)
7905 goto err_free_rx_1;
7906
7907 INIT_WORK(&tp->wk.work, rtl_task);
7908
7909 smp_mb();
7910
7911 rtl_request_firmware(tp);
7912
92a7c4e7 7913 retval = request_irq(pdev->irq, rtl8169_interrupt,
df43ac78
FR
7914 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
7915 dev->name, dev);
7916 if (retval < 0)
7917 goto err_release_fw_2;
7918
7919 rtl_lock_work(tp);
7920
7921 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
7922
7923 napi_enable(&tp->napi);
7924
7925 rtl8169_init_phy(dev, tp);
7926
7927 __rtl8169_set_features(dev, dev->features);
7928
7929 rtl_pll_power_up(tp);
7930
7931 rtl_hw_start(dev);
7932
6e85d5ad
CV
7933 if (!rtl8169_init_counter_offsets(dev))
7934 netif_warn(tp, hw, dev, "counter reset/update failed\n");
7935
df43ac78
FR
7936 netif_start_queue(dev);
7937
7938 rtl_unlock_work(tp);
7939
7940 tp->saved_wolopts = 0;
a92a0849 7941 pm_runtime_put_sync(&pdev->dev);
df43ac78
FR
7942
7943 rtl8169_check_link_status(dev, tp, ioaddr);
7944out:
7945 return retval;
7946
7947err_release_fw_2:
7948 rtl_release_firmware(tp);
7949 rtl8169_rx_clear(tp);
7950err_free_rx_1:
7951 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
7952 tp->RxPhyAddr);
7953 tp->RxDescArray = NULL;
7954err_free_tx_0:
7955 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
7956 tp->TxPhyAddr);
7957 tp->TxDescArray = NULL;
7958err_pm_runtime_put:
7959 pm_runtime_put_noidle(&pdev->dev);
7960 goto out;
7961}
7962
bc1f4470 7963static void
8027aa24 7964rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
1da177e4
LT
7965{
7966 struct rtl8169_private *tp = netdev_priv(dev);
7967 void __iomem *ioaddr = tp->mmio_addr;
f09cf4b7 7968 struct pci_dev *pdev = tp->pci_dev;
42020320 7969 struct rtl8169_counters *counters = tp->counters;
8027aa24 7970 unsigned int start;
1da177e4 7971
f09cf4b7
CHL
7972 pm_runtime_get_noresume(&pdev->dev);
7973
7974 if (netif_running(dev) && pm_runtime_active(&pdev->dev))
523a6094 7975 rtl8169_rx_missed(dev, ioaddr);
5b0384f4 7976
8027aa24 7977 do {
57a7744e 7978 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
8027aa24
JW
7979 stats->rx_packets = tp->rx_stats.packets;
7980 stats->rx_bytes = tp->rx_stats.bytes;
57a7744e 7981 } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
8027aa24 7982
8027aa24 7983 do {
57a7744e 7984 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
8027aa24
JW
7985 stats->tx_packets = tp->tx_stats.packets;
7986 stats->tx_bytes = tp->tx_stats.bytes;
57a7744e 7987 } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
8027aa24
JW
7988
7989 stats->rx_dropped = dev->stats.rx_dropped;
7990 stats->tx_dropped = dev->stats.tx_dropped;
7991 stats->rx_length_errors = dev->stats.rx_length_errors;
7992 stats->rx_errors = dev->stats.rx_errors;
7993 stats->rx_crc_errors = dev->stats.rx_crc_errors;
7994 stats->rx_fifo_errors = dev->stats.rx_fifo_errors;
7995 stats->rx_missed_errors = dev->stats.rx_missed_errors;
d7d2d89d 7996 stats->multicast = dev->stats.multicast;
8027aa24 7997
6e85d5ad
CV
7998 /*
7999 * Fetch additonal counter values missing in stats collected by driver
8000 * from tally counters.
8001 */
f09cf4b7
CHL
8002 if (pm_runtime_active(&pdev->dev))
8003 rtl8169_update_counters(dev);
6e85d5ad
CV
8004
8005 /*
8006 * Subtract values fetched during initalization.
8007 * See rtl8169_init_counter_offsets for a description why we do that.
8008 */
42020320 8009 stats->tx_errors = le64_to_cpu(counters->tx_errors) -
6e85d5ad 8010 le64_to_cpu(tp->tc_offset.tx_errors);
42020320 8011 stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
6e85d5ad 8012 le32_to_cpu(tp->tc_offset.tx_multi_collision);
42020320 8013 stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
6e85d5ad
CV
8014 le16_to_cpu(tp->tc_offset.tx_aborted);
8015
f09cf4b7 8016 pm_runtime_put_noidle(&pdev->dev);
1da177e4
LT
8017}
8018
861ab440 8019static void rtl8169_net_suspend(struct net_device *dev)
5d06a99f 8020{
065c27c1 8021 struct rtl8169_private *tp = netdev_priv(dev);
8022
5d06a99f 8023 if (!netif_running(dev))
861ab440 8024 return;
5d06a99f
FR
8025
8026 netif_device_detach(dev);
8027 netif_stop_queue(dev);
da78dbff
FR
8028
8029 rtl_lock_work(tp);
8030 napi_disable(&tp->napi);
6c4a70c5 8031 clear_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
da78dbff
FR
8032 rtl_unlock_work(tp);
8033
8034 rtl_pll_power_down(tp);
861ab440
RW
8035}
8036
8037#ifdef CONFIG_PM
8038
8039static int rtl8169_suspend(struct device *device)
8040{
8041 struct pci_dev *pdev = to_pci_dev(device);
8042 struct net_device *dev = pci_get_drvdata(pdev);
5d06a99f 8043
861ab440 8044 rtl8169_net_suspend(dev);
1371fa6d 8045
5d06a99f
FR
8046 return 0;
8047}
8048
e1759441
RW
8049static void __rtl8169_resume(struct net_device *dev)
8050{
065c27c1 8051 struct rtl8169_private *tp = netdev_priv(dev);
8052
e1759441 8053 netif_device_attach(dev);
065c27c1 8054
8055 rtl_pll_power_up(tp);
8056
cff4c162
AS
8057 rtl_lock_work(tp);
8058 napi_enable(&tp->napi);
6c4a70c5 8059 set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
cff4c162 8060 rtl_unlock_work(tp);
da78dbff 8061
98ddf986 8062 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
e1759441
RW
8063}
8064
861ab440 8065static int rtl8169_resume(struct device *device)
5d06a99f 8066{
861ab440 8067 struct pci_dev *pdev = to_pci_dev(device);
5d06a99f 8068 struct net_device *dev = pci_get_drvdata(pdev);
fccec10b
SG
8069 struct rtl8169_private *tp = netdev_priv(dev);
8070
8071 rtl8169_init_phy(dev, tp);
5d06a99f 8072
e1759441
RW
8073 if (netif_running(dev))
8074 __rtl8169_resume(dev);
5d06a99f 8075
e1759441
RW
8076 return 0;
8077}
8078
8079static int rtl8169_runtime_suspend(struct device *device)
8080{
8081 struct pci_dev *pdev = to_pci_dev(device);
8082 struct net_device *dev = pci_get_drvdata(pdev);
8083 struct rtl8169_private *tp = netdev_priv(dev);
8084
a92a0849
HK
8085 if (!tp->TxDescArray) {
8086 rtl_pll_power_down(tp);
e1759441 8087 return 0;
a92a0849 8088 }
e1759441 8089
da78dbff 8090 rtl_lock_work(tp);
e1759441
RW
8091 tp->saved_wolopts = __rtl8169_get_wol(tp);
8092 __rtl8169_set_wol(tp, WAKE_ANY);
da78dbff 8093 rtl_unlock_work(tp);
e1759441
RW
8094
8095 rtl8169_net_suspend(dev);
8096
f09cf4b7
CHL
8097 /* Update counters before going runtime suspend */
8098 rtl8169_rx_missed(dev, tp->mmio_addr);
8099 rtl8169_update_counters(dev);
8100
e1759441
RW
8101 return 0;
8102}
8103
8104static int rtl8169_runtime_resume(struct device *device)
8105{
8106 struct pci_dev *pdev = to_pci_dev(device);
8107 struct net_device *dev = pci_get_drvdata(pdev);
8108 struct rtl8169_private *tp = netdev_priv(dev);
f51d4a10 8109 rtl_rar_set(tp, dev->dev_addr);
e1759441
RW
8110
8111 if (!tp->TxDescArray)
8112 return 0;
8113
da78dbff 8114 rtl_lock_work(tp);
e1759441
RW
8115 __rtl8169_set_wol(tp, tp->saved_wolopts);
8116 tp->saved_wolopts = 0;
da78dbff 8117 rtl_unlock_work(tp);
e1759441 8118
fccec10b
SG
8119 rtl8169_init_phy(dev, tp);
8120
e1759441 8121 __rtl8169_resume(dev);
5d06a99f 8122
5d06a99f
FR
8123 return 0;
8124}
8125
e1759441
RW
8126static int rtl8169_runtime_idle(struct device *device)
8127{
8128 struct pci_dev *pdev = to_pci_dev(device);
8129 struct net_device *dev = pci_get_drvdata(pdev);
e1759441 8130
a92a0849
HK
8131 if (!netif_running(dev) || !netif_carrier_ok(dev))
8132 pm_schedule_suspend(device, 10000);
8133
8134 return -EBUSY;
e1759441
RW
8135}
8136
47145210 8137static const struct dev_pm_ops rtl8169_pm_ops = {
cecb5fd7
FR
8138 .suspend = rtl8169_suspend,
8139 .resume = rtl8169_resume,
8140 .freeze = rtl8169_suspend,
8141 .thaw = rtl8169_resume,
8142 .poweroff = rtl8169_suspend,
8143 .restore = rtl8169_resume,
8144 .runtime_suspend = rtl8169_runtime_suspend,
8145 .runtime_resume = rtl8169_runtime_resume,
8146 .runtime_idle = rtl8169_runtime_idle,
861ab440
RW
8147};
8148
8149#define RTL8169_PM_OPS (&rtl8169_pm_ops)
8150
8151#else /* !CONFIG_PM */
8152
8153#define RTL8169_PM_OPS NULL
8154
8155#endif /* !CONFIG_PM */
8156
649b3b8c 8157static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
8158{
8159 void __iomem *ioaddr = tp->mmio_addr;
8160
8161 /* WoL fails with 8168b when the receiver is disabled. */
8162 switch (tp->mac_version) {
8163 case RTL_GIGA_MAC_VER_11:
8164 case RTL_GIGA_MAC_VER_12:
8165 case RTL_GIGA_MAC_VER_17:
8166 pci_clear_master(tp->pci_dev);
8167
8168 RTL_W8(ChipCmd, CmdRxEnb);
8169 /* PCI commit */
8170 RTL_R8(ChipCmd);
8171 break;
8172 default:
8173 break;
8174 }
8175}
8176
1765f95d
FR
8177static void rtl_shutdown(struct pci_dev *pdev)
8178{
861ab440 8179 struct net_device *dev = pci_get_drvdata(pdev);
4bb3f522 8180 struct rtl8169_private *tp = netdev_priv(dev);
861ab440
RW
8181
8182 rtl8169_net_suspend(dev);
1765f95d 8183
cecb5fd7 8184 /* Restore original MAC address */
cc098dc7
IV
8185 rtl_rar_set(tp, dev->perm_addr);
8186
92fc43b4 8187 rtl8169_hw_reset(tp);
4bb3f522 8188
861ab440 8189 if (system_state == SYSTEM_POWER_OFF) {
649b3b8c 8190 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
8191 rtl_wol_suspend_quirk(tp);
8192 rtl_wol_shutdown_quirk(tp);
ca52efd5 8193 }
8194
861ab440
RW
8195 pci_wake_from_d3(pdev, true);
8196 pci_set_power_state(pdev, PCI_D3hot);
8197 }
8198}
5d06a99f 8199
baf63293 8200static void rtl_remove_one(struct pci_dev *pdev)
e27566ed
FR
8201{
8202 struct net_device *dev = pci_get_drvdata(pdev);
8203 struct rtl8169_private *tp = netdev_priv(dev);
8204
ee7a1beb
CHL
8205 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8206 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
935e2218
CHL
8207 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8208 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8209 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8210 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
ee7a1beb 8211 r8168_check_dash(tp)) {
e27566ed
FR
8212 rtl8168_driver_stop(tp);
8213 }
8214
ad1be8d3
DN
8215 netif_napi_del(&tp->napi);
8216
e27566ed
FR
8217 unregister_netdev(dev);
8218
8219 rtl_release_firmware(tp);
8220
8221 if (pci_dev_run_wake(pdev))
8222 pm_runtime_get_noresume(&pdev->dev);
8223
8224 /* restore original MAC address */
8225 rtl_rar_set(tp, dev->perm_addr);
e27566ed
FR
8226}
8227
fa9c385e 8228static const struct net_device_ops rtl_netdev_ops = {
df43ac78 8229 .ndo_open = rtl_open,
fa9c385e
FR
8230 .ndo_stop = rtl8169_close,
8231 .ndo_get_stats64 = rtl8169_get_stats64,
8232 .ndo_start_xmit = rtl8169_start_xmit,
8233 .ndo_tx_timeout = rtl8169_tx_timeout,
8234 .ndo_validate_addr = eth_validate_addr,
8235 .ndo_change_mtu = rtl8169_change_mtu,
8236 .ndo_fix_features = rtl8169_fix_features,
8237 .ndo_set_features = rtl8169_set_features,
8238 .ndo_set_mac_address = rtl_set_mac_address,
8239 .ndo_do_ioctl = rtl8169_ioctl,
8240 .ndo_set_rx_mode = rtl_set_rx_mode,
8241#ifdef CONFIG_NET_POLL_CONTROLLER
8242 .ndo_poll_controller = rtl8169_netpoll,
8243#endif
8244
8245};
8246
31fa8b18
FR
8247static const struct rtl_cfg_info {
8248 void (*hw_start)(struct net_device *);
8249 unsigned int region;
8250 unsigned int align;
8251 u16 event_slow;
8252 unsigned features;
50970831 8253 const struct rtl_coalesce_info *coalesce_info;
31fa8b18
FR
8254 u8 default_ver;
8255} rtl_cfg_infos [] = {
8256 [RTL_CFG_0] = {
8257 .hw_start = rtl_hw_start_8169,
8258 .region = 1,
8259 .align = 0,
8260 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
8261 .features = RTL_FEATURE_GMII,
50970831 8262 .coalesce_info = rtl_coalesce_info_8169,
31fa8b18
FR
8263 .default_ver = RTL_GIGA_MAC_VER_01,
8264 },
8265 [RTL_CFG_1] = {
8266 .hw_start = rtl_hw_start_8168,
8267 .region = 2,
8268 .align = 8,
8269 .event_slow = SYSErr | LinkChg | RxOverflow,
8270 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
50970831 8271 .coalesce_info = rtl_coalesce_info_8168_8136,
31fa8b18
FR
8272 .default_ver = RTL_GIGA_MAC_VER_11,
8273 },
8274 [RTL_CFG_2] = {
8275 .hw_start = rtl_hw_start_8101,
8276 .region = 2,
8277 .align = 8,
8278 .event_slow = SYSErr | LinkChg | RxOverflow | RxFIFOOver |
8279 PCSTimeout,
8280 .features = RTL_FEATURE_MSI,
50970831 8281 .coalesce_info = rtl_coalesce_info_8168_8136,
31fa8b18
FR
8282 .default_ver = RTL_GIGA_MAC_VER_13,
8283 }
8284};
8285
8286/* Cfg9346_Unlock assumed. */
8287static unsigned rtl_try_msi(struct rtl8169_private *tp,
8288 const struct rtl_cfg_info *cfg)
8289{
8290 void __iomem *ioaddr = tp->mmio_addr;
8291 unsigned msi = 0;
8292 u8 cfg2;
8293
8294 cfg2 = RTL_R8(Config2) & ~MSIEnable;
8295 if (cfg->features & RTL_FEATURE_MSI) {
8296 if (pci_enable_msi(tp->pci_dev)) {
8297 netif_info(tp, hw, tp->dev, "no MSI. Back to INTx.\n");
8298 } else {
8299 cfg2 |= MSIEnable;
8300 msi = RTL_FEATURE_MSI;
8301 }
8302 }
8303 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
8304 RTL_W8(Config2, cfg2);
8305 return msi;
8306}
8307
c558386b
HW
8308DECLARE_RTL_COND(rtl_link_list_ready_cond)
8309{
8310 void __iomem *ioaddr = tp->mmio_addr;
8311
8312 return RTL_R8(MCU) & LINK_LIST_RDY;
8313}
8314
8315DECLARE_RTL_COND(rtl_rxtx_empty_cond)
8316{
8317 void __iomem *ioaddr = tp->mmio_addr;
8318
8319 return (RTL_R8(MCU) & RXTX_EMPTY) == RXTX_EMPTY;
8320}
8321
baf63293 8322static void rtl_hw_init_8168g(struct rtl8169_private *tp)
c558386b
HW
8323{
8324 void __iomem *ioaddr = tp->mmio_addr;
8325 u32 data;
8326
8327 tp->ocp_base = OCP_STD_PHY_BASE;
8328
8329 RTL_W32(MISC, RTL_R32(MISC) | RXDV_GATED_EN);
8330
8331 if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
8332 return;
8333
8334 if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
8335 return;
8336
8337 RTL_W8(ChipCmd, RTL_R8(ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
8338 msleep(1);
8339 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
8340
5f8bcce9 8341 data = r8168_mac_ocp_read(tp, 0xe8de);
c558386b
HW
8342 data &= ~(1 << 14);
8343 r8168_mac_ocp_write(tp, 0xe8de, data);
8344
8345 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8346 return;
8347
5f8bcce9 8348 data = r8168_mac_ocp_read(tp, 0xe8de);
c558386b
HW
8349 data |= (1 << 15);
8350 r8168_mac_ocp_write(tp, 0xe8de, data);
8351
8352 if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
8353 return;
8354}
8355
003609da
CHL
8356static void rtl_hw_init_8168ep(struct rtl8169_private *tp)
8357{
8358 rtl8168ep_stop_cmac(tp);
8359 rtl_hw_init_8168g(tp);
8360}
8361
baf63293 8362static void rtl_hw_initialize(struct rtl8169_private *tp)
c558386b
HW
8363{
8364 switch (tp->mac_version) {
8365 case RTL_GIGA_MAC_VER_40:
8366 case RTL_GIGA_MAC_VER_41:
57538c4a 8367 case RTL_GIGA_MAC_VER_42:
58152cd4 8368 case RTL_GIGA_MAC_VER_43:
45dd95c4 8369 case RTL_GIGA_MAC_VER_44:
6e1d0b89
CHL
8370 case RTL_GIGA_MAC_VER_45:
8371 case RTL_GIGA_MAC_VER_46:
8372 case RTL_GIGA_MAC_VER_47:
8373 case RTL_GIGA_MAC_VER_48:
003609da
CHL
8374 rtl_hw_init_8168g(tp);
8375 break;
935e2218
CHL
8376 case RTL_GIGA_MAC_VER_49:
8377 case RTL_GIGA_MAC_VER_50:
8378 case RTL_GIGA_MAC_VER_51:
003609da 8379 rtl_hw_init_8168ep(tp);
c558386b 8380 break;
c558386b
HW
8381 default:
8382 break;
8383 }
8384}
8385
929a031d 8386static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3b6cf25d
FR
8387{
8388 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
8389 const unsigned int region = cfg->region;
8390 struct rtl8169_private *tp;
8391 struct mii_if_info *mii;
8392 struct net_device *dev;
8393 void __iomem *ioaddr;
8394 int chipset, i;
8395 int rc;
8396
8397 if (netif_msg_drv(&debug)) {
8398 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
8399 MODULENAME, RTL8169_VERSION);
8400 }
8401
4c45d24a
HK
8402 dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
8403 if (!dev)
8404 return -ENOMEM;
3b6cf25d
FR
8405
8406 SET_NETDEV_DEV(dev, &pdev->dev);
fa9c385e 8407 dev->netdev_ops = &rtl_netdev_ops;
3b6cf25d
FR
8408 tp = netdev_priv(dev);
8409 tp->dev = dev;
8410 tp->pci_dev = pdev;
8411 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
8412
8413 mii = &tp->mii;
8414 mii->dev = dev;
8415 mii->mdio_read = rtl_mdio_read;
8416 mii->mdio_write = rtl_mdio_write;
8417 mii->phy_id_mask = 0x1f;
8418 mii->reg_num_mask = 0x1f;
8419 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
8420
8421 /* disable ASPM completely as that cause random device stop working
8422 * problems as well as full system hangs for some PCIe devices users */
8423 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
8424 PCIE_LINK_STATE_CLKPM);
8425
8426 /* enable device (incl. PCI PM wakeup and hotplug setup) */
4c45d24a 8427 rc = pcim_enable_device(pdev);
3b6cf25d
FR
8428 if (rc < 0) {
8429 netif_err(tp, probe, dev, "enable failure\n");
4c45d24a 8430 return rc;
3b6cf25d
FR
8431 }
8432
4c45d24a 8433 if (pcim_set_mwi(pdev) < 0)
3b6cf25d
FR
8434 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
8435
8436 /* make sure PCI base addr 1 is MMIO */
8437 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
8438 netif_err(tp, probe, dev,
8439 "region #%d not an MMIO resource, aborting\n",
8440 region);
4c45d24a 8441 return -ENODEV;
3b6cf25d
FR
8442 }
8443
8444 /* check for weird/broken PCI region reporting */
8445 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
8446 netif_err(tp, probe, dev,
8447 "Invalid PCI region size(s), aborting\n");
4c45d24a 8448 return -ENODEV;
3b6cf25d
FR
8449 }
8450
8451 rc = pci_request_regions(pdev, MODULENAME);
8452 if (rc < 0) {
8453 netif_err(tp, probe, dev, "could not request regions\n");
4c45d24a 8454 return rc;
3b6cf25d
FR
8455 }
8456
3b6cf25d 8457 /* ioremap MMIO region */
4c45d24a
HK
8458 ioaddr = devm_ioremap(&pdev->dev, pci_resource_start(pdev, region),
8459 R8169_REGS_SIZE);
3b6cf25d
FR
8460 if (!ioaddr) {
8461 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
4c45d24a 8462 return -EIO;
3b6cf25d
FR
8463 }
8464 tp->mmio_addr = ioaddr;
8465
8466 if (!pci_is_pcie(pdev))
8467 netif_info(tp, probe, dev, "not PCI Express\n");
8468
8469 /* Identify chip attached to board */
8470 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
8471
27896c83
AB
8472 tp->cp_cmd = 0;
8473
8474 if ((sizeof(dma_addr_t) > 4) &&
8475 (use_dac == 1 || (use_dac == -1 && pci_is_pcie(pdev) &&
8476 tp->mac_version >= RTL_GIGA_MAC_VER_18)) &&
f0076436
AB
8477 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
8478 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
27896c83
AB
8479
8480 /* CPlusCmd Dual Access Cycle is only needed for non-PCIe */
8481 if (!pci_is_pcie(pdev))
8482 tp->cp_cmd |= PCIDAC;
8483 dev->features |= NETIF_F_HIGHDMA;
8484 } else {
8485 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
8486 if (rc < 0) {
8487 netif_err(tp, probe, dev, "DMA configuration failed\n");
4c45d24a 8488 return rc;
27896c83
AB
8489 }
8490 }
8491
3b6cf25d
FR
8492 rtl_init_rxcfg(tp);
8493
8494 rtl_irq_disable(tp);
8495
c558386b
HW
8496 rtl_hw_initialize(tp);
8497
3b6cf25d
FR
8498 rtl_hw_reset(tp);
8499
8500 rtl_ack_events(tp, 0xffff);
8501
8502 pci_set_master(pdev);
8503
3b6cf25d
FR
8504 rtl_init_mdio_ops(tp);
8505 rtl_init_pll_power_ops(tp);
8506 rtl_init_jumbo_ops(tp);
beb1fe18 8507 rtl_init_csi_ops(tp);
3b6cf25d
FR
8508
8509 rtl8169_print_mac_version(tp);
8510
8511 chipset = tp->mac_version;
8512 tp->txd_version = rtl_chip_infos[chipset].txd_version;
8513
8514 RTL_W8(Cfg9346, Cfg9346_Unlock);
3b6cf25d
FR
8515 tp->features |= rtl_try_msi(tp, cfg);
8516 RTL_W8(Cfg9346, Cfg9346_Lock);
8517
7edf6d31
HK
8518 /* override BIOS settings, use userspace tools to enable WOL */
8519 __rtl8169_set_wol(tp, 0);
8520
3b6cf25d
FR
8521 if (rtl_tbi_enabled(tp)) {
8522 tp->set_speed = rtl8169_set_speed_tbi;
6fa1ba61 8523 tp->get_link_ksettings = rtl8169_get_link_ksettings_tbi;
3b6cf25d
FR
8524 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
8525 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
8526 tp->link_ok = rtl8169_tbi_link_ok;
8527 tp->do_ioctl = rtl_tbi_ioctl;
8528 } else {
8529 tp->set_speed = rtl8169_set_speed_xmii;
6fa1ba61 8530 tp->get_link_ksettings = rtl8169_get_link_ksettings_xmii;
3b6cf25d
FR
8531 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
8532 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
8533 tp->link_ok = rtl8169_xmii_link_ok;
8534 tp->do_ioctl = rtl_xmii_ioctl;
8535 }
8536
8537 mutex_init(&tp->wk.mutex);
340fea3d
KM
8538 u64_stats_init(&tp->rx_stats.syncp);
8539 u64_stats_init(&tp->tx_stats.syncp);
3b6cf25d
FR
8540
8541 /* Get MAC address */
89cceb27
CHL
8542 if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
8543 tp->mac_version == RTL_GIGA_MAC_VER_36 ||
8544 tp->mac_version == RTL_GIGA_MAC_VER_37 ||
8545 tp->mac_version == RTL_GIGA_MAC_VER_38 ||
8546 tp->mac_version == RTL_GIGA_MAC_VER_40 ||
8547 tp->mac_version == RTL_GIGA_MAC_VER_41 ||
8548 tp->mac_version == RTL_GIGA_MAC_VER_42 ||
8549 tp->mac_version == RTL_GIGA_MAC_VER_43 ||
8550 tp->mac_version == RTL_GIGA_MAC_VER_44 ||
8551 tp->mac_version == RTL_GIGA_MAC_VER_45 ||
6e1d0b89
CHL
8552 tp->mac_version == RTL_GIGA_MAC_VER_46 ||
8553 tp->mac_version == RTL_GIGA_MAC_VER_47 ||
935e2218
CHL
8554 tp->mac_version == RTL_GIGA_MAC_VER_48 ||
8555 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8556 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8557 tp->mac_version == RTL_GIGA_MAC_VER_51) {
6e1d0b89
CHL
8558 u16 mac_addr[3];
8559
05b9687b
CHL
8560 *(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
8561 *(u16 *)&mac_addr[2] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
6e1d0b89
CHL
8562
8563 if (is_valid_ether_addr((u8 *)mac_addr))
8564 rtl_rar_set(tp, (u8 *)mac_addr);
8565 }
3b6cf25d
FR
8566 for (i = 0; i < ETH_ALEN; i++)
8567 dev->dev_addr[i] = RTL_R8(MAC0 + i);
3b6cf25d 8568
7ad24ea4 8569 dev->ethtool_ops = &rtl8169_ethtool_ops;
3b6cf25d 8570 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3b6cf25d
FR
8571
8572 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
8573
8574 /* don't enable SG, IP_CSUM and TSO by default - it might not work
8575 * properly for all devices */
8576 dev->features |= NETIF_F_RXCSUM |
f646968f 8577 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
3b6cf25d
FR
8578
8579 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
f646968f
PM
8580 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
8581 NETIF_F_HW_VLAN_CTAG_RX;
3b6cf25d
FR
8582 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
8583 NETIF_F_HIGHDMA;
8584
929a031d 8585 tp->cp_cmd |= RxChkSum | RxVlan;
8586
8587 /*
8588 * Pretend we are using VLANs; This bypasses a nasty bug where
8589 * Interrupts stop flowing on high load on 8110SCd controllers.
8590 */
3b6cf25d 8591 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
929a031d 8592 /* Disallow toggling */
f646968f 8593 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
3b6cf25d 8594
5888d3fc 8595 if (tp->txd_version == RTL_TD_0)
8596 tp->tso_csum = rtl8169_tso_csum_v1;
e974604b 8597 else if (tp->txd_version == RTL_TD_1) {
5888d3fc 8598 tp->tso_csum = rtl8169_tso_csum_v2;
e974604b 8599 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
8600 } else
5888d3fc 8601 WARN_ON_ONCE(1);
8602
3b6cf25d
FR
8603 dev->hw_features |= NETIF_F_RXALL;
8604 dev->hw_features |= NETIF_F_RXFCS;
8605
c7315a95
JW
8606 /* MTU range: 60 - hw-specific max */
8607 dev->min_mtu = ETH_ZLEN;
8608 dev->max_mtu = rtl_chip_infos[chipset].jumbo_max;
8609
3b6cf25d
FR
8610 tp->hw_start = cfg->hw_start;
8611 tp->event_slow = cfg->event_slow;
50970831 8612 tp->coalesce_info = cfg->coalesce_info;
3b6cf25d
FR
8613
8614 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
8615 ~(RxBOVF | RxFOVF) : ~0;
8616
9de36ccf 8617 timer_setup(&tp->timer, rtl8169_phy_timer, 0);
3b6cf25d
FR
8618
8619 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
8620
4c45d24a
HK
8621 tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
8622 &tp->counters_phys_addr,
8623 GFP_KERNEL);
4cf964af
HK
8624 if (!tp->counters)
8625 return -ENOMEM;
42020320 8626
3b6cf25d
FR
8627 rc = register_netdev(dev);
8628 if (rc < 0)
4cf964af 8629 return rc;
3b6cf25d
FR
8630
8631 pci_set_drvdata(pdev, dev);
8632
92a7c4e7
FR
8633 netif_info(tp, probe, dev, "%s at 0x%p, %pM, XID %08x IRQ %d\n",
8634 rtl_chip_infos[chipset].name, ioaddr, dev->dev_addr,
8635 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), pdev->irq);
3b6cf25d
FR
8636 if (rtl_chip_infos[chipset].jumbo_max != JUMBO_1K) {
8637 netif_info(tp, probe, dev, "jumbo features [frames: %d bytes, "
8638 "tx checksumming: %s]\n",
8639 rtl_chip_infos[chipset].jumbo_max,
8640 rtl_chip_infos[chipset].jumbo_tx_csum ? "ok" : "ko");
8641 }
8642
ee7a1beb
CHL
8643 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
8644 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
935e2218
CHL
8645 tp->mac_version == RTL_GIGA_MAC_VER_31 ||
8646 tp->mac_version == RTL_GIGA_MAC_VER_49 ||
8647 tp->mac_version == RTL_GIGA_MAC_VER_50 ||
8648 tp->mac_version == RTL_GIGA_MAC_VER_51) &&
ee7a1beb 8649 r8168_check_dash(tp)) {
3b6cf25d
FR
8650 rtl8168_driver_start(tp);
8651 }
8652
3b6cf25d
FR
8653 netif_carrier_off(dev);
8654
a92a0849
HK
8655 if (pci_dev_run_wake(pdev))
8656 pm_runtime_put_sync(&pdev->dev);
8657
4c45d24a 8658 return 0;
3b6cf25d
FR
8659}
8660
1da177e4
LT
8661static struct pci_driver rtl8169_pci_driver = {
8662 .name = MODULENAME,
8663 .id_table = rtl8169_pci_tbl,
3b6cf25d 8664 .probe = rtl_init_one,
baf63293 8665 .remove = rtl_remove_one,
1765f95d 8666 .shutdown = rtl_shutdown,
861ab440 8667 .driver.pm = RTL8169_PM_OPS,
1da177e4
LT
8668};
8669
3eeb7da9 8670module_pci_driver(rtl8169_pci_driver);