r8169: check firmware content sooner.
[cascardo/linux.git] / drivers / net / r8169.c
1 /*
2  * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3  *
4  * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5  * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6  * Copyright (c) a lot of people too. Please respect their work.
7  *
8  * See MAINTAINERS file for support contact information.
9  */
10
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/pci.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/delay.h>
17 #include <linux/ethtool.h>
18 #include <linux/mii.h>
19 #include <linux/if_vlan.h>
20 #include <linux/crc32.h>
21 #include <linux/in.h>
22 #include <linux/ip.h>
23 #include <linux/tcp.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/firmware.h>
29 #include <linux/pci-aspm.h>
30 #include <linux/prefetch.h>
31
32 #include <asm/system.h>
33 #include <asm/io.h>
34 #include <asm/irq.h>
35
36 #define RTL8169_VERSION "2.3LK-NAPI"
37 #define MODULENAME "r8169"
38 #define PFX MODULENAME ": "
39
40 #define FIRMWARE_8168D_1        "rtl_nic/rtl8168d-1.fw"
41 #define FIRMWARE_8168D_2        "rtl_nic/rtl8168d-2.fw"
42 #define FIRMWARE_8168E_1        "rtl_nic/rtl8168e-1.fw"
43 #define FIRMWARE_8168E_2        "rtl_nic/rtl8168e-2.fw"
44 #define FIRMWARE_8105E_1        "rtl_nic/rtl8105e-1.fw"
45
46 #ifdef RTL8169_DEBUG
47 #define assert(expr) \
48         if (!(expr)) {                                  \
49                 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
50                 #expr,__FILE__,__func__,__LINE__);              \
51         }
52 #define dprintk(fmt, args...) \
53         do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
54 #else
55 #define assert(expr) do {} while (0)
56 #define dprintk(fmt, args...)   do {} while (0)
57 #endif /* RTL8169_DEBUG */
58
59 #define R8169_MSG_DEFAULT \
60         (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
61
62 #define TX_BUFFS_AVAIL(tp) \
63         (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
64
65 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
66    The RTL chips use a 64 element hash table based on the Ethernet CRC. */
67 static const int multicast_filter_limit = 32;
68
69 /* MAC address length */
70 #define MAC_ADDR_LEN    6
71
72 #define MAX_READ_REQUEST_SHIFT  12
73 #define RX_FIFO_THRESH  7       /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
74 #define RX_DMA_BURST    6       /* Maximum PCI burst, '6' is 1024 */
75 #define TX_DMA_BURST    6       /* Maximum PCI burst, '6' is 1024 */
76 #define SafeMtu         0x1c20  /* ... actually life sucks beyond ~7k */
77 #define InterFrameGap   0x03    /* 3 means InterFrameGap = the shortest one */
78
79 #define R8169_REGS_SIZE         256
80 #define R8169_NAPI_WEIGHT       64
81 #define NUM_TX_DESC     64      /* Number of Tx descriptor registers */
82 #define NUM_RX_DESC     256     /* Number of Rx descriptor registers */
83 #define RX_BUF_SIZE     1536    /* Rx Buffer size */
84 #define R8169_TX_RING_BYTES     (NUM_TX_DESC * sizeof(struct TxDesc))
85 #define R8169_RX_RING_BYTES     (NUM_RX_DESC * sizeof(struct RxDesc))
86
87 #define RTL8169_TX_TIMEOUT      (6*HZ)
88 #define RTL8169_PHY_TIMEOUT     (10*HZ)
89
90 #define RTL_EEPROM_SIG          cpu_to_le32(0x8129)
91 #define RTL_EEPROM_SIG_MASK     cpu_to_le32(0xffff)
92 #define RTL_EEPROM_SIG_ADDR     0x0000
93
94 /* write/read MMIO register */
95 #define RTL_W8(reg, val8)       writeb ((val8), ioaddr + (reg))
96 #define RTL_W16(reg, val16)     writew ((val16), ioaddr + (reg))
97 #define RTL_W32(reg, val32)     writel ((val32), ioaddr + (reg))
98 #define RTL_R8(reg)             readb (ioaddr + (reg))
99 #define RTL_R16(reg)            readw (ioaddr + (reg))
100 #define RTL_R32(reg)            readl (ioaddr + (reg))
101
102 enum mac_version {
103         RTL_GIGA_MAC_VER_01 = 0,
104         RTL_GIGA_MAC_VER_02,
105         RTL_GIGA_MAC_VER_03,
106         RTL_GIGA_MAC_VER_04,
107         RTL_GIGA_MAC_VER_05,
108         RTL_GIGA_MAC_VER_06,
109         RTL_GIGA_MAC_VER_07,
110         RTL_GIGA_MAC_VER_08,
111         RTL_GIGA_MAC_VER_09,
112         RTL_GIGA_MAC_VER_10,
113         RTL_GIGA_MAC_VER_11,
114         RTL_GIGA_MAC_VER_12,
115         RTL_GIGA_MAC_VER_13,
116         RTL_GIGA_MAC_VER_14,
117         RTL_GIGA_MAC_VER_15,
118         RTL_GIGA_MAC_VER_16,
119         RTL_GIGA_MAC_VER_17,
120         RTL_GIGA_MAC_VER_18,
121         RTL_GIGA_MAC_VER_19,
122         RTL_GIGA_MAC_VER_20,
123         RTL_GIGA_MAC_VER_21,
124         RTL_GIGA_MAC_VER_22,
125         RTL_GIGA_MAC_VER_23,
126         RTL_GIGA_MAC_VER_24,
127         RTL_GIGA_MAC_VER_25,
128         RTL_GIGA_MAC_VER_26,
129         RTL_GIGA_MAC_VER_27,
130         RTL_GIGA_MAC_VER_28,
131         RTL_GIGA_MAC_VER_29,
132         RTL_GIGA_MAC_VER_30,
133         RTL_GIGA_MAC_VER_31,
134         RTL_GIGA_MAC_VER_32,
135         RTL_GIGA_MAC_VER_33,
136         RTL_GIGA_MAC_NONE   = 0xff,
137 };
138
139 enum rtl_tx_desc_version {
140         RTL_TD_0        = 0,
141         RTL_TD_1        = 1,
142 };
143
144 #define _R(NAME,TD,FW) \
145         { .name = NAME, .txd_version = TD, .fw_name = FW }
146
147 static const struct {
148         const char *name;
149         enum rtl_tx_desc_version txd_version;
150         const char *fw_name;
151 } rtl_chip_infos[] = {
152         /* PCI devices. */
153         [RTL_GIGA_MAC_VER_01] =
154                 _R("RTL8169",           RTL_TD_0, NULL),
155         [RTL_GIGA_MAC_VER_02] =
156                 _R("RTL8169s",          RTL_TD_0, NULL),
157         [RTL_GIGA_MAC_VER_03] =
158                 _R("RTL8110s",          RTL_TD_0, NULL),
159         [RTL_GIGA_MAC_VER_04] =
160                 _R("RTL8169sb/8110sb",  RTL_TD_0, NULL),
161         [RTL_GIGA_MAC_VER_05] =
162                 _R("RTL8169sc/8110sc",  RTL_TD_0, NULL),
163         [RTL_GIGA_MAC_VER_06] =
164                 _R("RTL8169sc/8110sc",  RTL_TD_0, NULL),
165         /* PCI-E devices. */
166         [RTL_GIGA_MAC_VER_07] =
167                 _R("RTL8102e",          RTL_TD_1, NULL),
168         [RTL_GIGA_MAC_VER_08] =
169                 _R("RTL8102e",          RTL_TD_1, NULL),
170         [RTL_GIGA_MAC_VER_09] =
171                 _R("RTL8102e",          RTL_TD_1, NULL),
172         [RTL_GIGA_MAC_VER_10] =
173                 _R("RTL8101e",          RTL_TD_0, NULL),
174         [RTL_GIGA_MAC_VER_11] =
175                 _R("RTL8168b/8111b",    RTL_TD_0, NULL),
176         [RTL_GIGA_MAC_VER_12] =
177                 _R("RTL8168b/8111b",    RTL_TD_0, NULL),
178         [RTL_GIGA_MAC_VER_13] =
179                 _R("RTL8101e",          RTL_TD_0, NULL),
180         [RTL_GIGA_MAC_VER_14] =
181                 _R("RTL8100e",          RTL_TD_0, NULL),
182         [RTL_GIGA_MAC_VER_15] =
183                 _R("RTL8100e",          RTL_TD_0, NULL),
184         [RTL_GIGA_MAC_VER_16] =
185                 _R("RTL8101e",          RTL_TD_0, NULL),
186         [RTL_GIGA_MAC_VER_17] =
187                 _R("RTL8168b/8111b",    RTL_TD_0, NULL),
188         [RTL_GIGA_MAC_VER_18] =
189                 _R("RTL8168cp/8111cp",  RTL_TD_1, NULL),
190         [RTL_GIGA_MAC_VER_19] =
191                 _R("RTL8168c/8111c",    RTL_TD_1, NULL),
192         [RTL_GIGA_MAC_VER_20] =
193                 _R("RTL8168c/8111c",    RTL_TD_1, NULL),
194         [RTL_GIGA_MAC_VER_21] =
195                 _R("RTL8168c/8111c",    RTL_TD_1, NULL),
196         [RTL_GIGA_MAC_VER_22] =
197                 _R("RTL8168c/8111c",    RTL_TD_1, NULL),
198         [RTL_GIGA_MAC_VER_23] =
199                 _R("RTL8168cp/8111cp",  RTL_TD_1, NULL),
200         [RTL_GIGA_MAC_VER_24] =
201                 _R("RTL8168cp/8111cp",  RTL_TD_1, NULL),
202         [RTL_GIGA_MAC_VER_25] =
203                 _R("RTL8168d/8111d",    RTL_TD_1, FIRMWARE_8168D_1),
204         [RTL_GIGA_MAC_VER_26] =
205                 _R("RTL8168d/8111d",    RTL_TD_1, FIRMWARE_8168D_2),
206         [RTL_GIGA_MAC_VER_27] =
207                 _R("RTL8168dp/8111dp",  RTL_TD_1, NULL),
208         [RTL_GIGA_MAC_VER_28] =
209                 _R("RTL8168dp/8111dp",  RTL_TD_1, NULL),
210         [RTL_GIGA_MAC_VER_29] =
211                 _R("RTL8105e",          RTL_TD_1, FIRMWARE_8105E_1),
212         [RTL_GIGA_MAC_VER_30] =
213                 _R("RTL8105e",          RTL_TD_1, FIRMWARE_8105E_1),
214         [RTL_GIGA_MAC_VER_31] =
215                 _R("RTL8168dp/8111dp",  RTL_TD_1, NULL),
216         [RTL_GIGA_MAC_VER_32] =
217                 _R("RTL8168e/8111e",    RTL_TD_1, FIRMWARE_8168E_1),
218         [RTL_GIGA_MAC_VER_33] =
219                 _R("RTL8168e/8111e",    RTL_TD_1, FIRMWARE_8168E_2)
220 };
221 #undef _R
222
223 enum cfg_version {
224         RTL_CFG_0 = 0x00,
225         RTL_CFG_1,
226         RTL_CFG_2
227 };
228
229 static void rtl_hw_start_8169(struct net_device *);
230 static void rtl_hw_start_8168(struct net_device *);
231 static void rtl_hw_start_8101(struct net_device *);
232
233 static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
234         { PCI_DEVICE(PCI_VENDOR_ID_REALTEK,     0x8129), 0, 0, RTL_CFG_0 },
235         { PCI_DEVICE(PCI_VENDOR_ID_REALTEK,     0x8136), 0, 0, RTL_CFG_2 },
236         { PCI_DEVICE(PCI_VENDOR_ID_REALTEK,     0x8167), 0, 0, RTL_CFG_0 },
237         { PCI_DEVICE(PCI_VENDOR_ID_REALTEK,     0x8168), 0, 0, RTL_CFG_1 },
238         { PCI_DEVICE(PCI_VENDOR_ID_REALTEK,     0x8169), 0, 0, RTL_CFG_0 },
239         { PCI_DEVICE(PCI_VENDOR_ID_DLINK,       0x4300), 0, 0, RTL_CFG_0 },
240         { PCI_DEVICE(PCI_VENDOR_ID_AT,          0xc107), 0, 0, RTL_CFG_0 },
241         { PCI_DEVICE(0x16ec,                    0x0116), 0, 0, RTL_CFG_0 },
242         { PCI_VENDOR_ID_LINKSYS,                0x1032,
243                 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
244         { 0x0001,                               0x8168,
245                 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
246         {0,},
247 };
248
249 MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
250
251 static int rx_buf_sz = 16383;
252 static int use_dac;
253 static struct {
254         u32 msg_enable;
255 } debug = { -1 };
256
257 enum rtl_registers {
258         MAC0            = 0,    /* Ethernet hardware address. */
259         MAC4            = 4,
260         MAR0            = 8,    /* Multicast filter. */
261         CounterAddrLow          = 0x10,
262         CounterAddrHigh         = 0x14,
263         TxDescStartAddrLow      = 0x20,
264         TxDescStartAddrHigh     = 0x24,
265         TxHDescStartAddrLow     = 0x28,
266         TxHDescStartAddrHigh    = 0x2c,
267         FLASH           = 0x30,
268         ERSR            = 0x36,
269         ChipCmd         = 0x37,
270         TxPoll          = 0x38,
271         IntrMask        = 0x3c,
272         IntrStatus      = 0x3e,
273         TxConfig        = 0x40,
274         RxConfig        = 0x44,
275
276 #define RTL_RX_CONFIG_MASK              0xff7e1880u
277
278         RxMissed        = 0x4c,
279         Cfg9346         = 0x50,
280         Config0         = 0x51,
281         Config1         = 0x52,
282         Config2         = 0x53,
283         Config3         = 0x54,
284         Config4         = 0x55,
285         Config5         = 0x56,
286         MultiIntr       = 0x5c,
287         PHYAR           = 0x60,
288         PHYstatus       = 0x6c,
289         RxMaxSize       = 0xda,
290         CPlusCmd        = 0xe0,
291         IntrMitigate    = 0xe2,
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
302         FuncEvent       = 0xf0,
303         FuncEventMask   = 0xf4,
304         FuncPresetState = 0xf8,
305         FuncForceEvent  = 0xfc,
306 };
307
308 enum rtl8110_registers {
309         TBICSR                  = 0x64,
310         TBI_ANAR                = 0x68,
311         TBI_LPAR                = 0x6a,
312 };
313
314 enum rtl8168_8101_registers {
315         CSIDR                   = 0x64,
316         CSIAR                   = 0x68,
317 #define CSIAR_FLAG                      0x80000000
318 #define CSIAR_WRITE_CMD                 0x80000000
319 #define CSIAR_BYTE_ENABLE               0x0f
320 #define CSIAR_BYTE_ENABLE_SHIFT         12
321 #define CSIAR_ADDR_MASK                 0x0fff
322         PMCH                    = 0x6f,
323         EPHYAR                  = 0x80,
324 #define EPHYAR_FLAG                     0x80000000
325 #define EPHYAR_WRITE_CMD                0x80000000
326 #define EPHYAR_REG_MASK                 0x1f
327 #define EPHYAR_REG_SHIFT                16
328 #define EPHYAR_DATA_MASK                0xffff
329         DLLPR                   = 0xd0,
330 #define PM_SWITCH                       (1 << 6)
331         DBG_REG                 = 0xd1,
332 #define FIX_NAK_1                       (1 << 4)
333 #define FIX_NAK_2                       (1 << 3)
334         TWSI                    = 0xd2,
335         MCU                     = 0xd3,
336 #define EN_NDP                          (1 << 3)
337 #define EN_OOB_RESET                    (1 << 2)
338         EFUSEAR                 = 0xdc,
339 #define EFUSEAR_FLAG                    0x80000000
340 #define EFUSEAR_WRITE_CMD               0x80000000
341 #define EFUSEAR_READ_CMD                0x00000000
342 #define EFUSEAR_REG_MASK                0x03ff
343 #define EFUSEAR_REG_SHIFT               8
344 #define EFUSEAR_DATA_MASK               0xff
345 };
346
347 enum rtl8168_registers {
348         ERIDR                   = 0x70,
349         ERIAR                   = 0x74,
350 #define ERIAR_FLAG                      0x80000000
351 #define ERIAR_WRITE_CMD                 0x80000000
352 #define ERIAR_READ_CMD                  0x00000000
353 #define ERIAR_ADDR_BYTE_ALIGN           4
354 #define ERIAR_EXGMAC                    0
355 #define ERIAR_MSIX                      1
356 #define ERIAR_ASF                       2
357 #define ERIAR_TYPE_SHIFT                16
358 #define ERIAR_BYTEEN                    0x0f
359 #define ERIAR_BYTEEN_SHIFT              12
360         EPHY_RXER_NUM           = 0x7c,
361         OCPDR                   = 0xb0, /* OCP GPHY access */
362 #define OCPDR_WRITE_CMD                 0x80000000
363 #define OCPDR_READ_CMD                  0x00000000
364 #define OCPDR_REG_MASK                  0x7f
365 #define OCPDR_GPHY_REG_SHIFT            16
366 #define OCPDR_DATA_MASK                 0xffff
367         OCPAR                   = 0xb4,
368 #define OCPAR_FLAG                      0x80000000
369 #define OCPAR_GPHY_WRITE_CMD            0x8000f060
370 #define OCPAR_GPHY_READ_CMD             0x0000f060
371         RDSAR1                  = 0xd0, /* 8168c only. Undocumented on 8168dp */
372         MISC                    = 0xf0, /* 8168e only. */
373 #define TXPLA_RST                       (1 << 29)
374 };
375
376 enum rtl_register_content {
377         /* InterruptStatusBits */
378         SYSErr          = 0x8000,
379         PCSTimeout      = 0x4000,
380         SWInt           = 0x0100,
381         TxDescUnavail   = 0x0080,
382         RxFIFOOver      = 0x0040,
383         LinkChg         = 0x0020,
384         RxOverflow      = 0x0010,
385         TxErr           = 0x0008,
386         TxOK            = 0x0004,
387         RxErr           = 0x0002,
388         RxOK            = 0x0001,
389
390         /* RxStatusDesc */
391         RxFOVF  = (1 << 23),
392         RxRWT   = (1 << 22),
393         RxRES   = (1 << 21),
394         RxRUNT  = (1 << 20),
395         RxCRC   = (1 << 19),
396
397         /* ChipCmdBits */
398         CmdReset        = 0x10,
399         CmdRxEnb        = 0x08,
400         CmdTxEnb        = 0x04,
401         RxBufEmpty      = 0x01,
402
403         /* TXPoll register p.5 */
404         HPQ             = 0x80,         /* Poll cmd on the high prio queue */
405         NPQ             = 0x40,         /* Poll cmd on the low prio queue */
406         FSWInt          = 0x01,         /* Forced software interrupt */
407
408         /* Cfg9346Bits */
409         Cfg9346_Lock    = 0x00,
410         Cfg9346_Unlock  = 0xc0,
411
412         /* rx_mode_bits */
413         AcceptErr       = 0x20,
414         AcceptRunt      = 0x10,
415         AcceptBroadcast = 0x08,
416         AcceptMulticast = 0x04,
417         AcceptMyPhys    = 0x02,
418         AcceptAllPhys   = 0x01,
419
420         /* RxConfigBits */
421         RxCfgFIFOShift  = 13,
422         RxCfgDMAShift   =  8,
423
424         /* TxConfigBits */
425         TxInterFrameGapShift = 24,
426         TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
427
428         /* Config1 register p.24 */
429         LEDS1           = (1 << 7),
430         LEDS0           = (1 << 6),
431         MSIEnable       = (1 << 5),     /* Enable Message Signaled Interrupt */
432         Speed_down      = (1 << 4),
433         MEMMAP          = (1 << 3),
434         IOMAP           = (1 << 2),
435         VPD             = (1 << 1),
436         PMEnable        = (1 << 0),     /* Power Management Enable */
437
438         /* Config2 register p. 25 */
439         PCI_Clock_66MHz = 0x01,
440         PCI_Clock_33MHz = 0x00,
441
442         /* Config3 register p.25 */
443         MagicPacket     = (1 << 5),     /* Wake up when receives a Magic Packet */
444         LinkUp          = (1 << 4),     /* Wake up when the cable connection is re-established */
445         Beacon_en       = (1 << 0),     /* 8168 only. Reserved in the 8168b */
446
447         /* Config5 register p.27 */
448         BWF             = (1 << 6),     /* Accept Broadcast wakeup frame */
449         MWF             = (1 << 5),     /* Accept Multicast wakeup frame */
450         UWF             = (1 << 4),     /* Accept Unicast wakeup frame */
451         Spi_en          = (1 << 3),
452         LanWake         = (1 << 1),     /* LanWake enable/disable */
453         PMEStatus       = (1 << 0),     /* PME status can be reset by PCI RST# */
454
455         /* TBICSR p.28 */
456         TBIReset        = 0x80000000,
457         TBILoopback     = 0x40000000,
458         TBINwEnable     = 0x20000000,
459         TBINwRestart    = 0x10000000,
460         TBILinkOk       = 0x02000000,
461         TBINwComplete   = 0x01000000,
462
463         /* CPlusCmd p.31 */
464         EnableBist      = (1 << 15),    // 8168 8101
465         Mac_dbgo_oe     = (1 << 14),    // 8168 8101
466         Normal_mode     = (1 << 13),    // unused
467         Force_half_dup  = (1 << 12),    // 8168 8101
468         Force_rxflow_en = (1 << 11),    // 8168 8101
469         Force_txflow_en = (1 << 10),    // 8168 8101
470         Cxpl_dbg_sel    = (1 << 9),     // 8168 8101
471         ASF             = (1 << 8),     // 8168 8101
472         PktCntrDisable  = (1 << 7),     // 8168 8101
473         Mac_dbgo_sel    = 0x001c,       // 8168
474         RxVlan          = (1 << 6),
475         RxChkSum        = (1 << 5),
476         PCIDAC          = (1 << 4),
477         PCIMulRW        = (1 << 3),
478         INTT_0          = 0x0000,       // 8168
479         INTT_1          = 0x0001,       // 8168
480         INTT_2          = 0x0002,       // 8168
481         INTT_3          = 0x0003,       // 8168
482
483         /* rtl8169_PHYstatus */
484         TBI_Enable      = 0x80,
485         TxFlowCtrl      = 0x40,
486         RxFlowCtrl      = 0x20,
487         _1000bpsF       = 0x10,
488         _100bps         = 0x08,
489         _10bps          = 0x04,
490         LinkStatus      = 0x02,
491         FullDup         = 0x01,
492
493         /* _TBICSRBit */
494         TBILinkOK       = 0x02000000,
495
496         /* DumpCounterCommand */
497         CounterDump     = 0x8,
498 };
499
500 enum rtl_desc_bit {
501         /* First doubleword. */
502         DescOwn         = (1 << 31), /* Descriptor is owned by NIC */
503         RingEnd         = (1 << 30), /* End of descriptor ring */
504         FirstFrag       = (1 << 29), /* First segment of a packet */
505         LastFrag        = (1 << 28), /* Final segment of a packet */
506 };
507
508 /* Generic case. */
509 enum rtl_tx_desc_bit {
510         /* First doubleword. */
511         TD_LSO          = (1 << 27),            /* Large Send Offload */
512 #define TD_MSS_MAX                      0x07ffu /* MSS value */
513
514         /* Second doubleword. */
515         TxVlanTag       = (1 << 17),            /* Add VLAN tag */
516 };
517
518 /* 8169, 8168b and 810x except 8102e. */
519 enum rtl_tx_desc_bit_0 {
520         /* First doubleword. */
521 #define TD0_MSS_SHIFT                   16      /* MSS position (11 bits) */
522         TD0_TCP_CS      = (1 << 16),            /* Calculate TCP/IP checksum */
523         TD0_UDP_CS      = (1 << 17),            /* Calculate UDP/IP checksum */
524         TD0_IP_CS       = (1 << 18),            /* Calculate IP checksum */
525 };
526
527 /* 8102e, 8168c and beyond. */
528 enum rtl_tx_desc_bit_1 {
529         /* Second doubleword. */
530 #define TD1_MSS_SHIFT                   18      /* MSS position (11 bits) */
531         TD1_IP_CS       = (1 << 29),            /* Calculate IP checksum */
532         TD1_TCP_CS      = (1 << 30),            /* Calculate TCP/IP checksum */
533         TD1_UDP_CS      = (1 << 31),            /* Calculate UDP/IP checksum */
534 };
535
536 static const struct rtl_tx_desc_info {
537         struct {
538                 u32 udp;
539                 u32 tcp;
540         } checksum;
541         u16 mss_shift;
542         u16 opts_offset;
543 } tx_desc_info [] = {
544         [RTL_TD_0] = {
545                 .checksum = {
546                         .udp    = TD0_IP_CS | TD0_UDP_CS,
547                         .tcp    = TD0_IP_CS | TD0_TCP_CS
548                 },
549                 .mss_shift      = TD0_MSS_SHIFT,
550                 .opts_offset    = 0
551         },
552         [RTL_TD_1] = {
553                 .checksum = {
554                         .udp    = TD1_IP_CS | TD1_UDP_CS,
555                         .tcp    = TD1_IP_CS | TD1_TCP_CS
556                 },
557                 .mss_shift      = TD1_MSS_SHIFT,
558                 .opts_offset    = 1
559         }
560 };
561
562 enum rtl_rx_desc_bit {
563         /* Rx private */
564         PID1            = (1 << 18), /* Protocol ID bit 1/2 */
565         PID0            = (1 << 17), /* Protocol ID bit 2/2 */
566
567 #define RxProtoUDP      (PID1)
568 #define RxProtoTCP      (PID0)
569 #define RxProtoIP       (PID1 | PID0)
570 #define RxProtoMask     RxProtoIP
571
572         IPFail          = (1 << 16), /* IP checksum failed */
573         UDPFail         = (1 << 15), /* UDP/IP checksum failed */
574         TCPFail         = (1 << 14), /* TCP/IP checksum failed */
575         RxVlanTag       = (1 << 16), /* VLAN tag available */
576 };
577
578 #define RsvdMask        0x3fffc000
579
580 struct TxDesc {
581         __le32 opts1;
582         __le32 opts2;
583         __le64 addr;
584 };
585
586 struct RxDesc {
587         __le32 opts1;
588         __le32 opts2;
589         __le64 addr;
590 };
591
592 struct ring_info {
593         struct sk_buff  *skb;
594         u32             len;
595         u8              __pad[sizeof(void *) - sizeof(u32)];
596 };
597
598 enum features {
599         RTL_FEATURE_WOL         = (1 << 0),
600         RTL_FEATURE_MSI         = (1 << 1),
601         RTL_FEATURE_GMII        = (1 << 2),
602 };
603
604 struct rtl8169_counters {
605         __le64  tx_packets;
606         __le64  rx_packets;
607         __le64  tx_errors;
608         __le32  rx_errors;
609         __le16  rx_missed;
610         __le16  align_errors;
611         __le32  tx_one_collision;
612         __le32  tx_multi_collision;
613         __le64  rx_unicast;
614         __le64  rx_broadcast;
615         __le32  rx_multicast;
616         __le16  tx_aborted;
617         __le16  tx_underun;
618 };
619
620 struct rtl8169_private {
621         void __iomem *mmio_addr;        /* memory map physical address */
622         struct pci_dev *pci_dev;
623         struct net_device *dev;
624         struct napi_struct napi;
625         spinlock_t lock;
626         u32 msg_enable;
627         u16 txd_version;
628         u16 mac_version;
629         u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
630         u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
631         u32 dirty_rx;
632         u32 dirty_tx;
633         struct TxDesc *TxDescArray;     /* 256-aligned Tx descriptor ring */
634         struct RxDesc *RxDescArray;     /* 256-aligned Rx descriptor ring */
635         dma_addr_t TxPhyAddr;
636         dma_addr_t RxPhyAddr;
637         void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
638         struct ring_info tx_skb[NUM_TX_DESC];   /* Tx data buffers */
639         struct timer_list timer;
640         u16 cp_cmd;
641         u16 intr_event;
642         u16 napi_event;
643         u16 intr_mask;
644
645         struct mdio_ops {
646                 void (*write)(void __iomem *, int, int);
647                 int (*read)(void __iomem *, int);
648         } mdio_ops;
649
650         struct pll_power_ops {
651                 void (*down)(struct rtl8169_private *);
652                 void (*up)(struct rtl8169_private *);
653         } pll_power_ops;
654
655         int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
656         int (*get_settings)(struct net_device *, struct ethtool_cmd *);
657         void (*phy_reset_enable)(struct rtl8169_private *tp);
658         void (*hw_start)(struct net_device *);
659         unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
660         unsigned int (*link_ok)(void __iomem *);
661         int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
662         int pcie_cap;
663         struct delayed_work task;
664         unsigned features;
665
666         struct mii_if_info mii;
667         struct rtl8169_counters counters;
668         u32 saved_wolopts;
669
670         struct rtl_fw {
671                 const struct firmware *fw;
672
673 #define RTL_VER_SIZE            32
674
675                 char version[RTL_VER_SIZE];
676
677                 struct rtl_fw_phy_action {
678                         __le32 *code;
679                         size_t size;
680                 } phy_action;
681         } *rtl_fw;
682 #define RTL_FIRMWARE_UNKNOWN    ERR_PTR(-EAGAIN);
683 };
684
685 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
686 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
687 module_param(use_dac, int, 0);
688 MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
689 module_param_named(debug, debug.msg_enable, int, 0);
690 MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
691 MODULE_LICENSE("GPL");
692 MODULE_VERSION(RTL8169_VERSION);
693 MODULE_FIRMWARE(FIRMWARE_8168D_1);
694 MODULE_FIRMWARE(FIRMWARE_8168D_2);
695 MODULE_FIRMWARE(FIRMWARE_8168E_1);
696 MODULE_FIRMWARE(FIRMWARE_8168E_2);
697 MODULE_FIRMWARE(FIRMWARE_8105E_1);
698
699 static int rtl8169_open(struct net_device *dev);
700 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
701                                       struct net_device *dev);
702 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
703 static int rtl8169_init_ring(struct net_device *dev);
704 static void rtl_hw_start(struct net_device *dev);
705 static int rtl8169_close(struct net_device *dev);
706 static void rtl_set_rx_mode(struct net_device *dev);
707 static void rtl8169_tx_timeout(struct net_device *dev);
708 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
709 static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
710                                 void __iomem *, u32 budget);
711 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
712 static void rtl8169_down(struct net_device *dev);
713 static void rtl8169_rx_clear(struct rtl8169_private *tp);
714 static int rtl8169_poll(struct napi_struct *napi, int budget);
715
716 static const unsigned int rtl8169_rx_config =
717         (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
718
719 static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
720 {
721         void __iomem *ioaddr = tp->mmio_addr;
722         int i;
723
724         RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
725         for (i = 0; i < 20; i++) {
726                 udelay(100);
727                 if (RTL_R32(OCPAR) & OCPAR_FLAG)
728                         break;
729         }
730         return RTL_R32(OCPDR);
731 }
732
733 static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
734 {
735         void __iomem *ioaddr = tp->mmio_addr;
736         int i;
737
738         RTL_W32(OCPDR, data);
739         RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
740         for (i = 0; i < 20; i++) {
741                 udelay(100);
742                 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
743                         break;
744         }
745 }
746
747 static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
748 {
749         void __iomem *ioaddr = tp->mmio_addr;
750         int i;
751
752         RTL_W8(ERIDR, cmd);
753         RTL_W32(ERIAR, 0x800010e8);
754         msleep(2);
755         for (i = 0; i < 5; i++) {
756                 udelay(100);
757                 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
758                         break;
759         }
760
761         ocp_write(tp, 0x1, 0x30, 0x00000001);
762 }
763
764 #define OOB_CMD_RESET           0x00
765 #define OOB_CMD_DRIVER_START    0x05
766 #define OOB_CMD_DRIVER_STOP     0x06
767
768 static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
769 {
770         return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
771 }
772
773 static void rtl8168_driver_start(struct rtl8169_private *tp)
774 {
775         u16 reg;
776         int i;
777
778         rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
779
780         reg = rtl8168_get_ocp_reg(tp);
781
782         for (i = 0; i < 10; i++) {
783                 msleep(10);
784                 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
785                         break;
786         }
787 }
788
789 static void rtl8168_driver_stop(struct rtl8169_private *tp)
790 {
791         u16 reg;
792         int i;
793
794         rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
795
796         reg = rtl8168_get_ocp_reg(tp);
797
798         for (i = 0; i < 10; i++) {
799                 msleep(10);
800                 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
801                         break;
802         }
803 }
804
805 static int r8168dp_check_dash(struct rtl8169_private *tp)
806 {
807         u16 reg = rtl8168_get_ocp_reg(tp);
808
809         return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
810 }
811
812 static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
813 {
814         int i;
815
816         RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
817
818         for (i = 20; i > 0; i--) {
819                 /*
820                  * Check if the RTL8169 has completed writing to the specified
821                  * MII register.
822                  */
823                 if (!(RTL_R32(PHYAR) & 0x80000000))
824                         break;
825                 udelay(25);
826         }
827         /*
828          * According to hardware specs a 20us delay is required after write
829          * complete indication, but before sending next command.
830          */
831         udelay(20);
832 }
833
834 static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
835 {
836         int i, value = -1;
837
838         RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
839
840         for (i = 20; i > 0; i--) {
841                 /*
842                  * Check if the RTL8169 has completed retrieving data from
843                  * the specified MII register.
844                  */
845                 if (RTL_R32(PHYAR) & 0x80000000) {
846                         value = RTL_R32(PHYAR) & 0xffff;
847                         break;
848                 }
849                 udelay(25);
850         }
851         /*
852          * According to hardware specs a 20us delay is required after read
853          * complete indication, but before sending next command.
854          */
855         udelay(20);
856
857         return value;
858 }
859
860 static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
861 {
862         int i;
863
864         RTL_W32(OCPDR, data |
865                 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
866         RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
867         RTL_W32(EPHY_RXER_NUM, 0);
868
869         for (i = 0; i < 100; i++) {
870                 mdelay(1);
871                 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
872                         break;
873         }
874 }
875
876 static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
877 {
878         r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
879                 (value & OCPDR_DATA_MASK));
880 }
881
882 static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
883 {
884         int i;
885
886         r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
887
888         mdelay(1);
889         RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
890         RTL_W32(EPHY_RXER_NUM, 0);
891
892         for (i = 0; i < 100; i++) {
893                 mdelay(1);
894                 if (RTL_R32(OCPAR) & OCPAR_FLAG)
895                         break;
896         }
897
898         return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
899 }
900
901 #define R8168DP_1_MDIO_ACCESS_BIT       0x00020000
902
903 static void r8168dp_2_mdio_start(void __iomem *ioaddr)
904 {
905         RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
906 }
907
908 static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
909 {
910         RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
911 }
912
913 static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
914 {
915         r8168dp_2_mdio_start(ioaddr);
916
917         r8169_mdio_write(ioaddr, reg_addr, value);
918
919         r8168dp_2_mdio_stop(ioaddr);
920 }
921
922 static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
923 {
924         int value;
925
926         r8168dp_2_mdio_start(ioaddr);
927
928         value = r8169_mdio_read(ioaddr, reg_addr);
929
930         r8168dp_2_mdio_stop(ioaddr);
931
932         return value;
933 }
934
935 static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
936 {
937         tp->mdio_ops.write(tp->mmio_addr, location, val);
938 }
939
940 static int rtl_readphy(struct rtl8169_private *tp, int location)
941 {
942         return tp->mdio_ops.read(tp->mmio_addr, location);
943 }
944
945 static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
946 {
947         rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
948 }
949
950 static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
951 {
952         int val;
953
954         val = rtl_readphy(tp, reg_addr);
955         rtl_writephy(tp, reg_addr, (val | p) & ~m);
956 }
957
958 static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
959                            int val)
960 {
961         struct rtl8169_private *tp = netdev_priv(dev);
962
963         rtl_writephy(tp, location, val);
964 }
965
966 static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
967 {
968         struct rtl8169_private *tp = netdev_priv(dev);
969
970         return rtl_readphy(tp, location);
971 }
972
973 static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
974 {
975         unsigned int i;
976
977         RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
978                 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
979
980         for (i = 0; i < 100; i++) {
981                 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
982                         break;
983                 udelay(10);
984         }
985 }
986
987 static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
988 {
989         u16 value = 0xffff;
990         unsigned int i;
991
992         RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
993
994         for (i = 0; i < 100; i++) {
995                 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
996                         value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
997                         break;
998                 }
999                 udelay(10);
1000         }
1001
1002         return value;
1003 }
1004
1005 static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1006 {
1007         unsigned int i;
1008
1009         RTL_W32(CSIDR, value);
1010         RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1011                 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1012
1013         for (i = 0; i < 100; i++) {
1014                 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1015                         break;
1016                 udelay(10);
1017         }
1018 }
1019
1020 static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1021 {
1022         u32 value = ~0x00;
1023         unsigned int i;
1024
1025         RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1026                 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1027
1028         for (i = 0; i < 100; i++) {
1029                 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1030                         value = RTL_R32(CSIDR);
1031                         break;
1032                 }
1033                 udelay(10);
1034         }
1035
1036         return value;
1037 }
1038
1039 static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1040 {
1041         u8 value = 0xff;
1042         unsigned int i;
1043
1044         RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1045
1046         for (i = 0; i < 300; i++) {
1047                 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1048                         value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1049                         break;
1050                 }
1051                 udelay(100);
1052         }
1053
1054         return value;
1055 }
1056
1057 static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1058 {
1059         RTL_W16(IntrMask, 0x0000);
1060
1061         RTL_W16(IntrStatus, 0xffff);
1062 }
1063
1064 static void rtl8169_asic_down(void __iomem *ioaddr)
1065 {
1066         RTL_W8(ChipCmd, 0x00);
1067         rtl8169_irq_mask_and_ack(ioaddr);
1068         RTL_R16(CPlusCmd);
1069 }
1070
1071 static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
1072 {
1073         void __iomem *ioaddr = tp->mmio_addr;
1074
1075         return RTL_R32(TBICSR) & TBIReset;
1076 }
1077
1078 static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
1079 {
1080         return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
1081 }
1082
1083 static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1084 {
1085         return RTL_R32(TBICSR) & TBILinkOk;
1086 }
1087
1088 static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1089 {
1090         return RTL_R8(PHYstatus) & LinkStatus;
1091 }
1092
1093 static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
1094 {
1095         void __iomem *ioaddr = tp->mmio_addr;
1096
1097         RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1098 }
1099
1100 static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
1101 {
1102         unsigned int val;
1103
1104         val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1105         rtl_writephy(tp, MII_BMCR, val & 0xffff);
1106 }
1107
1108 static void __rtl8169_check_link_status(struct net_device *dev,
1109                                         struct rtl8169_private *tp,
1110                                         void __iomem *ioaddr, bool pm)
1111 {
1112         unsigned long flags;
1113
1114         spin_lock_irqsave(&tp->lock, flags);
1115         if (tp->link_ok(ioaddr)) {
1116                 /* This is to cancel a scheduled suspend if there's one. */
1117                 if (pm)
1118                         pm_request_resume(&tp->pci_dev->dev);
1119                 netif_carrier_on(dev);
1120                 if (net_ratelimit())
1121                         netif_info(tp, ifup, dev, "link up\n");
1122         } else {
1123                 netif_carrier_off(dev);
1124                 netif_info(tp, ifdown, dev, "link down\n");
1125                 if (pm)
1126                         pm_schedule_suspend(&tp->pci_dev->dev, 100);
1127         }
1128         spin_unlock_irqrestore(&tp->lock, flags);
1129 }
1130
1131 static void rtl8169_check_link_status(struct net_device *dev,
1132                                       struct rtl8169_private *tp,
1133                                       void __iomem *ioaddr)
1134 {
1135         __rtl8169_check_link_status(dev, tp, ioaddr, false);
1136 }
1137
1138 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1139
1140 static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1141 {
1142         void __iomem *ioaddr = tp->mmio_addr;
1143         u8 options;
1144         u32 wolopts = 0;
1145
1146         options = RTL_R8(Config1);
1147         if (!(options & PMEnable))
1148                 return 0;
1149
1150         options = RTL_R8(Config3);
1151         if (options & LinkUp)
1152                 wolopts |= WAKE_PHY;
1153         if (options & MagicPacket)
1154                 wolopts |= WAKE_MAGIC;
1155
1156         options = RTL_R8(Config5);
1157         if (options & UWF)
1158                 wolopts |= WAKE_UCAST;
1159         if (options & BWF)
1160                 wolopts |= WAKE_BCAST;
1161         if (options & MWF)
1162                 wolopts |= WAKE_MCAST;
1163
1164         return wolopts;
1165 }
1166
1167 static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1168 {
1169         struct rtl8169_private *tp = netdev_priv(dev);
1170
1171         spin_lock_irq(&tp->lock);
1172
1173         wol->supported = WAKE_ANY;
1174         wol->wolopts = __rtl8169_get_wol(tp);
1175
1176         spin_unlock_irq(&tp->lock);
1177 }
1178
1179 static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
1180 {
1181         void __iomem *ioaddr = tp->mmio_addr;
1182         unsigned int i;
1183         static const struct {
1184                 u32 opt;
1185                 u16 reg;
1186                 u8  mask;
1187         } cfg[] = {
1188                 { WAKE_ANY,   Config1, PMEnable },
1189                 { WAKE_PHY,   Config3, LinkUp },
1190                 { WAKE_MAGIC, Config3, MagicPacket },
1191                 { WAKE_UCAST, Config5, UWF },
1192                 { WAKE_BCAST, Config5, BWF },
1193                 { WAKE_MCAST, Config5, MWF },
1194                 { WAKE_ANY,   Config5, LanWake }
1195         };
1196
1197         RTL_W8(Cfg9346, Cfg9346_Unlock);
1198
1199         for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1200                 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1201                 if (wolopts & cfg[i].opt)
1202                         options |= cfg[i].mask;
1203                 RTL_W8(cfg[i].reg, options);
1204         }
1205
1206         RTL_W8(Cfg9346, Cfg9346_Lock);
1207 }
1208
1209 static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1210 {
1211         struct rtl8169_private *tp = netdev_priv(dev);
1212
1213         spin_lock_irq(&tp->lock);
1214
1215         if (wol->wolopts)
1216                 tp->features |= RTL_FEATURE_WOL;
1217         else
1218                 tp->features &= ~RTL_FEATURE_WOL;
1219         __rtl8169_set_wol(tp, wol->wolopts);
1220         spin_unlock_irq(&tp->lock);
1221
1222         device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1223
1224         return 0;
1225 }
1226
1227 static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1228 {
1229         return rtl_chip_infos[tp->mac_version].fw_name;
1230 }
1231
1232 static void rtl8169_get_drvinfo(struct net_device *dev,
1233                                 struct ethtool_drvinfo *info)
1234 {
1235         struct rtl8169_private *tp = netdev_priv(dev);
1236         struct rtl_fw *rtl_fw = tp->rtl_fw;
1237
1238         strcpy(info->driver, MODULENAME);
1239         strcpy(info->version, RTL8169_VERSION);
1240         strcpy(info->bus_info, pci_name(tp->pci_dev));
1241         BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1242         strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1243                rtl_fw->version);
1244 }
1245
1246 static int rtl8169_get_regs_len(struct net_device *dev)
1247 {
1248         return R8169_REGS_SIZE;
1249 }
1250
1251 static int rtl8169_set_speed_tbi(struct net_device *dev,
1252                                  u8 autoneg, u16 speed, u8 duplex, u32 ignored)
1253 {
1254         struct rtl8169_private *tp = netdev_priv(dev);
1255         void __iomem *ioaddr = tp->mmio_addr;
1256         int ret = 0;
1257         u32 reg;
1258
1259         reg = RTL_R32(TBICSR);
1260         if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1261             (duplex == DUPLEX_FULL)) {
1262                 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1263         } else if (autoneg == AUTONEG_ENABLE)
1264                 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1265         else {
1266                 netif_warn(tp, link, dev,
1267                            "incorrect speed setting refused in TBI mode\n");
1268                 ret = -EOPNOTSUPP;
1269         }
1270
1271         return ret;
1272 }
1273
1274 static int rtl8169_set_speed_xmii(struct net_device *dev,
1275                                   u8 autoneg, u16 speed, u8 duplex, u32 adv)
1276 {
1277         struct rtl8169_private *tp = netdev_priv(dev);
1278         int giga_ctrl, bmcr;
1279         int rc = -EINVAL;
1280
1281         rtl_writephy(tp, 0x1f, 0x0000);
1282
1283         if (autoneg == AUTONEG_ENABLE) {
1284                 int auto_nego;
1285
1286                 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1287                 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1288                                 ADVERTISE_100HALF | ADVERTISE_100FULL);
1289
1290                 if (adv & ADVERTISED_10baseT_Half)
1291                         auto_nego |= ADVERTISE_10HALF;
1292                 if (adv & ADVERTISED_10baseT_Full)
1293                         auto_nego |= ADVERTISE_10FULL;
1294                 if (adv & ADVERTISED_100baseT_Half)
1295                         auto_nego |= ADVERTISE_100HALF;
1296                 if (adv & ADVERTISED_100baseT_Full)
1297                         auto_nego |= ADVERTISE_100FULL;
1298
1299                 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1300
1301                 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1302                 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1303
1304                 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1305                 if (tp->mii.supports_gmii) {
1306                         if (adv & ADVERTISED_1000baseT_Half)
1307                                 giga_ctrl |= ADVERTISE_1000HALF;
1308                         if (adv & ADVERTISED_1000baseT_Full)
1309                                 giga_ctrl |= ADVERTISE_1000FULL;
1310                 } else if (adv & (ADVERTISED_1000baseT_Half |
1311                                   ADVERTISED_1000baseT_Full)) {
1312                         netif_info(tp, link, dev,
1313                                    "PHY does not support 1000Mbps\n");
1314                         goto out;
1315                 }
1316
1317                 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1318
1319                 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1320                 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1321         } else {
1322                 giga_ctrl = 0;
1323
1324                 if (speed == SPEED_10)
1325                         bmcr = 0;
1326                 else if (speed == SPEED_100)
1327                         bmcr = BMCR_SPEED100;
1328                 else
1329                         goto out;
1330
1331                 if (duplex == DUPLEX_FULL)
1332                         bmcr |= BMCR_FULLDPLX;
1333         }
1334
1335         rtl_writephy(tp, MII_BMCR, bmcr);
1336
1337         if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1338             tp->mac_version == RTL_GIGA_MAC_VER_03) {
1339                 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1340                         rtl_writephy(tp, 0x17, 0x2138);
1341                         rtl_writephy(tp, 0x0e, 0x0260);
1342                 } else {
1343                         rtl_writephy(tp, 0x17, 0x2108);
1344                         rtl_writephy(tp, 0x0e, 0x0000);
1345                 }
1346         }
1347
1348         rc = 0;
1349 out:
1350         return rc;
1351 }
1352
1353 static int rtl8169_set_speed(struct net_device *dev,
1354                              u8 autoneg, u16 speed, u8 duplex, u32 advertising)
1355 {
1356         struct rtl8169_private *tp = netdev_priv(dev);
1357         int ret;
1358
1359         ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
1360         if (ret < 0)
1361                 goto out;
1362
1363         if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1364             (advertising & ADVERTISED_1000baseT_Full)) {
1365                 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1366         }
1367 out:
1368         return ret;
1369 }
1370
1371 static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1372 {
1373         struct rtl8169_private *tp = netdev_priv(dev);
1374         unsigned long flags;
1375         int ret;
1376
1377         del_timer_sync(&tp->timer);
1378
1379         spin_lock_irqsave(&tp->lock, flags);
1380         ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
1381                                 cmd->duplex, cmd->advertising);
1382         spin_unlock_irqrestore(&tp->lock, flags);
1383
1384         return ret;
1385 }
1386
1387 static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
1388 {
1389         if (dev->mtu > TD_MSS_MAX)
1390                 features &= ~NETIF_F_ALL_TSO;
1391
1392         return features;
1393 }
1394
1395 static int rtl8169_set_features(struct net_device *dev, u32 features)
1396 {
1397         struct rtl8169_private *tp = netdev_priv(dev);
1398         void __iomem *ioaddr = tp->mmio_addr;
1399         unsigned long flags;
1400
1401         spin_lock_irqsave(&tp->lock, flags);
1402
1403         if (features & NETIF_F_RXCSUM)
1404                 tp->cp_cmd |= RxChkSum;
1405         else
1406                 tp->cp_cmd &= ~RxChkSum;
1407
1408         if (dev->features & NETIF_F_HW_VLAN_RX)
1409                 tp->cp_cmd |= RxVlan;
1410         else
1411                 tp->cp_cmd &= ~RxVlan;
1412
1413         RTL_W16(CPlusCmd, tp->cp_cmd);
1414         RTL_R16(CPlusCmd);
1415
1416         spin_unlock_irqrestore(&tp->lock, flags);
1417
1418         return 0;
1419 }
1420
1421 static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1422                                       struct sk_buff *skb)
1423 {
1424         return (vlan_tx_tag_present(skb)) ?
1425                 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1426 }
1427
1428 static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
1429 {
1430         u32 opts2 = le32_to_cpu(desc->opts2);
1431
1432         if (opts2 & RxVlanTag)
1433                 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
1434
1435         desc->opts2 = 0;
1436 }
1437
1438 static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
1439 {
1440         struct rtl8169_private *tp = netdev_priv(dev);
1441         void __iomem *ioaddr = tp->mmio_addr;
1442         u32 status;
1443
1444         cmd->supported =
1445                 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1446         cmd->port = PORT_FIBRE;
1447         cmd->transceiver = XCVR_INTERNAL;
1448
1449         status = RTL_R32(TBICSR);
1450         cmd->advertising = (status & TBINwEnable) ?  ADVERTISED_Autoneg : 0;
1451         cmd->autoneg = !!(status & TBINwEnable);
1452
1453         ethtool_cmd_speed_set(cmd, SPEED_1000);
1454         cmd->duplex = DUPLEX_FULL; /* Always set */
1455
1456         return 0;
1457 }
1458
1459 static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
1460 {
1461         struct rtl8169_private *tp = netdev_priv(dev);
1462
1463         return mii_ethtool_gset(&tp->mii, cmd);
1464 }
1465
1466 static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1467 {
1468         struct rtl8169_private *tp = netdev_priv(dev);
1469         unsigned long flags;
1470         int rc;
1471
1472         spin_lock_irqsave(&tp->lock, flags);
1473
1474         rc = tp->get_settings(dev, cmd);
1475
1476         spin_unlock_irqrestore(&tp->lock, flags);
1477         return rc;
1478 }
1479
1480 static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1481                              void *p)
1482 {
1483         struct rtl8169_private *tp = netdev_priv(dev);
1484         unsigned long flags;
1485
1486         if (regs->len > R8169_REGS_SIZE)
1487                 regs->len = R8169_REGS_SIZE;
1488
1489         spin_lock_irqsave(&tp->lock, flags);
1490         memcpy_fromio(p, tp->mmio_addr, regs->len);
1491         spin_unlock_irqrestore(&tp->lock, flags);
1492 }
1493
1494 static u32 rtl8169_get_msglevel(struct net_device *dev)
1495 {
1496         struct rtl8169_private *tp = netdev_priv(dev);
1497
1498         return tp->msg_enable;
1499 }
1500
1501 static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1502 {
1503         struct rtl8169_private *tp = netdev_priv(dev);
1504
1505         tp->msg_enable = value;
1506 }
1507
1508 static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1509         "tx_packets",
1510         "rx_packets",
1511         "tx_errors",
1512         "rx_errors",
1513         "rx_missed",
1514         "align_errors",
1515         "tx_single_collisions",
1516         "tx_multi_collisions",
1517         "unicast",
1518         "broadcast",
1519         "multicast",
1520         "tx_aborted",
1521         "tx_underrun",
1522 };
1523
1524 static int rtl8169_get_sset_count(struct net_device *dev, int sset)
1525 {
1526         switch (sset) {
1527         case ETH_SS_STATS:
1528                 return ARRAY_SIZE(rtl8169_gstrings);
1529         default:
1530                 return -EOPNOTSUPP;
1531         }
1532 }
1533
1534 static void rtl8169_update_counters(struct net_device *dev)
1535 {
1536         struct rtl8169_private *tp = netdev_priv(dev);
1537         void __iomem *ioaddr = tp->mmio_addr;
1538         struct device *d = &tp->pci_dev->dev;
1539         struct rtl8169_counters *counters;
1540         dma_addr_t paddr;
1541         u32 cmd;
1542         int wait = 1000;
1543
1544         /*
1545          * Some chips are unable to dump tally counters when the receiver
1546          * is disabled.
1547          */
1548         if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1549                 return;
1550
1551         counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
1552         if (!counters)
1553                 return;
1554
1555         RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1556         cmd = (u64)paddr & DMA_BIT_MASK(32);
1557         RTL_W32(CounterAddrLow, cmd);
1558         RTL_W32(CounterAddrLow, cmd | CounterDump);
1559
1560         while (wait--) {
1561                 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1562                         memcpy(&tp->counters, counters, sizeof(*counters));
1563                         break;
1564                 }
1565                 udelay(10);
1566         }
1567
1568         RTL_W32(CounterAddrLow, 0);
1569         RTL_W32(CounterAddrHigh, 0);
1570
1571         dma_free_coherent(d, sizeof(*counters), counters, paddr);
1572 }
1573
1574 static void rtl8169_get_ethtool_stats(struct net_device *dev,
1575                                       struct ethtool_stats *stats, u64 *data)
1576 {
1577         struct rtl8169_private *tp = netdev_priv(dev);
1578
1579         ASSERT_RTNL();
1580
1581         rtl8169_update_counters(dev);
1582
1583         data[0] = le64_to_cpu(tp->counters.tx_packets);
1584         data[1] = le64_to_cpu(tp->counters.rx_packets);
1585         data[2] = le64_to_cpu(tp->counters.tx_errors);
1586         data[3] = le32_to_cpu(tp->counters.rx_errors);
1587         data[4] = le16_to_cpu(tp->counters.rx_missed);
1588         data[5] = le16_to_cpu(tp->counters.align_errors);
1589         data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1590         data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1591         data[8] = le64_to_cpu(tp->counters.rx_unicast);
1592         data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1593         data[10] = le32_to_cpu(tp->counters.rx_multicast);
1594         data[11] = le16_to_cpu(tp->counters.tx_aborted);
1595         data[12] = le16_to_cpu(tp->counters.tx_underun);
1596 }
1597
1598 static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1599 {
1600         switch(stringset) {
1601         case ETH_SS_STATS:
1602                 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1603                 break;
1604         }
1605 }
1606
1607 static const struct ethtool_ops rtl8169_ethtool_ops = {
1608         .get_drvinfo            = rtl8169_get_drvinfo,
1609         .get_regs_len           = rtl8169_get_regs_len,
1610         .get_link               = ethtool_op_get_link,
1611         .get_settings           = rtl8169_get_settings,
1612         .set_settings           = rtl8169_set_settings,
1613         .get_msglevel           = rtl8169_get_msglevel,
1614         .set_msglevel           = rtl8169_set_msglevel,
1615         .get_regs               = rtl8169_get_regs,
1616         .get_wol                = rtl8169_get_wol,
1617         .set_wol                = rtl8169_set_wol,
1618         .get_strings            = rtl8169_get_strings,
1619         .get_sset_count         = rtl8169_get_sset_count,
1620         .get_ethtool_stats      = rtl8169_get_ethtool_stats,
1621 };
1622
1623 static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1624                                     struct net_device *dev, u8 default_version)
1625 {
1626         void __iomem *ioaddr = tp->mmio_addr;
1627         /*
1628          * The driver currently handles the 8168Bf and the 8168Be identically
1629          * but they can be identified more specifically through the test below
1630          * if needed:
1631          *
1632          * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
1633          *
1634          * Same thing for the 8101Eb and the 8101Ec:
1635          *
1636          * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
1637          */
1638         static const struct {
1639                 u32 mask;
1640                 u32 val;
1641                 int mac_version;
1642         } mac_info[] = {
1643                 /* 8168E family. */
1644                 { 0x7cf00000, 0x2c200000,       RTL_GIGA_MAC_VER_33 },
1645                 { 0x7cf00000, 0x2c100000,       RTL_GIGA_MAC_VER_32 },
1646                 { 0x7c800000, 0x2c000000,       RTL_GIGA_MAC_VER_33 },
1647
1648                 /* 8168D family. */
1649                 { 0x7cf00000, 0x28300000,       RTL_GIGA_MAC_VER_26 },
1650                 { 0x7cf00000, 0x28100000,       RTL_GIGA_MAC_VER_25 },
1651                 { 0x7c800000, 0x28000000,       RTL_GIGA_MAC_VER_26 },
1652
1653                 /* 8168DP family. */
1654                 { 0x7cf00000, 0x28800000,       RTL_GIGA_MAC_VER_27 },
1655                 { 0x7cf00000, 0x28a00000,       RTL_GIGA_MAC_VER_28 },
1656                 { 0x7cf00000, 0x28b00000,       RTL_GIGA_MAC_VER_31 },
1657
1658                 /* 8168C family. */
1659                 { 0x7cf00000, 0x3cb00000,       RTL_GIGA_MAC_VER_24 },
1660                 { 0x7cf00000, 0x3c900000,       RTL_GIGA_MAC_VER_23 },
1661                 { 0x7cf00000, 0x3c800000,       RTL_GIGA_MAC_VER_18 },
1662                 { 0x7c800000, 0x3c800000,       RTL_GIGA_MAC_VER_24 },
1663                 { 0x7cf00000, 0x3c000000,       RTL_GIGA_MAC_VER_19 },
1664                 { 0x7cf00000, 0x3c200000,       RTL_GIGA_MAC_VER_20 },
1665                 { 0x7cf00000, 0x3c300000,       RTL_GIGA_MAC_VER_21 },
1666                 { 0x7cf00000, 0x3c400000,       RTL_GIGA_MAC_VER_22 },
1667                 { 0x7c800000, 0x3c000000,       RTL_GIGA_MAC_VER_22 },
1668
1669                 /* 8168B family. */
1670                 { 0x7cf00000, 0x38000000,       RTL_GIGA_MAC_VER_12 },
1671                 { 0x7cf00000, 0x38500000,       RTL_GIGA_MAC_VER_17 },
1672                 { 0x7c800000, 0x38000000,       RTL_GIGA_MAC_VER_17 },
1673                 { 0x7c800000, 0x30000000,       RTL_GIGA_MAC_VER_11 },
1674
1675                 /* 8101 family. */
1676                 { 0x7cf00000, 0x40b00000,       RTL_GIGA_MAC_VER_30 },
1677                 { 0x7cf00000, 0x40a00000,       RTL_GIGA_MAC_VER_30 },
1678                 { 0x7cf00000, 0x40900000,       RTL_GIGA_MAC_VER_29 },
1679                 { 0x7c800000, 0x40800000,       RTL_GIGA_MAC_VER_30 },
1680                 { 0x7cf00000, 0x34a00000,       RTL_GIGA_MAC_VER_09 },
1681                 { 0x7cf00000, 0x24a00000,       RTL_GIGA_MAC_VER_09 },
1682                 { 0x7cf00000, 0x34900000,       RTL_GIGA_MAC_VER_08 },
1683                 { 0x7cf00000, 0x24900000,       RTL_GIGA_MAC_VER_08 },
1684                 { 0x7cf00000, 0x34800000,       RTL_GIGA_MAC_VER_07 },
1685                 { 0x7cf00000, 0x24800000,       RTL_GIGA_MAC_VER_07 },
1686                 { 0x7cf00000, 0x34000000,       RTL_GIGA_MAC_VER_13 },
1687                 { 0x7cf00000, 0x34300000,       RTL_GIGA_MAC_VER_10 },
1688                 { 0x7cf00000, 0x34200000,       RTL_GIGA_MAC_VER_16 },
1689                 { 0x7c800000, 0x34800000,       RTL_GIGA_MAC_VER_09 },
1690                 { 0x7c800000, 0x24800000,       RTL_GIGA_MAC_VER_09 },
1691                 { 0x7c800000, 0x34000000,       RTL_GIGA_MAC_VER_16 },
1692                 /* FIXME: where did these entries come from ? -- FR */
1693                 { 0xfc800000, 0x38800000,       RTL_GIGA_MAC_VER_15 },
1694                 { 0xfc800000, 0x30800000,       RTL_GIGA_MAC_VER_14 },
1695
1696                 /* 8110 family. */
1697                 { 0xfc800000, 0x98000000,       RTL_GIGA_MAC_VER_06 },
1698                 { 0xfc800000, 0x18000000,       RTL_GIGA_MAC_VER_05 },
1699                 { 0xfc800000, 0x10000000,       RTL_GIGA_MAC_VER_04 },
1700                 { 0xfc800000, 0x04000000,       RTL_GIGA_MAC_VER_03 },
1701                 { 0xfc800000, 0x00800000,       RTL_GIGA_MAC_VER_02 },
1702                 { 0xfc800000, 0x00000000,       RTL_GIGA_MAC_VER_01 },
1703
1704                 /* Catch-all */
1705                 { 0x00000000, 0x00000000,       RTL_GIGA_MAC_NONE   }
1706         }, *p = mac_info;
1707         u32 reg;
1708
1709         reg = RTL_R32(TxConfig);
1710         while ((reg & p->mask) != p->val)
1711                 p++;
1712         tp->mac_version = p->mac_version;
1713
1714         if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1715                 netif_notice(tp, probe, dev,
1716                              "unknown MAC, using family default\n");
1717                 tp->mac_version = default_version;
1718         }
1719 }
1720
1721 static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1722 {
1723         dprintk("mac_version = 0x%02x\n", tp->mac_version);
1724 }
1725
1726 struct phy_reg {
1727         u16 reg;
1728         u16 val;
1729 };
1730
1731 static void rtl_writephy_batch(struct rtl8169_private *tp,
1732                                const struct phy_reg *regs, int len)
1733 {
1734         while (len-- > 0) {
1735                 rtl_writephy(tp, regs->reg, regs->val);
1736                 regs++;
1737         }
1738 }
1739
1740 #define PHY_READ                0x00000000
1741 #define PHY_DATA_OR             0x10000000
1742 #define PHY_DATA_AND            0x20000000
1743 #define PHY_BJMPN               0x30000000
1744 #define PHY_READ_EFUSE          0x40000000
1745 #define PHY_READ_MAC_BYTE       0x50000000
1746 #define PHY_WRITE_MAC_BYTE      0x60000000
1747 #define PHY_CLEAR_READCOUNT     0x70000000
1748 #define PHY_WRITE               0x80000000
1749 #define PHY_READCOUNT_EQ_SKIP   0x90000000
1750 #define PHY_COMP_EQ_SKIPN       0xa0000000
1751 #define PHY_COMP_NEQ_SKIPN      0xb0000000
1752 #define PHY_WRITE_PREVIOUS      0xc0000000
1753 #define PHY_SKIPN               0xd0000000
1754 #define PHY_DELAY_MS            0xe0000000
1755 #define PHY_WRITE_ERI_WORD      0xf0000000
1756
1757 struct fw_info {
1758         u32     magic;
1759         char    version[RTL_VER_SIZE];
1760         __le32  fw_start;
1761         __le32  fw_len;
1762         u8      chksum;
1763 } __packed;
1764
1765 #define FW_OPCODE_SIZE  sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1766
1767 static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1768 {
1769         const struct firmware *fw = rtl_fw->fw;
1770         struct fw_info *fw_info = (struct fw_info *)fw->data;
1771         struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1772         char *version = rtl_fw->version;
1773         bool rc = false;
1774
1775         if (fw->size < FW_OPCODE_SIZE)
1776                 goto out;
1777
1778         if (!fw_info->magic) {
1779                 size_t i, size, start;
1780                 u8 checksum = 0;
1781
1782                 if (fw->size < sizeof(*fw_info))
1783                         goto out;
1784
1785                 for (i = 0; i < fw->size; i++)
1786                         checksum += fw->data[i];
1787                 if (checksum != 0)
1788                         goto out;
1789
1790                 start = le32_to_cpu(fw_info->fw_start);
1791                 if (start > fw->size)
1792                         goto out;
1793
1794                 size = le32_to_cpu(fw_info->fw_len);
1795                 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1796                         goto out;
1797
1798                 memcpy(version, fw_info->version, RTL_VER_SIZE);
1799
1800                 pa->code = (__le32 *)(fw->data + start);
1801                 pa->size = size;
1802         } else {
1803                 if (fw->size % FW_OPCODE_SIZE)
1804                         goto out;
1805
1806                 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1807
1808                 pa->code = (__le32 *)fw->data;
1809                 pa->size = fw->size / FW_OPCODE_SIZE;
1810         }
1811         version[RTL_VER_SIZE - 1] = 0;
1812
1813         rc = true;
1814 out:
1815         return rc;
1816 }
1817
1818 static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1819                            struct rtl_fw_phy_action *pa)
1820 {
1821         bool rc = false;
1822         size_t index;
1823
1824         for (index = 0; index < pa->size; index++) {
1825                 u32 action = le32_to_cpu(pa->code[index]);
1826                 u32 regno = (action & 0x0fff0000) >> 16;
1827
1828                 switch(action & 0xf0000000) {
1829                 case PHY_READ:
1830                 case PHY_DATA_OR:
1831                 case PHY_DATA_AND:
1832                 case PHY_READ_EFUSE:
1833                 case PHY_CLEAR_READCOUNT:
1834                 case PHY_WRITE:
1835                 case PHY_WRITE_PREVIOUS:
1836                 case PHY_DELAY_MS:
1837                         break;
1838
1839                 case PHY_BJMPN:
1840                         if (regno > index) {
1841                                 netif_err(tp, ifup, tp->dev,
1842                                           "Out of range of firmware\n");
1843                                 goto out;
1844                         }
1845                         break;
1846                 case PHY_READCOUNT_EQ_SKIP:
1847                         if (index + 2 >= pa->size) {
1848                                 netif_err(tp, ifup, tp->dev,
1849                                           "Out of range of firmware\n");
1850                                 goto out;
1851                         }
1852                         break;
1853                 case PHY_COMP_EQ_SKIPN:
1854                 case PHY_COMP_NEQ_SKIPN:
1855                 case PHY_SKIPN:
1856                         if (index + 1 + regno >= pa->size) {
1857                                 netif_err(tp, ifup, tp->dev,
1858                                           "Out of range of firmware\n");
1859                                 goto out;
1860                         }
1861                         break;
1862
1863                 case PHY_READ_MAC_BYTE:
1864                 case PHY_WRITE_MAC_BYTE:
1865                 case PHY_WRITE_ERI_WORD:
1866                 default:
1867                         netif_err(tp, ifup, tp->dev,
1868                                   "Invalid action 0x%08x\n", action);
1869                         goto out;
1870                 }
1871         }
1872         rc = true;
1873 out:
1874         return rc;
1875 }
1876
1877 static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1878 {
1879         struct net_device *dev = tp->dev;
1880         int rc = -EINVAL;
1881
1882         if (!rtl_fw_format_ok(tp, rtl_fw)) {
1883                 netif_err(tp, ifup, dev, "invalid firwmare\n");
1884                 goto out;
1885         }
1886
1887         if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1888                 rc = 0;
1889 out:
1890         return rc;
1891 }
1892
1893 static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1894 {
1895         struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1896         u32 predata, count;
1897         size_t index;
1898
1899         predata = count = 0;
1900
1901         for (index = 0; index < pa->size; ) {
1902                 u32 action = le32_to_cpu(pa->code[index]);
1903                 u32 data = action & 0x0000ffff;
1904                 u32 regno = (action & 0x0fff0000) >> 16;
1905
1906                 if (!action)
1907                         break;
1908
1909                 switch(action & 0xf0000000) {
1910                 case PHY_READ:
1911                         predata = rtl_readphy(tp, regno);
1912                         count++;
1913                         index++;
1914                         break;
1915                 case PHY_DATA_OR:
1916                         predata |= data;
1917                         index++;
1918                         break;
1919                 case PHY_DATA_AND:
1920                         predata &= data;
1921                         index++;
1922                         break;
1923                 case PHY_BJMPN:
1924                         index -= regno;
1925                         break;
1926                 case PHY_READ_EFUSE:
1927                         predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1928                         index++;
1929                         break;
1930                 case PHY_CLEAR_READCOUNT:
1931                         count = 0;
1932                         index++;
1933                         break;
1934                 case PHY_WRITE:
1935                         rtl_writephy(tp, regno, data);
1936                         index++;
1937                         break;
1938                 case PHY_READCOUNT_EQ_SKIP:
1939                         index += (count == data) ? 2 : 1;
1940                         break;
1941                 case PHY_COMP_EQ_SKIPN:
1942                         if (predata == data)
1943                                 index += regno;
1944                         index++;
1945                         break;
1946                 case PHY_COMP_NEQ_SKIPN:
1947                         if (predata != data)
1948                                 index += regno;
1949                         index++;
1950                         break;
1951                 case PHY_WRITE_PREVIOUS:
1952                         rtl_writephy(tp, regno, predata);
1953                         index++;
1954                         break;
1955                 case PHY_SKIPN:
1956                         index += regno + 1;
1957                         break;
1958                 case PHY_DELAY_MS:
1959                         mdelay(data);
1960                         index++;
1961                         break;
1962
1963                 case PHY_READ_MAC_BYTE:
1964                 case PHY_WRITE_MAC_BYTE:
1965                 case PHY_WRITE_ERI_WORD:
1966                 default:
1967                         BUG();
1968                 }
1969         }
1970 }
1971
1972 static void rtl_release_firmware(struct rtl8169_private *tp)
1973 {
1974         if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
1975                 release_firmware(tp->rtl_fw->fw);
1976                 kfree(tp->rtl_fw);
1977         }
1978         tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
1979 }
1980
1981 static void rtl_apply_firmware(struct rtl8169_private *tp)
1982 {
1983         struct rtl_fw *rtl_fw = tp->rtl_fw;
1984
1985         /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1986         if (!IS_ERR_OR_NULL(rtl_fw))
1987                 rtl_phy_write_fw(tp, rtl_fw);
1988 }
1989
1990 static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1991 {
1992         if (rtl_readphy(tp, reg) != val)
1993                 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1994         else
1995                 rtl_apply_firmware(tp);
1996 }
1997
1998 static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
1999 {
2000         static const struct phy_reg phy_reg_init[] = {
2001                 { 0x1f, 0x0001 },
2002                 { 0x06, 0x006e },
2003                 { 0x08, 0x0708 },
2004                 { 0x15, 0x4000 },
2005                 { 0x18, 0x65c7 },
2006
2007                 { 0x1f, 0x0001 },
2008                 { 0x03, 0x00a1 },
2009                 { 0x02, 0x0008 },
2010                 { 0x01, 0x0120 },
2011                 { 0x00, 0x1000 },
2012                 { 0x04, 0x0800 },
2013                 { 0x04, 0x0000 },
2014
2015                 { 0x03, 0xff41 },
2016                 { 0x02, 0xdf60 },
2017                 { 0x01, 0x0140 },
2018                 { 0x00, 0x0077 },
2019                 { 0x04, 0x7800 },
2020                 { 0x04, 0x7000 },
2021
2022                 { 0x03, 0x802f },
2023                 { 0x02, 0x4f02 },
2024                 { 0x01, 0x0409 },
2025                 { 0x00, 0xf0f9 },
2026                 { 0x04, 0x9800 },
2027                 { 0x04, 0x9000 },
2028
2029                 { 0x03, 0xdf01 },
2030                 { 0x02, 0xdf20 },
2031                 { 0x01, 0xff95 },
2032                 { 0x00, 0xba00 },
2033                 { 0x04, 0xa800 },
2034                 { 0x04, 0xa000 },
2035
2036                 { 0x03, 0xff41 },
2037                 { 0x02, 0xdf20 },
2038                 { 0x01, 0x0140 },
2039                 { 0x00, 0x00bb },
2040                 { 0x04, 0xb800 },
2041                 { 0x04, 0xb000 },
2042
2043                 { 0x03, 0xdf41 },
2044                 { 0x02, 0xdc60 },
2045                 { 0x01, 0x6340 },
2046                 { 0x00, 0x007d },
2047                 { 0x04, 0xd800 },
2048                 { 0x04, 0xd000 },
2049
2050                 { 0x03, 0xdf01 },
2051                 { 0x02, 0xdf20 },
2052                 { 0x01, 0x100a },
2053                 { 0x00, 0xa0ff },
2054                 { 0x04, 0xf800 },
2055                 { 0x04, 0xf000 },
2056
2057                 { 0x1f, 0x0000 },
2058                 { 0x0b, 0x0000 },
2059                 { 0x00, 0x9200 }
2060         };
2061
2062         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2063 }
2064
2065 static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
2066 {
2067         static const struct phy_reg phy_reg_init[] = {
2068                 { 0x1f, 0x0002 },
2069                 { 0x01, 0x90d0 },
2070                 { 0x1f, 0x0000 }
2071         };
2072
2073         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2074 }
2075
2076 static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
2077 {
2078         struct pci_dev *pdev = tp->pci_dev;
2079         u16 vendor_id, device_id;
2080
2081         pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
2082         pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
2083
2084         if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
2085                 return;
2086
2087         rtl_writephy(tp, 0x1f, 0x0001);
2088         rtl_writephy(tp, 0x10, 0xf01b);
2089         rtl_writephy(tp, 0x1f, 0x0000);
2090 }
2091
2092 static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
2093 {
2094         static const struct phy_reg phy_reg_init[] = {
2095                 { 0x1f, 0x0001 },
2096                 { 0x04, 0x0000 },
2097                 { 0x03, 0x00a1 },
2098                 { 0x02, 0x0008 },
2099                 { 0x01, 0x0120 },
2100                 { 0x00, 0x1000 },
2101                 { 0x04, 0x0800 },
2102                 { 0x04, 0x9000 },
2103                 { 0x03, 0x802f },
2104                 { 0x02, 0x4f02 },
2105                 { 0x01, 0x0409 },
2106                 { 0x00, 0xf099 },
2107                 { 0x04, 0x9800 },
2108                 { 0x04, 0xa000 },
2109                 { 0x03, 0xdf01 },
2110                 { 0x02, 0xdf20 },
2111                 { 0x01, 0xff95 },
2112                 { 0x00, 0xba00 },
2113                 { 0x04, 0xa800 },
2114                 { 0x04, 0xf000 },
2115                 { 0x03, 0xdf01 },
2116                 { 0x02, 0xdf20 },
2117                 { 0x01, 0x101a },
2118                 { 0x00, 0xa0ff },
2119                 { 0x04, 0xf800 },
2120                 { 0x04, 0x0000 },
2121                 { 0x1f, 0x0000 },
2122
2123                 { 0x1f, 0x0001 },
2124                 { 0x10, 0xf41b },
2125                 { 0x14, 0xfb54 },
2126                 { 0x18, 0xf5c7 },
2127                 { 0x1f, 0x0000 },
2128
2129                 { 0x1f, 0x0001 },
2130                 { 0x17, 0x0cc0 },
2131                 { 0x1f, 0x0000 }
2132         };
2133
2134         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2135
2136         rtl8169scd_hw_phy_config_quirk(tp);
2137 }
2138
2139 static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
2140 {
2141         static const struct phy_reg phy_reg_init[] = {
2142                 { 0x1f, 0x0001 },
2143                 { 0x04, 0x0000 },
2144                 { 0x03, 0x00a1 },
2145                 { 0x02, 0x0008 },
2146                 { 0x01, 0x0120 },
2147                 { 0x00, 0x1000 },
2148                 { 0x04, 0x0800 },
2149                 { 0x04, 0x9000 },
2150                 { 0x03, 0x802f },
2151                 { 0x02, 0x4f02 },
2152                 { 0x01, 0x0409 },
2153                 { 0x00, 0xf099 },
2154                 { 0x04, 0x9800 },
2155                 { 0x04, 0xa000 },
2156                 { 0x03, 0xdf01 },
2157                 { 0x02, 0xdf20 },
2158                 { 0x01, 0xff95 },
2159                 { 0x00, 0xba00 },
2160                 { 0x04, 0xa800 },
2161                 { 0x04, 0xf000 },
2162                 { 0x03, 0xdf01 },
2163                 { 0x02, 0xdf20 },
2164                 { 0x01, 0x101a },
2165                 { 0x00, 0xa0ff },
2166                 { 0x04, 0xf800 },
2167                 { 0x04, 0x0000 },
2168                 { 0x1f, 0x0000 },
2169
2170                 { 0x1f, 0x0001 },
2171                 { 0x0b, 0x8480 },
2172                 { 0x1f, 0x0000 },
2173
2174                 { 0x1f, 0x0001 },
2175                 { 0x18, 0x67c7 },
2176                 { 0x04, 0x2000 },
2177                 { 0x03, 0x002f },
2178                 { 0x02, 0x4360 },
2179                 { 0x01, 0x0109 },
2180                 { 0x00, 0x3022 },
2181                 { 0x04, 0x2800 },
2182                 { 0x1f, 0x0000 },
2183
2184                 { 0x1f, 0x0001 },
2185                 { 0x17, 0x0cc0 },
2186                 { 0x1f, 0x0000 }
2187         };
2188
2189         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2190 }
2191
2192 static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
2193 {
2194         static const struct phy_reg phy_reg_init[] = {
2195                 { 0x10, 0xf41b },
2196                 { 0x1f, 0x0000 }
2197         };
2198
2199         rtl_writephy(tp, 0x1f, 0x0001);
2200         rtl_patchphy(tp, 0x16, 1 << 0);
2201
2202         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2203 }
2204
2205 static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
2206 {
2207         static const struct phy_reg phy_reg_init[] = {
2208                 { 0x1f, 0x0001 },
2209                 { 0x10, 0xf41b },
2210                 { 0x1f, 0x0000 }
2211         };
2212
2213         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2214 }
2215
2216 static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
2217 {
2218         static const struct phy_reg phy_reg_init[] = {
2219                 { 0x1f, 0x0000 },
2220                 { 0x1d, 0x0f00 },
2221                 { 0x1f, 0x0002 },
2222                 { 0x0c, 0x1ec8 },
2223                 { 0x1f, 0x0000 }
2224         };
2225
2226         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2227 }
2228
2229 static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
2230 {
2231         static const struct phy_reg phy_reg_init[] = {
2232                 { 0x1f, 0x0001 },
2233                 { 0x1d, 0x3d98 },
2234                 { 0x1f, 0x0000 }
2235         };
2236
2237         rtl_writephy(tp, 0x1f, 0x0000);
2238         rtl_patchphy(tp, 0x14, 1 << 5);
2239         rtl_patchphy(tp, 0x0d, 1 << 5);
2240
2241         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2242 }
2243
2244 static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
2245 {
2246         static const struct phy_reg phy_reg_init[] = {
2247                 { 0x1f, 0x0001 },
2248                 { 0x12, 0x2300 },
2249                 { 0x1f, 0x0002 },
2250                 { 0x00, 0x88d4 },
2251                 { 0x01, 0x82b1 },
2252                 { 0x03, 0x7002 },
2253                 { 0x08, 0x9e30 },
2254                 { 0x09, 0x01f0 },
2255                 { 0x0a, 0x5500 },
2256                 { 0x0c, 0x00c8 },
2257                 { 0x1f, 0x0003 },
2258                 { 0x12, 0xc096 },
2259                 { 0x16, 0x000a },
2260                 { 0x1f, 0x0000 },
2261                 { 0x1f, 0x0000 },
2262                 { 0x09, 0x2000 },
2263                 { 0x09, 0x0000 }
2264         };
2265
2266         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2267
2268         rtl_patchphy(tp, 0x14, 1 << 5);
2269         rtl_patchphy(tp, 0x0d, 1 << 5);
2270         rtl_writephy(tp, 0x1f, 0x0000);
2271 }
2272
2273 static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
2274 {
2275         static const struct phy_reg phy_reg_init[] = {
2276                 { 0x1f, 0x0001 },
2277                 { 0x12, 0x2300 },
2278                 { 0x03, 0x802f },
2279                 { 0x02, 0x4f02 },
2280                 { 0x01, 0x0409 },
2281                 { 0x00, 0xf099 },
2282                 { 0x04, 0x9800 },
2283                 { 0x04, 0x9000 },
2284                 { 0x1d, 0x3d98 },
2285                 { 0x1f, 0x0002 },
2286                 { 0x0c, 0x7eb8 },
2287                 { 0x06, 0x0761 },
2288                 { 0x1f, 0x0003 },
2289                 { 0x16, 0x0f0a },
2290                 { 0x1f, 0x0000 }
2291         };
2292
2293         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2294
2295         rtl_patchphy(tp, 0x16, 1 << 0);
2296         rtl_patchphy(tp, 0x14, 1 << 5);
2297         rtl_patchphy(tp, 0x0d, 1 << 5);
2298         rtl_writephy(tp, 0x1f, 0x0000);
2299 }
2300
2301 static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
2302 {
2303         static const struct phy_reg phy_reg_init[] = {
2304                 { 0x1f, 0x0001 },
2305                 { 0x12, 0x2300 },
2306                 { 0x1d, 0x3d98 },
2307                 { 0x1f, 0x0002 },
2308                 { 0x0c, 0x7eb8 },
2309                 { 0x06, 0x5461 },
2310                 { 0x1f, 0x0003 },
2311                 { 0x16, 0x0f0a },
2312                 { 0x1f, 0x0000 }
2313         };
2314
2315         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2316
2317         rtl_patchphy(tp, 0x16, 1 << 0);
2318         rtl_patchphy(tp, 0x14, 1 << 5);
2319         rtl_patchphy(tp, 0x0d, 1 << 5);
2320         rtl_writephy(tp, 0x1f, 0x0000);
2321 }
2322
2323 static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
2324 {
2325         rtl8168c_3_hw_phy_config(tp);
2326 }
2327
2328 static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
2329 {
2330         static const struct phy_reg phy_reg_init_0[] = {
2331                 /* Channel Estimation */
2332                 { 0x1f, 0x0001 },
2333                 { 0x06, 0x4064 },
2334                 { 0x07, 0x2863 },
2335                 { 0x08, 0x059c },
2336                 { 0x09, 0x26b4 },
2337                 { 0x0a, 0x6a19 },
2338                 { 0x0b, 0xdcc8 },
2339                 { 0x10, 0xf06d },
2340                 { 0x14, 0x7f68 },
2341                 { 0x18, 0x7fd9 },
2342                 { 0x1c, 0xf0ff },
2343                 { 0x1d, 0x3d9c },
2344                 { 0x1f, 0x0003 },
2345                 { 0x12, 0xf49f },
2346                 { 0x13, 0x070b },
2347                 { 0x1a, 0x05ad },
2348                 { 0x14, 0x94c0 },
2349
2350                 /*
2351                  * Tx Error Issue
2352                  * Enhance line driver power
2353                  */
2354                 { 0x1f, 0x0002 },
2355                 { 0x06, 0x5561 },
2356                 { 0x1f, 0x0005 },
2357                 { 0x05, 0x8332 },
2358                 { 0x06, 0x5561 },
2359
2360                 /*
2361                  * Can not link to 1Gbps with bad cable
2362                  * Decrease SNR threshold form 21.07dB to 19.04dB
2363                  */
2364                 { 0x1f, 0x0001 },
2365                 { 0x17, 0x0cc0 },
2366
2367                 { 0x1f, 0x0000 },
2368                 { 0x0d, 0xf880 }
2369         };
2370         void __iomem *ioaddr = tp->mmio_addr;
2371
2372         rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2373
2374         /*
2375          * Rx Error Issue
2376          * Fine Tune Switching regulator parameter
2377          */
2378         rtl_writephy(tp, 0x1f, 0x0002);
2379         rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2380         rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
2381
2382         if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2383                 static const struct phy_reg phy_reg_init[] = {
2384                         { 0x1f, 0x0002 },
2385                         { 0x05, 0x669a },
2386                         { 0x1f, 0x0005 },
2387                         { 0x05, 0x8330 },
2388                         { 0x06, 0x669a },
2389                         { 0x1f, 0x0002 }
2390                 };
2391                 int val;
2392
2393                 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2394
2395                 val = rtl_readphy(tp, 0x0d);
2396
2397                 if ((val & 0x00ff) != 0x006c) {
2398                         static const u32 set[] = {
2399                                 0x0065, 0x0066, 0x0067, 0x0068,
2400                                 0x0069, 0x006a, 0x006b, 0x006c
2401                         };
2402                         int i;
2403
2404                         rtl_writephy(tp, 0x1f, 0x0002);
2405
2406                         val &= 0xff00;
2407                         for (i = 0; i < ARRAY_SIZE(set); i++)
2408                                 rtl_writephy(tp, 0x0d, val | set[i]);
2409                 }
2410         } else {
2411                 static const struct phy_reg phy_reg_init[] = {
2412                         { 0x1f, 0x0002 },
2413                         { 0x05, 0x6662 },
2414                         { 0x1f, 0x0005 },
2415                         { 0x05, 0x8330 },
2416                         { 0x06, 0x6662 }
2417                 };
2418
2419                 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2420         }
2421
2422         /* RSET couple improve */
2423         rtl_writephy(tp, 0x1f, 0x0002);
2424         rtl_patchphy(tp, 0x0d, 0x0300);
2425         rtl_patchphy(tp, 0x0f, 0x0010);
2426
2427         /* Fine tune PLL performance */
2428         rtl_writephy(tp, 0x1f, 0x0002);
2429         rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2430         rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2431
2432         rtl_writephy(tp, 0x1f, 0x0005);
2433         rtl_writephy(tp, 0x05, 0x001b);
2434
2435         rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
2436
2437         rtl_writephy(tp, 0x1f, 0x0000);
2438 }
2439
2440 static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
2441 {
2442         static const struct phy_reg phy_reg_init_0[] = {
2443                 /* Channel Estimation */
2444                 { 0x1f, 0x0001 },
2445                 { 0x06, 0x4064 },
2446                 { 0x07, 0x2863 },
2447                 { 0x08, 0x059c },
2448                 { 0x09, 0x26b4 },
2449                 { 0x0a, 0x6a19 },
2450                 { 0x0b, 0xdcc8 },
2451                 { 0x10, 0xf06d },
2452                 { 0x14, 0x7f68 },
2453                 { 0x18, 0x7fd9 },
2454                 { 0x1c, 0xf0ff },
2455                 { 0x1d, 0x3d9c },
2456                 { 0x1f, 0x0003 },
2457                 { 0x12, 0xf49f },
2458                 { 0x13, 0x070b },
2459                 { 0x1a, 0x05ad },
2460                 { 0x14, 0x94c0 },
2461
2462                 /*
2463                  * Tx Error Issue
2464                  * Enhance line driver power
2465                  */
2466                 { 0x1f, 0x0002 },
2467                 { 0x06, 0x5561 },
2468                 { 0x1f, 0x0005 },
2469                 { 0x05, 0x8332 },
2470                 { 0x06, 0x5561 },
2471
2472                 /*
2473                  * Can not link to 1Gbps with bad cable
2474                  * Decrease SNR threshold form 21.07dB to 19.04dB
2475                  */
2476                 { 0x1f, 0x0001 },
2477                 { 0x17, 0x0cc0 },
2478
2479                 { 0x1f, 0x0000 },
2480                 { 0x0d, 0xf880 }
2481         };
2482         void __iomem *ioaddr = tp->mmio_addr;
2483
2484         rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
2485
2486         if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
2487                 static const struct phy_reg phy_reg_init[] = {
2488                         { 0x1f, 0x0002 },
2489                         { 0x05, 0x669a },
2490                         { 0x1f, 0x0005 },
2491                         { 0x05, 0x8330 },
2492                         { 0x06, 0x669a },
2493
2494                         { 0x1f, 0x0002 }
2495                 };
2496                 int val;
2497
2498                 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2499
2500                 val = rtl_readphy(tp, 0x0d);
2501                 if ((val & 0x00ff) != 0x006c) {
2502                         static const u32 set[] = {
2503                                 0x0065, 0x0066, 0x0067, 0x0068,
2504                                 0x0069, 0x006a, 0x006b, 0x006c
2505                         };
2506                         int i;
2507
2508                         rtl_writephy(tp, 0x1f, 0x0002);
2509
2510                         val &= 0xff00;
2511                         for (i = 0; i < ARRAY_SIZE(set); i++)
2512                                 rtl_writephy(tp, 0x0d, val | set[i]);
2513                 }
2514         } else {
2515                 static const struct phy_reg phy_reg_init[] = {
2516                         { 0x1f, 0x0002 },
2517                         { 0x05, 0x2642 },
2518                         { 0x1f, 0x0005 },
2519                         { 0x05, 0x8330 },
2520                         { 0x06, 0x2642 }
2521                 };
2522
2523                 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2524         }
2525
2526         /* Fine tune PLL performance */
2527         rtl_writephy(tp, 0x1f, 0x0002);
2528         rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2529         rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
2530
2531         /* Switching regulator Slew rate */
2532         rtl_writephy(tp, 0x1f, 0x0002);
2533         rtl_patchphy(tp, 0x0f, 0x0017);
2534
2535         rtl_writephy(tp, 0x1f, 0x0005);
2536         rtl_writephy(tp, 0x05, 0x001b);
2537
2538         rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
2539
2540         rtl_writephy(tp, 0x1f, 0x0000);
2541 }
2542
2543 static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
2544 {
2545         static const struct phy_reg phy_reg_init[] = {
2546                 { 0x1f, 0x0002 },
2547                 { 0x10, 0x0008 },
2548                 { 0x0d, 0x006c },
2549
2550                 { 0x1f, 0x0000 },
2551                 { 0x0d, 0xf880 },
2552
2553                 { 0x1f, 0x0001 },
2554                 { 0x17, 0x0cc0 },
2555
2556                 { 0x1f, 0x0001 },
2557                 { 0x0b, 0xa4d8 },
2558                 { 0x09, 0x281c },
2559                 { 0x07, 0x2883 },
2560                 { 0x0a, 0x6b35 },
2561                 { 0x1d, 0x3da4 },
2562                 { 0x1c, 0xeffd },
2563                 { 0x14, 0x7f52 },
2564                 { 0x18, 0x7fc6 },
2565                 { 0x08, 0x0601 },
2566                 { 0x06, 0x4063 },
2567                 { 0x10, 0xf074 },
2568                 { 0x1f, 0x0003 },
2569                 { 0x13, 0x0789 },
2570                 { 0x12, 0xf4bd },
2571                 { 0x1a, 0x04fd },
2572                 { 0x14, 0x84b0 },
2573                 { 0x1f, 0x0000 },
2574                 { 0x00, 0x9200 },
2575
2576                 { 0x1f, 0x0005 },
2577                 { 0x01, 0x0340 },
2578                 { 0x1f, 0x0001 },
2579                 { 0x04, 0x4000 },
2580                 { 0x03, 0x1d21 },
2581                 { 0x02, 0x0c32 },
2582                 { 0x01, 0x0200 },
2583                 { 0x00, 0x5554 },
2584                 { 0x04, 0x4800 },
2585                 { 0x04, 0x4000 },
2586                 { 0x04, 0xf000 },
2587                 { 0x03, 0xdf01 },
2588                 { 0x02, 0xdf20 },
2589                 { 0x01, 0x101a },
2590                 { 0x00, 0xa0ff },
2591                 { 0x04, 0xf800 },
2592                 { 0x04, 0xf000 },
2593                 { 0x1f, 0x0000 },
2594
2595                 { 0x1f, 0x0007 },
2596                 { 0x1e, 0x0023 },
2597                 { 0x16, 0x0000 },
2598                 { 0x1f, 0x0000 }
2599         };
2600
2601         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2602 }
2603
2604 static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2605 {
2606         static const struct phy_reg phy_reg_init[] = {
2607                 { 0x1f, 0x0001 },
2608                 { 0x17, 0x0cc0 },
2609
2610                 { 0x1f, 0x0007 },
2611                 { 0x1e, 0x002d },
2612                 { 0x18, 0x0040 },
2613                 { 0x1f, 0x0000 }
2614         };
2615
2616         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2617         rtl_patchphy(tp, 0x0d, 1 << 5);
2618 }
2619
2620 static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2621 {
2622         static const struct phy_reg phy_reg_init[] = {
2623                 /* Enable Delay cap */
2624                 { 0x1f, 0x0005 },
2625                 { 0x05, 0x8b80 },
2626                 { 0x06, 0xc896 },
2627                 { 0x1f, 0x0000 },
2628
2629                 /* Channel estimation fine tune */
2630                 { 0x1f, 0x0001 },
2631                 { 0x0b, 0x6c20 },
2632                 { 0x07, 0x2872 },
2633                 { 0x1c, 0xefff },
2634                 { 0x1f, 0x0003 },
2635                 { 0x14, 0x6420 },
2636                 { 0x1f, 0x0000 },
2637
2638                 /* Update PFM & 10M TX idle timer */
2639                 { 0x1f, 0x0007 },
2640                 { 0x1e, 0x002f },
2641                 { 0x15, 0x1919 },
2642                 { 0x1f, 0x0000 },
2643
2644                 { 0x1f, 0x0007 },
2645                 { 0x1e, 0x00ac },
2646                 { 0x18, 0x0006 },
2647                 { 0x1f, 0x0000 }
2648         };
2649
2650         rtl_apply_firmware(tp);
2651
2652         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2653
2654         /* DCO enable for 10M IDLE Power */
2655         rtl_writephy(tp, 0x1f, 0x0007);
2656         rtl_writephy(tp, 0x1e, 0x0023);
2657         rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2658         rtl_writephy(tp, 0x1f, 0x0000);
2659
2660         /* For impedance matching */
2661         rtl_writephy(tp, 0x1f, 0x0002);
2662         rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
2663         rtl_writephy(tp, 0x1f, 0x0000);
2664
2665         /* PHY auto speed down */
2666         rtl_writephy(tp, 0x1f, 0x0007);
2667         rtl_writephy(tp, 0x1e, 0x002d);
2668         rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2669         rtl_writephy(tp, 0x1f, 0x0000);
2670         rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2671
2672         rtl_writephy(tp, 0x1f, 0x0005);
2673         rtl_writephy(tp, 0x05, 0x8b86);
2674         rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2675         rtl_writephy(tp, 0x1f, 0x0000);
2676
2677         rtl_writephy(tp, 0x1f, 0x0005);
2678         rtl_writephy(tp, 0x05, 0x8b85);
2679         rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2680         rtl_writephy(tp, 0x1f, 0x0007);
2681         rtl_writephy(tp, 0x1e, 0x0020);
2682         rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2683         rtl_writephy(tp, 0x1f, 0x0006);
2684         rtl_writephy(tp, 0x00, 0x5a00);
2685         rtl_writephy(tp, 0x1f, 0x0000);
2686         rtl_writephy(tp, 0x0d, 0x0007);
2687         rtl_writephy(tp, 0x0e, 0x003c);
2688         rtl_writephy(tp, 0x0d, 0x4007);
2689         rtl_writephy(tp, 0x0e, 0x0000);
2690         rtl_writephy(tp, 0x0d, 0x0000);
2691 }
2692
2693 static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
2694 {
2695         static const struct phy_reg phy_reg_init[] = {
2696                 { 0x1f, 0x0003 },
2697                 { 0x08, 0x441d },
2698                 { 0x01, 0x9100 },
2699                 { 0x1f, 0x0000 }
2700         };
2701
2702         rtl_writephy(tp, 0x1f, 0x0000);
2703         rtl_patchphy(tp, 0x11, 1 << 12);
2704         rtl_patchphy(tp, 0x19, 1 << 13);
2705         rtl_patchphy(tp, 0x10, 1 << 15);
2706
2707         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2708 }
2709
2710 static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2711 {
2712         static const struct phy_reg phy_reg_init[] = {
2713                 { 0x1f, 0x0005 },
2714                 { 0x1a, 0x0000 },
2715                 { 0x1f, 0x0000 },
2716
2717                 { 0x1f, 0x0004 },
2718                 { 0x1c, 0x0000 },
2719                 { 0x1f, 0x0000 },
2720
2721                 { 0x1f, 0x0001 },
2722                 { 0x15, 0x7701 },
2723                 { 0x1f, 0x0000 }
2724         };
2725
2726         /* Disable ALDPS before ram code */
2727         rtl_writephy(tp, 0x1f, 0x0000);
2728         rtl_writephy(tp, 0x18, 0x0310);
2729         msleep(100);
2730
2731         rtl_apply_firmware(tp);
2732
2733         rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2734 }
2735
2736 static void rtl_hw_phy_config(struct net_device *dev)
2737 {
2738         struct rtl8169_private *tp = netdev_priv(dev);
2739
2740         rtl8169_print_mac_version(tp);
2741
2742         switch (tp->mac_version) {
2743         case RTL_GIGA_MAC_VER_01:
2744                 break;
2745         case RTL_GIGA_MAC_VER_02:
2746         case RTL_GIGA_MAC_VER_03:
2747                 rtl8169s_hw_phy_config(tp);
2748                 break;
2749         case RTL_GIGA_MAC_VER_04:
2750                 rtl8169sb_hw_phy_config(tp);
2751                 break;
2752         case RTL_GIGA_MAC_VER_05:
2753                 rtl8169scd_hw_phy_config(tp);
2754                 break;
2755         case RTL_GIGA_MAC_VER_06:
2756                 rtl8169sce_hw_phy_config(tp);
2757                 break;
2758         case RTL_GIGA_MAC_VER_07:
2759         case RTL_GIGA_MAC_VER_08:
2760         case RTL_GIGA_MAC_VER_09:
2761                 rtl8102e_hw_phy_config(tp);
2762                 break;
2763         case RTL_GIGA_MAC_VER_11:
2764                 rtl8168bb_hw_phy_config(tp);
2765                 break;
2766         case RTL_GIGA_MAC_VER_12:
2767                 rtl8168bef_hw_phy_config(tp);
2768                 break;
2769         case RTL_GIGA_MAC_VER_17:
2770                 rtl8168bef_hw_phy_config(tp);
2771                 break;
2772         case RTL_GIGA_MAC_VER_18:
2773                 rtl8168cp_1_hw_phy_config(tp);
2774                 break;
2775         case RTL_GIGA_MAC_VER_19:
2776                 rtl8168c_1_hw_phy_config(tp);
2777                 break;
2778         case RTL_GIGA_MAC_VER_20:
2779                 rtl8168c_2_hw_phy_config(tp);
2780                 break;
2781         case RTL_GIGA_MAC_VER_21:
2782                 rtl8168c_3_hw_phy_config(tp);
2783                 break;
2784         case RTL_GIGA_MAC_VER_22:
2785                 rtl8168c_4_hw_phy_config(tp);
2786                 break;
2787         case RTL_GIGA_MAC_VER_23:
2788         case RTL_GIGA_MAC_VER_24:
2789                 rtl8168cp_2_hw_phy_config(tp);
2790                 break;
2791         case RTL_GIGA_MAC_VER_25:
2792                 rtl8168d_1_hw_phy_config(tp);
2793                 break;
2794         case RTL_GIGA_MAC_VER_26:
2795                 rtl8168d_2_hw_phy_config(tp);
2796                 break;
2797         case RTL_GIGA_MAC_VER_27:
2798                 rtl8168d_3_hw_phy_config(tp);
2799                 break;
2800         case RTL_GIGA_MAC_VER_28:
2801                 rtl8168d_4_hw_phy_config(tp);
2802                 break;
2803         case RTL_GIGA_MAC_VER_29:
2804         case RTL_GIGA_MAC_VER_30:
2805                 rtl8105e_hw_phy_config(tp);
2806                 break;
2807         case RTL_GIGA_MAC_VER_31:
2808                 /* None. */
2809                 break;
2810         case RTL_GIGA_MAC_VER_32:
2811         case RTL_GIGA_MAC_VER_33:
2812                 rtl8168e_hw_phy_config(tp);
2813                 break;
2814
2815         default:
2816                 break;
2817         }
2818 }
2819
2820 static void rtl8169_phy_timer(unsigned long __opaque)
2821 {
2822         struct net_device *dev = (struct net_device *)__opaque;
2823         struct rtl8169_private *tp = netdev_priv(dev);
2824         struct timer_list *timer = &tp->timer;
2825         void __iomem *ioaddr = tp->mmio_addr;
2826         unsigned long timeout = RTL8169_PHY_TIMEOUT;
2827
2828         assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
2829
2830         spin_lock_irq(&tp->lock);
2831
2832         if (tp->phy_reset_pending(tp)) {
2833                 /*
2834                  * A busy loop could burn quite a few cycles on nowadays CPU.
2835                  * Let's delay the execution of the timer for a few ticks.
2836                  */
2837                 timeout = HZ/10;
2838                 goto out_mod_timer;
2839         }
2840
2841         if (tp->link_ok(ioaddr))
2842                 goto out_unlock;
2843
2844         netif_warn(tp, link, dev, "PHY reset until link up\n");
2845
2846         tp->phy_reset_enable(tp);
2847
2848 out_mod_timer:
2849         mod_timer(timer, jiffies + timeout);
2850 out_unlock:
2851         spin_unlock_irq(&tp->lock);
2852 }
2853
2854 #ifdef CONFIG_NET_POLL_CONTROLLER
2855 /*
2856  * Polling 'interrupt' - used by things like netconsole to send skbs
2857  * without having to re-enable interrupts. It's not called while
2858  * the interrupt routine is executing.
2859  */
2860 static void rtl8169_netpoll(struct net_device *dev)
2861 {
2862         struct rtl8169_private *tp = netdev_priv(dev);
2863         struct pci_dev *pdev = tp->pci_dev;
2864
2865         disable_irq(pdev->irq);
2866         rtl8169_interrupt(pdev->irq, dev);
2867         enable_irq(pdev->irq);
2868 }
2869 #endif
2870
2871 static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2872                                   void __iomem *ioaddr)
2873 {
2874         iounmap(ioaddr);
2875         pci_release_regions(pdev);
2876         pci_clear_mwi(pdev);
2877         pci_disable_device(pdev);
2878         free_netdev(dev);
2879 }
2880
2881 static void rtl8169_phy_reset(struct net_device *dev,
2882                               struct rtl8169_private *tp)
2883 {
2884         unsigned int i;
2885
2886         tp->phy_reset_enable(tp);
2887         for (i = 0; i < 100; i++) {
2888                 if (!tp->phy_reset_pending(tp))
2889                         return;
2890                 msleep(1);
2891         }
2892         netif_err(tp, link, dev, "PHY reset failed\n");
2893 }
2894
2895 static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
2896 {
2897         void __iomem *ioaddr = tp->mmio_addr;
2898
2899         rtl_hw_phy_config(dev);
2900
2901         if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2902                 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2903                 RTL_W8(0x82, 0x01);
2904         }
2905
2906         pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2907
2908         if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2909                 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
2910
2911         if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
2912                 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2913                 RTL_W8(0x82, 0x01);
2914                 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
2915                 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
2916         }
2917
2918         rtl8169_phy_reset(dev, tp);
2919
2920         rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
2921                           ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2922                           ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2923                           (tp->mii.supports_gmii ?
2924                            ADVERTISED_1000baseT_Half |
2925                            ADVERTISED_1000baseT_Full : 0));
2926
2927         if (RTL_R8(PHYstatus) & TBI_Enable)
2928                 netif_info(tp, link, dev, "TBI auto-negotiating\n");
2929 }
2930
2931 static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2932 {
2933         void __iomem *ioaddr = tp->mmio_addr;
2934         u32 high;
2935         u32 low;
2936
2937         low  = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2938         high = addr[4] | (addr[5] << 8);
2939
2940         spin_lock_irq(&tp->lock);
2941
2942         RTL_W8(Cfg9346, Cfg9346_Unlock);
2943
2944         RTL_W32(MAC4, high);
2945         RTL_R32(MAC4);
2946
2947         RTL_W32(MAC0, low);
2948         RTL_R32(MAC0);
2949
2950         RTL_W8(Cfg9346, Cfg9346_Lock);
2951
2952         spin_unlock_irq(&tp->lock);
2953 }
2954
2955 static int rtl_set_mac_address(struct net_device *dev, void *p)
2956 {
2957         struct rtl8169_private *tp = netdev_priv(dev);
2958         struct sockaddr *addr = p;
2959
2960         if (!is_valid_ether_addr(addr->sa_data))
2961                 return -EADDRNOTAVAIL;
2962
2963         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2964
2965         rtl_rar_set(tp, dev->dev_addr);
2966
2967         return 0;
2968 }
2969
2970 static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2971 {
2972         struct rtl8169_private *tp = netdev_priv(dev);
2973         struct mii_ioctl_data *data = if_mii(ifr);
2974
2975         return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2976 }
2977
2978 static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2979                           struct mii_ioctl_data *data, int cmd)
2980 {
2981         switch (cmd) {
2982         case SIOCGMIIPHY:
2983                 data->phy_id = 32; /* Internal PHY */
2984                 return 0;
2985
2986         case SIOCGMIIREG:
2987                 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
2988                 return 0;
2989
2990         case SIOCSMIIREG:
2991                 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
2992                 return 0;
2993         }
2994         return -EOPNOTSUPP;
2995 }
2996
2997 static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2998 {
2999         return -EOPNOTSUPP;
3000 }
3001
3002 static const struct rtl_cfg_info {
3003         void (*hw_start)(struct net_device *);
3004         unsigned int region;
3005         unsigned int align;
3006         u16 intr_event;
3007         u16 napi_event;
3008         unsigned features;
3009         u8 default_ver;
3010 } rtl_cfg_infos [] = {
3011         [RTL_CFG_0] = {
3012                 .hw_start       = rtl_hw_start_8169,
3013                 .region         = 1,
3014                 .align          = 0,
3015                 .intr_event     = SYSErr | LinkChg | RxOverflow |
3016                                   RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
3017                 .napi_event     = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
3018                 .features       = RTL_FEATURE_GMII,
3019                 .default_ver    = RTL_GIGA_MAC_VER_01,
3020         },
3021         [RTL_CFG_1] = {
3022                 .hw_start       = rtl_hw_start_8168,
3023                 .region         = 2,
3024                 .align          = 8,
3025                 .intr_event     = SYSErr | LinkChg | RxOverflow |
3026                                   TxErr | TxOK | RxOK | RxErr,
3027                 .napi_event     = TxErr | TxOK | RxOK | RxOverflow,
3028                 .features       = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3029                 .default_ver    = RTL_GIGA_MAC_VER_11,
3030         },
3031         [RTL_CFG_2] = {
3032                 .hw_start       = rtl_hw_start_8101,
3033                 .region         = 2,
3034                 .align          = 8,
3035                 .intr_event     = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3036                                   RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
3037                 .napi_event     = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
3038                 .features       = RTL_FEATURE_MSI,
3039                 .default_ver    = RTL_GIGA_MAC_VER_13,
3040         }
3041 };
3042
3043 /* Cfg9346_Unlock assumed. */
3044 static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3045                             const struct rtl_cfg_info *cfg)
3046 {
3047         unsigned msi = 0;
3048         u8 cfg2;
3049
3050         cfg2 = RTL_R8(Config2) & ~MSIEnable;
3051         if (cfg->features & RTL_FEATURE_MSI) {
3052                 if (pci_enable_msi(pdev)) {
3053                         dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3054                 } else {
3055                         cfg2 |= MSIEnable;
3056                         msi = RTL_FEATURE_MSI;
3057                 }
3058         }
3059         RTL_W8(Config2, cfg2);
3060         return msi;
3061 }
3062
3063 static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3064 {
3065         if (tp->features & RTL_FEATURE_MSI) {
3066                 pci_disable_msi(pdev);
3067                 tp->features &= ~RTL_FEATURE_MSI;
3068         }
3069 }
3070
3071 static const struct net_device_ops rtl8169_netdev_ops = {
3072         .ndo_open               = rtl8169_open,
3073         .ndo_stop               = rtl8169_close,
3074         .ndo_get_stats          = rtl8169_get_stats,
3075         .ndo_start_xmit         = rtl8169_start_xmit,
3076         .ndo_tx_timeout         = rtl8169_tx_timeout,
3077         .ndo_validate_addr      = eth_validate_addr,
3078         .ndo_change_mtu         = rtl8169_change_mtu,
3079         .ndo_fix_features       = rtl8169_fix_features,
3080         .ndo_set_features       = rtl8169_set_features,
3081         .ndo_set_mac_address    = rtl_set_mac_address,
3082         .ndo_do_ioctl           = rtl8169_ioctl,
3083         .ndo_set_multicast_list = rtl_set_rx_mode,
3084 #ifdef CONFIG_NET_POLL_CONTROLLER
3085         .ndo_poll_controller    = rtl8169_netpoll,
3086 #endif
3087
3088 };
3089
3090 static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3091 {
3092         struct mdio_ops *ops = &tp->mdio_ops;
3093
3094         switch (tp->mac_version) {
3095         case RTL_GIGA_MAC_VER_27:
3096                 ops->write      = r8168dp_1_mdio_write;
3097                 ops->read       = r8168dp_1_mdio_read;
3098                 break;
3099         case RTL_GIGA_MAC_VER_28:
3100         case RTL_GIGA_MAC_VER_31:
3101                 ops->write      = r8168dp_2_mdio_write;
3102                 ops->read       = r8168dp_2_mdio_read;
3103                 break;
3104         default:
3105                 ops->write      = r8169_mdio_write;
3106                 ops->read       = r8169_mdio_read;
3107                 break;
3108         }
3109 }
3110
3111 static void r810x_phy_power_down(struct rtl8169_private *tp)
3112 {
3113         rtl_writephy(tp, 0x1f, 0x0000);
3114         rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3115 }
3116
3117 static void r810x_phy_power_up(struct rtl8169_private *tp)
3118 {
3119         rtl_writephy(tp, 0x1f, 0x0000);
3120         rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3121 }
3122
3123 static void r810x_pll_power_down(struct rtl8169_private *tp)
3124 {
3125         if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3126                 rtl_writephy(tp, 0x1f, 0x0000);
3127                 rtl_writephy(tp, MII_BMCR, 0x0000);
3128                 return;
3129         }
3130
3131         r810x_phy_power_down(tp);
3132 }
3133
3134 static void r810x_pll_power_up(struct rtl8169_private *tp)
3135 {
3136         r810x_phy_power_up(tp);
3137 }
3138
3139 static void r8168_phy_power_up(struct rtl8169_private *tp)
3140 {
3141         rtl_writephy(tp, 0x1f, 0x0000);
3142         switch (tp->mac_version) {
3143         case RTL_GIGA_MAC_VER_11:
3144         case RTL_GIGA_MAC_VER_12:
3145         case RTL_GIGA_MAC_VER_17:
3146         case RTL_GIGA_MAC_VER_18:
3147         case RTL_GIGA_MAC_VER_19:
3148         case RTL_GIGA_MAC_VER_20:
3149         case RTL_GIGA_MAC_VER_21:
3150         case RTL_GIGA_MAC_VER_22:
3151         case RTL_GIGA_MAC_VER_23:
3152         case RTL_GIGA_MAC_VER_24:
3153         case RTL_GIGA_MAC_VER_25:
3154         case RTL_GIGA_MAC_VER_26:
3155         case RTL_GIGA_MAC_VER_27:
3156         case RTL_GIGA_MAC_VER_28:
3157         case RTL_GIGA_MAC_VER_31:
3158                 rtl_writephy(tp, 0x0e, 0x0000);
3159                 break;
3160         default:
3161                 break;
3162         }
3163         rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3164 }
3165
3166 static void r8168_phy_power_down(struct rtl8169_private *tp)
3167 {
3168         rtl_writephy(tp, 0x1f, 0x0000);
3169         switch (tp->mac_version) {
3170         case RTL_GIGA_MAC_VER_32:
3171         case RTL_GIGA_MAC_VER_33:
3172                 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3173                 break;
3174
3175         case RTL_GIGA_MAC_VER_11:
3176         case RTL_GIGA_MAC_VER_12:
3177         case RTL_GIGA_MAC_VER_17:
3178         case RTL_GIGA_MAC_VER_18:
3179         case RTL_GIGA_MAC_VER_19:
3180         case RTL_GIGA_MAC_VER_20:
3181         case RTL_GIGA_MAC_VER_21:
3182         case RTL_GIGA_MAC_VER_22:
3183         case RTL_GIGA_MAC_VER_23:
3184         case RTL_GIGA_MAC_VER_24:
3185         case RTL_GIGA_MAC_VER_25:
3186         case RTL_GIGA_MAC_VER_26:
3187         case RTL_GIGA_MAC_VER_27:
3188         case RTL_GIGA_MAC_VER_28:
3189         case RTL_GIGA_MAC_VER_31:
3190                 rtl_writephy(tp, 0x0e, 0x0200);
3191         default:
3192                 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3193                 break;
3194         }
3195 }
3196
3197 static void r8168_pll_power_down(struct rtl8169_private *tp)
3198 {
3199         void __iomem *ioaddr = tp->mmio_addr;
3200
3201         if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3202              tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3203              tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3204             r8168dp_check_dash(tp)) {
3205                 return;
3206         }
3207
3208         if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3209              tp->mac_version == RTL_GIGA_MAC_VER_24) &&
3210             (RTL_R16(CPlusCmd) & ASF)) {
3211                 return;
3212         }
3213
3214         if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3215             tp->mac_version == RTL_GIGA_MAC_VER_33)
3216                 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3217
3218         if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3219                 rtl_writephy(tp, 0x1f, 0x0000);
3220                 rtl_writephy(tp, MII_BMCR, 0x0000);
3221
3222                 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3223                         AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3224                 return;
3225         }
3226
3227         r8168_phy_power_down(tp);
3228
3229         switch (tp->mac_version) {
3230         case RTL_GIGA_MAC_VER_25:
3231         case RTL_GIGA_MAC_VER_26:
3232         case RTL_GIGA_MAC_VER_27:
3233         case RTL_GIGA_MAC_VER_28:
3234         case RTL_GIGA_MAC_VER_31:
3235         case RTL_GIGA_MAC_VER_32:
3236         case RTL_GIGA_MAC_VER_33:
3237                 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3238                 break;
3239         }
3240 }
3241
3242 static void r8168_pll_power_up(struct rtl8169_private *tp)
3243 {
3244         void __iomem *ioaddr = tp->mmio_addr;
3245
3246         if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3247              tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3248              tp->mac_version == RTL_GIGA_MAC_VER_31) &&
3249             r8168dp_check_dash(tp)) {
3250                 return;
3251         }
3252
3253         switch (tp->mac_version) {
3254         case RTL_GIGA_MAC_VER_25:
3255         case RTL_GIGA_MAC_VER_26:
3256         case RTL_GIGA_MAC_VER_27:
3257         case RTL_GIGA_MAC_VER_28:
3258         case RTL_GIGA_MAC_VER_31:
3259         case RTL_GIGA_MAC_VER_32:
3260         case RTL_GIGA_MAC_VER_33:
3261                 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3262                 break;
3263         }
3264
3265         r8168_phy_power_up(tp);
3266 }
3267
3268 static void rtl_pll_power_op(struct rtl8169_private *tp,
3269                              void (*op)(struct rtl8169_private *))
3270 {
3271         if (op)
3272                 op(tp);
3273 }
3274
3275 static void rtl_pll_power_down(struct rtl8169_private *tp)
3276 {
3277         rtl_pll_power_op(tp, tp->pll_power_ops.down);
3278 }
3279
3280 static void rtl_pll_power_up(struct rtl8169_private *tp)
3281 {
3282         rtl_pll_power_op(tp, tp->pll_power_ops.up);
3283 }
3284
3285 static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3286 {
3287         struct pll_power_ops *ops = &tp->pll_power_ops;
3288
3289         switch (tp->mac_version) {
3290         case RTL_GIGA_MAC_VER_07:
3291         case RTL_GIGA_MAC_VER_08:
3292         case RTL_GIGA_MAC_VER_09:
3293         case RTL_GIGA_MAC_VER_10:
3294         case RTL_GIGA_MAC_VER_16:
3295         case RTL_GIGA_MAC_VER_29:
3296         case RTL_GIGA_MAC_VER_30:
3297                 ops->down       = r810x_pll_power_down;
3298                 ops->up         = r810x_pll_power_up;
3299                 break;
3300
3301         case RTL_GIGA_MAC_VER_11:
3302         case RTL_GIGA_MAC_VER_12:
3303         case RTL_GIGA_MAC_VER_17:
3304         case RTL_GIGA_MAC_VER_18:
3305         case RTL_GIGA_MAC_VER_19:
3306         case RTL_GIGA_MAC_VER_20:
3307         case RTL_GIGA_MAC_VER_21:
3308         case RTL_GIGA_MAC_VER_22:
3309         case RTL_GIGA_MAC_VER_23:
3310         case RTL_GIGA_MAC_VER_24:
3311         case RTL_GIGA_MAC_VER_25:
3312         case RTL_GIGA_MAC_VER_26:
3313         case RTL_GIGA_MAC_VER_27:
3314         case RTL_GIGA_MAC_VER_28:
3315         case RTL_GIGA_MAC_VER_31:
3316         case RTL_GIGA_MAC_VER_32:
3317         case RTL_GIGA_MAC_VER_33:
3318                 ops->down       = r8168_pll_power_down;
3319                 ops->up         = r8168_pll_power_up;
3320                 break;
3321
3322         default:
3323                 ops->down       = NULL;
3324                 ops->up         = NULL;
3325                 break;
3326         }
3327 }
3328
3329 static void rtl_hw_reset(struct rtl8169_private *tp)
3330 {
3331         void __iomem *ioaddr = tp->mmio_addr;
3332         int i;
3333
3334         /* Soft reset the chip. */
3335         RTL_W8(ChipCmd, CmdReset);
3336
3337         /* Check that the chip has finished the reset. */
3338         for (i = 0; i < 100; i++) {
3339                 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3340                         break;
3341                 msleep_interruptible(1);
3342         }
3343 }
3344
3345 static int __devinit
3346 rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3347 {
3348         const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3349         const unsigned int region = cfg->region;
3350         struct rtl8169_private *tp;
3351         struct mii_if_info *mii;
3352         struct net_device *dev;
3353         void __iomem *ioaddr;
3354         int chipset, i;
3355         int rc;
3356
3357         if (netif_msg_drv(&debug)) {
3358                 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3359                        MODULENAME, RTL8169_VERSION);
3360         }
3361
3362         dev = alloc_etherdev(sizeof (*tp));
3363         if (!dev) {
3364                 if (netif_msg_drv(&debug))
3365                         dev_err(&pdev->dev, "unable to alloc new ethernet\n");
3366                 rc = -ENOMEM;
3367                 goto out;
3368         }
3369
3370         SET_NETDEV_DEV(dev, &pdev->dev);
3371         dev->netdev_ops = &rtl8169_netdev_ops;
3372         tp = netdev_priv(dev);
3373         tp->dev = dev;
3374         tp->pci_dev = pdev;
3375         tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
3376
3377         mii = &tp->mii;
3378         mii->dev = dev;
3379         mii->mdio_read = rtl_mdio_read;
3380         mii->mdio_write = rtl_mdio_write;
3381         mii->phy_id_mask = 0x1f;
3382         mii->reg_num_mask = 0x1f;
3383         mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3384
3385         /* disable ASPM completely as that cause random device stop working
3386          * problems as well as full system hangs for some PCIe devices users */
3387         pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3388                                      PCIE_LINK_STATE_CLKPM);
3389
3390         /* enable device (incl. PCI PM wakeup and hotplug setup) */
3391         rc = pci_enable_device(pdev);
3392         if (rc < 0) {
3393                 netif_err(tp, probe, dev, "enable failure\n");
3394                 goto err_out_free_dev_1;
3395         }
3396
3397         if (pci_set_mwi(pdev) < 0)
3398                 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
3399
3400         /* make sure PCI base addr 1 is MMIO */
3401         if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
3402                 netif_err(tp, probe, dev,
3403                           "region #%d not an MMIO resource, aborting\n",
3404                           region);
3405                 rc = -ENODEV;
3406                 goto err_out_mwi_2;
3407         }
3408
3409         /* check for weird/broken PCI region reporting */
3410         if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
3411                 netif_err(tp, probe, dev,
3412                           "Invalid PCI region size(s), aborting\n");
3413                 rc = -ENODEV;
3414                 goto err_out_mwi_2;
3415         }
3416
3417         rc = pci_request_regions(pdev, MODULENAME);
3418         if (rc < 0) {
3419                 netif_err(tp, probe, dev, "could not request regions\n");
3420                 goto err_out_mwi_2;
3421         }
3422
3423         tp->cp_cmd = RxChkSum;
3424
3425         if ((sizeof(dma_addr_t) > 4) &&
3426             !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
3427                 tp->cp_cmd |= PCIDAC;
3428                 dev->features |= NETIF_F_HIGHDMA;
3429         } else {
3430                 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3431                 if (rc < 0) {
3432                         netif_err(tp, probe, dev, "DMA configuration failed\n");
3433                         goto err_out_free_res_3;
3434                 }
3435         }
3436
3437         /* ioremap MMIO region */
3438         ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
3439         if (!ioaddr) {
3440                 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
3441                 rc = -EIO;
3442                 goto err_out_free_res_3;
3443         }
3444         tp->mmio_addr = ioaddr;
3445
3446         tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3447         if (!tp->pcie_cap)
3448                 netif_info(tp, probe, dev, "no PCI Express capability\n");
3449
3450         RTL_W16(IntrMask, 0x0000);
3451
3452         rtl_hw_reset(tp);
3453
3454         RTL_W16(IntrStatus, 0xffff);
3455
3456         pci_set_master(pdev);
3457
3458         /* Identify chip attached to board */
3459         rtl8169_get_mac_version(tp, dev, cfg->default_ver);
3460
3461         /*
3462          * Pretend we are using VLANs; This bypasses a nasty bug where
3463          * Interrupts stop flowing on high load on 8110SCd controllers.
3464          */
3465         if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3466                 tp->cp_cmd |= RxVlan;
3467
3468         rtl_init_mdio_ops(tp);
3469         rtl_init_pll_power_ops(tp);
3470
3471         rtl8169_print_mac_version(tp);
3472
3473         chipset = tp->mac_version;
3474         tp->txd_version = rtl_chip_infos[chipset].txd_version;
3475
3476         RTL_W8(Cfg9346, Cfg9346_Unlock);
3477         RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3478         RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
3479         if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3480                 tp->features |= RTL_FEATURE_WOL;
3481         if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3482                 tp->features |= RTL_FEATURE_WOL;
3483         tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
3484         RTL_W8(Cfg9346, Cfg9346_Lock);
3485
3486         if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3487             (RTL_R8(PHYstatus) & TBI_Enable)) {
3488                 tp->set_speed = rtl8169_set_speed_tbi;
3489                 tp->get_settings = rtl8169_gset_tbi;
3490                 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3491                 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3492                 tp->link_ok = rtl8169_tbi_link_ok;
3493                 tp->do_ioctl = rtl_tbi_ioctl;
3494         } else {
3495                 tp->set_speed = rtl8169_set_speed_xmii;
3496                 tp->get_settings = rtl8169_gset_xmii;
3497                 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3498                 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3499                 tp->link_ok = rtl8169_xmii_link_ok;
3500                 tp->do_ioctl = rtl_xmii_ioctl;
3501         }
3502
3503         spin_lock_init(&tp->lock);
3504
3505         /* Get MAC address */
3506         for (i = 0; i < MAC_ADDR_LEN; i++)
3507                 dev->dev_addr[i] = RTL_R8(MAC0 + i);
3508         memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
3509
3510         SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
3511         dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3512         dev->irq = pdev->irq;
3513         dev->base_addr = (unsigned long) ioaddr;
3514
3515         netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
3516
3517         /* don't enable SG, IP_CSUM and TSO by default - it might not work
3518          * properly for all devices */
3519         dev->features |= NETIF_F_RXCSUM |
3520                 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3521
3522         dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3523                 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3524         dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3525                 NETIF_F_HIGHDMA;
3526
3527         if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3528                 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3529                 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
3530
3531         tp->intr_mask = 0xffff;
3532         tp->hw_start = cfg->hw_start;
3533         tp->intr_event = cfg->intr_event;
3534         tp->napi_event = cfg->napi_event;
3535
3536         init_timer(&tp->timer);
3537         tp->timer.data = (unsigned long) dev;
3538         tp->timer.function = rtl8169_phy_timer;
3539
3540         tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
3541
3542         rc = register_netdev(dev);
3543         if (rc < 0)
3544                 goto err_out_msi_4;
3545
3546         pci_set_drvdata(pdev, dev);
3547
3548         netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3549                    rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
3550                    (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
3551
3552         if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3553             tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3554             tp->mac_version == RTL_GIGA_MAC_VER_31) {
3555                 rtl8168_driver_start(tp);
3556         }
3557
3558         device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
3559
3560         if (pci_dev_run_wake(pdev))
3561                 pm_runtime_put_noidle(&pdev->dev);
3562
3563         netif_carrier_off(dev);
3564
3565 out:
3566         return rc;
3567
3568 err_out_msi_4:
3569         rtl_disable_msi(pdev, tp);
3570         iounmap(ioaddr);
3571 err_out_free_res_3:
3572         pci_release_regions(pdev);
3573 err_out_mwi_2:
3574         pci_clear_mwi(pdev);
3575         pci_disable_device(pdev);
3576 err_out_free_dev_1:
3577         free_netdev(dev);
3578         goto out;
3579 }
3580
3581 static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
3582 {
3583         struct net_device *dev = pci_get_drvdata(pdev);
3584         struct rtl8169_private *tp = netdev_priv(dev);
3585
3586         if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3587             tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3588             tp->mac_version == RTL_GIGA_MAC_VER_31) {
3589                 rtl8168_driver_stop(tp);
3590         }
3591
3592         cancel_delayed_work_sync(&tp->task);
3593
3594         unregister_netdev(dev);
3595
3596         rtl_release_firmware(tp);
3597
3598         if (pci_dev_run_wake(pdev))
3599                 pm_runtime_get_noresume(&pdev->dev);
3600
3601         /* restore original MAC address */
3602         rtl_rar_set(tp, dev->perm_addr);
3603
3604         rtl_disable_msi(pdev, tp);
3605         rtl8169_release_board(pdev, dev, tp->mmio_addr);
3606         pci_set_drvdata(pdev, NULL);
3607 }
3608
3609 static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3610 {
3611         struct rtl_fw *rtl_fw;
3612         const char *name;
3613         int rc = -ENOMEM;
3614
3615         name = rtl_lookup_firmware_name(tp);
3616         if (!name)
3617                 goto out_no_firmware;
3618
3619         rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3620         if (!rtl_fw)
3621                 goto err_warn;
3622
3623         rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3624         if (rc < 0)
3625                 goto err_free;
3626
3627         rc = rtl_check_firmware(tp, rtl_fw);
3628         if (rc < 0)
3629                 goto err_release_firmware;
3630
3631         tp->rtl_fw = rtl_fw;
3632 out:
3633         return;
3634
3635 err_release_firmware:
3636         release_firmware(rtl_fw->fw);
3637 err_free:
3638         kfree(rtl_fw);
3639 err_warn:
3640         netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3641                    name, rc);
3642 out_no_firmware:
3643         tp->rtl_fw = NULL;
3644         goto out;
3645 }
3646
3647 static void rtl_request_firmware(struct rtl8169_private *tp)
3648 {
3649         if (IS_ERR(tp->rtl_fw))
3650                 rtl_request_uncached_firmware(tp);
3651 }
3652
3653 static int rtl8169_open(struct net_device *dev)
3654 {
3655         struct rtl8169_private *tp = netdev_priv(dev);
3656         void __iomem *ioaddr = tp->mmio_addr;
3657         struct pci_dev *pdev = tp->pci_dev;
3658         int retval = -ENOMEM;
3659
3660         pm_runtime_get_sync(&pdev->dev);
3661
3662         /*
3663          * Rx and Tx desscriptors needs 256 bytes alignment.
3664          * dma_alloc_coherent provides more.
3665          */
3666         tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3667                                              &tp->TxPhyAddr, GFP_KERNEL);
3668         if (!tp->TxDescArray)
3669                 goto err_pm_runtime_put;
3670
3671         tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3672                                              &tp->RxPhyAddr, GFP_KERNEL);
3673         if (!tp->RxDescArray)
3674                 goto err_free_tx_0;
3675
3676         retval = rtl8169_init_ring(dev);
3677         if (retval < 0)
3678                 goto err_free_rx_1;
3679
3680         INIT_DELAYED_WORK(&tp->task, NULL);
3681
3682         smp_mb();
3683
3684         rtl_request_firmware(tp);
3685
3686         retval = request_irq(dev->irq, rtl8169_interrupt,
3687                              (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
3688                              dev->name, dev);
3689         if (retval < 0)
3690                 goto err_release_fw_2;
3691
3692         napi_enable(&tp->napi);
3693
3694         rtl8169_init_phy(dev, tp);
3695
3696         rtl8169_set_features(dev, dev->features);
3697
3698         rtl_pll_power_up(tp);
3699
3700         rtl_hw_start(dev);
3701
3702         tp->saved_wolopts = 0;
3703         pm_runtime_put_noidle(&pdev->dev);
3704
3705         rtl8169_check_link_status(dev, tp, ioaddr);
3706 out:
3707         return retval;
3708
3709 err_release_fw_2:
3710         rtl_release_firmware(tp);
3711         rtl8169_rx_clear(tp);
3712 err_free_rx_1:
3713         dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3714                           tp->RxPhyAddr);
3715         tp->RxDescArray = NULL;
3716 err_free_tx_0:
3717         dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3718                           tp->TxPhyAddr);
3719         tp->TxDescArray = NULL;
3720 err_pm_runtime_put:
3721         pm_runtime_put_noidle(&pdev->dev);
3722         goto out;
3723 }
3724
3725 static void rtl8169_hw_reset(struct rtl8169_private *tp)
3726 {
3727         void __iomem *ioaddr = tp->mmio_addr;
3728
3729         /* Disable interrupts */
3730         rtl8169_irq_mask_and_ack(ioaddr);
3731
3732         if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3733             tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3734             tp->mac_version == RTL_GIGA_MAC_VER_31) {
3735                 while (RTL_R8(TxPoll) & NPQ)
3736                         udelay(20);
3737
3738         }
3739
3740         /* Reset the chipset */
3741         RTL_W8(ChipCmd, CmdReset);
3742
3743         /* PCI commit */
3744         RTL_R8(ChipCmd);
3745 }
3746
3747 static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
3748 {
3749         void __iomem *ioaddr = tp->mmio_addr;
3750         u32 cfg = rtl8169_rx_config;
3751
3752         cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
3753         RTL_W32(RxConfig, cfg);
3754
3755         /* Set DMA burst size and Interframe Gap Time */
3756         RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3757                 (InterFrameGap << TxInterFrameGapShift));
3758 }
3759
3760 static void rtl_hw_start(struct net_device *dev)
3761 {
3762         struct rtl8169_private *tp = netdev_priv(dev);
3763
3764         rtl_hw_reset(tp);
3765
3766         tp->hw_start(dev);
3767
3768         netif_start_queue(dev);
3769 }
3770
3771 static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3772                                          void __iomem *ioaddr)
3773 {
3774         /*
3775          * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3776          * register to be written before TxDescAddrLow to work.
3777          * Switching from MMIO to I/O access fixes the issue as well.
3778          */
3779         RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
3780         RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
3781         RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
3782         RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
3783 }
3784
3785 static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3786 {
3787         u16 cmd;
3788
3789         cmd = RTL_R16(CPlusCmd);
3790         RTL_W16(CPlusCmd, cmd);
3791         return cmd;
3792 }
3793
3794 static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
3795 {
3796         /* Low hurts. Let's disable the filtering. */
3797         RTL_W16(RxMaxSize, rx_buf_sz + 1);
3798 }
3799
3800 static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3801 {
3802         static const struct {
3803                 u32 mac_version;
3804                 u32 clk;
3805                 u32 val;
3806         } cfg2_info [] = {
3807                 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3808                 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3809                 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3810                 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3811         }, *p = cfg2_info;
3812         unsigned int i;
3813         u32 clk;
3814
3815         clk = RTL_R8(Config2) & PCI_Clock_66MHz;
3816         for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
3817                 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3818                         RTL_W32(0x7c, p->val);
3819                         break;
3820                 }
3821         }
3822 }
3823
3824 static void rtl_hw_start_8169(struct net_device *dev)
3825 {
3826         struct rtl8169_private *tp = netdev_priv(dev);
3827         void __iomem *ioaddr = tp->mmio_addr;
3828         struct pci_dev *pdev = tp->pci_dev;
3829
3830         if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3831                 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3832                 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3833         }
3834
3835         RTL_W8(Cfg9346, Cfg9346_Unlock);
3836         if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3837             tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3838             tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3839             tp->mac_version == RTL_GIGA_MAC_VER_04)
3840                 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3841
3842         RTL_W8(EarlyTxThres, NoEarlyTx);
3843
3844         rtl_set_rx_max_size(ioaddr, rx_buf_sz);
3845
3846         if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3847             tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3848             tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3849             tp->mac_version == RTL_GIGA_MAC_VER_04)
3850                 rtl_set_rx_tx_config_registers(tp);
3851
3852         tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
3853
3854         if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3855             tp->mac_version == RTL_GIGA_MAC_VER_03) {
3856                 dprintk("Set MAC Reg C+CR Offset 0xE0. "
3857                         "Bit-3 and bit-14 MUST be 1\n");
3858                 tp->cp_cmd |= (1 << 14);
3859         }
3860
3861         RTL_W16(CPlusCmd, tp->cp_cmd);
3862
3863         rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3864
3865         /*
3866          * Undocumented corner. Supposedly:
3867          * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3868          */
3869         RTL_W16(IntrMitigate, 0x0000);
3870
3871         rtl_set_rx_tx_desc_registers(tp, ioaddr);
3872
3873         if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3874             tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3875             tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3876             tp->mac_version != RTL_GIGA_MAC_VER_04) {
3877                 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3878                 rtl_set_rx_tx_config_registers(tp);
3879         }
3880
3881         RTL_W8(Cfg9346, Cfg9346_Lock);
3882
3883         /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3884         RTL_R8(IntrMask);
3885
3886         RTL_W32(RxMissed, 0);
3887
3888         rtl_set_rx_mode(dev);
3889
3890         /* no early-rx interrupts */
3891         RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
3892
3893         /* Enable all known interrupts by setting the interrupt mask. */
3894         RTL_W16(IntrMask, tp->intr_event);
3895 }
3896
3897 static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
3898 {
3899         struct net_device *dev = pci_get_drvdata(pdev);
3900         struct rtl8169_private *tp = netdev_priv(dev);
3901         int cap = tp->pcie_cap;
3902
3903         if (cap) {
3904                 u16 ctl;
3905
3906                 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3907                 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3908                 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3909         }
3910 }
3911
3912 static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
3913 {
3914         u32 csi;
3915
3916         csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
3917         rtl_csi_write(ioaddr, 0x070c, csi | bits);
3918 }
3919
3920 static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3921 {
3922         rtl_csi_access_enable(ioaddr, 0x17000000);
3923 }
3924
3925 static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3926 {
3927         rtl_csi_access_enable(ioaddr, 0x27000000);
3928 }
3929
3930 struct ephy_info {
3931         unsigned int offset;
3932         u16 mask;
3933         u16 bits;
3934 };
3935
3936 static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
3937 {
3938         u16 w;
3939
3940         while (len-- > 0) {
3941                 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3942                 rtl_ephy_write(ioaddr, e->offset, w);
3943                 e++;
3944         }
3945 }
3946
3947 static void rtl_disable_clock_request(struct pci_dev *pdev)
3948 {
3949         struct net_device *dev = pci_get_drvdata(pdev);
3950         struct rtl8169_private *tp = netdev_priv(dev);
3951         int cap = tp->pcie_cap;
3952
3953         if (cap) {
3954                 u16 ctl;
3955
3956                 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3957                 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3958                 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3959         }
3960 }
3961
3962 static void rtl_enable_clock_request(struct pci_dev *pdev)
3963 {
3964         struct net_device *dev = pci_get_drvdata(pdev);
3965         struct rtl8169_private *tp = netdev_priv(dev);
3966         int cap = tp->pcie_cap;
3967
3968         if (cap) {
3969                 u16 ctl;
3970
3971                 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3972                 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3973                 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3974         }
3975 }
3976
3977 #define R8168_CPCMD_QUIRK_MASK (\
3978         EnableBist | \
3979         Mac_dbgo_oe | \
3980         Force_half_dup | \
3981         Force_rxflow_en | \
3982         Force_txflow_en | \
3983         Cxpl_dbg_sel | \
3984         ASF | \
3985         PktCntrDisable | \
3986         Mac_dbgo_sel)
3987
3988 static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3989 {
3990         RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3991
3992         RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3993
3994         rtl_tx_performance_tweak(pdev,
3995                 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
3996 }
3997
3998 static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3999 {
4000         rtl_hw_start_8168bb(ioaddr, pdev);
4001
4002         RTL_W8(MaxTxPacketSize, TxPacketMax);
4003
4004         RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
4005 }
4006
4007 static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4008 {
4009         RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4010
4011         RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4012
4013         rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4014
4015         rtl_disable_clock_request(pdev);
4016
4017         RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4018 }
4019
4020 static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
4021 {
4022         static const struct ephy_info e_info_8168cp[] = {
4023                 { 0x01, 0,      0x0001 },
4024                 { 0x02, 0x0800, 0x1000 },
4025                 { 0x03, 0,      0x0042 },
4026                 { 0x06, 0x0080, 0x0000 },
4027                 { 0x07, 0,      0x2000 }
4028         };
4029
4030         rtl_csi_access_enable_2(ioaddr);
4031
4032         rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4033
4034         __rtl_hw_start_8168cp(ioaddr, pdev);
4035 }
4036
4037 static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4038 {
4039         rtl_csi_access_enable_2(ioaddr);
4040
4041         RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4042
4043         rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4044
4045         RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4046 }
4047
4048 static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4049 {
4050         rtl_csi_access_enable_2(ioaddr);
4051
4052         RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4053
4054         /* Magic. */
4055         RTL_W8(DBG_REG, 0x20);
4056
4057         RTL_W8(MaxTxPacketSize, TxPacketMax);
4058
4059         rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4060
4061         RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4062 }
4063
4064 static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4065 {
4066         static const struct ephy_info e_info_8168c_1[] = {
4067                 { 0x02, 0x0800, 0x1000 },
4068                 { 0x03, 0,      0x0002 },
4069                 { 0x06, 0x0080, 0x0000 }
4070         };
4071
4072         rtl_csi_access_enable_2(ioaddr);
4073
4074         RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4075
4076         rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4077
4078         __rtl_hw_start_8168cp(ioaddr, pdev);
4079 }
4080
4081 static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4082 {
4083         static const struct ephy_info e_info_8168c_2[] = {
4084                 { 0x01, 0,      0x0001 },
4085                 { 0x03, 0x0400, 0x0220 }
4086         };
4087
4088         rtl_csi_access_enable_2(ioaddr);
4089
4090         rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4091
4092         __rtl_hw_start_8168cp(ioaddr, pdev);
4093 }
4094
4095 static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4096 {
4097         rtl_hw_start_8168c_2(ioaddr, pdev);
4098 }
4099
4100 static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4101 {
4102         rtl_csi_access_enable_2(ioaddr);
4103
4104         __rtl_hw_start_8168cp(ioaddr, pdev);
4105 }
4106
4107 static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4108 {
4109         rtl_csi_access_enable_2(ioaddr);
4110
4111         rtl_disable_clock_request(pdev);
4112
4113         RTL_W8(MaxTxPacketSize, TxPacketMax);
4114
4115         rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4116
4117         RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4118 }
4119
4120 static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4121 {
4122         rtl_csi_access_enable_1(ioaddr);
4123
4124         rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4125
4126         RTL_W8(MaxTxPacketSize, TxPacketMax);
4127
4128         rtl_disable_clock_request(pdev);
4129 }
4130
4131 static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4132 {
4133         static const struct ephy_info e_info_8168d_4[] = {
4134                 { 0x0b, ~0,     0x48 },
4135                 { 0x19, 0x20,   0x50 },
4136                 { 0x0c, ~0,     0x20 }
4137         };
4138         int i;
4139
4140         rtl_csi_access_enable_1(ioaddr);
4141
4142         rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4143
4144         RTL_W8(MaxTxPacketSize, TxPacketMax);
4145
4146         for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4147                 const struct ephy_info *e = e_info_8168d_4 + i;
4148                 u16 w;
4149
4150                 w = rtl_ephy_read(ioaddr, e->offset);
4151                 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4152         }
4153
4154         rtl_enable_clock_request(pdev);
4155 }
4156
4157 static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4158 {
4159         static const struct ephy_info e_info_8168e[] = {
4160                 { 0x00, 0x0200, 0x0100 },
4161                 { 0x00, 0x0000, 0x0004 },
4162                 { 0x06, 0x0002, 0x0001 },
4163                 { 0x06, 0x0000, 0x0030 },
4164                 { 0x07, 0x0000, 0x2000 },
4165                 { 0x00, 0x0000, 0x0020 },
4166                 { 0x03, 0x5800, 0x2000 },
4167                 { 0x03, 0x0000, 0x0001 },
4168                 { 0x01, 0x0800, 0x1000 },
4169                 { 0x07, 0x0000, 0x4000 },
4170                 { 0x1e, 0x0000, 0x2000 },
4171                 { 0x19, 0xffff, 0xfe6c },
4172                 { 0x0a, 0x0000, 0x0040 }
4173         };
4174
4175         rtl_csi_access_enable_2(ioaddr);
4176
4177         rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4178
4179         rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4180
4181         RTL_W8(MaxTxPacketSize, TxPacketMax);
4182
4183         rtl_disable_clock_request(pdev);
4184
4185         /* Reset tx FIFO pointer */
4186         RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4187         RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
4188
4189         RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4190 }
4191
4192 static void rtl_hw_start_8168(struct net_device *dev)
4193 {
4194         struct rtl8169_private *tp = netdev_priv(dev);
4195         void __iomem *ioaddr = tp->mmio_addr;
4196         struct pci_dev *pdev = tp->pci_dev;
4197
4198         RTL_W8(Cfg9346, Cfg9346_Unlock);
4199
4200         RTL_W8(MaxTxPacketSize, TxPacketMax);
4201
4202         rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4203
4204         tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
4205
4206         RTL_W16(CPlusCmd, tp->cp_cmd);
4207
4208         RTL_W16(IntrMitigate, 0x5151);
4209
4210         /* Work around for RxFIFO overflow. */
4211         if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4212             tp->mac_version == RTL_GIGA_MAC_VER_22) {
4213                 tp->intr_event |= RxFIFOOver | PCSTimeout;
4214                 tp->intr_event &= ~RxOverflow;
4215         }
4216
4217         rtl_set_rx_tx_desc_registers(tp, ioaddr);
4218
4219         rtl_set_rx_mode(dev);
4220
4221         RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4222                 (InterFrameGap << TxInterFrameGapShift));
4223
4224         RTL_R8(IntrMask);
4225
4226         switch (tp->mac_version) {
4227         case RTL_GIGA_MAC_VER_11:
4228                 rtl_hw_start_8168bb(ioaddr, pdev);
4229                 break;
4230
4231         case RTL_GIGA_MAC_VER_12:
4232         case RTL_GIGA_MAC_VER_17:
4233                 rtl_hw_start_8168bef(ioaddr, pdev);
4234                 break;
4235
4236         case RTL_GIGA_MAC_VER_18:
4237                 rtl_hw_start_8168cp_1(ioaddr, pdev);
4238                 break;
4239
4240         case RTL_GIGA_MAC_VER_19:
4241                 rtl_hw_start_8168c_1(ioaddr, pdev);
4242                 break;
4243
4244         case RTL_GIGA_MAC_VER_20:
4245                 rtl_hw_start_8168c_2(ioaddr, pdev);
4246                 break;
4247
4248         case RTL_GIGA_MAC_VER_21:
4249                 rtl_hw_start_8168c_3(ioaddr, pdev);
4250                 break;
4251
4252         case RTL_GIGA_MAC_VER_22:
4253                 rtl_hw_start_8168c_4(ioaddr, pdev);
4254                 break;
4255
4256         case RTL_GIGA_MAC_VER_23:
4257                 rtl_hw_start_8168cp_2(ioaddr, pdev);
4258                 break;
4259
4260         case RTL_GIGA_MAC_VER_24:
4261                 rtl_hw_start_8168cp_3(ioaddr, pdev);
4262                 break;
4263
4264         case RTL_GIGA_MAC_VER_25:
4265         case RTL_GIGA_MAC_VER_26:
4266         case RTL_GIGA_MAC_VER_27:
4267                 rtl_hw_start_8168d(ioaddr, pdev);
4268                 break;
4269
4270         case RTL_GIGA_MAC_VER_28:
4271                 rtl_hw_start_8168d_4(ioaddr, pdev);
4272                 break;
4273
4274         case RTL_GIGA_MAC_VER_31:
4275                 rtl_hw_start_8168dp(ioaddr, pdev);
4276                 break;
4277
4278         case RTL_GIGA_MAC_VER_32:
4279         case RTL_GIGA_MAC_VER_33:
4280                 rtl_hw_start_8168e(ioaddr, pdev);
4281                 break;
4282
4283         default:
4284                 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4285                         dev->name, tp->mac_version);
4286                 break;
4287         }
4288
4289         RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4290
4291         RTL_W8(Cfg9346, Cfg9346_Lock);
4292
4293         RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
4294
4295         RTL_W16(IntrMask, tp->intr_event);
4296 }
4297
4298 #define R810X_CPCMD_QUIRK_MASK (\
4299         EnableBist | \
4300         Mac_dbgo_oe | \
4301         Force_half_dup | \
4302         Force_rxflow_en | \
4303         Force_txflow_en | \
4304         Cxpl_dbg_sel | \
4305         ASF | \
4306         PktCntrDisable | \
4307         Mac_dbgo_sel)
4308
4309 static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4310 {
4311         static const struct ephy_info e_info_8102e_1[] = {
4312                 { 0x01, 0, 0x6e65 },
4313                 { 0x02, 0, 0x091f },
4314                 { 0x03, 0, 0xc2f9 },
4315                 { 0x06, 0, 0xafb5 },
4316                 { 0x07, 0, 0x0e00 },
4317                 { 0x19, 0, 0xec80 },
4318                 { 0x01, 0, 0x2e65 },
4319                 { 0x01, 0, 0x6e65 }
4320         };
4321         u8 cfg1;
4322
4323         rtl_csi_access_enable_2(ioaddr);
4324
4325         RTL_W8(DBG_REG, FIX_NAK_1);
4326
4327         rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4328
4329         RTL_W8(Config1,
4330                LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4331         RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4332
4333         cfg1 = RTL_R8(Config1);
4334         if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4335                 RTL_W8(Config1, cfg1 & ~LEDS0);
4336
4337         rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4338 }
4339
4340 static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4341 {
4342         rtl_csi_access_enable_2(ioaddr);
4343
4344         rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4345
4346         RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4347         RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4348 }
4349
4350 static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4351 {
4352         rtl_hw_start_8102e_2(ioaddr, pdev);
4353
4354         rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4355 }
4356
4357 static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4358 {
4359         static const struct ephy_info e_info_8105e_1[] = {
4360                 { 0x07, 0, 0x4000 },
4361                 { 0x19, 0, 0x0200 },
4362                 { 0x19, 0, 0x0020 },
4363                 { 0x1e, 0, 0x2000 },
4364                 { 0x03, 0, 0x0001 },
4365                 { 0x19, 0, 0x0100 },
4366                 { 0x19, 0, 0x0004 },
4367                 { 0x0a, 0, 0x0020 }
4368         };
4369
4370         /* Force LAN exit from ASPM if Rx/Tx are not idle */
4371         RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4372
4373         /* Disable Early Tally Counter */
4374         RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4375
4376         RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4377         RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4378
4379         rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4380 }
4381
4382 static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4383 {
4384         rtl_hw_start_8105e_1(ioaddr, pdev);
4385         rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4386 }
4387
4388 static void rtl_hw_start_8101(struct net_device *dev)
4389 {
4390         struct rtl8169_private *tp = netdev_priv(dev);
4391         void __iomem *ioaddr = tp->mmio_addr;
4392         struct pci_dev *pdev = tp->pci_dev;
4393
4394         if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4395             tp->mac_version == RTL_GIGA_MAC_VER_16) {
4396                 int cap = tp->pcie_cap;
4397
4398                 if (cap) {
4399                         pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4400                                               PCI_EXP_DEVCTL_NOSNOOP_EN);
4401                 }
4402         }
4403
4404         RTL_W8(Cfg9346, Cfg9346_Unlock);
4405
4406         switch (tp->mac_version) {
4407         case RTL_GIGA_MAC_VER_07:
4408                 rtl_hw_start_8102e_1(ioaddr, pdev);
4409                 break;
4410
4411         case RTL_GIGA_MAC_VER_08:
4412                 rtl_hw_start_8102e_3(ioaddr, pdev);
4413                 break;
4414
4415         case RTL_GIGA_MAC_VER_09:
4416                 rtl_hw_start_8102e_2(ioaddr, pdev);
4417                 break;
4418
4419         case RTL_GIGA_MAC_VER_29:
4420                 rtl_hw_start_8105e_1(ioaddr, pdev);
4421                 break;
4422         case RTL_GIGA_MAC_VER_30:
4423                 rtl_hw_start_8105e_2(ioaddr, pdev);
4424                 break;
4425         }
4426
4427         RTL_W8(Cfg9346, Cfg9346_Lock);
4428
4429         RTL_W8(MaxTxPacketSize, TxPacketMax);
4430
4431         rtl_set_rx_max_size(ioaddr, rx_buf_sz);
4432
4433         tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
4434         RTL_W16(CPlusCmd, tp->cp_cmd);
4435
4436         RTL_W16(IntrMitigate, 0x0000);
4437
4438         rtl_set_rx_tx_desc_registers(tp, ioaddr);
4439
4440         RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4441         rtl_set_rx_tx_config_registers(tp);
4442
4443         RTL_R8(IntrMask);
4444
4445         rtl_set_rx_mode(dev);
4446
4447         RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
4448
4449         RTL_W16(IntrMask, tp->intr_event);
4450 }
4451
4452 static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4453 {
4454         if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4455                 return -EINVAL;
4456
4457         dev->mtu = new_mtu;
4458         netdev_update_features(dev);
4459
4460         return 0;
4461 }
4462
4463 static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4464 {
4465         desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
4466         desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4467 }
4468
4469 static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4470                                      void **data_buff, struct RxDesc *desc)
4471 {
4472         dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
4473                          DMA_FROM_DEVICE);
4474
4475         kfree(*data_buff);
4476         *data_buff = NULL;
4477         rtl8169_make_unusable_by_asic(desc);
4478 }
4479
4480 static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4481 {
4482         u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4483
4484         desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4485 }
4486
4487 static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4488                                        u32 rx_buf_sz)
4489 {
4490         desc->addr = cpu_to_le64(mapping);
4491         wmb();
4492         rtl8169_mark_to_asic(desc, rx_buf_sz);
4493 }
4494
4495 static inline void *rtl8169_align(void *data)
4496 {
4497         return (void *)ALIGN((long)data, 16);
4498 }
4499
4500 static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4501                                              struct RxDesc *desc)
4502 {
4503         void *data;
4504         dma_addr_t mapping;
4505         struct device *d = &tp->pci_dev->dev;
4506         struct net_device *dev = tp->dev;
4507         int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
4508
4509         data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4510         if (!data)
4511                 return NULL;
4512
4513         if (rtl8169_align(data) != data) {
4514                 kfree(data);
4515                 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4516                 if (!data)
4517                         return NULL;
4518         }
4519
4520         mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
4521                                  DMA_FROM_DEVICE);
4522         if (unlikely(dma_mapping_error(d, mapping))) {
4523                 if (net_ratelimit())
4524                         netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
4525                 goto err_out;
4526         }
4527
4528         rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
4529         return data;
4530
4531 err_out:
4532         kfree(data);
4533         return NULL;
4534 }
4535
4536 static void rtl8169_rx_clear(struct rtl8169_private *tp)
4537 {
4538         unsigned int i;
4539
4540         for (i = 0; i < NUM_RX_DESC; i++) {
4541                 if (tp->Rx_databuff[i]) {
4542                         rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
4543                                             tp->RxDescArray + i);
4544                 }
4545         }
4546 }
4547
4548 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
4549 {
4550         desc->opts1 |= cpu_to_le32(RingEnd);
4551 }
4552
4553 static int rtl8169_rx_fill(struct rtl8169_private *tp)
4554 {
4555         unsigned int i;
4556
4557         for (i = 0; i < NUM_RX_DESC; i++) {
4558                 void *data;
4559
4560                 if (tp->Rx_databuff[i])
4561                         continue;
4562
4563                 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
4564                 if (!data) {
4565                         rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
4566                         goto err_out;
4567                 }
4568                 tp->Rx_databuff[i] = data;
4569         }
4570
4571         rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4572         return 0;
4573
4574 err_out:
4575         rtl8169_rx_clear(tp);
4576         return -ENOMEM;
4577 }
4578
4579 static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4580 {
4581         tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4582 }
4583
4584 static int rtl8169_init_ring(struct net_device *dev)
4585 {
4586         struct rtl8169_private *tp = netdev_priv(dev);
4587
4588         rtl8169_init_ring_indexes(tp);
4589
4590         memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
4591         memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
4592
4593         return rtl8169_rx_fill(tp);
4594 }
4595
4596 static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
4597                                  struct TxDesc *desc)
4598 {
4599         unsigned int len = tx_skb->len;
4600
4601         dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4602
4603         desc->opts1 = 0x00;
4604         desc->opts2 = 0x00;
4605         desc->addr = 0x00;
4606         tx_skb->len = 0;
4607 }
4608
4609 static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4610                                    unsigned int n)
4611 {
4612         unsigned int i;
4613
4614         for (i = 0; i < n; i++) {
4615                 unsigned int entry = (start + i) % NUM_TX_DESC;
4616                 struct ring_info *tx_skb = tp->tx_skb + entry;
4617                 unsigned int len = tx_skb->len;
4618
4619                 if (len) {
4620                         struct sk_buff *skb = tx_skb->skb;
4621
4622                         rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4623                                              tp->TxDescArray + entry);
4624                         if (skb) {
4625                                 tp->dev->stats.tx_dropped++;
4626                                 dev_kfree_skb(skb);
4627                                 tx_skb->skb = NULL;
4628                         }
4629                 }
4630         }
4631 }
4632
4633 static void rtl8169_tx_clear(struct rtl8169_private *tp)
4634 {
4635         rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
4636         tp->cur_tx = tp->dirty_tx = 0;
4637 }
4638
4639 static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
4640 {
4641         struct rtl8169_private *tp = netdev_priv(dev);
4642
4643         PREPARE_DELAYED_WORK(&tp->task, task);
4644         schedule_delayed_work(&tp->task, 4);
4645 }
4646
4647 static void rtl8169_wait_for_quiescence(struct net_device *dev)
4648 {
4649         struct rtl8169_private *tp = netdev_priv(dev);
4650         void __iomem *ioaddr = tp->mmio_addr;
4651
4652         synchronize_irq(dev->irq);
4653
4654         /* Wait for any pending NAPI task to complete */
4655         napi_disable(&tp->napi);
4656
4657         rtl8169_irq_mask_and_ack(ioaddr);
4658
4659         tp->intr_mask = 0xffff;
4660         RTL_W16(IntrMask, tp->intr_event);
4661         napi_enable(&tp->napi);
4662 }
4663
4664 static void rtl8169_reinit_task(struct work_struct *work)
4665 {
4666         struct rtl8169_private *tp =
4667                 container_of(work, struct rtl8169_private, task.work);
4668         struct net_device *dev = tp->dev;
4669         int ret;
4670
4671         rtnl_lock();
4672
4673         if (!netif_running(dev))
4674                 goto out_unlock;
4675
4676         rtl8169_wait_for_quiescence(dev);
4677         rtl8169_close(dev);
4678
4679         ret = rtl8169_open(dev);
4680         if (unlikely(ret < 0)) {
4681                 if (net_ratelimit())
4682                         netif_err(tp, drv, dev,
4683                                   "reinit failure (status = %d). Rescheduling\n",
4684                                   ret);
4685                 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4686         }
4687
4688 out_unlock:
4689         rtnl_unlock();
4690 }
4691
4692 static void rtl8169_reset_task(struct work_struct *work)
4693 {
4694         struct rtl8169_private *tp =
4695                 container_of(work, struct rtl8169_private, task.work);
4696         struct net_device *dev = tp->dev;
4697         int i;
4698
4699         rtnl_lock();
4700
4701         if (!netif_running(dev))
4702                 goto out_unlock;
4703
4704         rtl8169_wait_for_quiescence(dev);
4705
4706         for (i = 0; i < NUM_RX_DESC; i++)
4707                 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4708
4709         rtl8169_tx_clear(tp);
4710
4711         rtl8169_init_ring_indexes(tp);
4712         rtl_hw_start(dev);
4713         netif_wake_queue(dev);
4714         rtl8169_check_link_status(dev, tp, tp->mmio_addr);
4715
4716 out_unlock:
4717         rtnl_unlock();
4718 }
4719
4720 static void rtl8169_tx_timeout(struct net_device *dev)
4721 {
4722         struct rtl8169_private *tp = netdev_priv(dev);
4723
4724         rtl8169_hw_reset(tp);
4725
4726         /* Let's wait a bit while any (async) irq lands on */
4727         rtl8169_schedule_work(dev, rtl8169_reset_task);
4728 }
4729
4730 static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4731                               u32 *opts)
4732 {
4733         struct skb_shared_info *info = skb_shinfo(skb);
4734         unsigned int cur_frag, entry;
4735         struct TxDesc * uninitialized_var(txd);
4736         struct device *d = &tp->pci_dev->dev;
4737
4738         entry = tp->cur_tx;
4739         for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4740                 skb_frag_t *frag = info->frags + cur_frag;
4741                 dma_addr_t mapping;
4742                 u32 status, len;
4743                 void *addr;
4744
4745                 entry = (entry + 1) % NUM_TX_DESC;
4746
4747                 txd = tp->TxDescArray + entry;
4748                 len = frag->size;
4749                 addr = ((void *) page_address(frag->page)) + frag->page_offset;
4750                 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
4751                 if (unlikely(dma_mapping_error(d, mapping))) {
4752                         if (net_ratelimit())
4753                                 netif_err(tp, drv, tp->dev,
4754                                           "Failed to map TX fragments DMA!\n");
4755                         goto err_out;
4756                 }
4757
4758                 /* Anti gcc 2.95.3 bugware (sic) */
4759                 status = opts[0] | len |
4760                         (RingEnd * !((entry + 1) % NUM_TX_DESC));
4761
4762                 txd->opts1 = cpu_to_le32(status);
4763                 txd->opts2 = cpu_to_le32(opts[1]);
4764                 txd->addr = cpu_to_le64(mapping);
4765
4766                 tp->tx_skb[entry].len = len;
4767         }
4768
4769         if (cur_frag) {
4770                 tp->tx_skb[entry].skb = skb;
4771                 txd->opts1 |= cpu_to_le32(LastFrag);
4772         }
4773
4774         return cur_frag;
4775
4776 err_out:
4777         rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4778         return -EIO;
4779 }
4780
4781 static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4782                                     struct sk_buff *skb, u32 *opts)
4783 {
4784         const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
4785         u32 mss = skb_shinfo(skb)->gso_size;
4786         int offset = info->opts_offset;
4787
4788         if (mss) {
4789                 opts[0] |= TD_LSO;
4790                 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4791         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
4792                 const struct iphdr *ip = ip_hdr(skb);
4793
4794                 if (ip->protocol == IPPROTO_TCP)
4795                         opts[offset] |= info->checksum.tcp;
4796                 else if (ip->protocol == IPPROTO_UDP)
4797                         opts[offset] |= info->checksum.udp;
4798                 else
4799                         WARN_ON_ONCE(1);
4800         }
4801 }
4802
4803 static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4804                                       struct net_device *dev)
4805 {
4806         struct rtl8169_private *tp = netdev_priv(dev);
4807         unsigned int entry = tp->cur_tx % NUM_TX_DESC;
4808         struct TxDesc *txd = tp->TxDescArray + entry;
4809         void __iomem *ioaddr = tp->mmio_addr;
4810         struct device *d = &tp->pci_dev->dev;
4811         dma_addr_t mapping;
4812         u32 status, len;
4813         u32 opts[2];
4814         int frags;
4815
4816         if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
4817                 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
4818                 goto err_stop_0;
4819         }
4820
4821         if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
4822                 goto err_stop_0;
4823
4824         len = skb_headlen(skb);
4825         mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
4826         if (unlikely(dma_mapping_error(d, mapping))) {
4827                 if (net_ratelimit())
4828                         netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
4829                 goto err_dma_0;
4830         }
4831
4832         tp->tx_skb[entry].len = len;
4833         txd->addr = cpu_to_le64(mapping);
4834
4835         opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4836         opts[0] = DescOwn;
4837
4838         rtl8169_tso_csum(tp, skb, opts);
4839
4840         frags = rtl8169_xmit_frags(tp, skb, opts);
4841         if (frags < 0)
4842                 goto err_dma_1;
4843         else if (frags)
4844                 opts[0] |= FirstFrag;
4845         else {
4846                 opts[0] |= FirstFrag | LastFrag;
4847                 tp->tx_skb[entry].skb = skb;
4848         }
4849
4850         txd->opts2 = cpu_to_le32(opts[1]);
4851
4852         wmb();
4853
4854         /* Anti gcc 2.95.3 bugware (sic) */
4855         status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4856         txd->opts1 = cpu_to_le32(status);
4857
4858         tp->cur_tx += frags + 1;
4859
4860         wmb();
4861
4862         RTL_W8(TxPoll, NPQ);
4863
4864         if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4865                 netif_stop_queue(dev);
4866                 smp_rmb();
4867                 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4868                         netif_wake_queue(dev);
4869         }
4870
4871         return NETDEV_TX_OK;
4872
4873 err_dma_1:
4874         rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
4875 err_dma_0:
4876         dev_kfree_skb(skb);
4877         dev->stats.tx_dropped++;
4878         return NETDEV_TX_OK;
4879
4880 err_stop_0:
4881         netif_stop_queue(dev);
4882         dev->stats.tx_dropped++;
4883         return NETDEV_TX_BUSY;
4884 }
4885
4886 static void rtl8169_pcierr_interrupt(struct net_device *dev)
4887 {
4888         struct rtl8169_private *tp = netdev_priv(dev);
4889         struct pci_dev *pdev = tp->pci_dev;
4890         u16 pci_status, pci_cmd;
4891
4892         pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4893         pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4894
4895         netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4896                   pci_cmd, pci_status);
4897
4898         /*
4899          * The recovery sequence below admits a very elaborated explanation:
4900          * - it seems to work;
4901          * - I did not see what else could be done;
4902          * - it makes iop3xx happy.
4903          *
4904          * Feel free to adjust to your needs.
4905          */
4906         if (pdev->broken_parity_status)
4907                 pci_cmd &= ~PCI_COMMAND_PARITY;
4908         else
4909                 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4910
4911         pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
4912
4913         pci_write_config_word(pdev, PCI_STATUS,
4914                 pci_status & (PCI_STATUS_DETECTED_PARITY |
4915                 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4916                 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4917
4918         /* The infamous DAC f*ckup only happens at boot time */
4919         if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
4920                 void __iomem *ioaddr = tp->mmio_addr;
4921
4922                 netif_info(tp, intr, dev, "disabling PCI DAC\n");
4923                 tp->cp_cmd &= ~PCIDAC;
4924                 RTL_W16(CPlusCmd, tp->cp_cmd);
4925                 dev->features &= ~NETIF_F_HIGHDMA;
4926         }
4927
4928         rtl8169_hw_reset(tp);
4929
4930         rtl8169_schedule_work(dev, rtl8169_reinit_task);
4931 }
4932
4933 static void rtl8169_tx_interrupt(struct net_device *dev,
4934                                  struct rtl8169_private *tp,
4935                                  void __iomem *ioaddr)
4936 {
4937         unsigned int dirty_tx, tx_left;
4938
4939         dirty_tx = tp->dirty_tx;
4940         smp_rmb();
4941         tx_left = tp->cur_tx - dirty_tx;
4942
4943         while (tx_left > 0) {
4944                 unsigned int entry = dirty_tx % NUM_TX_DESC;
4945                 struct ring_info *tx_skb = tp->tx_skb + entry;
4946                 u32 status;
4947
4948                 rmb();
4949                 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4950                 if (status & DescOwn)
4951                         break;
4952
4953                 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4954                                      tp->TxDescArray + entry);
4955                 if (status & LastFrag) {
4956                         dev->stats.tx_packets++;
4957                         dev->stats.tx_bytes += tx_skb->skb->len;
4958                         dev_kfree_skb(tx_skb->skb);
4959                         tx_skb->skb = NULL;
4960                 }
4961                 dirty_tx++;
4962                 tx_left--;
4963         }
4964
4965         if (tp->dirty_tx != dirty_tx) {
4966                 tp->dirty_tx = dirty_tx;
4967                 smp_wmb();
4968                 if (netif_queue_stopped(dev) &&
4969                     (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4970                         netif_wake_queue(dev);
4971                 }
4972                 /*
4973                  * 8168 hack: TxPoll requests are lost when the Tx packets are
4974                  * too close. Let's kick an extra TxPoll request when a burst
4975                  * of start_xmit activity is detected (if it is not detected,
4976                  * it is slow enough). -- FR
4977                  */
4978                 smp_rmb();
4979                 if (tp->cur_tx != dirty_tx)
4980                         RTL_W8(TxPoll, NPQ);
4981         }
4982 }
4983
4984 static inline int rtl8169_fragmented_frame(u32 status)
4985 {
4986         return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4987 }
4988
4989 static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
4990 {
4991         u32 status = opts1 & RxProtoMask;
4992
4993         if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
4994             ((status == RxProtoUDP) && !(opts1 & UDPFail)))
4995                 skb->ip_summed = CHECKSUM_UNNECESSARY;
4996         else
4997                 skb_checksum_none_assert(skb);
4998 }
4999
5000 static struct sk_buff *rtl8169_try_rx_copy(void *data,
5001                                            struct rtl8169_private *tp,
5002                                            int pkt_size,
5003                                            dma_addr_t addr)
5004 {
5005         struct sk_buff *skb;
5006         struct device *d = &tp->pci_dev->dev;
5007
5008         data = rtl8169_align(data);
5009         dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
5010         prefetch(data);
5011         skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5012         if (skb)
5013                 memcpy(skb->data, data, pkt_size);
5014         dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5015
5016         return skb;
5017 }
5018
5019 static int rtl8169_rx_interrupt(struct net_device *dev,
5020                                 struct rtl8169_private *tp,
5021                                 void __iomem *ioaddr, u32 budget)
5022 {
5023         unsigned int cur_rx, rx_left;
5024         unsigned int count;
5025
5026         cur_rx = tp->cur_rx;
5027         rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
5028         rx_left = min(rx_left, budget);
5029
5030         for (; rx_left > 0; rx_left--, cur_rx++) {
5031                 unsigned int entry = cur_rx % NUM_RX_DESC;
5032                 struct RxDesc *desc = tp->RxDescArray + entry;
5033                 u32 status;
5034
5035                 rmb();
5036                 status = le32_to_cpu(desc->opts1);
5037
5038                 if (status & DescOwn)
5039                         break;
5040                 if (unlikely(status & RxRES)) {
5041                         netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5042                                    status);
5043                         dev->stats.rx_errors++;
5044                         if (status & (RxRWT | RxRUNT))
5045                                 dev->stats.rx_length_errors++;
5046                         if (status & RxCRC)
5047                                 dev->stats.rx_crc_errors++;
5048                         if (status & RxFOVF) {
5049                                 rtl8169_schedule_work(dev, rtl8169_reset_task);
5050                                 dev->stats.rx_fifo_errors++;
5051                         }
5052                         rtl8169_mark_to_asic(desc, rx_buf_sz);
5053                 } else {
5054                         struct sk_buff *skb;
5055                         dma_addr_t addr = le64_to_cpu(desc->addr);
5056                         int pkt_size = (status & 0x00001FFF) - 4;
5057
5058                         /*
5059                          * The driver does not support incoming fragmented
5060                          * frames. They are seen as a symptom of over-mtu
5061                          * sized frames.
5062                          */
5063                         if (unlikely(rtl8169_fragmented_frame(status))) {
5064                                 dev->stats.rx_dropped++;
5065                                 dev->stats.rx_length_errors++;
5066                                 rtl8169_mark_to_asic(desc, rx_buf_sz);
5067                                 continue;
5068                         }
5069
5070                         skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5071                                                   tp, pkt_size, addr);
5072                         rtl8169_mark_to_asic(desc, rx_buf_sz);
5073                         if (!skb) {
5074                                 dev->stats.rx_dropped++;
5075                                 continue;
5076                         }
5077
5078                         rtl8169_rx_csum(skb, status);
5079                         skb_put(skb, pkt_size);
5080                         skb->protocol = eth_type_trans(skb, dev);
5081
5082                         rtl8169_rx_vlan_tag(desc, skb);
5083
5084                         napi_gro_receive(&tp->napi, skb);
5085
5086                         dev->stats.rx_bytes += pkt_size;
5087                         dev->stats.rx_packets++;
5088                 }
5089
5090                 /* Work around for AMD plateform. */
5091                 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
5092                     (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5093                         desc->opts2 = 0;
5094                         cur_rx++;
5095                 }
5096         }
5097
5098         count = cur_rx - tp->cur_rx;
5099         tp->cur_rx = cur_rx;
5100
5101         tp->dirty_rx += count;
5102
5103         return count;
5104 }
5105
5106 static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
5107 {
5108         struct net_device *dev = dev_instance;
5109         struct rtl8169_private *tp = netdev_priv(dev);
5110         void __iomem *ioaddr = tp->mmio_addr;
5111         int handled = 0;
5112         int status;
5113
5114         /* loop handling interrupts until we have no new ones or
5115          * we hit a invalid/hotplug case.
5116          */
5117         status = RTL_R16(IntrStatus);
5118         while (status && status != 0xffff) {
5119                 handled = 1;
5120
5121                 /* Handle all of the error cases first. These will reset
5122                  * the chip, so just exit the loop.
5123                  */
5124                 if (unlikely(!netif_running(dev))) {
5125                         rtl8169_asic_down(ioaddr);
5126                         break;
5127                 }
5128
5129                 if (unlikely(status & RxFIFOOver)) {
5130                         switch (tp->mac_version) {
5131                         /* Work around for rx fifo overflow */
5132                         case RTL_GIGA_MAC_VER_11:
5133                         case RTL_GIGA_MAC_VER_22:
5134                         case RTL_GIGA_MAC_VER_26:
5135                                 netif_stop_queue(dev);
5136                                 rtl8169_tx_timeout(dev);
5137                                 goto done;
5138                         /* Testers needed. */
5139                         case RTL_GIGA_MAC_VER_17:
5140                         case RTL_GIGA_MAC_VER_19:
5141                         case RTL_GIGA_MAC_VER_20:
5142                         case RTL_GIGA_MAC_VER_21:
5143                         case RTL_GIGA_MAC_VER_23:
5144                         case RTL_GIGA_MAC_VER_24:
5145                         case RTL_GIGA_MAC_VER_27:
5146                         case RTL_GIGA_MAC_VER_28:
5147                         case RTL_GIGA_MAC_VER_31:
5148                         /* Experimental science. Pktgen proof. */
5149                         case RTL_GIGA_MAC_VER_12:
5150                         case RTL_GIGA_MAC_VER_25:
5151                                 if (status == RxFIFOOver)
5152                                         goto done;
5153                                 break;
5154                         default:
5155                                 break;
5156                         }
5157                 }
5158
5159                 if (unlikely(status & SYSErr)) {
5160                         rtl8169_pcierr_interrupt(dev);
5161                         break;
5162                 }
5163
5164                 if (status & LinkChg)
5165                         __rtl8169_check_link_status(dev, tp, ioaddr, true);
5166
5167                 /* We need to see the lastest version of tp->intr_mask to
5168                  * avoid ignoring an MSI interrupt and having to wait for
5169                  * another event which may never come.
5170                  */
5171                 smp_rmb();
5172                 if (status & tp->intr_mask & tp->napi_event) {
5173                         RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5174                         tp->intr_mask = ~tp->napi_event;
5175
5176                         if (likely(napi_schedule_prep(&tp->napi)))
5177                                 __napi_schedule(&tp->napi);
5178                         else
5179                                 netif_info(tp, intr, dev,
5180                                            "interrupt %04x in poll\n", status);
5181                 }
5182
5183                 /* We only get a new MSI interrupt when all active irq
5184                  * sources on the chip have been acknowledged. So, ack
5185                  * everything we've seen and check if new sources have become
5186                  * active to avoid blocking all interrupts from the chip.
5187                  */
5188                 RTL_W16(IntrStatus,
5189                         (status & RxFIFOOver) ? (status | RxOverflow) : status);
5190                 status = RTL_R16(IntrStatus);
5191         }
5192 done:
5193         return IRQ_RETVAL(handled);
5194 }
5195
5196 static int rtl8169_poll(struct napi_struct *napi, int budget)
5197 {
5198         struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5199         struct net_device *dev = tp->dev;
5200         void __iomem *ioaddr = tp->mmio_addr;
5201         int work_done;
5202
5203         work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
5204         rtl8169_tx_interrupt(dev, tp, ioaddr);
5205
5206         if (work_done < budget) {
5207                 napi_complete(napi);
5208
5209                 /* We need for force the visibility of tp->intr_mask
5210                  * for other CPUs, as we can loose an MSI interrupt
5211                  * and potentially wait for a retransmit timeout if we don't.
5212                  * The posted write to IntrMask is safe, as it will
5213                  * eventually make it to the chip and we won't loose anything
5214                  * until it does.
5215                  */
5216                 tp->intr_mask = 0xffff;
5217                 wmb();
5218                 RTL_W16(IntrMask, tp->intr_event);
5219         }
5220
5221         return work_done;
5222 }
5223
5224 static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5225 {
5226         struct rtl8169_private *tp = netdev_priv(dev);
5227
5228         if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5229                 return;
5230
5231         dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5232         RTL_W32(RxMissed, 0);
5233 }
5234
5235 static void rtl8169_down(struct net_device *dev)
5236 {
5237         struct rtl8169_private *tp = netdev_priv(dev);
5238         void __iomem *ioaddr = tp->mmio_addr;
5239
5240         del_timer_sync(&tp->timer);
5241
5242         netif_stop_queue(dev);
5243
5244         napi_disable(&tp->napi);
5245
5246         spin_lock_irq(&tp->lock);
5247
5248         rtl8169_asic_down(ioaddr);
5249         /*
5250          * At this point device interrupts can not be enabled in any function,
5251          * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5252          * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5253          */
5254         rtl8169_rx_missed(dev, ioaddr);
5255
5256         spin_unlock_irq(&tp->lock);
5257
5258         synchronize_irq(dev->irq);
5259
5260         /* Give a racing hard_start_xmit a few cycles to complete. */
5261         synchronize_sched();  /* FIXME: should this be synchronize_irq()? */
5262
5263         rtl8169_tx_clear(tp);
5264
5265         rtl8169_rx_clear(tp);
5266
5267         rtl_pll_power_down(tp);
5268 }
5269
5270 static int rtl8169_close(struct net_device *dev)
5271 {
5272         struct rtl8169_private *tp = netdev_priv(dev);
5273         struct pci_dev *pdev = tp->pci_dev;
5274
5275         pm_runtime_get_sync(&pdev->dev);
5276
5277         /* Update counters before going down */
5278         rtl8169_update_counters(dev);
5279
5280         rtl8169_down(dev);
5281
5282         free_irq(dev->irq, dev);
5283
5284         dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5285                           tp->RxPhyAddr);
5286         dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5287                           tp->TxPhyAddr);
5288         tp->TxDescArray = NULL;
5289         tp->RxDescArray = NULL;
5290
5291         pm_runtime_put_sync(&pdev->dev);
5292
5293         return 0;
5294 }
5295
5296 static void rtl_set_rx_mode(struct net_device *dev)
5297 {
5298         struct rtl8169_private *tp = netdev_priv(dev);
5299         void __iomem *ioaddr = tp->mmio_addr;
5300         unsigned long flags;
5301         u32 mc_filter[2];       /* Multicast hash filter */
5302         int rx_mode;
5303         u32 tmp = 0;
5304
5305         if (dev->flags & IFF_PROMISC) {
5306                 /* Unconditionally log net taps. */
5307                 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
5308                 rx_mode =
5309                     AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5310                     AcceptAllPhys;
5311                 mc_filter[1] = mc_filter[0] = 0xffffffff;
5312         } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
5313                    (dev->flags & IFF_ALLMULTI)) {
5314                 /* Too many to filter perfectly -- accept all multicasts. */
5315                 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5316                 mc_filter[1] = mc_filter[0] = 0xffffffff;
5317         } else {
5318                 struct netdev_hw_addr *ha;
5319
5320                 rx_mode = AcceptBroadcast | AcceptMyPhys;
5321                 mc_filter[1] = mc_filter[0] = 0;
5322                 netdev_for_each_mc_addr(ha, dev) {
5323                         int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
5324                         mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5325                         rx_mode |= AcceptMulticast;
5326                 }
5327         }
5328
5329         spin_lock_irqsave(&tp->lock, flags);
5330
5331         tmp = rtl8169_rx_config | rx_mode |
5332               (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
5333
5334         if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
5335                 u32 data = mc_filter[0];
5336
5337                 mc_filter[0] = swab32(mc_filter[1]);
5338                 mc_filter[1] = swab32(data);
5339         }
5340
5341         RTL_W32(MAR0 + 4, mc_filter[1]);
5342         RTL_W32(MAR0 + 0, mc_filter[0]);
5343
5344         RTL_W32(RxConfig, tmp);
5345
5346         spin_unlock_irqrestore(&tp->lock, flags);
5347 }
5348
5349 /**
5350  *  rtl8169_get_stats - Get rtl8169 read/write statistics
5351  *  @dev: The Ethernet Device to get statistics for
5352  *
5353  *  Get TX/RX statistics for rtl8169
5354  */
5355 static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5356 {
5357         struct rtl8169_private *tp = netdev_priv(dev);
5358         void __iomem *ioaddr = tp->mmio_addr;
5359         unsigned long flags;
5360
5361         if (netif_running(dev)) {
5362                 spin_lock_irqsave(&tp->lock, flags);
5363                 rtl8169_rx_missed(dev, ioaddr);
5364                 spin_unlock_irqrestore(&tp->lock, flags);
5365         }
5366
5367         return &dev->stats;
5368 }
5369
5370 static void rtl8169_net_suspend(struct net_device *dev)
5371 {
5372         struct rtl8169_private *tp = netdev_priv(dev);
5373
5374         if (!netif_running(dev))
5375                 return;
5376
5377         rtl_pll_power_down(tp);
5378
5379         netif_device_detach(dev);
5380         netif_stop_queue(dev);
5381 }
5382
5383 #ifdef CONFIG_PM
5384
5385 static int rtl8169_suspend(struct device *device)
5386 {
5387         struct pci_dev *pdev = to_pci_dev(device);
5388         struct net_device *dev = pci_get_drvdata(pdev);
5389
5390         rtl8169_net_suspend(dev);
5391
5392         return 0;
5393 }
5394
5395 static void __rtl8169_resume(struct net_device *dev)
5396 {
5397         struct rtl8169_private *tp = netdev_priv(dev);
5398
5399         netif_device_attach(dev);
5400
5401         rtl_pll_power_up(tp);
5402
5403         rtl8169_schedule_work(dev, rtl8169_reset_task);
5404 }
5405
5406 static int rtl8169_resume(struct device *device)
5407 {
5408         struct pci_dev *pdev = to_pci_dev(device);
5409         struct net_device *dev = pci_get_drvdata(pdev);
5410         struct rtl8169_private *tp = netdev_priv(dev);
5411
5412         rtl8169_init_phy(dev, tp);
5413
5414         if (netif_running(dev))
5415                 __rtl8169_resume(dev);
5416
5417         return 0;
5418 }
5419
5420 static int rtl8169_runtime_suspend(struct device *device)
5421 {
5422         struct pci_dev *pdev = to_pci_dev(device);
5423         struct net_device *dev = pci_get_drvdata(pdev);
5424         struct rtl8169_private *tp = netdev_priv(dev);
5425
5426         if (!tp->TxDescArray)
5427                 return 0;
5428
5429         spin_lock_irq(&tp->lock);
5430         tp->saved_wolopts = __rtl8169_get_wol(tp);
5431         __rtl8169_set_wol(tp, WAKE_ANY);
5432         spin_unlock_irq(&tp->lock);
5433
5434         rtl8169_net_suspend(dev);
5435
5436         return 0;
5437 }
5438
5439 static int rtl8169_runtime_resume(struct device *device)
5440 {
5441         struct pci_dev *pdev = to_pci_dev(device);
5442         struct net_device *dev = pci_get_drvdata(pdev);
5443         struct rtl8169_private *tp = netdev_priv(dev);
5444
5445         if (!tp->TxDescArray)
5446                 return 0;
5447
5448         spin_lock_irq(&tp->lock);
5449         __rtl8169_set_wol(tp, tp->saved_wolopts);
5450         tp->saved_wolopts = 0;
5451         spin_unlock_irq(&tp->lock);
5452
5453         rtl8169_init_phy(dev, tp);
5454
5455         __rtl8169_resume(dev);
5456
5457         return 0;
5458 }
5459
5460 static int rtl8169_runtime_idle(struct device *device)
5461 {
5462         struct pci_dev *pdev = to_pci_dev(device);
5463         struct net_device *dev = pci_get_drvdata(pdev);
5464         struct rtl8169_private *tp = netdev_priv(dev);
5465
5466         return tp->TxDescArray ? -EBUSY : 0;
5467 }
5468
5469 static const struct dev_pm_ops rtl8169_pm_ops = {
5470         .suspend                = rtl8169_suspend,
5471         .resume                 = rtl8169_resume,
5472         .freeze                 = rtl8169_suspend,
5473         .thaw                   = rtl8169_resume,
5474         .poweroff               = rtl8169_suspend,
5475         .restore                = rtl8169_resume,
5476         .runtime_suspend        = rtl8169_runtime_suspend,
5477         .runtime_resume         = rtl8169_runtime_resume,
5478         .runtime_idle           = rtl8169_runtime_idle,
5479 };
5480
5481 #define RTL8169_PM_OPS  (&rtl8169_pm_ops)
5482
5483 #else /* !CONFIG_PM */
5484
5485 #define RTL8169_PM_OPS  NULL
5486
5487 #endif /* !CONFIG_PM */
5488
5489 static void rtl_shutdown(struct pci_dev *pdev)
5490 {
5491         struct net_device *dev = pci_get_drvdata(pdev);
5492         struct rtl8169_private *tp = netdev_priv(dev);
5493         void __iomem *ioaddr = tp->mmio_addr;
5494
5495         rtl8169_net_suspend(dev);
5496
5497         /* Restore original MAC address */
5498         rtl_rar_set(tp, dev->perm_addr);
5499
5500         spin_lock_irq(&tp->lock);
5501
5502         rtl8169_asic_down(ioaddr);
5503
5504         spin_unlock_irq(&tp->lock);
5505
5506         if (system_state == SYSTEM_POWER_OFF) {
5507                 /* WoL fails with some 8168 when the receiver is disabled. */
5508                 if (tp->features & RTL_FEATURE_WOL) {
5509                         pci_clear_master(pdev);
5510
5511                         RTL_W8(ChipCmd, CmdRxEnb);
5512                         /* PCI commit */
5513                         RTL_R8(ChipCmd);
5514                 }
5515
5516                 pci_wake_from_d3(pdev, true);
5517                 pci_set_power_state(pdev, PCI_D3hot);
5518         }
5519 }
5520
5521 static struct pci_driver rtl8169_pci_driver = {
5522         .name           = MODULENAME,
5523         .id_table       = rtl8169_pci_tbl,
5524         .probe          = rtl8169_init_one,
5525         .remove         = __devexit_p(rtl8169_remove_one),
5526         .shutdown       = rtl_shutdown,
5527         .driver.pm      = RTL8169_PM_OPS,
5528 };
5529
5530 static int __init rtl8169_init_module(void)
5531 {
5532         return pci_register_driver(&rtl8169_pci_driver);
5533 }
5534
5535 static void __exit rtl8169_cleanup_module(void)
5536 {
5537         pci_unregister_driver(&rtl8169_pci_driver);
5538 }
5539
5540 module_init(rtl8169_init_module);
5541 module_exit(rtl8169_cleanup_module);