]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/realtek/r8169.c
r8169: improve RTL8168d PHY initialization
[linux.git] / drivers / net / ethernet / realtek / r8169.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * r8169.c: RealTek 8169/8168/8101 ethernet driver.
4  *
5  * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
6  * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
7  * Copyright (c) a lot of people too. Please respect their work.
8  *
9  * See MAINTAINERS file for support contact information.
10  */
11
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/pci.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/ethtool.h>
20 #include <linux/phy.h>
21 #include <linux/if_vlan.h>
22 #include <linux/crc32.h>
23 #include <linux/in.h>
24 #include <linux/io.h>
25 #include <linux/ip.h>
26 #include <linux/tcp.h>
27 #include <linux/interrupt.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/firmware.h>
31 #include <linux/prefetch.h>
32 #include <linux/pci-aspm.h>
33 #include <linux/ipv6.h>
34 #include <net/ip6_checksum.h>
35
36 #define MODULENAME "r8169"
37
38 #define FIRMWARE_8168D_1        "rtl_nic/rtl8168d-1.fw"
39 #define FIRMWARE_8168D_2        "rtl_nic/rtl8168d-2.fw"
40 #define FIRMWARE_8168E_1        "rtl_nic/rtl8168e-1.fw"
41 #define FIRMWARE_8168E_2        "rtl_nic/rtl8168e-2.fw"
42 #define FIRMWARE_8168E_3        "rtl_nic/rtl8168e-3.fw"
43 #define FIRMWARE_8168F_1        "rtl_nic/rtl8168f-1.fw"
44 #define FIRMWARE_8168F_2        "rtl_nic/rtl8168f-2.fw"
45 #define FIRMWARE_8105E_1        "rtl_nic/rtl8105e-1.fw"
46 #define FIRMWARE_8402_1         "rtl_nic/rtl8402-1.fw"
47 #define FIRMWARE_8411_1         "rtl_nic/rtl8411-1.fw"
48 #define FIRMWARE_8411_2         "rtl_nic/rtl8411-2.fw"
49 #define FIRMWARE_8106E_1        "rtl_nic/rtl8106e-1.fw"
50 #define FIRMWARE_8106E_2        "rtl_nic/rtl8106e-2.fw"
51 #define FIRMWARE_8168G_2        "rtl_nic/rtl8168g-2.fw"
52 #define FIRMWARE_8168G_3        "rtl_nic/rtl8168g-3.fw"
53 #define FIRMWARE_8168H_1        "rtl_nic/rtl8168h-1.fw"
54 #define FIRMWARE_8168H_2        "rtl_nic/rtl8168h-2.fw"
55 #define FIRMWARE_8107E_1        "rtl_nic/rtl8107e-1.fw"
56 #define FIRMWARE_8107E_2        "rtl_nic/rtl8107e-2.fw"
57
58 #define R8169_MSG_DEFAULT \
59         (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
60
61 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
62    The RTL chips use a 64 element hash table based on the Ethernet CRC. */
63 static const int multicast_filter_limit = 32;
64
65 #define TX_DMA_BURST    7       /* Maximum PCI burst, '7' is unlimited */
66 #define InterFrameGap   0x03    /* 3 means InterFrameGap = the shortest one */
67
68 #define R8169_REGS_SIZE         256
69 #define R8169_RX_BUF_SIZE       (SZ_16K - 1)
70 #define NUM_TX_DESC     64      /* Number of Tx descriptor registers */
71 #define NUM_RX_DESC     256U    /* Number of Rx descriptor registers */
72 #define R8169_TX_RING_BYTES     (NUM_TX_DESC * sizeof(struct TxDesc))
73 #define R8169_RX_RING_BYTES     (NUM_RX_DESC * sizeof(struct RxDesc))
74
75 /* write/read MMIO register */
76 #define RTL_W8(tp, reg, val8)   writeb((val8), tp->mmio_addr + (reg))
77 #define RTL_W16(tp, reg, val16) writew((val16), tp->mmio_addr + (reg))
78 #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg))
79 #define RTL_R8(tp, reg)         readb(tp->mmio_addr + (reg))
80 #define RTL_R16(tp, reg)                readw(tp->mmio_addr + (reg))
81 #define RTL_R32(tp, reg)                readl(tp->mmio_addr + (reg))
82
83 enum mac_version {
84         RTL_GIGA_MAC_VER_01 = 0,
85         RTL_GIGA_MAC_VER_02,
86         RTL_GIGA_MAC_VER_03,
87         RTL_GIGA_MAC_VER_04,
88         RTL_GIGA_MAC_VER_05,
89         RTL_GIGA_MAC_VER_06,
90         RTL_GIGA_MAC_VER_07,
91         RTL_GIGA_MAC_VER_08,
92         RTL_GIGA_MAC_VER_09,
93         RTL_GIGA_MAC_VER_10,
94         RTL_GIGA_MAC_VER_11,
95         RTL_GIGA_MAC_VER_12,
96         RTL_GIGA_MAC_VER_13,
97         RTL_GIGA_MAC_VER_14,
98         RTL_GIGA_MAC_VER_15,
99         RTL_GIGA_MAC_VER_16,
100         RTL_GIGA_MAC_VER_17,
101         RTL_GIGA_MAC_VER_18,
102         RTL_GIGA_MAC_VER_19,
103         RTL_GIGA_MAC_VER_20,
104         RTL_GIGA_MAC_VER_21,
105         RTL_GIGA_MAC_VER_22,
106         RTL_GIGA_MAC_VER_23,
107         RTL_GIGA_MAC_VER_24,
108         RTL_GIGA_MAC_VER_25,
109         RTL_GIGA_MAC_VER_26,
110         RTL_GIGA_MAC_VER_27,
111         RTL_GIGA_MAC_VER_28,
112         RTL_GIGA_MAC_VER_29,
113         RTL_GIGA_MAC_VER_30,
114         RTL_GIGA_MAC_VER_31,
115         RTL_GIGA_MAC_VER_32,
116         RTL_GIGA_MAC_VER_33,
117         RTL_GIGA_MAC_VER_34,
118         RTL_GIGA_MAC_VER_35,
119         RTL_GIGA_MAC_VER_36,
120         RTL_GIGA_MAC_VER_37,
121         RTL_GIGA_MAC_VER_38,
122         RTL_GIGA_MAC_VER_39,
123         RTL_GIGA_MAC_VER_40,
124         RTL_GIGA_MAC_VER_41,
125         RTL_GIGA_MAC_VER_42,
126         RTL_GIGA_MAC_VER_43,
127         RTL_GIGA_MAC_VER_44,
128         RTL_GIGA_MAC_VER_45,
129         RTL_GIGA_MAC_VER_46,
130         RTL_GIGA_MAC_VER_47,
131         RTL_GIGA_MAC_VER_48,
132         RTL_GIGA_MAC_VER_49,
133         RTL_GIGA_MAC_VER_50,
134         RTL_GIGA_MAC_VER_51,
135         RTL_GIGA_MAC_NONE
136 };
137
138 #define JUMBO_1K        ETH_DATA_LEN
139 #define JUMBO_4K        (4*1024 - ETH_HLEN - 2)
140 #define JUMBO_6K        (6*1024 - ETH_HLEN - 2)
141 #define JUMBO_7K        (7*1024 - ETH_HLEN - 2)
142 #define JUMBO_9K        (9*1024 - ETH_HLEN - 2)
143
144 static const struct {
145         const char *name;
146         const char *fw_name;
147 } rtl_chip_infos[] = {
148         /* PCI devices. */
149         [RTL_GIGA_MAC_VER_01] = {"RTL8169"                              },
150         [RTL_GIGA_MAC_VER_02] = {"RTL8169s"                             },
151         [RTL_GIGA_MAC_VER_03] = {"RTL8110s"                             },
152         [RTL_GIGA_MAC_VER_04] = {"RTL8169sb/8110sb"                     },
153         [RTL_GIGA_MAC_VER_05] = {"RTL8169sc/8110sc"                     },
154         [RTL_GIGA_MAC_VER_06] = {"RTL8169sc/8110sc"                     },
155         /* PCI-E devices. */
156         [RTL_GIGA_MAC_VER_07] = {"RTL8102e"                             },
157         [RTL_GIGA_MAC_VER_08] = {"RTL8102e"                             },
158         [RTL_GIGA_MAC_VER_09] = {"RTL8102e"                             },
159         [RTL_GIGA_MAC_VER_10] = {"RTL8101e"                             },
160         [RTL_GIGA_MAC_VER_11] = {"RTL8168b/8111b"                       },
161         [RTL_GIGA_MAC_VER_12] = {"RTL8168b/8111b"                       },
162         [RTL_GIGA_MAC_VER_13] = {"RTL8101e"                             },
163         [RTL_GIGA_MAC_VER_14] = {"RTL8100e"                             },
164         [RTL_GIGA_MAC_VER_15] = {"RTL8100e"                             },
165         [RTL_GIGA_MAC_VER_16] = {"RTL8101e"                             },
166         [RTL_GIGA_MAC_VER_17] = {"RTL8168b/8111b"                       },
167         [RTL_GIGA_MAC_VER_18] = {"RTL8168cp/8111cp"                     },
168         [RTL_GIGA_MAC_VER_19] = {"RTL8168c/8111c"                       },
169         [RTL_GIGA_MAC_VER_20] = {"RTL8168c/8111c"                       },
170         [RTL_GIGA_MAC_VER_21] = {"RTL8168c/8111c"                       },
171         [RTL_GIGA_MAC_VER_22] = {"RTL8168c/8111c"                       },
172         [RTL_GIGA_MAC_VER_23] = {"RTL8168cp/8111cp"                     },
173         [RTL_GIGA_MAC_VER_24] = {"RTL8168cp/8111cp"                     },
174         [RTL_GIGA_MAC_VER_25] = {"RTL8168d/8111d",      FIRMWARE_8168D_1},
175         [RTL_GIGA_MAC_VER_26] = {"RTL8168d/8111d",      FIRMWARE_8168D_2},
176         [RTL_GIGA_MAC_VER_27] = {"RTL8168dp/8111dp"                     },
177         [RTL_GIGA_MAC_VER_28] = {"RTL8168dp/8111dp"                     },
178         [RTL_GIGA_MAC_VER_29] = {"RTL8105e",            FIRMWARE_8105E_1},
179         [RTL_GIGA_MAC_VER_30] = {"RTL8105e",            FIRMWARE_8105E_1},
180         [RTL_GIGA_MAC_VER_31] = {"RTL8168dp/8111dp"                     },
181         [RTL_GIGA_MAC_VER_32] = {"RTL8168e/8111e",      FIRMWARE_8168E_1},
182         [RTL_GIGA_MAC_VER_33] = {"RTL8168e/8111e",      FIRMWARE_8168E_2},
183         [RTL_GIGA_MAC_VER_34] = {"RTL8168evl/8111evl",  FIRMWARE_8168E_3},
184         [RTL_GIGA_MAC_VER_35] = {"RTL8168f/8111f",      FIRMWARE_8168F_1},
185         [RTL_GIGA_MAC_VER_36] = {"RTL8168f/8111f",      FIRMWARE_8168F_2},
186         [RTL_GIGA_MAC_VER_37] = {"RTL8402",             FIRMWARE_8402_1 },
187         [RTL_GIGA_MAC_VER_38] = {"RTL8411",             FIRMWARE_8411_1 },
188         [RTL_GIGA_MAC_VER_39] = {"RTL8106e",            FIRMWARE_8106E_1},
189         [RTL_GIGA_MAC_VER_40] = {"RTL8168g/8111g",      FIRMWARE_8168G_2},
190         [RTL_GIGA_MAC_VER_41] = {"RTL8168g/8111g"                       },
191         [RTL_GIGA_MAC_VER_42] = {"RTL8168g/8111g",      FIRMWARE_8168G_3},
192         [RTL_GIGA_MAC_VER_43] = {"RTL8106e",            FIRMWARE_8106E_2},
193         [RTL_GIGA_MAC_VER_44] = {"RTL8411",             FIRMWARE_8411_2 },
194         [RTL_GIGA_MAC_VER_45] = {"RTL8168h/8111h",      FIRMWARE_8168H_1},
195         [RTL_GIGA_MAC_VER_46] = {"RTL8168h/8111h",      FIRMWARE_8168H_2},
196         [RTL_GIGA_MAC_VER_47] = {"RTL8107e",            FIRMWARE_8107E_1},
197         [RTL_GIGA_MAC_VER_48] = {"RTL8107e",            FIRMWARE_8107E_2},
198         [RTL_GIGA_MAC_VER_49] = {"RTL8168ep/8111ep"                     },
199         [RTL_GIGA_MAC_VER_50] = {"RTL8168ep/8111ep"                     },
200         [RTL_GIGA_MAC_VER_51] = {"RTL8168ep/8111ep"                     },
201 };
202
203 enum cfg_version {
204         RTL_CFG_0 = 0x00,
205         RTL_CFG_1,
206         RTL_CFG_2
207 };
208
209 static const struct pci_device_id rtl8169_pci_tbl[] = {
210         { PCI_VDEVICE(REALTEK,  0x2502), RTL_CFG_1 },
211         { PCI_VDEVICE(REALTEK,  0x2600), RTL_CFG_1 },
212         { PCI_VDEVICE(REALTEK,  0x8129), RTL_CFG_0 },
213         { PCI_VDEVICE(REALTEK,  0x8136), RTL_CFG_2 },
214         { PCI_VDEVICE(REALTEK,  0x8161), RTL_CFG_1 },
215         { PCI_VDEVICE(REALTEK,  0x8167), RTL_CFG_0 },
216         { PCI_VDEVICE(REALTEK,  0x8168), RTL_CFG_1 },
217         { PCI_VDEVICE(NCUBE,    0x8168), RTL_CFG_1 },
218         { PCI_VDEVICE(REALTEK,  0x8169), RTL_CFG_0 },
219         { PCI_VENDOR_ID_DLINK,  0x4300,
220                 PCI_VENDOR_ID_DLINK, 0x4b10, 0, 0, RTL_CFG_1 },
221         { PCI_VDEVICE(DLINK,    0x4300), RTL_CFG_0 },
222         { PCI_VDEVICE(DLINK,    0x4302), RTL_CFG_0 },
223         { PCI_VDEVICE(AT,       0xc107), RTL_CFG_0 },
224         { PCI_VDEVICE(USR,      0x0116), RTL_CFG_0 },
225         { PCI_VENDOR_ID_LINKSYS,                0x1032,
226                 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
227         { 0x0001,                               0x8168,
228                 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
229         {}
230 };
231
232 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
233
234 static struct {
235         u32 msg_enable;
236 } debug = { -1 };
237
238 enum rtl_registers {
239         MAC0            = 0,    /* Ethernet hardware address. */
240         MAC4            = 4,
241         MAR0            = 8,    /* Multicast filter. */
242         CounterAddrLow          = 0x10,
243         CounterAddrHigh         = 0x14,
244         TxDescStartAddrLow      = 0x20,
245         TxDescStartAddrHigh     = 0x24,
246         TxHDescStartAddrLow     = 0x28,
247         TxHDescStartAddrHigh    = 0x2c,
248         FLASH           = 0x30,
249         ERSR            = 0x36,
250         ChipCmd         = 0x37,
251         TxPoll          = 0x38,
252         IntrMask        = 0x3c,
253         IntrStatus      = 0x3e,
254
255         TxConfig        = 0x40,
256 #define TXCFG_AUTO_FIFO                 (1 << 7)        /* 8111e-vl */
257 #define TXCFG_EMPTY                     (1 << 11)       /* 8111e-vl */
258
259         RxConfig        = 0x44,
260 #define RX128_INT_EN                    (1 << 15)       /* 8111c and later */
261 #define RX_MULTI_EN                     (1 << 14)       /* 8111c only */
262 #define RXCFG_FIFO_SHIFT                13
263                                         /* No threshold before first PCI xfer */
264 #define RX_FIFO_THRESH                  (7 << RXCFG_FIFO_SHIFT)
265 #define RX_EARLY_OFF                    (1 << 11)
266 #define RXCFG_DMA_SHIFT                 8
267                                         /* Unlimited maximum PCI burst. */
268 #define RX_DMA_BURST                    (7 << RXCFG_DMA_SHIFT)
269
270         RxMissed        = 0x4c,
271         Cfg9346         = 0x50,
272         Config0         = 0x51,
273         Config1         = 0x52,
274         Config2         = 0x53,
275 #define PME_SIGNAL                      (1 << 5)        /* 8168c and later */
276
277         Config3         = 0x54,
278         Config4         = 0x55,
279         Config5         = 0x56,
280         MultiIntr       = 0x5c,
281         PHYAR           = 0x60,
282         PHYstatus       = 0x6c,
283         RxMaxSize       = 0xda,
284         CPlusCmd        = 0xe0,
285         IntrMitigate    = 0xe2,
286
287 #define RTL_COALESCE_MASK       0x0f
288 #define RTL_COALESCE_SHIFT      4
289 #define RTL_COALESCE_T_MAX      (RTL_COALESCE_MASK)
290 #define RTL_COALESCE_FRAME_MAX  (RTL_COALESCE_MASK << 2)
291
292         RxDescAddrLow   = 0xe4,
293         RxDescAddrHigh  = 0xe8,
294         EarlyTxThres    = 0xec, /* 8169. Unit of 32 bytes. */
295
296 #define NoEarlyTx       0x3f    /* Max value : no early transmit. */
297
298         MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
299
300 #define TxPacketMax     (8064 >> 7)
301 #define EarlySize       0x27
302
303         FuncEvent       = 0xf0,
304         FuncEventMask   = 0xf4,
305         FuncPresetState = 0xf8,
306         IBCR0           = 0xf8,
307         IBCR2           = 0xf9,
308         IBIMR0          = 0xfa,
309         IBISR0          = 0xfb,
310         FuncForceEvent  = 0xfc,
311 };
312
313 enum rtl8168_8101_registers {
314         CSIDR                   = 0x64,
315         CSIAR                   = 0x68,
316 #define CSIAR_FLAG                      0x80000000
317 #define CSIAR_WRITE_CMD                 0x80000000
318 #define CSIAR_BYTE_ENABLE               0x0000f000
319 #define CSIAR_ADDR_MASK                 0x00000fff
320         PMCH                    = 0x6f,
321         EPHYAR                  = 0x80,
322 #define EPHYAR_FLAG                     0x80000000
323 #define EPHYAR_WRITE_CMD                0x80000000
324 #define EPHYAR_REG_MASK                 0x1f
325 #define EPHYAR_REG_SHIFT                16
326 #define EPHYAR_DATA_MASK                0xffff
327         DLLPR                   = 0xd0,
328 #define PFM_EN                          (1 << 6)
329 #define TX_10M_PS_EN                    (1 << 7)
330         DBG_REG                 = 0xd1,
331 #define FIX_NAK_1                       (1 << 4)
332 #define FIX_NAK_2                       (1 << 3)
333         TWSI                    = 0xd2,
334         MCU                     = 0xd3,
335 #define NOW_IS_OOB                      (1 << 7)
336 #define TX_EMPTY                        (1 << 5)
337 #define RX_EMPTY                        (1 << 4)
338 #define RXTX_EMPTY                      (TX_EMPTY | RX_EMPTY)
339 #define EN_NDP                          (1 << 3)
340 #define EN_OOB_RESET                    (1 << 2)
341 #define LINK_LIST_RDY                   (1 << 1)
342         EFUSEAR                 = 0xdc,
343 #define EFUSEAR_FLAG                    0x80000000
344 #define EFUSEAR_WRITE_CMD               0x80000000
345 #define EFUSEAR_READ_CMD                0x00000000
346 #define EFUSEAR_REG_MASK                0x03ff
347 #define EFUSEAR_REG_SHIFT               8
348 #define EFUSEAR_DATA_MASK               0xff
349         MISC_1                  = 0xf2,
350 #define PFM_D3COLD_EN                   (1 << 6)
351 };
352
353 enum rtl8168_registers {
354         LED_FREQ                = 0x1a,
355         EEE_LED                 = 0x1b,
356         ERIDR                   = 0x70,
357         ERIAR                   = 0x74,
358 #define ERIAR_FLAG                      0x80000000
359 #define ERIAR_WRITE_CMD                 0x80000000
360 #define ERIAR_READ_CMD                  0x00000000
361 #define ERIAR_ADDR_BYTE_ALIGN           4
362 #define ERIAR_TYPE_SHIFT                16
363 #define ERIAR_EXGMAC                    (0x00 << ERIAR_TYPE_SHIFT)
364 #define ERIAR_MSIX                      (0x01 << ERIAR_TYPE_SHIFT)
365 #define ERIAR_ASF                       (0x02 << ERIAR_TYPE_SHIFT)
366 #define ERIAR_OOB                       (0x02 << ERIAR_TYPE_SHIFT)
367 #define ERIAR_MASK_SHIFT                12
368 #define ERIAR_MASK_0001                 (0x1 << ERIAR_MASK_SHIFT)
369 #define ERIAR_MASK_0011                 (0x3 << ERIAR_MASK_SHIFT)
370 #define ERIAR_MASK_0100                 (0x4 << ERIAR_MASK_SHIFT)
371 #define ERIAR_MASK_0101                 (0x5 << ERIAR_MASK_SHIFT)
372 #define ERIAR_MASK_1111                 (0xf << ERIAR_MASK_SHIFT)
373         EPHY_RXER_NUM           = 0x7c,
374         OCPDR                   = 0xb0, /* OCP GPHY access */
375 #define OCPDR_WRITE_CMD                 0x80000000
376 #define OCPDR_READ_CMD                  0x00000000
377 #define OCPDR_REG_MASK                  0x7f
378 #define OCPDR_GPHY_REG_SHIFT            16
379 #define OCPDR_DATA_MASK                 0xffff
380         OCPAR                   = 0xb4,
381 #define OCPAR_FLAG                      0x80000000
382 #define OCPAR_GPHY_WRITE_CMD            0x8000f060
383 #define OCPAR_GPHY_READ_CMD             0x0000f060
384         GPHY_OCP                = 0xb8,
385         RDSAR1                  = 0xd0, /* 8168c only. Undocumented on 8168dp */
386         MISC                    = 0xf0, /* 8168e only. */
387 #define TXPLA_RST                       (1 << 29)
388 #define DISABLE_LAN_EN                  (1 << 23) /* Enable GPIO pin */
389 #define PWM_EN                          (1 << 22)
390 #define RXDV_GATED_EN                   (1 << 19)
391 #define EARLY_TALLY_EN                  (1 << 16)
392 };
393
394 enum rtl_register_content {
395         /* InterruptStatusBits */
396         SYSErr          = 0x8000,
397         PCSTimeout      = 0x4000,
398         SWInt           = 0x0100,
399         TxDescUnavail   = 0x0080,
400         RxFIFOOver      = 0x0040,
401         LinkChg         = 0x0020,
402         RxOverflow      = 0x0010,
403         TxErr           = 0x0008,
404         TxOK            = 0x0004,
405         RxErr           = 0x0002,
406         RxOK            = 0x0001,
407
408         /* RxStatusDesc */
409         RxBOVF  = (1 << 24),
410         RxFOVF  = (1 << 23),
411         RxRWT   = (1 << 22),
412         RxRES   = (1 << 21),
413         RxRUNT  = (1 << 20),
414         RxCRC   = (1 << 19),
415
416         /* ChipCmdBits */
417         StopReq         = 0x80,
418         CmdReset        = 0x10,
419         CmdRxEnb        = 0x08,
420         CmdTxEnb        = 0x04,
421         RxBufEmpty      = 0x01,
422
423         /* TXPoll register p.5 */
424         HPQ             = 0x80,         /* Poll cmd on the high prio queue */
425         NPQ             = 0x40,         /* Poll cmd on the low prio queue */
426         FSWInt          = 0x01,         /* Forced software interrupt */
427
428         /* Cfg9346Bits */
429         Cfg9346_Lock    = 0x00,
430         Cfg9346_Unlock  = 0xc0,
431
432         /* rx_mode_bits */
433         AcceptErr       = 0x20,
434         AcceptRunt      = 0x10,
435         AcceptBroadcast = 0x08,
436         AcceptMulticast = 0x04,
437         AcceptMyPhys    = 0x02,
438         AcceptAllPhys   = 0x01,
439 #define RX_CONFIG_ACCEPT_MASK           0x3f
440
441         /* TxConfigBits */
442         TxInterFrameGapShift = 24,
443         TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
444
445         /* Config1 register p.24 */
446         LEDS1           = (1 << 7),
447         LEDS0           = (1 << 6),
448         Speed_down      = (1 << 4),
449         MEMMAP          = (1 << 3),
450         IOMAP           = (1 << 2),
451         VPD             = (1 << 1),
452         PMEnable        = (1 << 0),     /* Power Management Enable */
453
454         /* Config2 register p. 25 */
455         ClkReqEn        = (1 << 7),     /* Clock Request Enable */
456         MSIEnable       = (1 << 5),     /* 8169 only. Reserved in the 8168. */
457         PCI_Clock_66MHz = 0x01,
458         PCI_Clock_33MHz = 0x00,
459
460         /* Config3 register p.25 */
461         MagicPacket     = (1 << 5),     /* Wake up when receives a Magic Packet */
462         LinkUp          = (1 << 4),     /* Wake up when the cable connection is re-established */
463         Jumbo_En0       = (1 << 2),     /* 8168 only. Reserved in the 8168b */
464         Rdy_to_L23      = (1 << 1),     /* L23 Enable */
465         Beacon_en       = (1 << 0),     /* 8168 only. Reserved in the 8168b */
466
467         /* Config4 register */
468         Jumbo_En1       = (1 << 1),     /* 8168 only. Reserved in the 8168b */
469
470         /* Config5 register p.27 */
471         BWF             = (1 << 6),     /* Accept Broadcast wakeup frame */
472         MWF             = (1 << 5),     /* Accept Multicast wakeup frame */
473         UWF             = (1 << 4),     /* Accept Unicast wakeup frame */
474         Spi_en          = (1 << 3),
475         LanWake         = (1 << 1),     /* LanWake enable/disable */
476         PMEStatus       = (1 << 0),     /* PME status can be reset by PCI RST# */
477         ASPM_en         = (1 << 0),     /* ASPM enable */
478
479         /* CPlusCmd p.31 */
480         EnableBist      = (1 << 15),    // 8168 8101
481         Mac_dbgo_oe     = (1 << 14),    // 8168 8101
482         Normal_mode     = (1 << 13),    // unused
483         Force_half_dup  = (1 << 12),    // 8168 8101
484         Force_rxflow_en = (1 << 11),    // 8168 8101
485         Force_txflow_en = (1 << 10),    // 8168 8101
486         Cxpl_dbg_sel    = (1 << 9),     // 8168 8101
487         ASF             = (1 << 8),     // 8168 8101
488         PktCntrDisable  = (1 << 7),     // 8168 8101
489         Mac_dbgo_sel    = 0x001c,       // 8168
490         RxVlan          = (1 << 6),
491         RxChkSum        = (1 << 5),
492         PCIDAC          = (1 << 4),
493         PCIMulRW        = (1 << 3),
494 #define INTT_MASK       GENMASK(1, 0)
495
496         /* rtl8169_PHYstatus */
497         TBI_Enable      = 0x80,
498         TxFlowCtrl      = 0x40,
499         RxFlowCtrl      = 0x20,
500         _1000bpsF       = 0x10,
501         _100bps         = 0x08,
502         _10bps          = 0x04,
503         LinkStatus      = 0x02,
504         FullDup         = 0x01,
505
506         /* _TBICSRBit */
507         TBILinkOK       = 0x02000000,
508
509         /* ResetCounterCommand */
510         CounterReset    = 0x1,
511
512         /* DumpCounterCommand */
513         CounterDump     = 0x8,
514
515         /* magic enable v2 */
516         MagicPacket_v2  = (1 << 16),    /* Wake up when receives a Magic Packet */
517 };
518
519 enum rtl_desc_bit {
520         /* First doubleword. */
521         DescOwn         = (1 << 31), /* Descriptor is owned by NIC */
522         RingEnd         = (1 << 30), /* End of descriptor ring */
523         FirstFrag       = (1 << 29), /* First segment of a packet */
524         LastFrag        = (1 << 28), /* Final segment of a packet */
525 };
526
527 /* Generic case. */
528 enum rtl_tx_desc_bit {
529         /* First doubleword. */
530         TD_LSO          = (1 << 27),            /* Large Send Offload */
531 #define TD_MSS_MAX                      0x07ffu /* MSS value */
532
533         /* Second doubleword. */
534         TxVlanTag       = (1 << 17),            /* Add VLAN tag */
535 };
536
537 /* 8169, 8168b and 810x except 8102e. */
538 enum rtl_tx_desc_bit_0 {
539         /* First doubleword. */
540 #define TD0_MSS_SHIFT                   16      /* MSS position (11 bits) */
541         TD0_TCP_CS      = (1 << 16),            /* Calculate TCP/IP checksum */
542         TD0_UDP_CS      = (1 << 17),            /* Calculate UDP/IP checksum */
543         TD0_IP_CS       = (1 << 18),            /* Calculate IP checksum */
544 };
545
546 /* 8102e, 8168c and beyond. */
547 enum rtl_tx_desc_bit_1 {
548         /* First doubleword. */
549         TD1_GTSENV4     = (1 << 26),            /* Giant Send for IPv4 */
550         TD1_GTSENV6     = (1 << 25),            /* Giant Send for IPv6 */
551 #define GTTCPHO_SHIFT                   18
552 #define GTTCPHO_MAX                     0x7fU
553
554         /* Second doubleword. */
555 #define TCPHO_SHIFT                     18
556 #define TCPHO_MAX                       0x3ffU
557 #define TD1_MSS_SHIFT                   18      /* MSS position (11 bits) */
558         TD1_IPv6_CS     = (1 << 28),            /* Calculate IPv6 checksum */
559         TD1_IPv4_CS     = (1 << 29),            /* Calculate IPv4 checksum */
560         TD1_TCP_CS      = (1 << 30),            /* Calculate TCP/IP checksum */
561         TD1_UDP_CS      = (1 << 31),            /* Calculate UDP/IP checksum */
562 };
563
564 enum rtl_rx_desc_bit {
565         /* Rx private */
566         PID1            = (1 << 18), /* Protocol ID bit 1/2 */
567         PID0            = (1 << 17), /* Protocol ID bit 0/2 */
568
569 #define RxProtoUDP      (PID1)
570 #define RxProtoTCP      (PID0)
571 #define RxProtoIP       (PID1 | PID0)
572 #define RxProtoMask     RxProtoIP
573
574         IPFail          = (1 << 16), /* IP checksum failed */
575         UDPFail         = (1 << 15), /* UDP/IP checksum failed */
576         TCPFail         = (1 << 14), /* TCP/IP checksum failed */
577         RxVlanTag       = (1 << 16), /* VLAN tag available */
578 };
579
580 #define RsvdMask        0x3fffc000
581 #define CPCMD_QUIRK_MASK        (Normal_mode | RxVlan | RxChkSum | INTT_MASK)
582
583 struct TxDesc {
584         __le32 opts1;
585         __le32 opts2;
586         __le64 addr;
587 };
588
589 struct RxDesc {
590         __le32 opts1;
591         __le32 opts2;
592         __le64 addr;
593 };
594
595 struct ring_info {
596         struct sk_buff  *skb;
597         u32             len;
598 };
599
600 struct rtl8169_counters {
601         __le64  tx_packets;
602         __le64  rx_packets;
603         __le64  tx_errors;
604         __le32  rx_errors;
605         __le16  rx_missed;
606         __le16  align_errors;
607         __le32  tx_one_collision;
608         __le32  tx_multi_collision;
609         __le64  rx_unicast;
610         __le64  rx_broadcast;
611         __le32  rx_multicast;
612         __le16  tx_aborted;
613         __le16  tx_underun;
614 };
615
616 struct rtl8169_tc_offsets {
617         bool    inited;
618         __le64  tx_errors;
619         __le32  tx_multi_collision;
620         __le16  tx_aborted;
621 };
622
623 enum rtl_flag {
624         RTL_FLAG_TASK_ENABLED = 0,
625         RTL_FLAG_TASK_RESET_PENDING,
626         RTL_FLAG_MAX
627 };
628
629 struct rtl8169_stats {
630         u64                     packets;
631         u64                     bytes;
632         struct u64_stats_sync   syncp;
633 };
634
635 struct rtl8169_private {
636         void __iomem *mmio_addr;        /* memory map physical address */
637         struct pci_dev *pci_dev;
638         struct net_device *dev;
639         struct phy_device *phydev;
640         struct napi_struct napi;
641         u32 msg_enable;
642         enum mac_version mac_version;
643         u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
644         u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
645         u32 dirty_tx;
646         struct rtl8169_stats rx_stats;
647         struct rtl8169_stats tx_stats;
648         struct TxDesc *TxDescArray;     /* 256-aligned Tx descriptor ring */
649         struct RxDesc *RxDescArray;     /* 256-aligned Rx descriptor ring */
650         dma_addr_t TxPhyAddr;
651         dma_addr_t RxPhyAddr;
652         void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
653         struct ring_info tx_skb[NUM_TX_DESC];   /* Tx data buffers */
654         u16 cp_cmd;
655
656         u16 irq_mask;
657         const struct rtl_coalesce_info *coalesce_info;
658         struct clk *clk;
659
660         struct mdio_ops {
661                 void (*write)(struct rtl8169_private *, int, int);
662                 int (*read)(struct rtl8169_private *, int);
663         } mdio_ops;
664
665         struct jumbo_ops {
666                 void (*enable)(struct rtl8169_private *);
667                 void (*disable)(struct rtl8169_private *);
668         } jumbo_ops;
669
670         void (*hw_start)(struct rtl8169_private *tp);
671         bool (*tso_csum)(struct rtl8169_private *, struct sk_buff *, u32 *);
672
673         struct {
674                 DECLARE_BITMAP(flags, RTL_FLAG_MAX);
675                 struct mutex mutex;
676                 struct work_struct work;
677         } wk;
678
679         unsigned irq_enabled:1;
680         unsigned supports_gmii:1;
681         dma_addr_t counters_phys_addr;
682         struct rtl8169_counters *counters;
683         struct rtl8169_tc_offsets tc_offset;
684         u32 saved_wolopts;
685
686         const char *fw_name;
687         struct rtl_fw {
688                 const struct firmware *fw;
689
690 #define RTL_VER_SIZE            32
691
692                 char version[RTL_VER_SIZE];
693
694                 struct rtl_fw_phy_action {
695                         __le32 *code;
696                         size_t size;
697                 } phy_action;
698         } *rtl_fw;
699
700         u32 ocp_base;
701 };
702
703 typedef void (*rtl_generic_fct)(struct rtl8169_private *tp);
704
705 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
706 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
707 module_param_named(debug, debug.msg_enable, int, 0);
708 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
709 MODULE_SOFTDEP("pre: realtek");
710 MODULE_LICENSE("GPL");
711 MODULE_FIRMWARE(FIRMWARE_8168D_1);
712 MODULE_FIRMWARE(FIRMWARE_8168D_2);
713 MODULE_FIRMWARE(FIRMWARE_8168E_1);
714 MODULE_FIRMWARE(FIRMWARE_8168E_2);
715 MODULE_FIRMWARE(FIRMWARE_8168E_3);
716 MODULE_FIRMWARE(FIRMWARE_8105E_1);
717 MODULE_FIRMWARE(FIRMWARE_8168F_1);
718 MODULE_FIRMWARE(FIRMWARE_8168F_2);
719 MODULE_FIRMWARE(FIRMWARE_8402_1);
720 MODULE_FIRMWARE(FIRMWARE_8411_1);
721 MODULE_FIRMWARE(FIRMWARE_8411_2);
722 MODULE_FIRMWARE(FIRMWARE_8106E_1);
723 MODULE_FIRMWARE(FIRMWARE_8106E_2);
724 MODULE_FIRMWARE(FIRMWARE_8168G_2);
725 MODULE_FIRMWARE(FIRMWARE_8168G_3);
726 MODULE_FIRMWARE(FIRMWARE_8168H_1);
727 MODULE_FIRMWARE(FIRMWARE_8168H_2);
728 MODULE_FIRMWARE(FIRMWARE_8107E_1);
729 MODULE_FIRMWARE(FIRMWARE_8107E_2);
730
731 static inline struct device *tp_to_dev(struct rtl8169_private *tp)
732 {
733         return &tp->pci_dev->dev;
734 }
735
736 static void rtl_lock_work(struct rtl8169_private *tp)
737 {
738         mutex_lock(&tp->wk.mutex);
739 }
740
741 static void rtl_unlock_work(struct rtl8169_private *tp)
742 {
743         mutex_unlock(&tp->wk.mutex);
744 }
745
746 static void rtl_lock_config_regs(struct rtl8169_private *tp)
747 {
748         RTL_W8(tp, Cfg9346, Cfg9346_Lock);
749 }
750
751 static void rtl_unlock_config_regs(struct rtl8169_private *tp)
752 {
753         RTL_W8(tp, Cfg9346, Cfg9346_Unlock);
754 }
755
756 static void rtl_tx_performance_tweak(struct rtl8169_private *tp, u16 force)
757 {
758         pcie_capability_clear_and_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
759                                            PCI_EXP_DEVCTL_READRQ, force);
760 }
761
762 struct rtl_cond {
763         bool (*check)(struct rtl8169_private *);
764         const char *msg;
765 };
766
767 static void rtl_udelay(unsigned int d)
768 {
769         udelay(d);
770 }
771
772 static bool rtl_loop_wait(struct rtl8169_private *tp, const struct rtl_cond *c,
773                           void (*delay)(unsigned int), unsigned int d, int n,
774                           bool high)
775 {
776         int i;
777
778         for (i = 0; i < n; i++) {
779                 if (c->check(tp) == high)
780                         return true;
781                 delay(d);
782         }
783         netif_err(tp, drv, tp->dev, "%s == %d (loop: %d, delay: %d).\n",
784                   c->msg, !high, n, d);
785         return false;
786 }
787
788 static bool rtl_udelay_loop_wait_high(struct rtl8169_private *tp,
789                                       const struct rtl_cond *c,
790                                       unsigned int d, int n)
791 {
792         return rtl_loop_wait(tp, c, rtl_udelay, d, n, true);
793 }
794
795 static bool rtl_udelay_loop_wait_low(struct rtl8169_private *tp,
796                                      const struct rtl_cond *c,
797                                      unsigned int d, int n)
798 {
799         return rtl_loop_wait(tp, c, rtl_udelay, d, n, false);
800 }
801
802 static bool rtl_msleep_loop_wait_high(struct rtl8169_private *tp,
803                                       const struct rtl_cond *c,
804                                       unsigned int d, int n)
805 {
806         return rtl_loop_wait(tp, c, msleep, d, n, true);
807 }
808
809 static bool rtl_msleep_loop_wait_low(struct rtl8169_private *tp,
810                                      const struct rtl_cond *c,
811                                      unsigned int d, int n)
812 {
813         return rtl_loop_wait(tp, c, msleep, d, n, false);
814 }
815
816 #define DECLARE_RTL_COND(name)                          \
817 static bool name ## _check(struct rtl8169_private *);   \
818                                                         \
819 static const struct rtl_cond name = {                   \
820         .check  = name ## _check,                       \
821         .msg    = #name                                 \
822 };                                                      \
823                                                         \
824 static bool name ## _check(struct rtl8169_private *tp)
825
826 static bool rtl_ocp_reg_failure(struct rtl8169_private *tp, u32 reg)
827 {
828         if (reg & 0xffff0001) {
829                 netif_err(tp, drv, tp->dev, "Invalid ocp reg %x!\n", reg);
830                 return true;
831         }
832         return false;
833 }
834
835 DECLARE_RTL_COND(rtl_ocp_gphy_cond)
836 {
837         return RTL_R32(tp, GPHY_OCP) & OCPAR_FLAG;
838 }
839
840 static void r8168_phy_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
841 {
842         if (rtl_ocp_reg_failure(tp, reg))
843                 return;
844
845         RTL_W32(tp, GPHY_OCP, OCPAR_FLAG | (reg << 15) | data);
846
847         rtl_udelay_loop_wait_low(tp, &rtl_ocp_gphy_cond, 25, 10);
848 }
849
850 static u16 r8168_phy_ocp_read(struct rtl8169_private *tp, u32 reg)
851 {
852         if (rtl_ocp_reg_failure(tp, reg))
853                 return 0;
854
855         RTL_W32(tp, GPHY_OCP, reg << 15);
856
857         return rtl_udelay_loop_wait_high(tp, &rtl_ocp_gphy_cond, 25, 10) ?
858                 (RTL_R32(tp, GPHY_OCP) & 0xffff) : ~0;
859 }
860
861 static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data)
862 {
863         if (rtl_ocp_reg_failure(tp, reg))
864                 return;
865
866         RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data);
867 }
868
869 static u16 r8168_mac_ocp_read(struct rtl8169_private *tp, u32 reg)
870 {
871         if (rtl_ocp_reg_failure(tp, reg))
872                 return 0;
873
874         RTL_W32(tp, OCPDR, reg << 15);
875
876         return RTL_R32(tp, OCPDR);
877 }
878
879 #define OCP_STD_PHY_BASE        0xa400
880
881 static void r8168g_mdio_write(struct rtl8169_private *tp, int reg, int value)
882 {
883         if (reg == 0x1f) {
884                 tp->ocp_base = value ? value << 4 : OCP_STD_PHY_BASE;
885                 return;
886         }
887
888         if (tp->ocp_base != OCP_STD_PHY_BASE)
889                 reg -= 0x10;
890
891         r8168_phy_ocp_write(tp, tp->ocp_base + reg * 2, value);
892 }
893
894 static int r8168g_mdio_read(struct rtl8169_private *tp, int reg)
895 {
896         if (tp->ocp_base != OCP_STD_PHY_BASE)
897                 reg -= 0x10;
898
899         return r8168_phy_ocp_read(tp, tp->ocp_base + reg * 2);
900 }
901
902 static void mac_mcu_write(struct rtl8169_private *tp, int reg, int value)
903 {
904         if (reg == 0x1f) {
905                 tp->ocp_base = value << 4;
906                 return;
907         }
908
909         r8168_mac_ocp_write(tp, tp->ocp_base + reg, value);
910 }
911
912 static int mac_mcu_read(struct rtl8169_private *tp, int reg)
913 {
914         return r8168_mac_ocp_read(tp, tp->ocp_base + reg);
915 }
916
917 DECLARE_RTL_COND(rtl_phyar_cond)
918 {
919         return RTL_R32(tp, PHYAR) & 0x80000000;
920 }
921
922 static void r8169_mdio_write(struct rtl8169_private *tp, int reg, int value)
923 {
924         RTL_W32(tp, PHYAR, 0x80000000 | (reg & 0x1f) << 16 | (value & 0xffff));
925
926         rtl_udelay_loop_wait_low(tp, &rtl_phyar_cond, 25, 20);
927         /*
928          * According to hardware specs a 20us delay is required after write
929          * complete indication, but before sending next command.
930          */
931         udelay(20);
932 }
933
934 static int r8169_mdio_read(struct rtl8169_private *tp, int reg)
935 {
936         int value;
937
938         RTL_W32(tp, PHYAR, 0x0 | (reg & 0x1f) << 16);
939
940         value = rtl_udelay_loop_wait_high(tp, &rtl_phyar_cond, 25, 20) ?
941                 RTL_R32(tp, PHYAR) & 0xffff : ~0;
942
943         /*
944          * According to hardware specs a 20us delay is required after read
945          * complete indication, but before sending next command.
946          */
947         udelay(20);
948
949         return value;
950 }
951
952 DECLARE_RTL_COND(rtl_ocpar_cond)
953 {
954         return RTL_R32(tp, OCPAR) & OCPAR_FLAG;
955 }
956
957 static void r8168dp_1_mdio_access(struct rtl8169_private *tp, int reg, u32 data)
958 {
959         RTL_W32(tp, OCPDR, data | ((reg & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
960         RTL_W32(tp, OCPAR, OCPAR_GPHY_WRITE_CMD);
961         RTL_W32(tp, EPHY_RXER_NUM, 0);
962
963         rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 1000, 100);
964 }
965
966 static void r8168dp_1_mdio_write(struct rtl8169_private *tp, int reg, int value)
967 {
968         r8168dp_1_mdio_access(tp, reg,
969                               OCPDR_WRITE_CMD | (value & OCPDR_DATA_MASK));
970 }
971
972 static int r8168dp_1_mdio_read(struct rtl8169_private *tp, int reg)
973 {
974         r8168dp_1_mdio_access(tp, reg, OCPDR_READ_CMD);
975
976         mdelay(1);
977         RTL_W32(tp, OCPAR, OCPAR_GPHY_READ_CMD);
978         RTL_W32(tp, EPHY_RXER_NUM, 0);
979
980         return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 1000, 100) ?
981                 RTL_R32(tp, OCPDR) & OCPDR_DATA_MASK : ~0;
982 }
983
984 #define R8168DP_1_MDIO_ACCESS_BIT       0x00020000
985
986 static void r8168dp_2_mdio_start(struct rtl8169_private *tp)
987 {
988         RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
989 }
990
991 static void r8168dp_2_mdio_stop(struct rtl8169_private *tp)
992 {
993         RTL_W32(tp, 0xd0, RTL_R32(tp, 0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
994 }
995
996 static void r8168dp_2_mdio_write(struct rtl8169_private *tp, int reg, int value)
997 {
998         r8168dp_2_mdio_start(tp);
999
1000         r8169_mdio_write(tp, reg, value);
1001
1002         r8168dp_2_mdio_stop(tp);
1003 }
1004
1005 static int r8168dp_2_mdio_read(struct rtl8169_private *tp, int reg)
1006 {
1007         int value;
1008
1009         r8168dp_2_mdio_start(tp);
1010
1011         value = r8169_mdio_read(tp, reg);
1012
1013         r8168dp_2_mdio_stop(tp);
1014
1015         return value;
1016 }
1017
1018 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
1019 {
1020         tp->mdio_ops.write(tp, location, val);
1021 }
1022
1023 static int rtl_readphy(struct rtl8169_private *tp, int location)
1024 {
1025         return tp->mdio_ops.read(tp, location);
1026 }
1027
1028 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
1029 {
1030         rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
1031 }
1032
1033 static void rtl_w0w1_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
1034 {
1035         int val;
1036
1037         val = rtl_readphy(tp, reg_addr);
1038         rtl_writephy(tp, reg_addr, (val & ~m) | p);
1039 }
1040
1041 DECLARE_RTL_COND(rtl_ephyar_cond)
1042 {
1043         return RTL_R32(tp, EPHYAR) & EPHYAR_FLAG;
1044 }
1045
1046 static void rtl_ephy_write(struct rtl8169_private *tp, int reg_addr, int value)
1047 {
1048         RTL_W32(tp, EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
1049                 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1050
1051         rtl_udelay_loop_wait_low(tp, &rtl_ephyar_cond, 10, 100);
1052
1053         udelay(10);
1054 }
1055
1056 static u16 rtl_ephy_read(struct rtl8169_private *tp, int reg_addr)
1057 {
1058         RTL_W32(tp, EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1059
1060         return rtl_udelay_loop_wait_high(tp, &rtl_ephyar_cond, 10, 100) ?
1061                 RTL_R32(tp, EPHYAR) & EPHYAR_DATA_MASK : ~0;
1062 }
1063
1064 DECLARE_RTL_COND(rtl_eriar_cond)
1065 {
1066         return RTL_R32(tp, ERIAR) & ERIAR_FLAG;
1067 }
1068
1069 static void _rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1070                            u32 val, int type)
1071 {
1072         BUG_ON((addr & 3) || (mask == 0));
1073         RTL_W32(tp, ERIDR, val);
1074         RTL_W32(tp, ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1075
1076         rtl_udelay_loop_wait_low(tp, &rtl_eriar_cond, 100, 100);
1077 }
1078
1079 static void rtl_eri_write(struct rtl8169_private *tp, int addr, u32 mask,
1080                           u32 val)
1081 {
1082         _rtl_eri_write(tp, addr, mask, val, ERIAR_EXGMAC);
1083 }
1084
1085 static u32 _rtl_eri_read(struct rtl8169_private *tp, int addr, int type)
1086 {
1087         RTL_W32(tp, ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1088
1089         return rtl_udelay_loop_wait_high(tp, &rtl_eriar_cond, 100, 100) ?
1090                 RTL_R32(tp, ERIDR) : ~0;
1091 }
1092
1093 static u32 rtl_eri_read(struct rtl8169_private *tp, int addr)
1094 {
1095         return _rtl_eri_read(tp, addr, ERIAR_EXGMAC);
1096 }
1097
1098 static void rtl_w0w1_eri(struct rtl8169_private *tp, int addr, u32 mask, u32 p,
1099                          u32 m)
1100 {
1101         u32 val;
1102
1103         val = rtl_eri_read(tp, addr);
1104         rtl_eri_write(tp, addr, mask, (val & ~m) | p);
1105 }
1106
1107 static void rtl_eri_set_bits(struct rtl8169_private *tp, int addr, u32 mask,
1108                              u32 p)
1109 {
1110         rtl_w0w1_eri(tp, addr, mask, p, 0);
1111 }
1112
1113 static void rtl_eri_clear_bits(struct rtl8169_private *tp, int addr, u32 mask,
1114                                u32 m)
1115 {
1116         rtl_w0w1_eri(tp, addr, mask, 0, m);
1117 }
1118
1119 static u32 r8168dp_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1120 {
1121         RTL_W32(tp, OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1122         return rtl_udelay_loop_wait_high(tp, &rtl_ocpar_cond, 100, 20) ?
1123                 RTL_R32(tp, OCPDR) : ~0;
1124 }
1125
1126 static u32 r8168ep_ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
1127 {
1128         return _rtl_eri_read(tp, reg, ERIAR_OOB);
1129 }
1130
1131 static void r8168dp_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1132                               u32 data)
1133 {
1134         RTL_W32(tp, OCPDR, data);
1135         RTL_W32(tp, OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
1136         rtl_udelay_loop_wait_low(tp, &rtl_ocpar_cond, 100, 20);
1137 }
1138
1139 static void r8168ep_ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg,
1140                               u32 data)
1141 {
1142         _rtl_eri_write(tp, reg, ((u32)mask & 0x0f) << ERIAR_MASK_SHIFT,
1143                        data, ERIAR_OOB);
1144 }
1145
1146 static void r8168dp_oob_notify(struct rtl8169_private *tp, u8 cmd)
1147 {
1148         rtl_eri_write(tp, 0xe8, ERIAR_MASK_0001, cmd);
1149
1150         r8168dp_ocp_write(tp, 0x1, 0x30, 0x00000001);
1151 }
1152
1153 #define OOB_CMD_RESET           0x00
1154 #define OOB_CMD_DRIVER_START    0x05
1155 #define OOB_CMD_DRIVER_STOP     0x06
1156
1157 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
1158 {
1159         return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
1160 }
1161
1162 DECLARE_RTL_COND(rtl_dp_ocp_read_cond)
1163 {
1164         u16 reg;
1165
1166         reg = rtl8168_get_ocp_reg(tp);
1167
1168         return r8168dp_ocp_read(tp, 0x0f, reg) & 0x00000800;
1169 }
1170
1171 DECLARE_RTL_COND(rtl_ep_ocp_read_cond)
1172 {
1173         return r8168ep_ocp_read(tp, 0x0f, 0x124) & 0x00000001;
1174 }
1175
1176 DECLARE_RTL_COND(rtl_ocp_tx_cond)
1177 {
1178         return RTL_R8(tp, IBISR0) & 0x20;
1179 }
1180
1181 static void rtl8168ep_stop_cmac(struct rtl8169_private *tp)
1182 {
1183         RTL_W8(tp, IBCR2, RTL_R8(tp, IBCR2) & ~0x01);
1184         rtl_msleep_loop_wait_high(tp, &rtl_ocp_tx_cond, 50, 2000);
1185         RTL_W8(tp, IBISR0, RTL_R8(tp, IBISR0) | 0x20);
1186         RTL_W8(tp, IBCR0, RTL_R8(tp, IBCR0) & ~0x01);
1187 }
1188
1189 static void rtl8168dp_driver_start(struct rtl8169_private *tp)
1190 {
1191         r8168dp_oob_notify(tp, OOB_CMD_DRIVER_START);
1192         rtl_msleep_loop_wait_high(tp, &rtl_dp_ocp_read_cond, 10, 10);
1193 }
1194
1195 static void rtl8168ep_driver_start(struct rtl8169_private *tp)
1196 {
1197         r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_START);
1198         r8168ep_ocp_write(tp, 0x01, 0x30,
1199                           r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
1200         rtl_msleep_loop_wait_high(tp, &rtl_ep_ocp_read_cond, 10, 10);
1201 }
1202
1203 static void rtl8168_driver_start(struct rtl8169_private *tp)
1204 {
1205         switch (tp->mac_version) {
1206         case RTL_GIGA_MAC_VER_27:
1207         case RTL_GIGA_MAC_VER_28:
1208         case RTL_GIGA_MAC_VER_31:
1209                 rtl8168dp_driver_start(tp);
1210                 break;
1211         case RTL_GIGA_MAC_VER_49:
1212         case RTL_GIGA_MAC_VER_50:
1213         case RTL_GIGA_MAC_VER_51:
1214                 rtl8168ep_driver_start(tp);
1215                 break;
1216         default:
1217                 BUG();
1218                 break;
1219         }
1220 }
1221
1222 static void rtl8168dp_driver_stop(struct rtl8169_private *tp)
1223 {
1224         r8168dp_oob_notify(tp, OOB_CMD_DRIVER_STOP);
1225         rtl_msleep_loop_wait_low(tp, &rtl_dp_ocp_read_cond, 10, 10);
1226 }
1227
1228 static void rtl8168ep_driver_stop(struct rtl8169_private *tp)
1229 {
1230         rtl8168ep_stop_cmac(tp);
1231         r8168ep_ocp_write(tp, 0x01, 0x180, OOB_CMD_DRIVER_STOP);
1232         r8168ep_ocp_write(tp, 0x01, 0x30,
1233                           r8168ep_ocp_read(tp, 0x01, 0x30) | 0x01);
1234         rtl_msleep_loop_wait_low(tp, &rtl_ep_ocp_read_cond, 10, 10);
1235 }
1236
1237 static void rtl8168_driver_stop(struct rtl8169_private *tp)
1238 {
1239         switch (tp->mac_version) {
1240         case RTL_GIGA_MAC_VER_27:
1241         case RTL_GIGA_MAC_VER_28:
1242         case RTL_GIGA_MAC_VER_31:
1243                 rtl8168dp_driver_stop(tp);
1244                 break;
1245         case RTL_GIGA_MAC_VER_49:
1246         case RTL_GIGA_MAC_VER_50:
1247         case RTL_GIGA_MAC_VER_51:
1248                 rtl8168ep_driver_stop(tp);
1249                 break;
1250         default:
1251                 BUG();
1252                 break;
1253         }
1254 }
1255
1256 static bool r8168dp_check_dash(struct rtl8169_private *tp)
1257 {
1258         u16 reg = rtl8168_get_ocp_reg(tp);
1259
1260         return !!(r8168dp_ocp_read(tp, 0x0f, reg) & 0x00008000);
1261 }
1262
1263 static bool r8168ep_check_dash(struct rtl8169_private *tp)
1264 {
1265         return !!(r8168ep_ocp_read(tp, 0x0f, 0x128) & 0x00000001);
1266 }
1267
1268 static bool r8168_check_dash(struct rtl8169_private *tp)
1269 {
1270         switch (tp->mac_version) {
1271         case RTL_GIGA_MAC_VER_27:
1272         case RTL_GIGA_MAC_VER_28:
1273         case RTL_GIGA_MAC_VER_31:
1274                 return r8168dp_check_dash(tp);
1275         case RTL_GIGA_MAC_VER_49:
1276         case RTL_GIGA_MAC_VER_50:
1277         case RTL_GIGA_MAC_VER_51:
1278                 return r8168ep_check_dash(tp);
1279         default:
1280                 return false;
1281         }
1282 }
1283
1284 static void rtl_reset_packet_filter(struct rtl8169_private *tp)
1285 {
1286         rtl_eri_clear_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1287         rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_0001, BIT(0));
1288 }
1289
1290 DECLARE_RTL_COND(rtl_efusear_cond)
1291 {
1292         return RTL_R32(tp, EFUSEAR) & EFUSEAR_FLAG;
1293 }
1294
1295 static u8 rtl8168d_efuse_read(struct rtl8169_private *tp, int reg_addr)
1296 {
1297         RTL_W32(tp, EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1298
1299         return rtl_udelay_loop_wait_high(tp, &rtl_efusear_cond, 100, 300) ?
1300                 RTL_R32(tp, EFUSEAR) & EFUSEAR_DATA_MASK : ~0;
1301 }
1302
1303 static void rtl_ack_events(struct rtl8169_private *tp, u16 bits)
1304 {
1305         RTL_W16(tp, IntrStatus, bits);
1306 }
1307
1308 static void rtl_irq_disable(struct rtl8169_private *tp)
1309 {
1310         RTL_W16(tp, IntrMask, 0);
1311         tp->irq_enabled = 0;
1312 }
1313
1314 #define RTL_EVENT_NAPI_RX       (RxOK | RxErr)
1315 #define RTL_EVENT_NAPI_TX       (TxOK | TxErr)
1316 #define RTL_EVENT_NAPI          (RTL_EVENT_NAPI_RX | RTL_EVENT_NAPI_TX)
1317
1318 static void rtl_irq_enable(struct rtl8169_private *tp)
1319 {
1320         tp->irq_enabled = 1;
1321         RTL_W16(tp, IntrMask, tp->irq_mask);
1322 }
1323
1324 static void rtl8169_irq_mask_and_ack(struct rtl8169_private *tp)
1325 {
1326         rtl_irq_disable(tp);
1327         rtl_ack_events(tp, 0xffff);
1328         /* PCI commit */
1329         RTL_R8(tp, ChipCmd);
1330 }
1331
1332 static void rtl_link_chg_patch(struct rtl8169_private *tp)
1333 {
1334         struct net_device *dev = tp->dev;
1335         struct phy_device *phydev = tp->phydev;
1336
1337         if (!netif_running(dev))
1338                 return;
1339
1340         if (tp->mac_version == RTL_GIGA_MAC_VER_34 ||
1341             tp->mac_version == RTL_GIGA_MAC_VER_38) {
1342                 if (phydev->speed == SPEED_1000) {
1343                         rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1344                         rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1345                 } else if (phydev->speed == SPEED_100) {
1346                         rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1347                         rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1348                 } else {
1349                         rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1350                         rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
1351                 }
1352                 rtl_reset_packet_filter(tp);
1353         } else if (tp->mac_version == RTL_GIGA_MAC_VER_35 ||
1354                    tp->mac_version == RTL_GIGA_MAC_VER_36) {
1355                 if (phydev->speed == SPEED_1000) {
1356                         rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x00000011);
1357                         rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x00000005);
1358                 } else {
1359                         rtl_eri_write(tp, 0x1bc, ERIAR_MASK_1111, 0x0000001f);
1360                         rtl_eri_write(tp, 0x1dc, ERIAR_MASK_1111, 0x0000003f);
1361                 }
1362         } else if (tp->mac_version == RTL_GIGA_MAC_VER_37) {
1363                 if (phydev->speed == SPEED_10) {
1364                         rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x4d02);
1365                         rtl_eri_write(tp, 0x1dc, ERIAR_MASK_0011, 0x0060a);
1366                 } else {
1367                         rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
1368                 }
1369         }
1370 }
1371
1372 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1373
1374 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1375 {
1376         struct rtl8169_private *tp = netdev_priv(dev);
1377
1378         rtl_lock_work(tp);
1379         wol->supported = WAKE_ANY;
1380         wol->wolopts = tp->saved_wolopts;
1381         rtl_unlock_work(tp);
1382 }
1383
1384 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1385 {
1386         unsigned int i, tmp;
1387         static const struct {
1388                 u32 opt;
1389                 u16 reg;
1390                 u8  mask;
1391         } cfg[] = {
1392                 { WAKE_PHY,   Config3, LinkUp },
1393                 { WAKE_UCAST, Config5, UWF },
1394                 { WAKE_BCAST, Config5, BWF },
1395                 { WAKE_MCAST, Config5, MWF },
1396                 { WAKE_ANY,   Config5, LanWake },
1397                 { WAKE_MAGIC, Config3, MagicPacket }
1398         };
1399         u8 options;
1400
1401         rtl_unlock_config_regs(tp);
1402
1403         switch (tp->mac_version) {
1404         case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
1405         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1406                 tmp = ARRAY_SIZE(cfg) - 1;
1407                 if (wolopts & WAKE_MAGIC)
1408                         rtl_eri_set_bits(tp, 0x0dc, ERIAR_MASK_0100,
1409                                          MagicPacket_v2);
1410                 else
1411                         rtl_eri_clear_bits(tp, 0x0dc, ERIAR_MASK_0100,
1412                                            MagicPacket_v2);
1413                 break;
1414         default:
1415                 tmp = ARRAY_SIZE(cfg);
1416                 break;
1417         }
1418
1419         for (i = 0; i < tmp; i++) {
1420                 options = RTL_R8(tp, cfg[i].reg) & ~cfg[i].mask;
1421                 if (wolopts & cfg[i].opt)
1422                         options |= cfg[i].mask;
1423                 RTL_W8(tp, cfg[i].reg, options);
1424         }
1425
1426         switch (tp->mac_version) {
1427         case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_17:
1428                 options = RTL_R8(tp, Config1) & ~PMEnable;
1429                 if (wolopts)
1430                         options |= PMEnable;
1431                 RTL_W8(tp, Config1, options);
1432                 break;
1433         default:
1434                 options = RTL_R8(tp, Config2) & ~PME_SIGNAL;
1435                 if (wolopts)
1436                         options |= PME_SIGNAL;
1437                 RTL_W8(tp, Config2, options);
1438                 break;
1439         }
1440
1441         rtl_lock_config_regs(tp);
1442
1443         device_set_wakeup_enable(tp_to_dev(tp), wolopts);
1444 }
1445
1446 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1447 {
1448         struct rtl8169_private *tp = netdev_priv(dev);
1449         struct device *d = tp_to_dev(tp);
1450
1451         if (wol->wolopts & ~WAKE_ANY)
1452                 return -EINVAL;
1453
1454         pm_runtime_get_noresume(d);
1455
1456         rtl_lock_work(tp);
1457
1458         tp->saved_wolopts = wol->wolopts;
1459
1460         if (pm_runtime_active(d))
1461                 __rtl8169_set_wol(tp, tp->saved_wolopts);
1462
1463         rtl_unlock_work(tp);
1464
1465         pm_runtime_put_noidle(d);
1466
1467         return 0;
1468 }
1469
1470 static void rtl8169_get_drvinfo(struct net_device *dev,
1471                                 struct ethtool_drvinfo *info)
1472 {
1473         struct rtl8169_private *tp = netdev_priv(dev);
1474         struct rtl_fw *rtl_fw = tp->rtl_fw;
1475
1476         strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1477         strlcpy(info->bus_info, pci_name(tp->pci_dev), sizeof(info->bus_info));
1478         BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1479         if (rtl_fw)
1480                 strlcpy(info->fw_version, rtl_fw->version,
1481                         sizeof(info->fw_version));
1482 }
1483
1484 static int rtl8169_get_regs_len(struct net_device *dev)
1485 {
1486         return R8169_REGS_SIZE;
1487 }
1488
1489 static netdev_features_t rtl8169_fix_features(struct net_device *dev,
1490         netdev_features_t features)
1491 {
1492         struct rtl8169_private *tp = netdev_priv(dev);
1493
1494         if (dev->mtu > TD_MSS_MAX)
1495                 features &= ~NETIF_F_ALL_TSO;
1496
1497         if (dev->mtu > JUMBO_1K &&
1498             tp->mac_version > RTL_GIGA_MAC_VER_06)
1499                 features &= ~NETIF_F_IP_CSUM;
1500
1501         return features;
1502 }
1503
1504 static int rtl8169_set_features(struct net_device *dev,
1505                                 netdev_features_t features)
1506 {
1507         struct rtl8169_private *tp = netdev_priv(dev);
1508         u32 rx_config;
1509
1510         rtl_lock_work(tp);
1511
1512         rx_config = RTL_R32(tp, RxConfig);
1513         if (features & NETIF_F_RXALL)
1514                 rx_config |= (AcceptErr | AcceptRunt);
1515         else
1516                 rx_config &= ~(AcceptErr | AcceptRunt);
1517
1518         RTL_W32(tp, RxConfig, rx_config);
1519
1520         if (features & NETIF_F_RXCSUM)
1521                 tp->cp_cmd |= RxChkSum;
1522         else
1523                 tp->cp_cmd &= ~RxChkSum;
1524
1525         if (features & NETIF_F_HW_VLAN_CTAG_RX)
1526                 tp->cp_cmd |= RxVlan;
1527         else
1528                 tp->cp_cmd &= ~RxVlan;
1529
1530         RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1531         RTL_R16(tp, CPlusCmd);
1532
1533         rtl_unlock_work(tp);
1534
1535         return 0;
1536 }
1537
1538 static inline u32 rtl8169_tx_vlan_tag(struct sk_buff *skb)
1539 {
1540         return (skb_vlan_tag_present(skb)) ?
1541                 TxVlanTag | swab16(skb_vlan_tag_get(skb)) : 0x00;
1542 }
1543
1544 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1545 {
1546         u32 opts2 = le32_to_cpu(desc->opts2);
1547
1548         if (opts2 & RxVlanTag)
1549                 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), swab16(opts2 & 0xffff));
1550 }
1551
1552 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1553                              void *p)
1554 {
1555         struct rtl8169_private *tp = netdev_priv(dev);
1556         u32 __iomem *data = tp->mmio_addr;
1557         u32 *dw = p;
1558         int i;
1559
1560         rtl_lock_work(tp);
1561         for (i = 0; i < R8169_REGS_SIZE; i += 4)
1562                 memcpy_fromio(dw++, data++, 4);
1563         rtl_unlock_work(tp);
1564 }
1565
1566 static u32 rtl8169_get_msglevel(struct net_device *dev)
1567 {
1568         struct rtl8169_private *tp = netdev_priv(dev);
1569
1570         return tp->msg_enable;
1571 }
1572
1573 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1574 {
1575         struct rtl8169_private *tp = netdev_priv(dev);
1576
1577         tp->msg_enable = value;
1578 }
1579
1580 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1581         "tx_packets",
1582         "rx_packets",
1583         "tx_errors",
1584         "rx_errors",
1585         "rx_missed",
1586         "align_errors",
1587         "tx_single_collisions",
1588         "tx_multi_collisions",
1589         "unicast",
1590         "broadcast",
1591         "multicast",
1592         "tx_aborted",
1593         "tx_underrun",
1594 };
1595
1596 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1597 {
1598         switch (sset) {
1599         case ETH_SS_STATS:
1600                 return ARRAY_SIZE(rtl8169_gstrings);
1601         default:
1602                 return -EOPNOTSUPP;
1603         }
1604 }
1605
1606 DECLARE_RTL_COND(rtl_counters_cond)
1607 {
1608         return RTL_R32(tp, CounterAddrLow) & (CounterReset | CounterDump);
1609 }
1610
1611 static bool rtl8169_do_counters(struct rtl8169_private *tp, u32 counter_cmd)
1612 {
1613         dma_addr_t paddr = tp->counters_phys_addr;
1614         u32 cmd;
1615
1616         RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
1617         RTL_R32(tp, CounterAddrHigh);
1618         cmd = (u64)paddr & DMA_BIT_MASK(32);
1619         RTL_W32(tp, CounterAddrLow, cmd);
1620         RTL_W32(tp, CounterAddrLow, cmd | counter_cmd);
1621
1622         return rtl_udelay_loop_wait_low(tp, &rtl_counters_cond, 10, 1000);
1623 }
1624
1625 static bool rtl8169_reset_counters(struct rtl8169_private *tp)
1626 {
1627         /*
1628          * Versions prior to RTL_GIGA_MAC_VER_19 don't support resetting the
1629          * tally counters.
1630          */
1631         if (tp->mac_version < RTL_GIGA_MAC_VER_19)
1632                 return true;
1633
1634         return rtl8169_do_counters(tp, CounterReset);
1635 }
1636
1637 static bool rtl8169_update_counters(struct rtl8169_private *tp)
1638 {
1639         u8 val = RTL_R8(tp, ChipCmd);
1640
1641         /*
1642          * Some chips are unable to dump tally counters when the receiver
1643          * is disabled. If 0xff chip may be in a PCI power-save state.
1644          */
1645         if (!(val & CmdRxEnb) || val == 0xff)
1646                 return true;
1647
1648         return rtl8169_do_counters(tp, CounterDump);
1649 }
1650
1651 static bool rtl8169_init_counter_offsets(struct rtl8169_private *tp)
1652 {
1653         struct rtl8169_counters *counters = tp->counters;
1654         bool ret = false;
1655
1656         /*
1657          * rtl8169_init_counter_offsets is called from rtl_open.  On chip
1658          * versions prior to RTL_GIGA_MAC_VER_19 the tally counters are only
1659          * reset by a power cycle, while the counter values collected by the
1660          * driver are reset at every driver unload/load cycle.
1661          *
1662          * To make sure the HW values returned by @get_stats64 match the SW
1663          * values, we collect the initial values at first open(*) and use them
1664          * as offsets to normalize the values returned by @get_stats64.
1665          *
1666          * (*) We can't call rtl8169_init_counter_offsets from rtl_init_one
1667          * for the reason stated in rtl8169_update_counters; CmdRxEnb is only
1668          * set at open time by rtl_hw_start.
1669          */
1670
1671         if (tp->tc_offset.inited)
1672                 return true;
1673
1674         /* If both, reset and update fail, propagate to caller. */
1675         if (rtl8169_reset_counters(tp))
1676                 ret = true;
1677
1678         if (rtl8169_update_counters(tp))
1679                 ret = true;
1680
1681         tp->tc_offset.tx_errors = counters->tx_errors;
1682         tp->tc_offset.tx_multi_collision = counters->tx_multi_collision;
1683         tp->tc_offset.tx_aborted = counters->tx_aborted;
1684         tp->tc_offset.inited = true;
1685
1686         return ret;
1687 }
1688
1689 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1690                                       struct ethtool_stats *stats, u64 *data)
1691 {
1692         struct rtl8169_private *tp = netdev_priv(dev);
1693         struct device *d = tp_to_dev(tp);
1694         struct rtl8169_counters *counters = tp->counters;
1695
1696         ASSERT_RTNL();
1697
1698         pm_runtime_get_noresume(d);
1699
1700         if (pm_runtime_active(d))
1701                 rtl8169_update_counters(tp);
1702
1703         pm_runtime_put_noidle(d);
1704
1705         data[0] = le64_to_cpu(counters->tx_packets);
1706         data[1] = le64_to_cpu(counters->rx_packets);
1707         data[2] = le64_to_cpu(counters->tx_errors);
1708         data[3] = le32_to_cpu(counters->rx_errors);
1709         data[4] = le16_to_cpu(counters->rx_missed);
1710         data[5] = le16_to_cpu(counters->align_errors);
1711         data[6] = le32_to_cpu(counters->tx_one_collision);
1712         data[7] = le32_to_cpu(counters->tx_multi_collision);
1713         data[8] = le64_to_cpu(counters->rx_unicast);
1714         data[9] = le64_to_cpu(counters->rx_broadcast);
1715         data[10] = le32_to_cpu(counters->rx_multicast);
1716         data[11] = le16_to_cpu(counters->tx_aborted);
1717         data[12] = le16_to_cpu(counters->tx_underun);
1718 }
1719
1720 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1721 {
1722         switch(stringset) {
1723         case ETH_SS_STATS:
1724                 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1725                 break;
1726         }
1727 }
1728
1729 /*
1730  * Interrupt coalescing
1731  *
1732  * > 1 - the availability of the IntrMitigate (0xe2) register through the
1733  * >     8169, 8168 and 810x line of chipsets
1734  *
1735  * 8169, 8168, and 8136(810x) serial chipsets support it.
1736  *
1737  * > 2 - the Tx timer unit at gigabit speed
1738  *
1739  * The unit of the timer depends on both the speed and the setting of CPlusCmd
1740  * (0xe0) bit 1 and bit 0.
1741  *
1742  * For 8169
1743  * bit[1:0] \ speed        1000M           100M            10M
1744  * 0 0                     320ns           2.56us          40.96us
1745  * 0 1                     2.56us          20.48us         327.7us
1746  * 1 0                     5.12us          40.96us         655.4us
1747  * 1 1                     10.24us         81.92us         1.31ms
1748  *
1749  * For the other
1750  * bit[1:0] \ speed        1000M           100M            10M
1751  * 0 0                     5us             2.56us          40.96us
1752  * 0 1                     40us            20.48us         327.7us
1753  * 1 0                     80us            40.96us         655.4us
1754  * 1 1                     160us           81.92us         1.31ms
1755  */
1756
1757 /* rx/tx scale factors for one particular CPlusCmd[0:1] value */
1758 struct rtl_coalesce_scale {
1759         /* Rx / Tx */
1760         u32 nsecs[2];
1761 };
1762
1763 /* rx/tx scale factors for all CPlusCmd[0:1] cases */
1764 struct rtl_coalesce_info {
1765         u32 speed;
1766         struct rtl_coalesce_scale scalev[4];    /* each CPlusCmd[0:1] case */
1767 };
1768
1769 /* produce (r,t) pairs with each being in series of *1, *8, *8*2, *8*2*2 */
1770 #define rxtx_x1822(r, t) {              \
1771         {{(r),          (t)}},          \
1772         {{(r)*8,        (t)*8}},        \
1773         {{(r)*8*2,      (t)*8*2}},      \
1774         {{(r)*8*2*2,    (t)*8*2*2}},    \
1775 }
1776 static const struct rtl_coalesce_info rtl_coalesce_info_8169[] = {
1777         /* speed        delays:     rx00   tx00 */
1778         { SPEED_10,     rxtx_x1822(40960, 40960)        },
1779         { SPEED_100,    rxtx_x1822( 2560,  2560)        },
1780         { SPEED_1000,   rxtx_x1822(  320,   320)        },
1781         { 0 },
1782 };
1783
1784 static const struct rtl_coalesce_info rtl_coalesce_info_8168_8136[] = {
1785         /* speed        delays:     rx00   tx00 */
1786         { SPEED_10,     rxtx_x1822(40960, 40960)        },
1787         { SPEED_100,    rxtx_x1822( 2560,  2560)        },
1788         { SPEED_1000,   rxtx_x1822( 5000,  5000)        },
1789         { 0 },
1790 };
1791 #undef rxtx_x1822
1792
1793 /* get rx/tx scale vector corresponding to current speed */
1794 static const struct rtl_coalesce_info *rtl_coalesce_info(struct net_device *dev)
1795 {
1796         struct rtl8169_private *tp = netdev_priv(dev);
1797         struct ethtool_link_ksettings ecmd;
1798         const struct rtl_coalesce_info *ci;
1799         int rc;
1800
1801         rc = phy_ethtool_get_link_ksettings(dev, &ecmd);
1802         if (rc < 0)
1803                 return ERR_PTR(rc);
1804
1805         for (ci = tp->coalesce_info; ci->speed != 0; ci++) {
1806                 if (ecmd.base.speed == ci->speed) {
1807                         return ci;
1808                 }
1809         }
1810
1811         return ERR_PTR(-ELNRNG);
1812 }
1813
1814 static int rtl_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1815 {
1816         struct rtl8169_private *tp = netdev_priv(dev);
1817         const struct rtl_coalesce_info *ci;
1818         const struct rtl_coalesce_scale *scale;
1819         struct {
1820                 u32 *max_frames;
1821                 u32 *usecs;
1822         } coal_settings [] = {
1823                 { &ec->rx_max_coalesced_frames, &ec->rx_coalesce_usecs },
1824                 { &ec->tx_max_coalesced_frames, &ec->tx_coalesce_usecs }
1825         }, *p = coal_settings;
1826         int i;
1827         u16 w;
1828
1829         memset(ec, 0, sizeof(*ec));
1830
1831         /* get rx/tx scale corresponding to current speed and CPlusCmd[0:1] */
1832         ci = rtl_coalesce_info(dev);
1833         if (IS_ERR(ci))
1834                 return PTR_ERR(ci);
1835
1836         scale = &ci->scalev[tp->cp_cmd & INTT_MASK];
1837
1838         /* read IntrMitigate and adjust according to scale */
1839         for (w = RTL_R16(tp, IntrMitigate); w; w >>= RTL_COALESCE_SHIFT, p++) {
1840                 *p->max_frames = (w & RTL_COALESCE_MASK) << 2;
1841                 w >>= RTL_COALESCE_SHIFT;
1842                 *p->usecs = w & RTL_COALESCE_MASK;
1843         }
1844
1845         for (i = 0; i < 2; i++) {
1846                 p = coal_settings + i;
1847                 *p->usecs = (*p->usecs * scale->nsecs[i]) / 1000;
1848
1849                 /*
1850                  * ethtool_coalesce says it is illegal to set both usecs and
1851                  * max_frames to 0.
1852                  */
1853                 if (!*p->usecs && !*p->max_frames)
1854                         *p->max_frames = 1;
1855         }
1856
1857         return 0;
1858 }
1859
1860 /* choose appropriate scale factor and CPlusCmd[0:1] for (speed, nsec) */
1861 static const struct rtl_coalesce_scale *rtl_coalesce_choose_scale(
1862                         struct net_device *dev, u32 nsec, u16 *cp01)
1863 {
1864         const struct rtl_coalesce_info *ci;
1865         u16 i;
1866
1867         ci = rtl_coalesce_info(dev);
1868         if (IS_ERR(ci))
1869                 return ERR_CAST(ci);
1870
1871         for (i = 0; i < 4; i++) {
1872                 u32 rxtx_maxscale = max(ci->scalev[i].nsecs[0],
1873                                         ci->scalev[i].nsecs[1]);
1874                 if (nsec <= rxtx_maxscale * RTL_COALESCE_T_MAX) {
1875                         *cp01 = i;
1876                         return &ci->scalev[i];
1877                 }
1878         }
1879
1880         return ERR_PTR(-EINVAL);
1881 }
1882
1883 static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1884 {
1885         struct rtl8169_private *tp = netdev_priv(dev);
1886         const struct rtl_coalesce_scale *scale;
1887         struct {
1888                 u32 frames;
1889                 u32 usecs;
1890         } coal_settings [] = {
1891                 { ec->rx_max_coalesced_frames, ec->rx_coalesce_usecs },
1892                 { ec->tx_max_coalesced_frames, ec->tx_coalesce_usecs }
1893         }, *p = coal_settings;
1894         u16 w = 0, cp01;
1895         int i;
1896
1897         scale = rtl_coalesce_choose_scale(dev,
1898                         max(p[0].usecs, p[1].usecs) * 1000, &cp01);
1899         if (IS_ERR(scale))
1900                 return PTR_ERR(scale);
1901
1902         for (i = 0; i < 2; i++, p++) {
1903                 u32 units;
1904
1905                 /*
1906                  * accept max_frames=1 we returned in rtl_get_coalesce.
1907                  * accept it not only when usecs=0 because of e.g. the following scenario:
1908                  *
1909                  * - both rx_usecs=0 & rx_frames=0 in hardware (no delay on RX)
1910                  * - rtl_get_coalesce returns rx_usecs=0, rx_frames=1
1911                  * - then user does `ethtool -C eth0 rx-usecs 100`
1912                  *
1913                  * since ethtool sends to kernel whole ethtool_coalesce
1914                  * settings, if we do not handle rx_usecs=!0, rx_frames=1
1915                  * we'll reject it below in `frames % 4 != 0`.
1916                  */
1917                 if (p->frames == 1) {
1918                         p->frames = 0;
1919                 }
1920
1921                 units = p->usecs * 1000 / scale->nsecs[i];
1922                 if (p->frames > RTL_COALESCE_FRAME_MAX || p->frames % 4)
1923                         return -EINVAL;
1924
1925                 w <<= RTL_COALESCE_SHIFT;
1926                 w |= units;
1927                 w <<= RTL_COALESCE_SHIFT;
1928                 w |= p->frames >> 2;
1929         }
1930
1931         rtl_lock_work(tp);
1932
1933         RTL_W16(tp, IntrMitigate, swab16(w));
1934
1935         tp->cp_cmd = (tp->cp_cmd & ~INTT_MASK) | cp01;
1936         RTL_W16(tp, CPlusCmd, tp->cp_cmd);
1937         RTL_R16(tp, CPlusCmd);
1938
1939         rtl_unlock_work(tp);
1940
1941         return 0;
1942 }
1943
1944 static int rtl_get_eee_supp(struct rtl8169_private *tp)
1945 {
1946         struct phy_device *phydev = tp->phydev;
1947         int ret;
1948
1949         switch (tp->mac_version) {
1950         case RTL_GIGA_MAC_VER_34:
1951         case RTL_GIGA_MAC_VER_35:
1952         case RTL_GIGA_MAC_VER_36:
1953         case RTL_GIGA_MAC_VER_38:
1954                 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1955                 break;
1956         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1957                 phy_write(phydev, 0x1f, 0x0a5c);
1958                 ret = phy_read(phydev, 0x12);
1959                 phy_write(phydev, 0x1f, 0x0000);
1960                 break;
1961         default:
1962                 ret = -EPROTONOSUPPORT;
1963                 break;
1964         }
1965
1966         return ret;
1967 }
1968
1969 static int rtl_get_eee_lpadv(struct rtl8169_private *tp)
1970 {
1971         struct phy_device *phydev = tp->phydev;
1972         int ret;
1973
1974         switch (tp->mac_version) {
1975         case RTL_GIGA_MAC_VER_34:
1976         case RTL_GIGA_MAC_VER_35:
1977         case RTL_GIGA_MAC_VER_36:
1978         case RTL_GIGA_MAC_VER_38:
1979                 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1980                 break;
1981         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
1982                 phy_write(phydev, 0x1f, 0x0a5d);
1983                 ret = phy_read(phydev, 0x11);
1984                 phy_write(phydev, 0x1f, 0x0000);
1985                 break;
1986         default:
1987                 ret = -EPROTONOSUPPORT;
1988                 break;
1989         }
1990
1991         return ret;
1992 }
1993
1994 static int rtl_get_eee_adv(struct rtl8169_private *tp)
1995 {
1996         struct phy_device *phydev = tp->phydev;
1997         int ret;
1998
1999         switch (tp->mac_version) {
2000         case RTL_GIGA_MAC_VER_34:
2001         case RTL_GIGA_MAC_VER_35:
2002         case RTL_GIGA_MAC_VER_36:
2003         case RTL_GIGA_MAC_VER_38:
2004                 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
2005                 break;
2006         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
2007                 phy_write(phydev, 0x1f, 0x0a5d);
2008                 ret = phy_read(phydev, 0x10);
2009                 phy_write(phydev, 0x1f, 0x0000);
2010                 break;
2011         default:
2012                 ret = -EPROTONOSUPPORT;
2013                 break;
2014         }
2015
2016         return ret;
2017 }
2018
2019 static int rtl_set_eee_adv(struct rtl8169_private *tp, int val)
2020 {
2021         struct phy_device *phydev = tp->phydev;
2022         int ret = 0;
2023
2024         switch (tp->mac_version) {
2025         case RTL_GIGA_MAC_VER_34:
2026         case RTL_GIGA_MAC_VER_35:
2027         case RTL_GIGA_MAC_VER_36:
2028         case RTL_GIGA_MAC_VER_38:
2029                 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
2030                 break;
2031         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
2032                 phy_write(phydev, 0x1f, 0x0a5d);
2033                 phy_write(phydev, 0x10, val);
2034                 phy_write(phydev, 0x1f, 0x0000);
2035                 break;
2036         default:
2037                 ret = -EPROTONOSUPPORT;
2038                 break;
2039         }
2040
2041         return ret;
2042 }
2043
2044 static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
2045 {
2046         struct rtl8169_private *tp = netdev_priv(dev);
2047         struct device *d = tp_to_dev(tp);
2048         int ret;
2049
2050         pm_runtime_get_noresume(d);
2051
2052         if (!pm_runtime_active(d)) {
2053                 ret = -EOPNOTSUPP;
2054                 goto out;
2055         }
2056
2057         /* Get Supported EEE */
2058         ret = rtl_get_eee_supp(tp);
2059         if (ret < 0)
2060                 goto out;
2061         data->supported = mmd_eee_cap_to_ethtool_sup_t(ret);
2062
2063         /* Get advertisement EEE */
2064         ret = rtl_get_eee_adv(tp);
2065         if (ret < 0)
2066                 goto out;
2067         data->advertised = mmd_eee_adv_to_ethtool_adv_t(ret);
2068         data->eee_enabled = !!data->advertised;
2069
2070         /* Get LP advertisement EEE */
2071         ret = rtl_get_eee_lpadv(tp);
2072         if (ret < 0)
2073                 goto out;
2074         data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(ret);
2075         data->eee_active = !!(data->advertised & data->lp_advertised);
2076 out:
2077         pm_runtime_put_noidle(d);
2078         return ret < 0 ? ret : 0;
2079 }
2080
2081 static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
2082 {
2083         struct rtl8169_private *tp = netdev_priv(dev);
2084         struct device *d = tp_to_dev(tp);
2085         int old_adv, adv = 0, cap, ret;
2086
2087         pm_runtime_get_noresume(d);
2088
2089         if (!dev->phydev || !pm_runtime_active(d)) {
2090                 ret = -EOPNOTSUPP;
2091                 goto out;
2092         }
2093
2094         if (dev->phydev->autoneg == AUTONEG_DISABLE ||
2095             dev->phydev->duplex != DUPLEX_FULL) {
2096                 ret = -EPROTONOSUPPORT;
2097                 goto out;
2098         }
2099
2100         /* Get Supported EEE */
2101         ret = rtl_get_eee_supp(tp);
2102         if (ret < 0)
2103                 goto out;
2104         cap = ret;
2105
2106         ret = rtl_get_eee_adv(tp);
2107         if (ret < 0)
2108                 goto out;
2109         old_adv = ret;
2110
2111         if (data->eee_enabled) {
2112                 adv = !data->advertised ? cap :
2113                       ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
2114                 /* Mask prohibited EEE modes */
2115                 adv &= ~dev->phydev->eee_broken_modes;
2116         }
2117
2118         if (old_adv != adv) {
2119                 ret = rtl_set_eee_adv(tp, adv);
2120                 if (ret < 0)
2121                         goto out;
2122
2123                 /* Restart autonegotiation so the new modes get sent to the
2124                  * link partner.
2125                  */
2126                 ret = phy_restart_aneg(dev->phydev);
2127         }
2128
2129 out:
2130         pm_runtime_put_noidle(d);
2131         return ret < 0 ? ret : 0;
2132 }
2133
2134 static const struct ethtool_ops rtl8169_ethtool_ops = {
2135         .get_drvinfo            = rtl8169_get_drvinfo,
2136         .get_regs_len           = rtl8169_get_regs_len,
2137         .get_link               = ethtool_op_get_link,
2138         .get_coalesce           = rtl_get_coalesce,
2139         .set_coalesce           = rtl_set_coalesce,
2140         .get_msglevel           = rtl8169_get_msglevel,
2141         .set_msglevel           = rtl8169_set_msglevel,
2142         .get_regs               = rtl8169_get_regs,
2143         .get_wol                = rtl8169_get_wol,
2144         .set_wol                = rtl8169_set_wol,
2145         .get_strings            = rtl8169_get_strings,
2146         .get_sset_count         = rtl8169_get_sset_count,
2147         .get_ethtool_stats      = rtl8169_get_ethtool_stats,
2148         .get_ts_info            = ethtool_op_get_ts_info,
2149         .nway_reset             = phy_ethtool_nway_reset,
2150         .get_eee                = rtl8169_get_eee,
2151         .set_eee                = rtl8169_set_eee,
2152         .get_link_ksettings     = phy_ethtool_get_link_ksettings,
2153         .set_link_ksettings     = phy_ethtool_set_link_ksettings,
2154 };
2155
2156 static void rtl_enable_eee(struct rtl8169_private *tp)
2157 {
2158         int supported = rtl_get_eee_supp(tp);
2159
2160         if (supported > 0)
2161                 rtl_set_eee_adv(tp, supported);
2162 }
2163
2164 static void rtl8169_get_mac_version(struct rtl8169_private *tp)
2165 {
2166         /*
2167          * The driver currently handles the 8168Bf and the 8168Be identically
2168          * but they can be identified more specifically through the test below
2169          * if needed:
2170          *
2171          * (RTL_R32(tp, TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
2172          *
2173          * Same thing for the 8101Eb and the 8101Ec:
2174          *
2175          * (RTL_R32(tp, TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
2176          */
2177         static const struct rtl_mac_info {
2178                 u16 mask;
2179                 u16 val;
2180                 u16 mac_version;
2181         } mac_info[] = {
2182                 /* 8168EP family. */
2183                 { 0x7cf, 0x502, RTL_GIGA_MAC_VER_51 },
2184                 { 0x7cf, 0x501, RTL_GIGA_MAC_VER_50 },
2185                 { 0x7cf, 0x500, RTL_GIGA_MAC_VER_49 },
2186
2187                 /* 8168H family. */
2188                 { 0x7cf, 0x541, RTL_GIGA_MAC_VER_46 },
2189                 { 0x7cf, 0x540, RTL_GIGA_MAC_VER_45 },
2190
2191                 /* 8168G family. */
2192                 { 0x7cf, 0x5c8, RTL_GIGA_MAC_VER_44 },
2193                 { 0x7cf, 0x509, RTL_GIGA_MAC_VER_42 },
2194                 { 0x7cf, 0x4c1, RTL_GIGA_MAC_VER_41 },
2195                 { 0x7cf, 0x4c0, RTL_GIGA_MAC_VER_40 },
2196
2197                 /* 8168F family. */
2198                 { 0x7c8, 0x488, RTL_GIGA_MAC_VER_38 },
2199                 { 0x7cf, 0x481, RTL_GIGA_MAC_VER_36 },
2200                 { 0x7cf, 0x480, RTL_GIGA_MAC_VER_35 },
2201
2202                 /* 8168E family. */
2203                 { 0x7c8, 0x2c8, RTL_GIGA_MAC_VER_34 },
2204                 { 0x7cf, 0x2c1, RTL_GIGA_MAC_VER_32 },
2205                 { 0x7c8, 0x2c0, RTL_GIGA_MAC_VER_33 },
2206
2207                 /* 8168D family. */
2208                 { 0x7cf, 0x281, RTL_GIGA_MAC_VER_25 },
2209                 { 0x7c8, 0x280, RTL_GIGA_MAC_VER_26 },
2210
2211                 /* 8168DP family. */
2212                 { 0x7cf, 0x288, RTL_GIGA_MAC_VER_27 },
2213                 { 0x7cf, 0x28a, RTL_GIGA_MAC_VER_28 },
2214                 { 0x7cf, 0x28b, RTL_GIGA_MAC_VER_31 },
2215
2216                 /* 8168C family. */
2217                 { 0x7cf, 0x3c9, RTL_GIGA_MAC_VER_23 },
2218                 { 0x7cf, 0x3c8, RTL_GIGA_MAC_VER_18 },
2219                 { 0x7c8, 0x3c8, RTL_GIGA_MAC_VER_24 },
2220                 { 0x7cf, 0x3c0, RTL_GIGA_MAC_VER_19 },
2221                 { 0x7cf, 0x3c2, RTL_GIGA_MAC_VER_20 },
2222                 { 0x7cf, 0x3c3, RTL_GIGA_MAC_VER_21 },
2223                 { 0x7c8, 0x3c0, RTL_GIGA_MAC_VER_22 },
2224
2225                 /* 8168B family. */
2226                 { 0x7cf, 0x380, RTL_GIGA_MAC_VER_12 },
2227                 { 0x7c8, 0x380, RTL_GIGA_MAC_VER_17 },
2228                 { 0x7c8, 0x300, RTL_GIGA_MAC_VER_11 },
2229
2230                 /* 8101 family. */
2231                 { 0x7c8, 0x448, RTL_GIGA_MAC_VER_39 },
2232                 { 0x7c8, 0x440, RTL_GIGA_MAC_VER_37 },
2233                 { 0x7cf, 0x409, RTL_GIGA_MAC_VER_29 },
2234                 { 0x7c8, 0x408, RTL_GIGA_MAC_VER_30 },
2235                 { 0x7cf, 0x349, RTL_GIGA_MAC_VER_08 },
2236                 { 0x7cf, 0x249, RTL_GIGA_MAC_VER_08 },
2237                 { 0x7cf, 0x348, RTL_GIGA_MAC_VER_07 },
2238                 { 0x7cf, 0x248, RTL_GIGA_MAC_VER_07 },
2239                 { 0x7cf, 0x340, RTL_GIGA_MAC_VER_13 },
2240                 { 0x7cf, 0x343, RTL_GIGA_MAC_VER_10 },
2241                 { 0x7cf, 0x342, RTL_GIGA_MAC_VER_16 },
2242                 { 0x7c8, 0x348, RTL_GIGA_MAC_VER_09 },
2243                 { 0x7c8, 0x248, RTL_GIGA_MAC_VER_09 },
2244                 { 0x7c8, 0x340, RTL_GIGA_MAC_VER_16 },
2245                 /* FIXME: where did these entries come from ? -- FR */
2246                 { 0xfc8, 0x388, RTL_GIGA_MAC_VER_15 },
2247                 { 0xfc8, 0x308, RTL_GIGA_MAC_VER_14 },
2248
2249                 /* 8110 family. */
2250                 { 0xfc8, 0x980, RTL_GIGA_MAC_VER_06 },
2251                 { 0xfc8, 0x180, RTL_GIGA_MAC_VER_05 },
2252                 { 0xfc8, 0x100, RTL_GIGA_MAC_VER_04 },
2253                 { 0xfc8, 0x040, RTL_GIGA_MAC_VER_03 },
2254                 { 0xfc8, 0x008, RTL_GIGA_MAC_VER_02 },
2255                 { 0xfc8, 0x000, RTL_GIGA_MAC_VER_01 },
2256
2257                 /* Catch-all */
2258                 { 0x000, 0x000, RTL_GIGA_MAC_NONE   }
2259         };
2260         const struct rtl_mac_info *p = mac_info;
2261         u16 reg = RTL_R32(tp, TxConfig) >> 20;
2262
2263         while ((reg & p->mask) != p->val)
2264                 p++;
2265         tp->mac_version = p->mac_version;
2266
2267         if (tp->mac_version == RTL_GIGA_MAC_NONE) {
2268                 dev_err(tp_to_dev(tp), "unknown chip XID %03x\n", reg & 0xfcf);
2269         } else if (!tp->supports_gmii) {
2270                 if (tp->mac_version == RTL_GIGA_MAC_VER_42)
2271                         tp->mac_version = RTL_GIGA_MAC_VER_43;
2272                 else if (tp->mac_version == RTL_GIGA_MAC_VER_45)
2273                         tp->mac_version = RTL_GIGA_MAC_VER_47;
2274                 else if (tp->mac_version == RTL_GIGA_MAC_VER_46)
2275                         tp->mac_version = RTL_GIGA_MAC_VER_48;
2276         }
2277 }
2278
2279 struct phy_reg {
2280         u16 reg;
2281         u16 val;
2282 };
2283
2284 static void __rtl_writephy_batch(struct rtl8169_private *tp,
2285                                  const struct phy_reg *regs, int len)
2286 {
2287         while (len-- > 0) {
2288                 rtl_writephy(tp, regs->reg, regs->val);
2289                 regs++;
2290         }
2291 }
2292
2293 #define rtl_writephy_batch(tp, a) __rtl_writephy_batch(tp, a, ARRAY_SIZE(a))
2294
2295 #define PHY_READ                0x00000000
2296 #define PHY_DATA_OR             0x10000000
2297 #define PHY_DATA_AND            0x20000000
2298 #define PHY_BJMPN               0x30000000
2299 #define PHY_MDIO_CHG            0x40000000
2300 #define PHY_CLEAR_READCOUNT     0x70000000
2301 #define PHY_WRITE               0x80000000
2302 #define PHY_READCOUNT_EQ_SKIP   0x90000000
2303 #define PHY_COMP_EQ_SKIPN       0xa0000000
2304 #define PHY_COMP_NEQ_SKIPN      0xb0000000
2305 #define PHY_WRITE_PREVIOUS      0xc0000000
2306 #define PHY_SKIPN               0xd0000000
2307 #define PHY_DELAY_MS            0xe0000000
2308
2309 struct fw_info {
2310         u32     magic;
2311         char    version[RTL_VER_SIZE];
2312         __le32  fw_start;
2313         __le32  fw_len;
2314         u8      chksum;
2315 } __packed;
2316
2317 #define FW_OPCODE_SIZE  sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
2318
2319 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2320 {
2321         const struct firmware *fw = rtl_fw->fw;
2322         struct fw_info *fw_info = (struct fw_info *)fw->data;
2323         struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2324         char *version = rtl_fw->version;
2325         bool rc = false;
2326
2327         if (fw->size < FW_OPCODE_SIZE)
2328                 goto out;
2329
2330         if (!fw_info->magic) {
2331                 size_t i, size, start;
2332                 u8 checksum = 0;
2333
2334                 if (fw->size < sizeof(*fw_info))
2335                         goto out;
2336
2337                 for (i = 0; i < fw->size; i++)
2338                         checksum += fw->data[i];
2339                 if (checksum != 0)
2340                         goto out;
2341
2342                 start = le32_to_cpu(fw_info->fw_start);
2343                 if (start > fw->size)
2344                         goto out;
2345
2346                 size = le32_to_cpu(fw_info->fw_len);
2347                 if (size > (fw->size - start) / FW_OPCODE_SIZE)
2348                         goto out;
2349
2350                 memcpy(version, fw_info->version, RTL_VER_SIZE);
2351
2352                 pa->code = (__le32 *)(fw->data + start);
2353                 pa->size = size;
2354         } else {
2355                 if (fw->size % FW_OPCODE_SIZE)
2356                         goto out;
2357
2358                 strlcpy(version, tp->fw_name, RTL_VER_SIZE);
2359
2360                 pa->code = (__le32 *)fw->data;
2361                 pa->size = fw->size / FW_OPCODE_SIZE;
2362         }
2363         version[RTL_VER_SIZE - 1] = 0;
2364
2365         rc = true;
2366 out:
2367         return rc;
2368 }
2369
2370 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
2371                            struct rtl_fw_phy_action *pa)
2372 {
2373         bool rc = false;
2374         size_t index;
2375
2376         for (index = 0; index < pa->size; index++) {
2377                 u32 action = le32_to_cpu(pa->code[index]);
2378                 u32 regno = (action & 0x0fff0000) >> 16;
2379
2380                 switch(action & 0xf0000000) {
2381                 case PHY_READ:
2382                 case PHY_DATA_OR:
2383                 case PHY_DATA_AND:
2384                 case PHY_MDIO_CHG:
2385                 case PHY_CLEAR_READCOUNT:
2386                 case PHY_WRITE:
2387                 case PHY_WRITE_PREVIOUS:
2388                 case PHY_DELAY_MS:
2389                         break;
2390
2391                 case PHY_BJMPN:
2392                         if (regno > index) {
2393                                 netif_err(tp, ifup, tp->dev,
2394                                           "Out of range of firmware\n");
2395                                 goto out;
2396                         }
2397                         break;
2398                 case PHY_READCOUNT_EQ_SKIP:
2399                         if (index + 2 >= pa->size) {
2400                                 netif_err(tp, ifup, tp->dev,
2401                                           "Out of range of firmware\n");
2402                                 goto out;
2403                         }
2404                         break;
2405                 case PHY_COMP_EQ_SKIPN:
2406                 case PHY_COMP_NEQ_SKIPN:
2407                 case PHY_SKIPN:
2408                         if (index + 1 + regno >= pa->size) {
2409                                 netif_err(tp, ifup, tp->dev,
2410                                           "Out of range of firmware\n");
2411                                 goto out;
2412                         }
2413                         break;
2414
2415                 default:
2416                         netif_err(tp, ifup, tp->dev,
2417                                   "Invalid action 0x%08x\n", action);
2418                         goto out;
2419                 }
2420         }
2421         rc = true;
2422 out:
2423         return rc;
2424 }
2425
2426 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2427 {
2428         struct net_device *dev = tp->dev;
2429         int rc = -EINVAL;
2430
2431         if (!rtl_fw_format_ok(tp, rtl_fw)) {
2432                 netif_err(tp, ifup, dev, "invalid firmware\n");
2433                 goto out;
2434         }
2435
2436         if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
2437                 rc = 0;
2438 out:
2439         return rc;
2440 }
2441
2442 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
2443 {
2444         struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2445         struct mdio_ops org, *ops = &tp->mdio_ops;
2446         u32 predata, count;
2447         size_t index;
2448
2449         predata = count = 0;
2450         org.write = ops->write;
2451         org.read = ops->read;
2452
2453         for (index = 0; index < pa->size; ) {
2454                 u32 action = le32_to_cpu(pa->code[index]);
2455                 u32 data = action & 0x0000ffff;
2456                 u32 regno = (action & 0x0fff0000) >> 16;
2457
2458                 if (!action)
2459                         break;
2460
2461                 switch(action & 0xf0000000) {
2462                 case PHY_READ:
2463                         predata = rtl_readphy(tp, regno);
2464                         count++;
2465                         index++;
2466                         break;
2467                 case PHY_DATA_OR:
2468                         predata |= data;
2469                         index++;
2470                         break;
2471                 case PHY_DATA_AND:
2472                         predata &= data;
2473                         index++;
2474                         break;
2475                 case PHY_BJMPN:
2476                         index -= regno;
2477                         break;
2478                 case PHY_MDIO_CHG:
2479                         if (data == 0) {
2480                                 ops->write = org.write;
2481                                 ops->read = org.read;
2482                         } else if (data == 1) {
2483                                 ops->write = mac_mcu_write;
2484                                 ops->read = mac_mcu_read;
2485                         }
2486
2487                         index++;
2488                         break;
2489                 case PHY_CLEAR_READCOUNT:
2490                         count = 0;
2491                         index++;
2492                         break;
2493                 case PHY_WRITE:
2494                         rtl_writephy(tp, regno, data);
2495                         index++;
2496                         break;
2497                 case PHY_READCOUNT_EQ_SKIP:
2498                         index += (count == data) ? 2 : 1;
2499                         break;
2500                 case PHY_COMP_EQ_SKIPN:
2501                         if (predata == data)
2502                                 index += regno;
2503                         index++;
2504                         break;
2505                 case PHY_COMP_NEQ_SKIPN:
2506                         if (predata != data)
2507                                 index += regno;
2508                         index++;
2509                         break;
2510                 case PHY_WRITE_PREVIOUS:
2511                         rtl_writephy(tp, regno, predata);
2512                         index++;
2513                         break;
2514                 case PHY_SKIPN:
2515                         index += regno + 1;
2516                         break;
2517                 case PHY_DELAY_MS:
2518                         mdelay(data);
2519                         index++;
2520                         break;
2521
2522                 default:
2523                         BUG();
2524                 }
2525         }
2526
2527         ops->write = org.write;
2528         ops->read = org.read;
2529 }
2530
2531 static void rtl_release_firmware(struct rtl8169_private *tp)
2532 {
2533         if (tp->rtl_fw) {
2534                 release_firmware(tp->rtl_fw->fw);
2535                 kfree(tp->rtl_fw);
2536                 tp->rtl_fw = NULL;
2537         }
2538 }
2539
2540 static void rtl_apply_firmware(struct rtl8169_private *tp)
2541 {
2542         /* TODO: release firmware once rtl_phy_write_fw signals failures. */
2543         if (tp->rtl_fw)
2544                 rtl_phy_write_fw(tp, tp->rtl_fw);
2545 }
2546
2547 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2548 {
2549         if (rtl_readphy(tp, reg) != val)
2550                 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2551         else
2552                 rtl_apply_firmware(tp);
2553 }
2554
2555 static void rtl8168_config_eee_mac(struct rtl8169_private *tp)
2556 {
2557         /* Adjust EEE LED frequency */
2558         if (tp->mac_version != RTL_GIGA_MAC_VER_38)
2559                 RTL_W8(tp, EEE_LED, RTL_R8(tp, EEE_LED) & ~0x07);
2560
2561         rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_1111, 0x0003);
2562 }
2563
2564 static void rtl8168f_config_eee_phy(struct rtl8169_private *tp)
2565 {
2566         struct phy_device *phydev = tp->phydev;
2567
2568         phy_write(phydev, 0x1f, 0x0007);
2569         phy_write(phydev, 0x1e, 0x0020);
2570         phy_set_bits(phydev, 0x15, BIT(8));
2571
2572         phy_write(phydev, 0x1f, 0x0005);
2573         phy_write(phydev, 0x05, 0x8b85);
2574         phy_set_bits(phydev, 0x06, BIT(13));
2575
2576         phy_write(phydev, 0x1f, 0x0000);
2577 }
2578
2579 static void rtl8168g_config_eee_phy(struct rtl8169_private *tp)
2580 {
2581         phy_write(tp->phydev, 0x1f, 0x0a43);
2582         phy_set_bits(tp->phydev, 0x11, BIT(4));
2583         phy_write(tp->phydev, 0x1f, 0x0000);
2584 }
2585
2586 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
2587 {
2588         static const struct phy_reg phy_reg_init[] = {
2589                 { 0x1f, 0x0001 },
2590                 { 0x06, 0x006e },
2591                 { 0x08, 0x0708 },
2592                 { 0x15, 0x4000 },
2593                 { 0x18, 0x65c7 },
2594
2595                 { 0x1f, 0x0001 },
2596                 { 0x03, 0x00a1 },
2597                 { 0x02, 0x0008 },
2598                 { 0x01, 0x0120 },
2599                 { 0x00, 0x1000 },
2600                 { 0x04, 0x0800 },
2601                 { 0x04, 0x0000 },
2602
2603                 { 0x03, 0xff41 },
2604                 { 0x02, 0xdf60 },
2605                 { 0x01, 0x0140 },
2606                 { 0x00, 0x0077 },
2607                 { 0x04, 0x7800 },
2608                 { 0x04, 0x7000 },
2609
2610                 { 0x03, 0x802f },
2611                 { 0x02, 0x4f02 },
2612                 { 0x01, 0x0409 },
2613                 { 0x00, 0xf0f9 },
2614                 { 0x04, 0x9800 },
2615                 { 0x04, 0x9000 },
2616
2617                 { 0x03, 0xdf01 },
2618                 { 0x02, 0xdf20 },
2619                 { 0x01, 0xff95 },
2620                 { 0x00, 0xba00 },
2621                 { 0x04, 0xa800 },
2622                 { 0x04, 0xa000 },
2623
2624                 { 0x03, 0xff41 },
2625                 { 0x02, 0xdf20 },
2626                 { 0x01, 0x0140 },
2627                 { 0x00, 0x00bb },
2628                 { 0x04, 0xb800 },
2629                 { 0x04, 0xb000 },
2630
2631                 { 0x03, 0xdf41 },
2632                 { 0x02, 0xdc60 },
2633                 { 0x01, 0x6340 },
2634                 { 0x00, 0x007d },
2635                 { 0x04, 0xd800 },
2636                 { 0x04, 0xd000 },
2637
2638                 { 0x03, 0xdf01 },
2639                 { 0x02, 0xdf20 },
2640                 { 0x01, 0x100a },
2641                 { 0x00, 0xa0ff },
2642                 { 0x04, 0xf800 },
2643                 { 0x04, 0xf000 },
2644
2645                 { 0x1f, 0x0000 },
2646                 { 0x0b, 0x0000 },
2647                 { 0x00, 0x9200 }
2648         };
2649
2650         rtl_writephy_batch(tp, phy_reg_init);
2651 }
2652
2653 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2654 {
2655         static const struct phy_reg phy_reg_init[] = {
2656                 { 0x1f, 0x0002 },
2657                 { 0x01, 0x90d0 },
2658                 { 0x1f, 0x0000 }
2659         };
2660
2661         rtl_writephy_batch(tp, phy_reg_init);
2662 }
2663
2664 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2665 {
2666         struct pci_dev *pdev = tp->pci_dev;
2667
2668         if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2669             (pdev->subsystem_device != 0xe000))
2670                 return;
2671
2672         rtl_writephy(tp, 0x1f, 0x0001);
2673         rtl_writephy(tp, 0x10, 0xf01b);
2674         rtl_writephy(tp, 0x1f, 0x0000);
2675 }
2676
2677 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2678 {
2679         static const struct phy_reg phy_reg_init[] = {
2680                 { 0x1f, 0x0001 },
2681                 { 0x04, 0x0000 },
2682                 { 0x03, 0x00a1 },
2683                 { 0x02, 0x0008 },
2684                 { 0x01, 0x0120 },
2685                 { 0x00, 0x1000 },
2686                 { 0x04, 0x0800 },
2687                 { 0x04, 0x9000 },
2688                 { 0x03, 0x802f },
2689                 { 0x02, 0x4f02 },
2690                 { 0x01, 0x0409 },
2691                 { 0x00, 0xf099 },
2692                 { 0x04, 0x9800 },
2693                 { 0x04, 0xa000 },
2694                 { 0x03, 0xdf01 },
2695                 { 0x02, 0xdf20 },
2696                 { 0x01, 0xff95 },
2697                 { 0x00, 0xba00 },
2698                 { 0x04, 0xa800 },
2699                 { 0x04, 0xf000 },
2700                 { 0x03, 0xdf01 },
2701                 { 0x02, 0xdf20 },
2702                 { 0x01, 0x101a },
2703                 { 0x00, 0xa0ff },
2704                 { 0x04, 0xf800 },
2705                 { 0x04, 0x0000 },
2706                 { 0x1f, 0x0000 },
2707
2708                 { 0x1f, 0x0001 },
2709                 { 0x10, 0xf41b },
2710                 { 0x14, 0xfb54 },
2711                 { 0x18, 0xf5c7 },
2712                 { 0x1f, 0x0000 },
2713
2714                 { 0x1f, 0x0001 },
2715                 { 0x17, 0x0cc0 },
2716                 { 0x1f, 0x0000 }
2717         };
2718
2719         rtl_writephy_batch(tp, phy_reg_init);
2720
2721         rtl8169scd_hw_phy_config_quirk(tp);
2722 }
2723
2724 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2725 {
2726         static const struct phy_reg phy_reg_init[] = {
2727                 { 0x1f, 0x0001 },
2728                 { 0x04, 0x0000 },
2729                 { 0x03, 0x00a1 },
2730                 { 0x02, 0x0008 },
2731                 { 0x01, 0x0120 },
2732                 { 0x00, 0x1000 },
2733                 { 0x04, 0x0800 },
2734                 { 0x04, 0x9000 },
2735                 { 0x03, 0x802f },
2736                 { 0x02, 0x4f02 },
2737                 { 0x01, 0x0409 },
2738                 { 0x00, 0xf099 },
2739                 { 0x04, 0x9800 },
2740                 { 0x04, 0xa000 },
2741                 { 0x03, 0xdf01 },
2742                 { 0x02, 0xdf20 },
2743                 { 0x01, 0xff95 },
2744                 { 0x00, 0xba00 },
2745                 { 0x04, 0xa800 },
2746                 { 0x04, 0xf000 },
2747                 { 0x03, 0xdf01 },
2748                 { 0x02, 0xdf20 },
2749                 { 0x01, 0x101a },
2750                 { 0x00, 0xa0ff },
2751                 { 0x04, 0xf800 },
2752                 { 0x04, 0x0000 },
2753                 { 0x1f, 0x0000 },
2754
2755                 { 0x1f, 0x0001 },
2756                 { 0x0b, 0x8480 },
2757                 { 0x1f, 0x0000 },
2758
2759                 { 0x1f, 0x0001 },
2760                 { 0x18, 0x67c7 },
2761                 { 0x04, 0x2000 },
2762                 { 0x03, 0x002f },
2763                 { 0x02, 0x4360 },
2764                 { 0x01, 0x0109 },
2765                 { 0x00, 0x3022 },
2766                 { 0x04, 0x2800 },
2767                 { 0x1f, 0x0000 },
2768
2769                 { 0x1f, 0x0001 },
2770                 { 0x17, 0x0cc0 },
2771                 { 0x1f, 0x0000 }
2772         };
2773
2774         rtl_writephy_batch(tp, phy_reg_init);
2775 }
2776
2777 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2778 {
2779         static const struct phy_reg phy_reg_init[] = {
2780                 { 0x10, 0xf41b },
2781                 { 0x1f, 0x0000 }
2782         };
2783
2784         rtl_writephy(tp, 0x1f, 0x0001);
2785         rtl_patchphy(tp, 0x16, 1 << 0);
2786
2787         rtl_writephy_batch(tp, phy_reg_init);
2788 }
2789
2790 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2791 {
2792         static const struct phy_reg phy_reg_init[] = {
2793                 { 0x1f, 0x0001 },
2794                 { 0x10, 0xf41b },
2795                 { 0x1f, 0x0000 }
2796         };
2797
2798         rtl_writephy_batch(tp, phy_reg_init);
2799 }
2800
2801 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2802 {
2803         static const struct phy_reg phy_reg_init[] = {
2804                 { 0x1f, 0x0000 },
2805                 { 0x1d, 0x0f00 },
2806                 { 0x1f, 0x0002 },
2807                 { 0x0c, 0x1ec8 },
2808                 { 0x1f, 0x0000 }
2809         };
2810
2811         rtl_writephy_batch(tp, phy_reg_init);
2812 }
2813
2814 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2815 {
2816         static const struct phy_reg phy_reg_init[] = {
2817                 { 0x1f, 0x0001 },
2818                 { 0x1d, 0x3d98 },
2819                 { 0x1f, 0x0000 }
2820         };
2821
2822         rtl_writephy(tp, 0x1f, 0x0000);
2823         rtl_patchphy(tp, 0x14, 1 << 5);
2824         rtl_patchphy(tp, 0x0d, 1 << 5);
2825
2826         rtl_writephy_batch(tp, phy_reg_init);
2827 }
2828
2829 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2830 {
2831         static const struct phy_reg phy_reg_init[] = {
2832                 { 0x1f, 0x0001 },
2833                 { 0x12, 0x2300 },
2834                 { 0x1f, 0x0002 },
2835                 { 0x00, 0x88d4 },
2836                 { 0x01, 0x82b1 },
2837                 { 0x03, 0x7002 },
2838                 { 0x08, 0x9e30 },
2839                 { 0x09, 0x01f0 },
2840                 { 0x0a, 0x5500 },
2841                 { 0x0c, 0x00c8 },
2842                 { 0x1f, 0x0003 },
2843                 { 0x12, 0xc096 },
2844                 { 0x16, 0x000a },
2845                 { 0x1f, 0x0000 },
2846                 { 0x1f, 0x0000 },
2847                 { 0x09, 0x2000 },
2848                 { 0x09, 0x0000 }
2849         };
2850
2851         rtl_writephy_batch(tp, phy_reg_init);
2852
2853         rtl_patchphy(tp, 0x14, 1 << 5);
2854         rtl_patchphy(tp, 0x0d, 1 << 5);
2855         rtl_writephy(tp, 0x1f, 0x0000);
2856 }
2857
2858 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2859 {
2860         static const struct phy_reg phy_reg_init[] = {
2861                 { 0x1f, 0x0001 },
2862                 { 0x12, 0x2300 },
2863                 { 0x03, 0x802f },
2864                 { 0x02, 0x4f02 },
2865                 { 0x01, 0x0409 },
2866                 { 0x00, 0xf099 },
2867                 { 0x04, 0x9800 },
2868                 { 0x04, 0x9000 },
2869                 { 0x1d, 0x3d98 },
2870                 { 0x1f, 0x0002 },
2871                 { 0x0c, 0x7eb8 },
2872                 { 0x06, 0x0761 },
2873                 { 0x1f, 0x0003 },
2874                 { 0x16, 0x0f0a },
2875                 { 0x1f, 0x0000 }
2876         };
2877
2878         rtl_writephy_batch(tp, phy_reg_init);
2879
2880         rtl_patchphy(tp, 0x16, 1 << 0);
2881         rtl_patchphy(tp, 0x14, 1 << 5);
2882         rtl_patchphy(tp, 0x0d, 1 << 5);
2883         rtl_writephy(tp, 0x1f, 0x0000);
2884 }
2885
2886 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2887 {
2888         static const struct phy_reg phy_reg_init[] = {
2889                 { 0x1f, 0x0001 },
2890                 { 0x12, 0x2300 },
2891                 { 0x1d, 0x3d98 },
2892                 { 0x1f, 0x0002 },
2893                 { 0x0c, 0x7eb8 },
2894                 { 0x06, 0x5461 },
2895                 { 0x1f, 0x0003 },
2896                 { 0x16, 0x0f0a },
2897                 { 0x1f, 0x0000 }
2898         };
2899
2900         rtl_writephy_batch(tp, phy_reg_init);
2901
2902         rtl_patchphy(tp, 0x16, 1 << 0);
2903         rtl_patchphy(tp, 0x14, 1 << 5);
2904         rtl_patchphy(tp, 0x0d, 1 << 5);
2905         rtl_writephy(tp, 0x1f, 0x0000);
2906 }
2907
2908 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2909 {
2910         rtl8168c_3_hw_phy_config(tp);
2911 }
2912
2913 static const struct phy_reg rtl8168d_1_phy_reg_init_0[] = {
2914         /* Channel Estimation */
2915         { 0x1f, 0x0001 },
2916         { 0x06, 0x4064 },
2917         { 0x07, 0x2863 },
2918         { 0x08, 0x059c },
2919         { 0x09, 0x26b4 },
2920         { 0x0a, 0x6a19 },
2921         { 0x0b, 0xdcc8 },
2922         { 0x10, 0xf06d },
2923         { 0x14, 0x7f68 },
2924         { 0x18, 0x7fd9 },
2925         { 0x1c, 0xf0ff },
2926         { 0x1d, 0x3d9c },
2927         { 0x1f, 0x0003 },
2928         { 0x12, 0xf49f },
2929         { 0x13, 0x070b },
2930         { 0x1a, 0x05ad },
2931         { 0x14, 0x94c0 },
2932
2933         /*
2934          * Tx Error Issue
2935          * Enhance line driver power
2936          */
2937         { 0x1f, 0x0002 },
2938         { 0x06, 0x5561 },
2939         { 0x1f, 0x0005 },
2940         { 0x05, 0x8332 },
2941         { 0x06, 0x5561 },
2942
2943         /*
2944          * Can not link to 1Gbps with bad cable
2945          * Decrease SNR threshold form 21.07dB to 19.04dB
2946          */
2947         { 0x1f, 0x0001 },
2948         { 0x17, 0x0cc0 },
2949
2950         { 0x1f, 0x0000 },
2951         { 0x0d, 0xf880 }
2952 };
2953
2954 static const struct phy_reg rtl8168d_1_phy_reg_init_1[] = {
2955         { 0x1f, 0x0002 },
2956         { 0x05, 0x669a },
2957         { 0x1f, 0x0005 },
2958         { 0x05, 0x8330 },
2959         { 0x06, 0x669a },
2960         { 0x1f, 0x0002 }
2961 };
2962
2963 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2964 {
2965         rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
2966
2967         /*
2968          * Rx Error Issue
2969          * Fine Tune Switching regulator parameter
2970          */
2971         rtl_writephy(tp, 0x1f, 0x0002);
2972         rtl_w0w1_phy(tp, 0x0b, 0x0010, 0x00ef);
2973         rtl_w0w1_phy(tp, 0x0c, 0xa200, 0x5d00);
2974
2975         if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
2976                 int val;
2977
2978                 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
2979
2980                 val = rtl_readphy(tp, 0x0d);
2981
2982                 if ((val & 0x00ff) != 0x006c) {
2983                         static const u32 set[] = {
2984                                 0x0065, 0x0066, 0x0067, 0x0068,
2985                                 0x0069, 0x006a, 0x006b, 0x006c
2986                         };
2987                         int i;
2988
2989                         rtl_writephy(tp, 0x1f, 0x0002);
2990
2991                         val &= 0xff00;
2992                         for (i = 0; i < ARRAY_SIZE(set); i++)
2993                                 rtl_writephy(tp, 0x0d, val | set[i]);
2994                 }
2995         } else {
2996                 static const struct phy_reg phy_reg_init[] = {
2997                         { 0x1f, 0x0002 },
2998                         { 0x05, 0x6662 },
2999                         { 0x1f, 0x0005 },
3000                         { 0x05, 0x8330 },
3001                         { 0x06, 0x6662 }
3002                 };
3003
3004                 rtl_writephy_batch(tp, phy_reg_init);
3005         }
3006
3007         /* RSET couple improve */
3008         rtl_writephy(tp, 0x1f, 0x0002);
3009         rtl_patchphy(tp, 0x0d, 0x0300);
3010         rtl_patchphy(tp, 0x0f, 0x0010);
3011
3012         /* Fine tune PLL performance */
3013         rtl_writephy(tp, 0x1f, 0x0002);
3014         rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3015         rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3016
3017         rtl_writephy(tp, 0x1f, 0x0005);
3018         rtl_writephy(tp, 0x05, 0x001b);
3019
3020         rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
3021
3022         rtl_writephy(tp, 0x1f, 0x0000);
3023 }
3024
3025 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
3026 {
3027         rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_0);
3028
3029         if (rtl8168d_efuse_read(tp, 0x01) == 0xb1) {
3030                 int val;
3031
3032                 rtl_writephy_batch(tp, rtl8168d_1_phy_reg_init_1);
3033
3034                 val = rtl_readphy(tp, 0x0d);
3035                 if ((val & 0x00ff) != 0x006c) {
3036                         static const u32 set[] = {
3037                                 0x0065, 0x0066, 0x0067, 0x0068,
3038                                 0x0069, 0x006a, 0x006b, 0x006c
3039                         };
3040                         int i;
3041
3042                         rtl_writephy(tp, 0x1f, 0x0002);
3043
3044                         val &= 0xff00;
3045                         for (i = 0; i < ARRAY_SIZE(set); i++)
3046                                 rtl_writephy(tp, 0x0d, val | set[i]);
3047                 }
3048         } else {
3049                 static const struct phy_reg phy_reg_init[] = {
3050                         { 0x1f, 0x0002 },
3051                         { 0x05, 0x2642 },
3052                         { 0x1f, 0x0005 },
3053                         { 0x05, 0x8330 },
3054                         { 0x06, 0x2642 }
3055                 };
3056
3057                 rtl_writephy_batch(tp, phy_reg_init);
3058         }
3059
3060         /* Fine tune PLL performance */
3061         rtl_writephy(tp, 0x1f, 0x0002);
3062         rtl_w0w1_phy(tp, 0x02, 0x0100, 0x0600);
3063         rtl_w0w1_phy(tp, 0x03, 0x0000, 0xe000);
3064
3065         /* Switching regulator Slew rate */
3066         rtl_writephy(tp, 0x1f, 0x0002);
3067         rtl_patchphy(tp, 0x0f, 0x0017);
3068
3069         rtl_writephy(tp, 0x1f, 0x0005);
3070         rtl_writephy(tp, 0x05, 0x001b);
3071
3072         rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
3073
3074         rtl_writephy(tp, 0x1f, 0x0000);
3075 }
3076
3077 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
3078 {
3079         static const struct phy_reg phy_reg_init[] = {
3080                 { 0x1f, 0x0002 },
3081                 { 0x10, 0x0008 },
3082                 { 0x0d, 0x006c },
3083
3084                 { 0x1f, 0x0000 },
3085                 { 0x0d, 0xf880 },
3086
3087                 { 0x1f, 0x0001 },
3088                 { 0x17, 0x0cc0 },
3089
3090                 { 0x1f, 0x0001 },
3091                 { 0x0b, 0xa4d8 },
3092                 { 0x09, 0x281c },
3093                 { 0x07, 0x2883 },
3094                 { 0x0a, 0x6b35 },
3095                 { 0x1d, 0x3da4 },
3096                 { 0x1c, 0xeffd },
3097                 { 0x14, 0x7f52 },
3098                 { 0x18, 0x7fc6 },
3099                 { 0x08, 0x0601 },
3100                 { 0x06, 0x4063 },
3101                 { 0x10, 0xf074 },
3102                 { 0x1f, 0x0003 },
3103                 { 0x13, 0x0789 },
3104                 { 0x12, 0xf4bd },
3105                 { 0x1a, 0x04fd },
3106                 { 0x14, 0x84b0 },
3107                 { 0x1f, 0x0000 },
3108                 { 0x00, 0x9200 },
3109
3110                 { 0x1f, 0x0005 },
3111                 { 0x01, 0x0340 },
3112                 { 0x1f, 0x0001 },
3113                 { 0x04, 0x4000 },
3114                 { 0x03, 0x1d21 },
3115                 { 0x02, 0x0c32 },
3116                 { 0x01, 0x0200 },
3117                 { 0x00, 0x5554 },
3118                 { 0x04, 0x4800 },
3119                 { 0x04, 0x4000 },
3120                 { 0x04, 0xf000 },
3121                 { 0x03, 0xdf01 },
3122                 { 0x02, 0xdf20 },
3123                 { 0x01, 0x101a },
3124                 { 0x00, 0xa0ff },
3125                 { 0x04, 0xf800 },
3126                 { 0x04, 0xf000 },
3127                 { 0x1f, 0x0000 },
3128
3129                 { 0x1f, 0x0007 },
3130                 { 0x1e, 0x0023 },
3131                 { 0x16, 0x0000 },
3132                 { 0x1f, 0x0000 }
3133         };
3134
3135         rtl_writephy_batch(tp, phy_reg_init);
3136 }
3137
3138 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
3139 {
3140         static const struct phy_reg phy_reg_init[] = {
3141                 { 0x1f, 0x0001 },
3142                 { 0x17, 0x0cc0 },
3143
3144                 { 0x1f, 0x0007 },
3145                 { 0x1e, 0x002d },
3146                 { 0x18, 0x0040 },
3147                 { 0x1f, 0x0000 }
3148         };
3149
3150         rtl_writephy_batch(tp, phy_reg_init);
3151         rtl_patchphy(tp, 0x0d, 1 << 5);
3152 }
3153
3154 static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
3155 {
3156         static const struct phy_reg phy_reg_init[] = {
3157                 /* Enable Delay cap */
3158                 { 0x1f, 0x0005 },
3159                 { 0x05, 0x8b80 },
3160                 { 0x06, 0xc896 },
3161                 { 0x1f, 0x0000 },
3162
3163                 /* Channel estimation fine tune */
3164                 { 0x1f, 0x0001 },
3165                 { 0x0b, 0x6c20 },
3166                 { 0x07, 0x2872 },
3167                 { 0x1c, 0xefff },
3168                 { 0x1f, 0x0003 },
3169                 { 0x14, 0x6420 },
3170                 { 0x1f, 0x0000 },
3171
3172                 /* Update PFM & 10M TX idle timer */
3173                 { 0x1f, 0x0007 },
3174                 { 0x1e, 0x002f },
3175                 { 0x15, 0x1919 },
3176                 { 0x1f, 0x0000 },
3177
3178                 { 0x1f, 0x0007 },
3179                 { 0x1e, 0x00ac },
3180                 { 0x18, 0x0006 },
3181                 { 0x1f, 0x0000 }
3182         };
3183
3184         rtl_apply_firmware(tp);
3185
3186         rtl_writephy_batch(tp, phy_reg_init);
3187
3188         /* DCO enable for 10M IDLE Power */
3189         rtl_writephy(tp, 0x1f, 0x0007);
3190         rtl_writephy(tp, 0x1e, 0x0023);
3191         rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3192         rtl_writephy(tp, 0x1f, 0x0000);
3193
3194         /* For impedance matching */
3195         rtl_writephy(tp, 0x1f, 0x0002);
3196         rtl_w0w1_phy(tp, 0x08, 0x8000, 0x7f00);
3197         rtl_writephy(tp, 0x1f, 0x0000);
3198
3199         /* PHY auto speed down */
3200         rtl_writephy(tp, 0x1f, 0x0007);
3201         rtl_writephy(tp, 0x1e, 0x002d);
3202         rtl_w0w1_phy(tp, 0x18, 0x0050, 0x0000);
3203         rtl_writephy(tp, 0x1f, 0x0000);
3204         rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3205
3206         rtl_writephy(tp, 0x1f, 0x0005);
3207         rtl_writephy(tp, 0x05, 0x8b86);
3208         rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3209         rtl_writephy(tp, 0x1f, 0x0000);
3210
3211         rtl_writephy(tp, 0x1f, 0x0005);
3212         rtl_writephy(tp, 0x05, 0x8b85);
3213         rtl_w0w1_phy(tp, 0x06, 0x0000, 0x2000);
3214         rtl_writephy(tp, 0x1f, 0x0007);
3215         rtl_writephy(tp, 0x1e, 0x0020);
3216         rtl_w0w1_phy(tp, 0x15, 0x0000, 0x1100);
3217         rtl_writephy(tp, 0x1f, 0x0006);
3218         rtl_writephy(tp, 0x00, 0x5a00);
3219         rtl_writephy(tp, 0x1f, 0x0000);
3220         rtl_writephy(tp, 0x0d, 0x0007);
3221         rtl_writephy(tp, 0x0e, 0x003c);
3222         rtl_writephy(tp, 0x0d, 0x4007);
3223         rtl_writephy(tp, 0x0e, 0x0000);
3224         rtl_writephy(tp, 0x0d, 0x0000);
3225 }
3226
3227 static void rtl_rar_exgmac_set(struct rtl8169_private *tp, u8 *addr)
3228 {
3229         const u16 w[] = {
3230                 addr[0] | (addr[1] << 8),
3231                 addr[2] | (addr[3] << 8),
3232                 addr[4] | (addr[5] << 8)
3233         };
3234
3235         rtl_eri_write(tp, 0xe0, ERIAR_MASK_1111, w[0] | (w[1] << 16));
3236         rtl_eri_write(tp, 0xe4, ERIAR_MASK_1111, w[2]);
3237         rtl_eri_write(tp, 0xf0, ERIAR_MASK_1111, w[0] << 16);
3238         rtl_eri_write(tp, 0xf4, ERIAR_MASK_1111, w[1] | (w[2] << 16));
3239 }
3240
3241 static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
3242 {
3243         static const struct phy_reg phy_reg_init[] = {
3244                 /* Enable Delay cap */
3245                 { 0x1f, 0x0004 },
3246                 { 0x1f, 0x0007 },
3247                 { 0x1e, 0x00ac },
3248                 { 0x18, 0x0006 },
3249                 { 0x1f, 0x0002 },
3250                 { 0x1f, 0x0000 },
3251                 { 0x1f, 0x0000 },
3252
3253                 /* Channel estimation fine tune */
3254                 { 0x1f, 0x0003 },
3255                 { 0x09, 0xa20f },
3256                 { 0x1f, 0x0000 },
3257                 { 0x1f, 0x0000 },
3258
3259                 /* Green Setting */
3260                 { 0x1f, 0x0005 },
3261                 { 0x05, 0x8b5b },
3262                 { 0x06, 0x9222 },
3263                 { 0x05, 0x8b6d },
3264                 { 0x06, 0x8000 },
3265                 { 0x05, 0x8b76 },
3266                 { 0x06, 0x8000 },
3267                 { 0x1f, 0x0000 }
3268         };
3269
3270         rtl_apply_firmware(tp);
3271
3272         rtl_writephy_batch(tp, phy_reg_init);
3273
3274         /* For 4-corner performance improve */
3275         rtl_writephy(tp, 0x1f, 0x0005);
3276         rtl_writephy(tp, 0x05, 0x8b80);
3277         rtl_w0w1_phy(tp, 0x17, 0x0006, 0x0000);
3278         rtl_writephy(tp, 0x1f, 0x0000);
3279
3280         /* PHY auto speed down */
3281         rtl_writephy(tp, 0x1f, 0x0004);
3282         rtl_writephy(tp, 0x1f, 0x0007);
3283         rtl_writephy(tp, 0x1e, 0x002d);
3284         rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3285         rtl_writephy(tp, 0x1f, 0x0002);
3286         rtl_writephy(tp, 0x1f, 0x0000);
3287         rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3288
3289         /* improve 10M EEE waveform */
3290         rtl_writephy(tp, 0x1f, 0x0005);
3291         rtl_writephy(tp, 0x05, 0x8b86);
3292         rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3293         rtl_writephy(tp, 0x1f, 0x0000);
3294
3295         /* Improve 2-pair detection performance */
3296         rtl_writephy(tp, 0x1f, 0x0005);
3297         rtl_writephy(tp, 0x05, 0x8b85);
3298         rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3299         rtl_writephy(tp, 0x1f, 0x0000);
3300
3301         rtl8168f_config_eee_phy(tp);
3302         rtl_enable_eee(tp);
3303
3304         /* Green feature */
3305         rtl_writephy(tp, 0x1f, 0x0003);
3306         rtl_w0w1_phy(tp, 0x19, 0x0001, 0x0000);
3307         rtl_w0w1_phy(tp, 0x10, 0x0400, 0x0000);
3308         rtl_writephy(tp, 0x1f, 0x0000);
3309         rtl_writephy(tp, 0x1f, 0x0005);
3310         rtl_w0w1_phy(tp, 0x01, 0x0100, 0x0000);
3311         rtl_writephy(tp, 0x1f, 0x0000);
3312
3313         /* Broken BIOS workaround: feed GigaMAC registers with MAC address. */
3314         rtl_rar_exgmac_set(tp, tp->dev->dev_addr);
3315 }
3316
3317 static void rtl8168f_hw_phy_config(struct rtl8169_private *tp)
3318 {
3319         /* For 4-corner performance improve */
3320         rtl_writephy(tp, 0x1f, 0x0005);
3321         rtl_writephy(tp, 0x05, 0x8b80);
3322         rtl_w0w1_phy(tp, 0x06, 0x0006, 0x0000);
3323         rtl_writephy(tp, 0x1f, 0x0000);
3324
3325         /* PHY auto speed down */
3326         rtl_writephy(tp, 0x1f, 0x0007);
3327         rtl_writephy(tp, 0x1e, 0x002d);
3328         rtl_w0w1_phy(tp, 0x18, 0x0010, 0x0000);
3329         rtl_writephy(tp, 0x1f, 0x0000);
3330         rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3331
3332         /* Improve 10M EEE waveform */
3333         rtl_writephy(tp, 0x1f, 0x0005);
3334         rtl_writephy(tp, 0x05, 0x8b86);
3335         rtl_w0w1_phy(tp, 0x06, 0x0001, 0x0000);
3336         rtl_writephy(tp, 0x1f, 0x0000);
3337
3338         rtl8168f_config_eee_phy(tp);
3339         rtl_enable_eee(tp);
3340 }
3341
3342 static void rtl8168f_1_hw_phy_config(struct rtl8169_private *tp)
3343 {
3344         static const struct phy_reg phy_reg_init[] = {
3345                 /* Channel estimation fine tune */
3346                 { 0x1f, 0x0003 },
3347                 { 0x09, 0xa20f },
3348                 { 0x1f, 0x0000 },
3349
3350                 /* Modify green table for giga & fnet */
3351                 { 0x1f, 0x0005 },
3352                 { 0x05, 0x8b55 },
3353                 { 0x06, 0x0000 },
3354                 { 0x05, 0x8b5e },
3355                 { 0x06, 0x0000 },
3356                 { 0x05, 0x8b67 },
3357                 { 0x06, 0x0000 },
3358                 { 0x05, 0x8b70 },
3359                 { 0x06, 0x0000 },
3360                 { 0x1f, 0x0000 },
3361                 { 0x1f, 0x0007 },
3362                 { 0x1e, 0x0078 },
3363                 { 0x17, 0x0000 },
3364                 { 0x19, 0x00fb },
3365                 { 0x1f, 0x0000 },
3366
3367                 /* Modify green table for 10M */
3368                 { 0x1f, 0x0005 },
3369                 { 0x05, 0x8b79 },
3370                 { 0x06, 0xaa00 },
3371                 { 0x1f, 0x0000 },
3372
3373                 /* Disable hiimpedance detection (RTCT) */
3374                 { 0x1f, 0x0003 },
3375                 { 0x01, 0x328a },
3376                 { 0x1f, 0x0000 }
3377         };
3378
3379         rtl_apply_firmware(tp);
3380
3381         rtl_writephy_batch(tp, phy_reg_init);
3382
3383         rtl8168f_hw_phy_config(tp);
3384
3385         /* Improve 2-pair detection performance */
3386         rtl_writephy(tp, 0x1f, 0x0005);
3387         rtl_writephy(tp, 0x05, 0x8b85);
3388         rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3389         rtl_writephy(tp, 0x1f, 0x0000);
3390 }
3391
3392 static void rtl8168f_2_hw_phy_config(struct rtl8169_private *tp)
3393 {
3394         rtl_apply_firmware(tp);
3395
3396         rtl8168f_hw_phy_config(tp);
3397 }
3398
3399 static void rtl8411_hw_phy_config(struct rtl8169_private *tp)
3400 {
3401         static const struct phy_reg phy_reg_init[] = {
3402                 /* Channel estimation fine tune */
3403                 { 0x1f, 0x0003 },
3404                 { 0x09, 0xa20f },
3405                 { 0x1f, 0x0000 },
3406
3407                 /* Modify green table for giga & fnet */
3408                 { 0x1f, 0x0005 },
3409                 { 0x05, 0x8b55 },
3410                 { 0x06, 0x0000 },
3411                 { 0x05, 0x8b5e },
3412                 { 0x06, 0x0000 },
3413                 { 0x05, 0x8b67 },
3414                 { 0x06, 0x0000 },
3415                 { 0x05, 0x8b70 },
3416                 { 0x06, 0x0000 },
3417                 { 0x1f, 0x0000 },
3418                 { 0x1f, 0x0007 },
3419                 { 0x1e, 0x0078 },
3420                 { 0x17, 0x0000 },
3421                 { 0x19, 0x00aa },
3422                 { 0x1f, 0x0000 },
3423
3424                 /* Modify green table for 10M */
3425                 { 0x1f, 0x0005 },
3426                 { 0x05, 0x8b79 },
3427                 { 0x06, 0xaa00 },
3428                 { 0x1f, 0x0000 },
3429
3430                 /* Disable hiimpedance detection (RTCT) */
3431                 { 0x1f, 0x0003 },
3432                 { 0x01, 0x328a },
3433                 { 0x1f, 0x0000 }
3434         };
3435
3436
3437         rtl_apply_firmware(tp);
3438
3439         rtl8168f_hw_phy_config(tp);
3440
3441         /* Improve 2-pair detection performance */
3442         rtl_writephy(tp, 0x1f, 0x0005);
3443         rtl_writephy(tp, 0x05, 0x8b85);
3444         rtl_w0w1_phy(tp, 0x06, 0x4000, 0x0000);
3445         rtl_writephy(tp, 0x1f, 0x0000);
3446
3447         rtl_writephy_batch(tp, phy_reg_init);
3448
3449         /* Modify green table for giga */
3450         rtl_writephy(tp, 0x1f, 0x0005);
3451         rtl_writephy(tp, 0x05, 0x8b54);
3452         rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3453         rtl_writephy(tp, 0x05, 0x8b5d);
3454         rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0800);
3455         rtl_writephy(tp, 0x05, 0x8a7c);
3456         rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3457         rtl_writephy(tp, 0x05, 0x8a7f);
3458         rtl_w0w1_phy(tp, 0x06, 0x0100, 0x0000);
3459         rtl_writephy(tp, 0x05, 0x8a82);
3460         rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3461         rtl_writephy(tp, 0x05, 0x8a85);
3462         rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3463         rtl_writephy(tp, 0x05, 0x8a88);
3464         rtl_w0w1_phy(tp, 0x06, 0x0000, 0x0100);
3465         rtl_writephy(tp, 0x1f, 0x0000);
3466
3467         /* uc same-seed solution */
3468         rtl_writephy(tp, 0x1f, 0x0005);
3469         rtl_writephy(tp, 0x05, 0x8b85);
3470         rtl_w0w1_phy(tp, 0x06, 0x8000, 0x0000);
3471         rtl_writephy(tp, 0x1f, 0x0000);
3472
3473         /* Green feature */
3474         rtl_writephy(tp, 0x1f, 0x0003);
3475         rtl_w0w1_phy(tp, 0x19, 0x0000, 0x0001);
3476         rtl_w0w1_phy(tp, 0x10, 0x0000, 0x0400);
3477         rtl_writephy(tp, 0x1f, 0x0000);
3478 }
3479
3480 static void rtl8168g_disable_aldps(struct rtl8169_private *tp)
3481 {
3482         phy_write(tp->phydev, 0x1f, 0x0a43);
3483         phy_clear_bits(tp->phydev, 0x10, BIT(2));
3484 }
3485
3486 static void rtl8168g_phy_adjust_10m_aldps(struct rtl8169_private *tp)
3487 {
3488         struct phy_device *phydev = tp->phydev;
3489
3490         phy_write(phydev, 0x1f, 0x0bcc);
3491         phy_clear_bits(phydev, 0x14, BIT(8));
3492
3493         phy_write(phydev, 0x1f, 0x0a44);
3494         phy_set_bits(phydev, 0x11, BIT(7) | BIT(6));
3495
3496         phy_write(phydev, 0x1f, 0x0a43);
3497         phy_write(phydev, 0x13, 0x8084);
3498         phy_clear_bits(phydev, 0x14, BIT(14) | BIT(13));
3499         phy_set_bits(phydev, 0x10, BIT(12) | BIT(1) | BIT(0));
3500
3501         phy_write(phydev, 0x1f, 0x0000);
3502 }
3503
3504 static void rtl8168g_1_hw_phy_config(struct rtl8169_private *tp)
3505 {
3506         rtl_apply_firmware(tp);
3507
3508         rtl_writephy(tp, 0x1f, 0x0a46);
3509         if (rtl_readphy(tp, 0x10) & 0x0100) {
3510                 rtl_writephy(tp, 0x1f, 0x0bcc);
3511                 rtl_w0w1_phy(tp, 0x12, 0x0000, 0x8000);
3512         } else {
3513                 rtl_writephy(tp, 0x1f, 0x0bcc);
3514                 rtl_w0w1_phy(tp, 0x12, 0x8000, 0x0000);
3515         }
3516
3517         rtl_writephy(tp, 0x1f, 0x0a46);
3518         if (rtl_readphy(tp, 0x13) & 0x0100) {
3519                 rtl_writephy(tp, 0x1f, 0x0c41);
3520                 rtl_w0w1_phy(tp, 0x15, 0x0002, 0x0000);
3521         } else {
3522                 rtl_writephy(tp, 0x1f, 0x0c41);
3523                 rtl_w0w1_phy(tp, 0x15, 0x0000, 0x0002);
3524         }
3525
3526         /* Enable PHY auto speed down */
3527         rtl_writephy(tp, 0x1f, 0x0a44);
3528         rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3529
3530         rtl8168g_phy_adjust_10m_aldps(tp);
3531
3532         /* EEE auto-fallback function */
3533         rtl_writephy(tp, 0x1f, 0x0a4b);
3534         rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3535
3536         /* Enable UC LPF tune function */
3537         rtl_writephy(tp, 0x1f, 0x0a43);
3538         rtl_writephy(tp, 0x13, 0x8012);
3539         rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3540
3541         rtl_writephy(tp, 0x1f, 0x0c42);
3542         rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3543
3544         /* Improve SWR Efficiency */
3545         rtl_writephy(tp, 0x1f, 0x0bcd);
3546         rtl_writephy(tp, 0x14, 0x5065);
3547         rtl_writephy(tp, 0x14, 0xd065);
3548         rtl_writephy(tp, 0x1f, 0x0bc8);
3549         rtl_writephy(tp, 0x11, 0x5655);
3550         rtl_writephy(tp, 0x1f, 0x0bcd);
3551         rtl_writephy(tp, 0x14, 0x1065);
3552         rtl_writephy(tp, 0x14, 0x9065);
3553         rtl_writephy(tp, 0x14, 0x1065);
3554
3555         rtl8168g_disable_aldps(tp);
3556         rtl8168g_config_eee_phy(tp);
3557         rtl_enable_eee(tp);
3558 }
3559
3560 static void rtl8168g_2_hw_phy_config(struct rtl8169_private *tp)
3561 {
3562         rtl_apply_firmware(tp);
3563         rtl8168g_config_eee_phy(tp);
3564         rtl_enable_eee(tp);
3565 }
3566
3567 static void rtl8168h_1_hw_phy_config(struct rtl8169_private *tp)
3568 {
3569         u16 dout_tapbin;
3570         u32 data;
3571
3572         rtl_apply_firmware(tp);
3573
3574         /* CHN EST parameters adjust - giga master */
3575         rtl_writephy(tp, 0x1f, 0x0a43);
3576         rtl_writephy(tp, 0x13, 0x809b);
3577         rtl_w0w1_phy(tp, 0x14, 0x8000, 0xf800);
3578         rtl_writephy(tp, 0x13, 0x80a2);
3579         rtl_w0w1_phy(tp, 0x14, 0x8000, 0xff00);
3580         rtl_writephy(tp, 0x13, 0x80a4);
3581         rtl_w0w1_phy(tp, 0x14, 0x8500, 0xff00);
3582         rtl_writephy(tp, 0x13, 0x809c);
3583         rtl_w0w1_phy(tp, 0x14, 0xbd00, 0xff00);
3584         rtl_writephy(tp, 0x1f, 0x0000);
3585
3586         /* CHN EST parameters adjust - giga slave */
3587         rtl_writephy(tp, 0x1f, 0x0a43);
3588         rtl_writephy(tp, 0x13, 0x80ad);
3589         rtl_w0w1_phy(tp, 0x14, 0x7000, 0xf800);
3590         rtl_writephy(tp, 0x13, 0x80b4);
3591         rtl_w0w1_phy(tp, 0x14, 0x5000, 0xff00);
3592         rtl_writephy(tp, 0x13, 0x80ac);
3593         rtl_w0w1_phy(tp, 0x14, 0x4000, 0xff00);
3594         rtl_writephy(tp, 0x1f, 0x0000);
3595
3596         /* CHN EST parameters adjust - fnet */
3597         rtl_writephy(tp, 0x1f, 0x0a43);
3598         rtl_writephy(tp, 0x13, 0x808e);
3599         rtl_w0w1_phy(tp, 0x14, 0x1200, 0xff00);
3600         rtl_writephy(tp, 0x13, 0x8090);
3601         rtl_w0w1_phy(tp, 0x14, 0xe500, 0xff00);
3602         rtl_writephy(tp, 0x13, 0x8092);
3603         rtl_w0w1_phy(tp, 0x14, 0x9f00, 0xff00);
3604         rtl_writephy(tp, 0x1f, 0x0000);
3605
3606         /* enable R-tune & PGA-retune function */
3607         dout_tapbin = 0;
3608         rtl_writephy(tp, 0x1f, 0x0a46);
3609         data = rtl_readphy(tp, 0x13);
3610         data &= 3;
3611         data <<= 2;
3612         dout_tapbin |= data;
3613         data = rtl_readphy(tp, 0x12);
3614         data &= 0xc000;
3615         data >>= 14;
3616         dout_tapbin |= data;
3617         dout_tapbin = ~(dout_tapbin^0x08);
3618         dout_tapbin <<= 12;
3619         dout_tapbin &= 0xf000;
3620         rtl_writephy(tp, 0x1f, 0x0a43);
3621         rtl_writephy(tp, 0x13, 0x827a);
3622         rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3623         rtl_writephy(tp, 0x13, 0x827b);
3624         rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3625         rtl_writephy(tp, 0x13, 0x827c);
3626         rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3627         rtl_writephy(tp, 0x13, 0x827d);
3628         rtl_w0w1_phy(tp, 0x14, dout_tapbin, 0xf000);
3629
3630         rtl_writephy(tp, 0x1f, 0x0a43);
3631         rtl_writephy(tp, 0x13, 0x0811);
3632         rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3633         rtl_writephy(tp, 0x1f, 0x0a42);
3634         rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3635         rtl_writephy(tp, 0x1f, 0x0000);
3636
3637         /* enable GPHY 10M */
3638         rtl_writephy(tp, 0x1f, 0x0a44);
3639         rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3640         rtl_writephy(tp, 0x1f, 0x0000);
3641
3642         /* SAR ADC performance */
3643         rtl_writephy(tp, 0x1f, 0x0bca);
3644         rtl_w0w1_phy(tp, 0x17, 0x4000, 0x3000);
3645         rtl_writephy(tp, 0x1f, 0x0000);
3646
3647         rtl_writephy(tp, 0x1f, 0x0a43);
3648         rtl_writephy(tp, 0x13, 0x803f);
3649         rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3650         rtl_writephy(tp, 0x13, 0x8047);
3651         rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3652         rtl_writephy(tp, 0x13, 0x804f);
3653         rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3654         rtl_writephy(tp, 0x13, 0x8057);
3655         rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3656         rtl_writephy(tp, 0x13, 0x805f);
3657         rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3658         rtl_writephy(tp, 0x13, 0x8067);
3659         rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3660         rtl_writephy(tp, 0x13, 0x806f);
3661         rtl_w0w1_phy(tp, 0x14, 0x0000, 0x3000);
3662         rtl_writephy(tp, 0x1f, 0x0000);
3663
3664         /* disable phy pfm mode */
3665         rtl_writephy(tp, 0x1f, 0x0a44);
3666         rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3667         rtl_writephy(tp, 0x1f, 0x0000);
3668
3669         rtl8168g_disable_aldps(tp);
3670         rtl8168g_config_eee_phy(tp);
3671         rtl_enable_eee(tp);
3672 }
3673
3674 static void rtl8168h_2_hw_phy_config(struct rtl8169_private *tp)
3675 {
3676         u16 ioffset_p3, ioffset_p2, ioffset_p1, ioffset_p0;
3677         u16 rlen;
3678         u32 data;
3679
3680         rtl_apply_firmware(tp);
3681
3682         /* CHIN EST parameter update */
3683         rtl_writephy(tp, 0x1f, 0x0a43);
3684         rtl_writephy(tp, 0x13, 0x808a);
3685         rtl_w0w1_phy(tp, 0x14, 0x000a, 0x003f);
3686         rtl_writephy(tp, 0x1f, 0x0000);
3687
3688         /* enable R-tune & PGA-retune function */
3689         rtl_writephy(tp, 0x1f, 0x0a43);
3690         rtl_writephy(tp, 0x13, 0x0811);
3691         rtl_w0w1_phy(tp, 0x14, 0x0800, 0x0000);
3692         rtl_writephy(tp, 0x1f, 0x0a42);
3693         rtl_w0w1_phy(tp, 0x16, 0x0002, 0x0000);
3694         rtl_writephy(tp, 0x1f, 0x0000);
3695
3696         /* enable GPHY 10M */
3697         rtl_writephy(tp, 0x1f, 0x0a44);
3698         rtl_w0w1_phy(tp, 0x11, 0x0800, 0x0000);
3699         rtl_writephy(tp, 0x1f, 0x0000);
3700
3701         r8168_mac_ocp_write(tp, 0xdd02, 0x807d);
3702         data = r8168_mac_ocp_read(tp, 0xdd02);
3703         ioffset_p3 = ((data & 0x80)>>7);
3704         ioffset_p3 <<= 3;
3705
3706         data = r8168_mac_ocp_read(tp, 0xdd00);
3707         ioffset_p3 |= ((data & (0xe000))>>13);
3708         ioffset_p2 = ((data & (0x1e00))>>9);
3709         ioffset_p1 = ((data & (0x01e0))>>5);
3710         ioffset_p0 = ((data & 0x0010)>>4);
3711         ioffset_p0 <<= 3;
3712         ioffset_p0 |= (data & (0x07));
3713         data = (ioffset_p3<<12)|(ioffset_p2<<8)|(ioffset_p1<<4)|(ioffset_p0);
3714
3715         if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) ||
3716             (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
3717                 rtl_writephy(tp, 0x1f, 0x0bcf);
3718                 rtl_writephy(tp, 0x16, data);
3719                 rtl_writephy(tp, 0x1f, 0x0000);
3720         }
3721
3722         /* Modify rlen (TX LPF corner frequency) level */
3723         rtl_writephy(tp, 0x1f, 0x0bcd);
3724         data = rtl_readphy(tp, 0x16);
3725         data &= 0x000f;
3726         rlen = 0;
3727         if (data > 3)
3728                 rlen = data - 3;
3729         data = rlen | (rlen<<4) | (rlen<<8) | (rlen<<12);
3730         rtl_writephy(tp, 0x17, data);
3731         rtl_writephy(tp, 0x1f, 0x0bcd);
3732         rtl_writephy(tp, 0x1f, 0x0000);
3733
3734         /* disable phy pfm mode */
3735         rtl_writephy(tp, 0x1f, 0x0a44);
3736         rtl_w0w1_phy(tp, 0x11, 0x0000, 0x0080);
3737         rtl_writephy(tp, 0x1f, 0x0000);
3738
3739         rtl8168g_disable_aldps(tp);
3740         rtl8168g_config_eee_phy(tp);
3741         rtl_enable_eee(tp);
3742 }
3743
3744 static void rtl8168ep_1_hw_phy_config(struct rtl8169_private *tp)
3745 {
3746         /* Enable PHY auto speed down */
3747         rtl_writephy(tp, 0x1f, 0x0a44);
3748         rtl_w0w1_phy(tp, 0x11, 0x000c, 0x0000);
3749         rtl_writephy(tp, 0x1f, 0x0000);
3750
3751         rtl8168g_phy_adjust_10m_aldps(tp);
3752
3753         /* Enable EEE auto-fallback function */
3754         rtl_writephy(tp, 0x1f, 0x0a4b);
3755         rtl_w0w1_phy(tp, 0x11, 0x0004, 0x0000);
3756         rtl_writephy(tp, 0x1f, 0x0000);
3757
3758         /* Enable UC LPF tune function */
3759         rtl_writephy(tp, 0x1f, 0x0a43);
3760         rtl_writephy(tp, 0x13, 0x8012);
3761         rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3762         rtl_writephy(tp, 0x1f, 0x0000);
3763
3764         /* set rg_sel_sdm_rate */
3765         rtl_writephy(tp, 0x1f, 0x0c42);
3766         rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3767         rtl_writephy(tp, 0x1f, 0x0000);
3768
3769         rtl8168g_disable_aldps(tp);
3770         rtl8168g_config_eee_phy(tp);
3771         rtl_enable_eee(tp);
3772 }
3773
3774 static void rtl8168ep_2_hw_phy_config(struct rtl8169_private *tp)
3775 {
3776         rtl8168g_phy_adjust_10m_aldps(tp);
3777
3778         /* Enable UC LPF tune function */
3779         rtl_writephy(tp, 0x1f, 0x0a43);
3780         rtl_writephy(tp, 0x13, 0x8012);
3781         rtl_w0w1_phy(tp, 0x14, 0x8000, 0x0000);
3782         rtl_writephy(tp, 0x1f, 0x0000);
3783
3784         /* Set rg_sel_sdm_rate */
3785         rtl_writephy(tp, 0x1f, 0x0c42);
3786         rtl_w0w1_phy(tp, 0x11, 0x4000, 0x2000);
3787         rtl_writephy(tp, 0x1f, 0x0000);
3788
3789         /* Channel estimation parameters */
3790         rtl_writephy(tp, 0x1f, 0x0a43);
3791         rtl_writephy(tp, 0x13, 0x80f3);
3792         rtl_w0w1_phy(tp, 0x14, 0x8b00, ~0x8bff);
3793         rtl_writephy(tp, 0x13, 0x80f0);
3794         rtl_w0w1_phy(tp, 0x14, 0x3a00, ~0x3aff);
3795         rtl_writephy(tp, 0x13, 0x80ef);
3796         rtl_w0w1_phy(tp, 0x14, 0x0500, ~0x05ff);
3797         rtl_writephy(tp, 0x13, 0x80f6);
3798         rtl_w0w1_phy(tp, 0x14, 0x6e00, ~0x6eff);
3799         rtl_writephy(tp, 0x13, 0x80ec);
3800         rtl_w0w1_phy(tp, 0x14, 0x6800, ~0x68ff);
3801         rtl_writephy(tp, 0x13, 0x80ed);
3802         rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3803         rtl_writephy(tp, 0x13, 0x80f2);
3804         rtl_w0w1_phy(tp, 0x14, 0xf400, ~0xf4ff);
3805         rtl_writephy(tp, 0x13, 0x80f4);
3806         rtl_w0w1_phy(tp, 0x14, 0x8500, ~0x85ff);
3807         rtl_writephy(tp, 0x1f, 0x0a43);
3808         rtl_writephy(tp, 0x13, 0x8110);
3809         rtl_w0w1_phy(tp, 0x14, 0xa800, ~0xa8ff);
3810         rtl_writephy(tp, 0x13, 0x810f);
3811         rtl_w0w1_phy(tp, 0x14, 0x1d00, ~0x1dff);
3812         rtl_writephy(tp, 0x13, 0x8111);
3813         rtl_w0w1_phy(tp, 0x14, 0xf500, ~0xf5ff);
3814         rtl_writephy(tp, 0x13, 0x8113);
3815         rtl_w0w1_phy(tp, 0x14, 0x6100, ~0x61ff);
3816         rtl_writephy(tp, 0x13, 0x8115);
3817         rtl_w0w1_phy(tp, 0x14, 0x9200, ~0x92ff);
3818         rtl_writephy(tp, 0x13, 0x810e);
3819         rtl_w0w1_phy(tp, 0x14, 0x0400, ~0x04ff);
3820         rtl_writephy(tp, 0x13, 0x810c);
3821         rtl_w0w1_phy(tp, 0x14, 0x7c00, ~0x7cff);
3822         rtl_writephy(tp, 0x13, 0x810b);
3823         rtl_w0w1_phy(tp, 0x14, 0x5a00, ~0x5aff);
3824         rtl_writephy(tp, 0x1f, 0x0a43);
3825         rtl_writephy(tp, 0x13, 0x80d1);
3826         rtl_w0w1_phy(tp, 0x14, 0xff00, ~0xffff);
3827         rtl_writephy(tp, 0x13, 0x80cd);
3828         rtl_w0w1_phy(tp, 0x14, 0x9e00, ~0x9eff);
3829         rtl_writephy(tp, 0x13, 0x80d3);
3830         rtl_w0w1_phy(tp, 0x14, 0x0e00, ~0x0eff);
3831         rtl_writephy(tp, 0x13, 0x80d5);
3832         rtl_w0w1_phy(tp, 0x14, 0xca00, ~0xcaff);
3833         rtl_writephy(tp, 0x13, 0x80d7);
3834         rtl_w0w1_phy(tp, 0x14, 0x8400, ~0x84ff);
3835
3836         /* Force PWM-mode */
3837         rtl_writephy(tp, 0x1f, 0x0bcd);
3838         rtl_writephy(tp, 0x14, 0x5065);
3839         rtl_writephy(tp, 0x14, 0xd065);
3840         rtl_writephy(tp, 0x1f, 0x0bc8);
3841         rtl_writephy(tp, 0x12, 0x00ed);
3842         rtl_writephy(tp, 0x1f, 0x0bcd);
3843         rtl_writephy(tp, 0x14, 0x1065);
3844         rtl_writephy(tp, 0x14, 0x9065);
3845         rtl_writephy(tp, 0x14, 0x1065);
3846         rtl_writephy(tp, 0x1f, 0x0000);
3847
3848         rtl8168g_disable_aldps(tp);
3849         rtl8168g_config_eee_phy(tp);
3850         rtl_enable_eee(tp);
3851 }
3852
3853 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
3854 {
3855         static const struct phy_reg phy_reg_init[] = {
3856                 { 0x1f, 0x0003 },
3857                 { 0x08, 0x441d },
3858                 { 0x01, 0x9100 },
3859                 { 0x1f, 0x0000 }
3860         };
3861
3862         rtl_writephy(tp, 0x1f, 0x0000);
3863         rtl_patchphy(tp, 0x11, 1 << 12);
3864         rtl_patchphy(tp, 0x19, 1 << 13);
3865         rtl_patchphy(tp, 0x10, 1 << 15);
3866
3867         rtl_writephy_batch(tp, phy_reg_init);
3868 }
3869
3870 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
3871 {
3872         static const struct phy_reg phy_reg_init[] = {
3873                 { 0x1f, 0x0005 },
3874                 { 0x1a, 0x0000 },
3875                 { 0x1f, 0x0000 },
3876
3877                 { 0x1f, 0x0004 },
3878                 { 0x1c, 0x0000 },
3879                 { 0x1f, 0x0000 },
3880
3881                 { 0x1f, 0x0001 },
3882                 { 0x15, 0x7701 },
3883                 { 0x1f, 0x0000 }
3884         };
3885
3886         /* Disable ALDPS before ram code */
3887         rtl_writephy(tp, 0x1f, 0x0000);
3888         rtl_writephy(tp, 0x18, 0x0310);
3889         msleep(100);
3890
3891         rtl_apply_firmware(tp);
3892
3893         rtl_writephy_batch(tp, phy_reg_init);
3894 }
3895
3896 static void rtl8402_hw_phy_config(struct rtl8169_private *tp)
3897 {
3898         /* Disable ALDPS before setting firmware */
3899         rtl_writephy(tp, 0x1f, 0x0000);
3900         rtl_writephy(tp, 0x18, 0x0310);
3901         msleep(20);
3902
3903         rtl_apply_firmware(tp);
3904
3905         /* EEE setting */
3906         rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
3907         rtl_writephy(tp, 0x1f, 0x0004);
3908         rtl_writephy(tp, 0x10, 0x401f);
3909         rtl_writephy(tp, 0x19, 0x7030);
3910         rtl_writephy(tp, 0x1f, 0x0000);
3911 }
3912
3913 static void rtl8106e_hw_phy_config(struct rtl8169_private *tp)
3914 {
3915         static const struct phy_reg phy_reg_init[] = {
3916                 { 0x1f, 0x0004 },
3917                 { 0x10, 0xc07f },
3918                 { 0x19, 0x7030 },
3919                 { 0x1f, 0x0000 }
3920         };
3921
3922         /* Disable ALDPS before ram code */
3923         rtl_writephy(tp, 0x1f, 0x0000);
3924         rtl_writephy(tp, 0x18, 0x0310);
3925         msleep(100);
3926
3927         rtl_apply_firmware(tp);
3928
3929         rtl_eri_write(tp, 0x1b0, ERIAR_MASK_0011, 0x0000);
3930         rtl_writephy_batch(tp, phy_reg_init);
3931
3932         rtl_eri_write(tp, 0x1d0, ERIAR_MASK_0011, 0x0000);
3933 }
3934
3935 static void rtl_hw_phy_config(struct net_device *dev)
3936 {
3937         static const rtl_generic_fct phy_configs[] = {
3938                 /* PCI devices. */
3939                 [RTL_GIGA_MAC_VER_01] = NULL,
3940                 [RTL_GIGA_MAC_VER_02] = rtl8169s_hw_phy_config,
3941                 [RTL_GIGA_MAC_VER_03] = rtl8169s_hw_phy_config,
3942                 [RTL_GIGA_MAC_VER_04] = rtl8169sb_hw_phy_config,
3943                 [RTL_GIGA_MAC_VER_05] = rtl8169scd_hw_phy_config,
3944                 [RTL_GIGA_MAC_VER_06] = rtl8169sce_hw_phy_config,
3945                 /* PCI-E devices. */
3946                 [RTL_GIGA_MAC_VER_07] = rtl8102e_hw_phy_config,
3947                 [RTL_GIGA_MAC_VER_08] = rtl8102e_hw_phy_config,
3948                 [RTL_GIGA_MAC_VER_09] = rtl8102e_hw_phy_config,
3949                 [RTL_GIGA_MAC_VER_10] = NULL,
3950                 [RTL_GIGA_MAC_VER_11] = rtl8168bb_hw_phy_config,
3951                 [RTL_GIGA_MAC_VER_12] = rtl8168bef_hw_phy_config,
3952                 [RTL_GIGA_MAC_VER_13] = NULL,
3953                 [RTL_GIGA_MAC_VER_14] = NULL,
3954                 [RTL_GIGA_MAC_VER_15] = NULL,
3955                 [RTL_GIGA_MAC_VER_16] = NULL,
3956                 [RTL_GIGA_MAC_VER_17] = rtl8168bef_hw_phy_config,
3957                 [RTL_GIGA_MAC_VER_18] = rtl8168cp_1_hw_phy_config,
3958                 [RTL_GIGA_MAC_VER_19] = rtl8168c_1_hw_phy_config,
3959                 [RTL_GIGA_MAC_VER_20] = rtl8168c_2_hw_phy_config,
3960                 [RTL_GIGA_MAC_VER_21] = rtl8168c_3_hw_phy_config,
3961                 [RTL_GIGA_MAC_VER_22] = rtl8168c_4_hw_phy_config,
3962                 [RTL_GIGA_MAC_VER_23] = rtl8168cp_2_hw_phy_config,
3963                 [RTL_GIGA_MAC_VER_24] = rtl8168cp_2_hw_phy_config,
3964                 [RTL_GIGA_MAC_VER_25] = rtl8168d_1_hw_phy_config,
3965                 [RTL_GIGA_MAC_VER_26] = rtl8168d_2_hw_phy_config,
3966                 [RTL_GIGA_MAC_VER_27] = rtl8168d_3_hw_phy_config,
3967                 [RTL_GIGA_MAC_VER_28] = rtl8168d_4_hw_phy_config,
3968                 [RTL_GIGA_MAC_VER_29] = rtl8105e_hw_phy_config,
3969                 [RTL_GIGA_MAC_VER_30] = rtl8105e_hw_phy_config,
3970                 [RTL_GIGA_MAC_VER_31] = NULL,
3971                 [RTL_GIGA_MAC_VER_32] = rtl8168e_1_hw_phy_config,
3972                 [RTL_GIGA_MAC_VER_33] = rtl8168e_1_hw_phy_config,
3973                 [RTL_GIGA_MAC_VER_34] = rtl8168e_2_hw_phy_config,
3974                 [RTL_GIGA_MAC_VER_35] = rtl8168f_1_hw_phy_config,
3975                 [RTL_GIGA_MAC_VER_36] = rtl8168f_2_hw_phy_config,
3976                 [RTL_GIGA_MAC_VER_37] = rtl8402_hw_phy_config,
3977                 [RTL_GIGA_MAC_VER_38] = rtl8411_hw_phy_config,
3978                 [RTL_GIGA_MAC_VER_39] = rtl8106e_hw_phy_config,
3979                 [RTL_GIGA_MAC_VER_40] = rtl8168g_1_hw_phy_config,
3980                 [RTL_GIGA_MAC_VER_41] = NULL,
3981                 [RTL_GIGA_MAC_VER_42] = rtl8168g_2_hw_phy_config,
3982                 [RTL_GIGA_MAC_VER_43] = rtl8168g_2_hw_phy_config,
3983                 [RTL_GIGA_MAC_VER_44] = rtl8168g_2_hw_phy_config,
3984                 [RTL_GIGA_MAC_VER_45] = rtl8168h_1_hw_phy_config,
3985                 [RTL_GIGA_MAC_VER_46] = rtl8168h_2_hw_phy_config,
3986                 [RTL_GIGA_MAC_VER_47] = rtl8168h_1_hw_phy_config,
3987                 [RTL_GIGA_MAC_VER_48] = rtl8168h_2_hw_phy_config,
3988                 [RTL_GIGA_MAC_VER_49] = rtl8168ep_1_hw_phy_config,
3989                 [RTL_GIGA_MAC_VER_50] = rtl8168ep_2_hw_phy_config,
3990                 [RTL_GIGA_MAC_VER_51] = rtl8168ep_2_hw_phy_config,
3991         };
3992         struct rtl8169_private *tp = netdev_priv(dev);
3993
3994         if (phy_configs[tp->mac_version])
3995                 phy_configs[tp->mac_version](tp);
3996 }
3997
3998 static void rtl_schedule_task(struct rtl8169_private *tp, enum rtl_flag flag)
3999 {
4000         if (!test_and_set_bit(flag, tp->wk.flags))
4001                 schedule_work(&tp->wk.work);
4002 }
4003
4004 static bool rtl_tbi_enabled(struct rtl8169_private *tp)
4005 {
4006         return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
4007                (RTL_R8(tp, PHYstatus) & TBI_Enable);
4008 }
4009
4010 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
4011 {
4012         rtl_hw_phy_config(dev);
4013
4014         if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
4015                 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
4016                 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4017                 netif_dbg(tp, drv, dev,
4018                           "Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
4019                 RTL_W8(tp, 0x82, 0x01);
4020         }
4021
4022         /* We may have called phy_speed_down before */
4023         phy_speed_up(tp->phydev);
4024
4025         genphy_soft_reset(tp->phydev);
4026 }
4027
4028 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
4029 {
4030         rtl_lock_work(tp);
4031
4032         rtl_unlock_config_regs(tp);
4033
4034         RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
4035         RTL_R32(tp, MAC4);
4036
4037         RTL_W32(tp, MAC0, addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4038         RTL_R32(tp, MAC0);
4039
4040         if (tp->mac_version == RTL_GIGA_MAC_VER_34)
4041                 rtl_rar_exgmac_set(tp, addr);
4042
4043         rtl_lock_config_regs(tp);
4044
4045         rtl_unlock_work(tp);
4046 }
4047
4048 static int rtl_set_mac_address(struct net_device *dev, void *p)
4049 {
4050         struct rtl8169_private *tp = netdev_priv(dev);
4051         struct device *d = tp_to_dev(tp);
4052         int ret;
4053
4054         ret = eth_mac_addr(dev, p);
4055         if (ret)
4056                 return ret;
4057
4058         pm_runtime_get_noresume(d);
4059
4060         if (pm_runtime_active(d))
4061                 rtl_rar_set(tp, dev->dev_addr);
4062
4063         pm_runtime_put_noidle(d);
4064
4065         return 0;
4066 }
4067
4068 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4069 {
4070         struct rtl8169_private *tp = netdev_priv(dev);
4071
4072         if (!netif_running(dev))
4073                 return -ENODEV;
4074
4075         return phy_mii_ioctl(tp->phydev, ifr, cmd);
4076 }
4077
4078 static void rtl_init_mdio_ops(struct rtl8169_private *tp)
4079 {
4080         struct mdio_ops *ops = &tp->mdio_ops;
4081
4082         switch (tp->mac_version) {
4083         case RTL_GIGA_MAC_VER_27:
4084                 ops->write      = r8168dp_1_mdio_write;
4085                 ops->read       = r8168dp_1_mdio_read;
4086                 break;
4087         case RTL_GIGA_MAC_VER_28:
4088         case RTL_GIGA_MAC_VER_31:
4089                 ops->write      = r8168dp_2_mdio_write;
4090                 ops->read       = r8168dp_2_mdio_read;
4091                 break;
4092         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4093                 ops->write      = r8168g_mdio_write;
4094                 ops->read       = r8168g_mdio_read;
4095                 break;
4096         default:
4097                 ops->write      = r8169_mdio_write;
4098                 ops->read       = r8169_mdio_read;
4099                 break;
4100         }
4101 }
4102
4103 static void rtl_wol_suspend_quirk(struct rtl8169_private *tp)
4104 {
4105         switch (tp->mac_version) {
4106         case RTL_GIGA_MAC_VER_25:
4107         case RTL_GIGA_MAC_VER_26:
4108         case RTL_GIGA_MAC_VER_29:
4109         case RTL_GIGA_MAC_VER_30:
4110         case RTL_GIGA_MAC_VER_32:
4111         case RTL_GIGA_MAC_VER_33:
4112         case RTL_GIGA_MAC_VER_34:
4113         case RTL_GIGA_MAC_VER_37 ... RTL_GIGA_MAC_VER_51:
4114                 RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) |
4115                         AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
4116                 break;
4117         default:
4118                 break;
4119         }
4120 }
4121
4122 static void r8168_pll_power_down(struct rtl8169_private *tp)
4123 {
4124         if (r8168_check_dash(tp))
4125                 return;
4126
4127         if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
4128             tp->mac_version == RTL_GIGA_MAC_VER_33)
4129                 rtl_ephy_write(tp, 0x19, 0xff64);
4130
4131         if (device_may_wakeup(tp_to_dev(tp))) {
4132                 phy_speed_down(tp->phydev, false);
4133                 rtl_wol_suspend_quirk(tp);
4134                 return;
4135         }
4136
4137         switch (tp->mac_version) {
4138         case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
4139         case RTL_GIGA_MAC_VER_37:
4140         case RTL_GIGA_MAC_VER_39:
4141         case RTL_GIGA_MAC_VER_43:
4142         case RTL_GIGA_MAC_VER_44:
4143         case RTL_GIGA_MAC_VER_45:
4144         case RTL_GIGA_MAC_VER_46:
4145         case RTL_GIGA_MAC_VER_47:
4146         case RTL_GIGA_MAC_VER_48:
4147         case RTL_GIGA_MAC_VER_50:
4148         case RTL_GIGA_MAC_VER_51:
4149                 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4150                 break;
4151         case RTL_GIGA_MAC_VER_40:
4152         case RTL_GIGA_MAC_VER_41:
4153         case RTL_GIGA_MAC_VER_49:
4154                 rtl_eri_clear_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
4155                 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) & ~0x80);
4156                 break;
4157         default:
4158                 break;
4159         }
4160 }
4161
4162 static void r8168_pll_power_up(struct rtl8169_private *tp)
4163 {
4164         switch (tp->mac_version) {
4165         case RTL_GIGA_MAC_VER_25 ... RTL_GIGA_MAC_VER_33:
4166         case RTL_GIGA_MAC_VER_37:
4167         case RTL_GIGA_MAC_VER_39:
4168         case RTL_GIGA_MAC_VER_43:
4169                 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0x80);
4170                 break;
4171         case RTL_GIGA_MAC_VER_44:
4172         case RTL_GIGA_MAC_VER_45:
4173         case RTL_GIGA_MAC_VER_46:
4174         case RTL_GIGA_MAC_VER_47:
4175         case RTL_GIGA_MAC_VER_48:
4176         case RTL_GIGA_MAC_VER_50:
4177         case RTL_GIGA_MAC_VER_51:
4178                 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4179                 break;
4180         case RTL_GIGA_MAC_VER_40:
4181         case RTL_GIGA_MAC_VER_41:
4182         case RTL_GIGA_MAC_VER_49:
4183                 RTL_W8(tp, PMCH, RTL_R8(tp, PMCH) | 0xc0);
4184                 rtl_eri_set_bits(tp, 0x1a8, ERIAR_MASK_1111, 0xfc000000);
4185                 break;
4186         default:
4187                 break;
4188         }
4189
4190         phy_resume(tp->phydev);
4191         /* give MAC/PHY some time to resume */
4192         msleep(20);
4193 }
4194
4195 static void rtl_pll_power_down(struct rtl8169_private *tp)
4196 {
4197         switch (tp->mac_version) {
4198         case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4199         case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4200                 break;
4201         default:
4202                 r8168_pll_power_down(tp);
4203         }
4204 }
4205
4206 static void rtl_pll_power_up(struct rtl8169_private *tp)
4207 {
4208         switch (tp->mac_version) {
4209         case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4210         case RTL_GIGA_MAC_VER_13 ... RTL_GIGA_MAC_VER_15:
4211                 break;
4212         default:
4213                 r8168_pll_power_up(tp);
4214         }
4215 }
4216
4217 static void rtl_init_rxcfg(struct rtl8169_private *tp)
4218 {
4219         switch (tp->mac_version) {
4220         case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
4221         case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
4222                 RTL_W32(tp, RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
4223                 break;
4224         case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
4225         case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_36:
4226         case RTL_GIGA_MAC_VER_38:
4227                 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
4228                 break;
4229         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4230                 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST | RX_EARLY_OFF);
4231                 break;
4232         default:
4233                 RTL_W32(tp, RxConfig, RX128_INT_EN | RX_DMA_BURST);
4234                 break;
4235         }
4236 }
4237
4238 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4239 {
4240         tp->dirty_tx = tp->cur_tx = tp->cur_rx = 0;
4241 }
4242
4243 static void rtl_hw_jumbo_enable(struct rtl8169_private *tp)
4244 {
4245         if (tp->jumbo_ops.enable) {
4246                 rtl_unlock_config_regs(tp);
4247                 tp->jumbo_ops.enable(tp);
4248                 rtl_lock_config_regs(tp);
4249         }
4250 }
4251
4252 static void rtl_hw_jumbo_disable(struct rtl8169_private *tp)
4253 {
4254         if (tp->jumbo_ops.disable) {
4255                 rtl_unlock_config_regs(tp);
4256                 tp->jumbo_ops.disable(tp);
4257                 rtl_lock_config_regs(tp);
4258         }
4259 }
4260
4261 static void r8168c_hw_jumbo_enable(struct rtl8169_private *tp)
4262 {
4263         RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4264         RTL_W8(tp, Config4, RTL_R8(tp, Config4) | Jumbo_En1);
4265         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
4266 }
4267
4268 static void r8168c_hw_jumbo_disable(struct rtl8169_private *tp)
4269 {
4270         RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4271         RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~Jumbo_En1);
4272         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4273 }
4274
4275 static void r8168dp_hw_jumbo_enable(struct rtl8169_private *tp)
4276 {
4277         RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4278 }
4279
4280 static void r8168dp_hw_jumbo_disable(struct rtl8169_private *tp)
4281 {
4282         RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4283 }
4284
4285 static void r8168e_hw_jumbo_enable(struct rtl8169_private *tp)
4286 {
4287         RTL_W8(tp, MaxTxPacketSize, 0x3f);
4288         RTL_W8(tp, Config3, RTL_R8(tp, Config3) | Jumbo_En0);
4289         RTL_W8(tp, Config4, RTL_R8(tp, Config4) | 0x01);
4290         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_512B);
4291 }
4292
4293 static void r8168e_hw_jumbo_disable(struct rtl8169_private *tp)
4294 {
4295         RTL_W8(tp, MaxTxPacketSize, 0x0c);
4296         RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Jumbo_En0);
4297         RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~0x01);
4298         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4299 }
4300
4301 static void r8168b_0_hw_jumbo_enable(struct rtl8169_private *tp)
4302 {
4303         rtl_tx_performance_tweak(tp,
4304                 PCI_EXP_DEVCTL_READRQ_512B | PCI_EXP_DEVCTL_NOSNOOP_EN);
4305 }
4306
4307 static void r8168b_0_hw_jumbo_disable(struct rtl8169_private *tp)
4308 {
4309         rtl_tx_performance_tweak(tp,
4310                 PCI_EXP_DEVCTL_READRQ_4096B | PCI_EXP_DEVCTL_NOSNOOP_EN);
4311 }
4312
4313 static void r8168b_1_hw_jumbo_enable(struct rtl8169_private *tp)
4314 {
4315         r8168b_0_hw_jumbo_enable(tp);
4316
4317         RTL_W8(tp, Config4, RTL_R8(tp, Config4) | (1 << 0));
4318 }
4319
4320 static void r8168b_1_hw_jumbo_disable(struct rtl8169_private *tp)
4321 {
4322         r8168b_0_hw_jumbo_disable(tp);
4323
4324         RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
4325 }
4326
4327 static void rtl_init_jumbo_ops(struct rtl8169_private *tp)
4328 {
4329         struct jumbo_ops *ops = &tp->jumbo_ops;
4330
4331         switch (tp->mac_version) {
4332         case RTL_GIGA_MAC_VER_11:
4333                 ops->disable    = r8168b_0_hw_jumbo_disable;
4334                 ops->enable     = r8168b_0_hw_jumbo_enable;
4335                 break;
4336         case RTL_GIGA_MAC_VER_12:
4337         case RTL_GIGA_MAC_VER_17:
4338                 ops->disable    = r8168b_1_hw_jumbo_disable;
4339                 ops->enable     = r8168b_1_hw_jumbo_enable;
4340                 break;
4341         case RTL_GIGA_MAC_VER_18: /* Wild guess. Needs info from Realtek. */
4342         case RTL_GIGA_MAC_VER_19:
4343         case RTL_GIGA_MAC_VER_20:
4344         case RTL_GIGA_MAC_VER_21: /* Wild guess. Needs info from Realtek. */
4345         case RTL_GIGA_MAC_VER_22:
4346         case RTL_GIGA_MAC_VER_23:
4347         case RTL_GIGA_MAC_VER_24:
4348         case RTL_GIGA_MAC_VER_25:
4349         case RTL_GIGA_MAC_VER_26:
4350                 ops->disable    = r8168c_hw_jumbo_disable;
4351                 ops->enable     = r8168c_hw_jumbo_enable;
4352                 break;
4353         case RTL_GIGA_MAC_VER_27:
4354         case RTL_GIGA_MAC_VER_28:
4355                 ops->disable    = r8168dp_hw_jumbo_disable;
4356                 ops->enable     = r8168dp_hw_jumbo_enable;
4357                 break;
4358         case RTL_GIGA_MAC_VER_31: /* Wild guess. Needs info from Realtek. */
4359         case RTL_GIGA_MAC_VER_32:
4360         case RTL_GIGA_MAC_VER_33:
4361         case RTL_GIGA_MAC_VER_34:
4362                 ops->disable    = r8168e_hw_jumbo_disable;
4363                 ops->enable     = r8168e_hw_jumbo_enable;
4364                 break;
4365
4366         /*
4367          * No action needed for jumbo frames with 8169.
4368          * No jumbo for 810x at all.
4369          */
4370         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4371         default:
4372                 ops->disable    = NULL;
4373                 ops->enable     = NULL;
4374                 break;
4375         }
4376 }
4377
4378 DECLARE_RTL_COND(rtl_chipcmd_cond)
4379 {
4380         return RTL_R8(tp, ChipCmd) & CmdReset;
4381 }
4382
4383 static void rtl_hw_reset(struct rtl8169_private *tp)
4384 {
4385         RTL_W8(tp, ChipCmd, CmdReset);
4386
4387         rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
4388 }
4389
4390 static void rtl_request_firmware(struct rtl8169_private *tp)
4391 {
4392         struct rtl_fw *rtl_fw;
4393         int rc = -ENOMEM;
4394
4395         /* firmware loaded already or no firmware available */
4396         if (tp->rtl_fw || !tp->fw_name)
4397                 return;
4398
4399         rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
4400         if (!rtl_fw)
4401                 goto err_warn;
4402
4403         rc = request_firmware(&rtl_fw->fw, tp->fw_name, tp_to_dev(tp));
4404         if (rc < 0)
4405                 goto err_free;
4406
4407         rc = rtl_check_firmware(tp, rtl_fw);
4408         if (rc < 0)
4409                 goto err_release_firmware;
4410
4411         tp->rtl_fw = rtl_fw;
4412
4413         return;
4414
4415 err_release_firmware:
4416         release_firmware(rtl_fw->fw);
4417 err_free:
4418         kfree(rtl_fw);
4419 err_warn:
4420         netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
4421                    tp->fw_name, rc);
4422 }
4423
4424 static void rtl_rx_close(struct rtl8169_private *tp)
4425 {
4426         RTL_W32(tp, RxConfig, RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
4427 }
4428
4429 DECLARE_RTL_COND(rtl_npq_cond)
4430 {
4431         return RTL_R8(tp, TxPoll) & NPQ;
4432 }
4433
4434 DECLARE_RTL_COND(rtl_txcfg_empty_cond)
4435 {
4436         return RTL_R32(tp, TxConfig) & TXCFG_EMPTY;
4437 }
4438
4439 static void rtl8169_hw_reset(struct rtl8169_private *tp)
4440 {
4441         /* Disable interrupts */
4442         rtl8169_irq_mask_and_ack(tp);
4443
4444         rtl_rx_close(tp);
4445
4446         switch (tp->mac_version) {
4447         case RTL_GIGA_MAC_VER_27:
4448         case RTL_GIGA_MAC_VER_28:
4449         case RTL_GIGA_MAC_VER_31:
4450                 rtl_udelay_loop_wait_low(tp, &rtl_npq_cond, 20, 42*42);
4451                 break;
4452         case RTL_GIGA_MAC_VER_34 ... RTL_GIGA_MAC_VER_38:
4453         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
4454                 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4455                 rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 666);
4456                 break;
4457         default:
4458                 RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) | StopReq);
4459                 udelay(100);
4460                 break;
4461         }
4462
4463         rtl_hw_reset(tp);
4464 }
4465
4466 static void rtl_set_tx_config_registers(struct rtl8169_private *tp)
4467 {
4468         u32 val = TX_DMA_BURST << TxDMAShift |
4469                   InterFrameGap << TxInterFrameGapShift;
4470
4471         if (tp->mac_version >= RTL_GIGA_MAC_VER_34 &&
4472             tp->mac_version != RTL_GIGA_MAC_VER_39)
4473                 val |= TXCFG_AUTO_FIFO;
4474
4475         RTL_W32(tp, TxConfig, val);
4476 }
4477
4478 static void rtl_set_rx_max_size(struct rtl8169_private *tp)
4479 {
4480         /* Low hurts. Let's disable the filtering. */
4481         RTL_W16(tp, RxMaxSize, R8169_RX_BUF_SIZE + 1);
4482 }
4483
4484 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp)
4485 {
4486         /*
4487          * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4488          * register to be written before TxDescAddrLow to work.
4489          * Switching from MMIO to I/O access fixes the issue as well.
4490          */
4491         RTL_W32(tp, TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
4492         RTL_W32(tp, TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
4493         RTL_W32(tp, RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
4494         RTL_W32(tp, RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
4495 }
4496
4497 static void rtl8169_set_magic_reg(struct rtl8169_private *tp, unsigned mac_version)
4498 {
4499         u32 val;
4500
4501         if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4502                 val = 0x000fff00;
4503         else if (tp->mac_version == RTL_GIGA_MAC_VER_06)
4504                 val = 0x00ffff00;
4505         else
4506                 return;
4507
4508         if (RTL_R8(tp, Config2) & PCI_Clock_66MHz)
4509                 val |= 0xff;
4510
4511         RTL_W32(tp, 0x7c, val);
4512 }
4513
4514 static void rtl_set_rx_mode(struct net_device *dev)
4515 {
4516         struct rtl8169_private *tp = netdev_priv(dev);
4517         u32 mc_filter[2];       /* Multicast hash filter */
4518         int rx_mode;
4519         u32 tmp = 0;
4520
4521         if (dev->flags & IFF_PROMISC) {
4522                 /* Unconditionally log net taps. */
4523                 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
4524                 rx_mode =
4525                     AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4526                     AcceptAllPhys;
4527                 mc_filter[1] = mc_filter[0] = 0xffffffff;
4528         } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
4529                    (dev->flags & IFF_ALLMULTI)) {
4530                 /* Too many to filter perfectly -- accept all multicasts. */
4531                 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4532                 mc_filter[1] = mc_filter[0] = 0xffffffff;
4533         } else {
4534                 struct netdev_hw_addr *ha;
4535
4536                 rx_mode = AcceptBroadcast | AcceptMyPhys;
4537                 mc_filter[1] = mc_filter[0] = 0;
4538                 netdev_for_each_mc_addr(ha, dev) {
4539                         int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
4540                         mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4541                         rx_mode |= AcceptMulticast;
4542                 }
4543         }
4544
4545         if (dev->features & NETIF_F_RXALL)
4546                 rx_mode |= (AcceptErr | AcceptRunt);
4547
4548         tmp = (RTL_R32(tp, RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
4549
4550         if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
4551                 u32 data = mc_filter[0];
4552
4553                 mc_filter[0] = swab32(mc_filter[1]);
4554                 mc_filter[1] = swab32(data);
4555         }
4556
4557         if (tp->mac_version == RTL_GIGA_MAC_VER_35)
4558                 mc_filter[1] = mc_filter[0] = 0xffffffff;
4559
4560         RTL_W32(tp, MAR0 + 4, mc_filter[1]);
4561         RTL_W32(tp, MAR0 + 0, mc_filter[0]);
4562
4563         RTL_W32(tp, RxConfig, tmp);
4564 }
4565
4566 static void rtl_hw_start(struct  rtl8169_private *tp)
4567 {
4568         rtl_unlock_config_regs(tp);
4569
4570         tp->hw_start(tp);
4571
4572         rtl_set_rx_max_size(tp);
4573         rtl_set_rx_tx_desc_registers(tp);
4574         rtl_lock_config_regs(tp);
4575
4576         /* disable interrupt coalescing */
4577         RTL_W16(tp, IntrMitigate, 0x0000);
4578         /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4579         RTL_R8(tp, IntrMask);
4580         RTL_W8(tp, ChipCmd, CmdTxEnb | CmdRxEnb);
4581         rtl_init_rxcfg(tp);
4582         rtl_set_tx_config_registers(tp);
4583
4584         rtl_set_rx_mode(tp->dev);
4585         /* no early-rx interrupts */
4586         RTL_W16(tp, MultiIntr, RTL_R16(tp, MultiIntr) & 0xf000);
4587         rtl_irq_enable(tp);
4588 }
4589
4590 static void rtl_hw_start_8169(struct rtl8169_private *tp)
4591 {
4592         if (tp->mac_version == RTL_GIGA_MAC_VER_05)
4593                 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
4594
4595         RTL_W8(tp, EarlyTxThres, NoEarlyTx);
4596
4597         tp->cp_cmd |= PCIMulRW;
4598
4599         if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4600             tp->mac_version == RTL_GIGA_MAC_VER_03) {
4601                 netif_dbg(tp, drv, tp->dev,
4602                           "Set MAC Reg C+CR Offset 0xe0. Bit 3 and Bit 14 MUST be 1\n");
4603                 tp->cp_cmd |= (1 << 14);
4604         }
4605
4606         RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4607
4608         rtl8169_set_magic_reg(tp, tp->mac_version);
4609
4610         RTL_W32(tp, RxMissed, 0);
4611 }
4612
4613 DECLARE_RTL_COND(rtl_csiar_cond)
4614 {
4615         return RTL_R32(tp, CSIAR) & CSIAR_FLAG;
4616 }
4617
4618 static void rtl_csi_write(struct rtl8169_private *tp, int addr, int value)
4619 {
4620         u32 func = PCI_FUNC(tp->pci_dev->devfn);
4621
4622         RTL_W32(tp, CSIDR, value);
4623         RTL_W32(tp, CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
4624                 CSIAR_BYTE_ENABLE | func << 16);
4625
4626         rtl_udelay_loop_wait_low(tp, &rtl_csiar_cond, 10, 100);
4627 }
4628
4629 static u32 rtl_csi_read(struct rtl8169_private *tp, int addr)
4630 {
4631         u32 func = PCI_FUNC(tp->pci_dev->devfn);
4632
4633         RTL_W32(tp, CSIAR, (addr & CSIAR_ADDR_MASK) | func << 16 |
4634                 CSIAR_BYTE_ENABLE);
4635
4636         return rtl_udelay_loop_wait_high(tp, &rtl_csiar_cond, 10, 100) ?
4637                 RTL_R32(tp, CSIDR) : ~0;
4638 }
4639
4640 static void rtl_csi_access_enable(struct rtl8169_private *tp, u8 val)
4641 {
4642         struct pci_dev *pdev = tp->pci_dev;
4643         u32 csi;
4644
4645         /* According to Realtek the value at config space address 0x070f
4646          * controls the L0s/L1 entrance latency. We try standard ECAM access
4647          * first and if it fails fall back to CSI.
4648          */
4649         if (pdev->cfg_size > 0x070f &&
4650             pci_write_config_byte(pdev, 0x070f, val) == PCIBIOS_SUCCESSFUL)
4651                 return;
4652
4653         netdev_notice_once(tp->dev,
4654                 "No native access to PCI extended config space, falling back to CSI\n");
4655         csi = rtl_csi_read(tp, 0x070c) & 0x00ffffff;
4656         rtl_csi_write(tp, 0x070c, csi | val << 24);
4657 }
4658
4659 static void rtl_set_def_aspm_entry_latency(struct rtl8169_private *tp)
4660 {
4661         rtl_csi_access_enable(tp, 0x27);
4662 }
4663
4664 struct ephy_info {
4665         unsigned int offset;
4666         u16 mask;
4667         u16 bits;
4668 };
4669
4670 static void __rtl_ephy_init(struct rtl8169_private *tp,
4671                             const struct ephy_info *e, int len)
4672 {
4673         u16 w;
4674
4675         while (len-- > 0) {
4676                 w = (rtl_ephy_read(tp, e->offset) & ~e->mask) | e->bits;
4677                 rtl_ephy_write(tp, e->offset, w);
4678                 e++;
4679         }
4680 }
4681
4682 #define rtl_ephy_init(tp, a) __rtl_ephy_init(tp, a, ARRAY_SIZE(a))
4683
4684 static void rtl_disable_clock_request(struct rtl8169_private *tp)
4685 {
4686         pcie_capability_clear_word(tp->pci_dev, PCI_EXP_LNKCTL,
4687                                    PCI_EXP_LNKCTL_CLKREQ_EN);
4688 }
4689
4690 static void rtl_enable_clock_request(struct rtl8169_private *tp)
4691 {
4692         pcie_capability_set_word(tp->pci_dev, PCI_EXP_LNKCTL,
4693                                  PCI_EXP_LNKCTL_CLKREQ_EN);
4694 }
4695
4696 static void rtl_pcie_state_l2l3_disable(struct rtl8169_private *tp)
4697 {
4698         /* work around an issue when PCI reset occurs during L2/L3 state */
4699         RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Rdy_to_L23);
4700 }
4701
4702 static void rtl_hw_aspm_clkreq_enable(struct rtl8169_private *tp, bool enable)
4703 {
4704         if (enable) {
4705                 RTL_W8(tp, Config5, RTL_R8(tp, Config5) | ASPM_en);
4706                 RTL_W8(tp, Config2, RTL_R8(tp, Config2) | ClkReqEn);
4707         } else {
4708                 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
4709                 RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
4710         }
4711
4712         udelay(10);
4713 }
4714
4715 static void rtl_set_fifo_size(struct rtl8169_private *tp, u16 rx_stat,
4716                               u16 tx_stat, u16 rx_dyn, u16 tx_dyn)
4717 {
4718         /* Usage of dynamic vs. static FIFO is controlled by bit
4719          * TXCFG_AUTO_FIFO. Exact meaning of FIFO values isn't known.
4720          */
4721         rtl_eri_write(tp, 0xc8, ERIAR_MASK_1111, (rx_stat << 16) | rx_dyn);
4722         rtl_eri_write(tp, 0xe8, ERIAR_MASK_1111, (tx_stat << 16) | tx_dyn);
4723 }
4724
4725 static void rtl8168g_set_pause_thresholds(struct rtl8169_private *tp,
4726                                           u8 low, u8 high)
4727 {
4728         /* FIFO thresholds for pause flow control */
4729         rtl_eri_write(tp, 0xcc, ERIAR_MASK_0001, low);
4730         rtl_eri_write(tp, 0xd0, ERIAR_MASK_0001, high);
4731 }
4732
4733 static void rtl_hw_start_8168bb(struct rtl8169_private *tp)
4734 {
4735         RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4736
4737         tp->cp_cmd &= CPCMD_QUIRK_MASK;
4738         RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4739
4740         if (tp->dev->mtu <= ETH_DATA_LEN) {
4741                 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B |
4742                                          PCI_EXP_DEVCTL_NOSNOOP_EN);
4743         }
4744 }
4745
4746 static void rtl_hw_start_8168bef(struct rtl8169_private *tp)
4747 {
4748         rtl_hw_start_8168bb(tp);
4749
4750         RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4751
4752         RTL_W8(tp, Config4, RTL_R8(tp, Config4) & ~(1 << 0));
4753 }
4754
4755 static void __rtl_hw_start_8168cp(struct rtl8169_private *tp)
4756 {
4757         RTL_W8(tp, Config1, RTL_R8(tp, Config1) | Speed_down);
4758
4759         RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4760
4761         if (tp->dev->mtu <= ETH_DATA_LEN)
4762                 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4763
4764         rtl_disable_clock_request(tp);
4765
4766         tp->cp_cmd &= CPCMD_QUIRK_MASK;
4767         RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4768 }
4769
4770 static void rtl_hw_start_8168cp_1(struct rtl8169_private *tp)
4771 {
4772         static const struct ephy_info e_info_8168cp[] = {
4773                 { 0x01, 0,      0x0001 },
4774                 { 0x02, 0x0800, 0x1000 },
4775                 { 0x03, 0,      0x0042 },
4776                 { 0x06, 0x0080, 0x0000 },
4777                 { 0x07, 0,      0x2000 }
4778         };
4779
4780         rtl_set_def_aspm_entry_latency(tp);
4781
4782         rtl_ephy_init(tp, e_info_8168cp);
4783
4784         __rtl_hw_start_8168cp(tp);
4785 }
4786
4787 static void rtl_hw_start_8168cp_2(struct rtl8169_private *tp)
4788 {
4789         rtl_set_def_aspm_entry_latency(tp);
4790
4791         RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4792
4793         if (tp->dev->mtu <= ETH_DATA_LEN)
4794                 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4795
4796         tp->cp_cmd &= CPCMD_QUIRK_MASK;
4797         RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4798 }
4799
4800 static void rtl_hw_start_8168cp_3(struct rtl8169_private *tp)
4801 {
4802         rtl_set_def_aspm_entry_latency(tp);
4803
4804         RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
4805
4806         /* Magic. */
4807         RTL_W8(tp, DBG_REG, 0x20);
4808
4809         RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4810
4811         if (tp->dev->mtu <= ETH_DATA_LEN)
4812                 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4813
4814         tp->cp_cmd &= CPCMD_QUIRK_MASK;
4815         RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4816 }
4817
4818 static void rtl_hw_start_8168c_1(struct rtl8169_private *tp)
4819 {
4820         static const struct ephy_info e_info_8168c_1[] = {
4821                 { 0x02, 0x0800, 0x1000 },
4822                 { 0x03, 0,      0x0002 },
4823                 { 0x06, 0x0080, 0x0000 }
4824         };
4825
4826         rtl_set_def_aspm_entry_latency(tp);
4827
4828         RTL_W8(tp, DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4829
4830         rtl_ephy_init(tp, e_info_8168c_1);
4831
4832         __rtl_hw_start_8168cp(tp);
4833 }
4834
4835 static void rtl_hw_start_8168c_2(struct rtl8169_private *tp)
4836 {
4837         static const struct ephy_info e_info_8168c_2[] = {
4838                 { 0x01, 0,      0x0001 },
4839                 { 0x03, 0x0400, 0x0220 }
4840         };
4841
4842         rtl_set_def_aspm_entry_latency(tp);
4843
4844         rtl_ephy_init(tp, e_info_8168c_2);
4845
4846         __rtl_hw_start_8168cp(tp);
4847 }
4848
4849 static void rtl_hw_start_8168c_3(struct rtl8169_private *tp)
4850 {
4851         rtl_hw_start_8168c_2(tp);
4852 }
4853
4854 static void rtl_hw_start_8168c_4(struct rtl8169_private *tp)
4855 {
4856         rtl_set_def_aspm_entry_latency(tp);
4857
4858         __rtl_hw_start_8168cp(tp);
4859 }
4860
4861 static void rtl_hw_start_8168d(struct rtl8169_private *tp)
4862 {
4863         rtl_set_def_aspm_entry_latency(tp);
4864
4865         rtl_disable_clock_request(tp);
4866
4867         RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4868
4869         if (tp->dev->mtu <= ETH_DATA_LEN)
4870                 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4871
4872         tp->cp_cmd &= CPCMD_QUIRK_MASK;
4873         RTL_W16(tp, CPlusCmd, tp->cp_cmd);
4874 }
4875
4876 static void rtl_hw_start_8168dp(struct rtl8169_private *tp)
4877 {
4878         rtl_set_def_aspm_entry_latency(tp);
4879
4880         if (tp->dev->mtu <= ETH_DATA_LEN)
4881                 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4882
4883         RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4884
4885         rtl_disable_clock_request(tp);
4886 }
4887
4888 static void rtl_hw_start_8168d_4(struct rtl8169_private *tp)
4889 {
4890         static const struct ephy_info e_info_8168d_4[] = {
4891                 { 0x0b, 0x0000, 0x0048 },
4892                 { 0x19, 0x0020, 0x0050 },
4893                 { 0x0c, 0x0100, 0x0020 }
4894         };
4895
4896         rtl_set_def_aspm_entry_latency(tp);
4897
4898         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4899
4900         RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4901
4902         rtl_ephy_init(tp, e_info_8168d_4);
4903
4904         rtl_enable_clock_request(tp);
4905 }
4906
4907 static void rtl_hw_start_8168e_1(struct rtl8169_private *tp)
4908 {
4909         static const struct ephy_info e_info_8168e_1[] = {
4910                 { 0x00, 0x0200, 0x0100 },
4911                 { 0x00, 0x0000, 0x0004 },
4912                 { 0x06, 0x0002, 0x0001 },
4913                 { 0x06, 0x0000, 0x0030 },
4914                 { 0x07, 0x0000, 0x2000 },
4915                 { 0x00, 0x0000, 0x0020 },
4916                 { 0x03, 0x5800, 0x2000 },
4917                 { 0x03, 0x0000, 0x0001 },
4918                 { 0x01, 0x0800, 0x1000 },
4919                 { 0x07, 0x0000, 0x4000 },
4920                 { 0x1e, 0x0000, 0x2000 },
4921                 { 0x19, 0xffff, 0xfe6c },
4922                 { 0x0a, 0x0000, 0x0040 }
4923         };
4924
4925         rtl_set_def_aspm_entry_latency(tp);
4926
4927         rtl_ephy_init(tp, e_info_8168e_1);
4928
4929         if (tp->dev->mtu <= ETH_DATA_LEN)
4930                 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4931
4932         RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
4933
4934         rtl_disable_clock_request(tp);
4935
4936         /* Reset tx FIFO pointer */
4937         RTL_W32(tp, MISC, RTL_R32(tp, MISC) | TXPLA_RST);
4938         RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~TXPLA_RST);
4939
4940         RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
4941 }
4942
4943 static void rtl_hw_start_8168e_2(struct rtl8169_private *tp)
4944 {
4945         static const struct ephy_info e_info_8168e_2[] = {
4946                 { 0x09, 0x0000, 0x0080 },
4947                 { 0x19, 0x0000, 0x0224 }
4948         };
4949
4950         rtl_set_def_aspm_entry_latency(tp);
4951
4952         rtl_ephy_init(tp, e_info_8168e_2);
4953
4954         if (tp->dev->mtu <= ETH_DATA_LEN)
4955                 rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4956
4957         rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4958         rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4959         rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
4960         rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4961         rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x07ff0060);
4962         rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
4963         rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
4964
4965         RTL_W8(tp, MaxTxPacketSize, EarlySize);
4966
4967         rtl_disable_clock_request(tp);
4968
4969         RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
4970
4971         rtl8168_config_eee_mac(tp);
4972
4973         RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
4974         RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
4975         RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
4976
4977         rtl_hw_aspm_clkreq_enable(tp, true);
4978 }
4979
4980 static void rtl_hw_start_8168f(struct rtl8169_private *tp)
4981 {
4982         rtl_set_def_aspm_entry_latency(tp);
4983
4984         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
4985
4986         rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
4987         rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
4988         rtl_set_fifo_size(tp, 0x10, 0x10, 0x02, 0x06);
4989         rtl_reset_packet_filter(tp);
4990         rtl_eri_set_bits(tp, 0x1b0, ERIAR_MASK_0001, BIT(4));
4991         rtl_eri_set_bits(tp, 0x1d0, ERIAR_MASK_0001, BIT(4));
4992         rtl_eri_write(tp, 0xcc, ERIAR_MASK_1111, 0x00000050);
4993         rtl_eri_write(tp, 0xd0, ERIAR_MASK_1111, 0x00000060);
4994
4995         RTL_W8(tp, MaxTxPacketSize, EarlySize);
4996
4997         rtl_disable_clock_request(tp);
4998
4999         RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5000         RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5001         RTL_W32(tp, MISC, RTL_R32(tp, MISC) | PWM_EN);
5002         RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~Spi_en);
5003
5004         rtl8168_config_eee_mac(tp);
5005 }
5006
5007 static void rtl_hw_start_8168f_1(struct rtl8169_private *tp)
5008 {
5009         static const struct ephy_info e_info_8168f_1[] = {
5010                 { 0x06, 0x00c0, 0x0020 },
5011                 { 0x08, 0x0001, 0x0002 },
5012                 { 0x09, 0x0000, 0x0080 },
5013                 { 0x19, 0x0000, 0x0224 }
5014         };
5015
5016         rtl_hw_start_8168f(tp);
5017
5018         rtl_ephy_init(tp, e_info_8168f_1);
5019
5020         rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00);
5021 }
5022
5023 static void rtl_hw_start_8411(struct rtl8169_private *tp)
5024 {
5025         static const struct ephy_info e_info_8168f_1[] = {
5026                 { 0x06, 0x00c0, 0x0020 },
5027                 { 0x0f, 0xffff, 0x5200 },
5028                 { 0x1e, 0x0000, 0x4000 },
5029                 { 0x19, 0x0000, 0x0224 }
5030         };
5031
5032         rtl_hw_start_8168f(tp);
5033         rtl_pcie_state_l2l3_disable(tp);
5034
5035         rtl_ephy_init(tp, e_info_8168f_1);
5036
5037         rtl_eri_set_bits(tp, 0x0d4, ERIAR_MASK_0011, 0x0c00);
5038 }
5039
5040 static void rtl_hw_start_8168g(struct rtl8169_private *tp)
5041 {
5042         rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
5043         rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
5044
5045         rtl_set_def_aspm_entry_latency(tp);
5046
5047         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5048
5049         rtl_reset_packet_filter(tp);
5050         rtl_eri_write(tp, 0x2f8, ERIAR_MASK_0011, 0x1d8f);
5051
5052         RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5053         RTL_W8(tp, MaxTxPacketSize, EarlySize);
5054
5055         rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5056         rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
5057
5058         rtl8168_config_eee_mac(tp);
5059
5060         rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
5061         rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
5062
5063         rtl_pcie_state_l2l3_disable(tp);
5064 }
5065
5066 static void rtl_hw_start_8168g_1(struct rtl8169_private *tp)
5067 {
5068         static const struct ephy_info e_info_8168g_1[] = {
5069                 { 0x00, 0x0000, 0x0008 },
5070                 { 0x0c, 0x37d0, 0x0820 },
5071                 { 0x1e, 0x0000, 0x0001 },
5072                 { 0x19, 0x8000, 0x0000 }
5073         };
5074
5075         rtl_hw_start_8168g(tp);
5076
5077         /* disable aspm and clock request before access ephy */
5078         rtl_hw_aspm_clkreq_enable(tp, false);
5079         rtl_ephy_init(tp, e_info_8168g_1);
5080         rtl_hw_aspm_clkreq_enable(tp, true);
5081 }
5082
5083 static void rtl_hw_start_8168g_2(struct rtl8169_private *tp)
5084 {
5085         static const struct ephy_info e_info_8168g_2[] = {
5086                 { 0x00, 0x0000, 0x0008 },
5087                 { 0x0c, 0x3df0, 0x0200 },
5088                 { 0x19, 0xffff, 0xfc00 },
5089                 { 0x1e, 0xffff, 0x20eb }
5090         };
5091
5092         rtl_hw_start_8168g(tp);
5093
5094         /* disable aspm and clock request before access ephy */
5095         RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~ClkReqEn);
5096         RTL_W8(tp, Config5, RTL_R8(tp, Config5) & ~ASPM_en);
5097         rtl_ephy_init(tp, e_info_8168g_2);
5098 }
5099
5100 static void rtl_hw_start_8411_2(struct rtl8169_private *tp)
5101 {
5102         static const struct ephy_info e_info_8411_2[] = {
5103                 { 0x00, 0x0000, 0x0008 },
5104                 { 0x0c, 0x3df0, 0x0200 },
5105                 { 0x0f, 0xffff, 0x5200 },
5106                 { 0x19, 0x0020, 0x0000 },
5107                 { 0x1e, 0x0000, 0x2000 }
5108         };
5109
5110         rtl_hw_start_8168g(tp);
5111
5112         /* disable aspm and clock request before access ephy */
5113         rtl_hw_aspm_clkreq_enable(tp, false);
5114         rtl_ephy_init(tp, e_info_8411_2);
5115         rtl_hw_aspm_clkreq_enable(tp, true);
5116 }
5117
5118 static void rtl_hw_start_8168h_1(struct rtl8169_private *tp)
5119 {
5120         int rg_saw_cnt;
5121         u32 data;
5122         static const struct ephy_info e_info_8168h_1[] = {
5123                 { 0x1e, 0x0800, 0x0001 },
5124                 { 0x1d, 0x0000, 0x0800 },
5125                 { 0x05, 0xffff, 0x2089 },
5126                 { 0x06, 0xffff, 0x5881 },
5127                 { 0x04, 0xffff, 0x154a },
5128                 { 0x01, 0xffff, 0x068b }
5129         };
5130
5131         /* disable aspm and clock request before access ephy */
5132         rtl_hw_aspm_clkreq_enable(tp, false);
5133         rtl_ephy_init(tp, e_info_8168h_1);
5134
5135         rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
5136         rtl8168g_set_pause_thresholds(tp, 0x38, 0x48);
5137
5138         rtl_set_def_aspm_entry_latency(tp);
5139
5140         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5141
5142         rtl_reset_packet_filter(tp);
5143
5144         rtl_eri_set_bits(tp, 0xdc, ERIAR_MASK_1111, BIT(4));
5145
5146         rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f00);
5147
5148         rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
5149
5150         RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5151         RTL_W8(tp, MaxTxPacketSize, EarlySize);
5152
5153         rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5154         rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
5155
5156         rtl8168_config_eee_mac(tp);
5157
5158         RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5159         RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5160
5161         RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
5162
5163         rtl_eri_clear_bits(tp, 0x1b0, ERIAR_MASK_0011, BIT(12));
5164
5165         rtl_pcie_state_l2l3_disable(tp);
5166
5167         rtl_writephy(tp, 0x1f, 0x0c42);
5168         rg_saw_cnt = (rtl_readphy(tp, 0x13) & 0x3fff);
5169         rtl_writephy(tp, 0x1f, 0x0000);
5170         if (rg_saw_cnt > 0) {
5171                 u16 sw_cnt_1ms_ini;
5172
5173                 sw_cnt_1ms_ini = 16000000/rg_saw_cnt;
5174                 sw_cnt_1ms_ini &= 0x0fff;
5175                 data = r8168_mac_ocp_read(tp, 0xd412);
5176                 data &= ~0x0fff;
5177                 data |= sw_cnt_1ms_ini;
5178                 r8168_mac_ocp_write(tp, 0xd412, data);
5179         }
5180
5181         data = r8168_mac_ocp_read(tp, 0xe056);
5182         data &= ~0xf0;
5183         data |= 0x70;
5184         r8168_mac_ocp_write(tp, 0xe056, data);
5185
5186         data = r8168_mac_ocp_read(tp, 0xe052);
5187         data &= ~0x6000;
5188         data |= 0x8008;
5189         r8168_mac_ocp_write(tp, 0xe052, data);
5190
5191         data = r8168_mac_ocp_read(tp, 0xe0d6);
5192         data &= ~0x01ff;
5193         data |= 0x017f;
5194         r8168_mac_ocp_write(tp, 0xe0d6, data);
5195
5196         data = r8168_mac_ocp_read(tp, 0xd420);
5197         data &= ~0x0fff;
5198         data |= 0x047f;
5199         r8168_mac_ocp_write(tp, 0xd420, data);
5200
5201         r8168_mac_ocp_write(tp, 0xe63e, 0x0001);
5202         r8168_mac_ocp_write(tp, 0xe63e, 0x0000);
5203         r8168_mac_ocp_write(tp, 0xc094, 0x0000);
5204         r8168_mac_ocp_write(tp, 0xc09e, 0x0000);
5205
5206         rtl_hw_aspm_clkreq_enable(tp, true);
5207 }
5208
5209 static void rtl_hw_start_8168ep(struct rtl8169_private *tp)
5210 {
5211         rtl8168ep_stop_cmac(tp);
5212
5213         rtl_set_fifo_size(tp, 0x08, 0x10, 0x02, 0x06);
5214         rtl8168g_set_pause_thresholds(tp, 0x2f, 0x5f);
5215
5216         rtl_set_def_aspm_entry_latency(tp);
5217
5218         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5219
5220         rtl_reset_packet_filter(tp);
5221
5222         rtl_eri_set_bits(tp, 0xd4, ERIAR_MASK_1111, 0x1f80);
5223
5224         rtl_eri_write(tp, 0x5f0, ERIAR_MASK_0011, 0x4f87);
5225
5226         RTL_W32(tp, MISC, RTL_R32(tp, MISC) & ~RXDV_GATED_EN);
5227         RTL_W8(tp, MaxTxPacketSize, EarlySize);
5228
5229         rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5230         rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
5231
5232         rtl8168_config_eee_mac(tp);
5233
5234         rtl_w0w1_eri(tp, 0x2fc, ERIAR_MASK_0001, 0x01, 0x06);
5235
5236         RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~TX_10M_PS_EN);
5237
5238         rtl_pcie_state_l2l3_disable(tp);
5239 }
5240
5241 static void rtl_hw_start_8168ep_1(struct rtl8169_private *tp)
5242 {
5243         static const struct ephy_info e_info_8168ep_1[] = {
5244                 { 0x00, 0xffff, 0x10ab },
5245                 { 0x06, 0xffff, 0xf030 },
5246                 { 0x08, 0xffff, 0x2006 },
5247                 { 0x0d, 0xffff, 0x1666 },
5248                 { 0x0c, 0x3ff0, 0x0000 }
5249         };
5250
5251         /* disable aspm and clock request before access ephy */
5252         rtl_hw_aspm_clkreq_enable(tp, false);
5253         rtl_ephy_init(tp, e_info_8168ep_1);
5254
5255         rtl_hw_start_8168ep(tp);
5256
5257         rtl_hw_aspm_clkreq_enable(tp, true);
5258 }
5259
5260 static void rtl_hw_start_8168ep_2(struct rtl8169_private *tp)
5261 {
5262         static const struct ephy_info e_info_8168ep_2[] = {
5263                 { 0x00, 0xffff, 0x10a3 },
5264                 { 0x19, 0xffff, 0xfc00 },
5265                 { 0x1e, 0xffff, 0x20ea }
5266         };
5267
5268         /* disable aspm and clock request before access ephy */
5269         rtl_hw_aspm_clkreq_enable(tp, false);
5270         rtl_ephy_init(tp, e_info_8168ep_2);
5271
5272         rtl_hw_start_8168ep(tp);
5273
5274         RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5275         RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5276
5277         rtl_hw_aspm_clkreq_enable(tp, true);
5278 }
5279
5280 static void rtl_hw_start_8168ep_3(struct rtl8169_private *tp)
5281 {
5282         u32 data;
5283         static const struct ephy_info e_info_8168ep_3[] = {
5284                 { 0x00, 0xffff, 0x10a3 },
5285                 { 0x19, 0xffff, 0x7c00 },
5286                 { 0x1e, 0xffff, 0x20eb },
5287                 { 0x0d, 0xffff, 0x1666 }
5288         };
5289
5290         /* disable aspm and clock request before access ephy */
5291         rtl_hw_aspm_clkreq_enable(tp, false);
5292         rtl_ephy_init(tp, e_info_8168ep_3);
5293
5294         rtl_hw_start_8168ep(tp);
5295
5296         RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5297         RTL_W8(tp, MISC_1, RTL_R8(tp, MISC_1) & ~PFM_D3COLD_EN);
5298
5299         data = r8168_mac_ocp_read(tp, 0xd3e2);
5300         data &= 0xf000;
5301         data |= 0x0271;
5302         r8168_mac_ocp_write(tp, 0xd3e2, data);
5303
5304         data = r8168_mac_ocp_read(tp, 0xd3e4);
5305         data &= 0xff00;
5306         r8168_mac_ocp_write(tp, 0xd3e4, data);
5307
5308         data = r8168_mac_ocp_read(tp, 0xe860);
5309         data |= 0x0080;
5310         r8168_mac_ocp_write(tp, 0xe860, data);
5311
5312         rtl_hw_aspm_clkreq_enable(tp, true);
5313 }
5314
5315 static void rtl_hw_start_8102e_1(struct rtl8169_private *tp)
5316 {
5317         static const struct ephy_info e_info_8102e_1[] = {
5318                 { 0x01, 0, 0x6e65 },
5319                 { 0x02, 0, 0x091f },
5320                 { 0x03, 0, 0xc2f9 },
5321                 { 0x06, 0, 0xafb5 },
5322                 { 0x07, 0, 0x0e00 },
5323                 { 0x19, 0, 0xec80 },
5324                 { 0x01, 0, 0x2e65 },
5325                 { 0x01, 0, 0x6e65 }
5326         };
5327         u8 cfg1;
5328
5329         rtl_set_def_aspm_entry_latency(tp);
5330
5331         RTL_W8(tp, DBG_REG, FIX_NAK_1);
5332
5333         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5334
5335         RTL_W8(tp, Config1,
5336                LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
5337         RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5338
5339         cfg1 = RTL_R8(tp, Config1);
5340         if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
5341                 RTL_W8(tp, Config1, cfg1 & ~LEDS0);
5342
5343         rtl_ephy_init(tp, e_info_8102e_1);
5344 }
5345
5346 static void rtl_hw_start_8102e_2(struct rtl8169_private *tp)
5347 {
5348         rtl_set_def_aspm_entry_latency(tp);
5349
5350         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5351
5352         RTL_W8(tp, Config1, MEMMAP | IOMAP | VPD | PMEnable);
5353         RTL_W8(tp, Config3, RTL_R8(tp, Config3) & ~Beacon_en);
5354 }
5355
5356 static void rtl_hw_start_8102e_3(struct rtl8169_private *tp)
5357 {
5358         rtl_hw_start_8102e_2(tp);
5359
5360         rtl_ephy_write(tp, 0x03, 0xc2f9);
5361 }
5362
5363 static void rtl_hw_start_8105e_1(struct rtl8169_private *tp)
5364 {
5365         static const struct ephy_info e_info_8105e_1[] = {
5366                 { 0x07, 0, 0x4000 },
5367                 { 0x19, 0, 0x0200 },
5368                 { 0x19, 0, 0x0020 },
5369                 { 0x1e, 0, 0x2000 },
5370                 { 0x03, 0, 0x0001 },
5371                 { 0x19, 0, 0x0100 },
5372                 { 0x19, 0, 0x0004 },
5373                 { 0x0a, 0, 0x0020 }
5374         };
5375
5376         /* Force LAN exit from ASPM if Rx/Tx are not idle */
5377         RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5378
5379         /* Disable Early Tally Counter */
5380         RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) & ~0x010000);
5381
5382         RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5383         RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) | PFM_EN);
5384
5385         rtl_ephy_init(tp, e_info_8105e_1);
5386
5387         rtl_pcie_state_l2l3_disable(tp);
5388 }
5389
5390 static void rtl_hw_start_8105e_2(struct rtl8169_private *tp)
5391 {
5392         rtl_hw_start_8105e_1(tp);
5393         rtl_ephy_write(tp, 0x1e, rtl_ephy_read(tp, 0x1e) | 0x8000);
5394 }
5395
5396 static void rtl_hw_start_8402(struct rtl8169_private *tp)
5397 {
5398         static const struct ephy_info e_info_8402[] = {
5399                 { 0x19, 0xffff, 0xff64 },
5400                 { 0x1e, 0, 0x4000 }
5401         };
5402
5403         rtl_set_def_aspm_entry_latency(tp);
5404
5405         /* Force LAN exit from ASPM if Rx/Tx are not idle */
5406         RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5407
5408         RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
5409
5410         rtl_ephy_init(tp, e_info_8402);
5411
5412         rtl_tx_performance_tweak(tp, PCI_EXP_DEVCTL_READRQ_4096B);
5413
5414         rtl_set_fifo_size(tp, 0x00, 0x00, 0x02, 0x06);
5415         rtl_reset_packet_filter(tp);
5416         rtl_eri_write(tp, 0xc0, ERIAR_MASK_0011, 0x0000);
5417         rtl_eri_write(tp, 0xb8, ERIAR_MASK_0011, 0x0000);
5418         rtl_w0w1_eri(tp, 0x0d4, ERIAR_MASK_0011, 0x0e00, 0xff00);
5419
5420         rtl_pcie_state_l2l3_disable(tp);
5421 }
5422
5423 static void rtl_hw_start_8106(struct rtl8169_private *tp)
5424 {
5425         rtl_hw_aspm_clkreq_enable(tp, false);
5426
5427         /* Force LAN exit from ASPM if Rx/Tx are not idle */
5428         RTL_W32(tp, FuncEvent, RTL_R32(tp, FuncEvent) | 0x002800);
5429
5430         RTL_W32(tp, MISC, (RTL_R32(tp, MISC) | DISABLE_LAN_EN) & ~EARLY_TALLY_EN);
5431         RTL_W8(tp, MCU, RTL_R8(tp, MCU) | EN_NDP | EN_OOB_RESET);
5432         RTL_W8(tp, DLLPR, RTL_R8(tp, DLLPR) & ~PFM_EN);
5433
5434         rtl_pcie_state_l2l3_disable(tp);
5435         rtl_hw_aspm_clkreq_enable(tp, true);
5436 }
5437
5438 static void rtl_hw_config(struct rtl8169_private *tp)
5439 {
5440         static const rtl_generic_fct hw_configs[] = {
5441                 [RTL_GIGA_MAC_VER_07] = rtl_hw_start_8102e_1,
5442                 [RTL_GIGA_MAC_VER_08] = rtl_hw_start_8102e_3,
5443                 [RTL_GIGA_MAC_VER_09] = rtl_hw_start_8102e_2,
5444                 [RTL_GIGA_MAC_VER_10] = NULL,
5445                 [RTL_GIGA_MAC_VER_11] = rtl_hw_start_8168bb,
5446                 [RTL_GIGA_MAC_VER_12] = rtl_hw_start_8168bef,
5447                 [RTL_GIGA_MAC_VER_13] = NULL,
5448                 [RTL_GIGA_MAC_VER_14] = NULL,
5449                 [RTL_GIGA_MAC_VER_15] = NULL,
5450                 [RTL_GIGA_MAC_VER_16] = NULL,
5451                 [RTL_GIGA_MAC_VER_17] = rtl_hw_start_8168bef,
5452                 [RTL_GIGA_MAC_VER_18] = rtl_hw_start_8168cp_1,
5453                 [RTL_GIGA_MAC_VER_19] = rtl_hw_start_8168c_1,
5454                 [RTL_GIGA_MAC_VER_20] = rtl_hw_start_8168c_2,
5455                 [RTL_GIGA_MAC_VER_21] = rtl_hw_start_8168c_3,
5456                 [RTL_GIGA_MAC_VER_22] = rtl_hw_start_8168c_4,
5457                 [RTL_GIGA_MAC_VER_23] = rtl_hw_start_8168cp_2,
5458                 [RTL_GIGA_MAC_VER_24] = rtl_hw_start_8168cp_3,
5459                 [RTL_GIGA_MAC_VER_25] = rtl_hw_start_8168d,
5460                 [RTL_GIGA_MAC_VER_26] = rtl_hw_start_8168d,
5461                 [RTL_GIGA_MAC_VER_27] = rtl_hw_start_8168d,
5462                 [RTL_GIGA_MAC_VER_28] = rtl_hw_start_8168d_4,
5463                 [RTL_GIGA_MAC_VER_29] = rtl_hw_start_8105e_1,
5464                 [RTL_GIGA_MAC_VER_30] = rtl_hw_start_8105e_2,
5465                 [RTL_GIGA_MAC_VER_31] = rtl_hw_start_8168dp,
5466                 [RTL_GIGA_MAC_VER_32] = rtl_hw_start_8168e_1,
5467                 [RTL_GIGA_MAC_VER_33] = rtl_hw_start_8168e_1,
5468                 [RTL_GIGA_MAC_VER_34] = rtl_hw_start_8168e_2,
5469                 [RTL_GIGA_MAC_VER_35] = rtl_hw_start_8168f_1,
5470                 [RTL_GIGA_MAC_VER_36] = rtl_hw_start_8168f_1,
5471                 [RTL_GIGA_MAC_VER_37] = rtl_hw_start_8402,
5472                 [RTL_GIGA_MAC_VER_38] = rtl_hw_start_8411,
5473                 [RTL_GIGA_MAC_VER_39] = rtl_hw_start_8106,
5474                 [RTL_GIGA_MAC_VER_40] = rtl_hw_start_8168g_1,
5475                 [RTL_GIGA_MAC_VER_41] = rtl_hw_start_8168g_1,
5476                 [RTL_GIGA_MAC_VER_42] = rtl_hw_start_8168g_2,
5477                 [RTL_GIGA_MAC_VER_43] = rtl_hw_start_8168g_2,
5478                 [RTL_GIGA_MAC_VER_44] = rtl_hw_start_8411_2,
5479                 [RTL_GIGA_MAC_VER_45] = rtl_hw_start_8168h_1,
5480                 [RTL_GIGA_MAC_VER_46] = rtl_hw_start_8168h_1,
5481                 [RTL_GIGA_MAC_VER_47] = rtl_hw_start_8168h_1,
5482                 [RTL_GIGA_MAC_VER_48] = rtl_hw_start_8168h_1,
5483                 [RTL_GIGA_MAC_VER_49] = rtl_hw_start_8168ep_1,
5484                 [RTL_GIGA_MAC_VER_50] = rtl_hw_start_8168ep_2,
5485                 [RTL_GIGA_MAC_VER_51] = rtl_hw_start_8168ep_3,
5486         };
5487
5488         if (hw_configs[tp->mac_version])
5489                 hw_configs[tp->mac_version](tp);
5490 }
5491
5492 static void rtl_hw_start_8168(struct rtl8169_private *tp)
5493 {
5494         RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5495
5496         /* Workaround for RxFIFO overflow. */
5497         if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
5498                 tp->irq_mask |= RxFIFOOver;
5499                 tp->irq_mask &= ~RxOverflow;
5500         }
5501
5502         rtl_hw_config(tp);
5503 }
5504
5505 static void rtl_hw_start_8101(struct rtl8169_private *tp)
5506 {
5507         if (tp->mac_version >= RTL_GIGA_MAC_VER_30)
5508                 tp->irq_mask &= ~RxFIFOOver;
5509
5510         if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
5511             tp->mac_version == RTL_GIGA_MAC_VER_16)
5512                 pcie_capability_set_word(tp->pci_dev, PCI_EXP_DEVCTL,
5513                                          PCI_EXP_DEVCTL_NOSNOOP_EN);
5514
5515         RTL_W8(tp, MaxTxPacketSize, TxPacketMax);
5516
5517         tp->cp_cmd &= CPCMD_QUIRK_MASK;
5518         RTL_W16(tp, CPlusCmd, tp->cp_cmd);
5519
5520         rtl_hw_config(tp);
5521 }
5522
5523 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
5524 {
5525         struct rtl8169_private *tp = netdev_priv(dev);
5526
5527         if (new_mtu > ETH_DATA_LEN)
5528                 rtl_hw_jumbo_enable(tp);
5529         else
5530                 rtl_hw_jumbo_disable(tp);
5531
5532         dev->mtu = new_mtu;
5533         netdev_update_features(dev);
5534
5535         return 0;
5536 }
5537
5538 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
5539 {
5540         desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
5541         desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
5542 }
5543
5544 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
5545                                      void **data_buff, struct RxDesc *desc)
5546 {
5547         dma_unmap_single(tp_to_dev(tp), le64_to_cpu(desc->addr),
5548                          R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5549
5550         kfree(*data_buff);
5551         *data_buff = NULL;
5552         rtl8169_make_unusable_by_asic(desc);
5553 }
5554
5555 static inline void rtl8169_mark_to_asic(struct RxDesc *desc)
5556 {
5557         u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
5558
5559         /* Force memory writes to complete before releasing descriptor */
5560         dma_wmb();
5561
5562         desc->opts1 = cpu_to_le32(DescOwn | eor | R8169_RX_BUF_SIZE);
5563 }
5564
5565 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
5566                                              struct RxDesc *desc)
5567 {
5568         void *data;
5569         dma_addr_t mapping;
5570         struct device *d = tp_to_dev(tp);
5571         int node = dev_to_node(d);
5572
5573         data = kmalloc_node(R8169_RX_BUF_SIZE, GFP_KERNEL, node);
5574         if (!data)
5575                 return NULL;
5576
5577         /* Memory should be properly aligned, but better check. */
5578         if (!IS_ALIGNED((unsigned long)data, 8)) {
5579                 netdev_err_once(tp->dev, "RX buffer not 8-byte-aligned\n");
5580                 goto err_out;
5581         }
5582
5583         mapping = dma_map_single(d, data, R8169_RX_BUF_SIZE, DMA_FROM_DEVICE);
5584         if (unlikely(dma_mapping_error(d, mapping))) {
5585                 if (net_ratelimit())
5586                         netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
5587                 goto err_out;
5588         }
5589
5590         desc->addr = cpu_to_le64(mapping);
5591         rtl8169_mark_to_asic(desc);
5592         return data;
5593
5594 err_out:
5595         kfree(data);
5596         return NULL;
5597 }
5598
5599 static void rtl8169_rx_clear(struct rtl8169_private *tp)
5600 {
5601         unsigned int i;
5602
5603         for (i = 0; i < NUM_RX_DESC; i++) {
5604                 if (tp->Rx_databuff[i]) {
5605                         rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
5606                                             tp->RxDescArray + i);
5607                 }
5608         }
5609 }
5610
5611 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
5612 {
5613         desc->opts1 |= cpu_to_le32(RingEnd);
5614 }
5615
5616 static int rtl8169_rx_fill(struct rtl8169_private *tp)
5617 {
5618         unsigned int i;
5619
5620         for (i = 0; i < NUM_RX_DESC; i++) {
5621                 void *data;
5622
5623                 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
5624                 if (!data) {
5625                         rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
5626                         goto err_out;
5627                 }
5628                 tp->Rx_databuff[i] = data;
5629         }
5630
5631         rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
5632         return 0;
5633
5634 err_out:
5635         rtl8169_rx_clear(tp);
5636         return -ENOMEM;
5637 }
5638
5639 static int rtl8169_init_ring(struct rtl8169_private *tp)
5640 {
5641         rtl8169_init_ring_indexes(tp);
5642
5643         memset(tp->tx_skb, 0, sizeof(tp->tx_skb));
5644         memset(tp->Rx_databuff, 0, sizeof(tp->Rx_databuff));
5645
5646         return rtl8169_rx_fill(tp);
5647 }
5648
5649 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
5650                                  struct TxDesc *desc)
5651 {
5652         unsigned int len = tx_skb->len;
5653
5654         dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
5655
5656         desc->opts1 = 0x00;
5657         desc->opts2 = 0x00;
5658         desc->addr = 0x00;
5659         tx_skb->len = 0;
5660 }
5661
5662 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
5663                                    unsigned int n)
5664 {
5665         unsigned int i;
5666
5667         for (i = 0; i < n; i++) {
5668                 unsigned int entry = (start + i) % NUM_TX_DESC;
5669                 struct ring_info *tx_skb = tp->tx_skb + entry;
5670                 unsigned int len = tx_skb->len;
5671
5672                 if (len) {
5673                         struct sk_buff *skb = tx_skb->skb;
5674
5675                         rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
5676                                              tp->TxDescArray + entry);
5677                         if (skb) {
5678                                 dev_consume_skb_any(skb);
5679                                 tx_skb->skb = NULL;
5680                         }
5681                 }
5682         }
5683 }
5684
5685 static void rtl8169_tx_clear(struct rtl8169_private *tp)
5686 {
5687         rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
5688         tp->cur_tx = tp->dirty_tx = 0;
5689         netdev_reset_queue(tp->dev);
5690 }
5691
5692 static void rtl_reset_work(struct rtl8169_private *tp)
5693 {
5694         struct net_device *dev = tp->dev;
5695         int i;
5696
5697         napi_disable(&tp->napi);
5698         netif_stop_queue(dev);
5699         synchronize_rcu();
5700
5701         rtl8169_hw_reset(tp);
5702
5703         for (i = 0; i < NUM_RX_DESC; i++)
5704                 rtl8169_mark_to_asic(tp->RxDescArray + i);
5705
5706         rtl8169_tx_clear(tp);
5707         rtl8169_init_ring_indexes(tp);
5708
5709         napi_enable(&tp->napi);
5710         rtl_hw_start(tp);
5711         netif_wake_queue(dev);
5712 }
5713
5714 static void rtl8169_tx_timeout(struct net_device *dev)
5715 {
5716         struct rtl8169_private *tp = netdev_priv(dev);
5717
5718         rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
5719 }
5720
5721 static __le32 rtl8169_get_txd_opts1(u32 opts0, u32 len, unsigned int entry)
5722 {
5723         u32 status = opts0 | len;
5724
5725         if (entry == NUM_TX_DESC - 1)
5726                 status |= RingEnd;
5727
5728         return cpu_to_le32(status);
5729 }
5730
5731 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
5732                               u32 *opts)
5733 {
5734         struct skb_shared_info *info = skb_shinfo(skb);
5735         unsigned int cur_frag, entry;
5736         struct TxDesc *uninitialized_var(txd);
5737         struct device *d = tp_to_dev(tp);
5738
5739         entry = tp->cur_tx;
5740         for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5741                 const skb_frag_t *frag = info->frags + cur_frag;
5742                 dma_addr_t mapping;
5743                 u32 len;
5744                 void *addr;
5745
5746                 entry = (entry + 1) % NUM_TX_DESC;
5747
5748                 txd = tp->TxDescArray + entry;
5749                 len = skb_frag_size(frag);
5750                 addr = skb_frag_address(frag);
5751                 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
5752                 if (unlikely(dma_mapping_error(d, mapping))) {
5753                         if (net_ratelimit())
5754                                 netif_err(tp, drv, tp->dev,
5755                                           "Failed to map TX fragments DMA!\n");
5756                         goto err_out;
5757                 }
5758
5759                 txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
5760                 txd->opts2 = cpu_to_le32(opts[1]);
5761                 txd->addr = cpu_to_le64(mapping);
5762
5763                 tp->tx_skb[entry].len = len;
5764         }
5765
5766         if (cur_frag) {
5767                 tp->tx_skb[entry].skb = skb;
5768                 txd->opts1 |= cpu_to_le32(LastFrag);
5769         }
5770
5771         return cur_frag;
5772
5773 err_out:
5774         rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5775         return -EIO;
5776 }
5777
5778 static bool rtl_test_hw_pad_bug(struct rtl8169_private *tp, struct sk_buff *skb)
5779 {
5780         return skb->len < ETH_ZLEN && tp->mac_version == RTL_GIGA_MAC_VER_34;
5781 }
5782
5783 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5784                                       struct net_device *dev);
5785 /* r8169_csum_workaround()
5786  * The hw limites the value the transport offset. When the offset is out of the
5787  * range, calculate the checksum by sw.
5788  */
5789 static void r8169_csum_workaround(struct rtl8169_private *tp,
5790                                   struct sk_buff *skb)
5791 {
5792         if (skb_shinfo(skb)->gso_size) {
5793                 netdev_features_t features = tp->dev->features;
5794                 struct sk_buff *segs, *nskb;
5795
5796                 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
5797                 segs = skb_gso_segment(skb, features);
5798                 if (IS_ERR(segs) || !segs)
5799                         goto drop;
5800
5801                 do {
5802                         nskb = segs;
5803                         segs = segs->next;
5804                         nskb->next = NULL;
5805                         rtl8169_start_xmit(nskb, tp->dev);
5806                 } while (segs);
5807
5808                 dev_consume_skb_any(skb);
5809         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5810                 if (skb_checksum_help(skb) < 0)
5811                         goto drop;
5812
5813                 rtl8169_start_xmit(skb, tp->dev);
5814         } else {
5815                 struct net_device_stats *stats;
5816
5817 drop:
5818                 stats = &tp->dev->stats;
5819                 stats->tx_dropped++;
5820                 dev_kfree_skb_any(skb);
5821         }
5822 }
5823
5824 /* msdn_giant_send_check()
5825  * According to the document of microsoft, the TCP Pseudo Header excludes the
5826  * packet length for IPv6 TCP large packets.
5827  */
5828 static int msdn_giant_send_check(struct sk_buff *skb)
5829 {
5830         const struct ipv6hdr *ipv6h;
5831         struct tcphdr *th;
5832         int ret;
5833
5834         ret = skb_cow_head(skb, 0);
5835         if (ret)
5836                 return ret;
5837
5838         ipv6h = ipv6_hdr(skb);
5839         th = tcp_hdr(skb);
5840
5841         th->check = 0;
5842         th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
5843
5844         return ret;
5845 }
5846
5847 static bool rtl8169_tso_csum_v1(struct rtl8169_private *tp,
5848                                 struct sk_buff *skb, u32 *opts)
5849 {
5850         u32 mss = skb_shinfo(skb)->gso_size;
5851
5852         if (mss) {
5853                 opts[0] |= TD_LSO;
5854                 opts[0] |= min(mss, TD_MSS_MAX) << TD0_MSS_SHIFT;
5855         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5856                 const struct iphdr *ip = ip_hdr(skb);
5857
5858                 if (ip->protocol == IPPROTO_TCP)
5859                         opts[0] |= TD0_IP_CS | TD0_TCP_CS;
5860                 else if (ip->protocol == IPPROTO_UDP)
5861                         opts[0] |= TD0_IP_CS | TD0_UDP_CS;
5862                 else
5863                         WARN_ON_ONCE(1);
5864         }
5865
5866         return true;
5867 }
5868
5869 static bool rtl8169_tso_csum_v2(struct rtl8169_private *tp,
5870                                 struct sk_buff *skb, u32 *opts)
5871 {
5872         u32 transport_offset = (u32)skb_transport_offset(skb);
5873         u32 mss = skb_shinfo(skb)->gso_size;
5874
5875         if (mss) {
5876                 if (transport_offset > GTTCPHO_MAX) {
5877                         netif_warn(tp, tx_err, tp->dev,
5878                                    "Invalid transport offset 0x%x for TSO\n",
5879                                    transport_offset);
5880                         return false;
5881                 }
5882
5883                 switch (vlan_get_protocol(skb)) {
5884                 case htons(ETH_P_IP):
5885                         opts[0] |= TD1_GTSENV4;
5886                         break;
5887
5888                 case htons(ETH_P_IPV6):
5889                         if (msdn_giant_send_check(skb))
5890                                 return false;
5891
5892                         opts[0] |= TD1_GTSENV6;
5893                         break;
5894
5895                 default:
5896                         WARN_ON_ONCE(1);
5897                         break;
5898                 }
5899
5900                 opts[0] |= transport_offset << GTTCPHO_SHIFT;
5901                 opts[1] |= min(mss, TD_MSS_MAX) << TD1_MSS_SHIFT;
5902         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
5903                 u8 ip_protocol;
5904
5905                 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
5906                         return !(skb_checksum_help(skb) || eth_skb_pad(skb));
5907
5908                 if (transport_offset > TCPHO_MAX) {
5909                         netif_warn(tp, tx_err, tp->dev,
5910                                    "Invalid transport offset 0x%x\n",
5911                                    transport_offset);
5912                         return false;
5913                 }
5914
5915                 switch (vlan_get_protocol(skb)) {
5916                 case htons(ETH_P_IP):
5917                         opts[1] |= TD1_IPv4_CS;
5918                         ip_protocol = ip_hdr(skb)->protocol;
5919                         break;
5920
5921                 case htons(ETH_P_IPV6):
5922                         opts[1] |= TD1_IPv6_CS;
5923                         ip_protocol = ipv6_hdr(skb)->nexthdr;
5924                         break;
5925
5926                 default:
5927                         ip_protocol = IPPROTO_RAW;
5928                         break;
5929                 }
5930
5931                 if (ip_protocol == IPPROTO_TCP)
5932                         opts[1] |= TD1_TCP_CS;
5933                 else if (ip_protocol == IPPROTO_UDP)
5934                         opts[1] |= TD1_UDP_CS;
5935                 else
5936                         WARN_ON_ONCE(1);
5937
5938                 opts[1] |= transport_offset << TCPHO_SHIFT;
5939         } else {
5940                 if (unlikely(rtl_test_hw_pad_bug(tp, skb)))
5941                         return !eth_skb_pad(skb);
5942         }
5943
5944         return true;
5945 }
5946
5947 static bool rtl_tx_slots_avail(struct rtl8169_private *tp,
5948                                unsigned int nr_frags)
5949 {
5950         unsigned int slots_avail = tp->dirty_tx + NUM_TX_DESC - tp->cur_tx;
5951
5952         /* A skbuff with nr_frags needs nr_frags+1 entries in the tx queue */
5953         return slots_avail > nr_frags;
5954 }
5955
5956 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5957                                       struct net_device *dev)
5958 {
5959         struct rtl8169_private *tp = netdev_priv(dev);
5960         unsigned int entry = tp->cur_tx % NUM_TX_DESC;
5961         struct TxDesc *txd = tp->TxDescArray + entry;
5962         struct device *d = tp_to_dev(tp);
5963         dma_addr_t mapping;
5964         u32 opts[2], len;
5965         int frags;
5966
5967         if (unlikely(!rtl_tx_slots_avail(tp, skb_shinfo(skb)->nr_frags))) {
5968                 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
5969                 goto err_stop_0;
5970         }
5971
5972         if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
5973                 goto err_stop_0;
5974
5975         opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(skb));
5976         opts[0] = DescOwn;
5977
5978         if (!tp->tso_csum(tp, skb, opts)) {
5979                 r8169_csum_workaround(tp, skb);
5980                 return NETDEV_TX_OK;
5981         }
5982
5983         len = skb_headlen(skb);
5984         mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
5985         if (unlikely(dma_mapping_error(d, mapping))) {
5986                 if (net_ratelimit())
5987                         netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
5988                 goto err_dma_0;
5989         }
5990
5991         tp->tx_skb[entry].len = len;
5992         txd->addr = cpu_to_le64(mapping);
5993
5994         frags = rtl8169_xmit_frags(tp, skb, opts);
5995         if (frags < 0)
5996                 goto err_dma_1;
5997         else if (frags)
5998                 opts[0] |= FirstFrag;
5999         else {
6000                 opts[0] |= FirstFrag | LastFrag;
6001                 tp->tx_skb[entry].skb = skb;
6002         }
6003
6004         txd->opts2 = cpu_to_le32(opts[1]);
6005
6006         netdev_sent_queue(dev, skb->len);
6007
6008         skb_tx_timestamp(skb);
6009
6010         /* Force memory writes to complete before releasing descriptor */
6011         dma_wmb();
6012
6013         txd->opts1 = rtl8169_get_txd_opts1(opts[0], len, entry);
6014
6015         /* Force all memory writes to complete before notifying device */
6016         wmb();
6017
6018         tp->cur_tx += frags + 1;
6019
6020         RTL_W8(tp, TxPoll, NPQ);
6021
6022         if (!rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
6023                 /* Avoid wrongly optimistic queue wake-up: rtl_tx thread must
6024                  * not miss a ring update when it notices a stopped queue.
6025                  */
6026                 smp_wmb();
6027                 netif_stop_queue(dev);
6028                 /* Sync with rtl_tx:
6029                  * - publish queue status and cur_tx ring index (write barrier)
6030                  * - refresh dirty_tx ring index (read barrier).
6031                  * May the current thread have a pessimistic view of the ring
6032                  * status and forget to wake up queue, a racing rtl_tx thread
6033                  * can't.
6034                  */
6035                 smp_mb();
6036                 if (rtl_tx_slots_avail(tp, MAX_SKB_FRAGS))
6037                         netif_start_queue(dev);
6038         }
6039
6040         return NETDEV_TX_OK;
6041
6042 err_dma_1:
6043         rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
6044 err_dma_0:
6045         dev_kfree_skb_any(skb);
6046         dev->stats.tx_dropped++;
6047         return NETDEV_TX_OK;
6048
6049 err_stop_0:
6050         netif_stop_queue(dev);
6051         dev->stats.tx_dropped++;
6052         return NETDEV_TX_BUSY;
6053 }
6054
6055 static void rtl8169_pcierr_interrupt(struct net_device *dev)
6056 {
6057         struct rtl8169_private *tp = netdev_priv(dev);
6058         struct pci_dev *pdev = tp->pci_dev;
6059         u16 pci_status, pci_cmd;
6060
6061         pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
6062         pci_read_config_word(pdev, PCI_STATUS, &pci_status);
6063
6064         netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
6065                   pci_cmd, pci_status);
6066
6067         /*
6068          * The recovery sequence below admits a very elaborated explanation:
6069          * - it seems to work;
6070          * - I did not see what else could be done;
6071          * - it makes iop3xx happy.
6072          *
6073          * Feel free to adjust to your needs.
6074          */
6075         if (pdev->broken_parity_status)
6076                 pci_cmd &= ~PCI_COMMAND_PARITY;
6077         else
6078                 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
6079
6080         pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
6081
6082         pci_write_config_word(pdev, PCI_STATUS,
6083                 pci_status & (PCI_STATUS_DETECTED_PARITY |
6084                 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
6085                 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
6086
6087         rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6088 }
6089
6090 static void rtl_tx(struct net_device *dev, struct rtl8169_private *tp,
6091                    int budget)
6092 {
6093         unsigned int dirty_tx, tx_left, bytes_compl = 0, pkts_compl = 0;
6094
6095         dirty_tx = tp->dirty_tx;
6096         smp_rmb();
6097         tx_left = tp->cur_tx - dirty_tx;
6098
6099         while (tx_left > 0) {
6100                 unsigned int entry = dirty_tx % NUM_TX_DESC;
6101                 struct ring_info *tx_skb = tp->tx_skb + entry;
6102                 u32 status;
6103
6104                 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
6105                 if (status & DescOwn)
6106                         break;
6107
6108                 /* This barrier is needed to keep us from reading
6109                  * any other fields out of the Tx descriptor until
6110                  * we know the status of DescOwn
6111                  */
6112                 dma_rmb();
6113
6114                 rtl8169_unmap_tx_skb(tp_to_dev(tp), tx_skb,
6115                                      tp->TxDescArray + entry);
6116                 if (status & LastFrag) {
6117                         pkts_compl++;
6118                         bytes_compl += tx_skb->skb->len;
6119                         napi_consume_skb(tx_skb->skb, budget);
6120                         tx_skb->skb = NULL;
6121                 }
6122                 dirty_tx++;
6123                 tx_left--;
6124         }
6125
6126         if (tp->dirty_tx != dirty_tx) {
6127                 netdev_completed_queue(dev, pkts_compl, bytes_compl);
6128
6129                 u64_stats_update_begin(&tp->tx_stats.syncp);
6130                 tp->tx_stats.packets += pkts_compl;
6131                 tp->tx_stats.bytes += bytes_compl;
6132                 u64_stats_update_end(&tp->tx_stats.syncp);
6133
6134                 tp->dirty_tx = dirty_tx;
6135                 /* Sync with rtl8169_start_xmit:
6136                  * - publish dirty_tx ring index (write barrier)
6137                  * - refresh cur_tx ring index and queue status (read barrier)
6138                  * May the current thread miss the stopped queue condition,
6139                  * a racing xmit thread can only have a right view of the
6140                  * ring status.
6141                  */
6142                 smp_mb();
6143                 if (netif_queue_stopped(dev) &&
6144                     rtl_tx_slots_avail(tp, MAX_SKB_FRAGS)) {
6145                         netif_wake_queue(dev);
6146                 }
6147                 /*
6148                  * 8168 hack: TxPoll requests are lost when the Tx packets are
6149                  * too close. Let's kick an extra TxPoll request when a burst
6150                  * of start_xmit activity is detected (if it is not detected,
6151                  * it is slow enough). -- FR
6152                  */
6153                 if (tp->cur_tx != dirty_tx)
6154                         RTL_W8(tp, TxPoll, NPQ);
6155         }
6156 }
6157
6158 static inline int rtl8169_fragmented_frame(u32 status)
6159 {
6160         return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
6161 }
6162
6163 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
6164 {
6165         u32 status = opts1 & RxProtoMask;
6166
6167         if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
6168             ((status == RxProtoUDP) && !(opts1 & UDPFail)))
6169                 skb->ip_summed = CHECKSUM_UNNECESSARY;
6170         else
6171                 skb_checksum_none_assert(skb);
6172 }
6173
6174 static struct sk_buff *rtl8169_try_rx_copy(void *data,
6175                                            struct rtl8169_private *tp,
6176                                            int pkt_size,
6177                                            dma_addr_t addr)
6178 {
6179         struct sk_buff *skb;
6180         struct device *d = tp_to_dev(tp);
6181
6182         dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
6183         prefetch(data);
6184         skb = napi_alloc_skb(&tp->napi, pkt_size);
6185         if (skb)
6186                 skb_copy_to_linear_data(skb, data, pkt_size);
6187         dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
6188
6189         return skb;
6190 }
6191
6192 static int rtl_rx(struct net_device *dev, struct rtl8169_private *tp, u32 budget)
6193 {
6194         unsigned int cur_rx, rx_left;
6195         unsigned int count;
6196
6197         cur_rx = tp->cur_rx;
6198
6199         for (rx_left = min(budget, NUM_RX_DESC); rx_left > 0; rx_left--, cur_rx++) {
6200                 unsigned int entry = cur_rx % NUM_RX_DESC;
6201                 struct RxDesc *desc = tp->RxDescArray + entry;
6202                 u32 status;
6203
6204                 status = le32_to_cpu(desc->opts1);
6205                 if (status & DescOwn)
6206                         break;
6207
6208                 /* This barrier is needed to keep us from reading
6209                  * any other fields out of the Rx descriptor until
6210                  * we know the status of DescOwn
6211                  */
6212                 dma_rmb();
6213
6214                 if (unlikely(status & RxRES)) {
6215                         netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
6216                                    status);
6217                         dev->stats.rx_errors++;
6218                         if (status & (RxRWT | RxRUNT))
6219                                 dev->stats.rx_length_errors++;
6220                         if (status & RxCRC)
6221                                 dev->stats.rx_crc_errors++;
6222                         /* RxFOVF is a reserved bit on later chip versions */
6223                         if (tp->mac_version == RTL_GIGA_MAC_VER_01 &&
6224                             status & RxFOVF) {
6225                                 rtl_schedule_task(tp, RTL_FLAG_TASK_RESET_PENDING);
6226                                 dev->stats.rx_fifo_errors++;
6227                         } else if (status & (RxRUNT | RxCRC) &&
6228                                    !(status & RxRWT) &&
6229                                    dev->features & NETIF_F_RXALL) {
6230                                 goto process_pkt;
6231                         }
6232                 } else {
6233                         struct sk_buff *skb;
6234                         dma_addr_t addr;
6235                         int pkt_size;
6236
6237 process_pkt:
6238                         addr = le64_to_cpu(desc->addr);
6239                         if (likely(!(dev->features & NETIF_F_RXFCS)))
6240                                 pkt_size = (status & 0x00003fff) - 4;
6241                         else
6242                                 pkt_size = status & 0x00003fff;
6243
6244                         /*
6245                          * The driver does not support incoming fragmented
6246                          * frames. They are seen as a symptom of over-mtu
6247                          * sized frames.
6248                          */
6249                         if (unlikely(rtl8169_fragmented_frame(status))) {
6250                                 dev->stats.rx_dropped++;
6251                                 dev->stats.rx_length_errors++;
6252                                 goto release_descriptor;
6253                         }
6254
6255                         skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
6256                                                   tp, pkt_size, addr);
6257                         if (!skb) {
6258                                 dev->stats.rx_dropped++;
6259                                 goto release_descriptor;
6260                         }
6261
6262                         rtl8169_rx_csum(skb, status);
6263                         skb_put(skb, pkt_size);
6264                         skb->protocol = eth_type_trans(skb, dev);
6265
6266                         rtl8169_rx_vlan_tag(desc, skb);
6267
6268                         if (skb->pkt_type == PACKET_MULTICAST)
6269                                 dev->stats.multicast++;
6270
6271                         napi_gro_receive(&tp->napi, skb);
6272
6273                         u64_stats_update_begin(&tp->rx_stats.syncp);
6274                         tp->rx_stats.packets++;
6275                         tp->rx_stats.bytes += pkt_size;
6276                         u64_stats_update_end(&tp->rx_stats.syncp);
6277                 }
6278 release_descriptor:
6279                 desc->opts2 = 0;
6280                 rtl8169_mark_to_asic(desc);
6281         }
6282
6283         count = cur_rx - tp->cur_rx;
6284         tp->cur_rx = cur_rx;
6285
6286         return count;
6287 }
6288
6289 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
6290 {
6291         struct rtl8169_private *tp = dev_instance;
6292         u16 status = RTL_R16(tp, IntrStatus);
6293
6294         if (!tp->irq_enabled || status == 0xffff || !(status & tp->irq_mask))
6295                 return IRQ_NONE;
6296
6297         if (unlikely(status & SYSErr)) {
6298                 rtl8169_pcierr_interrupt(tp->dev);
6299                 goto out;
6300         }
6301
6302         if (status & LinkChg)
6303                 phy_mac_interrupt(tp->phydev);
6304
6305         if (unlikely(status & RxFIFOOver &&
6306             tp->mac_version == RTL_GIGA_MAC_VER_11)) {
6307                 netif_stop_queue(tp->dev);
6308                 /* XXX - Hack alert. See rtl_task(). */
6309                 set_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags);
6310         }
6311
6312         rtl_irq_disable(tp);
6313         napi_schedule_irqoff(&tp->napi);
6314 out:
6315         rtl_ack_events(tp, status);
6316
6317         return IRQ_HANDLED;
6318 }
6319
6320 static void rtl_task(struct work_struct *work)
6321 {
6322         static const struct {
6323                 int bitnr;
6324                 void (*action)(struct rtl8169_private *);
6325         } rtl_work[] = {
6326                 { RTL_FLAG_TASK_RESET_PENDING,  rtl_reset_work },
6327         };
6328         struct rtl8169_private *tp =
6329                 container_of(work, struct rtl8169_private, wk.work);
6330         struct net_device *dev = tp->dev;
6331         int i;
6332
6333         rtl_lock_work(tp);
6334
6335         if (!netif_running(dev) ||
6336             !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
6337                 goto out_unlock;
6338
6339         for (i = 0; i < ARRAY_SIZE(rtl_work); i++) {
6340                 bool pending;
6341
6342                 pending = test_and_clear_bit(rtl_work[i].bitnr, tp->wk.flags);
6343                 if (pending)
6344                         rtl_work[i].action(tp);
6345         }
6346
6347 out_unlock:
6348         rtl_unlock_work(tp);
6349 }
6350
6351 static int rtl8169_poll(struct napi_struct *napi, int budget)
6352 {
6353         struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
6354         struct net_device *dev = tp->dev;
6355         int work_done;
6356
6357         work_done = rtl_rx(dev, tp, (u32) budget);
6358
6359         rtl_tx(dev, tp, budget);
6360
6361         if (work_done < budget) {
6362                 napi_complete_done(napi, work_done);
6363                 rtl_irq_enable(tp);
6364         }
6365
6366         return work_done;
6367 }
6368
6369 static void rtl8169_rx_missed(struct net_device *dev)
6370 {
6371         struct rtl8169_private *tp = netdev_priv(dev);
6372
6373         if (tp->mac_version > RTL_GIGA_MAC_VER_06)
6374                 return;
6375
6376         dev->stats.rx_missed_errors += RTL_R32(tp, RxMissed) & 0xffffff;
6377         RTL_W32(tp, RxMissed, 0);
6378 }
6379
6380 static void r8169_phylink_handler(struct net_device *ndev)
6381 {
6382         struct rtl8169_private *tp = netdev_priv(ndev);
6383
6384         if (netif_carrier_ok(ndev)) {
6385                 rtl_link_chg_patch(tp);
6386                 pm_request_resume(&tp->pci_dev->dev);
6387         } else {
6388                 pm_runtime_idle(&tp->pci_dev->dev);
6389         }
6390
6391         if (net_ratelimit())
6392                 phy_print_status(tp->phydev);
6393 }
6394
6395 static int r8169_phy_connect(struct rtl8169_private *tp)
6396 {
6397         struct phy_device *phydev = tp->phydev;
6398         phy_interface_t phy_mode;
6399         int ret;
6400
6401         phy_mode = tp->supports_gmii ? PHY_INTERFACE_MODE_GMII :
6402                    PHY_INTERFACE_MODE_MII;
6403
6404         ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6405                                  phy_mode);
6406         if (ret)
6407                 return ret;
6408
6409         if (!tp->supports_gmii)
6410                 phy_set_max_speed(phydev, SPEED_100);
6411
6412         phy_support_asym_pause(phydev);
6413
6414         phy_attached_info(phydev);
6415
6416         return 0;
6417 }
6418
6419 static void rtl8169_down(struct net_device *dev)
6420 {
6421         struct rtl8169_private *tp = netdev_priv(dev);
6422
6423         phy_stop(tp->phydev);
6424
6425         napi_disable(&tp->napi);
6426         netif_stop_queue(dev);
6427
6428         rtl8169_hw_reset(tp);
6429         /*
6430          * At this point device interrupts can not be enabled in any function,
6431          * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task)
6432          * and napi is disabled (rtl8169_poll).
6433          */
6434         rtl8169_rx_missed(dev);
6435
6436         /* Give a racing hard_start_xmit a few cycles to complete. */
6437         synchronize_rcu();
6438
6439         rtl8169_tx_clear(tp);
6440
6441         rtl8169_rx_clear(tp);
6442
6443         rtl_pll_power_down(tp);
6444 }
6445
6446 static int rtl8169_close(struct net_device *dev)
6447 {
6448         struct rtl8169_private *tp = netdev_priv(dev);
6449         struct pci_dev *pdev = tp->pci_dev;
6450
6451         pm_runtime_get_sync(&pdev->dev);
6452
6453         /* Update counters before going down */
6454         rtl8169_update_counters(tp);
6455
6456         rtl_lock_work(tp);
6457         /* Clear all task flags */
6458         bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6459
6460         rtl8169_down(dev);
6461         rtl_unlock_work(tp);
6462
6463         cancel_work_sync(&tp->wk.work);
6464
6465         phy_disconnect(tp->phydev);
6466
6467         pci_free_irq(pdev, 0, tp);
6468
6469         dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6470                           tp->RxPhyAddr);
6471         dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6472                           tp->TxPhyAddr);
6473         tp->TxDescArray = NULL;
6474         tp->RxDescArray = NULL;
6475
6476         pm_runtime_put_sync(&pdev->dev);
6477
6478         return 0;
6479 }
6480
6481 #ifdef CONFIG_NET_POLL_CONTROLLER
6482 static void rtl8169_netpoll(struct net_device *dev)
6483 {
6484         struct rtl8169_private *tp = netdev_priv(dev);
6485
6486         rtl8169_interrupt(pci_irq_vector(tp->pci_dev, 0), tp);
6487 }
6488 #endif
6489
6490 static int rtl_open(struct net_device *dev)
6491 {
6492         struct rtl8169_private *tp = netdev_priv(dev);
6493         struct pci_dev *pdev = tp->pci_dev;
6494         int retval = -ENOMEM;
6495
6496         pm_runtime_get_sync(&pdev->dev);
6497
6498         /*
6499          * Rx and Tx descriptors needs 256 bytes alignment.
6500          * dma_alloc_coherent provides more.
6501          */
6502         tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
6503                                              &tp->TxPhyAddr, GFP_KERNEL);
6504         if (!tp->TxDescArray)
6505                 goto err_pm_runtime_put;
6506
6507         tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
6508                                              &tp->RxPhyAddr, GFP_KERNEL);
6509         if (!tp->RxDescArray)
6510                 goto err_free_tx_0;
6511
6512         retval = rtl8169_init_ring(tp);
6513         if (retval < 0)
6514                 goto err_free_rx_1;
6515
6516         rtl_request_firmware(tp);
6517
6518         retval = pci_request_irq(pdev, 0, rtl8169_interrupt, NULL, tp,
6519                                  dev->name);
6520         if (retval < 0)
6521                 goto err_release_fw_2;
6522
6523         retval = r8169_phy_connect(tp);
6524         if (retval)
6525                 goto err_free_irq;
6526
6527         rtl_lock_work(tp);
6528
6529         set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6530
6531         napi_enable(&tp->napi);
6532
6533         rtl8169_init_phy(dev, tp);
6534
6535         rtl_pll_power_up(tp);
6536
6537         rtl_hw_start(tp);
6538
6539         if (!rtl8169_init_counter_offsets(tp))
6540                 netif_warn(tp, hw, dev, "counter reset/update failed\n");
6541
6542         phy_start(tp->phydev);
6543         netif_start_queue(dev);
6544
6545         rtl_unlock_work(tp);
6546
6547         pm_runtime_put_sync(&pdev->dev);
6548 out:
6549         return retval;
6550
6551 err_free_irq:
6552         pci_free_irq(pdev, 0, tp);
6553 err_release_fw_2:
6554         rtl_release_firmware(tp);
6555         rtl8169_rx_clear(tp);
6556 err_free_rx_1:
6557         dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
6558                           tp->RxPhyAddr);
6559         tp->RxDescArray = NULL;
6560 err_free_tx_0:
6561         dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
6562                           tp->TxPhyAddr);
6563         tp->TxDescArray = NULL;
6564 err_pm_runtime_put:
6565         pm_runtime_put_noidle(&pdev->dev);
6566         goto out;
6567 }
6568
6569 static void
6570 rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6571 {
6572         struct rtl8169_private *tp = netdev_priv(dev);
6573         struct pci_dev *pdev = tp->pci_dev;
6574         struct rtl8169_counters *counters = tp->counters;
6575         unsigned int start;
6576
6577         pm_runtime_get_noresume(&pdev->dev);
6578
6579         if (netif_running(dev) && pm_runtime_active(&pdev->dev))
6580                 rtl8169_rx_missed(dev);
6581
6582         do {
6583                 start = u64_stats_fetch_begin_irq(&tp->rx_stats.syncp);
6584                 stats->rx_packets = tp->rx_stats.packets;
6585                 stats->rx_bytes = tp->rx_stats.bytes;
6586         } while (u64_stats_fetch_retry_irq(&tp->rx_stats.syncp, start));
6587
6588         do {
6589                 start = u64_stats_fetch_begin_irq(&tp->tx_stats.syncp);
6590                 stats->tx_packets = tp->tx_stats.packets;
6591                 stats->tx_bytes = tp->tx_stats.bytes;
6592         } while (u64_stats_fetch_retry_irq(&tp->tx_stats.syncp, start));
6593
6594         stats->rx_dropped       = dev->stats.rx_dropped;
6595         stats->tx_dropped       = dev->stats.tx_dropped;
6596         stats->rx_length_errors = dev->stats.rx_length_errors;
6597         stats->rx_errors        = dev->stats.rx_errors;
6598         stats->rx_crc_errors    = dev->stats.rx_crc_errors;
6599         stats->rx_fifo_errors   = dev->stats.rx_fifo_errors;
6600         stats->rx_missed_errors = dev->stats.rx_missed_errors;
6601         stats->multicast        = dev->stats.multicast;
6602
6603         /*
6604          * Fetch additonal counter values missing in stats collected by driver
6605          * from tally counters.
6606          */
6607         if (pm_runtime_active(&pdev->dev))
6608                 rtl8169_update_counters(tp);
6609
6610         /*
6611          * Subtract values fetched during initalization.
6612          * See rtl8169_init_counter_offsets for a description why we do that.
6613          */
6614         stats->tx_errors = le64_to_cpu(counters->tx_errors) -
6615                 le64_to_cpu(tp->tc_offset.tx_errors);
6616         stats->collisions = le32_to_cpu(counters->tx_multi_collision) -
6617                 le32_to_cpu(tp->tc_offset.tx_multi_collision);
6618         stats->tx_aborted_errors = le16_to_cpu(counters->tx_aborted) -
6619                 le16_to_cpu(tp->tc_offset.tx_aborted);
6620
6621         pm_runtime_put_noidle(&pdev->dev);
6622 }
6623
6624 static void rtl8169_net_suspend(struct net_device *dev)
6625 {
6626         struct rtl8169_private *tp = netdev_priv(dev);
6627
6628         if (!netif_running(dev))
6629                 return;
6630
6631         phy_stop(tp->phydev);
6632         netif_device_detach(dev);
6633
6634         rtl_lock_work(tp);
6635         napi_disable(&tp->napi);
6636         /* Clear all task flags */
6637         bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);
6638
6639         rtl_unlock_work(tp);
6640
6641         rtl_pll_power_down(tp);
6642 }
6643
6644 #ifdef CONFIG_PM
6645
6646 static int rtl8169_suspend(struct device *device)
6647 {
6648         struct net_device *dev = dev_get_drvdata(device);
6649         struct rtl8169_private *tp = netdev_priv(dev);
6650
6651         rtl8169_net_suspend(dev);
6652         clk_disable_unprepare(tp->clk);
6653
6654         return 0;
6655 }
6656
6657 static void __rtl8169_resume(struct net_device *dev)
6658 {
6659         struct rtl8169_private *tp = netdev_priv(dev);
6660
6661         netif_device_attach(dev);
6662
6663         rtl_pll_power_up(tp);
6664         rtl8169_init_phy(dev, tp);
6665
6666         phy_start(tp->phydev);
6667
6668         rtl_lock_work(tp);
6669         napi_enable(&tp->napi);
6670         set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
6671         rtl_reset_work(tp);
6672         rtl_unlock_work(tp);
6673 }
6674
6675 static int rtl8169_resume(struct device *device)
6676 {
6677         struct net_device *dev = dev_get_drvdata(device);
6678         struct rtl8169_private *tp = netdev_priv(dev);
6679
6680         clk_prepare_enable(tp->clk);
6681
6682         if (netif_running(dev))
6683                 __rtl8169_resume(dev);
6684
6685         return 0;
6686 }
6687
6688 static int rtl8169_runtime_suspend(struct device *device)
6689 {
6690         struct net_device *dev = dev_get_drvdata(device);
6691         struct rtl8169_private *tp = netdev_priv(dev);
6692
6693         if (!tp->TxDescArray)
6694                 return 0;
6695
6696         rtl_lock_work(tp);
6697         __rtl8169_set_wol(tp, WAKE_ANY);
6698         rtl_unlock_work(tp);
6699
6700         rtl8169_net_suspend(dev);
6701
6702         /* Update counters before going runtime suspend */
6703         rtl8169_rx_missed(dev);
6704         rtl8169_update_counters(tp);
6705
6706         return 0;
6707 }
6708
6709 static int rtl8169_runtime_resume(struct device *device)
6710 {
6711         struct net_device *dev = dev_get_drvdata(device);
6712         struct rtl8169_private *tp = netdev_priv(dev);
6713         rtl_rar_set(tp, dev->dev_addr);
6714
6715         if (!tp->TxDescArray)
6716                 return 0;
6717
6718         rtl_lock_work(tp);
6719         __rtl8169_set_wol(tp, tp->saved_wolopts);
6720         rtl_unlock_work(tp);
6721
6722         __rtl8169_resume(dev);
6723
6724         return 0;
6725 }
6726
6727 static int rtl8169_runtime_idle(struct device *device)
6728 {
6729         struct net_device *dev = dev_get_drvdata(device);
6730
6731         if (!netif_running(dev) || !netif_carrier_ok(dev))
6732                 pm_schedule_suspend(device, 10000);
6733
6734         return -EBUSY;
6735 }
6736
6737 static const struct dev_pm_ops rtl8169_pm_ops = {
6738         .suspend                = rtl8169_suspend,
6739         .resume                 = rtl8169_resume,
6740         .freeze                 = rtl8169_suspend,
6741         .thaw                   = rtl8169_resume,
6742         .poweroff               = rtl8169_suspend,
6743         .restore                = rtl8169_resume,
6744         .runtime_suspend        = rtl8169_runtime_suspend,
6745         .runtime_resume         = rtl8169_runtime_resume,
6746         .runtime_idle           = rtl8169_runtime_idle,
6747 };
6748
6749 #define RTL8169_PM_OPS  (&rtl8169_pm_ops)
6750
6751 #else /* !CONFIG_PM */
6752
6753 #define RTL8169_PM_OPS  NULL
6754
6755 #endif /* !CONFIG_PM */
6756
6757 static void rtl_wol_shutdown_quirk(struct rtl8169_private *tp)
6758 {
6759         /* WoL fails with 8168b when the receiver is disabled. */
6760         switch (tp->mac_version) {
6761         case RTL_GIGA_MAC_VER_11:
6762         case RTL_GIGA_MAC_VER_12:
6763         case RTL_GIGA_MAC_VER_17:
6764                 pci_clear_master(tp->pci_dev);
6765
6766                 RTL_W8(tp, ChipCmd, CmdRxEnb);
6767                 /* PCI commit */
6768                 RTL_R8(tp, ChipCmd);
6769                 break;
6770         default:
6771                 break;
6772         }
6773 }
6774
6775 static void rtl_shutdown(struct pci_dev *pdev)
6776 {
6777         struct net_device *dev = pci_get_drvdata(pdev);
6778         struct rtl8169_private *tp = netdev_priv(dev);
6779
6780         rtl8169_net_suspend(dev);
6781
6782         /* Restore original MAC address */
6783         rtl_rar_set(tp, dev->perm_addr);
6784
6785         rtl8169_hw_reset(tp);
6786
6787         if (system_state == SYSTEM_POWER_OFF) {
6788                 if (tp->saved_wolopts) {
6789                         rtl_wol_suspend_quirk(tp);
6790                         rtl_wol_shutdown_quirk(tp);
6791                 }
6792
6793                 pci_wake_from_d3(pdev, true);
6794                 pci_set_power_state(pdev, PCI_D3hot);
6795         }
6796 }
6797
6798 static void rtl_remove_one(struct pci_dev *pdev)
6799 {
6800         struct net_device *dev = pci_get_drvdata(pdev);
6801         struct rtl8169_private *tp = netdev_priv(dev);
6802
6803         if (r8168_check_dash(tp))
6804                 rtl8168_driver_stop(tp);
6805
6806         netif_napi_del(&tp->napi);
6807
6808         unregister_netdev(dev);
6809         mdiobus_unregister(tp->phydev->mdio.bus);
6810
6811         rtl_release_firmware(tp);
6812
6813         if (pci_dev_run_wake(pdev))
6814                 pm_runtime_get_noresume(&pdev->dev);
6815
6816         /* restore original MAC address */
6817         rtl_rar_set(tp, dev->perm_addr);
6818 }
6819
6820 static const struct net_device_ops rtl_netdev_ops = {
6821         .ndo_open               = rtl_open,
6822         .ndo_stop               = rtl8169_close,
6823         .ndo_get_stats64        = rtl8169_get_stats64,
6824         .ndo_start_xmit         = rtl8169_start_xmit,
6825         .ndo_tx_timeout         = rtl8169_tx_timeout,
6826         .ndo_validate_addr      = eth_validate_addr,
6827         .ndo_change_mtu         = rtl8169_change_mtu,
6828         .ndo_fix_features       = rtl8169_fix_features,
6829         .ndo_set_features       = rtl8169_set_features,
6830         .ndo_set_mac_address    = rtl_set_mac_address,
6831         .ndo_do_ioctl           = rtl8169_ioctl,
6832         .ndo_set_rx_mode        = rtl_set_rx_mode,
6833 #ifdef CONFIG_NET_POLL_CONTROLLER
6834         .ndo_poll_controller    = rtl8169_netpoll,
6835 #endif
6836
6837 };
6838
6839 static const struct rtl_cfg_info {
6840         void (*hw_start)(struct rtl8169_private *tp);
6841         u16 irq_mask;
6842         unsigned int has_gmii:1;
6843         const struct rtl_coalesce_info *coalesce_info;
6844 } rtl_cfg_infos [] = {
6845         [RTL_CFG_0] = {
6846                 .hw_start       = rtl_hw_start_8169,
6847                 .irq_mask       = SYSErr | LinkChg | RxOverflow | RxFIFOOver,
6848                 .has_gmii       = 1,
6849                 .coalesce_info  = rtl_coalesce_info_8169,
6850         },
6851         [RTL_CFG_1] = {
6852                 .hw_start       = rtl_hw_start_8168,
6853                 .irq_mask       = LinkChg | RxOverflow,
6854                 .has_gmii       = 1,
6855                 .coalesce_info  = rtl_coalesce_info_8168_8136,
6856         },
6857         [RTL_CFG_2] = {
6858                 .hw_start       = rtl_hw_start_8101,
6859                 .irq_mask       = LinkChg | RxOverflow | RxFIFOOver,
6860                 .coalesce_info  = rtl_coalesce_info_8168_8136,
6861         }
6862 };
6863
6864 static int rtl_alloc_irq(struct rtl8169_private *tp)
6865 {
6866         unsigned int flags;
6867
6868         if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
6869                 rtl_unlock_config_regs(tp);
6870                 RTL_W8(tp, Config2, RTL_R8(tp, Config2) & ~MSIEnable);
6871                 rtl_lock_config_regs(tp);
6872                 flags = PCI_IRQ_LEGACY;
6873         } else {
6874                 flags = PCI_IRQ_ALL_TYPES;
6875         }
6876
6877         return pci_alloc_irq_vectors(tp->pci_dev, 1, 1, flags);
6878 }
6879
6880 static void rtl_read_mac_address(struct rtl8169_private *tp,
6881                                  u8 mac_addr[ETH_ALEN])
6882 {
6883         u32 value;
6884
6885         /* Get MAC address */
6886         switch (tp->mac_version) {
6887         case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
6888         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
6889                 value = rtl_eri_read(tp, 0xe0);
6890                 mac_addr[0] = (value >>  0) & 0xff;
6891                 mac_addr[1] = (value >>  8) & 0xff;
6892                 mac_addr[2] = (value >> 16) & 0xff;
6893                 mac_addr[3] = (value >> 24) & 0xff;
6894
6895                 value = rtl_eri_read(tp, 0xe4);
6896                 mac_addr[4] = (value >>  0) & 0xff;
6897                 mac_addr[5] = (value >>  8) & 0xff;
6898                 break;
6899         default:
6900                 break;
6901         }
6902 }
6903
6904 DECLARE_RTL_COND(rtl_link_list_ready_cond)
6905 {
6906         return RTL_R8(tp, MCU) & LINK_LIST_RDY;
6907 }
6908
6909 DECLARE_RTL_COND(rtl_rxtx_empty_cond)
6910 {
6911         return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
6912 }
6913
6914 static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
6915 {
6916         struct rtl8169_private *tp = mii_bus->priv;
6917
6918         if (phyaddr > 0)
6919                 return -ENODEV;
6920
6921         return rtl_readphy(tp, phyreg);
6922 }
6923
6924 static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
6925                                 int phyreg, u16 val)
6926 {
6927         struct rtl8169_private *tp = mii_bus->priv;
6928
6929         if (phyaddr > 0)
6930                 return -ENODEV;
6931
6932         rtl_writephy(tp, phyreg, val);
6933
6934         return 0;
6935 }
6936
6937 static int r8169_mdio_register(struct rtl8169_private *tp)
6938 {
6939         struct pci_dev *pdev = tp->pci_dev;
6940         struct mii_bus *new_bus;
6941         int ret;
6942
6943         new_bus = devm_mdiobus_alloc(&pdev->dev);
6944         if (!new_bus)
6945                 return -ENOMEM;
6946
6947         new_bus->name = "r8169";
6948         new_bus->priv = tp;
6949         new_bus->parent = &pdev->dev;
6950         new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
6951         snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x", pci_dev_id(pdev));
6952
6953         new_bus->read = r8169_mdio_read_reg;
6954         new_bus->write = r8169_mdio_write_reg;
6955
6956         ret = mdiobus_register(new_bus);
6957         if (ret)
6958                 return ret;
6959
6960         tp->phydev = mdiobus_get_phy(new_bus, 0);
6961         if (!tp->phydev) {
6962                 mdiobus_unregister(new_bus);
6963                 return -ENODEV;
6964         }
6965
6966         /* PHY will be woken up in rtl_open() */
6967         phy_suspend(tp->phydev);
6968
6969         return 0;
6970 }
6971
6972 static void rtl_hw_init_8168g(struct rtl8169_private *tp)
6973 {
6974         u32 data;
6975
6976         tp->ocp_base = OCP_STD_PHY_BASE;
6977
6978         RTL_W32(tp, MISC, RTL_R32(tp, MISC) | RXDV_GATED_EN);
6979
6980         if (!rtl_udelay_loop_wait_high(tp, &rtl_txcfg_empty_cond, 100, 42))
6981                 return;
6982
6983         if (!rtl_udelay_loop_wait_high(tp, &rtl_rxtx_empty_cond, 100, 42))
6984                 return;
6985
6986         RTL_W8(tp, ChipCmd, RTL_R8(tp, ChipCmd) & ~(CmdTxEnb | CmdRxEnb));
6987         msleep(1);
6988         RTL_W8(tp, MCU, RTL_R8(tp, MCU) & ~NOW_IS_OOB);
6989
6990         data = r8168_mac_ocp_read(tp, 0xe8de);
6991         data &= ~(1 << 14);
6992         r8168_mac_ocp_write(tp, 0xe8de, data);
6993
6994         if (!rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42))
6995                 return;
6996
6997         data = r8168_mac_ocp_read(tp, 0xe8de);
6998         data |= (1 << 15);
6999         r8168_mac_ocp_write(tp, 0xe8de, data);
7000
7001         rtl_udelay_loop_wait_high(tp, &rtl_link_list_ready_cond, 100, 42);
7002 }
7003
7004 static void rtl_hw_initialize(struct rtl8169_private *tp)
7005 {
7006         switch (tp->mac_version) {
7007         case RTL_GIGA_MAC_VER_49 ... RTL_GIGA_MAC_VER_51:
7008                 rtl8168ep_stop_cmac(tp);
7009                 /* fall through */
7010         case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_48:
7011                 rtl_hw_init_8168g(tp);
7012                 break;
7013         default:
7014                 break;
7015         }
7016 }
7017
7018 /* Versions RTL8102e and from RTL8168c onwards support csum_v2 */
7019 static bool rtl_chip_supports_csum_v2(struct rtl8169_private *tp)
7020 {
7021         switch (tp->mac_version) {
7022         case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7023         case RTL_GIGA_MAC_VER_10 ... RTL_GIGA_MAC_VER_17:
7024                 return false;
7025         default:
7026                 return true;
7027         }
7028 }
7029
7030 static int rtl_jumbo_max(struct rtl8169_private *tp)
7031 {
7032         /* Non-GBit versions don't support jumbo frames */
7033         if (!tp->supports_gmii)
7034                 return JUMBO_1K;
7035
7036         switch (tp->mac_version) {
7037         /* RTL8169 */
7038         case RTL_GIGA_MAC_VER_01 ... RTL_GIGA_MAC_VER_06:
7039                 return JUMBO_7K;
7040         /* RTL8168b */
7041         case RTL_GIGA_MAC_VER_11:
7042         case RTL_GIGA_MAC_VER_12:
7043         case RTL_GIGA_MAC_VER_17:
7044                 return JUMBO_4K;
7045         /* RTL8168c */
7046         case RTL_GIGA_MAC_VER_18 ... RTL_GIGA_MAC_VER_24:
7047                 return JUMBO_6K;
7048         default:
7049                 return JUMBO_9K;
7050         }
7051 }
7052
7053 static void rtl_disable_clk(void *data)
7054 {
7055         clk_disable_unprepare(data);
7056 }
7057
7058 static int rtl_get_ether_clk(struct rtl8169_private *tp)
7059 {
7060         struct device *d = tp_to_dev(tp);
7061         struct clk *clk;
7062         int rc;
7063
7064         clk = devm_clk_get(d, "ether_clk");
7065         if (IS_ERR(clk)) {
7066                 rc = PTR_ERR(clk);
7067                 if (rc == -ENOENT)
7068                         /* clk-core allows NULL (for suspend / resume) */
7069                         rc = 0;
7070                 else if (rc != -EPROBE_DEFER)
7071                         dev_err(d, "failed to get clk: %d\n", rc);
7072         } else {
7073                 tp->clk = clk;
7074                 rc = clk_prepare_enable(clk);
7075                 if (rc)
7076                         dev_err(d, "failed to enable clk: %d\n", rc);
7077                 else
7078                         rc = devm_add_action_or_reset(d, rtl_disable_clk, clk);
7079         }
7080
7081         return rc;
7082 }
7083
7084 static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
7085 {
7086         const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
7087         /* align to u16 for is_valid_ether_addr() */
7088         u8 mac_addr[ETH_ALEN] __aligned(2) = {};
7089         struct rtl8169_private *tp;
7090         struct net_device *dev;
7091         int chipset, region, i;
7092         int jumbo_max, rc;
7093
7094         dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
7095         if (!dev)
7096                 return -ENOMEM;
7097
7098         SET_NETDEV_DEV(dev, &pdev->dev);
7099         dev->netdev_ops = &rtl_netdev_ops;
7100         tp = netdev_priv(dev);
7101         tp->dev = dev;
7102         tp->pci_dev = pdev;
7103         tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
7104         tp->supports_gmii = cfg->has_gmii;
7105
7106         /* Get the *optional* external "ether_clk" used on some boards */
7107         rc = rtl_get_ether_clk(tp);
7108         if (rc)
7109                 return rc;
7110
7111         /* Disable ASPM completely as that cause random device stop working
7112          * problems as well as full system hangs for some PCIe devices users.
7113          */
7114         pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1);
7115
7116         /* enable device (incl. PCI PM wakeup and hotplug setup) */
7117         rc = pcim_enable_device(pdev);
7118         if (rc < 0) {
7119                 dev_err(&pdev->dev, "enable failure\n");
7120                 return rc;
7121         }
7122
7123         if (pcim_set_mwi(pdev) < 0)
7124                 dev_info(&pdev->dev, "Mem-Wr-Inval unavailable\n");
7125
7126         /* use first MMIO region */
7127         region = ffs(pci_select_bars(pdev, IORESOURCE_MEM)) - 1;
7128         if (region < 0) {
7129                 dev_err(&pdev->dev, "no MMIO resource found\n");
7130                 return -ENODEV;
7131         }
7132
7133         /* check for weird/broken PCI region reporting */
7134         if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
7135                 dev_err(&pdev->dev, "Invalid PCI region size(s), aborting\n");
7136                 return -ENODEV;
7137         }
7138
7139         rc = pcim_iomap_regions(pdev, BIT(region), MODULENAME);
7140         if (rc < 0) {
7141                 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
7142                 return rc;
7143         }
7144
7145         tp->mmio_addr = pcim_iomap_table(pdev)[region];
7146
7147         /* Identify chip attached to board */
7148         rtl8169_get_mac_version(tp);
7149         if (tp->mac_version == RTL_GIGA_MAC_NONE)
7150                 return -ENODEV;
7151
7152         if (rtl_tbi_enabled(tp)) {
7153                 dev_err(&pdev->dev, "TBI fiber mode not supported\n");
7154                 return -ENODEV;
7155         }
7156
7157         tp->cp_cmd = RTL_R16(tp, CPlusCmd);
7158
7159         if (sizeof(dma_addr_t) > 4 && tp->mac_version >= RTL_GIGA_MAC_VER_18 &&
7160             !dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
7161                 dev->features |= NETIF_F_HIGHDMA;
7162         } else {
7163                 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
7164                 if (rc < 0) {
7165                         dev_err(&pdev->dev, "DMA configuration failed\n");
7166                         return rc;
7167                 }
7168         }
7169
7170         rtl_init_rxcfg(tp);
7171
7172         rtl8169_irq_mask_and_ack(tp);
7173
7174         rtl_hw_initialize(tp);
7175
7176         rtl_hw_reset(tp);
7177
7178         pci_set_master(pdev);
7179
7180         rtl_init_mdio_ops(tp);
7181         rtl_init_jumbo_ops(tp);
7182
7183         chipset = tp->mac_version;
7184
7185         rc = rtl_alloc_irq(tp);
7186         if (rc < 0) {
7187                 dev_err(&pdev->dev, "Can't allocate interrupt\n");
7188                 return rc;
7189         }
7190
7191         mutex_init(&tp->wk.mutex);
7192         INIT_WORK(&tp->wk.work, rtl_task);
7193         u64_stats_init(&tp->rx_stats.syncp);
7194         u64_stats_init(&tp->tx_stats.syncp);
7195
7196         /* get MAC address */
7197         rc = eth_platform_get_mac_address(&pdev->dev, mac_addr);
7198         if (rc)
7199                 rtl_read_mac_address(tp, mac_addr);
7200
7201         if (is_valid_ether_addr(mac_addr))
7202                 rtl_rar_set(tp, mac_addr);
7203
7204         for (i = 0; i < ETH_ALEN; i++)
7205                 dev->dev_addr[i] = RTL_R8(tp, MAC0 + i);
7206
7207         dev->ethtool_ops = &rtl8169_ethtool_ops;
7208
7209         netif_napi_add(dev, &tp->napi, rtl8169_poll, NAPI_POLL_WEIGHT);
7210
7211         /* don't enable SG, IP_CSUM and TSO by default - it might not work
7212          * properly for all devices */
7213         dev->features |= NETIF_F_RXCSUM |
7214                 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
7215
7216         dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7217                 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX |
7218                 NETIF_F_HW_VLAN_CTAG_RX;
7219         dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
7220                 NETIF_F_HIGHDMA;
7221         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
7222
7223         tp->cp_cmd |= RxChkSum | RxVlan;
7224
7225         /*
7226          * Pretend we are using VLANs; This bypasses a nasty bug where
7227          * Interrupts stop flowing on high load on 8110SCd controllers.
7228          */
7229         if (tp->mac_version == RTL_GIGA_MAC_VER_05)
7230                 /* Disallow toggling */
7231                 dev->hw_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
7232
7233         if (rtl_chip_supports_csum_v2(tp)) {
7234                 tp->tso_csum = rtl8169_tso_csum_v2;
7235                 dev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
7236         } else {
7237                 tp->tso_csum = rtl8169_tso_csum_v1;
7238         }
7239
7240         dev->hw_features |= NETIF_F_RXALL;
7241         dev->hw_features |= NETIF_F_RXFCS;
7242
7243         /* MTU range: 60 - hw-specific max */
7244         dev->min_mtu = ETH_ZLEN;
7245         jumbo_max = rtl_jumbo_max(tp);
7246         dev->max_mtu = jumbo_max;
7247
7248         tp->hw_start = cfg->hw_start;
7249         tp->irq_mask = RTL_EVENT_NAPI | cfg->irq_mask;
7250         tp->coalesce_info = cfg->coalesce_info;
7251
7252         tp->fw_name = rtl_chip_infos[chipset].fw_name;
7253
7254         tp->counters = dmam_alloc_coherent (&pdev->dev, sizeof(*tp->counters),
7255                                             &tp->counters_phys_addr,
7256                                             GFP_KERNEL);
7257         if (!tp->counters)
7258                 return -ENOMEM;
7259
7260         pci_set_drvdata(pdev, dev);
7261
7262         rc = r8169_mdio_register(tp);
7263         if (rc)
7264                 return rc;
7265
7266         /* chip gets powered up in rtl_open() */
7267         rtl_pll_power_down(tp);
7268
7269         rc = register_netdev(dev);
7270         if (rc)
7271                 goto err_mdio_unregister;
7272
7273         netif_info(tp, probe, dev, "%s, %pM, XID %03x, IRQ %d\n",
7274                    rtl_chip_infos[chipset].name, dev->dev_addr,
7275                    (RTL_R32(tp, TxConfig) >> 20) & 0xfcf,
7276                    pci_irq_vector(pdev, 0));
7277
7278         if (jumbo_max > JUMBO_1K)
7279                 netif_info(tp, probe, dev,
7280                            "jumbo features [frames: %d bytes, tx checksumming: %s]\n",
7281                            jumbo_max, tp->mac_version <= RTL_GIGA_MAC_VER_06 ?
7282                            "ok" : "ko");
7283
7284         if (r8168_check_dash(tp))
7285                 rtl8168_driver_start(tp);
7286
7287         if (pci_dev_run_wake(pdev))
7288                 pm_runtime_put_sync(&pdev->dev);
7289
7290         return 0;
7291
7292 err_mdio_unregister:
7293         mdiobus_unregister(tp->phydev->mdio.bus);
7294         return rc;
7295 }
7296
7297 static struct pci_driver rtl8169_pci_driver = {
7298         .name           = MODULENAME,
7299         .id_table       = rtl8169_pci_tbl,
7300         .probe          = rtl_init_one,
7301         .remove         = rtl_remove_one,
7302         .shutdown       = rtl_shutdown,
7303         .driver.pm      = RTL8169_PM_OPS,
7304 };
7305
7306 module_pci_driver(rtl8169_pci_driver);