blob: 52e20d5c8e17abefe6c50f278d3b8534b1cef1e9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
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/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Francois Romieu99f252b2007-04-02 22:59:59 +020029#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
31#include <asm/irq.h>
32
Francois Romieu865c6522008-05-11 14:51:00 +020033#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define MODULENAME "r8169"
35#define PFX MODULENAME ": "
36
françois romieubca03d52011-01-03 15:07:31 +000037#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
38#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080039#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef RTL8169_DEBUG
42#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020043 if (!(expr)) { \
44 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070045 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020046 }
Joe Perches06fa7352007-10-18 21:15:00 +020047#define dprintk(fmt, args...) \
48 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#else
50#define assert(expr) do {} while (0)
51#define dprintk(fmt, args...) do {} while (0)
52#endif /* RTL8169_DEBUG */
53
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020054#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070055 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define TX_BUFFS_AVAIL(tp) \
58 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
61 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050062static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/* MAC address length */
65#define MAC_ADDR_LEN 6
66
Francois Romieu9c14cea2008-07-05 00:21:15 +020067#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
69#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
70#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
72#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
73
74#define R8169_REGS_SIZE 256
75#define R8169_NAPI_WEIGHT 64
76#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
77#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
78#define RX_BUF_SIZE 1536 /* Rx Buffer size */
79#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
80#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
81
82#define RTL8169_TX_TIMEOUT (6*HZ)
83#define RTL8169_PHY_TIMEOUT (10*HZ)
84
françois romieuea8dbdd2009-03-15 01:10:50 +000085#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
86#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020087#define RTL_EEPROM_SIG_ADDR 0x0000
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* write/read MMIO register */
90#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
91#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
92#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
93#define RTL_R8(reg) readb (ioaddr + (reg))
94#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000095#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97enum mac_version {
Jean Delvaref21b75e2009-05-26 20:54:48 -070098 RTL_GIGA_MAC_NONE = 0x00,
Francois Romieuba6eb6e2007-06-11 23:35:18 +020099 RTL_GIGA_MAC_VER_01 = 0x01, // 8169
100 RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
101 RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
102 RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
103 RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +0100104 RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
Francois Romieu2857ffb2008-08-02 21:08:49 +0200105 RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
106 RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
107 RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
108 RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
Francois Romieu2dd99532007-06-11 23:22:52 +0200109 RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200110 RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
111 RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
112 RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
113 RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
114 RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
115 RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
116 RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
117 RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
Francois Romieu197ff762008-06-28 13:16:02 +0200118 RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
Francois Romieu6fb07052008-06-29 11:54:28 +0200119 RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
Francois Romieuef3386f2008-06-29 12:24:30 +0200120 RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
Francois Romieu7f3e3d32008-07-20 18:53:20 +0200121 RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
Francois Romieu5b538df2008-07-20 16:22:45 +0200122 RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
françois romieudaf9df62009-10-07 12:44:20 +0000123 RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
124 RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
françois romieue6de30d2011-01-03 15:08:37 +0000125 RTL_GIGA_MAC_VER_27 = 0x1b, // 8168DP
126 RTL_GIGA_MAC_VER_28 = 0x1c, // 8168DP
Hayes Wang5a5e4442011-02-22 17:26:21 +0800127 RTL_GIGA_MAC_VER_29 = 0x1d, // 8105E
128 RTL_GIGA_MAC_VER_30 = 0x1e, // 8105E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define _R(NAME,MAC,MASK) \
132 { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
133
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800134static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 const char *name;
136 u8 mac_version;
137 u32 RxConfigMask; /* Clears the bits supported by this chip */
138} rtl_chip_info[] = {
Francois Romieuba6eb6e2007-06-11 23:35:18 +0200139 _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
140 _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
141 _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
142 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
143 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +0100144 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
Francois Romieu2857ffb2008-08-02 21:08:49 +0200145 _R("RTL8102e", RTL_GIGA_MAC_VER_07, 0xff7e1880), // PCI-E
146 _R("RTL8102e", RTL_GIGA_MAC_VER_08, 0xff7e1880), // PCI-E
147 _R("RTL8102e", RTL_GIGA_MAC_VER_09, 0xff7e1880), // PCI-E
148 _R("RTL8101e", RTL_GIGA_MAC_VER_10, 0xff7e1880), // PCI-E
Francois Romieubcf0bf92006-07-26 23:14:13 +0200149 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
150 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
151 _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
152 _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200153 _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880), // PCI-E 8139
154 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880), // PCI-E
155 _R("RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880), // PCI-E
156 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880), // PCI-E
157 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880), // PCI-E
Francois Romieu197ff762008-06-28 13:16:02 +0200158 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880), // PCI-E
Francois Romieu6fb07052008-06-29 11:54:28 +0200159 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, 0xff7e1880), // PCI-E
Francois Romieuef3386f2008-06-29 12:24:30 +0200160 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, 0xff7e1880), // PCI-E
Francois Romieu7f3e3d32008-07-20 18:53:20 +0200161 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, 0xff7e1880), // PCI-E
Francois Romieu5b538df2008-07-20 16:22:45 +0200162 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, 0xff7e1880), // PCI-E
françois romieudaf9df62009-10-07 12:44:20 +0000163 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, 0xff7e1880), // PCI-E
164 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, 0xff7e1880), // PCI-E
françois romieue6de30d2011-01-03 15:08:37 +0000165 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, 0xff7e1880), // PCI-E
Hayes Wang5a5e4442011-02-22 17:26:21 +0800166 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28, 0xff7e1880), // PCI-E
167 _R("RTL8105e", RTL_GIGA_MAC_VER_29, 0xff7e1880), // PCI-E
168 _R("RTL8105e", RTL_GIGA_MAC_VER_30, 0xff7e1880) // PCI-E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170#undef _R
171
Francois Romieubcf0bf92006-07-26 23:14:13 +0200172enum cfg_version {
173 RTL_CFG_0 = 0x00,
174 RTL_CFG_1,
175 RTL_CFG_2
176};
177
Francois Romieu07ce4062007-02-23 23:36:39 +0100178static void rtl_hw_start_8169(struct net_device *);
179static void rtl_hw_start_8168(struct net_device *);
180static void rtl_hw_start_8101(struct net_device *);
181
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000182static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200183 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200184 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200185 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100186 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200187 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
188 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200189 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200190 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
191 { PCI_VENDOR_ID_LINKSYS, 0x1032,
192 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100193 { 0x0001, 0x8168,
194 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 {0,},
196};
197
198MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
199
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000200static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700201static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200202static struct {
203 u32 msg_enable;
204} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Francois Romieu07d3f512007-02-21 22:40:46 +0100206enum rtl_registers {
207 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100208 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100209 MAR0 = 8, /* Multicast filter. */
210 CounterAddrLow = 0x10,
211 CounterAddrHigh = 0x14,
212 TxDescStartAddrLow = 0x20,
213 TxDescStartAddrHigh = 0x24,
214 TxHDescStartAddrLow = 0x28,
215 TxHDescStartAddrHigh = 0x2c,
216 FLASH = 0x30,
217 ERSR = 0x36,
218 ChipCmd = 0x37,
219 TxPoll = 0x38,
220 IntrMask = 0x3c,
221 IntrStatus = 0x3e,
222 TxConfig = 0x40,
223 RxConfig = 0x44,
224 RxMissed = 0x4c,
225 Cfg9346 = 0x50,
226 Config0 = 0x51,
227 Config1 = 0x52,
228 Config2 = 0x53,
229 Config3 = 0x54,
230 Config4 = 0x55,
231 Config5 = 0x56,
232 MultiIntr = 0x5c,
233 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100234 PHYstatus = 0x6c,
235 RxMaxSize = 0xda,
236 CPlusCmd = 0xe0,
237 IntrMitigate = 0xe2,
238 RxDescAddrLow = 0xe4,
239 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000240 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
241
242#define NoEarlyTx 0x3f /* Max value : no early transmit. */
243
244 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
245
246#define TxPacketMax (8064 >> 7)
247
Francois Romieu07d3f512007-02-21 22:40:46 +0100248 FuncEvent = 0xf0,
249 FuncEventMask = 0xf4,
250 FuncPresetState = 0xf8,
251 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
Francois Romieuf162a5d2008-06-01 22:37:49 +0200254enum rtl8110_registers {
255 TBICSR = 0x64,
256 TBI_ANAR = 0x68,
257 TBI_LPAR = 0x6a,
258};
259
260enum rtl8168_8101_registers {
261 CSIDR = 0x64,
262 CSIAR = 0x68,
263#define CSIAR_FLAG 0x80000000
264#define CSIAR_WRITE_CMD 0x80000000
265#define CSIAR_BYTE_ENABLE 0x0f
266#define CSIAR_BYTE_ENABLE_SHIFT 12
267#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000268 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200269 EPHYAR = 0x80,
270#define EPHYAR_FLAG 0x80000000
271#define EPHYAR_WRITE_CMD 0x80000000
272#define EPHYAR_REG_MASK 0x1f
273#define EPHYAR_REG_SHIFT 16
274#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800275 DLLPR = 0xd0,
276#define PM_SWITCH (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200277 DBG_REG = 0xd1,
278#define FIX_NAK_1 (1 << 4)
279#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800280 TWSI = 0xd2,
281 MCU = 0xd3,
282#define EN_NDP (1 << 3)
283#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000284 EFUSEAR = 0xdc,
285#define EFUSEAR_FLAG 0x80000000
286#define EFUSEAR_WRITE_CMD 0x80000000
287#define EFUSEAR_READ_CMD 0x00000000
288#define EFUSEAR_REG_MASK 0x03ff
289#define EFUSEAR_REG_SHIFT 8
290#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200291};
292
françois romieuc0e45c12011-01-03 15:08:04 +0000293enum rtl8168_registers {
françois romieub646d902011-01-03 15:08:21 +0000294 ERIDR = 0x70,
295 ERIAR = 0x74,
296#define ERIAR_FLAG 0x80000000
297#define ERIAR_WRITE_CMD 0x80000000
298#define ERIAR_READ_CMD 0x00000000
299#define ERIAR_ADDR_BYTE_ALIGN 4
300#define ERIAR_EXGMAC 0
301#define ERIAR_MSIX 1
302#define ERIAR_ASF 2
303#define ERIAR_TYPE_SHIFT 16
304#define ERIAR_BYTEEN 0x0f
305#define ERIAR_BYTEEN_SHIFT 12
françois romieuc0e45c12011-01-03 15:08:04 +0000306 EPHY_RXER_NUM = 0x7c,
307 OCPDR = 0xb0, /* OCP GPHY access */
308#define OCPDR_WRITE_CMD 0x80000000
309#define OCPDR_READ_CMD 0x00000000
310#define OCPDR_REG_MASK 0x7f
311#define OCPDR_GPHY_REG_SHIFT 16
312#define OCPDR_DATA_MASK 0xffff
313 OCPAR = 0xb4,
314#define OCPAR_FLAG 0x80000000
315#define OCPAR_GPHY_WRITE_CMD 0x8000f060
316#define OCPAR_GPHY_READ_CMD 0x0000f060
françois romieue6de30d2011-01-03 15:08:37 +0000317 RDSAR1 = 0xd0 /* 8168c only. Undocumented on 8168dp */
françois romieuc0e45c12011-01-03 15:08:04 +0000318};
319
Francois Romieu07d3f512007-02-21 22:40:46 +0100320enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100322 SYSErr = 0x8000,
323 PCSTimeout = 0x4000,
324 SWInt = 0x0100,
325 TxDescUnavail = 0x0080,
326 RxFIFOOver = 0x0040,
327 LinkChg = 0x0020,
328 RxOverflow = 0x0010,
329 TxErr = 0x0008,
330 TxOK = 0x0004,
331 RxErr = 0x0002,
332 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200335 RxFOVF = (1 << 23),
336 RxRWT = (1 << 22),
337 RxRES = (1 << 21),
338 RxRUNT = (1 << 20),
339 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100342 CmdReset = 0x10,
343 CmdRxEnb = 0x08,
344 CmdTxEnb = 0x04,
345 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Francois Romieu275391a2007-02-23 23:50:28 +0100347 /* TXPoll register p.5 */
348 HPQ = 0x80, /* Poll cmd on the high prio queue */
349 NPQ = 0x40, /* Poll cmd on the low prio queue */
350 FSWInt = 0x01, /* Forced software interrupt */
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100353 Cfg9346_Lock = 0x00,
354 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100357 AcceptErr = 0x20,
358 AcceptRunt = 0x10,
359 AcceptBroadcast = 0x08,
360 AcceptMulticast = 0x04,
361 AcceptMyPhys = 0x02,
362 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100365 RxCfgFIFOShift = 13,
366 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* TxConfigBits */
369 TxInterFrameGapShift = 24,
370 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
371
Francois Romieu5d06a992006-02-23 00:47:58 +0100372 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200373 LEDS1 = (1 << 7),
374 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200375 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200376 Speed_down = (1 << 4),
377 MEMMAP = (1 << 3),
378 IOMAP = (1 << 2),
379 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100380 PMEnable = (1 << 0), /* Power Management Enable */
381
Francois Romieu6dccd162007-02-13 23:38:05 +0100382 /* Config2 register p. 25 */
383 PCI_Clock_66MHz = 0x01,
384 PCI_Clock_33MHz = 0x00,
385
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100386 /* Config3 register p.25 */
387 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
388 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200389 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100390
Francois Romieu5d06a992006-02-23 00:47:58 +0100391 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100392 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
393 MWF = (1 << 5), /* Accept Multicast wakeup frame */
394 UWF = (1 << 4), /* Accept Unicast wakeup frame */
395 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100396 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* TBICSR p.28 */
399 TBIReset = 0x80000000,
400 TBILoopback = 0x40000000,
401 TBINwEnable = 0x20000000,
402 TBINwRestart = 0x10000000,
403 TBILinkOk = 0x02000000,
404 TBINwComplete = 0x01000000,
405
406 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200407 EnableBist = (1 << 15), // 8168 8101
408 Mac_dbgo_oe = (1 << 14), // 8168 8101
409 Normal_mode = (1 << 13), // unused
410 Force_half_dup = (1 << 12), // 8168 8101
411 Force_rxflow_en = (1 << 11), // 8168 8101
412 Force_txflow_en = (1 << 10), // 8168 8101
413 Cxpl_dbg_sel = (1 << 9), // 8168 8101
414 ASF = (1 << 8), // 8168 8101
415 PktCntrDisable = (1 << 7), // 8168 8101
416 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 RxVlan = (1 << 6),
418 RxChkSum = (1 << 5),
419 PCIDAC = (1 << 4),
420 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100421 INTT_0 = 0x0000, // 8168
422 INTT_1 = 0x0001, // 8168
423 INTT_2 = 0x0002, // 8168
424 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100427 TBI_Enable = 0x80,
428 TxFlowCtrl = 0x40,
429 RxFlowCtrl = 0x20,
430 _1000bpsF = 0x10,
431 _100bps = 0x08,
432 _10bps = 0x04,
433 LinkStatus = 0x02,
434 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100437 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200438
439 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100440 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441};
442
Francois Romieu07d3f512007-02-21 22:40:46 +0100443enum desc_status_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
445 RingEnd = (1 << 30), /* End of descriptor ring */
446 FirstFrag = (1 << 29), /* First segment of a packet */
447 LastFrag = (1 << 28), /* Final segment of a packet */
448
449 /* Tx private */
450 LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
451 MSSShift = 16, /* MSS value position */
452 MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
453 IPCS = (1 << 18), /* Calculate IP checksum */
454 UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
455 TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
456 TxVlanTag = (1 << 17), /* Add VLAN tag */
457
458 /* Rx private */
459 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
460 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
461
462#define RxProtoUDP (PID1)
463#define RxProtoTCP (PID0)
464#define RxProtoIP (PID1 | PID0)
465#define RxProtoMask RxProtoIP
466
467 IPFail = (1 << 16), /* IP checksum failed */
468 UDPFail = (1 << 15), /* UDP/IP checksum failed */
469 TCPFail = (1 << 14), /* TCP/IP checksum failed */
470 RxVlanTag = (1 << 16), /* VLAN tag available */
471};
472
473#define RsvdMask 0x3fffc000
474
475struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200476 __le32 opts1;
477 __le32 opts2;
478 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479};
480
481struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200482 __le32 opts1;
483 __le32 opts2;
484 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485};
486
487struct ring_info {
488 struct sk_buff *skb;
489 u32 len;
490 u8 __pad[sizeof(void *) - sizeof(u32)];
491};
492
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200493enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200494 RTL_FEATURE_WOL = (1 << 0),
495 RTL_FEATURE_MSI = (1 << 1),
496 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200497};
498
Ivan Vecera355423d2009-02-06 21:49:57 -0800499struct rtl8169_counters {
500 __le64 tx_packets;
501 __le64 rx_packets;
502 __le64 tx_errors;
503 __le32 rx_errors;
504 __le16 rx_missed;
505 __le16 align_errors;
506 __le32 tx_one_collision;
507 __le32 tx_multi_collision;
508 __le64 rx_unicast;
509 __le64 rx_broadcast;
510 __le32 rx_multicast;
511 __le16 tx_aborted;
512 __le16 tx_underun;
513};
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515struct rtl8169_private {
516 void __iomem *mmio_addr; /* memory map physical address */
517 struct pci_dev *pci_dev; /* Index of PCI device */
David Howellsc4028952006-11-22 14:57:56 +0000518 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700519 struct napi_struct napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 spinlock_t lock; /* spin lock flag */
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200521 u32 msg_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int chipset;
523 int mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
525 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
526 u32 dirty_rx;
527 u32 dirty_tx;
528 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
529 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
530 dma_addr_t TxPhyAddr;
531 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000532 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct timer_list timer;
535 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100536 u16 intr_event;
537 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 u16 intr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int phy_1000_ctrl_reg;
françois romieuc0e45c12011-01-03 15:08:04 +0000540
541 struct mdio_ops {
542 void (*write)(void __iomem *, int, int);
543 int (*read)(void __iomem *, int);
544 } mdio_ops;
545
françois romieu065c27c2011-01-03 15:08:12 +0000546 struct pll_power_ops {
547 void (*down)(struct rtl8169_private *);
548 void (*up)(struct rtl8169_private *);
549 } pll_power_ops;
550
Oliver Neukum54405cd2011-01-06 21:55:13 +0100551 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200552 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000553 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100554 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000555 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800557 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200558 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000559 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200560 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200561
562 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800563 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000564 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000565
566 const struct firmware *fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567};
568
Ralf Baechle979b6c12005-06-13 14:30:40 -0700569MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700572MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200573module_param_named(debug, debug.msg_enable, int, 0);
574MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575MODULE_LICENSE("GPL");
576MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000577MODULE_FIRMWARE(FIRMWARE_8168D_1);
578MODULE_FIRMWARE(FIRMWARE_8168D_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800579MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000582static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
583 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100584static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100586static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100588static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200590static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700592 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200593static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200595static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700596static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200599 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
françois romieub646d902011-01-03 15:08:21 +0000601static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
602{
603 void __iomem *ioaddr = tp->mmio_addr;
604 int i;
605
606 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
607 for (i = 0; i < 20; i++) {
608 udelay(100);
609 if (RTL_R32(OCPAR) & OCPAR_FLAG)
610 break;
611 }
612 return RTL_R32(OCPDR);
613}
614
615static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
616{
617 void __iomem *ioaddr = tp->mmio_addr;
618 int i;
619
620 RTL_W32(OCPDR, data);
621 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
622 for (i = 0; i < 20; i++) {
623 udelay(100);
624 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
625 break;
626 }
627}
628
629static void rtl8168_oob_notify(void __iomem *ioaddr, u8 cmd)
630{
631 int i;
632
633 RTL_W8(ERIDR, cmd);
634 RTL_W32(ERIAR, 0x800010e8);
635 msleep(2);
636 for (i = 0; i < 5; i++) {
637 udelay(100);
638 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
639 break;
640 }
641
642 ocp_write(ioaddr, 0x1, 0x30, 0x00000001);
643}
644
645#define OOB_CMD_RESET 0x00
646#define OOB_CMD_DRIVER_START 0x05
647#define OOB_CMD_DRIVER_STOP 0x06
648
649static void rtl8168_driver_start(struct rtl8169_private *tp)
650{
651 int i;
652
653 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
654
655 for (i = 0; i < 10; i++) {
656 msleep(10);
657 if (ocp_read(tp, 0x0f, 0x0010) & 0x00000800)
658 break;
659 }
660}
661
662static void rtl8168_driver_stop(struct rtl8169_private *tp)
663{
664 int i;
665
666 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
667
668 for (i = 0; i < 10; i++) {
669 msleep(10);
670 if ((ocp_read(tp, 0x0f, 0x0010) & 0x00000800) == 0)
671 break;
672 }
673}
674
675
françois romieu4da19632011-01-03 15:07:55 +0000676static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 int i;
679
Francois Romieua6baf3a2007-11-08 23:23:21 +0100680 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Francois Romieu23714082006-01-29 00:49:09 +0100682 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100683 /*
684 * Check if the RTL8169 has completed writing to the specified
685 * MII register.
686 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200687 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 break;
Francois Romieu23714082006-01-29 00:49:09 +0100689 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700691 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700692 * According to hardware specs a 20us delay is required after write
693 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700694 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700695 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
françois romieu4da19632011-01-03 15:07:55 +0000698static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 int i, value = -1;
701
Francois Romieua6baf3a2007-11-08 23:23:21 +0100702 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Francois Romieu23714082006-01-29 00:49:09 +0100704 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100705 /*
706 * Check if the RTL8169 has completed retrieving data from
707 * the specified MII register.
708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100710 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
712 }
Francois Romieu23714082006-01-29 00:49:09 +0100713 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700715 /*
716 * According to hardware specs a 20us delay is required after read
717 * complete indication, but before sending next command.
718 */
719 udelay(20);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return value;
722}
723
françois romieuc0e45c12011-01-03 15:08:04 +0000724static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
725{
726 int i;
727
728 RTL_W32(OCPDR, data |
729 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
730 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
731 RTL_W32(EPHY_RXER_NUM, 0);
732
733 for (i = 0; i < 100; i++) {
734 mdelay(1);
735 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
736 break;
737 }
738}
739
740static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
741{
742 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
743 (value & OCPDR_DATA_MASK));
744}
745
746static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
747{
748 int i;
749
750 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
751
752 mdelay(1);
753 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
754 RTL_W32(EPHY_RXER_NUM, 0);
755
756 for (i = 0; i < 100; i++) {
757 mdelay(1);
758 if (RTL_R32(OCPAR) & OCPAR_FLAG)
759 break;
760 }
761
762 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
763}
764
françois romieue6de30d2011-01-03 15:08:37 +0000765#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
766
767static void r8168dp_2_mdio_start(void __iomem *ioaddr)
768{
769 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
770}
771
772static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
773{
774 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
775}
776
777static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
778{
779 r8168dp_2_mdio_start(ioaddr);
780
781 r8169_mdio_write(ioaddr, reg_addr, value);
782
783 r8168dp_2_mdio_stop(ioaddr);
784}
785
786static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
787{
788 int value;
789
790 r8168dp_2_mdio_start(ioaddr);
791
792 value = r8169_mdio_read(ioaddr, reg_addr);
793
794 r8168dp_2_mdio_stop(ioaddr);
795
796 return value;
797}
798
françois romieu4da19632011-01-03 15:07:55 +0000799static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200800{
françois romieuc0e45c12011-01-03 15:08:04 +0000801 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200802}
803
françois romieu4da19632011-01-03 15:07:55 +0000804static int rtl_readphy(struct rtl8169_private *tp, int location)
805{
françois romieuc0e45c12011-01-03 15:08:04 +0000806 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000807}
808
809static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
810{
811 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
812}
813
814static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000815{
816 int val;
817
françois romieu4da19632011-01-03 15:07:55 +0000818 val = rtl_readphy(tp, reg_addr);
819 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000820}
821
Francois Romieuccdffb92008-07-26 14:26:06 +0200822static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
823 int val)
824{
825 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200826
françois romieu4da19632011-01-03 15:07:55 +0000827 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200828}
829
830static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
831{
832 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200833
françois romieu4da19632011-01-03 15:07:55 +0000834 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200835}
836
Francois Romieudacf8152008-08-02 20:44:13 +0200837static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
838{
839 unsigned int i;
840
841 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
842 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
843
844 for (i = 0; i < 100; i++) {
845 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
846 break;
847 udelay(10);
848 }
849}
850
851static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
852{
853 u16 value = 0xffff;
854 unsigned int i;
855
856 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
857
858 for (i = 0; i < 100; i++) {
859 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
860 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
861 break;
862 }
863 udelay(10);
864 }
865
866 return value;
867}
868
869static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
870{
871 unsigned int i;
872
873 RTL_W32(CSIDR, value);
874 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
875 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
876
877 for (i = 0; i < 100; i++) {
878 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
879 break;
880 udelay(10);
881 }
882}
883
884static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
885{
886 u32 value = ~0x00;
887 unsigned int i;
888
889 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
890 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
891
892 for (i = 0; i < 100; i++) {
893 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
894 value = RTL_R32(CSIDR);
895 break;
896 }
897 udelay(10);
898 }
899
900 return value;
901}
902
françois romieudaf9df62009-10-07 12:44:20 +0000903static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
904{
905 u8 value = 0xff;
906 unsigned int i;
907
908 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
909
910 for (i = 0; i < 300; i++) {
911 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
912 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
913 break;
914 }
915 udelay(100);
916 }
917
918 return value;
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
922{
923 RTL_W16(IntrMask, 0x0000);
924
925 RTL_W16(IntrStatus, 0xffff);
926}
927
928static void rtl8169_asic_down(void __iomem *ioaddr)
929{
930 RTL_W8(ChipCmd, 0x00);
931 rtl8169_irq_mask_and_ack(ioaddr);
932 RTL_R16(CPlusCmd);
933}
934
françois romieu4da19632011-01-03 15:07:55 +0000935static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
françois romieu4da19632011-01-03 15:07:55 +0000937 void __iomem *ioaddr = tp->mmio_addr;
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return RTL_R32(TBICSR) & TBIReset;
940}
941
françois romieu4da19632011-01-03 15:07:55 +0000942static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
françois romieu4da19632011-01-03 15:07:55 +0000944 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
948{
949 return RTL_R32(TBICSR) & TBILinkOk;
950}
951
952static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
953{
954 return RTL_R8(PHYstatus) & LinkStatus;
955}
956
françois romieu4da19632011-01-03 15:07:55 +0000957static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
françois romieu4da19632011-01-03 15:07:55 +0000959 void __iomem *ioaddr = tp->mmio_addr;
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
962}
963
françois romieu4da19632011-01-03 15:07:55 +0000964static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 unsigned int val;
967
françois romieu4da19632011-01-03 15:07:55 +0000968 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
969 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000972static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieu07d3f512007-02-21 22:40:46 +0100973 struct rtl8169_private *tp,
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000974 void __iomem *ioaddr,
975 bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 unsigned long flags;
978
979 spin_lock_irqsave(&tp->lock, flags);
980 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000981 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000982 if (pm)
983 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +0100985 if (net_ratelimit())
986 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200987 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +0000989 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000990 if (pm)
991 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 spin_unlock_irqrestore(&tp->lock, flags);
994}
995
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000996static void rtl8169_check_link_status(struct net_device *dev,
997 struct rtl8169_private *tp,
998 void __iomem *ioaddr)
999{
1000 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1001}
1002
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001003#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1004
1005static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1006{
1007 void __iomem *ioaddr = tp->mmio_addr;
1008 u8 options;
1009 u32 wolopts = 0;
1010
1011 options = RTL_R8(Config1);
1012 if (!(options & PMEnable))
1013 return 0;
1014
1015 options = RTL_R8(Config3);
1016 if (options & LinkUp)
1017 wolopts |= WAKE_PHY;
1018 if (options & MagicPacket)
1019 wolopts |= WAKE_MAGIC;
1020
1021 options = RTL_R8(Config5);
1022 if (options & UWF)
1023 wolopts |= WAKE_UCAST;
1024 if (options & BWF)
1025 wolopts |= WAKE_BCAST;
1026 if (options & MWF)
1027 wolopts |= WAKE_MCAST;
1028
1029 return wolopts;
1030}
1031
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001032static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1033{
1034 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001035
1036 spin_lock_irq(&tp->lock);
1037
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001038 wol->supported = WAKE_ANY;
1039 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001040
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001041 spin_unlock_irq(&tp->lock);
1042}
1043
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001044static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001045{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001046 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001047 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001048 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001049 u32 opt;
1050 u16 reg;
1051 u8 mask;
1052 } cfg[] = {
1053 { WAKE_ANY, Config1, PMEnable },
1054 { WAKE_PHY, Config3, LinkUp },
1055 { WAKE_MAGIC, Config3, MagicPacket },
1056 { WAKE_UCAST, Config5, UWF },
1057 { WAKE_BCAST, Config5, BWF },
1058 { WAKE_MCAST, Config5, MWF },
1059 { WAKE_ANY, Config5, LanWake }
1060 };
1061
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001062 RTL_W8(Cfg9346, Cfg9346_Unlock);
1063
1064 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1065 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001066 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001067 options |= cfg[i].mask;
1068 RTL_W8(cfg[i].reg, options);
1069 }
1070
1071 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001072}
1073
1074static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1075{
1076 struct rtl8169_private *tp = netdev_priv(dev);
1077
1078 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001079
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001080 if (wol->wolopts)
1081 tp->features |= RTL_FEATURE_WOL;
1082 else
1083 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001084 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001085 spin_unlock_irq(&tp->lock);
1086
françois romieuea809072010-11-08 13:23:58 +00001087 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1088
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001089 return 0;
1090}
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092static void rtl8169_get_drvinfo(struct net_device *dev,
1093 struct ethtool_drvinfo *info)
1094{
1095 struct rtl8169_private *tp = netdev_priv(dev);
1096
1097 strcpy(info->driver, MODULENAME);
1098 strcpy(info->version, RTL8169_VERSION);
1099 strcpy(info->bus_info, pci_name(tp->pci_dev));
1100}
1101
1102static int rtl8169_get_regs_len(struct net_device *dev)
1103{
1104 return R8169_REGS_SIZE;
1105}
1106
1107static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001108 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 struct rtl8169_private *tp = netdev_priv(dev);
1111 void __iomem *ioaddr = tp->mmio_addr;
1112 int ret = 0;
1113 u32 reg;
1114
1115 reg = RTL_R32(TBICSR);
1116 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1117 (duplex == DUPLEX_FULL)) {
1118 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1119 } else if (autoneg == AUTONEG_ENABLE)
1120 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1121 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001122 netif_warn(tp, link, dev,
1123 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 ret = -EOPNOTSUPP;
1125 }
1126
1127 return ret;
1128}
1129
1130static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001131 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001134 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001135 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Hayes Wang716b50a2011-02-22 17:26:18 +08001137 rtl_writephy(tp, 0x1f, 0x0000);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001140 int auto_nego;
1141
françois romieu4da19632011-01-03 15:07:55 +00001142 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001143 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1144 ADVERTISE_100HALF | ADVERTISE_100FULL);
1145
1146 if (adv & ADVERTISED_10baseT_Half)
1147 auto_nego |= ADVERTISE_10HALF;
1148 if (adv & ADVERTISED_10baseT_Full)
1149 auto_nego |= ADVERTISE_10FULL;
1150 if (adv & ADVERTISED_100baseT_Half)
1151 auto_nego |= ADVERTISE_100HALF;
1152 if (adv & ADVERTISED_100baseT_Full)
1153 auto_nego |= ADVERTISE_100FULL;
1154
françois romieu3577aa12009-05-19 10:46:48 +00001155 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1156
françois romieu4da19632011-01-03 15:07:55 +00001157 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001158 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1159
1160 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1161 if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
1162 (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
1163 (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
1164 (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
1165 (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
1166 (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
1167 (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
Hayes Wang5a5e4442011-02-22 17:26:21 +08001168 (tp->mac_version != RTL_GIGA_MAC_VER_16) &&
1169 (tp->mac_version != RTL_GIGA_MAC_VER_29) &&
1170 (tp->mac_version != RTL_GIGA_MAC_VER_30)) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001171 if (adv & ADVERTISED_1000baseT_Half)
1172 giga_ctrl |= ADVERTISE_1000HALF;
1173 if (adv & ADVERTISED_1000baseT_Full)
1174 giga_ctrl |= ADVERTISE_1000FULL;
1175 } else if (adv & (ADVERTISED_1000baseT_Half |
1176 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001177 netif_info(tp, link, dev,
1178 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001179 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
françois romieu3577aa12009-05-19 10:46:48 +00001182 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001183
françois romieu4da19632011-01-03 15:07:55 +00001184 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1185 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001186 } else {
1187 giga_ctrl = 0;
1188
1189 if (speed == SPEED_10)
1190 bmcr = 0;
1191 else if (speed == SPEED_100)
1192 bmcr = BMCR_SPEED100;
1193 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001194 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001195
1196 if (duplex == DUPLEX_FULL)
1197 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001198 }
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 tp->phy_1000_ctrl_reg = giga_ctrl;
1201
françois romieu4da19632011-01-03 15:07:55 +00001202 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001203
1204 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
1205 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
1206 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001207 rtl_writephy(tp, 0x17, 0x2138);
1208 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001209 } else {
françois romieu4da19632011-01-03 15:07:55 +00001210 rtl_writephy(tp, 0x17, 0x2108);
1211 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001212 }
1213 }
1214
Oliver Neukum54405cd2011-01-06 21:55:13 +01001215 rc = 0;
1216out:
1217 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001221 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 struct rtl8169_private *tp = netdev_priv(dev);
1224 int ret;
1225
Oliver Neukum54405cd2011-01-06 21:55:13 +01001226 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001228 if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1230
1231 return ret;
1232}
1233
1234static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1235{
1236 struct rtl8169_private *tp = netdev_priv(dev);
1237 unsigned long flags;
1238 int ret;
1239
1240 spin_lock_irqsave(&tp->lock, flags);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001241 ret = rtl8169_set_speed(dev,
1242 cmd->autoneg, cmd->speed, cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return ret;
1246}
1247
1248static u32 rtl8169_get_rx_csum(struct net_device *dev)
1249{
1250 struct rtl8169_private *tp = netdev_priv(dev);
1251
1252 return tp->cp_cmd & RxChkSum;
1253}
1254
1255static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
1256{
1257 struct rtl8169_private *tp = netdev_priv(dev);
1258 void __iomem *ioaddr = tp->mmio_addr;
1259 unsigned long flags;
1260
1261 spin_lock_irqsave(&tp->lock, flags);
1262
1263 if (data)
1264 tp->cp_cmd |= RxChkSum;
1265 else
1266 tp->cp_cmd &= ~RxChkSum;
1267
1268 RTL_W16(CPlusCmd, tp->cp_cmd);
1269 RTL_R16(CPlusCmd);
1270
1271 spin_unlock_irqrestore(&tp->lock, flags);
1272
1273 return 0;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1277 struct sk_buff *skb)
1278{
Jesse Grosseab6d182010-10-20 13:56:03 +00001279 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1281}
1282
Francois Romieu7a8fc772011-03-01 17:18:33 +01001283#define NETIF_F_HW_VLAN_TX_RX (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX)
1284
1285static void rtl8169_vlan_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct rtl8169_private *tp = netdev_priv(dev);
1288 void __iomem *ioaddr = tp->mmio_addr;
1289 unsigned long flags;
1290
1291 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu7a8fc772011-03-01 17:18:33 +01001292 if (dev->features & NETIF_F_HW_VLAN_RX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 tp->cp_cmd |= RxVlan;
1294 else
1295 tp->cp_cmd &= ~RxVlan;
1296 RTL_W16(CPlusCmd, tp->cp_cmd);
Francois Romieu7a8fc772011-03-01 17:18:33 +01001297 /* PCI commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 RTL_R16(CPlusCmd);
1299 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu7a8fc772011-03-01 17:18:33 +01001300
1301 dev->vlan_features = dev->features &~ NETIF_F_HW_VLAN_TX_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Francois Romieu7a8fc772011-03-01 17:18:33 +01001304static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Francois Romieu7a8fc772011-03-01 17:18:33 +01001308 if (opts2 & RxVlanTag)
1309 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Francois Romieuccdffb92008-07-26 14:26:06 +02001314static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct rtl8169_private *tp = netdev_priv(dev);
1317 void __iomem *ioaddr = tp->mmio_addr;
1318 u32 status;
1319
1320 cmd->supported =
1321 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1322 cmd->port = PORT_FIBRE;
1323 cmd->transceiver = XCVR_INTERNAL;
1324
1325 status = RTL_R32(TBICSR);
1326 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1327 cmd->autoneg = !!(status & TBINwEnable);
1328
1329 cmd->speed = SPEED_1000;
1330 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001331
1332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Francois Romieuccdffb92008-07-26 14:26:06 +02001335static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Francois Romieuccdffb92008-07-26 14:26:06 +02001339 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1343{
1344 struct rtl8169_private *tp = netdev_priv(dev);
1345 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001346 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 spin_lock_irqsave(&tp->lock, flags);
1349
Francois Romieuccdffb92008-07-26 14:26:06 +02001350 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001353 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1357 void *p)
1358{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001359 struct rtl8169_private *tp = netdev_priv(dev);
1360 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Francois Romieu5b0384f2006-08-16 16:00:01 +02001362 if (regs->len > R8169_REGS_SIZE)
1363 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Francois Romieu5b0384f2006-08-16 16:00:01 +02001365 spin_lock_irqsave(&tp->lock, flags);
1366 memcpy_fromio(p, tp->mmio_addr, regs->len);
1367 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001370static u32 rtl8169_get_msglevel(struct net_device *dev)
1371{
1372 struct rtl8169_private *tp = netdev_priv(dev);
1373
1374 return tp->msg_enable;
1375}
1376
1377static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1378{
1379 struct rtl8169_private *tp = netdev_priv(dev);
1380
1381 tp->msg_enable = value;
1382}
1383
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001384static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1385 "tx_packets",
1386 "rx_packets",
1387 "tx_errors",
1388 "rx_errors",
1389 "rx_missed",
1390 "align_errors",
1391 "tx_single_collisions",
1392 "tx_multi_collisions",
1393 "unicast",
1394 "broadcast",
1395 "multicast",
1396 "tx_aborted",
1397 "tx_underrun",
1398};
1399
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001400static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001401{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001402 switch (sset) {
1403 case ETH_SS_STATS:
1404 return ARRAY_SIZE(rtl8169_gstrings);
1405 default:
1406 return -EOPNOTSUPP;
1407 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001408}
1409
Ivan Vecera355423d2009-02-06 21:49:57 -08001410static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001411{
1412 struct rtl8169_private *tp = netdev_priv(dev);
1413 void __iomem *ioaddr = tp->mmio_addr;
1414 struct rtl8169_counters *counters;
1415 dma_addr_t paddr;
1416 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001417 int wait = 1000;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001418 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001419
Ivan Vecera355423d2009-02-06 21:49:57 -08001420 /*
1421 * Some chips are unable to dump tally counters when the receiver
1422 * is disabled.
1423 */
1424 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1425 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001426
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001427 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001428 if (!counters)
1429 return;
1430
1431 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001432 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001433 RTL_W32(CounterAddrLow, cmd);
1434 RTL_W32(CounterAddrLow, cmd | CounterDump);
1435
Ivan Vecera355423d2009-02-06 21:49:57 -08001436 while (wait--) {
1437 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1438 /* copy updated counters */
1439 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001440 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001441 }
1442 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001443 }
1444
1445 RTL_W32(CounterAddrLow, 0);
1446 RTL_W32(CounterAddrHigh, 0);
1447
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001448 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001449}
1450
Ivan Vecera355423d2009-02-06 21:49:57 -08001451static void rtl8169_get_ethtool_stats(struct net_device *dev,
1452 struct ethtool_stats *stats, u64 *data)
1453{
1454 struct rtl8169_private *tp = netdev_priv(dev);
1455
1456 ASSERT_RTNL();
1457
1458 rtl8169_update_counters(dev);
1459
1460 data[0] = le64_to_cpu(tp->counters.tx_packets);
1461 data[1] = le64_to_cpu(tp->counters.rx_packets);
1462 data[2] = le64_to_cpu(tp->counters.tx_errors);
1463 data[3] = le32_to_cpu(tp->counters.rx_errors);
1464 data[4] = le16_to_cpu(tp->counters.rx_missed);
1465 data[5] = le16_to_cpu(tp->counters.align_errors);
1466 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1467 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1468 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1469 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1470 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1471 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1472 data[12] = le16_to_cpu(tp->counters.tx_underun);
1473}
1474
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001475static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1476{
1477 switch(stringset) {
1478 case ETH_SS_STATS:
1479 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1480 break;
1481 }
1482}
1483
Francois Romieu7a8fc772011-03-01 17:18:33 +01001484static int rtl8169_set_flags(struct net_device *dev, u32 data)
1485{
1486 struct rtl8169_private *tp = netdev_priv(dev);
1487 unsigned long old_feat = dev->features;
1488 int rc;
1489
1490 if ((tp->mac_version == RTL_GIGA_MAC_VER_05) &&
1491 !(data & ETH_FLAG_RXVLAN)) {
1492 netif_info(tp, drv, dev, "8110SCd requires hardware Rx VLAN\n");
1493 return -EINVAL;
1494 }
1495
1496 rc = ethtool_op_set_flags(dev, data, ETH_FLAG_TXVLAN | ETH_FLAG_RXVLAN);
1497 if (rc)
1498 return rc;
1499
1500 if ((old_feat ^ dev->features) & NETIF_F_HW_VLAN_RX)
1501 rtl8169_vlan_mode(dev);
1502
1503 return 0;
1504}
1505
Jeff Garzik7282d492006-09-13 14:30:00 -04001506static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 .get_drvinfo = rtl8169_get_drvinfo,
1508 .get_regs_len = rtl8169_get_regs_len,
1509 .get_link = ethtool_op_get_link,
1510 .get_settings = rtl8169_get_settings,
1511 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001512 .get_msglevel = rtl8169_get_msglevel,
1513 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 .get_rx_csum = rtl8169_get_rx_csum,
1515 .set_rx_csum = rtl8169_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 .set_tx_csum = ethtool_op_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 .set_tso = ethtool_op_set_tso,
1519 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001520 .get_wol = rtl8169_get_wol,
1521 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001522 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001523 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001524 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Francois Romieu7a8fc772011-03-01 17:18:33 +01001525 .set_flags = rtl8169_set_flags,
1526 .get_flags = ethtool_op_get_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527};
1528
Francois Romieu07d3f512007-02-21 22:40:46 +01001529static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1530 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Francois Romieu0e485152007-02-20 00:00:26 +01001532 /*
1533 * The driver currently handles the 8168Bf and the 8168Be identically
1534 * but they can be identified more specifically through the test below
1535 * if needed:
1536 *
1537 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001538 *
1539 * Same thing for the 8101Eb and the 8101Ec:
1540 *
1541 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001542 */
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001543 static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001545 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 int mac_version;
1547 } mac_info[] = {
Francois Romieu5b538df2008-07-20 16:22:45 +02001548 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001549 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1550 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001551 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001552
françois romieue6de30d2011-01-03 15:08:37 +00001553 /* 8168DP family. */
1554 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1555 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1556
Francois Romieuef808d52008-06-29 13:10:54 +02001557 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001558 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001559 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001560 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001561 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001562 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1563 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001564 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001565 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001566 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001567
1568 /* 8168B family. */
1569 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1570 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1571 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1572 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1573
1574 /* 8101 family. */
Hayes Wang5a5e4442011-02-22 17:26:21 +08001575 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1576 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1577 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001578 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1579 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1580 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1581 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1582 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1583 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001584 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001585 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001586 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001587 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1588 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001589 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1590 /* FIXME: where did these entries come from ? -- FR */
1591 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1592 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1593
1594 /* 8110 family. */
1595 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1596 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1597 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1598 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1599 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1600 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1601
Jean Delvaref21b75e2009-05-26 20:54:48 -07001602 /* Catch-all */
1603 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }, *p = mac_info;
1605 u32 reg;
1606
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001607 reg = RTL_R32(TxConfig);
1608 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 p++;
1610 tp->mac_version = p->mac_version;
1611}
1612
1613static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1614{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001615 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
Francois Romieu867763c2007-08-17 18:21:58 +02001618struct phy_reg {
1619 u16 reg;
1620 u16 val;
1621};
1622
françois romieu4da19632011-01-03 15:07:55 +00001623static void rtl_writephy_batch(struct rtl8169_private *tp,
1624 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001625{
1626 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001627 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001628 regs++;
1629 }
1630}
1631
françois romieubca03d52011-01-03 15:07:31 +00001632#define PHY_READ 0x00000000
1633#define PHY_DATA_OR 0x10000000
1634#define PHY_DATA_AND 0x20000000
1635#define PHY_BJMPN 0x30000000
1636#define PHY_READ_EFUSE 0x40000000
1637#define PHY_READ_MAC_BYTE 0x50000000
1638#define PHY_WRITE_MAC_BYTE 0x60000000
1639#define PHY_CLEAR_READCOUNT 0x70000000
1640#define PHY_WRITE 0x80000000
1641#define PHY_READCOUNT_EQ_SKIP 0x90000000
1642#define PHY_COMP_EQ_SKIPN 0xa0000000
1643#define PHY_COMP_NEQ_SKIPN 0xb0000000
1644#define PHY_WRITE_PREVIOUS 0xc0000000
1645#define PHY_SKIPN 0xd0000000
1646#define PHY_DELAY_MS 0xe0000000
1647#define PHY_WRITE_ERI_WORD 0xf0000000
1648
1649static void
1650rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1651{
françois romieubca03d52011-01-03 15:07:31 +00001652 __le32 *phytable = (__le32 *)fw->data;
1653 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001654 size_t index, fw_size = fw->size / sizeof(*phytable);
1655 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001656
1657 if (fw->size % sizeof(*phytable)) {
1658 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1659 return;
1660 }
1661
hayeswang42b82dc2011-01-10 02:07:25 +00001662 for (index = 0; index < fw_size; index++) {
1663 u32 action = le32_to_cpu(phytable[index]);
1664 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001665
hayeswang42b82dc2011-01-10 02:07:25 +00001666 switch(action & 0xf0000000) {
1667 case PHY_READ:
1668 case PHY_DATA_OR:
1669 case PHY_DATA_AND:
1670 case PHY_READ_EFUSE:
1671 case PHY_CLEAR_READCOUNT:
1672 case PHY_WRITE:
1673 case PHY_WRITE_PREVIOUS:
1674 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001675 break;
1676
hayeswang42b82dc2011-01-10 02:07:25 +00001677 case PHY_BJMPN:
1678 if (regno > index) {
1679 netif_err(tp, probe, tp->dev,
1680 "Out of range of firmware\n");
1681 return;
1682 }
1683 break;
1684 case PHY_READCOUNT_EQ_SKIP:
1685 if (index + 2 >= fw_size) {
1686 netif_err(tp, probe, tp->dev,
1687 "Out of range of firmware\n");
1688 return;
1689 }
1690 break;
1691 case PHY_COMP_EQ_SKIPN:
1692 case PHY_COMP_NEQ_SKIPN:
1693 case PHY_SKIPN:
1694 if (index + 1 + regno >= fw_size) {
1695 netif_err(tp, probe, tp->dev,
1696 "Out of range of firmware\n");
1697 return;
1698 }
1699 break;
1700
1701 case PHY_READ_MAC_BYTE:
1702 case PHY_WRITE_MAC_BYTE:
1703 case PHY_WRITE_ERI_WORD:
1704 default:
1705 netif_err(tp, probe, tp->dev,
1706 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001707 return;
1708 }
1709 }
1710
hayeswang42b82dc2011-01-10 02:07:25 +00001711 predata = 0;
1712 count = 0;
1713
1714 for (index = 0; index < fw_size; ) {
1715 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001716 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001717 u32 regno = (action & 0x0fff0000) >> 16;
1718
1719 if (!action)
1720 break;
françois romieubca03d52011-01-03 15:07:31 +00001721
1722 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001723 case PHY_READ:
1724 predata = rtl_readphy(tp, regno);
1725 count++;
1726 index++;
françois romieubca03d52011-01-03 15:07:31 +00001727 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001728 case PHY_DATA_OR:
1729 predata |= data;
1730 index++;
1731 break;
1732 case PHY_DATA_AND:
1733 predata &= data;
1734 index++;
1735 break;
1736 case PHY_BJMPN:
1737 index -= regno;
1738 break;
1739 case PHY_READ_EFUSE:
1740 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1741 index++;
1742 break;
1743 case PHY_CLEAR_READCOUNT:
1744 count = 0;
1745 index++;
1746 break;
1747 case PHY_WRITE:
1748 rtl_writephy(tp, regno, data);
1749 index++;
1750 break;
1751 case PHY_READCOUNT_EQ_SKIP:
1752 if (count == data)
1753 index += 2;
1754 else
1755 index += 1;
1756 break;
1757 case PHY_COMP_EQ_SKIPN:
1758 if (predata == data)
1759 index += regno;
1760 index++;
1761 break;
1762 case PHY_COMP_NEQ_SKIPN:
1763 if (predata != data)
1764 index += regno;
1765 index++;
1766 break;
1767 case PHY_WRITE_PREVIOUS:
1768 rtl_writephy(tp, regno, predata);
1769 index++;
1770 break;
1771 case PHY_SKIPN:
1772 index += regno + 1;
1773 break;
1774 case PHY_DELAY_MS:
1775 mdelay(data);
1776 index++;
1777 break;
1778
1779 case PHY_READ_MAC_BYTE:
1780 case PHY_WRITE_MAC_BYTE:
1781 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001782 default:
1783 BUG();
1784 }
1785 }
1786}
1787
françois romieuf1e02ed2011-01-13 13:07:53 +00001788static void rtl_release_firmware(struct rtl8169_private *tp)
1789{
1790 release_firmware(tp->fw);
1791 tp->fw = NULL;
1792}
1793
1794static int rtl_apply_firmware(struct rtl8169_private *tp, const char *fw_name)
1795{
1796 const struct firmware **fw = &tp->fw;
1797 int rc = !*fw;
1798
1799 if (rc) {
1800 rc = request_firmware(fw, fw_name, &tp->pci_dev->dev);
1801 if (rc < 0)
1802 goto out;
1803 }
1804
1805 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1806 rtl_phy_write_fw(tp, *fw);
1807out:
1808 return rc;
1809}
1810
françois romieu4da19632011-01-03 15:07:55 +00001811static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001813 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001814 { 0x1f, 0x0001 },
1815 { 0x06, 0x006e },
1816 { 0x08, 0x0708 },
1817 { 0x15, 0x4000 },
1818 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
françois romieu0b9b5712009-08-10 19:44:56 +00001820 { 0x1f, 0x0001 },
1821 { 0x03, 0x00a1 },
1822 { 0x02, 0x0008 },
1823 { 0x01, 0x0120 },
1824 { 0x00, 0x1000 },
1825 { 0x04, 0x0800 },
1826 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
françois romieu0b9b5712009-08-10 19:44:56 +00001828 { 0x03, 0xff41 },
1829 { 0x02, 0xdf60 },
1830 { 0x01, 0x0140 },
1831 { 0x00, 0x0077 },
1832 { 0x04, 0x7800 },
1833 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
françois romieu0b9b5712009-08-10 19:44:56 +00001835 { 0x03, 0x802f },
1836 { 0x02, 0x4f02 },
1837 { 0x01, 0x0409 },
1838 { 0x00, 0xf0f9 },
1839 { 0x04, 0x9800 },
1840 { 0x04, 0x9000 },
1841
1842 { 0x03, 0xdf01 },
1843 { 0x02, 0xdf20 },
1844 { 0x01, 0xff95 },
1845 { 0x00, 0xba00 },
1846 { 0x04, 0xa800 },
1847 { 0x04, 0xa000 },
1848
1849 { 0x03, 0xff41 },
1850 { 0x02, 0xdf20 },
1851 { 0x01, 0x0140 },
1852 { 0x00, 0x00bb },
1853 { 0x04, 0xb800 },
1854 { 0x04, 0xb000 },
1855
1856 { 0x03, 0xdf41 },
1857 { 0x02, 0xdc60 },
1858 { 0x01, 0x6340 },
1859 { 0x00, 0x007d },
1860 { 0x04, 0xd800 },
1861 { 0x04, 0xd000 },
1862
1863 { 0x03, 0xdf01 },
1864 { 0x02, 0xdf20 },
1865 { 0x01, 0x100a },
1866 { 0x00, 0xa0ff },
1867 { 0x04, 0xf800 },
1868 { 0x04, 0xf000 },
1869
1870 { 0x1f, 0x0000 },
1871 { 0x0b, 0x0000 },
1872 { 0x00, 0x9200 }
1873 };
1874
françois romieu4da19632011-01-03 15:07:55 +00001875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
françois romieu4da19632011-01-03 15:07:55 +00001878static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001879{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001880 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001881 { 0x1f, 0x0002 },
1882 { 0x01, 0x90d0 },
1883 { 0x1f, 0x0000 }
1884 };
1885
françois romieu4da19632011-01-03 15:07:55 +00001886 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001887}
1888
françois romieu4da19632011-01-03 15:07:55 +00001889static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001890{
1891 struct pci_dev *pdev = tp->pci_dev;
1892 u16 vendor_id, device_id;
1893
1894 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1895 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1896
1897 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1898 return;
1899
françois romieu4da19632011-01-03 15:07:55 +00001900 rtl_writephy(tp, 0x1f, 0x0001);
1901 rtl_writephy(tp, 0x10, 0xf01b);
1902 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001903}
1904
françois romieu4da19632011-01-03 15:07:55 +00001905static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001906{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001907 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00001908 { 0x1f, 0x0001 },
1909 { 0x04, 0x0000 },
1910 { 0x03, 0x00a1 },
1911 { 0x02, 0x0008 },
1912 { 0x01, 0x0120 },
1913 { 0x00, 0x1000 },
1914 { 0x04, 0x0800 },
1915 { 0x04, 0x9000 },
1916 { 0x03, 0x802f },
1917 { 0x02, 0x4f02 },
1918 { 0x01, 0x0409 },
1919 { 0x00, 0xf099 },
1920 { 0x04, 0x9800 },
1921 { 0x04, 0xa000 },
1922 { 0x03, 0xdf01 },
1923 { 0x02, 0xdf20 },
1924 { 0x01, 0xff95 },
1925 { 0x00, 0xba00 },
1926 { 0x04, 0xa800 },
1927 { 0x04, 0xf000 },
1928 { 0x03, 0xdf01 },
1929 { 0x02, 0xdf20 },
1930 { 0x01, 0x101a },
1931 { 0x00, 0xa0ff },
1932 { 0x04, 0xf800 },
1933 { 0x04, 0x0000 },
1934 { 0x1f, 0x0000 },
1935
1936 { 0x1f, 0x0001 },
1937 { 0x10, 0xf41b },
1938 { 0x14, 0xfb54 },
1939 { 0x18, 0xf5c7 },
1940 { 0x1f, 0x0000 },
1941
1942 { 0x1f, 0x0001 },
1943 { 0x17, 0x0cc0 },
1944 { 0x1f, 0x0000 }
1945 };
1946
françois romieu4da19632011-01-03 15:07:55 +00001947 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00001948
françois romieu4da19632011-01-03 15:07:55 +00001949 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00001950}
1951
françois romieu4da19632011-01-03 15:07:55 +00001952static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00001953{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001954 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00001955 { 0x1f, 0x0001 },
1956 { 0x04, 0x0000 },
1957 { 0x03, 0x00a1 },
1958 { 0x02, 0x0008 },
1959 { 0x01, 0x0120 },
1960 { 0x00, 0x1000 },
1961 { 0x04, 0x0800 },
1962 { 0x04, 0x9000 },
1963 { 0x03, 0x802f },
1964 { 0x02, 0x4f02 },
1965 { 0x01, 0x0409 },
1966 { 0x00, 0xf099 },
1967 { 0x04, 0x9800 },
1968 { 0x04, 0xa000 },
1969 { 0x03, 0xdf01 },
1970 { 0x02, 0xdf20 },
1971 { 0x01, 0xff95 },
1972 { 0x00, 0xba00 },
1973 { 0x04, 0xa800 },
1974 { 0x04, 0xf000 },
1975 { 0x03, 0xdf01 },
1976 { 0x02, 0xdf20 },
1977 { 0x01, 0x101a },
1978 { 0x00, 0xa0ff },
1979 { 0x04, 0xf800 },
1980 { 0x04, 0x0000 },
1981 { 0x1f, 0x0000 },
1982
1983 { 0x1f, 0x0001 },
1984 { 0x0b, 0x8480 },
1985 { 0x1f, 0x0000 },
1986
1987 { 0x1f, 0x0001 },
1988 { 0x18, 0x67c7 },
1989 { 0x04, 0x2000 },
1990 { 0x03, 0x002f },
1991 { 0x02, 0x4360 },
1992 { 0x01, 0x0109 },
1993 { 0x00, 0x3022 },
1994 { 0x04, 0x2800 },
1995 { 0x1f, 0x0000 },
1996
1997 { 0x1f, 0x0001 },
1998 { 0x17, 0x0cc0 },
1999 { 0x1f, 0x0000 }
2000 };
2001
françois romieu4da19632011-01-03 15:07:55 +00002002 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002003}
2004
françois romieu4da19632011-01-03 15:07:55 +00002005static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002006{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002007 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002008 { 0x10, 0xf41b },
2009 { 0x1f, 0x0000 }
2010 };
2011
françois romieu4da19632011-01-03 15:07:55 +00002012 rtl_writephy(tp, 0x1f, 0x0001);
2013 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002014
françois romieu4da19632011-01-03 15:07:55 +00002015 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002016}
2017
françois romieu4da19632011-01-03 15:07:55 +00002018static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002019{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002020 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002021 { 0x1f, 0x0001 },
2022 { 0x10, 0xf41b },
2023 { 0x1f, 0x0000 }
2024 };
2025
françois romieu4da19632011-01-03 15:07:55 +00002026 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002027}
2028
françois romieu4da19632011-01-03 15:07:55 +00002029static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002030{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002031 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002032 { 0x1f, 0x0000 },
2033 { 0x1d, 0x0f00 },
2034 { 0x1f, 0x0002 },
2035 { 0x0c, 0x1ec8 },
2036 { 0x1f, 0x0000 }
2037 };
2038
françois romieu4da19632011-01-03 15:07:55 +00002039 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002040}
2041
françois romieu4da19632011-01-03 15:07:55 +00002042static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002043{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002044 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002045 { 0x1f, 0x0001 },
2046 { 0x1d, 0x3d98 },
2047 { 0x1f, 0x0000 }
2048 };
2049
françois romieu4da19632011-01-03 15:07:55 +00002050 rtl_writephy(tp, 0x1f, 0x0000);
2051 rtl_patchphy(tp, 0x14, 1 << 5);
2052 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002053
françois romieu4da19632011-01-03 15:07:55 +00002054 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002055}
2056
françois romieu4da19632011-01-03 15:07:55 +00002057static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002058{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002059 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002060 { 0x1f, 0x0001 },
2061 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002062 { 0x1f, 0x0002 },
2063 { 0x00, 0x88d4 },
2064 { 0x01, 0x82b1 },
2065 { 0x03, 0x7002 },
2066 { 0x08, 0x9e30 },
2067 { 0x09, 0x01f0 },
2068 { 0x0a, 0x5500 },
2069 { 0x0c, 0x00c8 },
2070 { 0x1f, 0x0003 },
2071 { 0x12, 0xc096 },
2072 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002073 { 0x1f, 0x0000 },
2074 { 0x1f, 0x0000 },
2075 { 0x09, 0x2000 },
2076 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002077 };
2078
françois romieu4da19632011-01-03 15:07:55 +00002079 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002080
françois romieu4da19632011-01-03 15:07:55 +00002081 rtl_patchphy(tp, 0x14, 1 << 5);
2082 rtl_patchphy(tp, 0x0d, 1 << 5);
2083 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002084}
2085
françois romieu4da19632011-01-03 15:07:55 +00002086static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002087{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002088 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002089 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002090 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002091 { 0x03, 0x802f },
2092 { 0x02, 0x4f02 },
2093 { 0x01, 0x0409 },
2094 { 0x00, 0xf099 },
2095 { 0x04, 0x9800 },
2096 { 0x04, 0x9000 },
2097 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002098 { 0x1f, 0x0002 },
2099 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002100 { 0x06, 0x0761 },
2101 { 0x1f, 0x0003 },
2102 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002103 { 0x1f, 0x0000 }
2104 };
2105
françois romieu4da19632011-01-03 15:07:55 +00002106 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002107
françois romieu4da19632011-01-03 15:07:55 +00002108 rtl_patchphy(tp, 0x16, 1 << 0);
2109 rtl_patchphy(tp, 0x14, 1 << 5);
2110 rtl_patchphy(tp, 0x0d, 1 << 5);
2111 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002112}
2113
françois romieu4da19632011-01-03 15:07:55 +00002114static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002115{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002116 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002117 { 0x1f, 0x0001 },
2118 { 0x12, 0x2300 },
2119 { 0x1d, 0x3d98 },
2120 { 0x1f, 0x0002 },
2121 { 0x0c, 0x7eb8 },
2122 { 0x06, 0x5461 },
2123 { 0x1f, 0x0003 },
2124 { 0x16, 0x0f0a },
2125 { 0x1f, 0x0000 }
2126 };
2127
françois romieu4da19632011-01-03 15:07:55 +00002128 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002129
françois romieu4da19632011-01-03 15:07:55 +00002130 rtl_patchphy(tp, 0x16, 1 << 0);
2131 rtl_patchphy(tp, 0x14, 1 << 5);
2132 rtl_patchphy(tp, 0x0d, 1 << 5);
2133 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002134}
2135
françois romieu4da19632011-01-03 15:07:55 +00002136static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002137{
françois romieu4da19632011-01-03 15:07:55 +00002138 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002139}
2140
françois romieubca03d52011-01-03 15:07:31 +00002141static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002142{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002143 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002144 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002145 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002146 { 0x06, 0x4064 },
2147 { 0x07, 0x2863 },
2148 { 0x08, 0x059c },
2149 { 0x09, 0x26b4 },
2150 { 0x0a, 0x6a19 },
2151 { 0x0b, 0xdcc8 },
2152 { 0x10, 0xf06d },
2153 { 0x14, 0x7f68 },
2154 { 0x18, 0x7fd9 },
2155 { 0x1c, 0xf0ff },
2156 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002157 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002158 { 0x12, 0xf49f },
2159 { 0x13, 0x070b },
2160 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002161 { 0x14, 0x94c0 },
2162
2163 /*
2164 * Tx Error Issue
2165 * enhance line driver power
2166 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002167 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002168 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002169 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002170 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002171 { 0x06, 0x5561 },
2172
2173 /*
2174 * Can not link to 1Gbps with bad cable
2175 * Decrease SNR threshold form 21.07dB to 19.04dB
2176 */
2177 { 0x1f, 0x0001 },
2178 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002179
2180 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002181 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002182 };
françois romieubca03d52011-01-03 15:07:31 +00002183 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002184
françois romieu4da19632011-01-03 15:07:55 +00002185 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002186
françois romieubca03d52011-01-03 15:07:31 +00002187 /*
2188 * Rx Error Issue
2189 * Fine Tune Switching regulator parameter
2190 */
françois romieu4da19632011-01-03 15:07:55 +00002191 rtl_writephy(tp, 0x1f, 0x0002);
2192 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2193 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002194
françois romieudaf9df62009-10-07 12:44:20 +00002195 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002196 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002197 { 0x1f, 0x0002 },
2198 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002199 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002200 { 0x05, 0x8330 },
2201 { 0x06, 0x669a },
2202 { 0x1f, 0x0002 }
2203 };
2204 int val;
2205
françois romieu4da19632011-01-03 15:07:55 +00002206 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002207
françois romieu4da19632011-01-03 15:07:55 +00002208 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002209
2210 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002211 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002212 0x0065, 0x0066, 0x0067, 0x0068,
2213 0x0069, 0x006a, 0x006b, 0x006c
2214 };
2215 int i;
2216
françois romieu4da19632011-01-03 15:07:55 +00002217 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002218
2219 val &= 0xff00;
2220 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002221 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002222 }
2223 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002224 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002225 { 0x1f, 0x0002 },
2226 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002227 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002228 { 0x05, 0x8330 },
2229 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002230 };
2231
françois romieu4da19632011-01-03 15:07:55 +00002232 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002233 }
2234
françois romieubca03d52011-01-03 15:07:31 +00002235 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002236 rtl_writephy(tp, 0x1f, 0x0002);
2237 rtl_patchphy(tp, 0x0d, 0x0300);
2238 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002239
françois romieubca03d52011-01-03 15:07:31 +00002240 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002241 rtl_writephy(tp, 0x1f, 0x0002);
2242 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2243 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002244
françois romieu4da19632011-01-03 15:07:55 +00002245 rtl_writephy(tp, 0x1f, 0x0005);
2246 rtl_writephy(tp, 0x05, 0x001b);
françois romieuf1e02ed2011-01-13 13:07:53 +00002247 if ((rtl_readphy(tp, 0x06) != 0xbf00) ||
2248 (rtl_apply_firmware(tp, FIRMWARE_8168D_1) < 0)) {
françois romieubca03d52011-01-03 15:07:31 +00002249 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2250 }
2251
françois romieu4da19632011-01-03 15:07:55 +00002252 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002253}
2254
françois romieubca03d52011-01-03 15:07:31 +00002255static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002256{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002257 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002258 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002259 { 0x1f, 0x0001 },
2260 { 0x06, 0x4064 },
2261 { 0x07, 0x2863 },
2262 { 0x08, 0x059c },
2263 { 0x09, 0x26b4 },
2264 { 0x0a, 0x6a19 },
2265 { 0x0b, 0xdcc8 },
2266 { 0x10, 0xf06d },
2267 { 0x14, 0x7f68 },
2268 { 0x18, 0x7fd9 },
2269 { 0x1c, 0xf0ff },
2270 { 0x1d, 0x3d9c },
2271 { 0x1f, 0x0003 },
2272 { 0x12, 0xf49f },
2273 { 0x13, 0x070b },
2274 { 0x1a, 0x05ad },
2275 { 0x14, 0x94c0 },
2276
françois romieubca03d52011-01-03 15:07:31 +00002277 /*
2278 * Tx Error Issue
2279 * enhance line driver power
2280 */
françois romieudaf9df62009-10-07 12:44:20 +00002281 { 0x1f, 0x0002 },
2282 { 0x06, 0x5561 },
2283 { 0x1f, 0x0005 },
2284 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002285 { 0x06, 0x5561 },
2286
2287 /*
2288 * Can not link to 1Gbps with bad cable
2289 * Decrease SNR threshold form 21.07dB to 19.04dB
2290 */
2291 { 0x1f, 0x0001 },
2292 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002293
2294 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002295 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002296 };
françois romieubca03d52011-01-03 15:07:31 +00002297 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002298
françois romieu4da19632011-01-03 15:07:55 +00002299 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002300
2301 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002302 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002303 { 0x1f, 0x0002 },
2304 { 0x05, 0x669a },
2305 { 0x1f, 0x0005 },
2306 { 0x05, 0x8330 },
2307 { 0x06, 0x669a },
2308
2309 { 0x1f, 0x0002 }
2310 };
2311 int val;
2312
françois romieu4da19632011-01-03 15:07:55 +00002313 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002314
françois romieu4da19632011-01-03 15:07:55 +00002315 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002316 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002317 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002318 0x0065, 0x0066, 0x0067, 0x0068,
2319 0x0069, 0x006a, 0x006b, 0x006c
2320 };
2321 int i;
2322
françois romieu4da19632011-01-03 15:07:55 +00002323 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002324
2325 val &= 0xff00;
2326 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002327 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002328 }
2329 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002330 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002331 { 0x1f, 0x0002 },
2332 { 0x05, 0x2642 },
2333 { 0x1f, 0x0005 },
2334 { 0x05, 0x8330 },
2335 { 0x06, 0x2642 }
2336 };
2337
françois romieu4da19632011-01-03 15:07:55 +00002338 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002339 }
2340
françois romieubca03d52011-01-03 15:07:31 +00002341 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002342 rtl_writephy(tp, 0x1f, 0x0002);
2343 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2344 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002345
françois romieubca03d52011-01-03 15:07:31 +00002346 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002347 rtl_writephy(tp, 0x1f, 0x0002);
2348 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002349
françois romieu4da19632011-01-03 15:07:55 +00002350 rtl_writephy(tp, 0x1f, 0x0005);
2351 rtl_writephy(tp, 0x05, 0x001b);
françois romieuf1e02ed2011-01-13 13:07:53 +00002352 if ((rtl_readphy(tp, 0x06) != 0xb300) ||
2353 (rtl_apply_firmware(tp, FIRMWARE_8168D_2) < 0)) {
françois romieubca03d52011-01-03 15:07:31 +00002354 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2355 }
2356
françois romieu4da19632011-01-03 15:07:55 +00002357 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002358}
2359
françois romieu4da19632011-01-03 15:07:55 +00002360static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002361{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002362 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002363 { 0x1f, 0x0002 },
2364 { 0x10, 0x0008 },
2365 { 0x0d, 0x006c },
2366
2367 { 0x1f, 0x0000 },
2368 { 0x0d, 0xf880 },
2369
2370 { 0x1f, 0x0001 },
2371 { 0x17, 0x0cc0 },
2372
2373 { 0x1f, 0x0001 },
2374 { 0x0b, 0xa4d8 },
2375 { 0x09, 0x281c },
2376 { 0x07, 0x2883 },
2377 { 0x0a, 0x6b35 },
2378 { 0x1d, 0x3da4 },
2379 { 0x1c, 0xeffd },
2380 { 0x14, 0x7f52 },
2381 { 0x18, 0x7fc6 },
2382 { 0x08, 0x0601 },
2383 { 0x06, 0x4063 },
2384 { 0x10, 0xf074 },
2385 { 0x1f, 0x0003 },
2386 { 0x13, 0x0789 },
2387 { 0x12, 0xf4bd },
2388 { 0x1a, 0x04fd },
2389 { 0x14, 0x84b0 },
2390 { 0x1f, 0x0000 },
2391 { 0x00, 0x9200 },
2392
2393 { 0x1f, 0x0005 },
2394 { 0x01, 0x0340 },
2395 { 0x1f, 0x0001 },
2396 { 0x04, 0x4000 },
2397 { 0x03, 0x1d21 },
2398 { 0x02, 0x0c32 },
2399 { 0x01, 0x0200 },
2400 { 0x00, 0x5554 },
2401 { 0x04, 0x4800 },
2402 { 0x04, 0x4000 },
2403 { 0x04, 0xf000 },
2404 { 0x03, 0xdf01 },
2405 { 0x02, 0xdf20 },
2406 { 0x01, 0x101a },
2407 { 0x00, 0xa0ff },
2408 { 0x04, 0xf800 },
2409 { 0x04, 0xf000 },
2410 { 0x1f, 0x0000 },
2411
2412 { 0x1f, 0x0007 },
2413 { 0x1e, 0x0023 },
2414 { 0x16, 0x0000 },
2415 { 0x1f, 0x0000 }
2416 };
2417
françois romieu4da19632011-01-03 15:07:55 +00002418 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002419}
2420
françois romieue6de30d2011-01-03 15:08:37 +00002421static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2422{
2423 static const struct phy_reg phy_reg_init[] = {
2424 { 0x1f, 0x0001 },
2425 { 0x17, 0x0cc0 },
2426
2427 { 0x1f, 0x0007 },
2428 { 0x1e, 0x002d },
2429 { 0x18, 0x0040 },
2430 { 0x1f, 0x0000 }
2431 };
2432
2433 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2434 rtl_patchphy(tp, 0x0d, 1 << 5);
2435}
2436
françois romieu4da19632011-01-03 15:07:55 +00002437static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002438{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002439 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002440 { 0x1f, 0x0003 },
2441 { 0x08, 0x441d },
2442 { 0x01, 0x9100 },
2443 { 0x1f, 0x0000 }
2444 };
2445
françois romieu4da19632011-01-03 15:07:55 +00002446 rtl_writephy(tp, 0x1f, 0x0000);
2447 rtl_patchphy(tp, 0x11, 1 << 12);
2448 rtl_patchphy(tp, 0x19, 1 << 13);
2449 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002450
françois romieu4da19632011-01-03 15:07:55 +00002451 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002452}
2453
Hayes Wang5a5e4442011-02-22 17:26:21 +08002454static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2455{
2456 static const struct phy_reg phy_reg_init[] = {
2457 { 0x1f, 0x0005 },
2458 { 0x1a, 0x0000 },
2459 { 0x1f, 0x0000 },
2460
2461 { 0x1f, 0x0004 },
2462 { 0x1c, 0x0000 },
2463 { 0x1f, 0x0000 },
2464
2465 { 0x1f, 0x0001 },
2466 { 0x15, 0x7701 },
2467 { 0x1f, 0x0000 }
2468 };
2469
2470 /* Disable ALDPS before ram code */
2471 rtl_writephy(tp, 0x1f, 0x0000);
2472 rtl_writephy(tp, 0x18, 0x0310);
2473 msleep(100);
2474
2475 if (rtl_apply_firmware(tp, FIRMWARE_8105E_1) < 0)
2476 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2477
2478 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2479}
2480
Francois Romieu5615d9f2007-08-17 17:50:46 +02002481static void rtl_hw_phy_config(struct net_device *dev)
2482{
2483 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002484
2485 rtl8169_print_mac_version(tp);
2486
2487 switch (tp->mac_version) {
2488 case RTL_GIGA_MAC_VER_01:
2489 break;
2490 case RTL_GIGA_MAC_VER_02:
2491 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002492 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002493 break;
2494 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002495 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002496 break;
françois romieu2e9558562009-08-10 19:44:19 +00002497 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002498 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002499 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002500 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002501 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002502 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002503 case RTL_GIGA_MAC_VER_07:
2504 case RTL_GIGA_MAC_VER_08:
2505 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002506 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002507 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002508 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002509 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002510 break;
2511 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002512 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002513 break;
2514 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002515 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002516 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002517 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002518 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002519 break;
2520 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002521 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002522 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002523 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002524 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002525 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002526 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002527 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002528 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002529 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002530 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002531 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002532 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002533 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002534 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002535 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002536 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002537 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002538 break;
2539 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002540 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002541 break;
2542 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002543 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002544 break;
françois romieue6de30d2011-01-03 15:08:37 +00002545 case RTL_GIGA_MAC_VER_28:
2546 rtl8168d_4_hw_phy_config(tp);
2547 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002548 case RTL_GIGA_MAC_VER_29:
2549 case RTL_GIGA_MAC_VER_30:
2550 rtl8105e_hw_phy_config(tp);
2551 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002552
Francois Romieu5615d9f2007-08-17 17:50:46 +02002553 default:
2554 break;
2555 }
2556}
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558static void rtl8169_phy_timer(unsigned long __opaque)
2559{
2560 struct net_device *dev = (struct net_device *)__opaque;
2561 struct rtl8169_private *tp = netdev_priv(dev);
2562 struct timer_list *timer = &tp->timer;
2563 void __iomem *ioaddr = tp->mmio_addr;
2564 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2565
Francois Romieubcf0bf92006-07-26 23:14:13 +02002566 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Francois Romieu64e4bfb2006-08-17 12:43:06 +02002568 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return;
2570
2571 spin_lock_irq(&tp->lock);
2572
françois romieu4da19632011-01-03 15:07:55 +00002573 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002574 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 * A busy loop could burn quite a few cycles on nowadays CPU.
2576 * Let's delay the execution of the timer for a few ticks.
2577 */
2578 timeout = HZ/10;
2579 goto out_mod_timer;
2580 }
2581
2582 if (tp->link_ok(ioaddr))
2583 goto out_unlock;
2584
Joe Perchesbf82c182010-02-09 11:49:50 +00002585 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
françois romieu4da19632011-01-03 15:07:55 +00002587 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589out_mod_timer:
2590 mod_timer(timer, jiffies + timeout);
2591out_unlock:
2592 spin_unlock_irq(&tp->lock);
2593}
2594
2595static inline void rtl8169_delete_timer(struct net_device *dev)
2596{
2597 struct rtl8169_private *tp = netdev_priv(dev);
2598 struct timer_list *timer = &tp->timer;
2599
Francois Romieue179bb72007-08-17 15:05:21 +02002600 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return;
2602
2603 del_timer_sync(timer);
2604}
2605
2606static inline void rtl8169_request_timer(struct net_device *dev)
2607{
2608 struct rtl8169_private *tp = netdev_priv(dev);
2609 struct timer_list *timer = &tp->timer;
2610
Francois Romieue179bb72007-08-17 15:05:21 +02002611 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return;
2613
Francois Romieu2efa53f2007-03-09 00:00:05 +01002614 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
2617#ifdef CONFIG_NET_POLL_CONTROLLER
2618/*
2619 * Polling 'interrupt' - used by things like netconsole to send skbs
2620 * without having to re-enable interrupts. It's not called while
2621 * the interrupt routine is executing.
2622 */
2623static void rtl8169_netpoll(struct net_device *dev)
2624{
2625 struct rtl8169_private *tp = netdev_priv(dev);
2626 struct pci_dev *pdev = tp->pci_dev;
2627
2628 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002629 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 enable_irq(pdev->irq);
2631}
2632#endif
2633
2634static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2635 void __iomem *ioaddr)
2636{
2637 iounmap(ioaddr);
2638 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002639 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 pci_disable_device(pdev);
2641 free_netdev(dev);
2642}
2643
Francois Romieubf793292006-11-01 00:53:05 +01002644static void rtl8169_phy_reset(struct net_device *dev,
2645 struct rtl8169_private *tp)
2646{
Francois Romieu07d3f512007-02-21 22:40:46 +01002647 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002648
françois romieu4da19632011-01-03 15:07:55 +00002649 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002650 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002651 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002652 return;
2653 msleep(1);
2654 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002655 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002656}
2657
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002658static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002660 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002661
Francois Romieu5615d9f2007-08-17 17:50:46 +02002662 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002663
Marcus Sundberg773328942008-07-10 21:28:08 +02002664 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2665 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2666 RTL_W8(0x82, 0x01);
2667 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002668
Francois Romieu6dccd162007-02-13 23:38:05 +01002669 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2670
2671 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2672 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002673
Francois Romieubcf0bf92006-07-26 23:14:13 +02002674 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002675 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2676 RTL_W8(0x82, 0x01);
2677 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002678 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002679 }
2680
Francois Romieubf793292006-11-01 00:53:05 +01002681 rtl8169_phy_reset(dev, tp);
2682
Oliver Neukum54405cd2011-01-06 21:55:13 +01002683 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
2684 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2685 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2686 tp->mii.supports_gmii ?
2687 ADVERTISED_1000baseT_Half |
2688 ADVERTISED_1000baseT_Full : 0);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002689
Joe Perchesbf82c182010-02-09 11:49:50 +00002690 if (RTL_R8(PHYstatus) & TBI_Enable)
2691 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002692}
2693
Francois Romieu773d2022007-01-31 23:47:43 +01002694static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2695{
2696 void __iomem *ioaddr = tp->mmio_addr;
2697 u32 high;
2698 u32 low;
2699
2700 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2701 high = addr[4] | (addr[5] << 8);
2702
2703 spin_lock_irq(&tp->lock);
2704
2705 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002706
Francois Romieu773d2022007-01-31 23:47:43 +01002707 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002708 RTL_R32(MAC4);
2709
Francois Romieu78f1cd02010-03-27 19:35:46 -07002710 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002711 RTL_R32(MAC0);
2712
Francois Romieu773d2022007-01-31 23:47:43 +01002713 RTL_W8(Cfg9346, Cfg9346_Lock);
2714
2715 spin_unlock_irq(&tp->lock);
2716}
2717
2718static int rtl_set_mac_address(struct net_device *dev, void *p)
2719{
2720 struct rtl8169_private *tp = netdev_priv(dev);
2721 struct sockaddr *addr = p;
2722
2723 if (!is_valid_ether_addr(addr->sa_data))
2724 return -EADDRNOTAVAIL;
2725
2726 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2727
2728 rtl_rar_set(tp, dev->dev_addr);
2729
2730 return 0;
2731}
2732
Francois Romieu5f787a12006-08-17 13:02:36 +02002733static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2734{
2735 struct rtl8169_private *tp = netdev_priv(dev);
2736 struct mii_ioctl_data *data = if_mii(ifr);
2737
Francois Romieu8b4ab282008-11-19 22:05:25 -08002738 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2739}
Francois Romieu5f787a12006-08-17 13:02:36 +02002740
Francois Romieu8b4ab282008-11-19 22:05:25 -08002741static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2742{
Francois Romieu5f787a12006-08-17 13:02:36 +02002743 switch (cmd) {
2744 case SIOCGMIIPHY:
2745 data->phy_id = 32; /* Internal PHY */
2746 return 0;
2747
2748 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002749 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002750 return 0;
2751
2752 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002753 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002754 return 0;
2755 }
2756 return -EOPNOTSUPP;
2757}
2758
Francois Romieu8b4ab282008-11-19 22:05:25 -08002759static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2760{
2761 return -EOPNOTSUPP;
2762}
2763
Francois Romieu0e485152007-02-20 00:00:26 +01002764static const struct rtl_cfg_info {
2765 void (*hw_start)(struct net_device *);
2766 unsigned int region;
2767 unsigned int align;
2768 u16 intr_event;
2769 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002770 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002771 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002772} rtl_cfg_infos [] = {
2773 [RTL_CFG_0] = {
2774 .hw_start = rtl_hw_start_8169,
2775 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002776 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002777 .intr_event = SYSErr | LinkChg | RxOverflow |
2778 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002779 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002780 .features = RTL_FEATURE_GMII,
2781 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002782 },
2783 [RTL_CFG_1] = {
2784 .hw_start = rtl_hw_start_8168,
2785 .region = 2,
2786 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002787 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002788 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002789 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002790 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2791 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002792 },
2793 [RTL_CFG_2] = {
2794 .hw_start = rtl_hw_start_8101,
2795 .region = 2,
2796 .align = 8,
2797 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2798 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002799 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002800 .features = RTL_FEATURE_MSI,
2801 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002802 }
2803};
2804
Francois Romieufbac58f2007-10-04 22:51:38 +02002805/* Cfg9346_Unlock assumed. */
2806static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2807 const struct rtl_cfg_info *cfg)
2808{
2809 unsigned msi = 0;
2810 u8 cfg2;
2811
2812 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002813 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002814 if (pci_enable_msi(pdev)) {
2815 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2816 } else {
2817 cfg2 |= MSIEnable;
2818 msi = RTL_FEATURE_MSI;
2819 }
2820 }
2821 RTL_W8(Config2, cfg2);
2822 return msi;
2823}
2824
2825static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2826{
2827 if (tp->features & RTL_FEATURE_MSI) {
2828 pci_disable_msi(pdev);
2829 tp->features &= ~RTL_FEATURE_MSI;
2830 }
2831}
2832
Francois Romieu8b4ab282008-11-19 22:05:25 -08002833static const struct net_device_ops rtl8169_netdev_ops = {
2834 .ndo_open = rtl8169_open,
2835 .ndo_stop = rtl8169_close,
2836 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002837 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002838 .ndo_tx_timeout = rtl8169_tx_timeout,
2839 .ndo_validate_addr = eth_validate_addr,
2840 .ndo_change_mtu = rtl8169_change_mtu,
2841 .ndo_set_mac_address = rtl_set_mac_address,
2842 .ndo_do_ioctl = rtl8169_ioctl,
2843 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002844#ifdef CONFIG_NET_POLL_CONTROLLER
2845 .ndo_poll_controller = rtl8169_netpoll,
2846#endif
2847
2848};
2849
françois romieuc0e45c12011-01-03 15:08:04 +00002850static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2851{
2852 struct mdio_ops *ops = &tp->mdio_ops;
2853
2854 switch (tp->mac_version) {
2855 case RTL_GIGA_MAC_VER_27:
2856 ops->write = r8168dp_1_mdio_write;
2857 ops->read = r8168dp_1_mdio_read;
2858 break;
françois romieue6de30d2011-01-03 15:08:37 +00002859 case RTL_GIGA_MAC_VER_28:
2860 ops->write = r8168dp_2_mdio_write;
2861 ops->read = r8168dp_2_mdio_read;
2862 break;
françois romieuc0e45c12011-01-03 15:08:04 +00002863 default:
2864 ops->write = r8169_mdio_write;
2865 ops->read = r8169_mdio_read;
2866 break;
2867 }
2868}
2869
françois romieu065c27c2011-01-03 15:08:12 +00002870static void r810x_phy_power_down(struct rtl8169_private *tp)
2871{
2872 rtl_writephy(tp, 0x1f, 0x0000);
2873 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2874}
2875
2876static void r810x_phy_power_up(struct rtl8169_private *tp)
2877{
2878 rtl_writephy(tp, 0x1f, 0x0000);
2879 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2880}
2881
2882static void r810x_pll_power_down(struct rtl8169_private *tp)
2883{
2884 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2885 rtl_writephy(tp, 0x1f, 0x0000);
2886 rtl_writephy(tp, MII_BMCR, 0x0000);
2887 return;
2888 }
2889
2890 r810x_phy_power_down(tp);
2891}
2892
2893static void r810x_pll_power_up(struct rtl8169_private *tp)
2894{
2895 r810x_phy_power_up(tp);
2896}
2897
2898static void r8168_phy_power_up(struct rtl8169_private *tp)
2899{
2900 rtl_writephy(tp, 0x1f, 0x0000);
2901 rtl_writephy(tp, 0x0e, 0x0000);
2902 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2903}
2904
2905static void r8168_phy_power_down(struct rtl8169_private *tp)
2906{
2907 rtl_writephy(tp, 0x1f, 0x0000);
2908 rtl_writephy(tp, 0x0e, 0x0200);
2909 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2910}
2911
2912static void r8168_pll_power_down(struct rtl8169_private *tp)
2913{
2914 void __iomem *ioaddr = tp->mmio_addr;
2915
2916 if (tp->mac_version == RTL_GIGA_MAC_VER_27)
2917 return;
2918
2919 if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
2920 (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
2921 (RTL_R16(CPlusCmd) & ASF)) {
2922 return;
2923 }
2924
2925 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2926 rtl_writephy(tp, 0x1f, 0x0000);
2927 rtl_writephy(tp, MII_BMCR, 0x0000);
2928
2929 RTL_W32(RxConfig, RTL_R32(RxConfig) |
2930 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
2931 return;
2932 }
2933
2934 r8168_phy_power_down(tp);
2935
2936 switch (tp->mac_version) {
2937 case RTL_GIGA_MAC_VER_25:
2938 case RTL_GIGA_MAC_VER_26:
2939 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
2940 break;
2941 }
2942}
2943
2944static void r8168_pll_power_up(struct rtl8169_private *tp)
2945{
2946 void __iomem *ioaddr = tp->mmio_addr;
2947
2948 if (tp->mac_version == RTL_GIGA_MAC_VER_27)
2949 return;
2950
2951 switch (tp->mac_version) {
2952 case RTL_GIGA_MAC_VER_25:
2953 case RTL_GIGA_MAC_VER_26:
2954 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
2955 break;
2956 }
2957
2958 r8168_phy_power_up(tp);
2959}
2960
2961static void rtl_pll_power_op(struct rtl8169_private *tp,
2962 void (*op)(struct rtl8169_private *))
2963{
2964 if (op)
2965 op(tp);
2966}
2967
2968static void rtl_pll_power_down(struct rtl8169_private *tp)
2969{
2970 rtl_pll_power_op(tp, tp->pll_power_ops.down);
2971}
2972
2973static void rtl_pll_power_up(struct rtl8169_private *tp)
2974{
2975 rtl_pll_power_op(tp, tp->pll_power_ops.up);
2976}
2977
2978static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
2979{
2980 struct pll_power_ops *ops = &tp->pll_power_ops;
2981
2982 switch (tp->mac_version) {
2983 case RTL_GIGA_MAC_VER_07:
2984 case RTL_GIGA_MAC_VER_08:
2985 case RTL_GIGA_MAC_VER_09:
2986 case RTL_GIGA_MAC_VER_10:
2987 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08002988 case RTL_GIGA_MAC_VER_29:
2989 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00002990 ops->down = r810x_pll_power_down;
2991 ops->up = r810x_pll_power_up;
2992 break;
2993
2994 case RTL_GIGA_MAC_VER_11:
2995 case RTL_GIGA_MAC_VER_12:
2996 case RTL_GIGA_MAC_VER_17:
2997 case RTL_GIGA_MAC_VER_18:
2998 case RTL_GIGA_MAC_VER_19:
2999 case RTL_GIGA_MAC_VER_20:
3000 case RTL_GIGA_MAC_VER_21:
3001 case RTL_GIGA_MAC_VER_22:
3002 case RTL_GIGA_MAC_VER_23:
3003 case RTL_GIGA_MAC_VER_24:
3004 case RTL_GIGA_MAC_VER_25:
3005 case RTL_GIGA_MAC_VER_26:
3006 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003007 case RTL_GIGA_MAC_VER_28:
françois romieu065c27c2011-01-03 15:08:12 +00003008 ops->down = r8168_pll_power_down;
3009 ops->up = r8168_pll_power_up;
3010 break;
3011
3012 default:
3013 ops->down = NULL;
3014 ops->up = NULL;
3015 break;
3016 }
3017}
3018
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003019static int __devinit
3020rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3021{
Francois Romieu0e485152007-02-20 00:00:26 +01003022 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3023 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003025 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003026 struct net_device *dev;
3027 void __iomem *ioaddr;
Francois Romieu07d3f512007-02-21 22:40:46 +01003028 unsigned int i;
3029 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003031 if (netif_msg_drv(&debug)) {
3032 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3033 MODULENAME, RTL8169_VERSION);
3034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003037 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003038 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003039 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003040 rc = -ENOMEM;
3041 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003045 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003047 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003048 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003049 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Francois Romieuccdffb92008-07-26 14:26:06 +02003051 mii = &tp->mii;
3052 mii->dev = dev;
3053 mii->mdio_read = rtl_mdio_read;
3054 mii->mdio_write = rtl_mdio_write;
3055 mii->phy_id_mask = 0x1f;
3056 mii->reg_num_mask = 0x1f;
3057 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3060 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003061 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003062 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003063 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
3065
françois romieu87aeec72010-04-26 11:42:06 +00003066 if (pci_set_mwi(pdev) < 0)
3067 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003070 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003071 netif_err(tp, probe, dev,
3072 "region #%d not an MMIO resource, aborting\n",
3073 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003075 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003079 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003080 netif_err(tp, probe, dev,
3081 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003083 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
3085
3086 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003087 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003088 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003089 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
3091
3092 tp->cp_cmd = PCIMulRW | RxChkSum;
3093
3094 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003095 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 tp->cp_cmd |= PCIDAC;
3097 dev->features |= NETIF_F_HIGHDMA;
3098 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003099 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003101 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003102 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104 }
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003107 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003108 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003109 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003111 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
3113
David S. Miller4300e8c2010-03-26 10:23:30 -07003114 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3115 if (!tp->pcie_cap)
3116 netif_info(tp, probe, dev, "no PCI Express capability\n");
3117
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003118 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120 /* Soft reset the chip. */
3121 RTL_W8(ChipCmd, CmdReset);
3122
3123 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003124 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3126 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003127 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
3129
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003130 RTL_W16(IntrStatus, 0xffff);
3131
françois romieuca52efd2009-07-24 12:34:19 +00003132 pci_set_master(pdev);
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 /* Identify chip attached to board */
3135 rtl8169_get_mac_version(tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Francois Romieu7a8fc772011-03-01 17:18:33 +01003137 /*
3138 * Pretend we are using VLANs; This bypasses a nasty bug where
3139 * Interrupts stop flowing on high load on 8110SCd controllers.
3140 */
3141 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3142 tp->cp_cmd |= RxVlan;
3143
françois romieuc0e45c12011-01-03 15:08:04 +00003144 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003145 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003146
Jean Delvaref21b75e2009-05-26 20:54:48 -07003147 /* Use appropriate default if unknown */
3148 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003149 netif_notice(tp, probe, dev,
3150 "unknown MAC, using family default\n");
Jean Delvaref21b75e2009-05-26 20:54:48 -07003151 tp->mac_version = cfg->default_ver;
3152 }
3153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Roel Kluincee60c32008-04-17 22:35:54 +02003156 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if (tp->mac_version == rtl_chip_info[i].mac_version)
3158 break;
3159 }
Roel Kluincee60c32008-04-17 22:35:54 +02003160 if (i == ARRAY_SIZE(rtl_chip_info)) {
Jean Delvaref21b75e2009-05-26 20:54:48 -07003161 dev_err(&pdev->dev,
3162 "driver bug, MAC version not found in rtl_chip_info\n");
françois romieu87aeec72010-04-26 11:42:06 +00003163 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 }
3165 tp->chipset = i;
3166
Francois Romieu5d06a992006-02-23 00:47:58 +01003167 RTL_W8(Cfg9346, Cfg9346_Unlock);
3168 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3169 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003170 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3171 tp->features |= RTL_FEATURE_WOL;
3172 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3173 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003174 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003175 RTL_W8(Cfg9346, Cfg9346_Lock);
3176
Francois Romieu66ec5d42007-11-06 22:56:10 +01003177 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3178 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 tp->set_speed = rtl8169_set_speed_tbi;
3180 tp->get_settings = rtl8169_gset_tbi;
3181 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3182 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3183 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003184 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Francois Romieu64e4bfb2006-08-17 12:43:06 +02003186 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 } else {
3188 tp->set_speed = rtl8169_set_speed_xmii;
3189 tp->get_settings = rtl8169_gset_xmii;
3190 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3191 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3192 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003193 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
3195
Francois Romieudf58ef52008-10-09 14:35:58 -07003196 spin_lock_init(&tp->lock);
3197
Petr Vandrovec738e1e62008-10-12 20:58:29 -07003198 tp->mmio_addr = ioaddr;
3199
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003200 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 for (i = 0; i < MAC_ADDR_LEN; i++)
3202 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003203 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3207 dev->irq = pdev->irq;
3208 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003210 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Francois Romieu7a8fc772011-03-01 17:18:33 +01003212 dev->features |= NETIF_F_HW_VLAN_TX_RX | NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003215 tp->hw_start = cfg->hw_start;
3216 tp->intr_event = cfg->intr_event;
3217 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Francois Romieu2efa53f2007-03-09 00:00:05 +01003219 init_timer(&tp->timer);
3220 tp->timer.data = (unsigned long) dev;
3221 tp->timer.function = rtl8169_phy_timer;
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003224 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003225 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
3227 pci_set_drvdata(pdev, dev);
3228
Joe Perchesbf82c182010-02-09 11:49:50 +00003229 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3230 rtl_chip_info[tp->chipset].name,
3231 dev->base_addr, dev->dev_addr,
3232 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
françois romieue6de30d2011-01-03 15:08:37 +00003234 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3235 (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
françois romieub646d902011-01-03 15:08:21 +00003236 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003237 }
françois romieub646d902011-01-03 15:08:21 +00003238
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003239 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Alan Sternf3ec4f82010-06-08 15:23:51 -04003241 if (pci_dev_run_wake(pdev))
3242 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003243
Ivan Vecera0d672e92011-02-15 02:08:39 +00003244 netif_carrier_off(dev);
3245
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003246out:
3247 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
françois romieu87aeec72010-04-26 11:42:06 +00003249err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003250 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003251 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003252err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003253 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003254err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003255 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003256 pci_disable_device(pdev);
3257err_out_free_dev_1:
3258 free_netdev(dev);
3259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
Francois Romieu07d3f512007-02-21 22:40:46 +01003262static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
3264 struct net_device *dev = pci_get_drvdata(pdev);
3265 struct rtl8169_private *tp = netdev_priv(dev);
3266
françois romieue6de30d2011-01-03 15:08:37 +00003267 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3268 (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
françois romieub646d902011-01-03 15:08:21 +00003269 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003270 }
françois romieub646d902011-01-03 15:08:21 +00003271
Tejun Heo23f333a2010-12-12 16:45:14 +01003272 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003273
françois romieuf1e02ed2011-01-13 13:07:53 +00003274 rtl_release_firmware(tp);
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003277
Alan Sternf3ec4f82010-06-08 15:23:51 -04003278 if (pci_dev_run_wake(pdev))
3279 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003280
Ivan Veceracc098dc2009-11-29 23:12:52 -08003281 /* restore original MAC address */
3282 rtl_rar_set(tp, dev->perm_addr);
3283
Francois Romieufbac58f2007-10-04 22:51:38 +02003284 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3286 pci_set_drvdata(pdev, NULL);
3287}
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289static int rtl8169_open(struct net_device *dev)
3290{
3291 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003292 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003294 int retval = -ENOMEM;
3295
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003296 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Neil Hormanc0cd8842010-03-29 13:16:02 -07003298 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003300 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003302 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3303 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003305 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003307 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3308 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003310 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
3312 retval = rtl8169_init_ring(dev);
3313 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003314 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
David Howellsc4028952006-11-22 14:57:56 +00003316 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Francois Romieu99f252b2007-04-02 22:59:59 +02003318 smp_mb();
3319
Francois Romieufbac58f2007-10-04 22:51:38 +02003320 retval = request_irq(dev->irq, rtl8169_interrupt,
3321 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003322 dev->name, dev);
3323 if (retval < 0)
3324 goto err_release_ring_2;
3325
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003326 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003327
françois romieueee3a962011-01-08 02:17:26 +00003328 rtl8169_init_phy(dev, tp);
3329
Francois Romieu7a8fc772011-03-01 17:18:33 +01003330 rtl8169_vlan_mode(dev);
françois romieueee3a962011-01-08 02:17:26 +00003331
françois romieu065c27c2011-01-03 15:08:12 +00003332 rtl_pll_power_up(tp);
3333
Francois Romieu07ce4062007-02-23 23:36:39 +01003334 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 rtl8169_request_timer(dev);
3337
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003338 tp->saved_wolopts = 0;
3339 pm_runtime_put_noidle(&pdev->dev);
3340
françois romieueee3a962011-01-08 02:17:26 +00003341 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342out:
3343 return retval;
3344
Francois Romieu99f252b2007-04-02 22:59:59 +02003345err_release_ring_2:
3346 rtl8169_rx_clear(tp);
3347err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003348 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3349 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003350 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003351err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003352 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3353 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003354 tp->TxDescArray = NULL;
3355err_pm_runtime_put:
3356 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 goto out;
3358}
3359
françois romieue6de30d2011-01-03 15:08:37 +00003360static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
françois romieue6de30d2011-01-03 15:08:37 +00003362 void __iomem *ioaddr = tp->mmio_addr;
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 /* Disable interrupts */
3365 rtl8169_irq_mask_and_ack(ioaddr);
3366
françois romieue6de30d2011-01-03 15:08:37 +00003367 if (tp->mac_version == RTL_GIGA_MAC_VER_28) {
3368 while (RTL_R8(TxPoll) & NPQ)
3369 udelay(20);
3370
3371 }
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 /* Reset the chipset */
3374 RTL_W8(ChipCmd, CmdReset);
3375
3376 /* PCI commit */
3377 RTL_R8(ChipCmd);
3378}
3379
Francois Romieu7f796d82007-06-11 23:04:41 +02003380static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003381{
3382 void __iomem *ioaddr = tp->mmio_addr;
3383 u32 cfg = rtl8169_rx_config;
3384
3385 cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
3386 RTL_W32(RxConfig, cfg);
3387
3388 /* Set DMA burst size and Interframe Gap Time */
3389 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3390 (InterFrameGap << TxInterFrameGapShift));
3391}
3392
Francois Romieu07ce4062007-02-23 23:36:39 +01003393static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394{
3395 struct rtl8169_private *tp = netdev_priv(dev);
3396 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01003397 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
3399 /* Soft reset the chip. */
3400 RTL_W8(ChipCmd, CmdReset);
3401
3402 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003403 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3405 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003406 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
3408
Francois Romieu07ce4062007-02-23 23:36:39 +01003409 tp->hw_start(dev);
3410
Francois Romieu07ce4062007-02-23 23:36:39 +01003411 netif_start_queue(dev);
3412}
3413
3414
Francois Romieu7f796d82007-06-11 23:04:41 +02003415static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3416 void __iomem *ioaddr)
3417{
3418 /*
3419 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3420 * register to be written before TxDescAddrLow to work.
3421 * Switching from MMIO to I/O access fixes the issue as well.
3422 */
3423 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003424 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003425 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003426 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003427}
3428
3429static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3430{
3431 u16 cmd;
3432
3433 cmd = RTL_R16(CPlusCmd);
3434 RTL_W16(CPlusCmd, cmd);
3435 return cmd;
3436}
3437
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003438static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003439{
3440 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003441 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003442}
3443
Francois Romieu6dccd162007-02-13 23:38:05 +01003444static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3445{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003446 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003447 u32 mac_version;
3448 u32 clk;
3449 u32 val;
3450 } cfg2_info [] = {
3451 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3452 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3453 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3454 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3455 }, *p = cfg2_info;
3456 unsigned int i;
3457 u32 clk;
3458
3459 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003460 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003461 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3462 RTL_W32(0x7c, p->val);
3463 break;
3464 }
3465 }
3466}
3467
Francois Romieu07ce4062007-02-23 23:36:39 +01003468static void rtl_hw_start_8169(struct net_device *dev)
3469{
3470 struct rtl8169_private *tp = netdev_priv(dev);
3471 void __iomem *ioaddr = tp->mmio_addr;
3472 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003473
Francois Romieu9cb427b2006-11-02 00:10:16 +01003474 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3475 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3476 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3477 }
3478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003480 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3481 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3482 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3483 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3484 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3485
françois romieuf0298f82011-01-03 15:07:42 +00003486 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003488 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Francois Romieuc946b302007-10-04 00:42:50 +02003490 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3491 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3492 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3493 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3494 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Francois Romieu7f796d82007-06-11 23:04:41 +02003496 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003497
3498 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3499 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
Joe Perches06fa7352007-10-18 21:15:00 +02003500 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003502 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 }
3504
Francois Romieubcf0bf92006-07-26 23:14:13 +02003505 RTL_W16(CPlusCmd, tp->cp_cmd);
3506
Francois Romieu6dccd162007-02-13 23:38:05 +01003507 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 /*
3510 * Undocumented corner. Supposedly:
3511 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3512 */
3513 RTL_W16(IntrMitigate, 0x0000);
3514
Francois Romieu7f796d82007-06-11 23:04:41 +02003515 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003516
Francois Romieuc946b302007-10-04 00:42:50 +02003517 if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
3518 (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
3519 (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
3520 (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
3521 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3522 rtl_set_rx_tx_config_registers(tp);
3523 }
3524
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003526
3527 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3528 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
3530 RTL_W32(RxMissed, 0);
3531
Francois Romieu07ce4062007-02-23 23:36:39 +01003532 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534 /* no early-rx interrupts */
3535 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003536
3537 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003538 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003539}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Francois Romieu9c14cea2008-07-05 00:21:15 +02003541static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003542{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003543 struct net_device *dev = pci_get_drvdata(pdev);
3544 struct rtl8169_private *tp = netdev_priv(dev);
3545 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003546
Francois Romieu9c14cea2008-07-05 00:21:15 +02003547 if (cap) {
3548 u16 ctl;
3549
3550 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3551 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3552 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3553 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003554}
3555
françois romieu650e8d52011-01-03 15:08:29 +00003556static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003557{
3558 u32 csi;
3559
3560 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003561 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3562}
3563
françois romieue6de30d2011-01-03 15:08:37 +00003564static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3565{
3566 rtl_csi_access_enable(ioaddr, 0x17000000);
3567}
3568
françois romieu650e8d52011-01-03 15:08:29 +00003569static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3570{
3571 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003572}
3573
3574struct ephy_info {
3575 unsigned int offset;
3576 u16 mask;
3577 u16 bits;
3578};
3579
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003580static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003581{
3582 u16 w;
3583
3584 while (len-- > 0) {
3585 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3586 rtl_ephy_write(ioaddr, e->offset, w);
3587 e++;
3588 }
3589}
3590
Francois Romieub726e492008-06-28 12:22:59 +02003591static void rtl_disable_clock_request(struct pci_dev *pdev)
3592{
3593 struct net_device *dev = pci_get_drvdata(pdev);
3594 struct rtl8169_private *tp = netdev_priv(dev);
3595 int cap = tp->pcie_cap;
3596
3597 if (cap) {
3598 u16 ctl;
3599
3600 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3601 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3602 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3603 }
3604}
3605
françois romieue6de30d2011-01-03 15:08:37 +00003606static void rtl_enable_clock_request(struct pci_dev *pdev)
3607{
3608 struct net_device *dev = pci_get_drvdata(pdev);
3609 struct rtl8169_private *tp = netdev_priv(dev);
3610 int cap = tp->pcie_cap;
3611
3612 if (cap) {
3613 u16 ctl;
3614
3615 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3616 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3617 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3618 }
3619}
3620
Francois Romieub726e492008-06-28 12:22:59 +02003621#define R8168_CPCMD_QUIRK_MASK (\
3622 EnableBist | \
3623 Mac_dbgo_oe | \
3624 Force_half_dup | \
3625 Force_rxflow_en | \
3626 Force_txflow_en | \
3627 Cxpl_dbg_sel | \
3628 ASF | \
3629 PktCntrDisable | \
3630 Mac_dbgo_sel)
3631
Francois Romieu219a1e92008-06-28 11:58:39 +02003632static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3633{
Francois Romieub726e492008-06-28 12:22:59 +02003634 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3635
3636 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3637
Francois Romieu2e68ae42008-06-28 12:00:55 +02003638 rtl_tx_performance_tweak(pdev,
3639 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003640}
3641
3642static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3643{
3644 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003645
françois romieuf0298f82011-01-03 15:07:42 +00003646 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003647
3648 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003649}
3650
3651static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3652{
Francois Romieub726e492008-06-28 12:22:59 +02003653 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3654
3655 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3656
Francois Romieu219a1e92008-06-28 11:58:39 +02003657 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003658
3659 rtl_disable_clock_request(pdev);
3660
3661 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003662}
3663
Francois Romieuef3386f2008-06-29 12:24:30 +02003664static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003665{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003666 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003667 { 0x01, 0, 0x0001 },
3668 { 0x02, 0x0800, 0x1000 },
3669 { 0x03, 0, 0x0042 },
3670 { 0x06, 0x0080, 0x0000 },
3671 { 0x07, 0, 0x2000 }
3672 };
3673
françois romieu650e8d52011-01-03 15:08:29 +00003674 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003675
3676 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3677
Francois Romieu219a1e92008-06-28 11:58:39 +02003678 __rtl_hw_start_8168cp(ioaddr, pdev);
3679}
3680
Francois Romieuef3386f2008-06-29 12:24:30 +02003681static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3682{
françois romieu650e8d52011-01-03 15:08:29 +00003683 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003684
3685 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3686
3687 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3688
3689 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3690}
3691
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003692static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3693{
françois romieu650e8d52011-01-03 15:08:29 +00003694 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003695
3696 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3697
3698 /* Magic. */
3699 RTL_W8(DBG_REG, 0x20);
3700
françois romieuf0298f82011-01-03 15:07:42 +00003701 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003702
3703 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3704
3705 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3706}
3707
Francois Romieu219a1e92008-06-28 11:58:39 +02003708static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3709{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003710 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003711 { 0x02, 0x0800, 0x1000 },
3712 { 0x03, 0, 0x0002 },
3713 { 0x06, 0x0080, 0x0000 }
3714 };
3715
françois romieu650e8d52011-01-03 15:08:29 +00003716 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003717
3718 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3719
3720 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3721
Francois Romieu219a1e92008-06-28 11:58:39 +02003722 __rtl_hw_start_8168cp(ioaddr, pdev);
3723}
3724
3725static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3726{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003727 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003728 { 0x01, 0, 0x0001 },
3729 { 0x03, 0x0400, 0x0220 }
3730 };
3731
françois romieu650e8d52011-01-03 15:08:29 +00003732 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003733
3734 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3735
Francois Romieu219a1e92008-06-28 11:58:39 +02003736 __rtl_hw_start_8168cp(ioaddr, pdev);
3737}
3738
Francois Romieu197ff762008-06-28 13:16:02 +02003739static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3740{
3741 rtl_hw_start_8168c_2(ioaddr, pdev);
3742}
3743
Francois Romieu6fb07052008-06-29 11:54:28 +02003744static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3745{
françois romieu650e8d52011-01-03 15:08:29 +00003746 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02003747
3748 __rtl_hw_start_8168cp(ioaddr, pdev);
3749}
3750
Francois Romieu5b538df2008-07-20 16:22:45 +02003751static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3752{
françois romieu650e8d52011-01-03 15:08:29 +00003753 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02003754
3755 rtl_disable_clock_request(pdev);
3756
françois romieuf0298f82011-01-03 15:07:42 +00003757 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02003758
3759 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3760
3761 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3762}
3763
françois romieue6de30d2011-01-03 15:08:37 +00003764static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
3765{
3766 static const struct ephy_info e_info_8168d_4[] = {
3767 { 0x0b, ~0, 0x48 },
3768 { 0x19, 0x20, 0x50 },
3769 { 0x0c, ~0, 0x20 }
3770 };
3771 int i;
3772
3773 rtl_csi_access_enable_1(ioaddr);
3774
3775 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3776
3777 RTL_W8(MaxTxPacketSize, TxPacketMax);
3778
3779 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
3780 const struct ephy_info *e = e_info_8168d_4 + i;
3781 u16 w;
3782
3783 w = rtl_ephy_read(ioaddr, e->offset);
3784 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
3785 }
3786
3787 rtl_enable_clock_request(pdev);
3788}
3789
Francois Romieu07ce4062007-02-23 23:36:39 +01003790static void rtl_hw_start_8168(struct net_device *dev)
3791{
Francois Romieu2dd99532007-06-11 23:22:52 +02003792 struct rtl8169_private *tp = netdev_priv(dev);
3793 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01003794 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02003795
3796 RTL_W8(Cfg9346, Cfg9346_Unlock);
3797
françois romieuf0298f82011-01-03 15:07:42 +00003798 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02003799
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003800 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02003801
Francois Romieu0e485152007-02-20 00:00:26 +01003802 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02003803
3804 RTL_W16(CPlusCmd, tp->cp_cmd);
3805
Francois Romieu0e485152007-02-20 00:00:26 +01003806 RTL_W16(IntrMitigate, 0x5151);
3807
3808 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01003809 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
3810 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01003811 tp->intr_event |= RxFIFOOver | PCSTimeout;
3812 tp->intr_event &= ~RxOverflow;
3813 }
Francois Romieu2dd99532007-06-11 23:22:52 +02003814
3815 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3816
Francois Romieub8363902008-06-01 12:31:57 +02003817 rtl_set_rx_mode(dev);
3818
3819 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3820 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02003821
3822 RTL_R8(IntrMask);
3823
Francois Romieu219a1e92008-06-28 11:58:39 +02003824 switch (tp->mac_version) {
3825 case RTL_GIGA_MAC_VER_11:
3826 rtl_hw_start_8168bb(ioaddr, pdev);
3827 break;
3828
3829 case RTL_GIGA_MAC_VER_12:
3830 case RTL_GIGA_MAC_VER_17:
3831 rtl_hw_start_8168bef(ioaddr, pdev);
3832 break;
3833
3834 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02003835 rtl_hw_start_8168cp_1(ioaddr, pdev);
Francois Romieu219a1e92008-06-28 11:58:39 +02003836 break;
3837
3838 case RTL_GIGA_MAC_VER_19:
3839 rtl_hw_start_8168c_1(ioaddr, pdev);
3840 break;
3841
3842 case RTL_GIGA_MAC_VER_20:
3843 rtl_hw_start_8168c_2(ioaddr, pdev);
3844 break;
3845
Francois Romieu197ff762008-06-28 13:16:02 +02003846 case RTL_GIGA_MAC_VER_21:
3847 rtl_hw_start_8168c_3(ioaddr, pdev);
3848 break;
3849
Francois Romieu6fb07052008-06-29 11:54:28 +02003850 case RTL_GIGA_MAC_VER_22:
3851 rtl_hw_start_8168c_4(ioaddr, pdev);
3852 break;
3853
Francois Romieuef3386f2008-06-29 12:24:30 +02003854 case RTL_GIGA_MAC_VER_23:
3855 rtl_hw_start_8168cp_2(ioaddr, pdev);
3856 break;
3857
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003858 case RTL_GIGA_MAC_VER_24:
3859 rtl_hw_start_8168cp_3(ioaddr, pdev);
3860 break;
3861
Francois Romieu5b538df2008-07-20 16:22:45 +02003862 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00003863 case RTL_GIGA_MAC_VER_26:
3864 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02003865 rtl_hw_start_8168d(ioaddr, pdev);
3866 break;
3867
françois romieue6de30d2011-01-03 15:08:37 +00003868 case RTL_GIGA_MAC_VER_28:
3869 rtl_hw_start_8168d_4(ioaddr, pdev);
3870 break;
3871
Francois Romieu219a1e92008-06-28 11:58:39 +02003872 default:
3873 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
3874 dev->name, tp->mac_version);
3875 break;
3876 }
Francois Romieu2dd99532007-06-11 23:22:52 +02003877
Francois Romieu0e485152007-02-20 00:00:26 +01003878 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3879
Francois Romieub8363902008-06-01 12:31:57 +02003880 RTL_W8(Cfg9346, Cfg9346_Lock);
3881
Francois Romieu2dd99532007-06-11 23:22:52 +02003882 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003883
Francois Romieu0e485152007-02-20 00:00:26 +01003884 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003885}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Francois Romieu2857ffb2008-08-02 21:08:49 +02003887#define R810X_CPCMD_QUIRK_MASK (\
3888 EnableBist | \
3889 Mac_dbgo_oe | \
3890 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00003891 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02003892 Force_txflow_en | \
3893 Cxpl_dbg_sel | \
3894 ASF | \
3895 PktCntrDisable | \
3896 PCIDAC | \
3897 PCIMulRW)
3898
3899static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
3900{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003901 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003902 { 0x01, 0, 0x6e65 },
3903 { 0x02, 0, 0x091f },
3904 { 0x03, 0, 0xc2f9 },
3905 { 0x06, 0, 0xafb5 },
3906 { 0x07, 0, 0x0e00 },
3907 { 0x19, 0, 0xec80 },
3908 { 0x01, 0, 0x2e65 },
3909 { 0x01, 0, 0x6e65 }
3910 };
3911 u8 cfg1;
3912
françois romieu650e8d52011-01-03 15:08:29 +00003913 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003914
3915 RTL_W8(DBG_REG, FIX_NAK_1);
3916
3917 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3918
3919 RTL_W8(Config1,
3920 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
3921 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3922
3923 cfg1 = RTL_R8(Config1);
3924 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
3925 RTL_W8(Config1, cfg1 & ~LEDS0);
3926
3927 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
3928
3929 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
3930}
3931
3932static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
3933{
françois romieu650e8d52011-01-03 15:08:29 +00003934 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003935
3936 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3937
3938 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
3939 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3940
3941 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
3942}
3943
3944static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
3945{
3946 rtl_hw_start_8102e_2(ioaddr, pdev);
3947
3948 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
3949}
3950
Hayes Wang5a5e4442011-02-22 17:26:21 +08003951static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
3952{
3953 static const struct ephy_info e_info_8105e_1[] = {
3954 { 0x07, 0, 0x4000 },
3955 { 0x19, 0, 0x0200 },
3956 { 0x19, 0, 0x0020 },
3957 { 0x1e, 0, 0x2000 },
3958 { 0x03, 0, 0x0001 },
3959 { 0x19, 0, 0x0100 },
3960 { 0x19, 0, 0x0004 },
3961 { 0x0a, 0, 0x0020 }
3962 };
3963
3964 /* Force LAN exit from ASPM if Rx/Tx are not idel */
3965 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
3966
3967 /* disable Early Tally Counter */
3968 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
3969
3970 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
3971 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
3972
3973 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
3974}
3975
3976static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
3977{
3978 rtl_hw_start_8105e_1(ioaddr, pdev);
3979 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
3980}
3981
Francois Romieu07ce4062007-02-23 23:36:39 +01003982static void rtl_hw_start_8101(struct net_device *dev)
3983{
Francois Romieucdf1a602007-06-11 23:29:50 +02003984 struct rtl8169_private *tp = netdev_priv(dev);
3985 void __iomem *ioaddr = tp->mmio_addr;
3986 struct pci_dev *pdev = tp->pci_dev;
3987
Francois Romieue3cf0cc2007-08-17 14:55:46 +02003988 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
3989 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02003990 int cap = tp->pcie_cap;
3991
3992 if (cap) {
3993 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
3994 PCI_EXP_DEVCTL_NOSNOOP_EN);
3995 }
Francois Romieucdf1a602007-06-11 23:29:50 +02003996 }
3997
Francois Romieu2857ffb2008-08-02 21:08:49 +02003998 switch (tp->mac_version) {
3999 case RTL_GIGA_MAC_VER_07:
4000 rtl_hw_start_8102e_1(ioaddr, pdev);
4001 break;
4002
4003 case RTL_GIGA_MAC_VER_08:
4004 rtl_hw_start_8102e_3(ioaddr, pdev);
4005 break;
4006
4007 case RTL_GIGA_MAC_VER_09:
4008 rtl_hw_start_8102e_2(ioaddr, pdev);
4009 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004010
4011 case RTL_GIGA_MAC_VER_29:
4012 rtl_hw_start_8105e_1(ioaddr, pdev);
4013 break;
4014 case RTL_GIGA_MAC_VER_30:
4015 rtl_hw_start_8105e_2(ioaddr, pdev);
4016 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004017 }
4018
4019 RTL_W8(Cfg9346, Cfg9346_Unlock);
4020
françois romieuf0298f82011-01-03 15:07:42 +00004021 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004022
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004023 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004024
4025 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4026
4027 RTL_W16(CPlusCmd, tp->cp_cmd);
4028
4029 RTL_W16(IntrMitigate, 0x0000);
4030
4031 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4032
4033 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4034 rtl_set_rx_tx_config_registers(tp);
4035
4036 RTL_W8(Cfg9346, Cfg9346_Lock);
4037
4038 RTL_R8(IntrMask);
4039
Francois Romieucdf1a602007-06-11 23:29:50 +02004040 rtl_set_rx_mode(dev);
4041
Francois Romieu0e485152007-02-20 00:00:26 +01004042 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4043
Francois Romieucdf1a602007-06-11 23:29:50 +02004044 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004045
Francois Romieu0e485152007-02-20 00:00:26 +01004046 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
4049static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4052 return -EINVAL;
4053
4054 dev->mtu = new_mtu;
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056}
4057
4058static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4059{
Al Viro95e09182007-12-22 18:55:39 +00004060 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4062}
4063
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004064static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4065 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004067 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004068 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004069
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004070 kfree(*data_buff);
4071 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 rtl8169_make_unusable_by_asic(desc);
4073}
4074
4075static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4076{
4077 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4078
4079 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4080}
4081
4082static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4083 u32 rx_buf_sz)
4084{
4085 desc->addr = cpu_to_le64(mapping);
4086 wmb();
4087 rtl8169_mark_to_asic(desc, rx_buf_sz);
4088}
4089
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004090static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004092 return (void *)ALIGN((long)data, 16);
4093}
4094
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004095static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4096 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004097{
4098 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004100 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004101 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004102 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004104 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4105 if (!data)
4106 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004107
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004108 if (rtl8169_align(data) != data) {
4109 kfree(data);
4110 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4111 if (!data)
4112 return NULL;
4113 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004114
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004115 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004116 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004117 if (unlikely(dma_mapping_error(d, mapping))) {
4118 if (net_ratelimit())
4119 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004120 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
4123 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004124 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004125
4126err_out:
4127 kfree(data);
4128 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129}
4130
4131static void rtl8169_rx_clear(struct rtl8169_private *tp)
4132{
Francois Romieu07d3f512007-02-21 22:40:46 +01004133 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
4135 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004136 if (tp->Rx_databuff[i]) {
4137 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 tp->RxDescArray + i);
4139 }
4140 }
4141}
4142
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004143static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004145 desc->opts1 |= cpu_to_le32(RingEnd);
4146}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004147
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004148static int rtl8169_rx_fill(struct rtl8169_private *tp)
4149{
4150 unsigned int i;
4151
4152 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004153 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004154
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004155 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004157
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004158 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004159 if (!data) {
4160 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004161 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004162 }
4163 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004166 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4167 return 0;
4168
4169err_out:
4170 rtl8169_rx_clear(tp);
4171 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172}
4173
4174static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4175{
4176 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4177}
4178
4179static int rtl8169_init_ring(struct net_device *dev)
4180{
4181 struct rtl8169_private *tp = netdev_priv(dev);
4182
4183 rtl8169_init_ring_indexes(tp);
4184
4185 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004186 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004188 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189}
4190
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004191static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 struct TxDesc *desc)
4193{
4194 unsigned int len = tx_skb->len;
4195
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004196 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4197
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 desc->opts1 = 0x00;
4199 desc->opts2 = 0x00;
4200 desc->addr = 0x00;
4201 tx_skb->len = 0;
4202}
4203
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004204static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4205 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
4207 unsigned int i;
4208
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004209 for (i = 0; i < n; i++) {
4210 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 struct ring_info *tx_skb = tp->tx_skb + entry;
4212 unsigned int len = tx_skb->len;
4213
4214 if (len) {
4215 struct sk_buff *skb = tx_skb->skb;
4216
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004217 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 tp->TxDescArray + entry);
4219 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004220 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 dev_kfree_skb(skb);
4222 tx_skb->skb = NULL;
4223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
4225 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004226}
4227
4228static void rtl8169_tx_clear(struct rtl8169_private *tp)
4229{
4230 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 tp->cur_tx = tp->dirty_tx = 0;
4232}
4233
David Howellsc4028952006-11-22 14:57:56 +00004234static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
4236 struct rtl8169_private *tp = netdev_priv(dev);
4237
David Howellsc4028952006-11-22 14:57:56 +00004238 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 schedule_delayed_work(&tp->task, 4);
4240}
4241
4242static void rtl8169_wait_for_quiescence(struct net_device *dev)
4243{
4244 struct rtl8169_private *tp = netdev_priv(dev);
4245 void __iomem *ioaddr = tp->mmio_addr;
4246
4247 synchronize_irq(dev->irq);
4248
4249 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004250 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
4252 rtl8169_irq_mask_and_ack(ioaddr);
4253
David S. Millerd1d08d12008-01-07 20:53:33 -08004254 tp->intr_mask = 0xffff;
4255 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004256 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257}
4258
David Howellsc4028952006-11-22 14:57:56 +00004259static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
David Howellsc4028952006-11-22 14:57:56 +00004261 struct rtl8169_private *tp =
4262 container_of(work, struct rtl8169_private, task.work);
4263 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 int ret;
4265
Francois Romieueb2a0212007-02-15 23:37:21 +01004266 rtnl_lock();
4267
4268 if (!netif_running(dev))
4269 goto out_unlock;
4270
4271 rtl8169_wait_for_quiescence(dev);
4272 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 ret = rtl8169_open(dev);
4275 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004276 if (net_ratelimit())
4277 netif_err(tp, drv, dev,
4278 "reinit failure (status = %d). Rescheduling\n",
4279 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4281 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004282
4283out_unlock:
4284 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285}
4286
David Howellsc4028952006-11-22 14:57:56 +00004287static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
David Howellsc4028952006-11-22 14:57:56 +00004289 struct rtl8169_private *tp =
4290 container_of(work, struct rtl8169_private, task.work);
4291 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Francois Romieueb2a0212007-02-15 23:37:21 +01004293 rtnl_lock();
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004296 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
4298 rtl8169_wait_for_quiescence(dev);
4299
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004300 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 rtl8169_tx_clear(tp);
4302
4303 if (tp->dirty_rx == tp->cur_rx) {
4304 rtl8169_init_ring_indexes(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004305 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 netif_wake_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004307 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 } else {
Joe Perchesbf82c182010-02-09 11:49:50 +00004309 if (net_ratelimit())
4310 netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 rtl8169_schedule_work(dev, rtl8169_reset_task);
4312 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004313
4314out_unlock:
4315 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
4318static void rtl8169_tx_timeout(struct net_device *dev)
4319{
4320 struct rtl8169_private *tp = netdev_priv(dev);
4321
françois romieue6de30d2011-01-03 15:08:37 +00004322 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
4324 /* Let's wait a bit while any (async) irq lands on */
4325 rtl8169_schedule_work(dev, rtl8169_reset_task);
4326}
4327
4328static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4329 u32 opts1)
4330{
4331 struct skb_shared_info *info = skb_shinfo(skb);
4332 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004333 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004334 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336 entry = tp->cur_tx;
4337 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4338 skb_frag_t *frag = info->frags + cur_frag;
4339 dma_addr_t mapping;
4340 u32 status, len;
4341 void *addr;
4342
4343 entry = (entry + 1) % NUM_TX_DESC;
4344
4345 txd = tp->TxDescArray + entry;
4346 len = frag->size;
4347 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004348 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004349 if (unlikely(dma_mapping_error(d, mapping))) {
4350 if (net_ratelimit())
4351 netif_err(tp, drv, tp->dev,
4352 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004353 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
4356 /* anti gcc 2.95.3 bugware (sic) */
4357 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4358
4359 txd->opts1 = cpu_to_le32(status);
4360 txd->addr = cpu_to_le64(mapping);
4361
4362 tp->tx_skb[entry].len = len;
4363 }
4364
4365 if (cur_frag) {
4366 tp->tx_skb[entry].skb = skb;
4367 txd->opts1 |= cpu_to_le32(LastFrag);
4368 }
4369
4370 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004371
4372err_out:
4373 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4374 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375}
4376
4377static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
4378{
4379 if (dev->features & NETIF_F_TSO) {
Herbert Xu79671682006-06-22 02:40:14 -07004380 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
4382 if (mss)
4383 return LargeSend | ((mss & MSSMask) << MSSShift);
4384 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07004385 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004386 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 if (ip->protocol == IPPROTO_TCP)
4389 return IPCS | TCPCS;
4390 else if (ip->protocol == IPPROTO_UDP)
4391 return IPCS | UDPCS;
4392 WARN_ON(1); /* we need a WARN() */
4393 }
4394 return 0;
4395}
4396
Stephen Hemminger613573252009-08-31 19:50:58 +00004397static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4398 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
4400 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004401 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 struct TxDesc *txd = tp->TxDescArray + entry;
4403 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004404 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 dma_addr_t mapping;
4406 u32 status, len;
4407 u32 opts1;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004408 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004411 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004412 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 }
4414
4415 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004416 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004418 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004419 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004420 if (unlikely(dma_mapping_error(d, mapping))) {
4421 if (net_ratelimit())
4422 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004423 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
4426 tp->tx_skb[entry].len = len;
4427 txd->addr = cpu_to_le64(mapping);
4428 txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4429
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004430 opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
4431
4432 frags = rtl8169_xmit_frags(tp, skb, opts1);
4433 if (frags < 0)
4434 goto err_dma_1;
4435 else if (frags)
4436 opts1 |= FirstFrag;
4437 else {
4438 opts1 |= FirstFrag | LastFrag;
4439 tp->tx_skb[entry].skb = skb;
4440 }
4441
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 wmb();
4443
4444 /* anti gcc 2.95.3 bugware (sic) */
4445 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4446 txd->opts1 = cpu_to_le32(status);
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 tp->cur_tx += frags + 1;
4449
David Dillow4c020a92010-03-03 16:33:10 +00004450 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Francois Romieu275391a2007-02-23 23:50:28 +01004452 RTL_W8(TxPoll, NPQ); /* set polling bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
4454 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4455 netif_stop_queue(dev);
4456 smp_rmb();
4457 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4458 netif_wake_queue(dev);
4459 }
4460
Stephen Hemminger613573252009-08-31 19:50:58 +00004461 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004463err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004464 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004465err_dma_0:
4466 dev_kfree_skb(skb);
4467 dev->stats.tx_dropped++;
4468 return NETDEV_TX_OK;
4469
4470err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004472 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004473 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474}
4475
4476static void rtl8169_pcierr_interrupt(struct net_device *dev)
4477{
4478 struct rtl8169_private *tp = netdev_priv(dev);
4479 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 u16 pci_status, pci_cmd;
4481
4482 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4483 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4484
Joe Perchesbf82c182010-02-09 11:49:50 +00004485 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4486 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
4488 /*
4489 * The recovery sequence below admits a very elaborated explanation:
4490 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004491 * - I did not see what else could be done;
4492 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 *
4494 * Feel free to adjust to your needs.
4495 */
Francois Romieua27993f2006-12-18 00:04:19 +01004496 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004497 pci_cmd &= ~PCI_COMMAND_PARITY;
4498 else
4499 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4500
4501 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
4503 pci_write_config_word(pdev, PCI_STATUS,
4504 pci_status & (PCI_STATUS_DETECTED_PARITY |
4505 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4506 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4507
4508 /* The infamous DAC f*ckup only happens at boot time */
4509 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004510 void __iomem *ioaddr = tp->mmio_addr;
4511
Joe Perchesbf82c182010-02-09 11:49:50 +00004512 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 tp->cp_cmd &= ~PCIDAC;
4514 RTL_W16(CPlusCmd, tp->cp_cmd);
4515 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 }
4517
françois romieue6de30d2011-01-03 15:08:37 +00004518 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004519
4520 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521}
4522
Francois Romieu07d3f512007-02-21 22:40:46 +01004523static void rtl8169_tx_interrupt(struct net_device *dev,
4524 struct rtl8169_private *tp,
4525 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
4527 unsigned int dirty_tx, tx_left;
4528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 dirty_tx = tp->dirty_tx;
4530 smp_rmb();
4531 tx_left = tp->cur_tx - dirty_tx;
4532
4533 while (tx_left > 0) {
4534 unsigned int entry = dirty_tx % NUM_TX_DESC;
4535 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 u32 status;
4537
4538 rmb();
4539 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4540 if (status & DescOwn)
4541 break;
4542
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004543 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4544 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004546 dev->stats.tx_packets++;
4547 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004548 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 tx_skb->skb = NULL;
4550 }
4551 dirty_tx++;
4552 tx_left--;
4553 }
4554
4555 if (tp->dirty_tx != dirty_tx) {
4556 tp->dirty_tx = dirty_tx;
4557 smp_wmb();
4558 if (netif_queue_stopped(dev) &&
4559 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4560 netif_wake_queue(dev);
4561 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004562 /*
4563 * 8168 hack: TxPoll requests are lost when the Tx packets are
4564 * too close. Let's kick an extra TxPoll request when a burst
4565 * of start_xmit activity is detected (if it is not detected,
4566 * it is slow enough). -- FR
4567 */
4568 smp_rmb();
4569 if (tp->cur_tx != dirty_tx)
4570 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 }
4572}
4573
Francois Romieu126fa4b2005-05-12 20:09:17 -04004574static inline int rtl8169_fragmented_frame(u32 status)
4575{
4576 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4577}
4578
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004579static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 u32 status = opts1 & RxProtoMask;
4582
4583 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004584 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 skb->ip_summed = CHECKSUM_UNNECESSARY;
4586 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004587 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004590static struct sk_buff *rtl8169_try_rx_copy(void *data,
4591 struct rtl8169_private *tp,
4592 int pkt_size,
4593 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004595 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004596 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004598 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004599 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004600 prefetch(data);
4601 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4602 if (skb)
4603 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004604 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4605
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004606 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607}
4608
Eric Dumazet630b9432010-03-31 02:08:31 +00004609/*
4610 * Warning : rtl8169_rx_interrupt() might be called :
4611 * 1) from NAPI (softirq) context
4612 * (polling = 1 : we should call netif_receive_skb())
4613 * 2) from process context (rtl8169_reset_task())
4614 * (polling = 0 : we must call netif_rx() instead)
4615 */
Francois Romieu07d3f512007-02-21 22:40:46 +01004616static int rtl8169_rx_interrupt(struct net_device *dev,
4617 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004618 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619{
4620 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004621 unsigned int count;
Eric Dumazet630b9432010-03-31 02:08:31 +00004622 int polling = (budget != ~(u32)0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 cur_rx = tp->cur_rx;
4625 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004626 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004628 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004630 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 u32 status;
4632
4633 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004634 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
4636 if (status & DescOwn)
4637 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004638 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004639 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4640 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004641 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004643 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004645 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004646 if (status & RxFOVF) {
4647 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004648 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004649 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004650 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004652 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004653 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Francois Romieu126fa4b2005-05-12 20:09:17 -04004656 /*
4657 * The driver does not support incoming fragmented
4658 * frames. They are seen as a symptom of over-mtu
4659 * sized frames.
4660 */
4661 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004662 dev->stats.rx_dropped++;
4663 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004664 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004665 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004666 }
4667
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004668 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4669 tp, pkt_size, addr);
4670 rtl8169_mark_to_asic(desc, rx_buf_sz);
4671 if (!skb) {
4672 dev->stats.rx_dropped++;
4673 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 }
4675
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004676 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 skb_put(skb, pkt_size);
4678 skb->protocol = eth_type_trans(skb, dev);
4679
Francois Romieu7a8fc772011-03-01 17:18:33 +01004680 rtl8169_rx_vlan_tag(desc, skb);
4681
4682 if (likely(polling))
4683 napi_gro_receive(&tp->napi, skb);
4684 else
4685 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Francois Romieucebf8cc2007-10-18 12:06:54 +02004687 dev->stats.rx_bytes += pkt_size;
4688 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
Francois Romieu6dccd162007-02-13 23:38:05 +01004690
4691 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00004692 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01004693 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4694 desc->opts2 = 0;
4695 cur_rx++;
4696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 }
4698
4699 count = cur_rx - tp->cur_rx;
4700 tp->cur_rx = cur_rx;
4701
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004702 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
4704 return count;
4705}
4706
Francois Romieu07d3f512007-02-21 22:40:46 +01004707static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
Francois Romieu07d3f512007-02-21 22:40:46 +01004709 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02004713 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
David Dillowf11a3772009-05-22 15:29:34 +00004715 /* loop handling interrupts until we have no new ones or
4716 * we hit a invalid/hotplug case.
4717 */
Francois Romieu865c6522008-05-11 14:51:00 +02004718 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00004719 while (status && status != 0xffff) {
4720 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
David Dillowf11a3772009-05-22 15:29:34 +00004722 /* Handle all of the error cases first. These will reset
4723 * the chip, so just exit the loop.
4724 */
4725 if (unlikely(!netif_running(dev))) {
4726 rtl8169_asic_down(ioaddr);
4727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 }
David Dillowf11a3772009-05-22 15:29:34 +00004729
Francois Romieu1519e572011-02-03 12:02:36 +01004730 if (unlikely(status & RxFIFOOver)) {
4731 switch (tp->mac_version) {
4732 /* Work around for rx fifo overflow */
4733 case RTL_GIGA_MAC_VER_11:
4734 case RTL_GIGA_MAC_VER_22:
4735 case RTL_GIGA_MAC_VER_26:
4736 netif_stop_queue(dev);
4737 rtl8169_tx_timeout(dev);
4738 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01004739 /* Testers needed. */
4740 case RTL_GIGA_MAC_VER_17:
4741 case RTL_GIGA_MAC_VER_19:
4742 case RTL_GIGA_MAC_VER_20:
4743 case RTL_GIGA_MAC_VER_21:
4744 case RTL_GIGA_MAC_VER_23:
4745 case RTL_GIGA_MAC_VER_24:
4746 case RTL_GIGA_MAC_VER_27:
4747 case RTL_GIGA_MAC_VER_28:
Francois Romieu1519e572011-02-03 12:02:36 +01004748 /* Experimental science. Pktgen proof. */
4749 case RTL_GIGA_MAC_VER_12:
4750 case RTL_GIGA_MAC_VER_25:
4751 if (status == RxFIFOOver)
4752 goto done;
4753 break;
4754 default:
4755 break;
4756 }
David Dillowf11a3772009-05-22 15:29:34 +00004757 }
4758
4759 if (unlikely(status & SYSErr)) {
4760 rtl8169_pcierr_interrupt(dev);
4761 break;
4762 }
4763
4764 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00004765 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00004766
4767 /* We need to see the lastest version of tp->intr_mask to
4768 * avoid ignoring an MSI interrupt and having to wait for
4769 * another event which may never come.
4770 */
4771 smp_rmb();
4772 if (status & tp->intr_mask & tp->napi_event) {
4773 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
4774 tp->intr_mask = ~tp->napi_event;
4775
4776 if (likely(napi_schedule_prep(&tp->napi)))
4777 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00004778 else
4779 netif_info(tp, intr, dev,
4780 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00004781 }
4782
4783 /* We only get a new MSI interrupt when all active irq
4784 * sources on the chip have been acknowledged. So, ack
4785 * everything we've seen and check if new sources have become
4786 * active to avoid blocking all interrupts from the chip.
4787 */
4788 RTL_W16(IntrStatus,
4789 (status & RxFIFOOver) ? (status | RxOverflow) : status);
4790 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 }
Francois Romieu1519e572011-02-03 12:02:36 +01004792done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 return IRQ_RETVAL(handled);
4794}
4795
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004796static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004798 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
4799 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004801 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004803 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 rtl8169_tx_interrupt(dev, tp, ioaddr);
4805
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004806 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004807 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00004808
4809 /* We need for force the visibility of tp->intr_mask
4810 * for other CPUs, as we can loose an MSI interrupt
4811 * and potentially wait for a retransmit timeout if we don't.
4812 * The posted write to IntrMask is safe, as it will
4813 * eventually make it to the chip and we won't loose anything
4814 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 */
David Dillowf11a3772009-05-22 15:29:34 +00004816 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00004817 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01004818 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 }
4820
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004821 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
Francois Romieu523a6092008-09-10 22:28:56 +02004824static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
4825{
4826 struct rtl8169_private *tp = netdev_priv(dev);
4827
4828 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
4829 return;
4830
4831 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
4832 RTL_W32(RxMissed, 0);
4833}
4834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835static void rtl8169_down(struct net_device *dev)
4836{
4837 struct rtl8169_private *tp = netdev_priv(dev);
4838 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
4840 rtl8169_delete_timer(dev);
4841
4842 netif_stop_queue(dev);
4843
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01004844 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01004845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 spin_lock_irq(&tp->lock);
4847
4848 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004849 /*
4850 * At this point device interrupts can not be enabled in any function,
4851 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
4852 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
4853 */
Francois Romieu523a6092008-09-10 22:28:56 +02004854 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
4856 spin_unlock_irq(&tp->lock);
4857
4858 synchronize_irq(dev->irq);
4859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004861 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 rtl8169_tx_clear(tp);
4864
4865 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004866
4867 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868}
4869
4870static int rtl8169_close(struct net_device *dev)
4871{
4872 struct rtl8169_private *tp = netdev_priv(dev);
4873 struct pci_dev *pdev = tp->pci_dev;
4874
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004875 pm_runtime_get_sync(&pdev->dev);
4876
Ivan Vecera355423d2009-02-06 21:49:57 -08004877 /* update counters before going down */
4878 rtl8169_update_counters(dev);
4879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 rtl8169_down(dev);
4881
4882 free_irq(dev->irq, dev);
4883
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004884 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4885 tp->RxPhyAddr);
4886 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4887 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 tp->TxDescArray = NULL;
4889 tp->RxDescArray = NULL;
4890
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004891 pm_runtime_put_sync(&pdev->dev);
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 return 0;
4894}
4895
Francois Romieu07ce4062007-02-23 23:36:39 +01004896static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897{
4898 struct rtl8169_private *tp = netdev_priv(dev);
4899 void __iomem *ioaddr = tp->mmio_addr;
4900 unsigned long flags;
4901 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01004902 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 u32 tmp = 0;
4904
4905 if (dev->flags & IFF_PROMISC) {
4906 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00004907 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 rx_mode =
4909 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4910 AcceptAllPhys;
4911 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00004912 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00004913 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 /* Too many to filter perfectly -- accept all multicasts. */
4915 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4916 mc_filter[1] = mc_filter[0] = 0xffffffff;
4917 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00004918 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01004919
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 rx_mode = AcceptBroadcast | AcceptMyPhys;
4921 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00004922 netdev_for_each_mc_addr(ha, dev) {
4923 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4925 rx_mode |= AcceptMulticast;
4926 }
4927 }
4928
4929 spin_lock_irqsave(&tp->lock, flags);
4930
4931 tmp = rtl8169_rx_config | rx_mode |
4932 (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
4933
Francois Romieuf887cce2008-07-17 22:24:18 +02004934 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01004935 u32 data = mc_filter[0];
4936
4937 mc_filter[0] = swab32(mc_filter[1]);
4938 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02004939 }
4940
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07004942 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
Francois Romieu57a9f232007-06-04 22:10:15 +02004944 RTL_W32(RxConfig, tmp);
4945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 spin_unlock_irqrestore(&tp->lock, flags);
4947}
4948
4949/**
4950 * rtl8169_get_stats - Get rtl8169 read/write statistics
4951 * @dev: The Ethernet Device to get statistics for
4952 *
4953 * Get TX/RX statistics for rtl8169
4954 */
4955static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
4956{
4957 struct rtl8169_private *tp = netdev_priv(dev);
4958 void __iomem *ioaddr = tp->mmio_addr;
4959 unsigned long flags;
4960
4961 if (netif_running(dev)) {
4962 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02004963 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 spin_unlock_irqrestore(&tp->lock, flags);
4965 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02004966
Francois Romieucebf8cc2007-10-18 12:06:54 +02004967 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968}
4969
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004970static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01004971{
françois romieu065c27c2011-01-03 15:08:12 +00004972 struct rtl8169_private *tp = netdev_priv(dev);
4973
Francois Romieu5d06a992006-02-23 00:47:58 +01004974 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004975 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01004976
françois romieu065c27c2011-01-03 15:08:12 +00004977 rtl_pll_power_down(tp);
4978
Francois Romieu5d06a992006-02-23 00:47:58 +01004979 netif_device_detach(dev);
4980 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004981}
Francois Romieu5d06a992006-02-23 00:47:58 +01004982
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004983#ifdef CONFIG_PM
4984
4985static int rtl8169_suspend(struct device *device)
4986{
4987 struct pci_dev *pdev = to_pci_dev(device);
4988 struct net_device *dev = pci_get_drvdata(pdev);
4989
4990 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02004991
Francois Romieu5d06a992006-02-23 00:47:58 +01004992 return 0;
4993}
4994
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004995static void __rtl8169_resume(struct net_device *dev)
4996{
françois romieu065c27c2011-01-03 15:08:12 +00004997 struct rtl8169_private *tp = netdev_priv(dev);
4998
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004999 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005000
5001 rtl_pll_power_up(tp);
5002
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005003 rtl8169_schedule_work(dev, rtl8169_reset_task);
5004}
5005
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005006static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005007{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005008 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005009 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005010 struct rtl8169_private *tp = netdev_priv(dev);
5011
5012 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005013
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005014 if (netif_running(dev))
5015 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005016
Francois Romieu5d06a992006-02-23 00:47:58 +01005017 return 0;
5018}
5019
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005020static int rtl8169_runtime_suspend(struct device *device)
5021{
5022 struct pci_dev *pdev = to_pci_dev(device);
5023 struct net_device *dev = pci_get_drvdata(pdev);
5024 struct rtl8169_private *tp = netdev_priv(dev);
5025
5026 if (!tp->TxDescArray)
5027 return 0;
5028
5029 spin_lock_irq(&tp->lock);
5030 tp->saved_wolopts = __rtl8169_get_wol(tp);
5031 __rtl8169_set_wol(tp, WAKE_ANY);
5032 spin_unlock_irq(&tp->lock);
5033
5034 rtl8169_net_suspend(dev);
5035
5036 return 0;
5037}
5038
5039static int rtl8169_runtime_resume(struct device *device)
5040{
5041 struct pci_dev *pdev = to_pci_dev(device);
5042 struct net_device *dev = pci_get_drvdata(pdev);
5043 struct rtl8169_private *tp = netdev_priv(dev);
5044
5045 if (!tp->TxDescArray)
5046 return 0;
5047
5048 spin_lock_irq(&tp->lock);
5049 __rtl8169_set_wol(tp, tp->saved_wolopts);
5050 tp->saved_wolopts = 0;
5051 spin_unlock_irq(&tp->lock);
5052
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005053 rtl8169_init_phy(dev, tp);
5054
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005055 __rtl8169_resume(dev);
5056
5057 return 0;
5058}
5059
5060static int rtl8169_runtime_idle(struct device *device)
5061{
5062 struct pci_dev *pdev = to_pci_dev(device);
5063 struct net_device *dev = pci_get_drvdata(pdev);
5064 struct rtl8169_private *tp = netdev_priv(dev);
5065
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005066 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005067}
5068
Alexey Dobriyan47145212009-12-14 18:00:08 -08005069static const struct dev_pm_ops rtl8169_pm_ops = {
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005070 .suspend = rtl8169_suspend,
5071 .resume = rtl8169_resume,
5072 .freeze = rtl8169_suspend,
5073 .thaw = rtl8169_resume,
5074 .poweroff = rtl8169_suspend,
5075 .restore = rtl8169_resume,
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005076 .runtime_suspend = rtl8169_runtime_suspend,
5077 .runtime_resume = rtl8169_runtime_resume,
5078 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005079};
5080
5081#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5082
5083#else /* !CONFIG_PM */
5084
5085#define RTL8169_PM_OPS NULL
5086
5087#endif /* !CONFIG_PM */
5088
Francois Romieu1765f952008-09-13 17:21:40 +02005089static void rtl_shutdown(struct pci_dev *pdev)
5090{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005091 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005092 struct rtl8169_private *tp = netdev_priv(dev);
5093 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005094
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005095 rtl8169_net_suspend(dev);
5096
Ivan Veceracc098dc2009-11-29 23:12:52 -08005097 /* restore original MAC address */
5098 rtl_rar_set(tp, dev->perm_addr);
5099
françois romieu4bb3f522009-06-17 11:41:45 +00005100 spin_lock_irq(&tp->lock);
5101
5102 rtl8169_asic_down(ioaddr);
5103
5104 spin_unlock_irq(&tp->lock);
5105
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005106 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005107 /* WoL fails with some 8168 when the receiver is disabled. */
5108 if (tp->features & RTL_FEATURE_WOL) {
5109 pci_clear_master(pdev);
5110
5111 RTL_W8(ChipCmd, CmdRxEnb);
5112 /* PCI commit */
5113 RTL_R8(ChipCmd);
5114 }
5115
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005116 pci_wake_from_d3(pdev, true);
5117 pci_set_power_state(pdev, PCI_D3hot);
5118 }
5119}
Francois Romieu5d06a992006-02-23 00:47:58 +01005120
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121static struct pci_driver rtl8169_pci_driver = {
5122 .name = MODULENAME,
5123 .id_table = rtl8169_pci_tbl,
5124 .probe = rtl8169_init_one,
5125 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005126 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005127 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128};
5129
Francois Romieu07d3f512007-02-21 22:40:46 +01005130static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131{
Jeff Garzik29917622006-08-19 17:48:59 -04005132 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133}
5134
Francois Romieu07d3f512007-02-21 22:40:46 +01005135static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136{
5137 pci_unregister_driver(&rtl8169_pci_driver);
5138}
5139
5140module_init(rtl8169_init_module);
5141module_exit(rtl8169_cleanup_module);