blob: dff0bf09416877edaaf8f5444ed350edeebe5fa0 [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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080045#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#ifdef RTL8169_DEBUG
48#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020049 if (!(expr)) { \
50 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070051 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020052 }
Joe Perches06fa7352007-10-18 21:15:00 +020053#define dprintk(fmt, args...) \
54 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#else
56#define assert(expr) do {} while (0)
57#define dprintk(fmt, args...) do {} while (0)
58#endif /* RTL8169_DEBUG */
59
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020060#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070061 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define TX_BUFFS_AVAIL(tp) \
64 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
67 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050068static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* MAC address length */
71#define MAC_ADDR_LEN 6
72
Francois Romieu9c14cea2008-07-05 00:21:15 +020073#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
76#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
77
78#define R8169_REGS_SIZE 256
79#define R8169_NAPI_WEIGHT 64
80#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
81#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
82#define RX_BUF_SIZE 1536 /* Rx Buffer size */
83#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
84#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
85
86#define RTL8169_TX_TIMEOUT (6*HZ)
87#define RTL8169_PHY_TIMEOUT (10*HZ)
88
françois romieuea8dbdd2009-03-15 01:10:50 +000089#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
90#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020091#define RTL_EEPROM_SIG_ADDR 0x0000
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* write/read MMIO register */
94#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
95#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
96#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
97#define RTL_R8(reg) readb (ioaddr + (reg))
98#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000099#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200102 RTL_GIGA_MAC_VER_01 = 0,
103 RTL_GIGA_MAC_VER_02,
104 RTL_GIGA_MAC_VER_03,
105 RTL_GIGA_MAC_VER_04,
106 RTL_GIGA_MAC_VER_05,
107 RTL_GIGA_MAC_VER_06,
108 RTL_GIGA_MAC_VER_07,
109 RTL_GIGA_MAC_VER_08,
110 RTL_GIGA_MAC_VER_09,
111 RTL_GIGA_MAC_VER_10,
112 RTL_GIGA_MAC_VER_11,
113 RTL_GIGA_MAC_VER_12,
114 RTL_GIGA_MAC_VER_13,
115 RTL_GIGA_MAC_VER_14,
116 RTL_GIGA_MAC_VER_15,
117 RTL_GIGA_MAC_VER_16,
118 RTL_GIGA_MAC_VER_17,
119 RTL_GIGA_MAC_VER_18,
120 RTL_GIGA_MAC_VER_19,
121 RTL_GIGA_MAC_VER_20,
122 RTL_GIGA_MAC_VER_21,
123 RTL_GIGA_MAC_VER_22,
124 RTL_GIGA_MAC_VER_23,
125 RTL_GIGA_MAC_VER_24,
126 RTL_GIGA_MAC_VER_25,
127 RTL_GIGA_MAC_VER_26,
128 RTL_GIGA_MAC_VER_27,
129 RTL_GIGA_MAC_VER_28,
130 RTL_GIGA_MAC_VER_29,
131 RTL_GIGA_MAC_VER_30,
132 RTL_GIGA_MAC_VER_31,
133 RTL_GIGA_MAC_VER_32,
134 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800135 RTL_GIGA_MAC_VER_34,
Francois Romieu85bffe62011-04-27 08:22:39 +0200136 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Francois Romieu2b7b4312011-04-18 22:53:24 -0700139enum rtl_tx_desc_version {
140 RTL_TD_0 = 0,
141 RTL_TD_1 = 1,
142};
143
Francois Romieu85bffe62011-04-27 08:22:39 +0200144#define _R(NAME,TD,FW) \
145 { .name = NAME, .txd_version = TD, .fw_name = FW }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800147static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700149 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200150 const char *fw_name;
151} rtl_chip_infos[] = {
152 /* PCI devices. */
153 [RTL_GIGA_MAC_VER_01] =
154 _R("RTL8169", RTL_TD_0, NULL),
155 [RTL_GIGA_MAC_VER_02] =
156 _R("RTL8169s", RTL_TD_0, NULL),
157 [RTL_GIGA_MAC_VER_03] =
158 _R("RTL8110s", RTL_TD_0, NULL),
159 [RTL_GIGA_MAC_VER_04] =
160 _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
161 [RTL_GIGA_MAC_VER_05] =
162 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
163 [RTL_GIGA_MAC_VER_06] =
164 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
165 /* PCI-E devices. */
166 [RTL_GIGA_MAC_VER_07] =
167 _R("RTL8102e", RTL_TD_1, NULL),
168 [RTL_GIGA_MAC_VER_08] =
169 _R("RTL8102e", RTL_TD_1, NULL),
170 [RTL_GIGA_MAC_VER_09] =
171 _R("RTL8102e", RTL_TD_1, NULL),
172 [RTL_GIGA_MAC_VER_10] =
173 _R("RTL8101e", RTL_TD_0, NULL),
174 [RTL_GIGA_MAC_VER_11] =
175 _R("RTL8168b/8111b", RTL_TD_0, NULL),
176 [RTL_GIGA_MAC_VER_12] =
177 _R("RTL8168b/8111b", RTL_TD_0, NULL),
178 [RTL_GIGA_MAC_VER_13] =
179 _R("RTL8101e", RTL_TD_0, NULL),
180 [RTL_GIGA_MAC_VER_14] =
181 _R("RTL8100e", RTL_TD_0, NULL),
182 [RTL_GIGA_MAC_VER_15] =
183 _R("RTL8100e", RTL_TD_0, NULL),
184 [RTL_GIGA_MAC_VER_16] =
185 _R("RTL8101e", RTL_TD_0, NULL),
186 [RTL_GIGA_MAC_VER_17] =
187 _R("RTL8168b/8111b", RTL_TD_0, NULL),
188 [RTL_GIGA_MAC_VER_18] =
189 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
190 [RTL_GIGA_MAC_VER_19] =
191 _R("RTL8168c/8111c", RTL_TD_1, NULL),
192 [RTL_GIGA_MAC_VER_20] =
193 _R("RTL8168c/8111c", RTL_TD_1, NULL),
194 [RTL_GIGA_MAC_VER_21] =
195 _R("RTL8168c/8111c", RTL_TD_1, NULL),
196 [RTL_GIGA_MAC_VER_22] =
197 _R("RTL8168c/8111c", RTL_TD_1, NULL),
198 [RTL_GIGA_MAC_VER_23] =
199 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
200 [RTL_GIGA_MAC_VER_24] =
201 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
202 [RTL_GIGA_MAC_VER_25] =
203 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
204 [RTL_GIGA_MAC_VER_26] =
205 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
206 [RTL_GIGA_MAC_VER_27] =
207 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
208 [RTL_GIGA_MAC_VER_28] =
209 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
210 [RTL_GIGA_MAC_VER_29] =
211 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
212 [RTL_GIGA_MAC_VER_30] =
213 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
214 [RTL_GIGA_MAC_VER_31] =
215 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
216 [RTL_GIGA_MAC_VER_32] =
217 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
218 [RTL_GIGA_MAC_VER_33] =
Hayes Wang70090422011-07-06 15:58:06 +0800219 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2),
220 [RTL_GIGA_MAC_VER_34] =
221 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223#undef _R
224
Francois Romieubcf0bf92006-07-26 23:14:13 +0200225enum cfg_version {
226 RTL_CFG_0 = 0x00,
227 RTL_CFG_1,
228 RTL_CFG_2
229};
230
Francois Romieu07ce4062007-02-23 23:36:39 +0100231static void rtl_hw_start_8169(struct net_device *);
232static void rtl_hw_start_8168(struct net_device *);
233static void rtl_hw_start_8101(struct net_device *);
234
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000235static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200236 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200237 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200238 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100239 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200240 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
241 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000242 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200243 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200244 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
245 { PCI_VENDOR_ID_LINKSYS, 0x1032,
246 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100247 { 0x0001, 0x8168,
248 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 {0,},
250};
251
252MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
253
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000254static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700255static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200256static struct {
257 u32 msg_enable;
258} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Francois Romieu07d3f512007-02-21 22:40:46 +0100260enum rtl_registers {
261 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100262 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100263 MAR0 = 8, /* Multicast filter. */
264 CounterAddrLow = 0x10,
265 CounterAddrHigh = 0x14,
266 TxDescStartAddrLow = 0x20,
267 TxDescStartAddrHigh = 0x24,
268 TxHDescStartAddrLow = 0x28,
269 TxHDescStartAddrHigh = 0x2c,
270 FLASH = 0x30,
271 ERSR = 0x36,
272 ChipCmd = 0x37,
273 TxPoll = 0x38,
274 IntrMask = 0x3c,
275 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700276
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800277 TxConfig = 0x40,
278#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
279#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
280
281 RxConfig = 0x44,
282#define RX128_INT_EN (1 << 15) /* 8111c and later */
283#define RX_MULTI_EN (1 << 14) /* 8111c only */
284#define RXCFG_FIFO_SHIFT 13
285 /* No threshold before first PCI xfer */
286#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
287#define RXCFG_DMA_SHIFT 8
288 /* Unlimited maximum PCI burst. */
289#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700290
Francois Romieu07d3f512007-02-21 22:40:46 +0100291 RxMissed = 0x4c,
292 Cfg9346 = 0x50,
293 Config0 = 0x51,
294 Config1 = 0x52,
295 Config2 = 0x53,
296 Config3 = 0x54,
297 Config4 = 0x55,
298 Config5 = 0x56,
299 MultiIntr = 0x5c,
300 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100301 PHYstatus = 0x6c,
302 RxMaxSize = 0xda,
303 CPlusCmd = 0xe0,
304 IntrMitigate = 0xe2,
305 RxDescAddrLow = 0xe4,
306 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000307 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
308
309#define NoEarlyTx 0x3f /* Max value : no early transmit. */
310
311 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
312
313#define TxPacketMax (8064 >> 7)
314
Francois Romieu07d3f512007-02-21 22:40:46 +0100315 FuncEvent = 0xf0,
316 FuncEventMask = 0xf4,
317 FuncPresetState = 0xf8,
318 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
Francois Romieuf162a5d2008-06-01 22:37:49 +0200321enum rtl8110_registers {
322 TBICSR = 0x64,
323 TBI_ANAR = 0x68,
324 TBI_LPAR = 0x6a,
325};
326
327enum rtl8168_8101_registers {
328 CSIDR = 0x64,
329 CSIAR = 0x68,
330#define CSIAR_FLAG 0x80000000
331#define CSIAR_WRITE_CMD 0x80000000
332#define CSIAR_BYTE_ENABLE 0x0f
333#define CSIAR_BYTE_ENABLE_SHIFT 12
334#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000335 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200336 EPHYAR = 0x80,
337#define EPHYAR_FLAG 0x80000000
338#define EPHYAR_WRITE_CMD 0x80000000
339#define EPHYAR_REG_MASK 0x1f
340#define EPHYAR_REG_SHIFT 16
341#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800342 DLLPR = 0xd0,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800343#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200344 DBG_REG = 0xd1,
345#define FIX_NAK_1 (1 << 4)
346#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800347 TWSI = 0xd2,
348 MCU = 0xd3,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800349#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800350#define EN_NDP (1 << 3)
351#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000352 EFUSEAR = 0xdc,
353#define EFUSEAR_FLAG 0x80000000
354#define EFUSEAR_WRITE_CMD 0x80000000
355#define EFUSEAR_READ_CMD 0x00000000
356#define EFUSEAR_REG_MASK 0x03ff
357#define EFUSEAR_REG_SHIFT 8
358#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200359};
360
françois romieuc0e45c12011-01-03 15:08:04 +0000361enum rtl8168_registers {
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800362 LED_FREQ = 0x1a,
363 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000364 ERIDR = 0x70,
365 ERIAR = 0x74,
366#define ERIAR_FLAG 0x80000000
367#define ERIAR_WRITE_CMD 0x80000000
368#define ERIAR_READ_CMD 0x00000000
369#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000370#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800371#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
372#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
373#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
374#define ERIAR_MASK_SHIFT 12
375#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
376#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
377#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000378 EPHY_RXER_NUM = 0x7c,
379 OCPDR = 0xb0, /* OCP GPHY access */
380#define OCPDR_WRITE_CMD 0x80000000
381#define OCPDR_READ_CMD 0x00000000
382#define OCPDR_REG_MASK 0x7f
383#define OCPDR_GPHY_REG_SHIFT 16
384#define OCPDR_DATA_MASK 0xffff
385 OCPAR = 0xb4,
386#define OCPAR_FLAG 0x80000000
387#define OCPAR_GPHY_WRITE_CMD 0x8000f060
388#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000389 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
390 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200391#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800392#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000393};
394
Francois Romieu07d3f512007-02-21 22:40:46 +0100395enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100397 SYSErr = 0x8000,
398 PCSTimeout = 0x4000,
399 SWInt = 0x0100,
400 TxDescUnavail = 0x0080,
401 RxFIFOOver = 0x0040,
402 LinkChg = 0x0020,
403 RxOverflow = 0x0010,
404 TxErr = 0x0008,
405 TxOK = 0x0004,
406 RxErr = 0x0002,
407 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* RxStatusDesc */
Francois Romieue03f33a2011-08-25 18:47:24 +0200410 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200411 RxFOVF = (1 << 23),
412 RxRWT = (1 << 22),
413 RxRES = (1 << 21),
414 RxRUNT = (1 << 20),
415 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /* ChipCmdBits */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800418 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100419 CmdReset = 0x10,
420 CmdRxEnb = 0x08,
421 CmdTxEnb = 0x04,
422 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Francois Romieu275391a2007-02-23 23:50:28 +0100424 /* TXPoll register p.5 */
425 HPQ = 0x80, /* Poll cmd on the high prio queue */
426 NPQ = 0x40, /* Poll cmd on the low prio queue */
427 FSWInt = 0x01, /* Forced software interrupt */
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100430 Cfg9346_Lock = 0x00,
431 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100434 AcceptErr = 0x20,
435 AcceptRunt = 0x10,
436 AcceptBroadcast = 0x08,
437 AcceptMulticast = 0x04,
438 AcceptMyPhys = 0x02,
439 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200440#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* TxConfigBits */
443 TxInterFrameGapShift = 24,
444 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
445
Francois Romieu5d06a992006-02-23 00:47:58 +0100446 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200447 LEDS1 = (1 << 7),
448 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200449 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200450 Speed_down = (1 << 4),
451 MEMMAP = (1 << 3),
452 IOMAP = (1 << 2),
453 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100454 PMEnable = (1 << 0), /* Power Management Enable */
455
Francois Romieu6dccd162007-02-13 23:38:05 +0100456 /* Config2 register p. 25 */
457 PCI_Clock_66MHz = 0x01,
458 PCI_Clock_33MHz = 0x00,
459
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100460 /* Config3 register p.25 */
461 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
462 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200463 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100464
Francois Romieu5d06a992006-02-23 00:47:58 +0100465 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100466 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
467 MWF = (1 << 5), /* Accept Multicast wakeup frame */
468 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200469 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100470 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100471 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* TBICSR p.28 */
474 TBIReset = 0x80000000,
475 TBILoopback = 0x40000000,
476 TBINwEnable = 0x20000000,
477 TBINwRestart = 0x10000000,
478 TBILinkOk = 0x02000000,
479 TBINwComplete = 0x01000000,
480
481 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200482 EnableBist = (1 << 15), // 8168 8101
483 Mac_dbgo_oe = (1 << 14), // 8168 8101
484 Normal_mode = (1 << 13), // unused
485 Force_half_dup = (1 << 12), // 8168 8101
486 Force_rxflow_en = (1 << 11), // 8168 8101
487 Force_txflow_en = (1 << 10), // 8168 8101
488 Cxpl_dbg_sel = (1 << 9), // 8168 8101
489 ASF = (1 << 8), // 8168 8101
490 PktCntrDisable = (1 << 7), // 8168 8101
491 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 RxVlan = (1 << 6),
493 RxChkSum = (1 << 5),
494 PCIDAC = (1 << 4),
495 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100496 INTT_0 = 0x0000, // 8168
497 INTT_1 = 0x0001, // 8168
498 INTT_2 = 0x0002, // 8168
499 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100502 TBI_Enable = 0x80,
503 TxFlowCtrl = 0x40,
504 RxFlowCtrl = 0x20,
505 _1000bpsF = 0x10,
506 _100bps = 0x08,
507 _10bps = 0x04,
508 LinkStatus = 0x02,
509 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100512 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200513
514 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100515 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516};
517
Francois Romieu2b7b4312011-04-18 22:53:24 -0700518enum rtl_desc_bit {
519 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
521 RingEnd = (1 << 30), /* End of descriptor ring */
522 FirstFrag = (1 << 29), /* First segment of a packet */
523 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700524};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Francois Romieu2b7b4312011-04-18 22:53:24 -0700526/* Generic case. */
527enum rtl_tx_desc_bit {
528 /* First doubleword. */
529 TD_LSO = (1 << 27), /* Large Send Offload */
530#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Francois Romieu2b7b4312011-04-18 22:53:24 -0700532 /* Second doubleword. */
533 TxVlanTag = (1 << 17), /* Add VLAN tag */
534};
535
536/* 8169, 8168b and 810x except 8102e. */
537enum rtl_tx_desc_bit_0 {
538 /* First doubleword. */
539#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
540 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
541 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
542 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
543};
544
545/* 8102e, 8168c and beyond. */
546enum rtl_tx_desc_bit_1 {
547 /* Second doubleword. */
548#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
549 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
550 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
551 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
552};
553
554static const struct rtl_tx_desc_info {
555 struct {
556 u32 udp;
557 u32 tcp;
558 } checksum;
559 u16 mss_shift;
560 u16 opts_offset;
561} tx_desc_info [] = {
562 [RTL_TD_0] = {
563 .checksum = {
564 .udp = TD0_IP_CS | TD0_UDP_CS,
565 .tcp = TD0_IP_CS | TD0_TCP_CS
566 },
567 .mss_shift = TD0_MSS_SHIFT,
568 .opts_offset = 0
569 },
570 [RTL_TD_1] = {
571 .checksum = {
572 .udp = TD1_IP_CS | TD1_UDP_CS,
573 .tcp = TD1_IP_CS | TD1_TCP_CS
574 },
575 .mss_shift = TD1_MSS_SHIFT,
576 .opts_offset = 1
577 }
578};
579
580enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Rx private */
582 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
583 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
584
585#define RxProtoUDP (PID1)
586#define RxProtoTCP (PID0)
587#define RxProtoIP (PID1 | PID0)
588#define RxProtoMask RxProtoIP
589
590 IPFail = (1 << 16), /* IP checksum failed */
591 UDPFail = (1 << 15), /* UDP/IP checksum failed */
592 TCPFail = (1 << 14), /* TCP/IP checksum failed */
593 RxVlanTag = (1 << 16), /* VLAN tag available */
594};
595
596#define RsvdMask 0x3fffc000
597
598struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200599 __le32 opts1;
600 __le32 opts2;
601 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602};
603
604struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200605 __le32 opts1;
606 __le32 opts2;
607 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608};
609
610struct ring_info {
611 struct sk_buff *skb;
612 u32 len;
613 u8 __pad[sizeof(void *) - sizeof(u32)];
614};
615
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200616enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200617 RTL_FEATURE_WOL = (1 << 0),
618 RTL_FEATURE_MSI = (1 << 1),
619 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200620};
621
Ivan Vecera355423d2009-02-06 21:49:57 -0800622struct rtl8169_counters {
623 __le64 tx_packets;
624 __le64 rx_packets;
625 __le64 tx_errors;
626 __le32 rx_errors;
627 __le16 rx_missed;
628 __le16 align_errors;
629 __le32 tx_one_collision;
630 __le32 tx_multi_collision;
631 __le64 rx_unicast;
632 __le64 rx_broadcast;
633 __le32 rx_multicast;
634 __le16 tx_aborted;
635 __le16 tx_underun;
636};
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638struct rtl8169_private {
639 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200640 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000641 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700642 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200643 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200644 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700645 u16 txd_version;
646 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
648 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
649 u32 dirty_rx;
650 u32 dirty_tx;
651 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
652 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
653 dma_addr_t TxPhyAddr;
654 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000655 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct timer_list timer;
658 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100659 u16 intr_event;
660 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000662
663 struct mdio_ops {
664 void (*write)(void __iomem *, int, int);
665 int (*read)(void __iomem *, int);
666 } mdio_ops;
667
françois romieu065c27c2011-01-03 15:08:12 +0000668 struct pll_power_ops {
669 void (*down)(struct rtl8169_private *);
670 void (*up)(struct rtl8169_private *);
671 } pll_power_ops;
672
Oliver Neukum54405cd2011-01-06 21:55:13 +0100673 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200674 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000675 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100676 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000677 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800679 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
David Howellsc4028952006-11-22 14:57:56 +0000680 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200681 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200682
683 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800684 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000685 u32 saved_wolopts;
Francois Romieue03f33a2011-08-25 18:47:24 +0200686 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000687
Francois Romieub6ffd972011-06-17 17:00:05 +0200688 struct rtl_fw {
689 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200690
691#define RTL_VER_SIZE 32
692
693 char version[RTL_VER_SIZE];
694
695 struct rtl_fw_phy_action {
696 __le32 *code;
697 size_t size;
698 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200699 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300700#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701};
702
Ralf Baechle979b6c12005-06-13 14:30:40 -0700703MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700706MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200707module_param_named(debug, debug.msg_enable, int, 0);
708MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709MODULE_LICENSE("GPL");
710MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000711MODULE_FIRMWARE(FIRMWARE_8168D_1);
712MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000713MODULE_FIRMWARE(FIRMWARE_8168E_1);
714MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wangbbb8af72011-09-06 16:55:17 +0800715MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800716MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000719static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
720 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100721static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100723static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100725static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200727static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700729 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200730static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200732static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700733static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
françois romieub646d902011-01-03 15:08:21 +0000735static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
736{
737 void __iomem *ioaddr = tp->mmio_addr;
738 int i;
739
740 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
741 for (i = 0; i < 20; i++) {
742 udelay(100);
743 if (RTL_R32(OCPAR) & OCPAR_FLAG)
744 break;
745 }
746 return RTL_R32(OCPDR);
747}
748
749static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
750{
751 void __iomem *ioaddr = tp->mmio_addr;
752 int i;
753
754 RTL_W32(OCPDR, data);
755 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
756 for (i = 0; i < 20; i++) {
757 udelay(100);
758 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
759 break;
760 }
761}
762
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800763static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000764{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800765 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000766 int i;
767
768 RTL_W8(ERIDR, cmd);
769 RTL_W32(ERIAR, 0x800010e8);
770 msleep(2);
771 for (i = 0; i < 5; i++) {
772 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200773 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000774 break;
775 }
776
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800777 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000778}
779
780#define OOB_CMD_RESET 0x00
781#define OOB_CMD_DRIVER_START 0x05
782#define OOB_CMD_DRIVER_STOP 0x06
783
Francois Romieucecb5fd2011-04-01 10:21:07 +0200784static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
785{
786 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
787}
788
françois romieub646d902011-01-03 15:08:21 +0000789static void rtl8168_driver_start(struct rtl8169_private *tp)
790{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200791 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000792 int i;
793
794 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
795
Francois Romieucecb5fd2011-04-01 10:21:07 +0200796 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000797
françois romieub646d902011-01-03 15:08:21 +0000798 for (i = 0; i < 10; i++) {
799 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000800 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000801 break;
802 }
803}
804
805static void rtl8168_driver_stop(struct rtl8169_private *tp)
806{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200807 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000808 int i;
809
810 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
811
Francois Romieucecb5fd2011-04-01 10:21:07 +0200812 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000813
françois romieub646d902011-01-03 15:08:21 +0000814 for (i = 0; i < 10; i++) {
815 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000816 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000817 break;
818 }
819}
820
hayeswang4804b3b2011-03-21 01:50:29 +0000821static int r8168dp_check_dash(struct rtl8169_private *tp)
822{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200823 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000824
Francois Romieucecb5fd2011-04-01 10:21:07 +0200825 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000826}
françois romieub646d902011-01-03 15:08:21 +0000827
françois romieu4da19632011-01-03 15:07:55 +0000828static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 int i;
831
Francois Romieua6baf3a2007-11-08 23:23:21 +0100832 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Francois Romieu23714082006-01-29 00:49:09 +0100834 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100835 /*
836 * Check if the RTL8169 has completed writing to the specified
837 * MII register.
838 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200839 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 break;
Francois Romieu23714082006-01-29 00:49:09 +0100841 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700843 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700844 * According to hardware specs a 20us delay is required after write
845 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700846 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700847 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
françois romieu4da19632011-01-03 15:07:55 +0000850static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 int i, value = -1;
853
Francois Romieua6baf3a2007-11-08 23:23:21 +0100854 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Francois Romieu23714082006-01-29 00:49:09 +0100856 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100857 /*
858 * Check if the RTL8169 has completed retrieving data from
859 * the specified MII register.
860 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100862 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 break;
864 }
Francois Romieu23714082006-01-29 00:49:09 +0100865 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700867 /*
868 * According to hardware specs a 20us delay is required after read
869 * complete indication, but before sending next command.
870 */
871 udelay(20);
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return value;
874}
875
françois romieuc0e45c12011-01-03 15:08:04 +0000876static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
877{
878 int i;
879
880 RTL_W32(OCPDR, data |
881 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
882 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
883 RTL_W32(EPHY_RXER_NUM, 0);
884
885 for (i = 0; i < 100; i++) {
886 mdelay(1);
887 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
888 break;
889 }
890}
891
892static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
893{
894 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
895 (value & OCPDR_DATA_MASK));
896}
897
898static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
899{
900 int i;
901
902 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
903
904 mdelay(1);
905 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
906 RTL_W32(EPHY_RXER_NUM, 0);
907
908 for (i = 0; i < 100; i++) {
909 mdelay(1);
910 if (RTL_R32(OCPAR) & OCPAR_FLAG)
911 break;
912 }
913
914 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
915}
916
françois romieue6de30d2011-01-03 15:08:37 +0000917#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
918
919static void r8168dp_2_mdio_start(void __iomem *ioaddr)
920{
921 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
922}
923
924static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
925{
926 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
927}
928
929static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
930{
931 r8168dp_2_mdio_start(ioaddr);
932
933 r8169_mdio_write(ioaddr, reg_addr, value);
934
935 r8168dp_2_mdio_stop(ioaddr);
936}
937
938static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
939{
940 int value;
941
942 r8168dp_2_mdio_start(ioaddr);
943
944 value = r8169_mdio_read(ioaddr, reg_addr);
945
946 r8168dp_2_mdio_stop(ioaddr);
947
948 return value;
949}
950
françois romieu4da19632011-01-03 15:07:55 +0000951static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200952{
françois romieuc0e45c12011-01-03 15:08:04 +0000953 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200954}
955
françois romieu4da19632011-01-03 15:07:55 +0000956static int rtl_readphy(struct rtl8169_private *tp, int location)
957{
françois romieuc0e45c12011-01-03 15:08:04 +0000958 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000959}
960
961static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
962{
963 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
964}
965
966static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000967{
968 int val;
969
françois romieu4da19632011-01-03 15:07:55 +0000970 val = rtl_readphy(tp, reg_addr);
971 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000972}
973
Francois Romieuccdffb92008-07-26 14:26:06 +0200974static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
975 int val)
976{
977 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200978
françois romieu4da19632011-01-03 15:07:55 +0000979 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200980}
981
982static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
983{
984 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200985
françois romieu4da19632011-01-03 15:07:55 +0000986 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200987}
988
Francois Romieudacf8152008-08-02 20:44:13 +0200989static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
990{
991 unsigned int i;
992
993 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
994 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
995
996 for (i = 0; i < 100; i++) {
997 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
998 break;
999 udelay(10);
1000 }
1001}
1002
1003static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1004{
1005 u16 value = 0xffff;
1006 unsigned int i;
1007
1008 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1009
1010 for (i = 0; i < 100; i++) {
1011 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1012 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1013 break;
1014 }
1015 udelay(10);
1016 }
1017
1018 return value;
1019}
1020
1021static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1022{
1023 unsigned int i;
1024
1025 RTL_W32(CSIDR, value);
1026 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1027 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1028
1029 for (i = 0; i < 100; i++) {
1030 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1031 break;
1032 udelay(10);
1033 }
1034}
1035
1036static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1037{
1038 u32 value = ~0x00;
1039 unsigned int i;
1040
1041 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1042 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1043
1044 for (i = 0; i < 100; i++) {
1045 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1046 value = RTL_R32(CSIDR);
1047 break;
1048 }
1049 udelay(10);
1050 }
1051
1052 return value;
1053}
1054
Hayes Wang133ac402011-07-06 15:58:05 +08001055static
1056void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1057{
1058 unsigned int i;
1059
1060 BUG_ON((addr & 3) || (mask == 0));
1061 RTL_W32(ERIDR, val);
1062 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1063
1064 for (i = 0; i < 100; i++) {
1065 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1066 break;
1067 udelay(100);
1068 }
1069}
1070
1071static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1072{
1073 u32 value = ~0x00;
1074 unsigned int i;
1075
1076 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1077
1078 for (i = 0; i < 100; i++) {
1079 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1080 value = RTL_R32(ERIDR);
1081 break;
1082 }
1083 udelay(100);
1084 }
1085
1086 return value;
1087}
1088
1089static void
1090rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1091{
1092 u32 val;
1093
1094 val = rtl_eri_read(ioaddr, addr, type);
1095 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1096}
1097
françois romieuc28aa382011-08-02 03:53:43 +00001098struct exgmac_reg {
1099 u16 addr;
1100 u16 mask;
1101 u32 val;
1102};
1103
1104static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1105 const struct exgmac_reg *r, int len)
1106{
1107 while (len-- > 0) {
1108 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1109 r++;
1110 }
1111}
1112
françois romieudaf9df62009-10-07 12:44:20 +00001113static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1114{
1115 u8 value = 0xff;
1116 unsigned int i;
1117
1118 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1119
1120 for (i = 0; i < 300; i++) {
1121 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1122 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1123 break;
1124 }
1125 udelay(100);
1126 }
1127
1128 return value;
1129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1132{
1133 RTL_W16(IntrMask, 0x0000);
1134
1135 RTL_W16(IntrStatus, 0xffff);
1136}
1137
françois romieu4da19632011-01-03 15:07:55 +00001138static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
françois romieu4da19632011-01-03 15:07:55 +00001140 void __iomem *ioaddr = tp->mmio_addr;
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return RTL_R32(TBICSR) & TBIReset;
1143}
1144
françois romieu4da19632011-01-03 15:07:55 +00001145static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
françois romieu4da19632011-01-03 15:07:55 +00001147 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
1150static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1151{
1152 return RTL_R32(TBICSR) & TBILinkOk;
1153}
1154
1155static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1156{
1157 return RTL_R8(PHYstatus) & LinkStatus;
1158}
1159
françois romieu4da19632011-01-03 15:07:55 +00001160static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
françois romieu4da19632011-01-03 15:07:55 +00001162 void __iomem *ioaddr = tp->mmio_addr;
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1165}
1166
françois romieu4da19632011-01-03 15:07:55 +00001167static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 unsigned int val;
1170
françois romieu4da19632011-01-03 15:07:55 +00001171 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1172 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Hayes Wang70090422011-07-06 15:58:06 +08001175static void rtl_link_chg_patch(struct rtl8169_private *tp)
1176{
1177 void __iomem *ioaddr = tp->mmio_addr;
1178 struct net_device *dev = tp->dev;
1179
1180 if (!netif_running(dev))
1181 return;
1182
1183 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1184 if (RTL_R8(PHYstatus) & _1000bpsF) {
1185 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1186 0x00000011, ERIAR_EXGMAC);
1187 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1188 0x00000005, ERIAR_EXGMAC);
1189 } else if (RTL_R8(PHYstatus) & _100bps) {
1190 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1191 0x0000001f, ERIAR_EXGMAC);
1192 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1193 0x00000005, ERIAR_EXGMAC);
1194 } else {
1195 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1196 0x0000001f, ERIAR_EXGMAC);
1197 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1198 0x0000003f, ERIAR_EXGMAC);
1199 }
1200 /* Reset packet filter */
1201 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1202 ERIAR_EXGMAC);
1203 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1204 ERIAR_EXGMAC);
1205 }
1206}
1207
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001208static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001209 struct rtl8169_private *tp,
1210 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 unsigned long flags;
1213
1214 spin_lock_irqsave(&tp->lock, flags);
1215 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001216 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001217 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001218 if (pm)
1219 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001221 if (net_ratelimit())
1222 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001223 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001225 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001226 if (pm)
1227 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 spin_unlock_irqrestore(&tp->lock, flags);
1230}
1231
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001232static void rtl8169_check_link_status(struct net_device *dev,
1233 struct rtl8169_private *tp,
1234 void __iomem *ioaddr)
1235{
1236 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1237}
1238
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001239#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1240
1241static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1242{
1243 void __iomem *ioaddr = tp->mmio_addr;
1244 u8 options;
1245 u32 wolopts = 0;
1246
1247 options = RTL_R8(Config1);
1248 if (!(options & PMEnable))
1249 return 0;
1250
1251 options = RTL_R8(Config3);
1252 if (options & LinkUp)
1253 wolopts |= WAKE_PHY;
1254 if (options & MagicPacket)
1255 wolopts |= WAKE_MAGIC;
1256
1257 options = RTL_R8(Config5);
1258 if (options & UWF)
1259 wolopts |= WAKE_UCAST;
1260 if (options & BWF)
1261 wolopts |= WAKE_BCAST;
1262 if (options & MWF)
1263 wolopts |= WAKE_MCAST;
1264
1265 return wolopts;
1266}
1267
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001268static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1269{
1270 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001271
1272 spin_lock_irq(&tp->lock);
1273
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001274 wol->supported = WAKE_ANY;
1275 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001276
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001277 spin_unlock_irq(&tp->lock);
1278}
1279
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001280static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001281{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001282 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001283 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001284 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001285 u32 opt;
1286 u16 reg;
1287 u8 mask;
1288 } cfg[] = {
1289 { WAKE_ANY, Config1, PMEnable },
1290 { WAKE_PHY, Config3, LinkUp },
1291 { WAKE_MAGIC, Config3, MagicPacket },
1292 { WAKE_UCAST, Config5, UWF },
1293 { WAKE_BCAST, Config5, BWF },
1294 { WAKE_MCAST, Config5, MWF },
1295 { WAKE_ANY, Config5, LanWake }
1296 };
1297
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001298 RTL_W8(Cfg9346, Cfg9346_Unlock);
1299
1300 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1301 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001302 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001303 options |= cfg[i].mask;
1304 RTL_W8(cfg[i].reg, options);
1305 }
1306
1307 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001308}
1309
1310static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1311{
1312 struct rtl8169_private *tp = netdev_priv(dev);
1313
1314 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001315
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001316 if (wol->wolopts)
1317 tp->features |= RTL_FEATURE_WOL;
1318 else
1319 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001320 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001321 spin_unlock_irq(&tp->lock);
1322
françois romieuea809072010-11-08 13:23:58 +00001323 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1324
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001325 return 0;
1326}
1327
Francois Romieu31bd2042011-04-26 18:58:59 +02001328static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1329{
Francois Romieu85bffe62011-04-27 08:22:39 +02001330 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333static void rtl8169_get_drvinfo(struct net_device *dev,
1334 struct ethtool_drvinfo *info)
1335{
1336 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001337 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 strcpy(info->driver, MODULENAME);
1340 strcpy(info->version, RTL8169_VERSION);
1341 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001342 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1343 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1344 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
1347static int rtl8169_get_regs_len(struct net_device *dev)
1348{
1349 return R8169_REGS_SIZE;
1350}
1351
1352static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001353 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 struct rtl8169_private *tp = netdev_priv(dev);
1356 void __iomem *ioaddr = tp->mmio_addr;
1357 int ret = 0;
1358 u32 reg;
1359
1360 reg = RTL_R32(TBICSR);
1361 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1362 (duplex == DUPLEX_FULL)) {
1363 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1364 } else if (autoneg == AUTONEG_ENABLE)
1365 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1366 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001367 netif_warn(tp, link, dev,
1368 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 ret = -EOPNOTSUPP;
1370 }
1371
1372 return ret;
1373}
1374
1375static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001376 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001379 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001380 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Hayes Wang716b50a2011-02-22 17:26:18 +08001382 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001385 int auto_nego;
1386
françois romieu4da19632011-01-03 15:07:55 +00001387 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001388 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1389 ADVERTISE_100HALF | ADVERTISE_100FULL);
1390
1391 if (adv & ADVERTISED_10baseT_Half)
1392 auto_nego |= ADVERTISE_10HALF;
1393 if (adv & ADVERTISED_10baseT_Full)
1394 auto_nego |= ADVERTISE_10FULL;
1395 if (adv & ADVERTISED_100baseT_Half)
1396 auto_nego |= ADVERTISE_100HALF;
1397 if (adv & ADVERTISED_100baseT_Full)
1398 auto_nego |= ADVERTISE_100FULL;
1399
françois romieu3577aa12009-05-19 10:46:48 +00001400 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1401
françois romieu4da19632011-01-03 15:07:55 +00001402 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001403 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1404
1405 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001406 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001407 if (adv & ADVERTISED_1000baseT_Half)
1408 giga_ctrl |= ADVERTISE_1000HALF;
1409 if (adv & ADVERTISED_1000baseT_Full)
1410 giga_ctrl |= ADVERTISE_1000FULL;
1411 } else if (adv & (ADVERTISED_1000baseT_Half |
1412 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001413 netif_info(tp, link, dev,
1414 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001415 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
françois romieu3577aa12009-05-19 10:46:48 +00001418 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001419
françois romieu4da19632011-01-03 15:07:55 +00001420 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1421 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001422 } else {
1423 giga_ctrl = 0;
1424
1425 if (speed == SPEED_10)
1426 bmcr = 0;
1427 else if (speed == SPEED_100)
1428 bmcr = BMCR_SPEED100;
1429 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001430 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001431
1432 if (duplex == DUPLEX_FULL)
1433 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001434 }
1435
françois romieu4da19632011-01-03 15:07:55 +00001436 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001437
Francois Romieucecb5fd2011-04-01 10:21:07 +02001438 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1439 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001440 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001441 rtl_writephy(tp, 0x17, 0x2138);
1442 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001443 } else {
françois romieu4da19632011-01-03 15:07:55 +00001444 rtl_writephy(tp, 0x17, 0x2108);
1445 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001446 }
1447 }
1448
Oliver Neukum54405cd2011-01-06 21:55:13 +01001449 rc = 0;
1450out:
1451 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001455 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 struct rtl8169_private *tp = netdev_priv(dev);
1458 int ret;
1459
Oliver Neukum54405cd2011-01-06 21:55:13 +01001460 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001461 if (ret < 0)
1462 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Francois Romieu4876cc12011-03-11 21:07:11 +01001464 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1465 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001467 }
1468out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return ret;
1470}
1471
1472static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1473{
1474 struct rtl8169_private *tp = netdev_priv(dev);
1475 unsigned long flags;
1476 int ret;
1477
Francois Romieu4876cc12011-03-11 21:07:11 +01001478 del_timer_sync(&tp->timer);
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001481 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001482 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return ret;
1486}
1487
Michał Mirosław350fb322011-04-08 06:35:56 +00001488static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001490 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001491 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Michał Mirosław350fb322011-04-08 06:35:56 +00001493 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Michał Mirosław350fb322011-04-08 06:35:56 +00001496static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 struct rtl8169_private *tp = netdev_priv(dev);
1499 void __iomem *ioaddr = tp->mmio_addr;
1500 unsigned long flags;
1501
1502 spin_lock_irqsave(&tp->lock, flags);
1503
Michał Mirosław350fb322011-04-08 06:35:56 +00001504 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 tp->cp_cmd |= RxChkSum;
1506 else
1507 tp->cp_cmd &= ~RxChkSum;
1508
Michał Mirosław350fb322011-04-08 06:35:56 +00001509 if (dev->features & NETIF_F_HW_VLAN_RX)
1510 tp->cp_cmd |= RxVlan;
1511 else
1512 tp->cp_cmd &= ~RxVlan;
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 RTL_W16(CPlusCmd, tp->cp_cmd);
1515 RTL_R16(CPlusCmd);
1516
1517 spin_unlock_irqrestore(&tp->lock, flags);
1518
1519 return 0;
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1523 struct sk_buff *skb)
1524{
Jesse Grosseab6d182010-10-20 13:56:03 +00001525 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1527}
1528
Francois Romieu7a8fc772011-03-01 17:18:33 +01001529static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Francois Romieu7a8fc772011-03-01 17:18:33 +01001533 if (opts2 & RxVlanTag)
1534 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
Francois Romieuccdffb92008-07-26 14:26:06 +02001539static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct rtl8169_private *tp = netdev_priv(dev);
1542 void __iomem *ioaddr = tp->mmio_addr;
1543 u32 status;
1544
1545 cmd->supported =
1546 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1547 cmd->port = PORT_FIBRE;
1548 cmd->transceiver = XCVR_INTERNAL;
1549
1550 status = RTL_R32(TBICSR);
1551 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1552 cmd->autoneg = !!(status & TBINwEnable);
1553
David Decotigny70739492011-04-27 18:32:40 +00001554 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001556
1557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
Francois Romieuccdffb92008-07-26 14:26:06 +02001560static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Francois Romieuccdffb92008-07-26 14:26:06 +02001564 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1568{
1569 struct rtl8169_private *tp = netdev_priv(dev);
1570 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001571 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 spin_lock_irqsave(&tp->lock, flags);
1574
Francois Romieuccdffb92008-07-26 14:26:06 +02001575 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001578 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
1581static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1582 void *p)
1583{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001584 struct rtl8169_private *tp = netdev_priv(dev);
1585 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Francois Romieu5b0384f2006-08-16 16:00:01 +02001587 if (regs->len > R8169_REGS_SIZE)
1588 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Francois Romieu5b0384f2006-08-16 16:00:01 +02001590 spin_lock_irqsave(&tp->lock, flags);
1591 memcpy_fromio(p, tp->mmio_addr, regs->len);
1592 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001595static u32 rtl8169_get_msglevel(struct net_device *dev)
1596{
1597 struct rtl8169_private *tp = netdev_priv(dev);
1598
1599 return tp->msg_enable;
1600}
1601
1602static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1603{
1604 struct rtl8169_private *tp = netdev_priv(dev);
1605
1606 tp->msg_enable = value;
1607}
1608
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001609static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1610 "tx_packets",
1611 "rx_packets",
1612 "tx_errors",
1613 "rx_errors",
1614 "rx_missed",
1615 "align_errors",
1616 "tx_single_collisions",
1617 "tx_multi_collisions",
1618 "unicast",
1619 "broadcast",
1620 "multicast",
1621 "tx_aborted",
1622 "tx_underrun",
1623};
1624
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001625static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001626{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001627 switch (sset) {
1628 case ETH_SS_STATS:
1629 return ARRAY_SIZE(rtl8169_gstrings);
1630 default:
1631 return -EOPNOTSUPP;
1632 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001633}
1634
Ivan Vecera355423d2009-02-06 21:49:57 -08001635static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001636{
1637 struct rtl8169_private *tp = netdev_priv(dev);
1638 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001639 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001640 struct rtl8169_counters *counters;
1641 dma_addr_t paddr;
1642 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001643 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001644
Ivan Vecera355423d2009-02-06 21:49:57 -08001645 /*
1646 * Some chips are unable to dump tally counters when the receiver
1647 * is disabled.
1648 */
1649 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1650 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001651
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001652 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001653 if (!counters)
1654 return;
1655
1656 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001657 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001658 RTL_W32(CounterAddrLow, cmd);
1659 RTL_W32(CounterAddrLow, cmd | CounterDump);
1660
Ivan Vecera355423d2009-02-06 21:49:57 -08001661 while (wait--) {
1662 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001663 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001664 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001665 }
1666 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001667 }
1668
1669 RTL_W32(CounterAddrLow, 0);
1670 RTL_W32(CounterAddrHigh, 0);
1671
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001672 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001673}
1674
Ivan Vecera355423d2009-02-06 21:49:57 -08001675static void rtl8169_get_ethtool_stats(struct net_device *dev,
1676 struct ethtool_stats *stats, u64 *data)
1677{
1678 struct rtl8169_private *tp = netdev_priv(dev);
1679
1680 ASSERT_RTNL();
1681
1682 rtl8169_update_counters(dev);
1683
1684 data[0] = le64_to_cpu(tp->counters.tx_packets);
1685 data[1] = le64_to_cpu(tp->counters.rx_packets);
1686 data[2] = le64_to_cpu(tp->counters.tx_errors);
1687 data[3] = le32_to_cpu(tp->counters.rx_errors);
1688 data[4] = le16_to_cpu(tp->counters.rx_missed);
1689 data[5] = le16_to_cpu(tp->counters.align_errors);
1690 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1691 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1692 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1693 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1694 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1695 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1696 data[12] = le16_to_cpu(tp->counters.tx_underun);
1697}
1698
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001699static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1700{
1701 switch(stringset) {
1702 case ETH_SS_STATS:
1703 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1704 break;
1705 }
1706}
1707
Jeff Garzik7282d492006-09-13 14:30:00 -04001708static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 .get_drvinfo = rtl8169_get_drvinfo,
1710 .get_regs_len = rtl8169_get_regs_len,
1711 .get_link = ethtool_op_get_link,
1712 .get_settings = rtl8169_get_settings,
1713 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001714 .get_msglevel = rtl8169_get_msglevel,
1715 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001717 .get_wol = rtl8169_get_wol,
1718 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001719 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001720 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001721 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722};
1723
Francois Romieu07d3f512007-02-21 22:40:46 +01001724static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001725 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Francois Romieu5d320a22011-05-08 17:47:36 +02001727 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001728 /*
1729 * The driver currently handles the 8168Bf and the 8168Be identically
1730 * but they can be identified more specifically through the test below
1731 * if needed:
1732 *
1733 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001734 *
1735 * Same thing for the 8101Eb and the 8101Ec:
1736 *
1737 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001738 */
Francois Romieu37441002011-06-17 22:58:54 +02001739 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001741 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int mac_version;
1743 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001744 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001745 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001746 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1747 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1748 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1749
Francois Romieu5b538df2008-07-20 16:22:45 +02001750 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001751 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1752 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001753 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001754
françois romieue6de30d2011-01-03 15:08:37 +00001755 /* 8168DP family. */
1756 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1757 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001758 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001759
Francois Romieuef808d52008-06-29 13:10:54 +02001760 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001761 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001762 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001763 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001764 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001765 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1766 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001767 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001768 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001769 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001770
1771 /* 8168B family. */
1772 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1773 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1774 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1775 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1776
1777 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001778 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001779 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1780 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1781 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001782 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1783 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1784 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1785 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1786 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1787 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001788 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001789 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001790 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001791 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1792 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001793 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1794 /* FIXME: where did these entries come from ? -- FR */
1795 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1796 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1797
1798 /* 8110 family. */
1799 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1800 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1801 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1802 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1803 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1804 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1805
Jean Delvaref21b75e2009-05-26 20:54:48 -07001806 /* Catch-all */
1807 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001808 };
1809 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 u32 reg;
1811
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001812 reg = RTL_R32(TxConfig);
1813 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 p++;
1815 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001816
1817 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1818 netif_notice(tp, probe, dev,
1819 "unknown MAC, using family default\n");
1820 tp->mac_version = default_version;
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
1824static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1825{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001826 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Francois Romieu867763c2007-08-17 18:21:58 +02001829struct phy_reg {
1830 u16 reg;
1831 u16 val;
1832};
1833
françois romieu4da19632011-01-03 15:07:55 +00001834static void rtl_writephy_batch(struct rtl8169_private *tp,
1835 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001836{
1837 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001838 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001839 regs++;
1840 }
1841}
1842
françois romieubca03d52011-01-03 15:07:31 +00001843#define PHY_READ 0x00000000
1844#define PHY_DATA_OR 0x10000000
1845#define PHY_DATA_AND 0x20000000
1846#define PHY_BJMPN 0x30000000
1847#define PHY_READ_EFUSE 0x40000000
1848#define PHY_READ_MAC_BYTE 0x50000000
1849#define PHY_WRITE_MAC_BYTE 0x60000000
1850#define PHY_CLEAR_READCOUNT 0x70000000
1851#define PHY_WRITE 0x80000000
1852#define PHY_READCOUNT_EQ_SKIP 0x90000000
1853#define PHY_COMP_EQ_SKIPN 0xa0000000
1854#define PHY_COMP_NEQ_SKIPN 0xb0000000
1855#define PHY_WRITE_PREVIOUS 0xc0000000
1856#define PHY_SKIPN 0xd0000000
1857#define PHY_DELAY_MS 0xe0000000
1858#define PHY_WRITE_ERI_WORD 0xf0000000
1859
Hayes Wang960aee62011-06-18 11:37:48 +02001860struct fw_info {
1861 u32 magic;
1862 char version[RTL_VER_SIZE];
1863 __le32 fw_start;
1864 __le32 fw_len;
1865 u8 chksum;
1866} __packed;
1867
Francois Romieu1c361ef2011-06-17 17:16:24 +02001868#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1869
1870static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001871{
Francois Romieub6ffd972011-06-17 17:00:05 +02001872 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001873 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001874 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1875 char *version = rtl_fw->version;
1876 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001877
Francois Romieu1c361ef2011-06-17 17:16:24 +02001878 if (fw->size < FW_OPCODE_SIZE)
1879 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001880
1881 if (!fw_info->magic) {
1882 size_t i, size, start;
1883 u8 checksum = 0;
1884
1885 if (fw->size < sizeof(*fw_info))
1886 goto out;
1887
1888 for (i = 0; i < fw->size; i++)
1889 checksum += fw->data[i];
1890 if (checksum != 0)
1891 goto out;
1892
1893 start = le32_to_cpu(fw_info->fw_start);
1894 if (start > fw->size)
1895 goto out;
1896
1897 size = le32_to_cpu(fw_info->fw_len);
1898 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1899 goto out;
1900
1901 memcpy(version, fw_info->version, RTL_VER_SIZE);
1902
1903 pa->code = (__le32 *)(fw->data + start);
1904 pa->size = size;
1905 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001906 if (fw->size % FW_OPCODE_SIZE)
1907 goto out;
1908
1909 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1910
1911 pa->code = (__le32 *)fw->data;
1912 pa->size = fw->size / FW_OPCODE_SIZE;
1913 }
1914 version[RTL_VER_SIZE - 1] = 0;
1915
1916 rc = true;
1917out:
1918 return rc;
1919}
1920
Francois Romieufd112f22011-06-18 00:10:29 +02001921static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1922 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02001923{
Francois Romieufd112f22011-06-18 00:10:29 +02001924 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001925 size_t index;
1926
Francois Romieu1c361ef2011-06-17 17:16:24 +02001927 for (index = 0; index < pa->size; index++) {
1928 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00001929 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001930
hayeswang42b82dc2011-01-10 02:07:25 +00001931 switch(action & 0xf0000000) {
1932 case PHY_READ:
1933 case PHY_DATA_OR:
1934 case PHY_DATA_AND:
1935 case PHY_READ_EFUSE:
1936 case PHY_CLEAR_READCOUNT:
1937 case PHY_WRITE:
1938 case PHY_WRITE_PREVIOUS:
1939 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001940 break;
1941
hayeswang42b82dc2011-01-10 02:07:25 +00001942 case PHY_BJMPN:
1943 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02001944 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001945 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001946 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001947 }
1948 break;
1949 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001950 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001951 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001952 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001953 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001954 }
1955 break;
1956 case PHY_COMP_EQ_SKIPN:
1957 case PHY_COMP_NEQ_SKIPN:
1958 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001959 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001960 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001961 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001962 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001963 }
1964 break;
1965
1966 case PHY_READ_MAC_BYTE:
1967 case PHY_WRITE_MAC_BYTE:
1968 case PHY_WRITE_ERI_WORD:
1969 default:
Francois Romieufd112f22011-06-18 00:10:29 +02001970 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00001971 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02001972 goto out;
françois romieubca03d52011-01-03 15:07:31 +00001973 }
1974 }
Francois Romieufd112f22011-06-18 00:10:29 +02001975 rc = true;
1976out:
1977 return rc;
1978}
françois romieubca03d52011-01-03 15:07:31 +00001979
Francois Romieufd112f22011-06-18 00:10:29 +02001980static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1981{
1982 struct net_device *dev = tp->dev;
1983 int rc = -EINVAL;
1984
1985 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1986 netif_err(tp, ifup, dev, "invalid firwmare\n");
1987 goto out;
1988 }
1989
1990 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1991 rc = 0;
1992out:
1993 return rc;
1994}
1995
1996static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1997{
1998 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1999 u32 predata, count;
2000 size_t index;
2001
2002 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002003
Francois Romieu1c361ef2011-06-17 17:16:24 +02002004 for (index = 0; index < pa->size; ) {
2005 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002006 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002007 u32 regno = (action & 0x0fff0000) >> 16;
2008
2009 if (!action)
2010 break;
françois romieubca03d52011-01-03 15:07:31 +00002011
2012 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002013 case PHY_READ:
2014 predata = rtl_readphy(tp, regno);
2015 count++;
2016 index++;
françois romieubca03d52011-01-03 15:07:31 +00002017 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002018 case PHY_DATA_OR:
2019 predata |= data;
2020 index++;
2021 break;
2022 case PHY_DATA_AND:
2023 predata &= data;
2024 index++;
2025 break;
2026 case PHY_BJMPN:
2027 index -= regno;
2028 break;
2029 case PHY_READ_EFUSE:
2030 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2031 index++;
2032 break;
2033 case PHY_CLEAR_READCOUNT:
2034 count = 0;
2035 index++;
2036 break;
2037 case PHY_WRITE:
2038 rtl_writephy(tp, regno, data);
2039 index++;
2040 break;
2041 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002042 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002043 break;
2044 case PHY_COMP_EQ_SKIPN:
2045 if (predata == data)
2046 index += regno;
2047 index++;
2048 break;
2049 case PHY_COMP_NEQ_SKIPN:
2050 if (predata != data)
2051 index += regno;
2052 index++;
2053 break;
2054 case PHY_WRITE_PREVIOUS:
2055 rtl_writephy(tp, regno, predata);
2056 index++;
2057 break;
2058 case PHY_SKIPN:
2059 index += regno + 1;
2060 break;
2061 case PHY_DELAY_MS:
2062 mdelay(data);
2063 index++;
2064 break;
2065
2066 case PHY_READ_MAC_BYTE:
2067 case PHY_WRITE_MAC_BYTE:
2068 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002069 default:
2070 BUG();
2071 }
2072 }
2073}
2074
françois romieuf1e02ed2011-01-13 13:07:53 +00002075static void rtl_release_firmware(struct rtl8169_private *tp)
2076{
Francois Romieub6ffd972011-06-17 17:00:05 +02002077 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2078 release_firmware(tp->rtl_fw->fw);
2079 kfree(tp->rtl_fw);
2080 }
2081 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002082}
2083
François Romieu953a12c2011-04-24 17:38:48 +02002084static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002085{
Francois Romieub6ffd972011-06-17 17:00:05 +02002086 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002087
2088 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002089 if (!IS_ERR_OR_NULL(rtl_fw))
2090 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002091}
2092
2093static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2094{
2095 if (rtl_readphy(tp, reg) != val)
2096 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2097 else
2098 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002099}
2100
françois romieu4da19632011-01-03 15:07:55 +00002101static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002103 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002104 { 0x1f, 0x0001 },
2105 { 0x06, 0x006e },
2106 { 0x08, 0x0708 },
2107 { 0x15, 0x4000 },
2108 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
françois romieu0b9b5712009-08-10 19:44:56 +00002110 { 0x1f, 0x0001 },
2111 { 0x03, 0x00a1 },
2112 { 0x02, 0x0008 },
2113 { 0x01, 0x0120 },
2114 { 0x00, 0x1000 },
2115 { 0x04, 0x0800 },
2116 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
françois romieu0b9b5712009-08-10 19:44:56 +00002118 { 0x03, 0xff41 },
2119 { 0x02, 0xdf60 },
2120 { 0x01, 0x0140 },
2121 { 0x00, 0x0077 },
2122 { 0x04, 0x7800 },
2123 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
françois romieu0b9b5712009-08-10 19:44:56 +00002125 { 0x03, 0x802f },
2126 { 0x02, 0x4f02 },
2127 { 0x01, 0x0409 },
2128 { 0x00, 0xf0f9 },
2129 { 0x04, 0x9800 },
2130 { 0x04, 0x9000 },
2131
2132 { 0x03, 0xdf01 },
2133 { 0x02, 0xdf20 },
2134 { 0x01, 0xff95 },
2135 { 0x00, 0xba00 },
2136 { 0x04, 0xa800 },
2137 { 0x04, 0xa000 },
2138
2139 { 0x03, 0xff41 },
2140 { 0x02, 0xdf20 },
2141 { 0x01, 0x0140 },
2142 { 0x00, 0x00bb },
2143 { 0x04, 0xb800 },
2144 { 0x04, 0xb000 },
2145
2146 { 0x03, 0xdf41 },
2147 { 0x02, 0xdc60 },
2148 { 0x01, 0x6340 },
2149 { 0x00, 0x007d },
2150 { 0x04, 0xd800 },
2151 { 0x04, 0xd000 },
2152
2153 { 0x03, 0xdf01 },
2154 { 0x02, 0xdf20 },
2155 { 0x01, 0x100a },
2156 { 0x00, 0xa0ff },
2157 { 0x04, 0xf800 },
2158 { 0x04, 0xf000 },
2159
2160 { 0x1f, 0x0000 },
2161 { 0x0b, 0x0000 },
2162 { 0x00, 0x9200 }
2163 };
2164
françois romieu4da19632011-01-03 15:07:55 +00002165 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
françois romieu4da19632011-01-03 15:07:55 +00002168static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002169{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002170 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002171 { 0x1f, 0x0002 },
2172 { 0x01, 0x90d0 },
2173 { 0x1f, 0x0000 }
2174 };
2175
françois romieu4da19632011-01-03 15:07:55 +00002176 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002177}
2178
françois romieu4da19632011-01-03 15:07:55 +00002179static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002180{
2181 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002182
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002183 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2184 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002185 return;
2186
françois romieu4da19632011-01-03 15:07:55 +00002187 rtl_writephy(tp, 0x1f, 0x0001);
2188 rtl_writephy(tp, 0x10, 0xf01b);
2189 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002190}
2191
françois romieu4da19632011-01-03 15:07:55 +00002192static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002193{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002194 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002195 { 0x1f, 0x0001 },
2196 { 0x04, 0x0000 },
2197 { 0x03, 0x00a1 },
2198 { 0x02, 0x0008 },
2199 { 0x01, 0x0120 },
2200 { 0x00, 0x1000 },
2201 { 0x04, 0x0800 },
2202 { 0x04, 0x9000 },
2203 { 0x03, 0x802f },
2204 { 0x02, 0x4f02 },
2205 { 0x01, 0x0409 },
2206 { 0x00, 0xf099 },
2207 { 0x04, 0x9800 },
2208 { 0x04, 0xa000 },
2209 { 0x03, 0xdf01 },
2210 { 0x02, 0xdf20 },
2211 { 0x01, 0xff95 },
2212 { 0x00, 0xba00 },
2213 { 0x04, 0xa800 },
2214 { 0x04, 0xf000 },
2215 { 0x03, 0xdf01 },
2216 { 0x02, 0xdf20 },
2217 { 0x01, 0x101a },
2218 { 0x00, 0xa0ff },
2219 { 0x04, 0xf800 },
2220 { 0x04, 0x0000 },
2221 { 0x1f, 0x0000 },
2222
2223 { 0x1f, 0x0001 },
2224 { 0x10, 0xf41b },
2225 { 0x14, 0xfb54 },
2226 { 0x18, 0xf5c7 },
2227 { 0x1f, 0x0000 },
2228
2229 { 0x1f, 0x0001 },
2230 { 0x17, 0x0cc0 },
2231 { 0x1f, 0x0000 }
2232 };
2233
françois romieu4da19632011-01-03 15:07:55 +00002234 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002235
françois romieu4da19632011-01-03 15:07:55 +00002236 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002237}
2238
françois romieu4da19632011-01-03 15:07:55 +00002239static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002240{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002241 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002242 { 0x1f, 0x0001 },
2243 { 0x04, 0x0000 },
2244 { 0x03, 0x00a1 },
2245 { 0x02, 0x0008 },
2246 { 0x01, 0x0120 },
2247 { 0x00, 0x1000 },
2248 { 0x04, 0x0800 },
2249 { 0x04, 0x9000 },
2250 { 0x03, 0x802f },
2251 { 0x02, 0x4f02 },
2252 { 0x01, 0x0409 },
2253 { 0x00, 0xf099 },
2254 { 0x04, 0x9800 },
2255 { 0x04, 0xa000 },
2256 { 0x03, 0xdf01 },
2257 { 0x02, 0xdf20 },
2258 { 0x01, 0xff95 },
2259 { 0x00, 0xba00 },
2260 { 0x04, 0xa800 },
2261 { 0x04, 0xf000 },
2262 { 0x03, 0xdf01 },
2263 { 0x02, 0xdf20 },
2264 { 0x01, 0x101a },
2265 { 0x00, 0xa0ff },
2266 { 0x04, 0xf800 },
2267 { 0x04, 0x0000 },
2268 { 0x1f, 0x0000 },
2269
2270 { 0x1f, 0x0001 },
2271 { 0x0b, 0x8480 },
2272 { 0x1f, 0x0000 },
2273
2274 { 0x1f, 0x0001 },
2275 { 0x18, 0x67c7 },
2276 { 0x04, 0x2000 },
2277 { 0x03, 0x002f },
2278 { 0x02, 0x4360 },
2279 { 0x01, 0x0109 },
2280 { 0x00, 0x3022 },
2281 { 0x04, 0x2800 },
2282 { 0x1f, 0x0000 },
2283
2284 { 0x1f, 0x0001 },
2285 { 0x17, 0x0cc0 },
2286 { 0x1f, 0x0000 }
2287 };
2288
françois romieu4da19632011-01-03 15:07:55 +00002289 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002290}
2291
françois romieu4da19632011-01-03 15:07:55 +00002292static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002293{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002294 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002295 { 0x10, 0xf41b },
2296 { 0x1f, 0x0000 }
2297 };
2298
françois romieu4da19632011-01-03 15:07:55 +00002299 rtl_writephy(tp, 0x1f, 0x0001);
2300 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002301
françois romieu4da19632011-01-03 15:07:55 +00002302 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002303}
2304
françois romieu4da19632011-01-03 15:07:55 +00002305static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002306{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002307 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002308 { 0x1f, 0x0001 },
2309 { 0x10, 0xf41b },
2310 { 0x1f, 0x0000 }
2311 };
2312
françois romieu4da19632011-01-03 15:07:55 +00002313 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002314}
2315
françois romieu4da19632011-01-03 15:07:55 +00002316static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002317{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002318 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002319 { 0x1f, 0x0000 },
2320 { 0x1d, 0x0f00 },
2321 { 0x1f, 0x0002 },
2322 { 0x0c, 0x1ec8 },
2323 { 0x1f, 0x0000 }
2324 };
2325
françois romieu4da19632011-01-03 15:07:55 +00002326 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002327}
2328
françois romieu4da19632011-01-03 15:07:55 +00002329static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002330{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002331 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002332 { 0x1f, 0x0001 },
2333 { 0x1d, 0x3d98 },
2334 { 0x1f, 0x0000 }
2335 };
2336
françois romieu4da19632011-01-03 15:07:55 +00002337 rtl_writephy(tp, 0x1f, 0x0000);
2338 rtl_patchphy(tp, 0x14, 1 << 5);
2339 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002340
françois romieu4da19632011-01-03 15:07:55 +00002341 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002342}
2343
françois romieu4da19632011-01-03 15:07:55 +00002344static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002345{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002346 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002347 { 0x1f, 0x0001 },
2348 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002349 { 0x1f, 0x0002 },
2350 { 0x00, 0x88d4 },
2351 { 0x01, 0x82b1 },
2352 { 0x03, 0x7002 },
2353 { 0x08, 0x9e30 },
2354 { 0x09, 0x01f0 },
2355 { 0x0a, 0x5500 },
2356 { 0x0c, 0x00c8 },
2357 { 0x1f, 0x0003 },
2358 { 0x12, 0xc096 },
2359 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002360 { 0x1f, 0x0000 },
2361 { 0x1f, 0x0000 },
2362 { 0x09, 0x2000 },
2363 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002364 };
2365
françois romieu4da19632011-01-03 15:07:55 +00002366 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002367
françois romieu4da19632011-01-03 15:07:55 +00002368 rtl_patchphy(tp, 0x14, 1 << 5);
2369 rtl_patchphy(tp, 0x0d, 1 << 5);
2370 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002371}
2372
françois romieu4da19632011-01-03 15:07:55 +00002373static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002374{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002375 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002376 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002377 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002378 { 0x03, 0x802f },
2379 { 0x02, 0x4f02 },
2380 { 0x01, 0x0409 },
2381 { 0x00, 0xf099 },
2382 { 0x04, 0x9800 },
2383 { 0x04, 0x9000 },
2384 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002385 { 0x1f, 0x0002 },
2386 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002387 { 0x06, 0x0761 },
2388 { 0x1f, 0x0003 },
2389 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002390 { 0x1f, 0x0000 }
2391 };
2392
françois romieu4da19632011-01-03 15:07:55 +00002393 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002394
françois romieu4da19632011-01-03 15:07:55 +00002395 rtl_patchphy(tp, 0x16, 1 << 0);
2396 rtl_patchphy(tp, 0x14, 1 << 5);
2397 rtl_patchphy(tp, 0x0d, 1 << 5);
2398 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002399}
2400
françois romieu4da19632011-01-03 15:07:55 +00002401static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002402{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002403 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002404 { 0x1f, 0x0001 },
2405 { 0x12, 0x2300 },
2406 { 0x1d, 0x3d98 },
2407 { 0x1f, 0x0002 },
2408 { 0x0c, 0x7eb8 },
2409 { 0x06, 0x5461 },
2410 { 0x1f, 0x0003 },
2411 { 0x16, 0x0f0a },
2412 { 0x1f, 0x0000 }
2413 };
2414
françois romieu4da19632011-01-03 15:07:55 +00002415 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002416
françois romieu4da19632011-01-03 15:07:55 +00002417 rtl_patchphy(tp, 0x16, 1 << 0);
2418 rtl_patchphy(tp, 0x14, 1 << 5);
2419 rtl_patchphy(tp, 0x0d, 1 << 5);
2420 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002421}
2422
françois romieu4da19632011-01-03 15:07:55 +00002423static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002424{
françois romieu4da19632011-01-03 15:07:55 +00002425 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002426}
2427
françois romieubca03d52011-01-03 15:07:31 +00002428static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002429{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002430 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002431 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002432 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002433 { 0x06, 0x4064 },
2434 { 0x07, 0x2863 },
2435 { 0x08, 0x059c },
2436 { 0x09, 0x26b4 },
2437 { 0x0a, 0x6a19 },
2438 { 0x0b, 0xdcc8 },
2439 { 0x10, 0xf06d },
2440 { 0x14, 0x7f68 },
2441 { 0x18, 0x7fd9 },
2442 { 0x1c, 0xf0ff },
2443 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002444 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002445 { 0x12, 0xf49f },
2446 { 0x13, 0x070b },
2447 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002448 { 0x14, 0x94c0 },
2449
2450 /*
2451 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002452 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002453 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002454 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002455 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002456 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002457 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002458 { 0x06, 0x5561 },
2459
2460 /*
2461 * Can not link to 1Gbps with bad cable
2462 * Decrease SNR threshold form 21.07dB to 19.04dB
2463 */
2464 { 0x1f, 0x0001 },
2465 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002466
2467 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002468 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002469 };
françois romieubca03d52011-01-03 15:07:31 +00002470 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002471
françois romieu4da19632011-01-03 15:07:55 +00002472 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002473
françois romieubca03d52011-01-03 15:07:31 +00002474 /*
2475 * Rx Error Issue
2476 * Fine Tune Switching regulator parameter
2477 */
françois romieu4da19632011-01-03 15:07:55 +00002478 rtl_writephy(tp, 0x1f, 0x0002);
2479 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2480 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002481
françois romieudaf9df62009-10-07 12:44:20 +00002482 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002483 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002484 { 0x1f, 0x0002 },
2485 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002486 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002487 { 0x05, 0x8330 },
2488 { 0x06, 0x669a },
2489 { 0x1f, 0x0002 }
2490 };
2491 int val;
2492
françois romieu4da19632011-01-03 15:07:55 +00002493 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002494
françois romieu4da19632011-01-03 15:07:55 +00002495 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002496
2497 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002498 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002499 0x0065, 0x0066, 0x0067, 0x0068,
2500 0x0069, 0x006a, 0x006b, 0x006c
2501 };
2502 int i;
2503
françois romieu4da19632011-01-03 15:07:55 +00002504 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002505
2506 val &= 0xff00;
2507 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002508 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002509 }
2510 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002511 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002512 { 0x1f, 0x0002 },
2513 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002514 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002515 { 0x05, 0x8330 },
2516 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002517 };
2518
françois romieu4da19632011-01-03 15:07:55 +00002519 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002520 }
2521
françois romieubca03d52011-01-03 15:07:31 +00002522 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002523 rtl_writephy(tp, 0x1f, 0x0002);
2524 rtl_patchphy(tp, 0x0d, 0x0300);
2525 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002526
françois romieubca03d52011-01-03 15:07:31 +00002527 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002528 rtl_writephy(tp, 0x1f, 0x0002);
2529 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2530 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002531
françois romieu4da19632011-01-03 15:07:55 +00002532 rtl_writephy(tp, 0x1f, 0x0005);
2533 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002534
2535 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002536
françois romieu4da19632011-01-03 15:07:55 +00002537 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002538}
2539
françois romieubca03d52011-01-03 15:07:31 +00002540static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002541{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002542 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002543 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002544 { 0x1f, 0x0001 },
2545 { 0x06, 0x4064 },
2546 { 0x07, 0x2863 },
2547 { 0x08, 0x059c },
2548 { 0x09, 0x26b4 },
2549 { 0x0a, 0x6a19 },
2550 { 0x0b, 0xdcc8 },
2551 { 0x10, 0xf06d },
2552 { 0x14, 0x7f68 },
2553 { 0x18, 0x7fd9 },
2554 { 0x1c, 0xf0ff },
2555 { 0x1d, 0x3d9c },
2556 { 0x1f, 0x0003 },
2557 { 0x12, 0xf49f },
2558 { 0x13, 0x070b },
2559 { 0x1a, 0x05ad },
2560 { 0x14, 0x94c0 },
2561
françois romieubca03d52011-01-03 15:07:31 +00002562 /*
2563 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002564 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002565 */
françois romieudaf9df62009-10-07 12:44:20 +00002566 { 0x1f, 0x0002 },
2567 { 0x06, 0x5561 },
2568 { 0x1f, 0x0005 },
2569 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002570 { 0x06, 0x5561 },
2571
2572 /*
2573 * Can not link to 1Gbps with bad cable
2574 * Decrease SNR threshold form 21.07dB to 19.04dB
2575 */
2576 { 0x1f, 0x0001 },
2577 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002578
2579 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002580 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002581 };
françois romieubca03d52011-01-03 15:07:31 +00002582 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002583
françois romieu4da19632011-01-03 15:07:55 +00002584 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002585
2586 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002587 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002588 { 0x1f, 0x0002 },
2589 { 0x05, 0x669a },
2590 { 0x1f, 0x0005 },
2591 { 0x05, 0x8330 },
2592 { 0x06, 0x669a },
2593
2594 { 0x1f, 0x0002 }
2595 };
2596 int val;
2597
françois romieu4da19632011-01-03 15:07:55 +00002598 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002599
françois romieu4da19632011-01-03 15:07:55 +00002600 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002601 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002602 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002603 0x0065, 0x0066, 0x0067, 0x0068,
2604 0x0069, 0x006a, 0x006b, 0x006c
2605 };
2606 int i;
2607
françois romieu4da19632011-01-03 15:07:55 +00002608 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002609
2610 val &= 0xff00;
2611 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002612 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002613 }
2614 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002615 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002616 { 0x1f, 0x0002 },
2617 { 0x05, 0x2642 },
2618 { 0x1f, 0x0005 },
2619 { 0x05, 0x8330 },
2620 { 0x06, 0x2642 }
2621 };
2622
françois romieu4da19632011-01-03 15:07:55 +00002623 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002624 }
2625
françois romieubca03d52011-01-03 15:07:31 +00002626 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002627 rtl_writephy(tp, 0x1f, 0x0002);
2628 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2629 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002630
françois romieubca03d52011-01-03 15:07:31 +00002631 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002632 rtl_writephy(tp, 0x1f, 0x0002);
2633 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002634
françois romieu4da19632011-01-03 15:07:55 +00002635 rtl_writephy(tp, 0x1f, 0x0005);
2636 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002637
2638 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002639
françois romieu4da19632011-01-03 15:07:55 +00002640 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002641}
2642
françois romieu4da19632011-01-03 15:07:55 +00002643static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002644{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002645 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002646 { 0x1f, 0x0002 },
2647 { 0x10, 0x0008 },
2648 { 0x0d, 0x006c },
2649
2650 { 0x1f, 0x0000 },
2651 { 0x0d, 0xf880 },
2652
2653 { 0x1f, 0x0001 },
2654 { 0x17, 0x0cc0 },
2655
2656 { 0x1f, 0x0001 },
2657 { 0x0b, 0xa4d8 },
2658 { 0x09, 0x281c },
2659 { 0x07, 0x2883 },
2660 { 0x0a, 0x6b35 },
2661 { 0x1d, 0x3da4 },
2662 { 0x1c, 0xeffd },
2663 { 0x14, 0x7f52 },
2664 { 0x18, 0x7fc6 },
2665 { 0x08, 0x0601 },
2666 { 0x06, 0x4063 },
2667 { 0x10, 0xf074 },
2668 { 0x1f, 0x0003 },
2669 { 0x13, 0x0789 },
2670 { 0x12, 0xf4bd },
2671 { 0x1a, 0x04fd },
2672 { 0x14, 0x84b0 },
2673 { 0x1f, 0x0000 },
2674 { 0x00, 0x9200 },
2675
2676 { 0x1f, 0x0005 },
2677 { 0x01, 0x0340 },
2678 { 0x1f, 0x0001 },
2679 { 0x04, 0x4000 },
2680 { 0x03, 0x1d21 },
2681 { 0x02, 0x0c32 },
2682 { 0x01, 0x0200 },
2683 { 0x00, 0x5554 },
2684 { 0x04, 0x4800 },
2685 { 0x04, 0x4000 },
2686 { 0x04, 0xf000 },
2687 { 0x03, 0xdf01 },
2688 { 0x02, 0xdf20 },
2689 { 0x01, 0x101a },
2690 { 0x00, 0xa0ff },
2691 { 0x04, 0xf800 },
2692 { 0x04, 0xf000 },
2693 { 0x1f, 0x0000 },
2694
2695 { 0x1f, 0x0007 },
2696 { 0x1e, 0x0023 },
2697 { 0x16, 0x0000 },
2698 { 0x1f, 0x0000 }
2699 };
2700
françois romieu4da19632011-01-03 15:07:55 +00002701 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002702}
2703
françois romieue6de30d2011-01-03 15:08:37 +00002704static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2705{
2706 static const struct phy_reg phy_reg_init[] = {
2707 { 0x1f, 0x0001 },
2708 { 0x17, 0x0cc0 },
2709
2710 { 0x1f, 0x0007 },
2711 { 0x1e, 0x002d },
2712 { 0x18, 0x0040 },
2713 { 0x1f, 0x0000 }
2714 };
2715
2716 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2717 rtl_patchphy(tp, 0x0d, 1 << 5);
2718}
2719
Hayes Wang70090422011-07-06 15:58:06 +08002720static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002721{
2722 static const struct phy_reg phy_reg_init[] = {
2723 /* Enable Delay cap */
2724 { 0x1f, 0x0005 },
2725 { 0x05, 0x8b80 },
2726 { 0x06, 0xc896 },
2727 { 0x1f, 0x0000 },
2728
2729 /* Channel estimation fine tune */
2730 { 0x1f, 0x0001 },
2731 { 0x0b, 0x6c20 },
2732 { 0x07, 0x2872 },
2733 { 0x1c, 0xefff },
2734 { 0x1f, 0x0003 },
2735 { 0x14, 0x6420 },
2736 { 0x1f, 0x0000 },
2737
2738 /* Update PFM & 10M TX idle timer */
2739 { 0x1f, 0x0007 },
2740 { 0x1e, 0x002f },
2741 { 0x15, 0x1919 },
2742 { 0x1f, 0x0000 },
2743
2744 { 0x1f, 0x0007 },
2745 { 0x1e, 0x00ac },
2746 { 0x18, 0x0006 },
2747 { 0x1f, 0x0000 }
2748 };
2749
Francois Romieu15ecd032011-04-27 13:52:22 -07002750 rtl_apply_firmware(tp);
2751
hayeswang01dc7fe2011-03-21 01:50:28 +00002752 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2753
2754 /* DCO enable for 10M IDLE Power */
2755 rtl_writephy(tp, 0x1f, 0x0007);
2756 rtl_writephy(tp, 0x1e, 0x0023);
2757 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2758 rtl_writephy(tp, 0x1f, 0x0000);
2759
2760 /* For impedance matching */
2761 rtl_writephy(tp, 0x1f, 0x0002);
2762 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002763 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002764
2765 /* PHY auto speed down */
2766 rtl_writephy(tp, 0x1f, 0x0007);
2767 rtl_writephy(tp, 0x1e, 0x002d);
2768 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2769 rtl_writephy(tp, 0x1f, 0x0000);
2770 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2771
2772 rtl_writephy(tp, 0x1f, 0x0005);
2773 rtl_writephy(tp, 0x05, 0x8b86);
2774 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2775 rtl_writephy(tp, 0x1f, 0x0000);
2776
2777 rtl_writephy(tp, 0x1f, 0x0005);
2778 rtl_writephy(tp, 0x05, 0x8b85);
2779 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2780 rtl_writephy(tp, 0x1f, 0x0007);
2781 rtl_writephy(tp, 0x1e, 0x0020);
2782 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2783 rtl_writephy(tp, 0x1f, 0x0006);
2784 rtl_writephy(tp, 0x00, 0x5a00);
2785 rtl_writephy(tp, 0x1f, 0x0000);
2786 rtl_writephy(tp, 0x0d, 0x0007);
2787 rtl_writephy(tp, 0x0e, 0x003c);
2788 rtl_writephy(tp, 0x0d, 0x4007);
2789 rtl_writephy(tp, 0x0e, 0x0000);
2790 rtl_writephy(tp, 0x0d, 0x0000);
2791}
2792
Hayes Wang70090422011-07-06 15:58:06 +08002793static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2794{
2795 static const struct phy_reg phy_reg_init[] = {
2796 /* Enable Delay cap */
2797 { 0x1f, 0x0004 },
2798 { 0x1f, 0x0007 },
2799 { 0x1e, 0x00ac },
2800 { 0x18, 0x0006 },
2801 { 0x1f, 0x0002 },
2802 { 0x1f, 0x0000 },
2803 { 0x1f, 0x0000 },
2804
2805 /* Channel estimation fine tune */
2806 { 0x1f, 0x0003 },
2807 { 0x09, 0xa20f },
2808 { 0x1f, 0x0000 },
2809 { 0x1f, 0x0000 },
2810
2811 /* Green Setting */
2812 { 0x1f, 0x0005 },
2813 { 0x05, 0x8b5b },
2814 { 0x06, 0x9222 },
2815 { 0x05, 0x8b6d },
2816 { 0x06, 0x8000 },
2817 { 0x05, 0x8b76 },
2818 { 0x06, 0x8000 },
2819 { 0x1f, 0x0000 }
2820 };
2821
2822 rtl_apply_firmware(tp);
2823
2824 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2825
2826 /* For 4-corner performance improve */
2827 rtl_writephy(tp, 0x1f, 0x0005);
2828 rtl_writephy(tp, 0x05, 0x8b80);
2829 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2830 rtl_writephy(tp, 0x1f, 0x0000);
2831
2832 /* PHY auto speed down */
2833 rtl_writephy(tp, 0x1f, 0x0004);
2834 rtl_writephy(tp, 0x1f, 0x0007);
2835 rtl_writephy(tp, 0x1e, 0x002d);
2836 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2837 rtl_writephy(tp, 0x1f, 0x0002);
2838 rtl_writephy(tp, 0x1f, 0x0000);
2839 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2840
2841 /* improve 10M EEE waveform */
2842 rtl_writephy(tp, 0x1f, 0x0005);
2843 rtl_writephy(tp, 0x05, 0x8b86);
2844 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2845 rtl_writephy(tp, 0x1f, 0x0000);
2846
2847 /* Improve 2-pair detection performance */
2848 rtl_writephy(tp, 0x1f, 0x0005);
2849 rtl_writephy(tp, 0x05, 0x8b85);
2850 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2851 rtl_writephy(tp, 0x1f, 0x0000);
2852
2853 /* EEE setting */
2854 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2855 ERIAR_EXGMAC);
2856 rtl_writephy(tp, 0x1f, 0x0005);
2857 rtl_writephy(tp, 0x05, 0x8b85);
2858 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2859 rtl_writephy(tp, 0x1f, 0x0004);
2860 rtl_writephy(tp, 0x1f, 0x0007);
2861 rtl_writephy(tp, 0x1e, 0x0020);
2862 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
2863 rtl_writephy(tp, 0x1f, 0x0002);
2864 rtl_writephy(tp, 0x1f, 0x0000);
2865 rtl_writephy(tp, 0x0d, 0x0007);
2866 rtl_writephy(tp, 0x0e, 0x003c);
2867 rtl_writephy(tp, 0x0d, 0x4007);
2868 rtl_writephy(tp, 0x0e, 0x0000);
2869 rtl_writephy(tp, 0x0d, 0x0000);
2870
2871 /* Green feature */
2872 rtl_writephy(tp, 0x1f, 0x0003);
2873 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2874 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2875 rtl_writephy(tp, 0x1f, 0x0000);
2876}
2877
françois romieu4da19632011-01-03 15:07:55 +00002878static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002879{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002880 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002881 { 0x1f, 0x0003 },
2882 { 0x08, 0x441d },
2883 { 0x01, 0x9100 },
2884 { 0x1f, 0x0000 }
2885 };
2886
françois romieu4da19632011-01-03 15:07:55 +00002887 rtl_writephy(tp, 0x1f, 0x0000);
2888 rtl_patchphy(tp, 0x11, 1 << 12);
2889 rtl_patchphy(tp, 0x19, 1 << 13);
2890 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002891
françois romieu4da19632011-01-03 15:07:55 +00002892 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002893}
2894
Hayes Wang5a5e4442011-02-22 17:26:21 +08002895static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2896{
2897 static const struct phy_reg phy_reg_init[] = {
2898 { 0x1f, 0x0005 },
2899 { 0x1a, 0x0000 },
2900 { 0x1f, 0x0000 },
2901
2902 { 0x1f, 0x0004 },
2903 { 0x1c, 0x0000 },
2904 { 0x1f, 0x0000 },
2905
2906 { 0x1f, 0x0001 },
2907 { 0x15, 0x7701 },
2908 { 0x1f, 0x0000 }
2909 };
2910
2911 /* Disable ALDPS before ram code */
2912 rtl_writephy(tp, 0x1f, 0x0000);
2913 rtl_writephy(tp, 0x18, 0x0310);
2914 msleep(100);
2915
François Romieu953a12c2011-04-24 17:38:48 +02002916 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002917
2918 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2919}
2920
Francois Romieu5615d9f2007-08-17 17:50:46 +02002921static void rtl_hw_phy_config(struct net_device *dev)
2922{
2923 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002924
2925 rtl8169_print_mac_version(tp);
2926
2927 switch (tp->mac_version) {
2928 case RTL_GIGA_MAC_VER_01:
2929 break;
2930 case RTL_GIGA_MAC_VER_02:
2931 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002932 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002933 break;
2934 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002935 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002936 break;
françois romieu2e9558562009-08-10 19:44:19 +00002937 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002938 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002939 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002940 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002941 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002942 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002943 case RTL_GIGA_MAC_VER_07:
2944 case RTL_GIGA_MAC_VER_08:
2945 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002946 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002947 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002948 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002949 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002950 break;
2951 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002952 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002953 break;
2954 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002955 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002956 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002957 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002958 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002959 break;
2960 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002961 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002962 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002963 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002964 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002965 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002966 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002967 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002968 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002969 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002970 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002971 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002972 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002973 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002974 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002975 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002976 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002977 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002978 break;
2979 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002980 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002981 break;
2982 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002983 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002984 break;
françois romieue6de30d2011-01-03 15:08:37 +00002985 case RTL_GIGA_MAC_VER_28:
2986 rtl8168d_4_hw_phy_config(tp);
2987 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002988 case RTL_GIGA_MAC_VER_29:
2989 case RTL_GIGA_MAC_VER_30:
2990 rtl8105e_hw_phy_config(tp);
2991 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002992 case RTL_GIGA_MAC_VER_31:
2993 /* None. */
2994 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002995 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002996 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08002997 rtl8168e_1_hw_phy_config(tp);
2998 break;
2999 case RTL_GIGA_MAC_VER_34:
3000 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003001 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003002
Francois Romieu5615d9f2007-08-17 17:50:46 +02003003 default:
3004 break;
3005 }
3006}
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008static void rtl8169_phy_timer(unsigned long __opaque)
3009{
3010 struct net_device *dev = (struct net_device *)__opaque;
3011 struct rtl8169_private *tp = netdev_priv(dev);
3012 struct timer_list *timer = &tp->timer;
3013 void __iomem *ioaddr = tp->mmio_addr;
3014 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3015
Francois Romieubcf0bf92006-07-26 23:14:13 +02003016 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 spin_lock_irq(&tp->lock);
3019
françois romieu4da19632011-01-03 15:07:55 +00003020 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003021 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 * A busy loop could burn quite a few cycles on nowadays CPU.
3023 * Let's delay the execution of the timer for a few ticks.
3024 */
3025 timeout = HZ/10;
3026 goto out_mod_timer;
3027 }
3028
3029 if (tp->link_ok(ioaddr))
3030 goto out_unlock;
3031
Joe Perchesbf82c182010-02-09 11:49:50 +00003032 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
françois romieu4da19632011-01-03 15:07:55 +00003034 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
3036out_mod_timer:
3037 mod_timer(timer, jiffies + timeout);
3038out_unlock:
3039 spin_unlock_irq(&tp->lock);
3040}
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042#ifdef CONFIG_NET_POLL_CONTROLLER
3043/*
3044 * Polling 'interrupt' - used by things like netconsole to send skbs
3045 * without having to re-enable interrupts. It's not called while
3046 * the interrupt routine is executing.
3047 */
3048static void rtl8169_netpoll(struct net_device *dev)
3049{
3050 struct rtl8169_private *tp = netdev_priv(dev);
3051 struct pci_dev *pdev = tp->pci_dev;
3052
3053 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003054 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 enable_irq(pdev->irq);
3056}
3057#endif
3058
3059static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3060 void __iomem *ioaddr)
3061{
3062 iounmap(ioaddr);
3063 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003064 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 pci_disable_device(pdev);
3066 free_netdev(dev);
3067}
3068
Francois Romieubf793292006-11-01 00:53:05 +01003069static void rtl8169_phy_reset(struct net_device *dev,
3070 struct rtl8169_private *tp)
3071{
Francois Romieu07d3f512007-02-21 22:40:46 +01003072 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003073
françois romieu4da19632011-01-03 15:07:55 +00003074 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003075 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003076 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003077 return;
3078 msleep(1);
3079 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003080 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003081}
3082
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003083static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003085 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003086
Francois Romieu5615d9f2007-08-17 17:50:46 +02003087 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003088
Marcus Sundberg773328942008-07-10 21:28:08 +02003089 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3090 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3091 RTL_W8(0x82, 0x01);
3092 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003093
Francois Romieu6dccd162007-02-13 23:38:05 +01003094 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3095
3096 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3097 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003098
Francois Romieubcf0bf92006-07-26 23:14:13 +02003099 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003100 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3101 RTL_W8(0x82, 0x01);
3102 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003103 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003104 }
3105
Francois Romieubf793292006-11-01 00:53:05 +01003106 rtl8169_phy_reset(dev, tp);
3107
Oliver Neukum54405cd2011-01-06 21:55:13 +01003108 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003109 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3110 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3111 (tp->mii.supports_gmii ?
3112 ADVERTISED_1000baseT_Half |
3113 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003114
Joe Perchesbf82c182010-02-09 11:49:50 +00003115 if (RTL_R8(PHYstatus) & TBI_Enable)
3116 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003117}
3118
Francois Romieu773d2022007-01-31 23:47:43 +01003119static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3120{
3121 void __iomem *ioaddr = tp->mmio_addr;
3122 u32 high;
3123 u32 low;
3124
3125 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3126 high = addr[4] | (addr[5] << 8);
3127
3128 spin_lock_irq(&tp->lock);
3129
3130 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003131
Francois Romieu773d2022007-01-31 23:47:43 +01003132 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003133 RTL_R32(MAC4);
3134
Francois Romieu78f1cd02010-03-27 19:35:46 -07003135 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003136 RTL_R32(MAC0);
3137
françois romieuc28aa382011-08-02 03:53:43 +00003138 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3139 const struct exgmac_reg e[] = {
3140 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3141 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3142 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3143 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3144 low >> 16 },
3145 };
3146
3147 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3148 }
3149
Francois Romieu773d2022007-01-31 23:47:43 +01003150 RTL_W8(Cfg9346, Cfg9346_Lock);
3151
3152 spin_unlock_irq(&tp->lock);
3153}
3154
3155static int rtl_set_mac_address(struct net_device *dev, void *p)
3156{
3157 struct rtl8169_private *tp = netdev_priv(dev);
3158 struct sockaddr *addr = p;
3159
3160 if (!is_valid_ether_addr(addr->sa_data))
3161 return -EADDRNOTAVAIL;
3162
3163 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3164
3165 rtl_rar_set(tp, dev->dev_addr);
3166
3167 return 0;
3168}
3169
Francois Romieu5f787a12006-08-17 13:02:36 +02003170static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3171{
3172 struct rtl8169_private *tp = netdev_priv(dev);
3173 struct mii_ioctl_data *data = if_mii(ifr);
3174
Francois Romieu8b4ab282008-11-19 22:05:25 -08003175 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3176}
Francois Romieu5f787a12006-08-17 13:02:36 +02003177
Francois Romieucecb5fd2011-04-01 10:21:07 +02003178static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3179 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003180{
Francois Romieu5f787a12006-08-17 13:02:36 +02003181 switch (cmd) {
3182 case SIOCGMIIPHY:
3183 data->phy_id = 32; /* Internal PHY */
3184 return 0;
3185
3186 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003187 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003188 return 0;
3189
3190 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003191 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003192 return 0;
3193 }
3194 return -EOPNOTSUPP;
3195}
3196
Francois Romieu8b4ab282008-11-19 22:05:25 -08003197static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3198{
3199 return -EOPNOTSUPP;
3200}
3201
Francois Romieu0e485152007-02-20 00:00:26 +01003202static const struct rtl_cfg_info {
3203 void (*hw_start)(struct net_device *);
3204 unsigned int region;
3205 unsigned int align;
3206 u16 intr_event;
3207 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003208 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003209 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003210} rtl_cfg_infos [] = {
3211 [RTL_CFG_0] = {
3212 .hw_start = rtl_hw_start_8169,
3213 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003214 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003215 .intr_event = SYSErr | LinkChg | RxOverflow |
3216 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003217 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003218 .features = RTL_FEATURE_GMII,
3219 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003220 },
3221 [RTL_CFG_1] = {
3222 .hw_start = rtl_hw_start_8168,
3223 .region = 2,
3224 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003225 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003226 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003227 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003228 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3229 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003230 },
3231 [RTL_CFG_2] = {
3232 .hw_start = rtl_hw_start_8101,
3233 .region = 2,
3234 .align = 8,
3235 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3236 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003237 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003238 .features = RTL_FEATURE_MSI,
3239 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003240 }
3241};
3242
Francois Romieufbac58f2007-10-04 22:51:38 +02003243/* Cfg9346_Unlock assumed. */
3244static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3245 const struct rtl_cfg_info *cfg)
3246{
3247 unsigned msi = 0;
3248 u8 cfg2;
3249
3250 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003251 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003252 if (pci_enable_msi(pdev)) {
3253 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3254 } else {
3255 cfg2 |= MSIEnable;
3256 msi = RTL_FEATURE_MSI;
3257 }
3258 }
3259 RTL_W8(Config2, cfg2);
3260 return msi;
3261}
3262
3263static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3264{
3265 if (tp->features & RTL_FEATURE_MSI) {
3266 pci_disable_msi(pdev);
3267 tp->features &= ~RTL_FEATURE_MSI;
3268 }
3269}
3270
Francois Romieu8b4ab282008-11-19 22:05:25 -08003271static const struct net_device_ops rtl8169_netdev_ops = {
3272 .ndo_open = rtl8169_open,
3273 .ndo_stop = rtl8169_close,
3274 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003275 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003276 .ndo_tx_timeout = rtl8169_tx_timeout,
3277 .ndo_validate_addr = eth_validate_addr,
3278 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003279 .ndo_fix_features = rtl8169_fix_features,
3280 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003281 .ndo_set_mac_address = rtl_set_mac_address,
3282 .ndo_do_ioctl = rtl8169_ioctl,
3283 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003284#ifdef CONFIG_NET_POLL_CONTROLLER
3285 .ndo_poll_controller = rtl8169_netpoll,
3286#endif
3287
3288};
3289
françois romieuc0e45c12011-01-03 15:08:04 +00003290static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3291{
3292 struct mdio_ops *ops = &tp->mdio_ops;
3293
3294 switch (tp->mac_version) {
3295 case RTL_GIGA_MAC_VER_27:
3296 ops->write = r8168dp_1_mdio_write;
3297 ops->read = r8168dp_1_mdio_read;
3298 break;
françois romieue6de30d2011-01-03 15:08:37 +00003299 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003300 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003301 ops->write = r8168dp_2_mdio_write;
3302 ops->read = r8168dp_2_mdio_read;
3303 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003304 default:
3305 ops->write = r8169_mdio_write;
3306 ops->read = r8169_mdio_read;
3307 break;
3308 }
3309}
3310
françois romieu065c27c2011-01-03 15:08:12 +00003311static void r810x_phy_power_down(struct rtl8169_private *tp)
3312{
3313 rtl_writephy(tp, 0x1f, 0x0000);
3314 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3315}
3316
3317static void r810x_phy_power_up(struct rtl8169_private *tp)
3318{
3319 rtl_writephy(tp, 0x1f, 0x0000);
3320 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3321}
3322
3323static void r810x_pll_power_down(struct rtl8169_private *tp)
3324{
Hayes Wang10663382011-09-06 16:55:14 +08003325 void __iomem *ioaddr = tp->mmio_addr;
3326
françois romieu065c27c2011-01-03 15:08:12 +00003327 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3328 rtl_writephy(tp, 0x1f, 0x0000);
3329 rtl_writephy(tp, MII_BMCR, 0x0000);
Hayes Wang10663382011-09-06 16:55:14 +08003330
3331 if (tp->mac_version == RTL_GIGA_MAC_VER_29 ||
3332 tp->mac_version == RTL_GIGA_MAC_VER_30)
3333 RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
3334 AcceptMulticast | AcceptMyPhys);
françois romieu065c27c2011-01-03 15:08:12 +00003335 return;
3336 }
3337
3338 r810x_phy_power_down(tp);
3339}
3340
3341static void r810x_pll_power_up(struct rtl8169_private *tp)
3342{
3343 r810x_phy_power_up(tp);
3344}
3345
3346static void r8168_phy_power_up(struct rtl8169_private *tp)
3347{
3348 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003349 switch (tp->mac_version) {
3350 case RTL_GIGA_MAC_VER_11:
3351 case RTL_GIGA_MAC_VER_12:
3352 case RTL_GIGA_MAC_VER_17:
3353 case RTL_GIGA_MAC_VER_18:
3354 case RTL_GIGA_MAC_VER_19:
3355 case RTL_GIGA_MAC_VER_20:
3356 case RTL_GIGA_MAC_VER_21:
3357 case RTL_GIGA_MAC_VER_22:
3358 case RTL_GIGA_MAC_VER_23:
3359 case RTL_GIGA_MAC_VER_24:
3360 case RTL_GIGA_MAC_VER_25:
3361 case RTL_GIGA_MAC_VER_26:
3362 case RTL_GIGA_MAC_VER_27:
3363 case RTL_GIGA_MAC_VER_28:
3364 case RTL_GIGA_MAC_VER_31:
3365 rtl_writephy(tp, 0x0e, 0x0000);
3366 break;
3367 default:
3368 break;
3369 }
françois romieu065c27c2011-01-03 15:08:12 +00003370 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3371}
3372
3373static void r8168_phy_power_down(struct rtl8169_private *tp)
3374{
3375 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003376 switch (tp->mac_version) {
3377 case RTL_GIGA_MAC_VER_32:
3378 case RTL_GIGA_MAC_VER_33:
3379 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3380 break;
3381
3382 case RTL_GIGA_MAC_VER_11:
3383 case RTL_GIGA_MAC_VER_12:
3384 case RTL_GIGA_MAC_VER_17:
3385 case RTL_GIGA_MAC_VER_18:
3386 case RTL_GIGA_MAC_VER_19:
3387 case RTL_GIGA_MAC_VER_20:
3388 case RTL_GIGA_MAC_VER_21:
3389 case RTL_GIGA_MAC_VER_22:
3390 case RTL_GIGA_MAC_VER_23:
3391 case RTL_GIGA_MAC_VER_24:
3392 case RTL_GIGA_MAC_VER_25:
3393 case RTL_GIGA_MAC_VER_26:
3394 case RTL_GIGA_MAC_VER_27:
3395 case RTL_GIGA_MAC_VER_28:
3396 case RTL_GIGA_MAC_VER_31:
3397 rtl_writephy(tp, 0x0e, 0x0200);
3398 default:
3399 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3400 break;
3401 }
françois romieu065c27c2011-01-03 15:08:12 +00003402}
3403
3404static void r8168_pll_power_down(struct rtl8169_private *tp)
3405{
3406 void __iomem *ioaddr = tp->mmio_addr;
3407
Francois Romieucecb5fd2011-04-01 10:21:07 +02003408 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3409 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3410 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003411 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003412 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003413 }
françois romieu065c27c2011-01-03 15:08:12 +00003414
Francois Romieucecb5fd2011-04-01 10:21:07 +02003415 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3416 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003417 (RTL_R16(CPlusCmd) & ASF)) {
3418 return;
3419 }
3420
hayeswang01dc7fe2011-03-21 01:50:28 +00003421 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3422 tp->mac_version == RTL_GIGA_MAC_VER_33)
3423 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3424
françois romieu065c27c2011-01-03 15:08:12 +00003425 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3426 rtl_writephy(tp, 0x1f, 0x0000);
3427 rtl_writephy(tp, MII_BMCR, 0x0000);
3428
Hayes Wangd4ed95d2011-07-06 15:58:07 +08003429 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
Hayes Wang10663382011-09-06 16:55:14 +08003430 tp->mac_version == RTL_GIGA_MAC_VER_33 ||
3431 tp->mac_version == RTL_GIGA_MAC_VER_34)
Hayes Wangd4ed95d2011-07-06 15:58:07 +08003432 RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
3433 AcceptMulticast | AcceptMyPhys);
françois romieu065c27c2011-01-03 15:08:12 +00003434 return;
3435 }
3436
3437 r8168_phy_power_down(tp);
3438
3439 switch (tp->mac_version) {
3440 case RTL_GIGA_MAC_VER_25:
3441 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003442 case RTL_GIGA_MAC_VER_27:
3443 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003444 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003445 case RTL_GIGA_MAC_VER_32:
3446 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003447 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3448 break;
3449 }
3450}
3451
3452static void r8168_pll_power_up(struct rtl8169_private *tp)
3453{
3454 void __iomem *ioaddr = tp->mmio_addr;
3455
Francois Romieucecb5fd2011-04-01 10:21:07 +02003456 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3457 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3458 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003459 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003460 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003461 }
françois romieu065c27c2011-01-03 15:08:12 +00003462
3463 switch (tp->mac_version) {
3464 case RTL_GIGA_MAC_VER_25:
3465 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003466 case RTL_GIGA_MAC_VER_27:
3467 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003468 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003469 case RTL_GIGA_MAC_VER_32:
3470 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003471 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3472 break;
3473 }
3474
3475 r8168_phy_power_up(tp);
3476}
3477
3478static void rtl_pll_power_op(struct rtl8169_private *tp,
3479 void (*op)(struct rtl8169_private *))
3480{
3481 if (op)
3482 op(tp);
3483}
3484
3485static void rtl_pll_power_down(struct rtl8169_private *tp)
3486{
3487 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3488}
3489
3490static void rtl_pll_power_up(struct rtl8169_private *tp)
3491{
3492 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3493}
3494
3495static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3496{
3497 struct pll_power_ops *ops = &tp->pll_power_ops;
3498
3499 switch (tp->mac_version) {
3500 case RTL_GIGA_MAC_VER_07:
3501 case RTL_GIGA_MAC_VER_08:
3502 case RTL_GIGA_MAC_VER_09:
3503 case RTL_GIGA_MAC_VER_10:
3504 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003505 case RTL_GIGA_MAC_VER_29:
3506 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003507 ops->down = r810x_pll_power_down;
3508 ops->up = r810x_pll_power_up;
3509 break;
3510
3511 case RTL_GIGA_MAC_VER_11:
3512 case RTL_GIGA_MAC_VER_12:
3513 case RTL_GIGA_MAC_VER_17:
3514 case RTL_GIGA_MAC_VER_18:
3515 case RTL_GIGA_MAC_VER_19:
3516 case RTL_GIGA_MAC_VER_20:
3517 case RTL_GIGA_MAC_VER_21:
3518 case RTL_GIGA_MAC_VER_22:
3519 case RTL_GIGA_MAC_VER_23:
3520 case RTL_GIGA_MAC_VER_24:
3521 case RTL_GIGA_MAC_VER_25:
3522 case RTL_GIGA_MAC_VER_26:
3523 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003524 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003525 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003526 case RTL_GIGA_MAC_VER_32:
3527 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003528 case RTL_GIGA_MAC_VER_34:
françois romieu065c27c2011-01-03 15:08:12 +00003529 ops->down = r8168_pll_power_down;
3530 ops->up = r8168_pll_power_up;
3531 break;
3532
3533 default:
3534 ops->down = NULL;
3535 ops->up = NULL;
3536 break;
3537 }
3538}
3539
Hayes Wange542a222011-07-06 15:58:04 +08003540static void rtl_init_rxcfg(struct rtl8169_private *tp)
3541{
3542 void __iomem *ioaddr = tp->mmio_addr;
3543
3544 switch (tp->mac_version) {
3545 case RTL_GIGA_MAC_VER_01:
3546 case RTL_GIGA_MAC_VER_02:
3547 case RTL_GIGA_MAC_VER_03:
3548 case RTL_GIGA_MAC_VER_04:
3549 case RTL_GIGA_MAC_VER_05:
3550 case RTL_GIGA_MAC_VER_06:
3551 case RTL_GIGA_MAC_VER_10:
3552 case RTL_GIGA_MAC_VER_11:
3553 case RTL_GIGA_MAC_VER_12:
3554 case RTL_GIGA_MAC_VER_13:
3555 case RTL_GIGA_MAC_VER_14:
3556 case RTL_GIGA_MAC_VER_15:
3557 case RTL_GIGA_MAC_VER_16:
3558 case RTL_GIGA_MAC_VER_17:
3559 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3560 break;
3561 case RTL_GIGA_MAC_VER_18:
3562 case RTL_GIGA_MAC_VER_19:
3563 case RTL_GIGA_MAC_VER_20:
3564 case RTL_GIGA_MAC_VER_21:
3565 case RTL_GIGA_MAC_VER_22:
3566 case RTL_GIGA_MAC_VER_23:
3567 case RTL_GIGA_MAC_VER_24:
3568 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3569 break;
3570 default:
3571 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3572 break;
3573 }
3574}
3575
Hayes Wang92fc43b2011-07-06 15:58:03 +08003576static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3577{
3578 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3579}
3580
Francois Romieu6f43adc2011-04-29 15:05:51 +02003581static void rtl_hw_reset(struct rtl8169_private *tp)
3582{
3583 void __iomem *ioaddr = tp->mmio_addr;
3584 int i;
3585
3586 /* Soft reset the chip. */
3587 RTL_W8(ChipCmd, CmdReset);
3588
3589 /* Check that the chip has finished the reset. */
3590 for (i = 0; i < 100; i++) {
3591 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3592 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003593 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003594 }
Hayes Wang92fc43b2011-07-06 15:58:03 +08003595
3596 rtl8169_init_ring_indexes(tp);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003597}
3598
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003599static int __devinit
3600rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3601{
Francois Romieu0e485152007-02-20 00:00:26 +01003602 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3603 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003605 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003606 struct net_device *dev;
3607 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003608 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003609 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003611 if (netif_msg_drv(&debug)) {
3612 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3613 MODULENAME, RTL8169_VERSION);
3614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003617 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003618 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003619 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003620 rc = -ENOMEM;
3621 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
3623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003625 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003627 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003628 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003629 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Francois Romieuccdffb92008-07-26 14:26:06 +02003631 mii = &tp->mii;
3632 mii->dev = dev;
3633 mii->mdio_read = rtl_mdio_read;
3634 mii->mdio_write = rtl_mdio_write;
3635 mii->phy_id_mask = 0x1f;
3636 mii->reg_num_mask = 0x1f;
3637 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3638
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003639 /* disable ASPM completely as that cause random device stop working
3640 * problems as well as full system hangs for some PCIe devices users */
3641 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3642 PCIE_LINK_STATE_CLKPM);
3643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3645 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003646 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003647 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003648 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
3650
françois romieu87aeec72010-04-26 11:42:06 +00003651 if (pci_set_mwi(pdev) < 0)
3652 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003655 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003656 netif_err(tp, probe, dev,
3657 "region #%d not an MMIO resource, aborting\n",
3658 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003660 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003664 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003665 netif_err(tp, probe, dev,
3666 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003668 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 }
3670
3671 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003672 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003673 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003674 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
3676
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003677 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
3679 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003680 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 tp->cp_cmd |= PCIDAC;
3682 dev->features |= NETIF_F_HIGHDMA;
3683 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003684 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003686 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003687 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 }
3689 }
3690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003692 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003693 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003694 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003696 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003698 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Jon Masone44daad2011-06-27 07:46:31 +00003700 if (!pci_is_pcie(pdev))
3701 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07003702
Hayes Wange542a222011-07-06 15:58:04 +08003703 /* Identify chip attached to board */
3704 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
3705
3706 rtl_init_rxcfg(tp);
3707
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003708 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
Francois Romieu6f43adc2011-04-29 15:05:51 +02003710 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003712 RTL_W16(IntrStatus, 0xffff);
3713
françois romieuca52efd2009-07-24 12:34:19 +00003714 pci_set_master(pdev);
3715
Francois Romieu7a8fc772011-03-01 17:18:33 +01003716 /*
3717 * Pretend we are using VLANs; This bypasses a nasty bug where
3718 * Interrupts stop flowing on high load on 8110SCd controllers.
3719 */
3720 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3721 tp->cp_cmd |= RxVlan;
3722
françois romieuc0e45c12011-01-03 15:08:04 +00003723 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003724 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Francois Romieu85bffe62011-04-27 08:22:39 +02003728 chipset = tp->mac_version;
3729 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Francois Romieu5d06a992006-02-23 00:47:58 +01003731 RTL_W8(Cfg9346, Cfg9346_Unlock);
3732 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3733 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003734 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3735 tp->features |= RTL_FEATURE_WOL;
3736 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3737 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003738 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003739 RTL_W8(Cfg9346, Cfg9346_Lock);
3740
Francois Romieu66ec5d42007-11-06 22:56:10 +01003741 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3742 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 tp->set_speed = rtl8169_set_speed_tbi;
3744 tp->get_settings = rtl8169_gset_tbi;
3745 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3746 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3747 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003748 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 } else {
3750 tp->set_speed = rtl8169_set_speed_xmii;
3751 tp->get_settings = rtl8169_gset_xmii;
3752 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3753 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3754 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003755 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 }
3757
Francois Romieudf58ef52008-10-09 14:35:58 -07003758 spin_lock_init(&tp->lock);
3759
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003760 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 for (i = 0; i < MAC_ADDR_LEN; i++)
3762 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003763 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3767 dev->irq = pdev->irq;
3768 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003770 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Michał Mirosław350fb322011-04-08 06:35:56 +00003772 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3773 * properly for all devices */
3774 dev->features |= NETIF_F_RXCSUM |
3775 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3776
3777 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3778 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3779 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3780 NETIF_F_HIGHDMA;
3781
3782 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3783 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3784 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
3786 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003787 tp->hw_start = cfg->hw_start;
3788 tp->intr_event = cfg->intr_event;
3789 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Francois Romieue03f33a2011-08-25 18:47:24 +02003791 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
3792 ~(RxBOVF | RxFOVF) : ~0;
3793
Francois Romieu2efa53f2007-03-09 00:00:05 +01003794 init_timer(&tp->timer);
3795 tp->timer.data = (unsigned long) dev;
3796 tp->timer.function = rtl8169_phy_timer;
3797
Francois Romieub6ffd972011-06-17 17:00:05 +02003798 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003801 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003802 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
3804 pci_set_drvdata(pdev, dev);
3805
Joe Perchesbf82c182010-02-09 11:49:50 +00003806 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003807 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003808 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Francois Romieucecb5fd2011-04-01 10:21:07 +02003810 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3811 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3812 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003813 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003814 }
françois romieub646d902011-01-03 15:08:21 +00003815
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003816 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Alan Sternf3ec4f82010-06-08 15:23:51 -04003818 if (pci_dev_run_wake(pdev))
3819 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003820
Ivan Vecera0d672e92011-02-15 02:08:39 +00003821 netif_carrier_off(dev);
3822
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003823out:
3824 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
françois romieu87aeec72010-04-26 11:42:06 +00003826err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003827 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003828 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003829err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003830 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003831err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003832 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003833 pci_disable_device(pdev);
3834err_out_free_dev_1:
3835 free_netdev(dev);
3836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
Francois Romieu07d3f512007-02-21 22:40:46 +01003839static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840{
3841 struct net_device *dev = pci_get_drvdata(pdev);
3842 struct rtl8169_private *tp = netdev_priv(dev);
3843
Francois Romieucecb5fd2011-04-01 10:21:07 +02003844 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3845 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3846 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003847 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003848 }
françois romieub646d902011-01-03 15:08:21 +00003849
Tejun Heo23f333a2010-12-12 16:45:14 +01003850 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003851
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003853
François Romieu953a12c2011-04-24 17:38:48 +02003854 rtl_release_firmware(tp);
3855
Alan Sternf3ec4f82010-06-08 15:23:51 -04003856 if (pci_dev_run_wake(pdev))
3857 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003858
Ivan Veceracc098dc2009-11-29 23:12:52 -08003859 /* restore original MAC address */
3860 rtl_rar_set(tp, dev->perm_addr);
3861
Francois Romieufbac58f2007-10-04 22:51:38 +02003862 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3864 pci_set_drvdata(pdev, NULL);
3865}
3866
Francois Romieub6ffd972011-06-17 17:00:05 +02003867static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3868{
3869 struct rtl_fw *rtl_fw;
3870 const char *name;
3871 int rc = -ENOMEM;
3872
3873 name = rtl_lookup_firmware_name(tp);
3874 if (!name)
3875 goto out_no_firmware;
3876
3877 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3878 if (!rtl_fw)
3879 goto err_warn;
3880
3881 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3882 if (rc < 0)
3883 goto err_free;
3884
Francois Romieufd112f22011-06-18 00:10:29 +02003885 rc = rtl_check_firmware(tp, rtl_fw);
3886 if (rc < 0)
3887 goto err_release_firmware;
3888
Francois Romieub6ffd972011-06-17 17:00:05 +02003889 tp->rtl_fw = rtl_fw;
3890out:
3891 return;
3892
Francois Romieufd112f22011-06-18 00:10:29 +02003893err_release_firmware:
3894 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003895err_free:
3896 kfree(rtl_fw);
3897err_warn:
3898 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3899 name, rc);
3900out_no_firmware:
3901 tp->rtl_fw = NULL;
3902 goto out;
3903}
3904
François Romieu953a12c2011-04-24 17:38:48 +02003905static void rtl_request_firmware(struct rtl8169_private *tp)
3906{
Francois Romieub6ffd972011-06-17 17:00:05 +02003907 if (IS_ERR(tp->rtl_fw))
3908 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003909}
3910
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911static int rtl8169_open(struct net_device *dev)
3912{
3913 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003914 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003916 int retval = -ENOMEM;
3917
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003918 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Neil Hormanc0cd8842010-03-29 13:16:02 -07003920 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003922 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003924 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3925 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003927 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003929 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3930 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003932 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
3934 retval = rtl8169_init_ring(dev);
3935 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003936 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
David Howellsc4028952006-11-22 14:57:56 +00003938 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Francois Romieu99f252b2007-04-02 22:59:59 +02003940 smp_mb();
3941
François Romieu953a12c2011-04-24 17:38:48 +02003942 rtl_request_firmware(tp);
3943
Francois Romieufbac58f2007-10-04 22:51:38 +02003944 retval = request_irq(dev->irq, rtl8169_interrupt,
3945 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003946 dev->name, dev);
3947 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003948 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003949
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003950 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003951
françois romieueee3a962011-01-08 02:17:26 +00003952 rtl8169_init_phy(dev, tp);
3953
Michał Mirosław350fb322011-04-08 06:35:56 +00003954 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003955
françois romieu065c27c2011-01-03 15:08:12 +00003956 rtl_pll_power_up(tp);
3957
Francois Romieu07ce4062007-02-23 23:36:39 +01003958 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003960 tp->saved_wolopts = 0;
3961 pm_runtime_put_noidle(&pdev->dev);
3962
françois romieueee3a962011-01-08 02:17:26 +00003963 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964out:
3965 return retval;
3966
François Romieu953a12c2011-04-24 17:38:48 +02003967err_release_fw_2:
3968 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003969 rtl8169_rx_clear(tp);
3970err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003971 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3972 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003973 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003974err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003975 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3976 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003977 tp->TxDescArray = NULL;
3978err_pm_runtime_put:
3979 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 goto out;
3981}
3982
Hayes Wang92fc43b2011-07-06 15:58:03 +08003983static void rtl_rx_close(struct rtl8169_private *tp)
3984{
3985 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003986
Francois Romieu1687b562011-07-19 17:21:29 +02003987 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003988}
3989
françois romieue6de30d2011-01-03 15:08:37 +00003990static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991{
françois romieue6de30d2011-01-03 15:08:37 +00003992 void __iomem *ioaddr = tp->mmio_addr;
3993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 /* Disable interrupts */
3995 rtl8169_irq_mask_and_ack(ioaddr);
3996
Hayes Wang92fc43b2011-07-06 15:58:03 +08003997 rtl_rx_close(tp);
3998
Hayes Wang5d2e1952011-02-22 17:26:22 +08003999 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004000 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4001 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004002 while (RTL_R8(TxPoll) & NPQ)
4003 udelay(20);
Hayes Wang70090422011-07-06 15:58:06 +08004004 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
Hayes Wangc2b0c1e2011-09-06 16:55:16 +08004005 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004006 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4007 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004008 } else {
4009 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4010 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004011 }
4012
Hayes Wang92fc43b2011-07-06 15:58:03 +08004013 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014}
4015
Francois Romieu7f796d82007-06-11 23:04:41 +02004016static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004017{
4018 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004019
4020 /* Set DMA burst size and Interframe Gap Time */
4021 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4022 (InterFrameGap << TxInterFrameGapShift));
4023}
4024
Francois Romieu07ce4062007-02-23 23:36:39 +01004025static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
4027 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Francois Romieu07ce4062007-02-23 23:36:39 +01004029 tp->hw_start(dev);
4030
Francois Romieu07ce4062007-02-23 23:36:39 +01004031 netif_start_queue(dev);
4032}
4033
Francois Romieu7f796d82007-06-11 23:04:41 +02004034static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4035 void __iomem *ioaddr)
4036{
4037 /*
4038 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4039 * register to be written before TxDescAddrLow to work.
4040 * Switching from MMIO to I/O access fixes the issue as well.
4041 */
4042 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004043 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02004044 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004045 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02004046}
4047
4048static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4049{
4050 u16 cmd;
4051
4052 cmd = RTL_R16(CPlusCmd);
4053 RTL_W16(CPlusCmd, cmd);
4054 return cmd;
4055}
4056
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004057static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02004058{
4059 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00004060 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02004061}
4062
Francois Romieu6dccd162007-02-13 23:38:05 +01004063static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4064{
Francois Romieu37441002011-06-17 22:58:54 +02004065 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004066 u32 mac_version;
4067 u32 clk;
4068 u32 val;
4069 } cfg2_info [] = {
4070 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4071 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4072 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4073 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004074 };
4075 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004076 unsigned int i;
4077 u32 clk;
4078
4079 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004080 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004081 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4082 RTL_W32(0x7c, p->val);
4083 break;
4084 }
4085 }
4086}
4087
Francois Romieu07ce4062007-02-23 23:36:39 +01004088static void rtl_hw_start_8169(struct net_device *dev)
4089{
4090 struct rtl8169_private *tp = netdev_priv(dev);
4091 void __iomem *ioaddr = tp->mmio_addr;
4092 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004093
Francois Romieu9cb427b2006-11-02 00:10:16 +01004094 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4095 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4096 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4097 }
4098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004100 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4101 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4102 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4103 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004104 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4105
Hayes Wange542a222011-07-06 15:58:04 +08004106 rtl_init_rxcfg(tp);
4107
françois romieuf0298f82011-01-03 15:07:42 +00004108 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004110 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Francois Romieucecb5fd2011-04-01 10:21:07 +02004112 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4113 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4114 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4115 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004116 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Francois Romieu7f796d82007-06-11 23:04:41 +02004118 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004119
Francois Romieucecb5fd2011-04-01 10:21:07 +02004120 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4121 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004122 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004124 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 }
4126
Francois Romieubcf0bf92006-07-26 23:14:13 +02004127 RTL_W16(CPlusCmd, tp->cp_cmd);
4128
Francois Romieu6dccd162007-02-13 23:38:05 +01004129 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4130
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 /*
4132 * Undocumented corner. Supposedly:
4133 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4134 */
4135 RTL_W16(IntrMitigate, 0x0000);
4136
Francois Romieu7f796d82007-06-11 23:04:41 +02004137 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004138
Francois Romieucecb5fd2011-04-01 10:21:07 +02004139 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4140 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4141 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4142 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004143 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4144 rtl_set_rx_tx_config_registers(tp);
4145 }
4146
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004148
4149 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4150 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
4152 RTL_W32(RxMissed, 0);
4153
Francois Romieu07ce4062007-02-23 23:36:39 +01004154 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
4156 /* no early-rx interrupts */
4157 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004158
4159 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01004160 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
Francois Romieu9c14cea2008-07-05 00:21:15 +02004163static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02004164{
Jon Masone44daad2011-06-27 07:46:31 +00004165 int cap = pci_pcie_cap(pdev);
Francois Romieu458a9f62008-08-02 15:50:02 +02004166
Francois Romieu9c14cea2008-07-05 00:21:15 +02004167 if (cap) {
4168 u16 ctl;
4169
4170 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
4171 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
4172 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
4173 }
Francois Romieu458a9f62008-08-02 15:50:02 +02004174}
4175
françois romieu650e8d52011-01-03 15:08:29 +00004176static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004177{
4178 u32 csi;
4179
4180 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004181 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4182}
4183
françois romieue6de30d2011-01-03 15:08:37 +00004184static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4185{
4186 rtl_csi_access_enable(ioaddr, 0x17000000);
4187}
4188
françois romieu650e8d52011-01-03 15:08:29 +00004189static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4190{
4191 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004192}
4193
4194struct ephy_info {
4195 unsigned int offset;
4196 u16 mask;
4197 u16 bits;
4198};
4199
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004200static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004201{
4202 u16 w;
4203
4204 while (len-- > 0) {
4205 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4206 rtl_ephy_write(ioaddr, e->offset, w);
4207 e++;
4208 }
4209}
4210
Francois Romieub726e492008-06-28 12:22:59 +02004211static void rtl_disable_clock_request(struct pci_dev *pdev)
4212{
Jon Masone44daad2011-06-27 07:46:31 +00004213 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004214
4215 if (cap) {
4216 u16 ctl;
4217
4218 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4219 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4220 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4221 }
4222}
4223
françois romieue6de30d2011-01-03 15:08:37 +00004224static void rtl_enable_clock_request(struct pci_dev *pdev)
4225{
Jon Masone44daad2011-06-27 07:46:31 +00004226 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004227
4228 if (cap) {
4229 u16 ctl;
4230
4231 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4232 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4233 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4234 }
4235}
4236
Francois Romieub726e492008-06-28 12:22:59 +02004237#define R8168_CPCMD_QUIRK_MASK (\
4238 EnableBist | \
4239 Mac_dbgo_oe | \
4240 Force_half_dup | \
4241 Force_rxflow_en | \
4242 Force_txflow_en | \
4243 Cxpl_dbg_sel | \
4244 ASF | \
4245 PktCntrDisable | \
4246 Mac_dbgo_sel)
4247
Francois Romieu219a1e92008-06-28 11:58:39 +02004248static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4249{
Francois Romieub726e492008-06-28 12:22:59 +02004250 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4251
4252 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4253
Francois Romieu2e68ae42008-06-28 12:00:55 +02004254 rtl_tx_performance_tweak(pdev,
4255 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004256}
4257
4258static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4259{
4260 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004261
françois romieuf0298f82011-01-03 15:07:42 +00004262 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004263
4264 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004265}
4266
4267static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4268{
Francois Romieub726e492008-06-28 12:22:59 +02004269 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4270
4271 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4272
Francois Romieu219a1e92008-06-28 11:58:39 +02004273 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004274
4275 rtl_disable_clock_request(pdev);
4276
4277 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004278}
4279
Francois Romieuef3386f2008-06-29 12:24:30 +02004280static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004281{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004282 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004283 { 0x01, 0, 0x0001 },
4284 { 0x02, 0x0800, 0x1000 },
4285 { 0x03, 0, 0x0042 },
4286 { 0x06, 0x0080, 0x0000 },
4287 { 0x07, 0, 0x2000 }
4288 };
4289
françois romieu650e8d52011-01-03 15:08:29 +00004290 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004291
4292 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4293
Francois Romieu219a1e92008-06-28 11:58:39 +02004294 __rtl_hw_start_8168cp(ioaddr, pdev);
4295}
4296
Francois Romieuef3386f2008-06-29 12:24:30 +02004297static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4298{
françois romieu650e8d52011-01-03 15:08:29 +00004299 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004300
4301 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4302
4303 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4304
4305 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4306}
4307
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004308static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4309{
françois romieu650e8d52011-01-03 15:08:29 +00004310 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004311
4312 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4313
4314 /* Magic. */
4315 RTL_W8(DBG_REG, 0x20);
4316
françois romieuf0298f82011-01-03 15:07:42 +00004317 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004318
4319 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4320
4321 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4322}
4323
Francois Romieu219a1e92008-06-28 11:58:39 +02004324static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4325{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004326 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004327 { 0x02, 0x0800, 0x1000 },
4328 { 0x03, 0, 0x0002 },
4329 { 0x06, 0x0080, 0x0000 }
4330 };
4331
françois romieu650e8d52011-01-03 15:08:29 +00004332 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004333
4334 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4335
4336 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4337
Francois Romieu219a1e92008-06-28 11:58:39 +02004338 __rtl_hw_start_8168cp(ioaddr, pdev);
4339}
4340
4341static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4342{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004343 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004344 { 0x01, 0, 0x0001 },
4345 { 0x03, 0x0400, 0x0220 }
4346 };
4347
françois romieu650e8d52011-01-03 15:08:29 +00004348 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004349
4350 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4351
Francois Romieu219a1e92008-06-28 11:58:39 +02004352 __rtl_hw_start_8168cp(ioaddr, pdev);
4353}
4354
Francois Romieu197ff762008-06-28 13:16:02 +02004355static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4356{
4357 rtl_hw_start_8168c_2(ioaddr, pdev);
4358}
4359
Francois Romieu6fb07052008-06-29 11:54:28 +02004360static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4361{
françois romieu650e8d52011-01-03 15:08:29 +00004362 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004363
4364 __rtl_hw_start_8168cp(ioaddr, pdev);
4365}
4366
Francois Romieu5b538df2008-07-20 16:22:45 +02004367static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4368{
françois romieu650e8d52011-01-03 15:08:29 +00004369 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004370
4371 rtl_disable_clock_request(pdev);
4372
françois romieuf0298f82011-01-03 15:07:42 +00004373 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004374
4375 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4376
4377 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4378}
4379
hayeswang4804b3b2011-03-21 01:50:29 +00004380static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4381{
4382 rtl_csi_access_enable_1(ioaddr);
4383
4384 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4385
4386 RTL_W8(MaxTxPacketSize, TxPacketMax);
4387
4388 rtl_disable_clock_request(pdev);
4389}
4390
françois romieue6de30d2011-01-03 15:08:37 +00004391static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4392{
4393 static const struct ephy_info e_info_8168d_4[] = {
4394 { 0x0b, ~0, 0x48 },
4395 { 0x19, 0x20, 0x50 },
4396 { 0x0c, ~0, 0x20 }
4397 };
4398 int i;
4399
4400 rtl_csi_access_enable_1(ioaddr);
4401
4402 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4403
4404 RTL_W8(MaxTxPacketSize, TxPacketMax);
4405
4406 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4407 const struct ephy_info *e = e_info_8168d_4 + i;
4408 u16 w;
4409
4410 w = rtl_ephy_read(ioaddr, e->offset);
4411 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4412 }
4413
4414 rtl_enable_clock_request(pdev);
4415}
4416
Hayes Wang70090422011-07-06 15:58:06 +08004417static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004418{
Hayes Wang70090422011-07-06 15:58:06 +08004419 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004420 { 0x00, 0x0200, 0x0100 },
4421 { 0x00, 0x0000, 0x0004 },
4422 { 0x06, 0x0002, 0x0001 },
4423 { 0x06, 0x0000, 0x0030 },
4424 { 0x07, 0x0000, 0x2000 },
4425 { 0x00, 0x0000, 0x0020 },
4426 { 0x03, 0x5800, 0x2000 },
4427 { 0x03, 0x0000, 0x0001 },
4428 { 0x01, 0x0800, 0x1000 },
4429 { 0x07, 0x0000, 0x4000 },
4430 { 0x1e, 0x0000, 0x2000 },
4431 { 0x19, 0xffff, 0xfe6c },
4432 { 0x0a, 0x0000, 0x0040 }
4433 };
4434
4435 rtl_csi_access_enable_2(ioaddr);
4436
Hayes Wang70090422011-07-06 15:58:06 +08004437 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004438
4439 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4440
4441 RTL_W8(MaxTxPacketSize, TxPacketMax);
4442
4443 rtl_disable_clock_request(pdev);
4444
4445 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004446 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4447 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004448
Francois Romieucecb5fd2011-04-01 10:21:07 +02004449 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004450}
4451
Hayes Wang70090422011-07-06 15:58:06 +08004452static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4453{
4454 static const struct ephy_info e_info_8168e_2[] = {
4455 { 0x09, 0x0000, 0x0080 },
4456 { 0x19, 0x0000, 0x0224 }
4457 };
4458
4459 rtl_csi_access_enable_1(ioaddr);
4460
4461 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4462
4463 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4464
4465 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4466 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4467 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4468 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4469 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4470 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4471 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4472 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4473 ERIAR_EXGMAC);
4474
4475 RTL_W8(MaxTxPacketSize, 0x27);
4476
4477 rtl_disable_clock_request(pdev);
4478
4479 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4480 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4481
4482 /* Adjust EEE LED frequency */
4483 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4484
4485 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4486 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4487 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4488}
4489
Francois Romieu07ce4062007-02-23 23:36:39 +01004490static void rtl_hw_start_8168(struct net_device *dev)
4491{
Francois Romieu2dd99532007-06-11 23:22:52 +02004492 struct rtl8169_private *tp = netdev_priv(dev);
4493 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004494 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004495
4496 RTL_W8(Cfg9346, Cfg9346_Unlock);
4497
françois romieuf0298f82011-01-03 15:07:42 +00004498 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004499
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004500 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004501
Francois Romieu0e485152007-02-20 00:00:26 +01004502 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004503
4504 RTL_W16(CPlusCmd, tp->cp_cmd);
4505
Francois Romieu0e485152007-02-20 00:00:26 +01004506 RTL_W16(IntrMitigate, 0x5151);
4507
4508 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004509 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4510 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004511 tp->intr_event |= RxFIFOOver | PCSTimeout;
4512 tp->intr_event &= ~RxOverflow;
4513 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004514
4515 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4516
Francois Romieub8363902008-06-01 12:31:57 +02004517 rtl_set_rx_mode(dev);
4518
4519 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4520 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004521
4522 RTL_R8(IntrMask);
4523
Francois Romieu219a1e92008-06-28 11:58:39 +02004524 switch (tp->mac_version) {
4525 case RTL_GIGA_MAC_VER_11:
4526 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004527 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004528
4529 case RTL_GIGA_MAC_VER_12:
4530 case RTL_GIGA_MAC_VER_17:
4531 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004532 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004533
4534 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004535 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004536 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004537
4538 case RTL_GIGA_MAC_VER_19:
4539 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004540 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004541
4542 case RTL_GIGA_MAC_VER_20:
4543 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004544 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004545
Francois Romieu197ff762008-06-28 13:16:02 +02004546 case RTL_GIGA_MAC_VER_21:
4547 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004548 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004549
Francois Romieu6fb07052008-06-29 11:54:28 +02004550 case RTL_GIGA_MAC_VER_22:
4551 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004552 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004553
Francois Romieuef3386f2008-06-29 12:24:30 +02004554 case RTL_GIGA_MAC_VER_23:
4555 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004556 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004557
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004558 case RTL_GIGA_MAC_VER_24:
4559 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004560 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004561
Francois Romieu5b538df2008-07-20 16:22:45 +02004562 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004563 case RTL_GIGA_MAC_VER_26:
4564 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004565 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004566 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004567
françois romieue6de30d2011-01-03 15:08:37 +00004568 case RTL_GIGA_MAC_VER_28:
4569 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004570 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004571
hayeswang4804b3b2011-03-21 01:50:29 +00004572 case RTL_GIGA_MAC_VER_31:
4573 rtl_hw_start_8168dp(ioaddr, pdev);
4574 break;
4575
hayeswang01dc7fe2011-03-21 01:50:28 +00004576 case RTL_GIGA_MAC_VER_32:
4577 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004578 rtl_hw_start_8168e_1(ioaddr, pdev);
4579 break;
4580 case RTL_GIGA_MAC_VER_34:
4581 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004582 break;
françois romieue6de30d2011-01-03 15:08:37 +00004583
Francois Romieu219a1e92008-06-28 11:58:39 +02004584 default:
4585 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4586 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004587 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004588 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004589
Francois Romieu0e485152007-02-20 00:00:26 +01004590 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4591
Francois Romieub8363902008-06-01 12:31:57 +02004592 RTL_W8(Cfg9346, Cfg9346_Lock);
4593
Francois Romieu2dd99532007-06-11 23:22:52 +02004594 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004595
Francois Romieu0e485152007-02-20 00:00:26 +01004596 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Francois Romieu2857ffb2008-08-02 21:08:49 +02004599#define R810X_CPCMD_QUIRK_MASK (\
4600 EnableBist | \
4601 Mac_dbgo_oe | \
4602 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004603 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004604 Force_txflow_en | \
4605 Cxpl_dbg_sel | \
4606 ASF | \
4607 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004608 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004609
4610static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4611{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004612 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004613 { 0x01, 0, 0x6e65 },
4614 { 0x02, 0, 0x091f },
4615 { 0x03, 0, 0xc2f9 },
4616 { 0x06, 0, 0xafb5 },
4617 { 0x07, 0, 0x0e00 },
4618 { 0x19, 0, 0xec80 },
4619 { 0x01, 0, 0x2e65 },
4620 { 0x01, 0, 0x6e65 }
4621 };
4622 u8 cfg1;
4623
françois romieu650e8d52011-01-03 15:08:29 +00004624 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004625
4626 RTL_W8(DBG_REG, FIX_NAK_1);
4627
4628 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4629
4630 RTL_W8(Config1,
4631 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4632 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4633
4634 cfg1 = RTL_R8(Config1);
4635 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4636 RTL_W8(Config1, cfg1 & ~LEDS0);
4637
Francois Romieu2857ffb2008-08-02 21:08:49 +02004638 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4639}
4640
4641static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4642{
françois romieu650e8d52011-01-03 15:08:29 +00004643 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004644
4645 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4646
4647 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4648 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004649}
4650
4651static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4652{
4653 rtl_hw_start_8102e_2(ioaddr, pdev);
4654
4655 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4656}
4657
Hayes Wang5a5e4442011-02-22 17:26:21 +08004658static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4659{
4660 static const struct ephy_info e_info_8105e_1[] = {
4661 { 0x07, 0, 0x4000 },
4662 { 0x19, 0, 0x0200 },
4663 { 0x19, 0, 0x0020 },
4664 { 0x1e, 0, 0x2000 },
4665 { 0x03, 0, 0x0001 },
4666 { 0x19, 0, 0x0100 },
4667 { 0x19, 0, 0x0004 },
4668 { 0x0a, 0, 0x0020 }
4669 };
4670
Francois Romieucecb5fd2011-04-01 10:21:07 +02004671 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004672 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4673
Francois Romieucecb5fd2011-04-01 10:21:07 +02004674 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004675 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4676
4677 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004678 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004679
4680 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4681}
4682
4683static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4684{
4685 rtl_hw_start_8105e_1(ioaddr, pdev);
4686 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4687}
4688
Francois Romieu07ce4062007-02-23 23:36:39 +01004689static void rtl_hw_start_8101(struct net_device *dev)
4690{
Francois Romieucdf1a602007-06-11 23:29:50 +02004691 struct rtl8169_private *tp = netdev_priv(dev);
4692 void __iomem *ioaddr = tp->mmio_addr;
4693 struct pci_dev *pdev = tp->pci_dev;
4694
Francois Romieucecb5fd2011-04-01 10:21:07 +02004695 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4696 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004697 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004698
4699 if (cap) {
4700 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4701 PCI_EXP_DEVCTL_NOSNOOP_EN);
4702 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004703 }
4704
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004705 RTL_W8(Cfg9346, Cfg9346_Unlock);
4706
Francois Romieu2857ffb2008-08-02 21:08:49 +02004707 switch (tp->mac_version) {
4708 case RTL_GIGA_MAC_VER_07:
4709 rtl_hw_start_8102e_1(ioaddr, pdev);
4710 break;
4711
4712 case RTL_GIGA_MAC_VER_08:
4713 rtl_hw_start_8102e_3(ioaddr, pdev);
4714 break;
4715
4716 case RTL_GIGA_MAC_VER_09:
4717 rtl_hw_start_8102e_2(ioaddr, pdev);
4718 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004719
4720 case RTL_GIGA_MAC_VER_29:
4721 rtl_hw_start_8105e_1(ioaddr, pdev);
4722 break;
4723 case RTL_GIGA_MAC_VER_30:
4724 rtl_hw_start_8105e_2(ioaddr, pdev);
4725 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004726 }
4727
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004728 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004729
françois romieuf0298f82011-01-03 15:07:42 +00004730 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004731
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004732 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004733
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004734 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004735 RTL_W16(CPlusCmd, tp->cp_cmd);
4736
4737 RTL_W16(IntrMitigate, 0x0000);
4738
4739 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4740
4741 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4742 rtl_set_rx_tx_config_registers(tp);
4743
Francois Romieucdf1a602007-06-11 23:29:50 +02004744 RTL_R8(IntrMask);
4745
Francois Romieucdf1a602007-06-11 23:29:50 +02004746 rtl_set_rx_mode(dev);
4747
4748 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004749
Francois Romieu0e485152007-02-20 00:00:26 +01004750 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751}
4752
4753static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4756 return -EINVAL;
4757
4758 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004759 netdev_update_features(dev);
4760
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
4764static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4765{
Al Viro95e09182007-12-22 18:55:39 +00004766 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4768}
4769
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004770static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4771 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004773 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004774 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004775
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004776 kfree(*data_buff);
4777 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 rtl8169_make_unusable_by_asic(desc);
4779}
4780
4781static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4782{
4783 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4784
4785 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4786}
4787
4788static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4789 u32 rx_buf_sz)
4790{
4791 desc->addr = cpu_to_le64(mapping);
4792 wmb();
4793 rtl8169_mark_to_asic(desc, rx_buf_sz);
4794}
4795
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004796static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004798 return (void *)ALIGN((long)data, 16);
4799}
4800
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004801static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4802 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004803{
4804 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004806 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004807 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004808 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004810 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4811 if (!data)
4812 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004813
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004814 if (rtl8169_align(data) != data) {
4815 kfree(data);
4816 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4817 if (!data)
4818 return NULL;
4819 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004820
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004821 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004822 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004823 if (unlikely(dma_mapping_error(d, mapping))) {
4824 if (net_ratelimit())
4825 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004826 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
4829 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004830 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004831
4832err_out:
4833 kfree(data);
4834 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
4837static void rtl8169_rx_clear(struct rtl8169_private *tp)
4838{
Francois Romieu07d3f512007-02-21 22:40:46 +01004839 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
4841 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004842 if (tp->Rx_databuff[i]) {
4843 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 tp->RxDescArray + i);
4845 }
4846 }
4847}
4848
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004849static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004851 desc->opts1 |= cpu_to_le32(RingEnd);
4852}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004853
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004854static int rtl8169_rx_fill(struct rtl8169_private *tp)
4855{
4856 unsigned int i;
4857
4858 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004859 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004860
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004861 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004863
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004864 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004865 if (!data) {
4866 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004867 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004868 }
4869 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004872 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4873 return 0;
4874
4875err_out:
4876 rtl8169_rx_clear(tp);
4877 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878}
4879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880static int rtl8169_init_ring(struct net_device *dev)
4881{
4882 struct rtl8169_private *tp = netdev_priv(dev);
4883
4884 rtl8169_init_ring_indexes(tp);
4885
4886 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004887 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004889 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890}
4891
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004892static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 struct TxDesc *desc)
4894{
4895 unsigned int len = tx_skb->len;
4896
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004897 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 desc->opts1 = 0x00;
4900 desc->opts2 = 0x00;
4901 desc->addr = 0x00;
4902 tx_skb->len = 0;
4903}
4904
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004905static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4906 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
4908 unsigned int i;
4909
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004910 for (i = 0; i < n; i++) {
4911 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 struct ring_info *tx_skb = tp->tx_skb + entry;
4913 unsigned int len = tx_skb->len;
4914
4915 if (len) {
4916 struct sk_buff *skb = tx_skb->skb;
4917
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004918 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 tp->TxDescArray + entry);
4920 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004921 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 dev_kfree_skb(skb);
4923 tx_skb->skb = NULL;
4924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 }
4926 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004927}
4928
4929static void rtl8169_tx_clear(struct rtl8169_private *tp)
4930{
4931 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 tp->cur_tx = tp->dirty_tx = 0;
4933}
4934
David Howellsc4028952006-11-22 14:57:56 +00004935static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
4937 struct rtl8169_private *tp = netdev_priv(dev);
4938
David Howellsc4028952006-11-22 14:57:56 +00004939 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 schedule_delayed_work(&tp->task, 4);
4941}
4942
4943static void rtl8169_wait_for_quiescence(struct net_device *dev)
4944{
4945 struct rtl8169_private *tp = netdev_priv(dev);
4946 void __iomem *ioaddr = tp->mmio_addr;
4947
4948 synchronize_irq(dev->irq);
4949
4950 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004951 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953 rtl8169_irq_mask_and_ack(ioaddr);
4954
David S. Millerd1d08d12008-01-07 20:53:33 -08004955 tp->intr_mask = 0xffff;
4956 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004957 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958}
4959
David Howellsc4028952006-11-22 14:57:56 +00004960static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961{
David Howellsc4028952006-11-22 14:57:56 +00004962 struct rtl8169_private *tp =
4963 container_of(work, struct rtl8169_private, task.work);
4964 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 int ret;
4966
Francois Romieueb2a0212007-02-15 23:37:21 +01004967 rtnl_lock();
4968
4969 if (!netif_running(dev))
4970 goto out_unlock;
4971
4972 rtl8169_wait_for_quiescence(dev);
4973 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
4975 ret = rtl8169_open(dev);
4976 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004977 if (net_ratelimit())
4978 netif_err(tp, drv, dev,
4979 "reinit failure (status = %d). Rescheduling\n",
4980 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4982 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004983
4984out_unlock:
4985 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986}
4987
David Howellsc4028952006-11-22 14:57:56 +00004988static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989{
David Howellsc4028952006-11-22 14:57:56 +00004990 struct rtl8169_private *tp =
4991 container_of(work, struct rtl8169_private, task.work);
4992 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004993 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Francois Romieueb2a0212007-02-15 23:37:21 +01004995 rtnl_lock();
4996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004998 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
5000 rtl8169_wait_for_quiescence(dev);
5001
Francois Romieu56de4142011-03-15 17:29:31 +01005002 for (i = 0; i < NUM_RX_DESC; i++)
5003 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 rtl8169_tx_clear(tp);
5006
Hayes Wang92fc43b2011-07-06 15:58:03 +08005007 rtl8169_hw_reset(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005008 rtl_hw_start(dev);
5009 netif_wake_queue(dev);
5010 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01005011
5012out_unlock:
5013 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014}
5015
5016static void rtl8169_tx_timeout(struct net_device *dev)
5017{
5018 struct rtl8169_private *tp = netdev_priv(dev);
5019
françois romieue6de30d2011-01-03 15:08:37 +00005020 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
5022 /* Let's wait a bit while any (async) irq lands on */
5023 rtl8169_schedule_work(dev, rtl8169_reset_task);
5024}
5025
5026static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005027 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
5029 struct skb_shared_info *info = skb_shinfo(skb);
5030 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005031 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005032 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
5034 entry = tp->cur_tx;
5035 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5036 skb_frag_t *frag = info->frags + cur_frag;
5037 dma_addr_t mapping;
5038 u32 status, len;
5039 void *addr;
5040
5041 entry = (entry + 1) % NUM_TX_DESC;
5042
5043 txd = tp->TxDescArray + entry;
5044 len = frag->size;
5045 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005046 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005047 if (unlikely(dma_mapping_error(d, mapping))) {
5048 if (net_ratelimit())
5049 netif_err(tp, drv, tp->dev,
5050 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005051 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
Francois Romieucecb5fd2011-04-01 10:21:07 +02005054 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005055 status = opts[0] | len |
5056 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
5058 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005059 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 txd->addr = cpu_to_le64(mapping);
5061
5062 tp->tx_skb[entry].len = len;
5063 }
5064
5065 if (cur_frag) {
5066 tp->tx_skb[entry].skb = skb;
5067 txd->opts1 |= cpu_to_le32(LastFrag);
5068 }
5069
5070 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005071
5072err_out:
5073 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5074 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075}
5076
Francois Romieu2b7b4312011-04-18 22:53:24 -07005077static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5078 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005080 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005081 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005082 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
Francois Romieu2b7b4312011-04-18 22:53:24 -07005084 if (mss) {
5085 opts[0] |= TD_LSO;
5086 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5087 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005088 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
5090 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005091 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005093 opts[offset] |= info->checksum.udp;
5094 else
5095 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097}
5098
Stephen Hemminger613573252009-08-31 19:50:58 +00005099static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5100 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
5102 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005103 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 struct TxDesc *txd = tp->TxDescArray + entry;
5105 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005106 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 dma_addr_t mapping;
5108 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005109 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005110 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005111
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005113 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005114 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 }
5116
5117 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005118 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005120 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005121 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005122 if (unlikely(dma_mapping_error(d, mapping))) {
5123 if (net_ratelimit())
5124 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005125 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
5128 tp->tx_skb[entry].len = len;
5129 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
Francois Romieu2b7b4312011-04-18 22:53:24 -07005131 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5132 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005133
Francois Romieu2b7b4312011-04-18 22:53:24 -07005134 rtl8169_tso_csum(tp, skb, opts);
5135
5136 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005137 if (frags < 0)
5138 goto err_dma_1;
5139 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005140 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005141 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005142 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005143 tp->tx_skb[entry].skb = skb;
5144 }
5145
Francois Romieu2b7b4312011-04-18 22:53:24 -07005146 txd->opts2 = cpu_to_le32(opts[1]);
5147
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 wmb();
5149
Francois Romieucecb5fd2011-04-01 10:21:07 +02005150 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005151 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 txd->opts1 = cpu_to_le32(status);
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 tp->cur_tx += frags + 1;
5155
David Dillow4c020a92010-03-03 16:33:10 +00005156 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
Francois Romieucecb5fd2011-04-01 10:21:07 +02005158 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
5160 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5161 netif_stop_queue(dev);
5162 smp_rmb();
5163 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5164 netif_wake_queue(dev);
5165 }
5166
Stephen Hemminger613573252009-08-31 19:50:58 +00005167 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005169err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005170 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005171err_dma_0:
5172 dev_kfree_skb(skb);
5173 dev->stats.tx_dropped++;
5174 return NETDEV_TX_OK;
5175
5176err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005178 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005179 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180}
5181
5182static void rtl8169_pcierr_interrupt(struct net_device *dev)
5183{
5184 struct rtl8169_private *tp = netdev_priv(dev);
5185 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 u16 pci_status, pci_cmd;
5187
5188 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5189 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5190
Joe Perchesbf82c182010-02-09 11:49:50 +00005191 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5192 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
5194 /*
5195 * The recovery sequence below admits a very elaborated explanation:
5196 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005197 * - I did not see what else could be done;
5198 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 *
5200 * Feel free to adjust to your needs.
5201 */
Francois Romieua27993f2006-12-18 00:04:19 +01005202 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005203 pci_cmd &= ~PCI_COMMAND_PARITY;
5204 else
5205 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5206
5207 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
5209 pci_write_config_word(pdev, PCI_STATUS,
5210 pci_status & (PCI_STATUS_DETECTED_PARITY |
5211 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5212 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5213
5214 /* The infamous DAC f*ckup only happens at boot time */
5215 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005216 void __iomem *ioaddr = tp->mmio_addr;
5217
Joe Perchesbf82c182010-02-09 11:49:50 +00005218 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 tp->cp_cmd &= ~PCIDAC;
5220 RTL_W16(CPlusCmd, tp->cp_cmd);
5221 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 }
5223
françois romieue6de30d2011-01-03 15:08:37 +00005224 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005225
5226 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227}
5228
Francois Romieu07d3f512007-02-21 22:40:46 +01005229static void rtl8169_tx_interrupt(struct net_device *dev,
5230 struct rtl8169_private *tp,
5231 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232{
5233 unsigned int dirty_tx, tx_left;
5234
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 dirty_tx = tp->dirty_tx;
5236 smp_rmb();
5237 tx_left = tp->cur_tx - dirty_tx;
5238
5239 while (tx_left > 0) {
5240 unsigned int entry = dirty_tx % NUM_TX_DESC;
5241 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 u32 status;
5243
5244 rmb();
5245 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5246 if (status & DescOwn)
5247 break;
5248
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005249 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5250 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005252 dev->stats.tx_packets++;
5253 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005254 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 tx_skb->skb = NULL;
5256 }
5257 dirty_tx++;
5258 tx_left--;
5259 }
5260
5261 if (tp->dirty_tx != dirty_tx) {
5262 tp->dirty_tx = dirty_tx;
5263 smp_wmb();
5264 if (netif_queue_stopped(dev) &&
5265 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5266 netif_wake_queue(dev);
5267 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005268 /*
5269 * 8168 hack: TxPoll requests are lost when the Tx packets are
5270 * too close. Let's kick an extra TxPoll request when a burst
5271 * of start_xmit activity is detected (if it is not detected,
5272 * it is slow enough). -- FR
5273 */
5274 smp_rmb();
5275 if (tp->cur_tx != dirty_tx)
5276 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 }
5278}
5279
Francois Romieu126fa4b2005-05-12 20:09:17 -04005280static inline int rtl8169_fragmented_frame(u32 status)
5281{
5282 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5283}
5284
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005285static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 u32 status = opts1 & RxProtoMask;
5288
5289 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005290 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 skb->ip_summed = CHECKSUM_UNNECESSARY;
5292 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005293 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294}
5295
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005296static struct sk_buff *rtl8169_try_rx_copy(void *data,
5297 struct rtl8169_private *tp,
5298 int pkt_size,
5299 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005301 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005302 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005304 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005305 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005306 prefetch(data);
5307 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5308 if (skb)
5309 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005310 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5311
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005312 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313}
5314
Francois Romieu07d3f512007-02-21 22:40:46 +01005315static int rtl8169_rx_interrupt(struct net_device *dev,
5316 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005317 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318{
5319 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005320 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 cur_rx = tp->cur_rx;
5323 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005324 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005326 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005328 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 u32 status;
5330
5331 rmb();
Francois Romieue03f33a2011-08-25 18:47:24 +02005332 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
5334 if (status & DescOwn)
5335 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005336 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005337 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5338 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005339 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005341 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005343 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005344 if (status & RxFOVF) {
5345 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005346 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005347 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005348 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005350 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005351 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
Francois Romieu126fa4b2005-05-12 20:09:17 -04005354 /*
5355 * The driver does not support incoming fragmented
5356 * frames. They are seen as a symptom of over-mtu
5357 * sized frames.
5358 */
5359 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005360 dev->stats.rx_dropped++;
5361 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005362 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005363 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005364 }
5365
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005366 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5367 tp, pkt_size, addr);
5368 rtl8169_mark_to_asic(desc, rx_buf_sz);
5369 if (!skb) {
5370 dev->stats.rx_dropped++;
5371 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 }
5373
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005374 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 skb_put(skb, pkt_size);
5376 skb->protocol = eth_type_trans(skb, dev);
5377
Francois Romieu7a8fc772011-03-01 17:18:33 +01005378 rtl8169_rx_vlan_tag(desc, skb);
5379
Francois Romieu56de4142011-03-15 17:29:31 +01005380 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
Francois Romieucebf8cc2007-10-18 12:06:54 +02005382 dev->stats.rx_bytes += pkt_size;
5383 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005385
5386 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005387 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005388 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5389 desc->opts2 = 0;
5390 cur_rx++;
5391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 }
5393
5394 count = cur_rx - tp->cur_rx;
5395 tp->cur_rx = cur_rx;
5396
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005397 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
5399 return count;
5400}
5401
Francois Romieu07d3f512007-02-21 22:40:46 +01005402static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403{
Francois Romieu07d3f512007-02-21 22:40:46 +01005404 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005408 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
David Dillowf11a3772009-05-22 15:29:34 +00005410 /* loop handling interrupts until we have no new ones or
5411 * we hit a invalid/hotplug case.
5412 */
Francois Romieu865c6522008-05-11 14:51:00 +02005413 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005414 while (status && status != 0xffff) {
5415 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
David Dillowf11a3772009-05-22 15:29:34 +00005417 /* Handle all of the error cases first. These will reset
5418 * the chip, so just exit the loop.
5419 */
5420 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005421 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 }
David Dillowf11a3772009-05-22 15:29:34 +00005424
Francois Romieu1519e572011-02-03 12:02:36 +01005425 if (unlikely(status & RxFIFOOver)) {
5426 switch (tp->mac_version) {
5427 /* Work around for rx fifo overflow */
5428 case RTL_GIGA_MAC_VER_11:
5429 case RTL_GIGA_MAC_VER_22:
5430 case RTL_GIGA_MAC_VER_26:
5431 netif_stop_queue(dev);
5432 rtl8169_tx_timeout(dev);
5433 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005434 /* Testers needed. */
5435 case RTL_GIGA_MAC_VER_17:
5436 case RTL_GIGA_MAC_VER_19:
5437 case RTL_GIGA_MAC_VER_20:
5438 case RTL_GIGA_MAC_VER_21:
5439 case RTL_GIGA_MAC_VER_23:
5440 case RTL_GIGA_MAC_VER_24:
5441 case RTL_GIGA_MAC_VER_27:
5442 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005443 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005444 /* Experimental science. Pktgen proof. */
5445 case RTL_GIGA_MAC_VER_12:
5446 case RTL_GIGA_MAC_VER_25:
5447 if (status == RxFIFOOver)
5448 goto done;
5449 break;
5450 default:
5451 break;
5452 }
David Dillowf11a3772009-05-22 15:29:34 +00005453 }
5454
5455 if (unlikely(status & SYSErr)) {
5456 rtl8169_pcierr_interrupt(dev);
5457 break;
5458 }
5459
5460 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005461 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005462
5463 /* We need to see the lastest version of tp->intr_mask to
5464 * avoid ignoring an MSI interrupt and having to wait for
5465 * another event which may never come.
5466 */
5467 smp_rmb();
5468 if (status & tp->intr_mask & tp->napi_event) {
5469 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5470 tp->intr_mask = ~tp->napi_event;
5471
5472 if (likely(napi_schedule_prep(&tp->napi)))
5473 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005474 else
5475 netif_info(tp, intr, dev,
5476 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005477 }
5478
5479 /* We only get a new MSI interrupt when all active irq
5480 * sources on the chip have been acknowledged. So, ack
5481 * everything we've seen and check if new sources have become
5482 * active to avoid blocking all interrupts from the chip.
5483 */
5484 RTL_W16(IntrStatus,
5485 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5486 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 }
Francois Romieu1519e572011-02-03 12:02:36 +01005488done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 return IRQ_RETVAL(handled);
5490}
5491
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005492static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005494 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5495 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005497 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005499 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 rtl8169_tx_interrupt(dev, tp, ioaddr);
5501
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005502 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005503 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005504
5505 /* We need for force the visibility of tp->intr_mask
5506 * for other CPUs, as we can loose an MSI interrupt
5507 * and potentially wait for a retransmit timeout if we don't.
5508 * The posted write to IntrMask is safe, as it will
5509 * eventually make it to the chip and we won't loose anything
5510 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 */
David Dillowf11a3772009-05-22 15:29:34 +00005512 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005513 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005514 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 }
5516
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005517 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
Francois Romieu523a6092008-09-10 22:28:56 +02005520static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5521{
5522 struct rtl8169_private *tp = netdev_priv(dev);
5523
5524 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5525 return;
5526
5527 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5528 RTL_W32(RxMissed, 0);
5529}
5530
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531static void rtl8169_down(struct net_device *dev)
5532{
5533 struct rtl8169_private *tp = netdev_priv(dev);
5534 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
Francois Romieu4876cc12011-03-11 21:07:11 +01005536 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
5538 netif_stop_queue(dev);
5539
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005540 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005541
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 spin_lock_irq(&tp->lock);
5543
Hayes Wang92fc43b2011-07-06 15:58:03 +08005544 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005545 /*
5546 * At this point device interrupts can not be enabled in any function,
5547 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5548 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5549 */
Francois Romieu523a6092008-09-10 22:28:56 +02005550 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
5552 spin_unlock_irq(&tp->lock);
5553
5554 synchronize_irq(dev->irq);
5555
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005557 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 rtl8169_tx_clear(tp);
5560
5561 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005562
5563 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564}
5565
5566static int rtl8169_close(struct net_device *dev)
5567{
5568 struct rtl8169_private *tp = netdev_priv(dev);
5569 struct pci_dev *pdev = tp->pci_dev;
5570
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005571 pm_runtime_get_sync(&pdev->dev);
5572
Francois Romieucecb5fd2011-04-01 10:21:07 +02005573 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005574 rtl8169_update_counters(dev);
5575
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 rtl8169_down(dev);
5577
5578 free_irq(dev->irq, dev);
5579
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005580 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5581 tp->RxPhyAddr);
5582 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5583 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 tp->TxDescArray = NULL;
5585 tp->RxDescArray = NULL;
5586
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005587 pm_runtime_put_sync(&pdev->dev);
5588
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 return 0;
5590}
5591
Francois Romieu07ce4062007-02-23 23:36:39 +01005592static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593{
5594 struct rtl8169_private *tp = netdev_priv(dev);
5595 void __iomem *ioaddr = tp->mmio_addr;
5596 unsigned long flags;
5597 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005598 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 u32 tmp = 0;
5600
5601 if (dev->flags & IFF_PROMISC) {
5602 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005603 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 rx_mode =
5605 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5606 AcceptAllPhys;
5607 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005608 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005609 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 /* Too many to filter perfectly -- accept all multicasts. */
5611 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5612 mc_filter[1] = mc_filter[0] = 0xffffffff;
5613 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005614 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005615
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 rx_mode = AcceptBroadcast | AcceptMyPhys;
5617 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005618 netdev_for_each_mc_addr(ha, dev) {
5619 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5621 rx_mode |= AcceptMulticast;
5622 }
5623 }
5624
5625 spin_lock_irqsave(&tp->lock, flags);
5626
Francois Romieu1687b562011-07-19 17:21:29 +02005627 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Francois Romieuf887cce2008-07-17 22:24:18 +02005629 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005630 u32 data = mc_filter[0];
5631
5632 mc_filter[0] = swab32(mc_filter[1]);
5633 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005634 }
5635
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005637 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
Francois Romieu57a9f232007-06-04 22:10:15 +02005639 RTL_W32(RxConfig, tmp);
5640
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 spin_unlock_irqrestore(&tp->lock, flags);
5642}
5643
5644/**
5645 * rtl8169_get_stats - Get rtl8169 read/write statistics
5646 * @dev: The Ethernet Device to get statistics for
5647 *
5648 * Get TX/RX statistics for rtl8169
5649 */
5650static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5651{
5652 struct rtl8169_private *tp = netdev_priv(dev);
5653 void __iomem *ioaddr = tp->mmio_addr;
5654 unsigned long flags;
5655
5656 if (netif_running(dev)) {
5657 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005658 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 spin_unlock_irqrestore(&tp->lock, flags);
5660 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005661
Francois Romieucebf8cc2007-10-18 12:06:54 +02005662 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663}
5664
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005665static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005666{
françois romieu065c27c2011-01-03 15:08:12 +00005667 struct rtl8169_private *tp = netdev_priv(dev);
5668
Francois Romieu5d06a992006-02-23 00:47:58 +01005669 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005670 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005671
françois romieu065c27c2011-01-03 15:08:12 +00005672 rtl_pll_power_down(tp);
5673
Francois Romieu5d06a992006-02-23 00:47:58 +01005674 netif_device_detach(dev);
5675 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005676}
Francois Romieu5d06a992006-02-23 00:47:58 +01005677
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005678#ifdef CONFIG_PM
5679
5680static int rtl8169_suspend(struct device *device)
5681{
5682 struct pci_dev *pdev = to_pci_dev(device);
5683 struct net_device *dev = pci_get_drvdata(pdev);
5684
5685 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005686
Francois Romieu5d06a992006-02-23 00:47:58 +01005687 return 0;
5688}
5689
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005690static void __rtl8169_resume(struct net_device *dev)
5691{
françois romieu065c27c2011-01-03 15:08:12 +00005692 struct rtl8169_private *tp = netdev_priv(dev);
5693
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005694 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005695
5696 rtl_pll_power_up(tp);
5697
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005698 rtl8169_schedule_work(dev, rtl8169_reset_task);
5699}
5700
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005701static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005702{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005703 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005704 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005705 struct rtl8169_private *tp = netdev_priv(dev);
5706
5707 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005708
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005709 if (netif_running(dev))
5710 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005711
Francois Romieu5d06a992006-02-23 00:47:58 +01005712 return 0;
5713}
5714
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005715static int rtl8169_runtime_suspend(struct device *device)
5716{
5717 struct pci_dev *pdev = to_pci_dev(device);
5718 struct net_device *dev = pci_get_drvdata(pdev);
5719 struct rtl8169_private *tp = netdev_priv(dev);
5720
5721 if (!tp->TxDescArray)
5722 return 0;
5723
5724 spin_lock_irq(&tp->lock);
5725 tp->saved_wolopts = __rtl8169_get_wol(tp);
5726 __rtl8169_set_wol(tp, WAKE_ANY);
5727 spin_unlock_irq(&tp->lock);
5728
5729 rtl8169_net_suspend(dev);
5730
5731 return 0;
5732}
5733
5734static int rtl8169_runtime_resume(struct device *device)
5735{
5736 struct pci_dev *pdev = to_pci_dev(device);
5737 struct net_device *dev = pci_get_drvdata(pdev);
5738 struct rtl8169_private *tp = netdev_priv(dev);
5739
5740 if (!tp->TxDescArray)
5741 return 0;
5742
5743 spin_lock_irq(&tp->lock);
5744 __rtl8169_set_wol(tp, tp->saved_wolopts);
5745 tp->saved_wolopts = 0;
5746 spin_unlock_irq(&tp->lock);
5747
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005748 rtl8169_init_phy(dev, tp);
5749
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005750 __rtl8169_resume(dev);
5751
5752 return 0;
5753}
5754
5755static int rtl8169_runtime_idle(struct device *device)
5756{
5757 struct pci_dev *pdev = to_pci_dev(device);
5758 struct net_device *dev = pci_get_drvdata(pdev);
5759 struct rtl8169_private *tp = netdev_priv(dev);
5760
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005761 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005762}
5763
Alexey Dobriyan47145212009-12-14 18:00:08 -08005764static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005765 .suspend = rtl8169_suspend,
5766 .resume = rtl8169_resume,
5767 .freeze = rtl8169_suspend,
5768 .thaw = rtl8169_resume,
5769 .poweroff = rtl8169_suspend,
5770 .restore = rtl8169_resume,
5771 .runtime_suspend = rtl8169_runtime_suspend,
5772 .runtime_resume = rtl8169_runtime_resume,
5773 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005774};
5775
5776#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5777
5778#else /* !CONFIG_PM */
5779
5780#define RTL8169_PM_OPS NULL
5781
5782#endif /* !CONFIG_PM */
5783
Francois Romieu1765f952008-09-13 17:21:40 +02005784static void rtl_shutdown(struct pci_dev *pdev)
5785{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005786 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005787 struct rtl8169_private *tp = netdev_priv(dev);
5788 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005789
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005790 rtl8169_net_suspend(dev);
5791
Francois Romieucecb5fd2011-04-01 10:21:07 +02005792 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005793 rtl_rar_set(tp, dev->perm_addr);
5794
françois romieu4bb3f522009-06-17 11:41:45 +00005795 spin_lock_irq(&tp->lock);
5796
Hayes Wang92fc43b2011-07-06 15:58:03 +08005797 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005798
5799 spin_unlock_irq(&tp->lock);
5800
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005801 if (system_state == SYSTEM_POWER_OFF) {
Hayes Wangaaa89c02011-07-06 15:58:08 +08005802 /* WoL fails with 8168b when the receiver is disabled. */
5803 if ((tp->mac_version == RTL_GIGA_MAC_VER_11 ||
5804 tp->mac_version == RTL_GIGA_MAC_VER_12 ||
5805 tp->mac_version == RTL_GIGA_MAC_VER_17) &&
5806 (tp->features & RTL_FEATURE_WOL)) {
françois romieuca52efd2009-07-24 12:34:19 +00005807 pci_clear_master(pdev);
5808
5809 RTL_W8(ChipCmd, CmdRxEnb);
5810 /* PCI commit */
5811 RTL_R8(ChipCmd);
5812 }
5813
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005814 pci_wake_from_d3(pdev, true);
5815 pci_set_power_state(pdev, PCI_D3hot);
5816 }
5817}
Francois Romieu5d06a992006-02-23 00:47:58 +01005818
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819static struct pci_driver rtl8169_pci_driver = {
5820 .name = MODULENAME,
5821 .id_table = rtl8169_pci_tbl,
5822 .probe = rtl8169_init_one,
5823 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005824 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005825 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826};
5827
Francois Romieu07d3f512007-02-21 22:40:46 +01005828static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829{
Jeff Garzik29917622006-08-19 17:48:59 -04005830 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831}
5832
Francois Romieu07d3f512007-02-21 22:40:46 +01005833static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834{
5835 pci_unregister_driver(&rtl8169_pci_driver);
5836}
5837
5838module_init(rtl8169_init_module);
5839module_exit(rtl8169_cleanup_module);