blob: b55fba7f3a0413395d33c2c8a01f0361972410c0 [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 Romieu9dccf612006-05-14 12:31:17 +0200410 RxFOVF = (1 << 23),
411 RxRWT = (1 << 22),
412 RxRES = (1 << 21),
413 RxRUNT = (1 << 20),
414 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* ChipCmdBits */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800417 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100418 CmdReset = 0x10,
419 CmdRxEnb = 0x08,
420 CmdTxEnb = 0x04,
421 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Francois Romieu275391a2007-02-23 23:50:28 +0100423 /* TXPoll register p.5 */
424 HPQ = 0x80, /* Poll cmd on the high prio queue */
425 NPQ = 0x40, /* Poll cmd on the low prio queue */
426 FSWInt = 0x01, /* Forced software interrupt */
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100429 Cfg9346_Lock = 0x00,
430 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100433 AcceptErr = 0x20,
434 AcceptRunt = 0x10,
435 AcceptBroadcast = 0x08,
436 AcceptMulticast = 0x04,
437 AcceptMyPhys = 0x02,
438 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200439#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* TxConfigBits */
442 TxInterFrameGapShift = 24,
443 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
444
Francois Romieu5d06a992006-02-23 00:47:58 +0100445 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200446 LEDS1 = (1 << 7),
447 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200448 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200449 Speed_down = (1 << 4),
450 MEMMAP = (1 << 3),
451 IOMAP = (1 << 2),
452 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100453 PMEnable = (1 << 0), /* Power Management Enable */
454
Francois Romieu6dccd162007-02-13 23:38:05 +0100455 /* Config2 register p. 25 */
456 PCI_Clock_66MHz = 0x01,
457 PCI_Clock_33MHz = 0x00,
458
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100459 /* Config3 register p.25 */
460 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
461 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200462 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100463
Francois Romieu5d06a992006-02-23 00:47:58 +0100464 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100465 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
466 MWF = (1 << 5), /* Accept Multicast wakeup frame */
467 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200468 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100469 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100470 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* TBICSR p.28 */
473 TBIReset = 0x80000000,
474 TBILoopback = 0x40000000,
475 TBINwEnable = 0x20000000,
476 TBINwRestart = 0x10000000,
477 TBILinkOk = 0x02000000,
478 TBINwComplete = 0x01000000,
479
480 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200481 EnableBist = (1 << 15), // 8168 8101
482 Mac_dbgo_oe = (1 << 14), // 8168 8101
483 Normal_mode = (1 << 13), // unused
484 Force_half_dup = (1 << 12), // 8168 8101
485 Force_rxflow_en = (1 << 11), // 8168 8101
486 Force_txflow_en = (1 << 10), // 8168 8101
487 Cxpl_dbg_sel = (1 << 9), // 8168 8101
488 ASF = (1 << 8), // 8168 8101
489 PktCntrDisable = (1 << 7), // 8168 8101
490 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 RxVlan = (1 << 6),
492 RxChkSum = (1 << 5),
493 PCIDAC = (1 << 4),
494 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100495 INTT_0 = 0x0000, // 8168
496 INTT_1 = 0x0001, // 8168
497 INTT_2 = 0x0002, // 8168
498 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100501 TBI_Enable = 0x80,
502 TxFlowCtrl = 0x40,
503 RxFlowCtrl = 0x20,
504 _1000bpsF = 0x10,
505 _100bps = 0x08,
506 _10bps = 0x04,
507 LinkStatus = 0x02,
508 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100511 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200512
513 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100514 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515};
516
Francois Romieu2b7b4312011-04-18 22:53:24 -0700517enum rtl_desc_bit {
518 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
520 RingEnd = (1 << 30), /* End of descriptor ring */
521 FirstFrag = (1 << 29), /* First segment of a packet */
522 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700523};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Francois Romieu2b7b4312011-04-18 22:53:24 -0700525/* Generic case. */
526enum rtl_tx_desc_bit {
527 /* First doubleword. */
528 TD_LSO = (1 << 27), /* Large Send Offload */
529#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Francois Romieu2b7b4312011-04-18 22:53:24 -0700531 /* Second doubleword. */
532 TxVlanTag = (1 << 17), /* Add VLAN tag */
533};
534
535/* 8169, 8168b and 810x except 8102e. */
536enum rtl_tx_desc_bit_0 {
537 /* First doubleword. */
538#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
539 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
540 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
541 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
542};
543
544/* 8102e, 8168c and beyond. */
545enum rtl_tx_desc_bit_1 {
546 /* Second doubleword. */
547#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
548 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
549 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
550 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
551};
552
553static const struct rtl_tx_desc_info {
554 struct {
555 u32 udp;
556 u32 tcp;
557 } checksum;
558 u16 mss_shift;
559 u16 opts_offset;
560} tx_desc_info [] = {
561 [RTL_TD_0] = {
562 .checksum = {
563 .udp = TD0_IP_CS | TD0_UDP_CS,
564 .tcp = TD0_IP_CS | TD0_TCP_CS
565 },
566 .mss_shift = TD0_MSS_SHIFT,
567 .opts_offset = 0
568 },
569 [RTL_TD_1] = {
570 .checksum = {
571 .udp = TD1_IP_CS | TD1_UDP_CS,
572 .tcp = TD1_IP_CS | TD1_TCP_CS
573 },
574 .mss_shift = TD1_MSS_SHIFT,
575 .opts_offset = 1
576 }
577};
578
579enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* Rx private */
581 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
582 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
583
584#define RxProtoUDP (PID1)
585#define RxProtoTCP (PID0)
586#define RxProtoIP (PID1 | PID0)
587#define RxProtoMask RxProtoIP
588
589 IPFail = (1 << 16), /* IP checksum failed */
590 UDPFail = (1 << 15), /* UDP/IP checksum failed */
591 TCPFail = (1 << 14), /* TCP/IP checksum failed */
592 RxVlanTag = (1 << 16), /* VLAN tag available */
593};
594
595#define RsvdMask 0x3fffc000
596
597struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200598 __le32 opts1;
599 __le32 opts2;
600 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601};
602
603struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200604 __le32 opts1;
605 __le32 opts2;
606 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607};
608
609struct ring_info {
610 struct sk_buff *skb;
611 u32 len;
612 u8 __pad[sizeof(void *) - sizeof(u32)];
613};
614
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200615enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200616 RTL_FEATURE_WOL = (1 << 0),
617 RTL_FEATURE_MSI = (1 << 1),
618 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200619};
620
Ivan Vecera355423d2009-02-06 21:49:57 -0800621struct rtl8169_counters {
622 __le64 tx_packets;
623 __le64 rx_packets;
624 __le64 tx_errors;
625 __le32 rx_errors;
626 __le16 rx_missed;
627 __le16 align_errors;
628 __le32 tx_one_collision;
629 __le32 tx_multi_collision;
630 __le64 rx_unicast;
631 __le64 rx_broadcast;
632 __le32 rx_multicast;
633 __le16 tx_aborted;
634 __le16 tx_underun;
635};
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637struct rtl8169_private {
638 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200639 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000640 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700641 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200642 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200643 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700644 u16 txd_version;
645 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
647 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
648 u32 dirty_rx;
649 u32 dirty_tx;
650 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
651 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
652 dma_addr_t TxPhyAddr;
653 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000654 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct timer_list timer;
657 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100658 u16 intr_event;
659 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000661
662 struct mdio_ops {
663 void (*write)(void __iomem *, int, int);
664 int (*read)(void __iomem *, int);
665 } mdio_ops;
666
françois romieu065c27c2011-01-03 15:08:12 +0000667 struct pll_power_ops {
668 void (*down)(struct rtl8169_private *);
669 void (*up)(struct rtl8169_private *);
670 } pll_power_ops;
671
Oliver Neukum54405cd2011-01-06 21:55:13 +0100672 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200673 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000674 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100675 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000676 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800678 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
David Howellsc4028952006-11-22 14:57:56 +0000679 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200680 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200681
682 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800683 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000684 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000685
Francois Romieub6ffd972011-06-17 17:00:05 +0200686 struct rtl_fw {
687 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200688
689#define RTL_VER_SIZE 32
690
691 char version[RTL_VER_SIZE];
692
693 struct rtl_fw_phy_action {
694 __le32 *code;
695 size_t size;
696 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200697 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300698#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
Ralf Baechle979b6c12005-06-13 14:30:40 -0700701MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700704MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200705module_param_named(debug, debug.msg_enable, int, 0);
706MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707MODULE_LICENSE("GPL");
708MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000709MODULE_FIRMWARE(FIRMWARE_8168D_1);
710MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000711MODULE_FIRMWARE(FIRMWARE_8168E_1);
712MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wangbbb8af72011-09-06 16:55:17 +0800713MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800714MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000717static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
718 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100719static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100721static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100723static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200725static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700727 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200728static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200730static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700731static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
françois romieub646d902011-01-03 15:08:21 +0000733static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
734{
735 void __iomem *ioaddr = tp->mmio_addr;
736 int i;
737
738 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
739 for (i = 0; i < 20; i++) {
740 udelay(100);
741 if (RTL_R32(OCPAR) & OCPAR_FLAG)
742 break;
743 }
744 return RTL_R32(OCPDR);
745}
746
747static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
748{
749 void __iomem *ioaddr = tp->mmio_addr;
750 int i;
751
752 RTL_W32(OCPDR, data);
753 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
754 for (i = 0; i < 20; i++) {
755 udelay(100);
756 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
757 break;
758 }
759}
760
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800761static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000762{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800763 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000764 int i;
765
766 RTL_W8(ERIDR, cmd);
767 RTL_W32(ERIAR, 0x800010e8);
768 msleep(2);
769 for (i = 0; i < 5; i++) {
770 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200771 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000772 break;
773 }
774
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800775 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000776}
777
778#define OOB_CMD_RESET 0x00
779#define OOB_CMD_DRIVER_START 0x05
780#define OOB_CMD_DRIVER_STOP 0x06
781
Francois Romieucecb5fd2011-04-01 10:21:07 +0200782static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
783{
784 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
785}
786
françois romieub646d902011-01-03 15:08:21 +0000787static void rtl8168_driver_start(struct rtl8169_private *tp)
788{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200789 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000790 int i;
791
792 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
793
Francois Romieucecb5fd2011-04-01 10:21:07 +0200794 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000795
françois romieub646d902011-01-03 15:08:21 +0000796 for (i = 0; i < 10; i++) {
797 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000798 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000799 break;
800 }
801}
802
803static void rtl8168_driver_stop(struct rtl8169_private *tp)
804{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200805 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000806 int i;
807
808 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
809
Francois Romieucecb5fd2011-04-01 10:21:07 +0200810 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000811
françois romieub646d902011-01-03 15:08:21 +0000812 for (i = 0; i < 10; i++) {
813 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000814 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000815 break;
816 }
817}
818
hayeswang4804b3b2011-03-21 01:50:29 +0000819static int r8168dp_check_dash(struct rtl8169_private *tp)
820{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200821 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000822
Francois Romieucecb5fd2011-04-01 10:21:07 +0200823 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000824}
françois romieub646d902011-01-03 15:08:21 +0000825
françois romieu4da19632011-01-03 15:07:55 +0000826static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 int i;
829
Francois Romieua6baf3a2007-11-08 23:23:21 +0100830 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Francois Romieu23714082006-01-29 00:49:09 +0100832 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100833 /*
834 * Check if the RTL8169 has completed writing to the specified
835 * MII register.
836 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200837 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 break;
Francois Romieu23714082006-01-29 00:49:09 +0100839 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700841 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700842 * According to hardware specs a 20us delay is required after write
843 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700844 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700845 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
françois romieu4da19632011-01-03 15:07:55 +0000848static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 int i, value = -1;
851
Francois Romieua6baf3a2007-11-08 23:23:21 +0100852 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Francois Romieu23714082006-01-29 00:49:09 +0100854 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100855 /*
856 * Check if the RTL8169 has completed retrieving data from
857 * the specified MII register.
858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100860 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 }
Francois Romieu23714082006-01-29 00:49:09 +0100863 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700865 /*
866 * According to hardware specs a 20us delay is required after read
867 * complete indication, but before sending next command.
868 */
869 udelay(20);
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return value;
872}
873
françois romieuc0e45c12011-01-03 15:08:04 +0000874static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
875{
876 int i;
877
878 RTL_W32(OCPDR, data |
879 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
880 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
881 RTL_W32(EPHY_RXER_NUM, 0);
882
883 for (i = 0; i < 100; i++) {
884 mdelay(1);
885 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
886 break;
887 }
888}
889
890static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
891{
892 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
893 (value & OCPDR_DATA_MASK));
894}
895
896static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
897{
898 int i;
899
900 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
901
902 mdelay(1);
903 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
904 RTL_W32(EPHY_RXER_NUM, 0);
905
906 for (i = 0; i < 100; i++) {
907 mdelay(1);
908 if (RTL_R32(OCPAR) & OCPAR_FLAG)
909 break;
910 }
911
912 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
913}
914
françois romieue6de30d2011-01-03 15:08:37 +0000915#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
916
917static void r8168dp_2_mdio_start(void __iomem *ioaddr)
918{
919 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
920}
921
922static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
923{
924 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
925}
926
927static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
928{
929 r8168dp_2_mdio_start(ioaddr);
930
931 r8169_mdio_write(ioaddr, reg_addr, value);
932
933 r8168dp_2_mdio_stop(ioaddr);
934}
935
936static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
937{
938 int value;
939
940 r8168dp_2_mdio_start(ioaddr);
941
942 value = r8169_mdio_read(ioaddr, reg_addr);
943
944 r8168dp_2_mdio_stop(ioaddr);
945
946 return value;
947}
948
françois romieu4da19632011-01-03 15:07:55 +0000949static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200950{
françois romieuc0e45c12011-01-03 15:08:04 +0000951 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200952}
953
françois romieu4da19632011-01-03 15:07:55 +0000954static int rtl_readphy(struct rtl8169_private *tp, int location)
955{
françois romieuc0e45c12011-01-03 15:08:04 +0000956 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000957}
958
959static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
960{
961 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
962}
963
964static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000965{
966 int val;
967
françois romieu4da19632011-01-03 15:07:55 +0000968 val = rtl_readphy(tp, reg_addr);
969 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000970}
971
Francois Romieuccdffb92008-07-26 14:26:06 +0200972static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
973 int val)
974{
975 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200976
françois romieu4da19632011-01-03 15:07:55 +0000977 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200978}
979
980static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
981{
982 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200983
françois romieu4da19632011-01-03 15:07:55 +0000984 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200985}
986
Francois Romieudacf8152008-08-02 20:44:13 +0200987static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
988{
989 unsigned int i;
990
991 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
992 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
993
994 for (i = 0; i < 100; i++) {
995 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
996 break;
997 udelay(10);
998 }
999}
1000
1001static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1002{
1003 u16 value = 0xffff;
1004 unsigned int i;
1005
1006 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1007
1008 for (i = 0; i < 100; i++) {
1009 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1010 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1011 break;
1012 }
1013 udelay(10);
1014 }
1015
1016 return value;
1017}
1018
1019static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1020{
1021 unsigned int i;
1022
1023 RTL_W32(CSIDR, value);
1024 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1025 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1026
1027 for (i = 0; i < 100; i++) {
1028 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1029 break;
1030 udelay(10);
1031 }
1032}
1033
1034static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1035{
1036 u32 value = ~0x00;
1037 unsigned int i;
1038
1039 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1040 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1041
1042 for (i = 0; i < 100; i++) {
1043 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1044 value = RTL_R32(CSIDR);
1045 break;
1046 }
1047 udelay(10);
1048 }
1049
1050 return value;
1051}
1052
Hayes Wang133ac402011-07-06 15:58:05 +08001053static
1054void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1055{
1056 unsigned int i;
1057
1058 BUG_ON((addr & 3) || (mask == 0));
1059 RTL_W32(ERIDR, val);
1060 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1061
1062 for (i = 0; i < 100; i++) {
1063 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1064 break;
1065 udelay(100);
1066 }
1067}
1068
1069static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1070{
1071 u32 value = ~0x00;
1072 unsigned int i;
1073
1074 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1075
1076 for (i = 0; i < 100; i++) {
1077 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1078 value = RTL_R32(ERIDR);
1079 break;
1080 }
1081 udelay(100);
1082 }
1083
1084 return value;
1085}
1086
1087static void
1088rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1089{
1090 u32 val;
1091
1092 val = rtl_eri_read(ioaddr, addr, type);
1093 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1094}
1095
françois romieuc28aa382011-08-02 03:53:43 +00001096struct exgmac_reg {
1097 u16 addr;
1098 u16 mask;
1099 u32 val;
1100};
1101
1102static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1103 const struct exgmac_reg *r, int len)
1104{
1105 while (len-- > 0) {
1106 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1107 r++;
1108 }
1109}
1110
françois romieudaf9df62009-10-07 12:44:20 +00001111static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1112{
1113 u8 value = 0xff;
1114 unsigned int i;
1115
1116 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1117
1118 for (i = 0; i < 300; i++) {
1119 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1120 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1121 break;
1122 }
1123 udelay(100);
1124 }
1125
1126 return value;
1127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1130{
1131 RTL_W16(IntrMask, 0x0000);
1132
1133 RTL_W16(IntrStatus, 0xffff);
1134}
1135
françois romieu4da19632011-01-03 15:07:55 +00001136static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
françois romieu4da19632011-01-03 15:07:55 +00001138 void __iomem *ioaddr = tp->mmio_addr;
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return RTL_R32(TBICSR) & TBIReset;
1141}
1142
françois romieu4da19632011-01-03 15:07:55 +00001143static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
françois romieu4da19632011-01-03 15:07:55 +00001145 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1149{
1150 return RTL_R32(TBICSR) & TBILinkOk;
1151}
1152
1153static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1154{
1155 return RTL_R8(PHYstatus) & LinkStatus;
1156}
1157
françois romieu4da19632011-01-03 15:07:55 +00001158static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
françois romieu4da19632011-01-03 15:07:55 +00001160 void __iomem *ioaddr = tp->mmio_addr;
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1163}
1164
françois romieu4da19632011-01-03 15:07:55 +00001165static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 unsigned int val;
1168
françois romieu4da19632011-01-03 15:07:55 +00001169 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1170 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Hayes Wang70090422011-07-06 15:58:06 +08001173static void rtl_link_chg_patch(struct rtl8169_private *tp)
1174{
1175 void __iomem *ioaddr = tp->mmio_addr;
1176 struct net_device *dev = tp->dev;
1177
1178 if (!netif_running(dev))
1179 return;
1180
1181 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1182 if (RTL_R8(PHYstatus) & _1000bpsF) {
1183 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1184 0x00000011, ERIAR_EXGMAC);
1185 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1186 0x00000005, ERIAR_EXGMAC);
1187 } else if (RTL_R8(PHYstatus) & _100bps) {
1188 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1189 0x0000001f, ERIAR_EXGMAC);
1190 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1191 0x00000005, ERIAR_EXGMAC);
1192 } else {
1193 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1194 0x0000001f, ERIAR_EXGMAC);
1195 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1196 0x0000003f, ERIAR_EXGMAC);
1197 }
1198 /* Reset packet filter */
1199 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1200 ERIAR_EXGMAC);
1201 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1202 ERIAR_EXGMAC);
1203 }
1204}
1205
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001206static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001207 struct rtl8169_private *tp,
1208 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 unsigned long flags;
1211
1212 spin_lock_irqsave(&tp->lock, flags);
1213 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001214 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001215 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001216 if (pm)
1217 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001219 if (net_ratelimit())
1220 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001221 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001223 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001224 if (pm)
1225 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 spin_unlock_irqrestore(&tp->lock, flags);
1228}
1229
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001230static void rtl8169_check_link_status(struct net_device *dev,
1231 struct rtl8169_private *tp,
1232 void __iomem *ioaddr)
1233{
1234 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1235}
1236
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001237#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1238
1239static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1240{
1241 void __iomem *ioaddr = tp->mmio_addr;
1242 u8 options;
1243 u32 wolopts = 0;
1244
1245 options = RTL_R8(Config1);
1246 if (!(options & PMEnable))
1247 return 0;
1248
1249 options = RTL_R8(Config3);
1250 if (options & LinkUp)
1251 wolopts |= WAKE_PHY;
1252 if (options & MagicPacket)
1253 wolopts |= WAKE_MAGIC;
1254
1255 options = RTL_R8(Config5);
1256 if (options & UWF)
1257 wolopts |= WAKE_UCAST;
1258 if (options & BWF)
1259 wolopts |= WAKE_BCAST;
1260 if (options & MWF)
1261 wolopts |= WAKE_MCAST;
1262
1263 return wolopts;
1264}
1265
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001266static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1267{
1268 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001269
1270 spin_lock_irq(&tp->lock);
1271
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001272 wol->supported = WAKE_ANY;
1273 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001274
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001275 spin_unlock_irq(&tp->lock);
1276}
1277
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001278static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001279{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001280 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001281 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001282 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001283 u32 opt;
1284 u16 reg;
1285 u8 mask;
1286 } cfg[] = {
1287 { WAKE_ANY, Config1, PMEnable },
1288 { WAKE_PHY, Config3, LinkUp },
1289 { WAKE_MAGIC, Config3, MagicPacket },
1290 { WAKE_UCAST, Config5, UWF },
1291 { WAKE_BCAST, Config5, BWF },
1292 { WAKE_MCAST, Config5, MWF },
1293 { WAKE_ANY, Config5, LanWake }
1294 };
1295
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001296 RTL_W8(Cfg9346, Cfg9346_Unlock);
1297
1298 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1299 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001300 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001301 options |= cfg[i].mask;
1302 RTL_W8(cfg[i].reg, options);
1303 }
1304
1305 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001306}
1307
1308static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1309{
1310 struct rtl8169_private *tp = netdev_priv(dev);
1311
1312 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001313
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001314 if (wol->wolopts)
1315 tp->features |= RTL_FEATURE_WOL;
1316 else
1317 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001318 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001319 spin_unlock_irq(&tp->lock);
1320
françois romieuea809072010-11-08 13:23:58 +00001321 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1322
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001323 return 0;
1324}
1325
Francois Romieu31bd2042011-04-26 18:58:59 +02001326static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1327{
Francois Romieu85bffe62011-04-27 08:22:39 +02001328 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001329}
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331static void rtl8169_get_drvinfo(struct net_device *dev,
1332 struct ethtool_drvinfo *info)
1333{
1334 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001335 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 strcpy(info->driver, MODULENAME);
1338 strcpy(info->version, RTL8169_VERSION);
1339 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001340 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1341 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1342 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345static int rtl8169_get_regs_len(struct net_device *dev)
1346{
1347 return R8169_REGS_SIZE;
1348}
1349
1350static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001351 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 struct rtl8169_private *tp = netdev_priv(dev);
1354 void __iomem *ioaddr = tp->mmio_addr;
1355 int ret = 0;
1356 u32 reg;
1357
1358 reg = RTL_R32(TBICSR);
1359 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1360 (duplex == DUPLEX_FULL)) {
1361 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1362 } else if (autoneg == AUTONEG_ENABLE)
1363 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1364 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001365 netif_warn(tp, link, dev,
1366 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 ret = -EOPNOTSUPP;
1368 }
1369
1370 return ret;
1371}
1372
1373static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001374 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001377 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001378 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Hayes Wang716b50a2011-02-22 17:26:18 +08001380 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001383 int auto_nego;
1384
françois romieu4da19632011-01-03 15:07:55 +00001385 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001386 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1387 ADVERTISE_100HALF | ADVERTISE_100FULL);
1388
1389 if (adv & ADVERTISED_10baseT_Half)
1390 auto_nego |= ADVERTISE_10HALF;
1391 if (adv & ADVERTISED_10baseT_Full)
1392 auto_nego |= ADVERTISE_10FULL;
1393 if (adv & ADVERTISED_100baseT_Half)
1394 auto_nego |= ADVERTISE_100HALF;
1395 if (adv & ADVERTISED_100baseT_Full)
1396 auto_nego |= ADVERTISE_100FULL;
1397
françois romieu3577aa12009-05-19 10:46:48 +00001398 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1399
françois romieu4da19632011-01-03 15:07:55 +00001400 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001401 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1402
1403 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001404 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001405 if (adv & ADVERTISED_1000baseT_Half)
1406 giga_ctrl |= ADVERTISE_1000HALF;
1407 if (adv & ADVERTISED_1000baseT_Full)
1408 giga_ctrl |= ADVERTISE_1000FULL;
1409 } else if (adv & (ADVERTISED_1000baseT_Half |
1410 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001411 netif_info(tp, link, dev,
1412 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001413 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
françois romieu3577aa12009-05-19 10:46:48 +00001416 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001417
françois romieu4da19632011-01-03 15:07:55 +00001418 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1419 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001420 } else {
1421 giga_ctrl = 0;
1422
1423 if (speed == SPEED_10)
1424 bmcr = 0;
1425 else if (speed == SPEED_100)
1426 bmcr = BMCR_SPEED100;
1427 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001428 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001429
1430 if (duplex == DUPLEX_FULL)
1431 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001432 }
1433
françois romieu4da19632011-01-03 15:07:55 +00001434 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001435
Francois Romieucecb5fd2011-04-01 10:21:07 +02001436 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1437 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001438 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001439 rtl_writephy(tp, 0x17, 0x2138);
1440 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001441 } else {
françois romieu4da19632011-01-03 15:07:55 +00001442 rtl_writephy(tp, 0x17, 0x2108);
1443 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001444 }
1445 }
1446
Oliver Neukum54405cd2011-01-06 21:55:13 +01001447 rc = 0;
1448out:
1449 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
1452static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001453 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 struct rtl8169_private *tp = netdev_priv(dev);
1456 int ret;
1457
Oliver Neukum54405cd2011-01-06 21:55:13 +01001458 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001459 if (ret < 0)
1460 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Francois Romieu4876cc12011-03-11 21:07:11 +01001462 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1463 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001465 }
1466out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return ret;
1468}
1469
1470static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1471{
1472 struct rtl8169_private *tp = netdev_priv(dev);
1473 unsigned long flags;
1474 int ret;
1475
Francois Romieu4876cc12011-03-11 21:07:11 +01001476 del_timer_sync(&tp->timer);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001479 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001480 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return ret;
1484}
1485
Michał Mirosław350fb322011-04-08 06:35:56 +00001486static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001488 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001489 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Michał Mirosław350fb322011-04-08 06:35:56 +00001491 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
Michał Mirosław350fb322011-04-08 06:35:56 +00001494static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 struct rtl8169_private *tp = netdev_priv(dev);
1497 void __iomem *ioaddr = tp->mmio_addr;
1498 unsigned long flags;
1499
1500 spin_lock_irqsave(&tp->lock, flags);
1501
Michał Mirosław350fb322011-04-08 06:35:56 +00001502 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 tp->cp_cmd |= RxChkSum;
1504 else
1505 tp->cp_cmd &= ~RxChkSum;
1506
Michał Mirosław350fb322011-04-08 06:35:56 +00001507 if (dev->features & NETIF_F_HW_VLAN_RX)
1508 tp->cp_cmd |= RxVlan;
1509 else
1510 tp->cp_cmd &= ~RxVlan;
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 RTL_W16(CPlusCmd, tp->cp_cmd);
1513 RTL_R16(CPlusCmd);
1514
1515 spin_unlock_irqrestore(&tp->lock, flags);
1516
1517 return 0;
1518}
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1521 struct sk_buff *skb)
1522{
Jesse Grosseab6d182010-10-20 13:56:03 +00001523 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1525}
1526
Francois Romieu7a8fc772011-03-01 17:18:33 +01001527static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Francois Romieu7a8fc772011-03-01 17:18:33 +01001531 if (opts2 & RxVlanTag)
1532 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Francois Romieuccdffb92008-07-26 14:26:06 +02001537static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
1539 struct rtl8169_private *tp = netdev_priv(dev);
1540 void __iomem *ioaddr = tp->mmio_addr;
1541 u32 status;
1542
1543 cmd->supported =
1544 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1545 cmd->port = PORT_FIBRE;
1546 cmd->transceiver = XCVR_INTERNAL;
1547
1548 status = RTL_R32(TBICSR);
1549 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1550 cmd->autoneg = !!(status & TBINwEnable);
1551
David Decotigny70739492011-04-27 18:32:40 +00001552 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001554
1555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Francois Romieuccdffb92008-07-26 14:26:06 +02001558static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Francois Romieuccdffb92008-07-26 14:26:06 +02001562 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1566{
1567 struct rtl8169_private *tp = netdev_priv(dev);
1568 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001569 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 spin_lock_irqsave(&tp->lock, flags);
1572
Francois Romieuccdffb92008-07-26 14:26:06 +02001573 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001576 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
1579static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1580 void *p)
1581{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001582 struct rtl8169_private *tp = netdev_priv(dev);
1583 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Francois Romieu5b0384f2006-08-16 16:00:01 +02001585 if (regs->len > R8169_REGS_SIZE)
1586 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Francois Romieu5b0384f2006-08-16 16:00:01 +02001588 spin_lock_irqsave(&tp->lock, flags);
1589 memcpy_fromio(p, tp->mmio_addr, regs->len);
1590 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001593static u32 rtl8169_get_msglevel(struct net_device *dev)
1594{
1595 struct rtl8169_private *tp = netdev_priv(dev);
1596
1597 return tp->msg_enable;
1598}
1599
1600static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1601{
1602 struct rtl8169_private *tp = netdev_priv(dev);
1603
1604 tp->msg_enable = value;
1605}
1606
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001607static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1608 "tx_packets",
1609 "rx_packets",
1610 "tx_errors",
1611 "rx_errors",
1612 "rx_missed",
1613 "align_errors",
1614 "tx_single_collisions",
1615 "tx_multi_collisions",
1616 "unicast",
1617 "broadcast",
1618 "multicast",
1619 "tx_aborted",
1620 "tx_underrun",
1621};
1622
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001623static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001624{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001625 switch (sset) {
1626 case ETH_SS_STATS:
1627 return ARRAY_SIZE(rtl8169_gstrings);
1628 default:
1629 return -EOPNOTSUPP;
1630 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001631}
1632
Ivan Vecera355423d2009-02-06 21:49:57 -08001633static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001634{
1635 struct rtl8169_private *tp = netdev_priv(dev);
1636 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001637 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001638 struct rtl8169_counters *counters;
1639 dma_addr_t paddr;
1640 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001641 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001642
Ivan Vecera355423d2009-02-06 21:49:57 -08001643 /*
1644 * Some chips are unable to dump tally counters when the receiver
1645 * is disabled.
1646 */
1647 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1648 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001649
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001650 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001651 if (!counters)
1652 return;
1653
1654 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001655 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001656 RTL_W32(CounterAddrLow, cmd);
1657 RTL_W32(CounterAddrLow, cmd | CounterDump);
1658
Ivan Vecera355423d2009-02-06 21:49:57 -08001659 while (wait--) {
1660 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001661 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001662 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001663 }
1664 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001665 }
1666
1667 RTL_W32(CounterAddrLow, 0);
1668 RTL_W32(CounterAddrHigh, 0);
1669
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001670 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001671}
1672
Ivan Vecera355423d2009-02-06 21:49:57 -08001673static void rtl8169_get_ethtool_stats(struct net_device *dev,
1674 struct ethtool_stats *stats, u64 *data)
1675{
1676 struct rtl8169_private *tp = netdev_priv(dev);
1677
1678 ASSERT_RTNL();
1679
1680 rtl8169_update_counters(dev);
1681
1682 data[0] = le64_to_cpu(tp->counters.tx_packets);
1683 data[1] = le64_to_cpu(tp->counters.rx_packets);
1684 data[2] = le64_to_cpu(tp->counters.tx_errors);
1685 data[3] = le32_to_cpu(tp->counters.rx_errors);
1686 data[4] = le16_to_cpu(tp->counters.rx_missed);
1687 data[5] = le16_to_cpu(tp->counters.align_errors);
1688 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1689 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1690 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1691 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1692 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1693 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1694 data[12] = le16_to_cpu(tp->counters.tx_underun);
1695}
1696
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001697static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1698{
1699 switch(stringset) {
1700 case ETH_SS_STATS:
1701 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1702 break;
1703 }
1704}
1705
Jeff Garzik7282d492006-09-13 14:30:00 -04001706static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 .get_drvinfo = rtl8169_get_drvinfo,
1708 .get_regs_len = rtl8169_get_regs_len,
1709 .get_link = ethtool_op_get_link,
1710 .get_settings = rtl8169_get_settings,
1711 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001712 .get_msglevel = rtl8169_get_msglevel,
1713 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001715 .get_wol = rtl8169_get_wol,
1716 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001717 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001718 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001719 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720};
1721
Francois Romieu07d3f512007-02-21 22:40:46 +01001722static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001723 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Francois Romieu5d320a22011-05-08 17:47:36 +02001725 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001726 /*
1727 * The driver currently handles the 8168Bf and the 8168Be identically
1728 * but they can be identified more specifically through the test below
1729 * if needed:
1730 *
1731 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001732 *
1733 * Same thing for the 8101Eb and the 8101Ec:
1734 *
1735 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001736 */
Francois Romieu37441002011-06-17 22:58:54 +02001737 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001739 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 int mac_version;
1741 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001742 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001743 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001744 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1745 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1746 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1747
Francois Romieu5b538df2008-07-20 16:22:45 +02001748 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001749 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1750 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001751 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001752
françois romieue6de30d2011-01-03 15:08:37 +00001753 /* 8168DP family. */
1754 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1755 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001756 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001757
Francois Romieuef808d52008-06-29 13:10:54 +02001758 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001759 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001760 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001761 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001762 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001763 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1764 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001765 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001766 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001767 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001768
1769 /* 8168B family. */
1770 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1771 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1772 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1773 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1774
1775 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001776 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001777 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1778 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1779 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001780 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1781 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1782 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1783 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1784 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1785 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001786 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001787 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001788 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001789 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1790 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001791 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1792 /* FIXME: where did these entries come from ? -- FR */
1793 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1794 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1795
1796 /* 8110 family. */
1797 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1798 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1799 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1800 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1801 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1802 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1803
Jean Delvaref21b75e2009-05-26 20:54:48 -07001804 /* Catch-all */
1805 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001806 };
1807 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 u32 reg;
1809
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001810 reg = RTL_R32(TxConfig);
1811 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 p++;
1813 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001814
1815 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1816 netif_notice(tp, probe, dev,
1817 "unknown MAC, using family default\n");
1818 tp->mac_version = default_version;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
1822static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1823{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001824 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Francois Romieu867763c2007-08-17 18:21:58 +02001827struct phy_reg {
1828 u16 reg;
1829 u16 val;
1830};
1831
françois romieu4da19632011-01-03 15:07:55 +00001832static void rtl_writephy_batch(struct rtl8169_private *tp,
1833 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001834{
1835 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001836 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001837 regs++;
1838 }
1839}
1840
françois romieubca03d52011-01-03 15:07:31 +00001841#define PHY_READ 0x00000000
1842#define PHY_DATA_OR 0x10000000
1843#define PHY_DATA_AND 0x20000000
1844#define PHY_BJMPN 0x30000000
1845#define PHY_READ_EFUSE 0x40000000
1846#define PHY_READ_MAC_BYTE 0x50000000
1847#define PHY_WRITE_MAC_BYTE 0x60000000
1848#define PHY_CLEAR_READCOUNT 0x70000000
1849#define PHY_WRITE 0x80000000
1850#define PHY_READCOUNT_EQ_SKIP 0x90000000
1851#define PHY_COMP_EQ_SKIPN 0xa0000000
1852#define PHY_COMP_NEQ_SKIPN 0xb0000000
1853#define PHY_WRITE_PREVIOUS 0xc0000000
1854#define PHY_SKIPN 0xd0000000
1855#define PHY_DELAY_MS 0xe0000000
1856#define PHY_WRITE_ERI_WORD 0xf0000000
1857
Hayes Wang960aee62011-06-18 11:37:48 +02001858struct fw_info {
1859 u32 magic;
1860 char version[RTL_VER_SIZE];
1861 __le32 fw_start;
1862 __le32 fw_len;
1863 u8 chksum;
1864} __packed;
1865
Francois Romieu1c361ef2011-06-17 17:16:24 +02001866#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1867
1868static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001869{
Francois Romieub6ffd972011-06-17 17:00:05 +02001870 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001871 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001872 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1873 char *version = rtl_fw->version;
1874 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001875
Francois Romieu1c361ef2011-06-17 17:16:24 +02001876 if (fw->size < FW_OPCODE_SIZE)
1877 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001878
1879 if (!fw_info->magic) {
1880 size_t i, size, start;
1881 u8 checksum = 0;
1882
1883 if (fw->size < sizeof(*fw_info))
1884 goto out;
1885
1886 for (i = 0; i < fw->size; i++)
1887 checksum += fw->data[i];
1888 if (checksum != 0)
1889 goto out;
1890
1891 start = le32_to_cpu(fw_info->fw_start);
1892 if (start > fw->size)
1893 goto out;
1894
1895 size = le32_to_cpu(fw_info->fw_len);
1896 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1897 goto out;
1898
1899 memcpy(version, fw_info->version, RTL_VER_SIZE);
1900
1901 pa->code = (__le32 *)(fw->data + start);
1902 pa->size = size;
1903 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001904 if (fw->size % FW_OPCODE_SIZE)
1905 goto out;
1906
1907 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1908
1909 pa->code = (__le32 *)fw->data;
1910 pa->size = fw->size / FW_OPCODE_SIZE;
1911 }
1912 version[RTL_VER_SIZE - 1] = 0;
1913
1914 rc = true;
1915out:
1916 return rc;
1917}
1918
Francois Romieufd112f22011-06-18 00:10:29 +02001919static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1920 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02001921{
Francois Romieufd112f22011-06-18 00:10:29 +02001922 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001923 size_t index;
1924
Francois Romieu1c361ef2011-06-17 17:16:24 +02001925 for (index = 0; index < pa->size; index++) {
1926 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00001927 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001928
hayeswang42b82dc2011-01-10 02:07:25 +00001929 switch(action & 0xf0000000) {
1930 case PHY_READ:
1931 case PHY_DATA_OR:
1932 case PHY_DATA_AND:
1933 case PHY_READ_EFUSE:
1934 case PHY_CLEAR_READCOUNT:
1935 case PHY_WRITE:
1936 case PHY_WRITE_PREVIOUS:
1937 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001938 break;
1939
hayeswang42b82dc2011-01-10 02:07:25 +00001940 case PHY_BJMPN:
1941 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02001942 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001943 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001944 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001945 }
1946 break;
1947 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001948 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001949 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001950 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001951 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001952 }
1953 break;
1954 case PHY_COMP_EQ_SKIPN:
1955 case PHY_COMP_NEQ_SKIPN:
1956 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001957 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001958 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001959 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001960 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001961 }
1962 break;
1963
1964 case PHY_READ_MAC_BYTE:
1965 case PHY_WRITE_MAC_BYTE:
1966 case PHY_WRITE_ERI_WORD:
1967 default:
Francois Romieufd112f22011-06-18 00:10:29 +02001968 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00001969 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02001970 goto out;
françois romieubca03d52011-01-03 15:07:31 +00001971 }
1972 }
Francois Romieufd112f22011-06-18 00:10:29 +02001973 rc = true;
1974out:
1975 return rc;
1976}
françois romieubca03d52011-01-03 15:07:31 +00001977
Francois Romieufd112f22011-06-18 00:10:29 +02001978static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1979{
1980 struct net_device *dev = tp->dev;
1981 int rc = -EINVAL;
1982
1983 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1984 netif_err(tp, ifup, dev, "invalid firwmare\n");
1985 goto out;
1986 }
1987
1988 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1989 rc = 0;
1990out:
1991 return rc;
1992}
1993
1994static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1995{
1996 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1997 u32 predata, count;
1998 size_t index;
1999
2000 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002001
Francois Romieu1c361ef2011-06-17 17:16:24 +02002002 for (index = 0; index < pa->size; ) {
2003 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002004 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002005 u32 regno = (action & 0x0fff0000) >> 16;
2006
2007 if (!action)
2008 break;
françois romieubca03d52011-01-03 15:07:31 +00002009
2010 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002011 case PHY_READ:
2012 predata = rtl_readphy(tp, regno);
2013 count++;
2014 index++;
françois romieubca03d52011-01-03 15:07:31 +00002015 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002016 case PHY_DATA_OR:
2017 predata |= data;
2018 index++;
2019 break;
2020 case PHY_DATA_AND:
2021 predata &= data;
2022 index++;
2023 break;
2024 case PHY_BJMPN:
2025 index -= regno;
2026 break;
2027 case PHY_READ_EFUSE:
2028 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2029 index++;
2030 break;
2031 case PHY_CLEAR_READCOUNT:
2032 count = 0;
2033 index++;
2034 break;
2035 case PHY_WRITE:
2036 rtl_writephy(tp, regno, data);
2037 index++;
2038 break;
2039 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002040 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002041 break;
2042 case PHY_COMP_EQ_SKIPN:
2043 if (predata == data)
2044 index += regno;
2045 index++;
2046 break;
2047 case PHY_COMP_NEQ_SKIPN:
2048 if (predata != data)
2049 index += regno;
2050 index++;
2051 break;
2052 case PHY_WRITE_PREVIOUS:
2053 rtl_writephy(tp, regno, predata);
2054 index++;
2055 break;
2056 case PHY_SKIPN:
2057 index += regno + 1;
2058 break;
2059 case PHY_DELAY_MS:
2060 mdelay(data);
2061 index++;
2062 break;
2063
2064 case PHY_READ_MAC_BYTE:
2065 case PHY_WRITE_MAC_BYTE:
2066 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002067 default:
2068 BUG();
2069 }
2070 }
2071}
2072
françois romieuf1e02ed2011-01-13 13:07:53 +00002073static void rtl_release_firmware(struct rtl8169_private *tp)
2074{
Francois Romieub6ffd972011-06-17 17:00:05 +02002075 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2076 release_firmware(tp->rtl_fw->fw);
2077 kfree(tp->rtl_fw);
2078 }
2079 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002080}
2081
François Romieu953a12c2011-04-24 17:38:48 +02002082static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002083{
Francois Romieub6ffd972011-06-17 17:00:05 +02002084 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002085
2086 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002087 if (!IS_ERR_OR_NULL(rtl_fw))
2088 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002089}
2090
2091static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2092{
2093 if (rtl_readphy(tp, reg) != val)
2094 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2095 else
2096 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002097}
2098
françois romieu4da19632011-01-03 15:07:55 +00002099static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002101 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002102 { 0x1f, 0x0001 },
2103 { 0x06, 0x006e },
2104 { 0x08, 0x0708 },
2105 { 0x15, 0x4000 },
2106 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
françois romieu0b9b5712009-08-10 19:44:56 +00002108 { 0x1f, 0x0001 },
2109 { 0x03, 0x00a1 },
2110 { 0x02, 0x0008 },
2111 { 0x01, 0x0120 },
2112 { 0x00, 0x1000 },
2113 { 0x04, 0x0800 },
2114 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
françois romieu0b9b5712009-08-10 19:44:56 +00002116 { 0x03, 0xff41 },
2117 { 0x02, 0xdf60 },
2118 { 0x01, 0x0140 },
2119 { 0x00, 0x0077 },
2120 { 0x04, 0x7800 },
2121 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
françois romieu0b9b5712009-08-10 19:44:56 +00002123 { 0x03, 0x802f },
2124 { 0x02, 0x4f02 },
2125 { 0x01, 0x0409 },
2126 { 0x00, 0xf0f9 },
2127 { 0x04, 0x9800 },
2128 { 0x04, 0x9000 },
2129
2130 { 0x03, 0xdf01 },
2131 { 0x02, 0xdf20 },
2132 { 0x01, 0xff95 },
2133 { 0x00, 0xba00 },
2134 { 0x04, 0xa800 },
2135 { 0x04, 0xa000 },
2136
2137 { 0x03, 0xff41 },
2138 { 0x02, 0xdf20 },
2139 { 0x01, 0x0140 },
2140 { 0x00, 0x00bb },
2141 { 0x04, 0xb800 },
2142 { 0x04, 0xb000 },
2143
2144 { 0x03, 0xdf41 },
2145 { 0x02, 0xdc60 },
2146 { 0x01, 0x6340 },
2147 { 0x00, 0x007d },
2148 { 0x04, 0xd800 },
2149 { 0x04, 0xd000 },
2150
2151 { 0x03, 0xdf01 },
2152 { 0x02, 0xdf20 },
2153 { 0x01, 0x100a },
2154 { 0x00, 0xa0ff },
2155 { 0x04, 0xf800 },
2156 { 0x04, 0xf000 },
2157
2158 { 0x1f, 0x0000 },
2159 { 0x0b, 0x0000 },
2160 { 0x00, 0x9200 }
2161 };
2162
françois romieu4da19632011-01-03 15:07:55 +00002163 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
françois romieu4da19632011-01-03 15:07:55 +00002166static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002167{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002168 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002169 { 0x1f, 0x0002 },
2170 { 0x01, 0x90d0 },
2171 { 0x1f, 0x0000 }
2172 };
2173
françois romieu4da19632011-01-03 15:07:55 +00002174 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002175}
2176
françois romieu4da19632011-01-03 15:07:55 +00002177static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002178{
2179 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002180
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002181 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2182 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002183 return;
2184
françois romieu4da19632011-01-03 15:07:55 +00002185 rtl_writephy(tp, 0x1f, 0x0001);
2186 rtl_writephy(tp, 0x10, 0xf01b);
2187 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002188}
2189
françois romieu4da19632011-01-03 15:07:55 +00002190static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002191{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002192 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002193 { 0x1f, 0x0001 },
2194 { 0x04, 0x0000 },
2195 { 0x03, 0x00a1 },
2196 { 0x02, 0x0008 },
2197 { 0x01, 0x0120 },
2198 { 0x00, 0x1000 },
2199 { 0x04, 0x0800 },
2200 { 0x04, 0x9000 },
2201 { 0x03, 0x802f },
2202 { 0x02, 0x4f02 },
2203 { 0x01, 0x0409 },
2204 { 0x00, 0xf099 },
2205 { 0x04, 0x9800 },
2206 { 0x04, 0xa000 },
2207 { 0x03, 0xdf01 },
2208 { 0x02, 0xdf20 },
2209 { 0x01, 0xff95 },
2210 { 0x00, 0xba00 },
2211 { 0x04, 0xa800 },
2212 { 0x04, 0xf000 },
2213 { 0x03, 0xdf01 },
2214 { 0x02, 0xdf20 },
2215 { 0x01, 0x101a },
2216 { 0x00, 0xa0ff },
2217 { 0x04, 0xf800 },
2218 { 0x04, 0x0000 },
2219 { 0x1f, 0x0000 },
2220
2221 { 0x1f, 0x0001 },
2222 { 0x10, 0xf41b },
2223 { 0x14, 0xfb54 },
2224 { 0x18, 0xf5c7 },
2225 { 0x1f, 0x0000 },
2226
2227 { 0x1f, 0x0001 },
2228 { 0x17, 0x0cc0 },
2229 { 0x1f, 0x0000 }
2230 };
2231
françois romieu4da19632011-01-03 15:07:55 +00002232 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002233
françois romieu4da19632011-01-03 15:07:55 +00002234 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002235}
2236
françois romieu4da19632011-01-03 15:07:55 +00002237static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002238{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002239 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002240 { 0x1f, 0x0001 },
2241 { 0x04, 0x0000 },
2242 { 0x03, 0x00a1 },
2243 { 0x02, 0x0008 },
2244 { 0x01, 0x0120 },
2245 { 0x00, 0x1000 },
2246 { 0x04, 0x0800 },
2247 { 0x04, 0x9000 },
2248 { 0x03, 0x802f },
2249 { 0x02, 0x4f02 },
2250 { 0x01, 0x0409 },
2251 { 0x00, 0xf099 },
2252 { 0x04, 0x9800 },
2253 { 0x04, 0xa000 },
2254 { 0x03, 0xdf01 },
2255 { 0x02, 0xdf20 },
2256 { 0x01, 0xff95 },
2257 { 0x00, 0xba00 },
2258 { 0x04, 0xa800 },
2259 { 0x04, 0xf000 },
2260 { 0x03, 0xdf01 },
2261 { 0x02, 0xdf20 },
2262 { 0x01, 0x101a },
2263 { 0x00, 0xa0ff },
2264 { 0x04, 0xf800 },
2265 { 0x04, 0x0000 },
2266 { 0x1f, 0x0000 },
2267
2268 { 0x1f, 0x0001 },
2269 { 0x0b, 0x8480 },
2270 { 0x1f, 0x0000 },
2271
2272 { 0x1f, 0x0001 },
2273 { 0x18, 0x67c7 },
2274 { 0x04, 0x2000 },
2275 { 0x03, 0x002f },
2276 { 0x02, 0x4360 },
2277 { 0x01, 0x0109 },
2278 { 0x00, 0x3022 },
2279 { 0x04, 0x2800 },
2280 { 0x1f, 0x0000 },
2281
2282 { 0x1f, 0x0001 },
2283 { 0x17, 0x0cc0 },
2284 { 0x1f, 0x0000 }
2285 };
2286
françois romieu4da19632011-01-03 15:07:55 +00002287 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002288}
2289
françois romieu4da19632011-01-03 15:07:55 +00002290static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002291{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002292 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002293 { 0x10, 0xf41b },
2294 { 0x1f, 0x0000 }
2295 };
2296
françois romieu4da19632011-01-03 15:07:55 +00002297 rtl_writephy(tp, 0x1f, 0x0001);
2298 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002299
françois romieu4da19632011-01-03 15:07:55 +00002300 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002301}
2302
françois romieu4da19632011-01-03 15:07:55 +00002303static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002304{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002305 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002306 { 0x1f, 0x0001 },
2307 { 0x10, 0xf41b },
2308 { 0x1f, 0x0000 }
2309 };
2310
françois romieu4da19632011-01-03 15:07:55 +00002311 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002312}
2313
françois romieu4da19632011-01-03 15:07:55 +00002314static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002315{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002316 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002317 { 0x1f, 0x0000 },
2318 { 0x1d, 0x0f00 },
2319 { 0x1f, 0x0002 },
2320 { 0x0c, 0x1ec8 },
2321 { 0x1f, 0x0000 }
2322 };
2323
françois romieu4da19632011-01-03 15:07:55 +00002324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002325}
2326
françois romieu4da19632011-01-03 15:07:55 +00002327static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002328{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002329 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002330 { 0x1f, 0x0001 },
2331 { 0x1d, 0x3d98 },
2332 { 0x1f, 0x0000 }
2333 };
2334
françois romieu4da19632011-01-03 15:07:55 +00002335 rtl_writephy(tp, 0x1f, 0x0000);
2336 rtl_patchphy(tp, 0x14, 1 << 5);
2337 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002338
françois romieu4da19632011-01-03 15:07:55 +00002339 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002340}
2341
françois romieu4da19632011-01-03 15:07:55 +00002342static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002343{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002344 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002345 { 0x1f, 0x0001 },
2346 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002347 { 0x1f, 0x0002 },
2348 { 0x00, 0x88d4 },
2349 { 0x01, 0x82b1 },
2350 { 0x03, 0x7002 },
2351 { 0x08, 0x9e30 },
2352 { 0x09, 0x01f0 },
2353 { 0x0a, 0x5500 },
2354 { 0x0c, 0x00c8 },
2355 { 0x1f, 0x0003 },
2356 { 0x12, 0xc096 },
2357 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002358 { 0x1f, 0x0000 },
2359 { 0x1f, 0x0000 },
2360 { 0x09, 0x2000 },
2361 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002362 };
2363
françois romieu4da19632011-01-03 15:07:55 +00002364 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002365
françois romieu4da19632011-01-03 15:07:55 +00002366 rtl_patchphy(tp, 0x14, 1 << 5);
2367 rtl_patchphy(tp, 0x0d, 1 << 5);
2368 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002369}
2370
françois romieu4da19632011-01-03 15:07:55 +00002371static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002372{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002373 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002374 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002375 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002376 { 0x03, 0x802f },
2377 { 0x02, 0x4f02 },
2378 { 0x01, 0x0409 },
2379 { 0x00, 0xf099 },
2380 { 0x04, 0x9800 },
2381 { 0x04, 0x9000 },
2382 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002383 { 0x1f, 0x0002 },
2384 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002385 { 0x06, 0x0761 },
2386 { 0x1f, 0x0003 },
2387 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002388 { 0x1f, 0x0000 }
2389 };
2390
françois romieu4da19632011-01-03 15:07:55 +00002391 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002392
françois romieu4da19632011-01-03 15:07:55 +00002393 rtl_patchphy(tp, 0x16, 1 << 0);
2394 rtl_patchphy(tp, 0x14, 1 << 5);
2395 rtl_patchphy(tp, 0x0d, 1 << 5);
2396 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002397}
2398
françois romieu4da19632011-01-03 15:07:55 +00002399static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002400{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002401 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002402 { 0x1f, 0x0001 },
2403 { 0x12, 0x2300 },
2404 { 0x1d, 0x3d98 },
2405 { 0x1f, 0x0002 },
2406 { 0x0c, 0x7eb8 },
2407 { 0x06, 0x5461 },
2408 { 0x1f, 0x0003 },
2409 { 0x16, 0x0f0a },
2410 { 0x1f, 0x0000 }
2411 };
2412
françois romieu4da19632011-01-03 15:07:55 +00002413 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002414
françois romieu4da19632011-01-03 15:07:55 +00002415 rtl_patchphy(tp, 0x16, 1 << 0);
2416 rtl_patchphy(tp, 0x14, 1 << 5);
2417 rtl_patchphy(tp, 0x0d, 1 << 5);
2418 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002419}
2420
françois romieu4da19632011-01-03 15:07:55 +00002421static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002422{
françois romieu4da19632011-01-03 15:07:55 +00002423 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002424}
2425
françois romieubca03d52011-01-03 15:07:31 +00002426static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002427{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002428 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002429 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002430 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002431 { 0x06, 0x4064 },
2432 { 0x07, 0x2863 },
2433 { 0x08, 0x059c },
2434 { 0x09, 0x26b4 },
2435 { 0x0a, 0x6a19 },
2436 { 0x0b, 0xdcc8 },
2437 { 0x10, 0xf06d },
2438 { 0x14, 0x7f68 },
2439 { 0x18, 0x7fd9 },
2440 { 0x1c, 0xf0ff },
2441 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002442 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002443 { 0x12, 0xf49f },
2444 { 0x13, 0x070b },
2445 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002446 { 0x14, 0x94c0 },
2447
2448 /*
2449 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002450 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002451 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002452 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002453 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002454 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002455 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002456 { 0x06, 0x5561 },
2457
2458 /*
2459 * Can not link to 1Gbps with bad cable
2460 * Decrease SNR threshold form 21.07dB to 19.04dB
2461 */
2462 { 0x1f, 0x0001 },
2463 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002464
2465 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002466 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002467 };
françois romieubca03d52011-01-03 15:07:31 +00002468 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002469
françois romieu4da19632011-01-03 15:07:55 +00002470 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002471
françois romieubca03d52011-01-03 15:07:31 +00002472 /*
2473 * Rx Error Issue
2474 * Fine Tune Switching regulator parameter
2475 */
françois romieu4da19632011-01-03 15:07:55 +00002476 rtl_writephy(tp, 0x1f, 0x0002);
2477 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2478 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002479
françois romieudaf9df62009-10-07 12:44:20 +00002480 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002481 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002482 { 0x1f, 0x0002 },
2483 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002484 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002485 { 0x05, 0x8330 },
2486 { 0x06, 0x669a },
2487 { 0x1f, 0x0002 }
2488 };
2489 int val;
2490
françois romieu4da19632011-01-03 15:07:55 +00002491 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002492
françois romieu4da19632011-01-03 15:07:55 +00002493 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002494
2495 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002496 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002497 0x0065, 0x0066, 0x0067, 0x0068,
2498 0x0069, 0x006a, 0x006b, 0x006c
2499 };
2500 int i;
2501
françois romieu4da19632011-01-03 15:07:55 +00002502 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002503
2504 val &= 0xff00;
2505 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002506 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002507 }
2508 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002509 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002510 { 0x1f, 0x0002 },
2511 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002512 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002513 { 0x05, 0x8330 },
2514 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002515 };
2516
françois romieu4da19632011-01-03 15:07:55 +00002517 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002518 }
2519
françois romieubca03d52011-01-03 15:07:31 +00002520 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002521 rtl_writephy(tp, 0x1f, 0x0002);
2522 rtl_patchphy(tp, 0x0d, 0x0300);
2523 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002524
françois romieubca03d52011-01-03 15:07:31 +00002525 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002526 rtl_writephy(tp, 0x1f, 0x0002);
2527 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2528 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002529
françois romieu4da19632011-01-03 15:07:55 +00002530 rtl_writephy(tp, 0x1f, 0x0005);
2531 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002532
2533 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002534
françois romieu4da19632011-01-03 15:07:55 +00002535 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002536}
2537
françois romieubca03d52011-01-03 15:07:31 +00002538static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002539{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002540 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002541 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002542 { 0x1f, 0x0001 },
2543 { 0x06, 0x4064 },
2544 { 0x07, 0x2863 },
2545 { 0x08, 0x059c },
2546 { 0x09, 0x26b4 },
2547 { 0x0a, 0x6a19 },
2548 { 0x0b, 0xdcc8 },
2549 { 0x10, 0xf06d },
2550 { 0x14, 0x7f68 },
2551 { 0x18, 0x7fd9 },
2552 { 0x1c, 0xf0ff },
2553 { 0x1d, 0x3d9c },
2554 { 0x1f, 0x0003 },
2555 { 0x12, 0xf49f },
2556 { 0x13, 0x070b },
2557 { 0x1a, 0x05ad },
2558 { 0x14, 0x94c0 },
2559
françois romieubca03d52011-01-03 15:07:31 +00002560 /*
2561 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002562 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002563 */
françois romieudaf9df62009-10-07 12:44:20 +00002564 { 0x1f, 0x0002 },
2565 { 0x06, 0x5561 },
2566 { 0x1f, 0x0005 },
2567 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002568 { 0x06, 0x5561 },
2569
2570 /*
2571 * Can not link to 1Gbps with bad cable
2572 * Decrease SNR threshold form 21.07dB to 19.04dB
2573 */
2574 { 0x1f, 0x0001 },
2575 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002576
2577 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002578 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002579 };
françois romieubca03d52011-01-03 15:07:31 +00002580 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002581
françois romieu4da19632011-01-03 15:07:55 +00002582 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002583
2584 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002585 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002586 { 0x1f, 0x0002 },
2587 { 0x05, 0x669a },
2588 { 0x1f, 0x0005 },
2589 { 0x05, 0x8330 },
2590 { 0x06, 0x669a },
2591
2592 { 0x1f, 0x0002 }
2593 };
2594 int val;
2595
françois romieu4da19632011-01-03 15:07:55 +00002596 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002597
françois romieu4da19632011-01-03 15:07:55 +00002598 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002599 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002600 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002601 0x0065, 0x0066, 0x0067, 0x0068,
2602 0x0069, 0x006a, 0x006b, 0x006c
2603 };
2604 int i;
2605
françois romieu4da19632011-01-03 15:07:55 +00002606 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002607
2608 val &= 0xff00;
2609 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002610 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002611 }
2612 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002613 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002614 { 0x1f, 0x0002 },
2615 { 0x05, 0x2642 },
2616 { 0x1f, 0x0005 },
2617 { 0x05, 0x8330 },
2618 { 0x06, 0x2642 }
2619 };
2620
françois romieu4da19632011-01-03 15:07:55 +00002621 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002622 }
2623
françois romieubca03d52011-01-03 15:07:31 +00002624 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002625 rtl_writephy(tp, 0x1f, 0x0002);
2626 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2627 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002628
françois romieubca03d52011-01-03 15:07:31 +00002629 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002630 rtl_writephy(tp, 0x1f, 0x0002);
2631 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002632
françois romieu4da19632011-01-03 15:07:55 +00002633 rtl_writephy(tp, 0x1f, 0x0005);
2634 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002635
2636 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002637
françois romieu4da19632011-01-03 15:07:55 +00002638 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002639}
2640
françois romieu4da19632011-01-03 15:07:55 +00002641static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002642{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002643 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002644 { 0x1f, 0x0002 },
2645 { 0x10, 0x0008 },
2646 { 0x0d, 0x006c },
2647
2648 { 0x1f, 0x0000 },
2649 { 0x0d, 0xf880 },
2650
2651 { 0x1f, 0x0001 },
2652 { 0x17, 0x0cc0 },
2653
2654 { 0x1f, 0x0001 },
2655 { 0x0b, 0xa4d8 },
2656 { 0x09, 0x281c },
2657 { 0x07, 0x2883 },
2658 { 0x0a, 0x6b35 },
2659 { 0x1d, 0x3da4 },
2660 { 0x1c, 0xeffd },
2661 { 0x14, 0x7f52 },
2662 { 0x18, 0x7fc6 },
2663 { 0x08, 0x0601 },
2664 { 0x06, 0x4063 },
2665 { 0x10, 0xf074 },
2666 { 0x1f, 0x0003 },
2667 { 0x13, 0x0789 },
2668 { 0x12, 0xf4bd },
2669 { 0x1a, 0x04fd },
2670 { 0x14, 0x84b0 },
2671 { 0x1f, 0x0000 },
2672 { 0x00, 0x9200 },
2673
2674 { 0x1f, 0x0005 },
2675 { 0x01, 0x0340 },
2676 { 0x1f, 0x0001 },
2677 { 0x04, 0x4000 },
2678 { 0x03, 0x1d21 },
2679 { 0x02, 0x0c32 },
2680 { 0x01, 0x0200 },
2681 { 0x00, 0x5554 },
2682 { 0x04, 0x4800 },
2683 { 0x04, 0x4000 },
2684 { 0x04, 0xf000 },
2685 { 0x03, 0xdf01 },
2686 { 0x02, 0xdf20 },
2687 { 0x01, 0x101a },
2688 { 0x00, 0xa0ff },
2689 { 0x04, 0xf800 },
2690 { 0x04, 0xf000 },
2691 { 0x1f, 0x0000 },
2692
2693 { 0x1f, 0x0007 },
2694 { 0x1e, 0x0023 },
2695 { 0x16, 0x0000 },
2696 { 0x1f, 0x0000 }
2697 };
2698
françois romieu4da19632011-01-03 15:07:55 +00002699 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002700}
2701
françois romieue6de30d2011-01-03 15:08:37 +00002702static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2703{
2704 static const struct phy_reg phy_reg_init[] = {
2705 { 0x1f, 0x0001 },
2706 { 0x17, 0x0cc0 },
2707
2708 { 0x1f, 0x0007 },
2709 { 0x1e, 0x002d },
2710 { 0x18, 0x0040 },
2711 { 0x1f, 0x0000 }
2712 };
2713
2714 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2715 rtl_patchphy(tp, 0x0d, 1 << 5);
2716}
2717
Hayes Wang70090422011-07-06 15:58:06 +08002718static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002719{
2720 static const struct phy_reg phy_reg_init[] = {
2721 /* Enable Delay cap */
2722 { 0x1f, 0x0005 },
2723 { 0x05, 0x8b80 },
2724 { 0x06, 0xc896 },
2725 { 0x1f, 0x0000 },
2726
2727 /* Channel estimation fine tune */
2728 { 0x1f, 0x0001 },
2729 { 0x0b, 0x6c20 },
2730 { 0x07, 0x2872 },
2731 { 0x1c, 0xefff },
2732 { 0x1f, 0x0003 },
2733 { 0x14, 0x6420 },
2734 { 0x1f, 0x0000 },
2735
2736 /* Update PFM & 10M TX idle timer */
2737 { 0x1f, 0x0007 },
2738 { 0x1e, 0x002f },
2739 { 0x15, 0x1919 },
2740 { 0x1f, 0x0000 },
2741
2742 { 0x1f, 0x0007 },
2743 { 0x1e, 0x00ac },
2744 { 0x18, 0x0006 },
2745 { 0x1f, 0x0000 }
2746 };
2747
Francois Romieu15ecd032011-04-27 13:52:22 -07002748 rtl_apply_firmware(tp);
2749
hayeswang01dc7fe2011-03-21 01:50:28 +00002750 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2751
2752 /* DCO enable for 10M IDLE Power */
2753 rtl_writephy(tp, 0x1f, 0x0007);
2754 rtl_writephy(tp, 0x1e, 0x0023);
2755 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2756 rtl_writephy(tp, 0x1f, 0x0000);
2757
2758 /* For impedance matching */
2759 rtl_writephy(tp, 0x1f, 0x0002);
2760 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002761 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002762
2763 /* PHY auto speed down */
2764 rtl_writephy(tp, 0x1f, 0x0007);
2765 rtl_writephy(tp, 0x1e, 0x002d);
2766 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2767 rtl_writephy(tp, 0x1f, 0x0000);
2768 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2769
2770 rtl_writephy(tp, 0x1f, 0x0005);
2771 rtl_writephy(tp, 0x05, 0x8b86);
2772 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2773 rtl_writephy(tp, 0x1f, 0x0000);
2774
2775 rtl_writephy(tp, 0x1f, 0x0005);
2776 rtl_writephy(tp, 0x05, 0x8b85);
2777 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2778 rtl_writephy(tp, 0x1f, 0x0007);
2779 rtl_writephy(tp, 0x1e, 0x0020);
2780 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2781 rtl_writephy(tp, 0x1f, 0x0006);
2782 rtl_writephy(tp, 0x00, 0x5a00);
2783 rtl_writephy(tp, 0x1f, 0x0000);
2784 rtl_writephy(tp, 0x0d, 0x0007);
2785 rtl_writephy(tp, 0x0e, 0x003c);
2786 rtl_writephy(tp, 0x0d, 0x4007);
2787 rtl_writephy(tp, 0x0e, 0x0000);
2788 rtl_writephy(tp, 0x0d, 0x0000);
2789}
2790
Hayes Wang70090422011-07-06 15:58:06 +08002791static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2792{
2793 static const struct phy_reg phy_reg_init[] = {
2794 /* Enable Delay cap */
2795 { 0x1f, 0x0004 },
2796 { 0x1f, 0x0007 },
2797 { 0x1e, 0x00ac },
2798 { 0x18, 0x0006 },
2799 { 0x1f, 0x0002 },
2800 { 0x1f, 0x0000 },
2801 { 0x1f, 0x0000 },
2802
2803 /* Channel estimation fine tune */
2804 { 0x1f, 0x0003 },
2805 { 0x09, 0xa20f },
2806 { 0x1f, 0x0000 },
2807 { 0x1f, 0x0000 },
2808
2809 /* Green Setting */
2810 { 0x1f, 0x0005 },
2811 { 0x05, 0x8b5b },
2812 { 0x06, 0x9222 },
2813 { 0x05, 0x8b6d },
2814 { 0x06, 0x8000 },
2815 { 0x05, 0x8b76 },
2816 { 0x06, 0x8000 },
2817 { 0x1f, 0x0000 }
2818 };
2819
2820 rtl_apply_firmware(tp);
2821
2822 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2823
2824 /* For 4-corner performance improve */
2825 rtl_writephy(tp, 0x1f, 0x0005);
2826 rtl_writephy(tp, 0x05, 0x8b80);
2827 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2828 rtl_writephy(tp, 0x1f, 0x0000);
2829
2830 /* PHY auto speed down */
2831 rtl_writephy(tp, 0x1f, 0x0004);
2832 rtl_writephy(tp, 0x1f, 0x0007);
2833 rtl_writephy(tp, 0x1e, 0x002d);
2834 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2835 rtl_writephy(tp, 0x1f, 0x0002);
2836 rtl_writephy(tp, 0x1f, 0x0000);
2837 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2838
2839 /* improve 10M EEE waveform */
2840 rtl_writephy(tp, 0x1f, 0x0005);
2841 rtl_writephy(tp, 0x05, 0x8b86);
2842 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2843 rtl_writephy(tp, 0x1f, 0x0000);
2844
2845 /* Improve 2-pair detection performance */
2846 rtl_writephy(tp, 0x1f, 0x0005);
2847 rtl_writephy(tp, 0x05, 0x8b85);
2848 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2849 rtl_writephy(tp, 0x1f, 0x0000);
2850
2851 /* EEE setting */
2852 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2853 ERIAR_EXGMAC);
2854 rtl_writephy(tp, 0x1f, 0x0005);
2855 rtl_writephy(tp, 0x05, 0x8b85);
2856 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2857 rtl_writephy(tp, 0x1f, 0x0004);
2858 rtl_writephy(tp, 0x1f, 0x0007);
2859 rtl_writephy(tp, 0x1e, 0x0020);
2860 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
2861 rtl_writephy(tp, 0x1f, 0x0002);
2862 rtl_writephy(tp, 0x1f, 0x0000);
2863 rtl_writephy(tp, 0x0d, 0x0007);
2864 rtl_writephy(tp, 0x0e, 0x003c);
2865 rtl_writephy(tp, 0x0d, 0x4007);
2866 rtl_writephy(tp, 0x0e, 0x0000);
2867 rtl_writephy(tp, 0x0d, 0x0000);
2868
2869 /* Green feature */
2870 rtl_writephy(tp, 0x1f, 0x0003);
2871 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2872 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2873 rtl_writephy(tp, 0x1f, 0x0000);
2874}
2875
françois romieu4da19632011-01-03 15:07:55 +00002876static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002877{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002878 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002879 { 0x1f, 0x0003 },
2880 { 0x08, 0x441d },
2881 { 0x01, 0x9100 },
2882 { 0x1f, 0x0000 }
2883 };
2884
françois romieu4da19632011-01-03 15:07:55 +00002885 rtl_writephy(tp, 0x1f, 0x0000);
2886 rtl_patchphy(tp, 0x11, 1 << 12);
2887 rtl_patchphy(tp, 0x19, 1 << 13);
2888 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002889
françois romieu4da19632011-01-03 15:07:55 +00002890 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002891}
2892
Hayes Wang5a5e4442011-02-22 17:26:21 +08002893static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2894{
2895 static const struct phy_reg phy_reg_init[] = {
2896 { 0x1f, 0x0005 },
2897 { 0x1a, 0x0000 },
2898 { 0x1f, 0x0000 },
2899
2900 { 0x1f, 0x0004 },
2901 { 0x1c, 0x0000 },
2902 { 0x1f, 0x0000 },
2903
2904 { 0x1f, 0x0001 },
2905 { 0x15, 0x7701 },
2906 { 0x1f, 0x0000 }
2907 };
2908
2909 /* Disable ALDPS before ram code */
2910 rtl_writephy(tp, 0x1f, 0x0000);
2911 rtl_writephy(tp, 0x18, 0x0310);
2912 msleep(100);
2913
François Romieu953a12c2011-04-24 17:38:48 +02002914 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002915
2916 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2917}
2918
Francois Romieu5615d9f2007-08-17 17:50:46 +02002919static void rtl_hw_phy_config(struct net_device *dev)
2920{
2921 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002922
2923 rtl8169_print_mac_version(tp);
2924
2925 switch (tp->mac_version) {
2926 case RTL_GIGA_MAC_VER_01:
2927 break;
2928 case RTL_GIGA_MAC_VER_02:
2929 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002930 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002931 break;
2932 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002933 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002934 break;
françois romieu2e9558562009-08-10 19:44:19 +00002935 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002936 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002937 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002938 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002939 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002940 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002941 case RTL_GIGA_MAC_VER_07:
2942 case RTL_GIGA_MAC_VER_08:
2943 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002944 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002945 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002946 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002947 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002948 break;
2949 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002950 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002951 break;
2952 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002953 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002954 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002955 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002956 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002957 break;
2958 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002959 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002960 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002961 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002962 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002963 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002964 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002965 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002966 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002967 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002968 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002969 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002970 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002971 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002972 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002973 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002974 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002975 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002976 break;
2977 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002978 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002979 break;
2980 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002981 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002982 break;
françois romieue6de30d2011-01-03 15:08:37 +00002983 case RTL_GIGA_MAC_VER_28:
2984 rtl8168d_4_hw_phy_config(tp);
2985 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002986 case RTL_GIGA_MAC_VER_29:
2987 case RTL_GIGA_MAC_VER_30:
2988 rtl8105e_hw_phy_config(tp);
2989 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002990 case RTL_GIGA_MAC_VER_31:
2991 /* None. */
2992 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002993 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002994 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08002995 rtl8168e_1_hw_phy_config(tp);
2996 break;
2997 case RTL_GIGA_MAC_VER_34:
2998 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002999 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003000
Francois Romieu5615d9f2007-08-17 17:50:46 +02003001 default:
3002 break;
3003 }
3004}
3005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006static void rtl8169_phy_timer(unsigned long __opaque)
3007{
3008 struct net_device *dev = (struct net_device *)__opaque;
3009 struct rtl8169_private *tp = netdev_priv(dev);
3010 struct timer_list *timer = &tp->timer;
3011 void __iomem *ioaddr = tp->mmio_addr;
3012 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3013
Francois Romieubcf0bf92006-07-26 23:14:13 +02003014 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 spin_lock_irq(&tp->lock);
3017
françois romieu4da19632011-01-03 15:07:55 +00003018 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003019 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 * A busy loop could burn quite a few cycles on nowadays CPU.
3021 * Let's delay the execution of the timer for a few ticks.
3022 */
3023 timeout = HZ/10;
3024 goto out_mod_timer;
3025 }
3026
3027 if (tp->link_ok(ioaddr))
3028 goto out_unlock;
3029
Joe Perchesbf82c182010-02-09 11:49:50 +00003030 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
françois romieu4da19632011-01-03 15:07:55 +00003032 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034out_mod_timer:
3035 mod_timer(timer, jiffies + timeout);
3036out_unlock:
3037 spin_unlock_irq(&tp->lock);
3038}
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040#ifdef CONFIG_NET_POLL_CONTROLLER
3041/*
3042 * Polling 'interrupt' - used by things like netconsole to send skbs
3043 * without having to re-enable interrupts. It's not called while
3044 * the interrupt routine is executing.
3045 */
3046static void rtl8169_netpoll(struct net_device *dev)
3047{
3048 struct rtl8169_private *tp = netdev_priv(dev);
3049 struct pci_dev *pdev = tp->pci_dev;
3050
3051 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003052 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 enable_irq(pdev->irq);
3054}
3055#endif
3056
3057static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3058 void __iomem *ioaddr)
3059{
3060 iounmap(ioaddr);
3061 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003062 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 pci_disable_device(pdev);
3064 free_netdev(dev);
3065}
3066
Francois Romieubf793292006-11-01 00:53:05 +01003067static void rtl8169_phy_reset(struct net_device *dev,
3068 struct rtl8169_private *tp)
3069{
Francois Romieu07d3f512007-02-21 22:40:46 +01003070 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003071
françois romieu4da19632011-01-03 15:07:55 +00003072 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003073 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003074 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003075 return;
3076 msleep(1);
3077 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003078 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003079}
3080
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003081static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003083 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003084
Francois Romieu5615d9f2007-08-17 17:50:46 +02003085 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003086
Marcus Sundberg773328942008-07-10 21:28:08 +02003087 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3088 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3089 RTL_W8(0x82, 0x01);
3090 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003091
Francois Romieu6dccd162007-02-13 23:38:05 +01003092 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3093
3094 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3095 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003096
Francois Romieubcf0bf92006-07-26 23:14:13 +02003097 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003098 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3099 RTL_W8(0x82, 0x01);
3100 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003101 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003102 }
3103
Francois Romieubf793292006-11-01 00:53:05 +01003104 rtl8169_phy_reset(dev, tp);
3105
Oliver Neukum54405cd2011-01-06 21:55:13 +01003106 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003107 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3108 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3109 (tp->mii.supports_gmii ?
3110 ADVERTISED_1000baseT_Half |
3111 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003112
Joe Perchesbf82c182010-02-09 11:49:50 +00003113 if (RTL_R8(PHYstatus) & TBI_Enable)
3114 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003115}
3116
Francois Romieu773d2022007-01-31 23:47:43 +01003117static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3118{
3119 void __iomem *ioaddr = tp->mmio_addr;
3120 u32 high;
3121 u32 low;
3122
3123 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3124 high = addr[4] | (addr[5] << 8);
3125
3126 spin_lock_irq(&tp->lock);
3127
3128 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003129
Francois Romieu773d2022007-01-31 23:47:43 +01003130 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003131 RTL_R32(MAC4);
3132
Francois Romieu78f1cd02010-03-27 19:35:46 -07003133 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003134 RTL_R32(MAC0);
3135
françois romieuc28aa382011-08-02 03:53:43 +00003136 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3137 const struct exgmac_reg e[] = {
3138 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3139 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3140 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3141 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3142 low >> 16 },
3143 };
3144
3145 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3146 }
3147
Francois Romieu773d2022007-01-31 23:47:43 +01003148 RTL_W8(Cfg9346, Cfg9346_Lock);
3149
3150 spin_unlock_irq(&tp->lock);
3151}
3152
3153static int rtl_set_mac_address(struct net_device *dev, void *p)
3154{
3155 struct rtl8169_private *tp = netdev_priv(dev);
3156 struct sockaddr *addr = p;
3157
3158 if (!is_valid_ether_addr(addr->sa_data))
3159 return -EADDRNOTAVAIL;
3160
3161 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3162
3163 rtl_rar_set(tp, dev->dev_addr);
3164
3165 return 0;
3166}
3167
Francois Romieu5f787a12006-08-17 13:02:36 +02003168static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3169{
3170 struct rtl8169_private *tp = netdev_priv(dev);
3171 struct mii_ioctl_data *data = if_mii(ifr);
3172
Francois Romieu8b4ab282008-11-19 22:05:25 -08003173 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3174}
Francois Romieu5f787a12006-08-17 13:02:36 +02003175
Francois Romieucecb5fd2011-04-01 10:21:07 +02003176static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3177 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003178{
Francois Romieu5f787a12006-08-17 13:02:36 +02003179 switch (cmd) {
3180 case SIOCGMIIPHY:
3181 data->phy_id = 32; /* Internal PHY */
3182 return 0;
3183
3184 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003185 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003186 return 0;
3187
3188 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003189 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003190 return 0;
3191 }
3192 return -EOPNOTSUPP;
3193}
3194
Francois Romieu8b4ab282008-11-19 22:05:25 -08003195static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3196{
3197 return -EOPNOTSUPP;
3198}
3199
Francois Romieu0e485152007-02-20 00:00:26 +01003200static const struct rtl_cfg_info {
3201 void (*hw_start)(struct net_device *);
3202 unsigned int region;
3203 unsigned int align;
3204 u16 intr_event;
3205 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003206 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003207 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003208} rtl_cfg_infos [] = {
3209 [RTL_CFG_0] = {
3210 .hw_start = rtl_hw_start_8169,
3211 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003212 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003213 .intr_event = SYSErr | LinkChg | RxOverflow |
3214 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003215 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003216 .features = RTL_FEATURE_GMII,
3217 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003218 },
3219 [RTL_CFG_1] = {
3220 .hw_start = rtl_hw_start_8168,
3221 .region = 2,
3222 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003223 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003224 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003225 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003226 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3227 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003228 },
3229 [RTL_CFG_2] = {
3230 .hw_start = rtl_hw_start_8101,
3231 .region = 2,
3232 .align = 8,
3233 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3234 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003235 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003236 .features = RTL_FEATURE_MSI,
3237 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003238 }
3239};
3240
Francois Romieufbac58f2007-10-04 22:51:38 +02003241/* Cfg9346_Unlock assumed. */
3242static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3243 const struct rtl_cfg_info *cfg)
3244{
3245 unsigned msi = 0;
3246 u8 cfg2;
3247
3248 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003249 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003250 if (pci_enable_msi(pdev)) {
3251 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3252 } else {
3253 cfg2 |= MSIEnable;
3254 msi = RTL_FEATURE_MSI;
3255 }
3256 }
3257 RTL_W8(Config2, cfg2);
3258 return msi;
3259}
3260
3261static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3262{
3263 if (tp->features & RTL_FEATURE_MSI) {
3264 pci_disable_msi(pdev);
3265 tp->features &= ~RTL_FEATURE_MSI;
3266 }
3267}
3268
Francois Romieu8b4ab282008-11-19 22:05:25 -08003269static const struct net_device_ops rtl8169_netdev_ops = {
3270 .ndo_open = rtl8169_open,
3271 .ndo_stop = rtl8169_close,
3272 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003273 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003274 .ndo_tx_timeout = rtl8169_tx_timeout,
3275 .ndo_validate_addr = eth_validate_addr,
3276 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003277 .ndo_fix_features = rtl8169_fix_features,
3278 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003279 .ndo_set_mac_address = rtl_set_mac_address,
3280 .ndo_do_ioctl = rtl8169_ioctl,
3281 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003282#ifdef CONFIG_NET_POLL_CONTROLLER
3283 .ndo_poll_controller = rtl8169_netpoll,
3284#endif
3285
3286};
3287
françois romieuc0e45c12011-01-03 15:08:04 +00003288static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3289{
3290 struct mdio_ops *ops = &tp->mdio_ops;
3291
3292 switch (tp->mac_version) {
3293 case RTL_GIGA_MAC_VER_27:
3294 ops->write = r8168dp_1_mdio_write;
3295 ops->read = r8168dp_1_mdio_read;
3296 break;
françois romieue6de30d2011-01-03 15:08:37 +00003297 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003298 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003299 ops->write = r8168dp_2_mdio_write;
3300 ops->read = r8168dp_2_mdio_read;
3301 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003302 default:
3303 ops->write = r8169_mdio_write;
3304 ops->read = r8169_mdio_read;
3305 break;
3306 }
3307}
3308
françois romieu065c27c2011-01-03 15:08:12 +00003309static void r810x_phy_power_down(struct rtl8169_private *tp)
3310{
3311 rtl_writephy(tp, 0x1f, 0x0000);
3312 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3313}
3314
3315static void r810x_phy_power_up(struct rtl8169_private *tp)
3316{
3317 rtl_writephy(tp, 0x1f, 0x0000);
3318 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3319}
3320
3321static void r810x_pll_power_down(struct rtl8169_private *tp)
3322{
3323 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3324 rtl_writephy(tp, 0x1f, 0x0000);
3325 rtl_writephy(tp, MII_BMCR, 0x0000);
3326 return;
3327 }
3328
3329 r810x_phy_power_down(tp);
3330}
3331
3332static void r810x_pll_power_up(struct rtl8169_private *tp)
3333{
3334 r810x_phy_power_up(tp);
3335}
3336
3337static void r8168_phy_power_up(struct rtl8169_private *tp)
3338{
3339 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003340 switch (tp->mac_version) {
3341 case RTL_GIGA_MAC_VER_11:
3342 case RTL_GIGA_MAC_VER_12:
3343 case RTL_GIGA_MAC_VER_17:
3344 case RTL_GIGA_MAC_VER_18:
3345 case RTL_GIGA_MAC_VER_19:
3346 case RTL_GIGA_MAC_VER_20:
3347 case RTL_GIGA_MAC_VER_21:
3348 case RTL_GIGA_MAC_VER_22:
3349 case RTL_GIGA_MAC_VER_23:
3350 case RTL_GIGA_MAC_VER_24:
3351 case RTL_GIGA_MAC_VER_25:
3352 case RTL_GIGA_MAC_VER_26:
3353 case RTL_GIGA_MAC_VER_27:
3354 case RTL_GIGA_MAC_VER_28:
3355 case RTL_GIGA_MAC_VER_31:
3356 rtl_writephy(tp, 0x0e, 0x0000);
3357 break;
3358 default:
3359 break;
3360 }
françois romieu065c27c2011-01-03 15:08:12 +00003361 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3362}
3363
3364static void r8168_phy_power_down(struct rtl8169_private *tp)
3365{
3366 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003367 switch (tp->mac_version) {
3368 case RTL_GIGA_MAC_VER_32:
3369 case RTL_GIGA_MAC_VER_33:
3370 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3371 break;
3372
3373 case RTL_GIGA_MAC_VER_11:
3374 case RTL_GIGA_MAC_VER_12:
3375 case RTL_GIGA_MAC_VER_17:
3376 case RTL_GIGA_MAC_VER_18:
3377 case RTL_GIGA_MAC_VER_19:
3378 case RTL_GIGA_MAC_VER_20:
3379 case RTL_GIGA_MAC_VER_21:
3380 case RTL_GIGA_MAC_VER_22:
3381 case RTL_GIGA_MAC_VER_23:
3382 case RTL_GIGA_MAC_VER_24:
3383 case RTL_GIGA_MAC_VER_25:
3384 case RTL_GIGA_MAC_VER_26:
3385 case RTL_GIGA_MAC_VER_27:
3386 case RTL_GIGA_MAC_VER_28:
3387 case RTL_GIGA_MAC_VER_31:
3388 rtl_writephy(tp, 0x0e, 0x0200);
3389 default:
3390 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3391 break;
3392 }
françois romieu065c27c2011-01-03 15:08:12 +00003393}
3394
3395static void r8168_pll_power_down(struct rtl8169_private *tp)
3396{
3397 void __iomem *ioaddr = tp->mmio_addr;
3398
Francois Romieucecb5fd2011-04-01 10:21:07 +02003399 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3400 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3401 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003402 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003403 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003404 }
françois romieu065c27c2011-01-03 15:08:12 +00003405
Francois Romieucecb5fd2011-04-01 10:21:07 +02003406 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3407 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003408 (RTL_R16(CPlusCmd) & ASF)) {
3409 return;
3410 }
3411
hayeswang01dc7fe2011-03-21 01:50:28 +00003412 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3413 tp->mac_version == RTL_GIGA_MAC_VER_33)
3414 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3415
françois romieu065c27c2011-01-03 15:08:12 +00003416 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3417 rtl_writephy(tp, 0x1f, 0x0000);
3418 rtl_writephy(tp, MII_BMCR, 0x0000);
3419
Hayes Wangd4ed95d2011-07-06 15:58:07 +08003420 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3421 tp->mac_version == RTL_GIGA_MAC_VER_33)
3422 RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
3423 AcceptMulticast | AcceptMyPhys);
françois romieu065c27c2011-01-03 15:08:12 +00003424 return;
3425 }
3426
3427 r8168_phy_power_down(tp);
3428
3429 switch (tp->mac_version) {
3430 case RTL_GIGA_MAC_VER_25:
3431 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003432 case RTL_GIGA_MAC_VER_27:
3433 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003434 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003435 case RTL_GIGA_MAC_VER_32:
3436 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003437 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3438 break;
3439 }
3440}
3441
3442static void r8168_pll_power_up(struct rtl8169_private *tp)
3443{
3444 void __iomem *ioaddr = tp->mmio_addr;
3445
Francois Romieucecb5fd2011-04-01 10:21:07 +02003446 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3447 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3448 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003449 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003450 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003451 }
françois romieu065c27c2011-01-03 15:08:12 +00003452
3453 switch (tp->mac_version) {
3454 case RTL_GIGA_MAC_VER_25:
3455 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003456 case RTL_GIGA_MAC_VER_27:
3457 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003458 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003459 case RTL_GIGA_MAC_VER_32:
3460 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003461 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3462 break;
3463 }
3464
3465 r8168_phy_power_up(tp);
3466}
3467
3468static void rtl_pll_power_op(struct rtl8169_private *tp,
3469 void (*op)(struct rtl8169_private *))
3470{
3471 if (op)
3472 op(tp);
3473}
3474
3475static void rtl_pll_power_down(struct rtl8169_private *tp)
3476{
3477 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3478}
3479
3480static void rtl_pll_power_up(struct rtl8169_private *tp)
3481{
3482 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3483}
3484
3485static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3486{
3487 struct pll_power_ops *ops = &tp->pll_power_ops;
3488
3489 switch (tp->mac_version) {
3490 case RTL_GIGA_MAC_VER_07:
3491 case RTL_GIGA_MAC_VER_08:
3492 case RTL_GIGA_MAC_VER_09:
3493 case RTL_GIGA_MAC_VER_10:
3494 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003495 case RTL_GIGA_MAC_VER_29:
3496 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003497 ops->down = r810x_pll_power_down;
3498 ops->up = r810x_pll_power_up;
3499 break;
3500
3501 case RTL_GIGA_MAC_VER_11:
3502 case RTL_GIGA_MAC_VER_12:
3503 case RTL_GIGA_MAC_VER_17:
3504 case RTL_GIGA_MAC_VER_18:
3505 case RTL_GIGA_MAC_VER_19:
3506 case RTL_GIGA_MAC_VER_20:
3507 case RTL_GIGA_MAC_VER_21:
3508 case RTL_GIGA_MAC_VER_22:
3509 case RTL_GIGA_MAC_VER_23:
3510 case RTL_GIGA_MAC_VER_24:
3511 case RTL_GIGA_MAC_VER_25:
3512 case RTL_GIGA_MAC_VER_26:
3513 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003514 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003515 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003516 case RTL_GIGA_MAC_VER_32:
3517 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003518 case RTL_GIGA_MAC_VER_34:
françois romieu065c27c2011-01-03 15:08:12 +00003519 ops->down = r8168_pll_power_down;
3520 ops->up = r8168_pll_power_up;
3521 break;
3522
3523 default:
3524 ops->down = NULL;
3525 ops->up = NULL;
3526 break;
3527 }
3528}
3529
Hayes Wange542a222011-07-06 15:58:04 +08003530static void rtl_init_rxcfg(struct rtl8169_private *tp)
3531{
3532 void __iomem *ioaddr = tp->mmio_addr;
3533
3534 switch (tp->mac_version) {
3535 case RTL_GIGA_MAC_VER_01:
3536 case RTL_GIGA_MAC_VER_02:
3537 case RTL_GIGA_MAC_VER_03:
3538 case RTL_GIGA_MAC_VER_04:
3539 case RTL_GIGA_MAC_VER_05:
3540 case RTL_GIGA_MAC_VER_06:
3541 case RTL_GIGA_MAC_VER_10:
3542 case RTL_GIGA_MAC_VER_11:
3543 case RTL_GIGA_MAC_VER_12:
3544 case RTL_GIGA_MAC_VER_13:
3545 case RTL_GIGA_MAC_VER_14:
3546 case RTL_GIGA_MAC_VER_15:
3547 case RTL_GIGA_MAC_VER_16:
3548 case RTL_GIGA_MAC_VER_17:
3549 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3550 break;
3551 case RTL_GIGA_MAC_VER_18:
3552 case RTL_GIGA_MAC_VER_19:
3553 case RTL_GIGA_MAC_VER_20:
3554 case RTL_GIGA_MAC_VER_21:
3555 case RTL_GIGA_MAC_VER_22:
3556 case RTL_GIGA_MAC_VER_23:
3557 case RTL_GIGA_MAC_VER_24:
3558 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3559 break;
3560 default:
3561 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3562 break;
3563 }
3564}
3565
Hayes Wang92fc43b2011-07-06 15:58:03 +08003566static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3567{
3568 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3569}
3570
Francois Romieu6f43adc2011-04-29 15:05:51 +02003571static void rtl_hw_reset(struct rtl8169_private *tp)
3572{
3573 void __iomem *ioaddr = tp->mmio_addr;
3574 int i;
3575
3576 /* Soft reset the chip. */
3577 RTL_W8(ChipCmd, CmdReset);
3578
3579 /* Check that the chip has finished the reset. */
3580 for (i = 0; i < 100; i++) {
3581 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3582 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003583 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003584 }
Hayes Wang92fc43b2011-07-06 15:58:03 +08003585
3586 rtl8169_init_ring_indexes(tp);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003587}
3588
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003589static int __devinit
3590rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3591{
Francois Romieu0e485152007-02-20 00:00:26 +01003592 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3593 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003595 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003596 struct net_device *dev;
3597 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003598 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003599 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003601 if (netif_msg_drv(&debug)) {
3602 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3603 MODULENAME, RTL8169_VERSION);
3604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003607 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003608 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003609 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003610 rc = -ENOMEM;
3611 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 }
3613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003615 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003617 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003618 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003619 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Francois Romieuccdffb92008-07-26 14:26:06 +02003621 mii = &tp->mii;
3622 mii->dev = dev;
3623 mii->mdio_read = rtl_mdio_read;
3624 mii->mdio_write = rtl_mdio_write;
3625 mii->phy_id_mask = 0x1f;
3626 mii->reg_num_mask = 0x1f;
3627 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3628
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003629 /* disable ASPM completely as that cause random device stop working
3630 * problems as well as full system hangs for some PCIe devices users */
3631 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3632 PCIE_LINK_STATE_CLKPM);
3633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3635 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003636 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003637 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003638 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 }
3640
françois romieu87aeec72010-04-26 11:42:06 +00003641 if (pci_set_mwi(pdev) < 0)
3642 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003645 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003646 netif_err(tp, probe, dev,
3647 "region #%d not an MMIO resource, aborting\n",
3648 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003650 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003652
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003654 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003655 netif_err(tp, probe, dev,
3656 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003658 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 }
3660
3661 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003662 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003663 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003664 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003667 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
3669 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003670 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 tp->cp_cmd |= PCIDAC;
3672 dev->features |= NETIF_F_HIGHDMA;
3673 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003674 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003676 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003677 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
3679 }
3680
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003682 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003683 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003684 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003686 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003688 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
Jon Masone44daad2011-06-27 07:46:31 +00003690 if (!pci_is_pcie(pdev))
3691 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07003692
Hayes Wange542a222011-07-06 15:58:04 +08003693 /* Identify chip attached to board */
3694 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
3695
3696 rtl_init_rxcfg(tp);
3697
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003698 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Francois Romieu6f43adc2011-04-29 15:05:51 +02003700 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003702 RTL_W16(IntrStatus, 0xffff);
3703
françois romieuca52efd2009-07-24 12:34:19 +00003704 pci_set_master(pdev);
3705
Francois Romieu7a8fc772011-03-01 17:18:33 +01003706 /*
3707 * Pretend we are using VLANs; This bypasses a nasty bug where
3708 * Interrupts stop flowing on high load on 8110SCd controllers.
3709 */
3710 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3711 tp->cp_cmd |= RxVlan;
3712
françois romieuc0e45c12011-01-03 15:08:04 +00003713 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003714 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Francois Romieu85bffe62011-04-27 08:22:39 +02003718 chipset = tp->mac_version;
3719 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Francois Romieu5d06a992006-02-23 00:47:58 +01003721 RTL_W8(Cfg9346, Cfg9346_Unlock);
3722 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3723 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003724 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3725 tp->features |= RTL_FEATURE_WOL;
3726 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3727 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003728 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003729 RTL_W8(Cfg9346, Cfg9346_Lock);
3730
Francois Romieu66ec5d42007-11-06 22:56:10 +01003731 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3732 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 tp->set_speed = rtl8169_set_speed_tbi;
3734 tp->get_settings = rtl8169_gset_tbi;
3735 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3736 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3737 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003738 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 } else {
3740 tp->set_speed = rtl8169_set_speed_xmii;
3741 tp->get_settings = rtl8169_gset_xmii;
3742 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3743 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3744 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003745 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 }
3747
Francois Romieudf58ef52008-10-09 14:35:58 -07003748 spin_lock_init(&tp->lock);
3749
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003750 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 for (i = 0; i < MAC_ADDR_LEN; i++)
3752 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003753 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3757 dev->irq = pdev->irq;
3758 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003760 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Michał Mirosław350fb322011-04-08 06:35:56 +00003762 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3763 * properly for all devices */
3764 dev->features |= NETIF_F_RXCSUM |
3765 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3766
3767 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3768 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3769 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3770 NETIF_F_HIGHDMA;
3771
3772 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3773 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3774 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
3776 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003777 tp->hw_start = cfg->hw_start;
3778 tp->intr_event = cfg->intr_event;
3779 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Francois Romieu2efa53f2007-03-09 00:00:05 +01003781 init_timer(&tp->timer);
3782 tp->timer.data = (unsigned long) dev;
3783 tp->timer.function = rtl8169_phy_timer;
3784
Francois Romieub6ffd972011-06-17 17:00:05 +02003785 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003788 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003789 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
3791 pci_set_drvdata(pdev, dev);
3792
Joe Perchesbf82c182010-02-09 11:49:50 +00003793 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003794 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003795 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
Francois Romieucecb5fd2011-04-01 10:21:07 +02003797 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3798 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3799 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003800 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003801 }
françois romieub646d902011-01-03 15:08:21 +00003802
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003803 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Alan Sternf3ec4f82010-06-08 15:23:51 -04003805 if (pci_dev_run_wake(pdev))
3806 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003807
Ivan Vecera0d672e92011-02-15 02:08:39 +00003808 netif_carrier_off(dev);
3809
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003810out:
3811 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
françois romieu87aeec72010-04-26 11:42:06 +00003813err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003814 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003815 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003816err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003817 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003818err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003819 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003820 pci_disable_device(pdev);
3821err_out_free_dev_1:
3822 free_netdev(dev);
3823 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
3825
Francois Romieu07d3f512007-02-21 22:40:46 +01003826static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
3828 struct net_device *dev = pci_get_drvdata(pdev);
3829 struct rtl8169_private *tp = netdev_priv(dev);
3830
Francois Romieucecb5fd2011-04-01 10:21:07 +02003831 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3832 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3833 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003834 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003835 }
françois romieub646d902011-01-03 15:08:21 +00003836
Tejun Heo23f333a2010-12-12 16:45:14 +01003837 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003840
François Romieu953a12c2011-04-24 17:38:48 +02003841 rtl_release_firmware(tp);
3842
Alan Sternf3ec4f82010-06-08 15:23:51 -04003843 if (pci_dev_run_wake(pdev))
3844 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003845
Ivan Veceracc098dc2009-11-29 23:12:52 -08003846 /* restore original MAC address */
3847 rtl_rar_set(tp, dev->perm_addr);
3848
Francois Romieufbac58f2007-10-04 22:51:38 +02003849 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3851 pci_set_drvdata(pdev, NULL);
3852}
3853
Francois Romieub6ffd972011-06-17 17:00:05 +02003854static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3855{
3856 struct rtl_fw *rtl_fw;
3857 const char *name;
3858 int rc = -ENOMEM;
3859
3860 name = rtl_lookup_firmware_name(tp);
3861 if (!name)
3862 goto out_no_firmware;
3863
3864 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3865 if (!rtl_fw)
3866 goto err_warn;
3867
3868 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3869 if (rc < 0)
3870 goto err_free;
3871
Francois Romieufd112f22011-06-18 00:10:29 +02003872 rc = rtl_check_firmware(tp, rtl_fw);
3873 if (rc < 0)
3874 goto err_release_firmware;
3875
Francois Romieub6ffd972011-06-17 17:00:05 +02003876 tp->rtl_fw = rtl_fw;
3877out:
3878 return;
3879
Francois Romieufd112f22011-06-18 00:10:29 +02003880err_release_firmware:
3881 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003882err_free:
3883 kfree(rtl_fw);
3884err_warn:
3885 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3886 name, rc);
3887out_no_firmware:
3888 tp->rtl_fw = NULL;
3889 goto out;
3890}
3891
François Romieu953a12c2011-04-24 17:38:48 +02003892static void rtl_request_firmware(struct rtl8169_private *tp)
3893{
Francois Romieub6ffd972011-06-17 17:00:05 +02003894 if (IS_ERR(tp->rtl_fw))
3895 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003896}
3897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898static int rtl8169_open(struct net_device *dev)
3899{
3900 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003901 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003903 int retval = -ENOMEM;
3904
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003905 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Neil Hormanc0cd8842010-03-29 13:16:02 -07003907 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003909 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003911 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3912 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003914 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003916 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3917 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003919 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
3921 retval = rtl8169_init_ring(dev);
3922 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003923 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
David Howellsc4028952006-11-22 14:57:56 +00003925 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Francois Romieu99f252b2007-04-02 22:59:59 +02003927 smp_mb();
3928
François Romieu953a12c2011-04-24 17:38:48 +02003929 rtl_request_firmware(tp);
3930
Francois Romieufbac58f2007-10-04 22:51:38 +02003931 retval = request_irq(dev->irq, rtl8169_interrupt,
3932 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003933 dev->name, dev);
3934 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003935 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003936
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003937 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003938
françois romieueee3a962011-01-08 02:17:26 +00003939 rtl8169_init_phy(dev, tp);
3940
Michał Mirosław350fb322011-04-08 06:35:56 +00003941 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003942
françois romieu065c27c2011-01-03 15:08:12 +00003943 rtl_pll_power_up(tp);
3944
Francois Romieu07ce4062007-02-23 23:36:39 +01003945 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003947 tp->saved_wolopts = 0;
3948 pm_runtime_put_noidle(&pdev->dev);
3949
françois romieueee3a962011-01-08 02:17:26 +00003950 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951out:
3952 return retval;
3953
François Romieu953a12c2011-04-24 17:38:48 +02003954err_release_fw_2:
3955 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003956 rtl8169_rx_clear(tp);
3957err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003958 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3959 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003960 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003961err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003962 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3963 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003964 tp->TxDescArray = NULL;
3965err_pm_runtime_put:
3966 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 goto out;
3968}
3969
Hayes Wang92fc43b2011-07-06 15:58:03 +08003970static void rtl_rx_close(struct rtl8169_private *tp)
3971{
3972 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003973
Francois Romieu1687b562011-07-19 17:21:29 +02003974 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003975}
3976
françois romieue6de30d2011-01-03 15:08:37 +00003977static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978{
françois romieue6de30d2011-01-03 15:08:37 +00003979 void __iomem *ioaddr = tp->mmio_addr;
3980
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 /* Disable interrupts */
3982 rtl8169_irq_mask_and_ack(ioaddr);
3983
Hayes Wang92fc43b2011-07-06 15:58:03 +08003984 rtl_rx_close(tp);
3985
Hayes Wang5d2e1952011-02-22 17:26:22 +08003986 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003987 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3988 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003989 while (RTL_R8(TxPoll) & NPQ)
3990 udelay(20);
Hayes Wang70090422011-07-06 15:58:06 +08003991 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
Hayes Wangc2b0c1e2011-09-06 16:55:16 +08003992 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08003993 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
3994 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003995 } else {
3996 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
3997 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00003998 }
3999
Hayes Wang92fc43b2011-07-06 15:58:03 +08004000 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
Francois Romieu7f796d82007-06-11 23:04:41 +02004003static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004004{
4005 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004006
4007 /* Set DMA burst size and Interframe Gap Time */
4008 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4009 (InterFrameGap << TxInterFrameGapShift));
4010}
4011
Francois Romieu07ce4062007-02-23 23:36:39 +01004012static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013{
4014 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Francois Romieu07ce4062007-02-23 23:36:39 +01004016 tp->hw_start(dev);
4017
Francois Romieu07ce4062007-02-23 23:36:39 +01004018 netif_start_queue(dev);
4019}
4020
Francois Romieu7f796d82007-06-11 23:04:41 +02004021static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4022 void __iomem *ioaddr)
4023{
4024 /*
4025 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4026 * register to be written before TxDescAddrLow to work.
4027 * Switching from MMIO to I/O access fixes the issue as well.
4028 */
4029 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004030 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02004031 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004032 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02004033}
4034
4035static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4036{
4037 u16 cmd;
4038
4039 cmd = RTL_R16(CPlusCmd);
4040 RTL_W16(CPlusCmd, cmd);
4041 return cmd;
4042}
4043
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004044static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02004045{
4046 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00004047 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02004048}
4049
Francois Romieu6dccd162007-02-13 23:38:05 +01004050static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4051{
Francois Romieu37441002011-06-17 22:58:54 +02004052 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004053 u32 mac_version;
4054 u32 clk;
4055 u32 val;
4056 } cfg2_info [] = {
4057 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4058 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4059 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4060 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004061 };
4062 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004063 unsigned int i;
4064 u32 clk;
4065
4066 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004067 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004068 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4069 RTL_W32(0x7c, p->val);
4070 break;
4071 }
4072 }
4073}
4074
Francois Romieu07ce4062007-02-23 23:36:39 +01004075static void rtl_hw_start_8169(struct net_device *dev)
4076{
4077 struct rtl8169_private *tp = netdev_priv(dev);
4078 void __iomem *ioaddr = tp->mmio_addr;
4079 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004080
Francois Romieu9cb427b2006-11-02 00:10:16 +01004081 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4082 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4083 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4084 }
4085
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004087 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4088 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4089 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4090 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004091 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4092
Hayes Wange542a222011-07-06 15:58:04 +08004093 rtl_init_rxcfg(tp);
4094
françois romieuf0298f82011-01-03 15:07:42 +00004095 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004097 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Francois Romieucecb5fd2011-04-01 10:21:07 +02004099 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4100 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4101 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4102 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004103 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Francois Romieu7f796d82007-06-11 23:04:41 +02004105 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004106
Francois Romieucecb5fd2011-04-01 10:21:07 +02004107 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4108 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004109 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004111 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 }
4113
Francois Romieubcf0bf92006-07-26 23:14:13 +02004114 RTL_W16(CPlusCmd, tp->cp_cmd);
4115
Francois Romieu6dccd162007-02-13 23:38:05 +01004116 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 /*
4119 * Undocumented corner. Supposedly:
4120 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4121 */
4122 RTL_W16(IntrMitigate, 0x0000);
4123
Francois Romieu7f796d82007-06-11 23:04:41 +02004124 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004125
Francois Romieucecb5fd2011-04-01 10:21:07 +02004126 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4127 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4128 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4129 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004130 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4131 rtl_set_rx_tx_config_registers(tp);
4132 }
4133
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004135
4136 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4137 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
4139 RTL_W32(RxMissed, 0);
4140
Francois Romieu07ce4062007-02-23 23:36:39 +01004141 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
4143 /* no early-rx interrupts */
4144 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004145
4146 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01004147 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Francois Romieu9c14cea2008-07-05 00:21:15 +02004150static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02004151{
Jon Masone44daad2011-06-27 07:46:31 +00004152 int cap = pci_pcie_cap(pdev);
Francois Romieu458a9f62008-08-02 15:50:02 +02004153
Francois Romieu9c14cea2008-07-05 00:21:15 +02004154 if (cap) {
4155 u16 ctl;
4156
4157 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
4158 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
4159 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
4160 }
Francois Romieu458a9f62008-08-02 15:50:02 +02004161}
4162
françois romieu650e8d52011-01-03 15:08:29 +00004163static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004164{
4165 u32 csi;
4166
4167 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004168 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4169}
4170
françois romieue6de30d2011-01-03 15:08:37 +00004171static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4172{
4173 rtl_csi_access_enable(ioaddr, 0x17000000);
4174}
4175
françois romieu650e8d52011-01-03 15:08:29 +00004176static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4177{
4178 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004179}
4180
4181struct ephy_info {
4182 unsigned int offset;
4183 u16 mask;
4184 u16 bits;
4185};
4186
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004187static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004188{
4189 u16 w;
4190
4191 while (len-- > 0) {
4192 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4193 rtl_ephy_write(ioaddr, e->offset, w);
4194 e++;
4195 }
4196}
4197
Francois Romieub726e492008-06-28 12:22:59 +02004198static void rtl_disable_clock_request(struct pci_dev *pdev)
4199{
Jon Masone44daad2011-06-27 07:46:31 +00004200 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004201
4202 if (cap) {
4203 u16 ctl;
4204
4205 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4206 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4207 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4208 }
4209}
4210
françois romieue6de30d2011-01-03 15:08:37 +00004211static void rtl_enable_clock_request(struct pci_dev *pdev)
4212{
Jon Masone44daad2011-06-27 07:46:31 +00004213 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004214
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
Francois Romieub726e492008-06-28 12:22:59 +02004224#define R8168_CPCMD_QUIRK_MASK (\
4225 EnableBist | \
4226 Mac_dbgo_oe | \
4227 Force_half_dup | \
4228 Force_rxflow_en | \
4229 Force_txflow_en | \
4230 Cxpl_dbg_sel | \
4231 ASF | \
4232 PktCntrDisable | \
4233 Mac_dbgo_sel)
4234
Francois Romieu219a1e92008-06-28 11:58:39 +02004235static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4236{
Francois Romieub726e492008-06-28 12:22:59 +02004237 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4238
4239 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4240
Francois Romieu2e68ae42008-06-28 12:00:55 +02004241 rtl_tx_performance_tweak(pdev,
4242 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004243}
4244
4245static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4246{
4247 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004248
françois romieuf0298f82011-01-03 15:07:42 +00004249 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004250
4251 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004252}
4253
4254static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4255{
Francois Romieub726e492008-06-28 12:22:59 +02004256 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4257
4258 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4259
Francois Romieu219a1e92008-06-28 11:58:39 +02004260 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004261
4262 rtl_disable_clock_request(pdev);
4263
4264 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004265}
4266
Francois Romieuef3386f2008-06-29 12:24:30 +02004267static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004268{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004269 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004270 { 0x01, 0, 0x0001 },
4271 { 0x02, 0x0800, 0x1000 },
4272 { 0x03, 0, 0x0042 },
4273 { 0x06, 0x0080, 0x0000 },
4274 { 0x07, 0, 0x2000 }
4275 };
4276
françois romieu650e8d52011-01-03 15:08:29 +00004277 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004278
4279 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4280
Francois Romieu219a1e92008-06-28 11:58:39 +02004281 __rtl_hw_start_8168cp(ioaddr, pdev);
4282}
4283
Francois Romieuef3386f2008-06-29 12:24:30 +02004284static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4285{
françois romieu650e8d52011-01-03 15:08:29 +00004286 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004287
4288 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4289
4290 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4291
4292 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4293}
4294
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004295static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4296{
françois romieu650e8d52011-01-03 15:08:29 +00004297 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004298
4299 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4300
4301 /* Magic. */
4302 RTL_W8(DBG_REG, 0x20);
4303
françois romieuf0298f82011-01-03 15:07:42 +00004304 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004305
4306 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4307
4308 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4309}
4310
Francois Romieu219a1e92008-06-28 11:58:39 +02004311static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4312{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004313 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004314 { 0x02, 0x0800, 0x1000 },
4315 { 0x03, 0, 0x0002 },
4316 { 0x06, 0x0080, 0x0000 }
4317 };
4318
françois romieu650e8d52011-01-03 15:08:29 +00004319 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004320
4321 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4322
4323 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4324
Francois Romieu219a1e92008-06-28 11:58:39 +02004325 __rtl_hw_start_8168cp(ioaddr, pdev);
4326}
4327
4328static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4329{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004330 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004331 { 0x01, 0, 0x0001 },
4332 { 0x03, 0x0400, 0x0220 }
4333 };
4334
françois romieu650e8d52011-01-03 15:08:29 +00004335 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004336
4337 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4338
Francois Romieu219a1e92008-06-28 11:58:39 +02004339 __rtl_hw_start_8168cp(ioaddr, pdev);
4340}
4341
Francois Romieu197ff762008-06-28 13:16:02 +02004342static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4343{
4344 rtl_hw_start_8168c_2(ioaddr, pdev);
4345}
4346
Francois Romieu6fb07052008-06-29 11:54:28 +02004347static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4348{
françois romieu650e8d52011-01-03 15:08:29 +00004349 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004350
4351 __rtl_hw_start_8168cp(ioaddr, pdev);
4352}
4353
Francois Romieu5b538df2008-07-20 16:22:45 +02004354static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4355{
françois romieu650e8d52011-01-03 15:08:29 +00004356 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004357
4358 rtl_disable_clock_request(pdev);
4359
françois romieuf0298f82011-01-03 15:07:42 +00004360 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004361
4362 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4363
4364 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4365}
4366
hayeswang4804b3b2011-03-21 01:50:29 +00004367static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4368{
4369 rtl_csi_access_enable_1(ioaddr);
4370
4371 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4372
4373 RTL_W8(MaxTxPacketSize, TxPacketMax);
4374
4375 rtl_disable_clock_request(pdev);
4376}
4377
françois romieue6de30d2011-01-03 15:08:37 +00004378static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4379{
4380 static const struct ephy_info e_info_8168d_4[] = {
4381 { 0x0b, ~0, 0x48 },
4382 { 0x19, 0x20, 0x50 },
4383 { 0x0c, ~0, 0x20 }
4384 };
4385 int i;
4386
4387 rtl_csi_access_enable_1(ioaddr);
4388
4389 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4390
4391 RTL_W8(MaxTxPacketSize, TxPacketMax);
4392
4393 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4394 const struct ephy_info *e = e_info_8168d_4 + i;
4395 u16 w;
4396
4397 w = rtl_ephy_read(ioaddr, e->offset);
4398 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4399 }
4400
4401 rtl_enable_clock_request(pdev);
4402}
4403
Hayes Wang70090422011-07-06 15:58:06 +08004404static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004405{
Hayes Wang70090422011-07-06 15:58:06 +08004406 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004407 { 0x00, 0x0200, 0x0100 },
4408 { 0x00, 0x0000, 0x0004 },
4409 { 0x06, 0x0002, 0x0001 },
4410 { 0x06, 0x0000, 0x0030 },
4411 { 0x07, 0x0000, 0x2000 },
4412 { 0x00, 0x0000, 0x0020 },
4413 { 0x03, 0x5800, 0x2000 },
4414 { 0x03, 0x0000, 0x0001 },
4415 { 0x01, 0x0800, 0x1000 },
4416 { 0x07, 0x0000, 0x4000 },
4417 { 0x1e, 0x0000, 0x2000 },
4418 { 0x19, 0xffff, 0xfe6c },
4419 { 0x0a, 0x0000, 0x0040 }
4420 };
4421
4422 rtl_csi_access_enable_2(ioaddr);
4423
Hayes Wang70090422011-07-06 15:58:06 +08004424 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004425
4426 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4427
4428 RTL_W8(MaxTxPacketSize, TxPacketMax);
4429
4430 rtl_disable_clock_request(pdev);
4431
4432 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004433 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4434 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004435
Francois Romieucecb5fd2011-04-01 10:21:07 +02004436 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004437}
4438
Hayes Wang70090422011-07-06 15:58:06 +08004439static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4440{
4441 static const struct ephy_info e_info_8168e_2[] = {
4442 { 0x09, 0x0000, 0x0080 },
4443 { 0x19, 0x0000, 0x0224 }
4444 };
4445
4446 rtl_csi_access_enable_1(ioaddr);
4447
4448 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4449
4450 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4451
4452 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4453 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4454 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4455 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4456 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4457 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4458 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4459 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4460 ERIAR_EXGMAC);
4461
4462 RTL_W8(MaxTxPacketSize, 0x27);
4463
4464 rtl_disable_clock_request(pdev);
4465
4466 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4467 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4468
4469 /* Adjust EEE LED frequency */
4470 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4471
4472 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4473 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4474 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4475}
4476
Francois Romieu07ce4062007-02-23 23:36:39 +01004477static void rtl_hw_start_8168(struct net_device *dev)
4478{
Francois Romieu2dd99532007-06-11 23:22:52 +02004479 struct rtl8169_private *tp = netdev_priv(dev);
4480 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004481 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004482
4483 RTL_W8(Cfg9346, Cfg9346_Unlock);
4484
françois romieuf0298f82011-01-03 15:07:42 +00004485 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004486
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004487 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004488
Francois Romieu0e485152007-02-20 00:00:26 +01004489 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004490
4491 RTL_W16(CPlusCmd, tp->cp_cmd);
4492
Francois Romieu0e485152007-02-20 00:00:26 +01004493 RTL_W16(IntrMitigate, 0x5151);
4494
4495 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004496 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4497 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004498 tp->intr_event |= RxFIFOOver | PCSTimeout;
4499 tp->intr_event &= ~RxOverflow;
4500 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004501
4502 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4503
Francois Romieub8363902008-06-01 12:31:57 +02004504 rtl_set_rx_mode(dev);
4505
4506 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4507 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004508
4509 RTL_R8(IntrMask);
4510
Francois Romieu219a1e92008-06-28 11:58:39 +02004511 switch (tp->mac_version) {
4512 case RTL_GIGA_MAC_VER_11:
4513 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004514 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004515
4516 case RTL_GIGA_MAC_VER_12:
4517 case RTL_GIGA_MAC_VER_17:
4518 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004519 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004520
4521 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004522 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004523 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004524
4525 case RTL_GIGA_MAC_VER_19:
4526 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004527 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004528
4529 case RTL_GIGA_MAC_VER_20:
4530 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004531 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004532
Francois Romieu197ff762008-06-28 13:16:02 +02004533 case RTL_GIGA_MAC_VER_21:
4534 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004535 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004536
Francois Romieu6fb07052008-06-29 11:54:28 +02004537 case RTL_GIGA_MAC_VER_22:
4538 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004539 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004540
Francois Romieuef3386f2008-06-29 12:24:30 +02004541 case RTL_GIGA_MAC_VER_23:
4542 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004543 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004544
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004545 case RTL_GIGA_MAC_VER_24:
4546 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004547 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004548
Francois Romieu5b538df2008-07-20 16:22:45 +02004549 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004550 case RTL_GIGA_MAC_VER_26:
4551 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004552 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004553 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004554
françois romieue6de30d2011-01-03 15:08:37 +00004555 case RTL_GIGA_MAC_VER_28:
4556 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004557 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004558
hayeswang4804b3b2011-03-21 01:50:29 +00004559 case RTL_GIGA_MAC_VER_31:
4560 rtl_hw_start_8168dp(ioaddr, pdev);
4561 break;
4562
hayeswang01dc7fe2011-03-21 01:50:28 +00004563 case RTL_GIGA_MAC_VER_32:
4564 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004565 rtl_hw_start_8168e_1(ioaddr, pdev);
4566 break;
4567 case RTL_GIGA_MAC_VER_34:
4568 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004569 break;
françois romieue6de30d2011-01-03 15:08:37 +00004570
Francois Romieu219a1e92008-06-28 11:58:39 +02004571 default:
4572 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4573 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004574 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004575 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004576
Francois Romieu0e485152007-02-20 00:00:26 +01004577 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4578
Francois Romieub8363902008-06-01 12:31:57 +02004579 RTL_W8(Cfg9346, Cfg9346_Lock);
4580
Francois Romieu2dd99532007-06-11 23:22:52 +02004581 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004582
Francois Romieu0e485152007-02-20 00:00:26 +01004583 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Francois Romieu2857ffb2008-08-02 21:08:49 +02004586#define R810X_CPCMD_QUIRK_MASK (\
4587 EnableBist | \
4588 Mac_dbgo_oe | \
4589 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004590 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004591 Force_txflow_en | \
4592 Cxpl_dbg_sel | \
4593 ASF | \
4594 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004595 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004596
4597static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4598{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004599 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004600 { 0x01, 0, 0x6e65 },
4601 { 0x02, 0, 0x091f },
4602 { 0x03, 0, 0xc2f9 },
4603 { 0x06, 0, 0xafb5 },
4604 { 0x07, 0, 0x0e00 },
4605 { 0x19, 0, 0xec80 },
4606 { 0x01, 0, 0x2e65 },
4607 { 0x01, 0, 0x6e65 }
4608 };
4609 u8 cfg1;
4610
françois romieu650e8d52011-01-03 15:08:29 +00004611 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004612
4613 RTL_W8(DBG_REG, FIX_NAK_1);
4614
4615 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4616
4617 RTL_W8(Config1,
4618 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4619 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4620
4621 cfg1 = RTL_R8(Config1);
4622 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4623 RTL_W8(Config1, cfg1 & ~LEDS0);
4624
Francois Romieu2857ffb2008-08-02 21:08:49 +02004625 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4626}
4627
4628static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4629{
françois romieu650e8d52011-01-03 15:08:29 +00004630 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004631
4632 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4633
4634 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4635 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004636}
4637
4638static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4639{
4640 rtl_hw_start_8102e_2(ioaddr, pdev);
4641
4642 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4643}
4644
Hayes Wang5a5e4442011-02-22 17:26:21 +08004645static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4646{
4647 static const struct ephy_info e_info_8105e_1[] = {
4648 { 0x07, 0, 0x4000 },
4649 { 0x19, 0, 0x0200 },
4650 { 0x19, 0, 0x0020 },
4651 { 0x1e, 0, 0x2000 },
4652 { 0x03, 0, 0x0001 },
4653 { 0x19, 0, 0x0100 },
4654 { 0x19, 0, 0x0004 },
4655 { 0x0a, 0, 0x0020 }
4656 };
4657
Francois Romieucecb5fd2011-04-01 10:21:07 +02004658 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004659 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4660
Francois Romieucecb5fd2011-04-01 10:21:07 +02004661 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004662 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4663
4664 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004665 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004666
4667 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4668}
4669
4670static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4671{
4672 rtl_hw_start_8105e_1(ioaddr, pdev);
4673 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4674}
4675
Francois Romieu07ce4062007-02-23 23:36:39 +01004676static void rtl_hw_start_8101(struct net_device *dev)
4677{
Francois Romieucdf1a602007-06-11 23:29:50 +02004678 struct rtl8169_private *tp = netdev_priv(dev);
4679 void __iomem *ioaddr = tp->mmio_addr;
4680 struct pci_dev *pdev = tp->pci_dev;
4681
Francois Romieucecb5fd2011-04-01 10:21:07 +02004682 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4683 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004684 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004685
4686 if (cap) {
4687 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4688 PCI_EXP_DEVCTL_NOSNOOP_EN);
4689 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004690 }
4691
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004692 RTL_W8(Cfg9346, Cfg9346_Unlock);
4693
Francois Romieu2857ffb2008-08-02 21:08:49 +02004694 switch (tp->mac_version) {
4695 case RTL_GIGA_MAC_VER_07:
4696 rtl_hw_start_8102e_1(ioaddr, pdev);
4697 break;
4698
4699 case RTL_GIGA_MAC_VER_08:
4700 rtl_hw_start_8102e_3(ioaddr, pdev);
4701 break;
4702
4703 case RTL_GIGA_MAC_VER_09:
4704 rtl_hw_start_8102e_2(ioaddr, pdev);
4705 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004706
4707 case RTL_GIGA_MAC_VER_29:
4708 rtl_hw_start_8105e_1(ioaddr, pdev);
4709 break;
4710 case RTL_GIGA_MAC_VER_30:
4711 rtl_hw_start_8105e_2(ioaddr, pdev);
4712 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004713 }
4714
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004715 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004716
françois romieuf0298f82011-01-03 15:07:42 +00004717 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004718
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004719 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004720
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004721 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004722 RTL_W16(CPlusCmd, tp->cp_cmd);
4723
4724 RTL_W16(IntrMitigate, 0x0000);
4725
4726 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4727
4728 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4729 rtl_set_rx_tx_config_registers(tp);
4730
Francois Romieucdf1a602007-06-11 23:29:50 +02004731 RTL_R8(IntrMask);
4732
Francois Romieucdf1a602007-06-11 23:29:50 +02004733 rtl_set_rx_mode(dev);
4734
4735 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004736
Francois Romieu0e485152007-02-20 00:00:26 +01004737 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738}
4739
4740static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4743 return -EINVAL;
4744
4745 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004746 netdev_update_features(dev);
4747
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749}
4750
4751static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4752{
Al Viro95e09182007-12-22 18:55:39 +00004753 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4755}
4756
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004757static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4758 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004760 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004761 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004762
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004763 kfree(*data_buff);
4764 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 rtl8169_make_unusable_by_asic(desc);
4766}
4767
4768static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4769{
4770 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4771
4772 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4773}
4774
4775static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4776 u32 rx_buf_sz)
4777{
4778 desc->addr = cpu_to_le64(mapping);
4779 wmb();
4780 rtl8169_mark_to_asic(desc, rx_buf_sz);
4781}
4782
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004783static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004785 return (void *)ALIGN((long)data, 16);
4786}
4787
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004788static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4789 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004790{
4791 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004793 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004794 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004795 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004797 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4798 if (!data)
4799 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004800
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004801 if (rtl8169_align(data) != data) {
4802 kfree(data);
4803 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4804 if (!data)
4805 return NULL;
4806 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004807
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004808 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004809 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004810 if (unlikely(dma_mapping_error(d, mapping))) {
4811 if (net_ratelimit())
4812 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004813 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
4816 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004817 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004818
4819err_out:
4820 kfree(data);
4821 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822}
4823
4824static void rtl8169_rx_clear(struct rtl8169_private *tp)
4825{
Francois Romieu07d3f512007-02-21 22:40:46 +01004826 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004829 if (tp->Rx_databuff[i]) {
4830 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 tp->RxDescArray + i);
4832 }
4833 }
4834}
4835
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004836static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004838 desc->opts1 |= cpu_to_le32(RingEnd);
4839}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004840
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004841static int rtl8169_rx_fill(struct rtl8169_private *tp)
4842{
4843 unsigned int i;
4844
4845 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004846 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004847
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004848 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004850
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004851 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004852 if (!data) {
4853 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004854 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004855 }
4856 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004859 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4860 return 0;
4861
4862err_out:
4863 rtl8169_rx_clear(tp);
4864 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867static int rtl8169_init_ring(struct net_device *dev)
4868{
4869 struct rtl8169_private *tp = netdev_priv(dev);
4870
4871 rtl8169_init_ring_indexes(tp);
4872
4873 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004874 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004876 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877}
4878
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004879static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 struct TxDesc *desc)
4881{
4882 unsigned int len = tx_skb->len;
4883
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004884 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4885
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 desc->opts1 = 0x00;
4887 desc->opts2 = 0x00;
4888 desc->addr = 0x00;
4889 tx_skb->len = 0;
4890}
4891
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004892static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4893 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894{
4895 unsigned int i;
4896
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004897 for (i = 0; i < n; i++) {
4898 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 struct ring_info *tx_skb = tp->tx_skb + entry;
4900 unsigned int len = tx_skb->len;
4901
4902 if (len) {
4903 struct sk_buff *skb = tx_skb->skb;
4904
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004905 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 tp->TxDescArray + entry);
4907 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004908 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 dev_kfree_skb(skb);
4910 tx_skb->skb = NULL;
4911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 }
4913 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004914}
4915
4916static void rtl8169_tx_clear(struct rtl8169_private *tp)
4917{
4918 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 tp->cur_tx = tp->dirty_tx = 0;
4920}
4921
David Howellsc4028952006-11-22 14:57:56 +00004922static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923{
4924 struct rtl8169_private *tp = netdev_priv(dev);
4925
David Howellsc4028952006-11-22 14:57:56 +00004926 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 schedule_delayed_work(&tp->task, 4);
4928}
4929
4930static void rtl8169_wait_for_quiescence(struct net_device *dev)
4931{
4932 struct rtl8169_private *tp = netdev_priv(dev);
4933 void __iomem *ioaddr = tp->mmio_addr;
4934
4935 synchronize_irq(dev->irq);
4936
4937 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004938 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
4940 rtl8169_irq_mask_and_ack(ioaddr);
4941
David S. Millerd1d08d12008-01-07 20:53:33 -08004942 tp->intr_mask = 0xffff;
4943 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004944 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945}
4946
David Howellsc4028952006-11-22 14:57:56 +00004947static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948{
David Howellsc4028952006-11-22 14:57:56 +00004949 struct rtl8169_private *tp =
4950 container_of(work, struct rtl8169_private, task.work);
4951 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 int ret;
4953
Francois Romieueb2a0212007-02-15 23:37:21 +01004954 rtnl_lock();
4955
4956 if (!netif_running(dev))
4957 goto out_unlock;
4958
4959 rtl8169_wait_for_quiescence(dev);
4960 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
4962 ret = rtl8169_open(dev);
4963 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004964 if (net_ratelimit())
4965 netif_err(tp, drv, dev,
4966 "reinit failure (status = %d). Rescheduling\n",
4967 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4969 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004970
4971out_unlock:
4972 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973}
4974
David Howellsc4028952006-11-22 14:57:56 +00004975static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976{
David Howellsc4028952006-11-22 14:57:56 +00004977 struct rtl8169_private *tp =
4978 container_of(work, struct rtl8169_private, task.work);
4979 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004980 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
Francois Romieueb2a0212007-02-15 23:37:21 +01004982 rtnl_lock();
4983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004985 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
4987 rtl8169_wait_for_quiescence(dev);
4988
Francois Romieu56de4142011-03-15 17:29:31 +01004989 for (i = 0; i < NUM_RX_DESC; i++)
4990 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4991
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 rtl8169_tx_clear(tp);
4993
Hayes Wang92fc43b2011-07-06 15:58:03 +08004994 rtl8169_hw_reset(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01004995 rtl_hw_start(dev);
4996 netif_wake_queue(dev);
4997 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004998
4999out_unlock:
5000 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001}
5002
5003static void rtl8169_tx_timeout(struct net_device *dev)
5004{
5005 struct rtl8169_private *tp = netdev_priv(dev);
5006
françois romieue6de30d2011-01-03 15:08:37 +00005007 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
5009 /* Let's wait a bit while any (async) irq lands on */
5010 rtl8169_schedule_work(dev, rtl8169_reset_task);
5011}
5012
5013static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005014 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015{
5016 struct skb_shared_info *info = skb_shinfo(skb);
5017 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005018 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005019 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020
5021 entry = tp->cur_tx;
5022 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5023 skb_frag_t *frag = info->frags + cur_frag;
5024 dma_addr_t mapping;
5025 u32 status, len;
5026 void *addr;
5027
5028 entry = (entry + 1) % NUM_TX_DESC;
5029
5030 txd = tp->TxDescArray + entry;
5031 len = frag->size;
5032 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005033 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005034 if (unlikely(dma_mapping_error(d, mapping))) {
5035 if (net_ratelimit())
5036 netif_err(tp, drv, tp->dev,
5037 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005038 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
Francois Romieucecb5fd2011-04-01 10:21:07 +02005041 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005042 status = opts[0] | len |
5043 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
5045 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005046 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 txd->addr = cpu_to_le64(mapping);
5048
5049 tp->tx_skb[entry].len = len;
5050 }
5051
5052 if (cur_frag) {
5053 tp->tx_skb[entry].skb = skb;
5054 txd->opts1 |= cpu_to_le32(LastFrag);
5055 }
5056
5057 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005058
5059err_out:
5060 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5061 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062}
5063
Francois Romieu2b7b4312011-04-18 22:53:24 -07005064static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5065 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005067 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005068 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005069 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
Francois Romieu2b7b4312011-04-18 22:53:24 -07005071 if (mss) {
5072 opts[0] |= TD_LSO;
5073 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5074 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005075 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
5077 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005078 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005080 opts[offset] |= info->checksum.udp;
5081 else
5082 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084}
5085
Stephen Hemminger613573252009-08-31 19:50:58 +00005086static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5087 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088{
5089 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005090 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 struct TxDesc *txd = tp->TxDescArray + entry;
5092 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005093 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 dma_addr_t mapping;
5095 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005096 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005097 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005098
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005100 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005101 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 }
5103
5104 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005105 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005107 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005108 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005109 if (unlikely(dma_mapping_error(d, mapping))) {
5110 if (net_ratelimit())
5111 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005112 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
5115 tp->tx_skb[entry].len = len;
5116 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117
Francois Romieu2b7b4312011-04-18 22:53:24 -07005118 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5119 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005120
Francois Romieu2b7b4312011-04-18 22:53:24 -07005121 rtl8169_tso_csum(tp, skb, opts);
5122
5123 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005124 if (frags < 0)
5125 goto err_dma_1;
5126 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005127 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005128 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005129 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005130 tp->tx_skb[entry].skb = skb;
5131 }
5132
Francois Romieu2b7b4312011-04-18 22:53:24 -07005133 txd->opts2 = cpu_to_le32(opts[1]);
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 wmb();
5136
Francois Romieucecb5fd2011-04-01 10:21:07 +02005137 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005138 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 txd->opts1 = cpu_to_le32(status);
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 tp->cur_tx += frags + 1;
5142
David Dillow4c020a92010-03-03 16:33:10 +00005143 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
Francois Romieucecb5fd2011-04-01 10:21:07 +02005145 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
5147 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5148 netif_stop_queue(dev);
5149 smp_rmb();
5150 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5151 netif_wake_queue(dev);
5152 }
5153
Stephen Hemminger613573252009-08-31 19:50:58 +00005154 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005156err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005157 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005158err_dma_0:
5159 dev_kfree_skb(skb);
5160 dev->stats.tx_dropped++;
5161 return NETDEV_TX_OK;
5162
5163err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005165 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005166 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167}
5168
5169static void rtl8169_pcierr_interrupt(struct net_device *dev)
5170{
5171 struct rtl8169_private *tp = netdev_priv(dev);
5172 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 u16 pci_status, pci_cmd;
5174
5175 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5176 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5177
Joe Perchesbf82c182010-02-09 11:49:50 +00005178 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5179 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
5181 /*
5182 * The recovery sequence below admits a very elaborated explanation:
5183 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005184 * - I did not see what else could be done;
5185 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 *
5187 * Feel free to adjust to your needs.
5188 */
Francois Romieua27993f2006-12-18 00:04:19 +01005189 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005190 pci_cmd &= ~PCI_COMMAND_PARITY;
5191 else
5192 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5193
5194 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
5196 pci_write_config_word(pdev, PCI_STATUS,
5197 pci_status & (PCI_STATUS_DETECTED_PARITY |
5198 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5199 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5200
5201 /* The infamous DAC f*ckup only happens at boot time */
5202 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005203 void __iomem *ioaddr = tp->mmio_addr;
5204
Joe Perchesbf82c182010-02-09 11:49:50 +00005205 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 tp->cp_cmd &= ~PCIDAC;
5207 RTL_W16(CPlusCmd, tp->cp_cmd);
5208 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 }
5210
françois romieue6de30d2011-01-03 15:08:37 +00005211 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005212
5213 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214}
5215
Francois Romieu07d3f512007-02-21 22:40:46 +01005216static void rtl8169_tx_interrupt(struct net_device *dev,
5217 struct rtl8169_private *tp,
5218 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219{
5220 unsigned int dirty_tx, tx_left;
5221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 dirty_tx = tp->dirty_tx;
5223 smp_rmb();
5224 tx_left = tp->cur_tx - dirty_tx;
5225
5226 while (tx_left > 0) {
5227 unsigned int entry = dirty_tx % NUM_TX_DESC;
5228 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 u32 status;
5230
5231 rmb();
5232 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5233 if (status & DescOwn)
5234 break;
5235
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005236 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5237 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005239 dev->stats.tx_packets++;
5240 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005241 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 tx_skb->skb = NULL;
5243 }
5244 dirty_tx++;
5245 tx_left--;
5246 }
5247
5248 if (tp->dirty_tx != dirty_tx) {
5249 tp->dirty_tx = dirty_tx;
5250 smp_wmb();
5251 if (netif_queue_stopped(dev) &&
5252 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5253 netif_wake_queue(dev);
5254 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005255 /*
5256 * 8168 hack: TxPoll requests are lost when the Tx packets are
5257 * too close. Let's kick an extra TxPoll request when a burst
5258 * of start_xmit activity is detected (if it is not detected,
5259 * it is slow enough). -- FR
5260 */
5261 smp_rmb();
5262 if (tp->cur_tx != dirty_tx)
5263 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 }
5265}
5266
Francois Romieu126fa4b2005-05-12 20:09:17 -04005267static inline int rtl8169_fragmented_frame(u32 status)
5268{
5269 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5270}
5271
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005272static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 u32 status = opts1 & RxProtoMask;
5275
5276 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005277 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 skb->ip_summed = CHECKSUM_UNNECESSARY;
5279 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005280 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281}
5282
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005283static struct sk_buff *rtl8169_try_rx_copy(void *data,
5284 struct rtl8169_private *tp,
5285 int pkt_size,
5286 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005288 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005289 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005291 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005292 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005293 prefetch(data);
5294 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5295 if (skb)
5296 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005297 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5298
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005299 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300}
5301
Francois Romieu07d3f512007-02-21 22:40:46 +01005302static int rtl8169_rx_interrupt(struct net_device *dev,
5303 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005304 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305{
5306 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005307 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 cur_rx = tp->cur_rx;
5310 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005311 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005313 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005315 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 u32 status;
5317
5318 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04005319 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
5321 if (status & DescOwn)
5322 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005323 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005324 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5325 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005326 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005328 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005330 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005331 if (status & RxFOVF) {
5332 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005333 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005334 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005335 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005337 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005338 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
Francois Romieu126fa4b2005-05-12 20:09:17 -04005341 /*
5342 * The driver does not support incoming fragmented
5343 * frames. They are seen as a symptom of over-mtu
5344 * sized frames.
5345 */
5346 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005347 dev->stats.rx_dropped++;
5348 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005349 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005350 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005351 }
5352
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005353 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5354 tp, pkt_size, addr);
5355 rtl8169_mark_to_asic(desc, rx_buf_sz);
5356 if (!skb) {
5357 dev->stats.rx_dropped++;
5358 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 }
5360
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005361 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 skb_put(skb, pkt_size);
5363 skb->protocol = eth_type_trans(skb, dev);
5364
Francois Romieu7a8fc772011-03-01 17:18:33 +01005365 rtl8169_rx_vlan_tag(desc, skb);
5366
Francois Romieu56de4142011-03-15 17:29:31 +01005367 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
Francois Romieucebf8cc2007-10-18 12:06:54 +02005369 dev->stats.rx_bytes += pkt_size;
5370 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005372
5373 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005374 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005375 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5376 desc->opts2 = 0;
5377 cur_rx++;
5378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 }
5380
5381 count = cur_rx - tp->cur_rx;
5382 tp->cur_rx = cur_rx;
5383
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005384 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
5386 return count;
5387}
5388
Francois Romieu07d3f512007-02-21 22:40:46 +01005389static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390{
Francois Romieu07d3f512007-02-21 22:40:46 +01005391 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005395 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
David Dillowf11a3772009-05-22 15:29:34 +00005397 /* loop handling interrupts until we have no new ones or
5398 * we hit a invalid/hotplug case.
5399 */
Francois Romieu865c6522008-05-11 14:51:00 +02005400 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005401 while (status && status != 0xffff) {
5402 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
David Dillowf11a3772009-05-22 15:29:34 +00005404 /* Handle all of the error cases first. These will reset
5405 * the chip, so just exit the loop.
5406 */
5407 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005408 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 }
David Dillowf11a3772009-05-22 15:29:34 +00005411
Francois Romieu1519e572011-02-03 12:02:36 +01005412 if (unlikely(status & RxFIFOOver)) {
5413 switch (tp->mac_version) {
5414 /* Work around for rx fifo overflow */
5415 case RTL_GIGA_MAC_VER_11:
5416 case RTL_GIGA_MAC_VER_22:
5417 case RTL_GIGA_MAC_VER_26:
5418 netif_stop_queue(dev);
5419 rtl8169_tx_timeout(dev);
5420 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005421 /* Testers needed. */
5422 case RTL_GIGA_MAC_VER_17:
5423 case RTL_GIGA_MAC_VER_19:
5424 case RTL_GIGA_MAC_VER_20:
5425 case RTL_GIGA_MAC_VER_21:
5426 case RTL_GIGA_MAC_VER_23:
5427 case RTL_GIGA_MAC_VER_24:
5428 case RTL_GIGA_MAC_VER_27:
5429 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005430 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005431 /* Experimental science. Pktgen proof. */
5432 case RTL_GIGA_MAC_VER_12:
5433 case RTL_GIGA_MAC_VER_25:
5434 if (status == RxFIFOOver)
5435 goto done;
5436 break;
5437 default:
5438 break;
5439 }
David Dillowf11a3772009-05-22 15:29:34 +00005440 }
5441
5442 if (unlikely(status & SYSErr)) {
5443 rtl8169_pcierr_interrupt(dev);
5444 break;
5445 }
5446
5447 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005448 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005449
5450 /* We need to see the lastest version of tp->intr_mask to
5451 * avoid ignoring an MSI interrupt and having to wait for
5452 * another event which may never come.
5453 */
5454 smp_rmb();
5455 if (status & tp->intr_mask & tp->napi_event) {
5456 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5457 tp->intr_mask = ~tp->napi_event;
5458
5459 if (likely(napi_schedule_prep(&tp->napi)))
5460 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005461 else
5462 netif_info(tp, intr, dev,
5463 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005464 }
5465
5466 /* We only get a new MSI interrupt when all active irq
5467 * sources on the chip have been acknowledged. So, ack
5468 * everything we've seen and check if new sources have become
5469 * active to avoid blocking all interrupts from the chip.
5470 */
5471 RTL_W16(IntrStatus,
5472 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5473 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 }
Francois Romieu1519e572011-02-03 12:02:36 +01005475done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 return IRQ_RETVAL(handled);
5477}
5478
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005479static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005481 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5482 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005484 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005486 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 rtl8169_tx_interrupt(dev, tp, ioaddr);
5488
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005489 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005490 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005491
5492 /* We need for force the visibility of tp->intr_mask
5493 * for other CPUs, as we can loose an MSI interrupt
5494 * and potentially wait for a retransmit timeout if we don't.
5495 * The posted write to IntrMask is safe, as it will
5496 * eventually make it to the chip and we won't loose anything
5497 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 */
David Dillowf11a3772009-05-22 15:29:34 +00005499 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005500 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005501 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
5503
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005504 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Francois Romieu523a6092008-09-10 22:28:56 +02005507static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5508{
5509 struct rtl8169_private *tp = netdev_priv(dev);
5510
5511 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5512 return;
5513
5514 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5515 RTL_W32(RxMissed, 0);
5516}
5517
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518static void rtl8169_down(struct net_device *dev)
5519{
5520 struct rtl8169_private *tp = netdev_priv(dev);
5521 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
Francois Romieu4876cc12011-03-11 21:07:11 +01005523 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
5525 netif_stop_queue(dev);
5526
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005527 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005528
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 spin_lock_irq(&tp->lock);
5530
Hayes Wang92fc43b2011-07-06 15:58:03 +08005531 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005532 /*
5533 * At this point device interrupts can not be enabled in any function,
5534 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5535 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5536 */
Francois Romieu523a6092008-09-10 22:28:56 +02005537 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
5539 spin_unlock_irq(&tp->lock);
5540
5541 synchronize_irq(dev->irq);
5542
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005544 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 rtl8169_tx_clear(tp);
5547
5548 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005549
5550 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551}
5552
5553static int rtl8169_close(struct net_device *dev)
5554{
5555 struct rtl8169_private *tp = netdev_priv(dev);
5556 struct pci_dev *pdev = tp->pci_dev;
5557
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005558 pm_runtime_get_sync(&pdev->dev);
5559
Francois Romieucecb5fd2011-04-01 10:21:07 +02005560 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005561 rtl8169_update_counters(dev);
5562
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 rtl8169_down(dev);
5564
5565 free_irq(dev->irq, dev);
5566
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005567 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5568 tp->RxPhyAddr);
5569 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5570 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 tp->TxDescArray = NULL;
5572 tp->RxDescArray = NULL;
5573
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005574 pm_runtime_put_sync(&pdev->dev);
5575
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 return 0;
5577}
5578
Francois Romieu07ce4062007-02-23 23:36:39 +01005579static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580{
5581 struct rtl8169_private *tp = netdev_priv(dev);
5582 void __iomem *ioaddr = tp->mmio_addr;
5583 unsigned long flags;
5584 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005585 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 u32 tmp = 0;
5587
5588 if (dev->flags & IFF_PROMISC) {
5589 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005590 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 rx_mode =
5592 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5593 AcceptAllPhys;
5594 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005595 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005596 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 /* Too many to filter perfectly -- accept all multicasts. */
5598 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5599 mc_filter[1] = mc_filter[0] = 0xffffffff;
5600 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005601 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 rx_mode = AcceptBroadcast | AcceptMyPhys;
5604 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005605 netdev_for_each_mc_addr(ha, dev) {
5606 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5608 rx_mode |= AcceptMulticast;
5609 }
5610 }
5611
5612 spin_lock_irqsave(&tp->lock, flags);
5613
Francois Romieu1687b562011-07-19 17:21:29 +02005614 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
Francois Romieuf887cce2008-07-17 22:24:18 +02005616 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005617 u32 data = mc_filter[0];
5618
5619 mc_filter[0] = swab32(mc_filter[1]);
5620 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005621 }
5622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005624 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
Francois Romieu57a9f232007-06-04 22:10:15 +02005626 RTL_W32(RxConfig, tmp);
5627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 spin_unlock_irqrestore(&tp->lock, flags);
5629}
5630
5631/**
5632 * rtl8169_get_stats - Get rtl8169 read/write statistics
5633 * @dev: The Ethernet Device to get statistics for
5634 *
5635 * Get TX/RX statistics for rtl8169
5636 */
5637static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5638{
5639 struct rtl8169_private *tp = netdev_priv(dev);
5640 void __iomem *ioaddr = tp->mmio_addr;
5641 unsigned long flags;
5642
5643 if (netif_running(dev)) {
5644 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005645 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 spin_unlock_irqrestore(&tp->lock, flags);
5647 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005648
Francois Romieucebf8cc2007-10-18 12:06:54 +02005649 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650}
5651
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005652static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005653{
françois romieu065c27c2011-01-03 15:08:12 +00005654 struct rtl8169_private *tp = netdev_priv(dev);
5655
Francois Romieu5d06a992006-02-23 00:47:58 +01005656 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005657 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005658
françois romieu065c27c2011-01-03 15:08:12 +00005659 rtl_pll_power_down(tp);
5660
Francois Romieu5d06a992006-02-23 00:47:58 +01005661 netif_device_detach(dev);
5662 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005663}
Francois Romieu5d06a992006-02-23 00:47:58 +01005664
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005665#ifdef CONFIG_PM
5666
5667static int rtl8169_suspend(struct device *device)
5668{
5669 struct pci_dev *pdev = to_pci_dev(device);
5670 struct net_device *dev = pci_get_drvdata(pdev);
5671
5672 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005673
Francois Romieu5d06a992006-02-23 00:47:58 +01005674 return 0;
5675}
5676
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005677static void __rtl8169_resume(struct net_device *dev)
5678{
françois romieu065c27c2011-01-03 15:08:12 +00005679 struct rtl8169_private *tp = netdev_priv(dev);
5680
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005681 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005682
5683 rtl_pll_power_up(tp);
5684
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005685 rtl8169_schedule_work(dev, rtl8169_reset_task);
5686}
5687
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005688static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005689{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005690 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005691 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005692 struct rtl8169_private *tp = netdev_priv(dev);
5693
5694 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005695
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005696 if (netif_running(dev))
5697 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005698
Francois Romieu5d06a992006-02-23 00:47:58 +01005699 return 0;
5700}
5701
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005702static int rtl8169_runtime_suspend(struct device *device)
5703{
5704 struct pci_dev *pdev = to_pci_dev(device);
5705 struct net_device *dev = pci_get_drvdata(pdev);
5706 struct rtl8169_private *tp = netdev_priv(dev);
5707
5708 if (!tp->TxDescArray)
5709 return 0;
5710
5711 spin_lock_irq(&tp->lock);
5712 tp->saved_wolopts = __rtl8169_get_wol(tp);
5713 __rtl8169_set_wol(tp, WAKE_ANY);
5714 spin_unlock_irq(&tp->lock);
5715
5716 rtl8169_net_suspend(dev);
5717
5718 return 0;
5719}
5720
5721static int rtl8169_runtime_resume(struct device *device)
5722{
5723 struct pci_dev *pdev = to_pci_dev(device);
5724 struct net_device *dev = pci_get_drvdata(pdev);
5725 struct rtl8169_private *tp = netdev_priv(dev);
5726
5727 if (!tp->TxDescArray)
5728 return 0;
5729
5730 spin_lock_irq(&tp->lock);
5731 __rtl8169_set_wol(tp, tp->saved_wolopts);
5732 tp->saved_wolopts = 0;
5733 spin_unlock_irq(&tp->lock);
5734
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005735 rtl8169_init_phy(dev, tp);
5736
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005737 __rtl8169_resume(dev);
5738
5739 return 0;
5740}
5741
5742static int rtl8169_runtime_idle(struct device *device)
5743{
5744 struct pci_dev *pdev = to_pci_dev(device);
5745 struct net_device *dev = pci_get_drvdata(pdev);
5746 struct rtl8169_private *tp = netdev_priv(dev);
5747
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005748 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005749}
5750
Alexey Dobriyan47145212009-12-14 18:00:08 -08005751static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005752 .suspend = rtl8169_suspend,
5753 .resume = rtl8169_resume,
5754 .freeze = rtl8169_suspend,
5755 .thaw = rtl8169_resume,
5756 .poweroff = rtl8169_suspend,
5757 .restore = rtl8169_resume,
5758 .runtime_suspend = rtl8169_runtime_suspend,
5759 .runtime_resume = rtl8169_runtime_resume,
5760 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005761};
5762
5763#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5764
5765#else /* !CONFIG_PM */
5766
5767#define RTL8169_PM_OPS NULL
5768
5769#endif /* !CONFIG_PM */
5770
Francois Romieu1765f952008-09-13 17:21:40 +02005771static void rtl_shutdown(struct pci_dev *pdev)
5772{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005773 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005774 struct rtl8169_private *tp = netdev_priv(dev);
5775 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005776
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005777 rtl8169_net_suspend(dev);
5778
Francois Romieucecb5fd2011-04-01 10:21:07 +02005779 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005780 rtl_rar_set(tp, dev->perm_addr);
5781
françois romieu4bb3f522009-06-17 11:41:45 +00005782 spin_lock_irq(&tp->lock);
5783
Hayes Wang92fc43b2011-07-06 15:58:03 +08005784 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005785
5786 spin_unlock_irq(&tp->lock);
5787
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005788 if (system_state == SYSTEM_POWER_OFF) {
Hayes Wangaaa89c02011-07-06 15:58:08 +08005789 /* WoL fails with 8168b when the receiver is disabled. */
5790 if ((tp->mac_version == RTL_GIGA_MAC_VER_11 ||
5791 tp->mac_version == RTL_GIGA_MAC_VER_12 ||
5792 tp->mac_version == RTL_GIGA_MAC_VER_17) &&
5793 (tp->features & RTL_FEATURE_WOL)) {
françois romieuca52efd2009-07-24 12:34:19 +00005794 pci_clear_master(pdev);
5795
5796 RTL_W8(ChipCmd, CmdRxEnb);
5797 /* PCI commit */
5798 RTL_R8(ChipCmd);
5799 }
5800
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005801 pci_wake_from_d3(pdev, true);
5802 pci_set_power_state(pdev, PCI_D3hot);
5803 }
5804}
Francois Romieu5d06a992006-02-23 00:47:58 +01005805
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806static struct pci_driver rtl8169_pci_driver = {
5807 .name = MODULENAME,
5808 .id_table = rtl8169_pci_tbl,
5809 .probe = rtl8169_init_one,
5810 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005811 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005812 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813};
5814
Francois Romieu07d3f512007-02-21 22:40:46 +01005815static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816{
Jeff Garzik29917622006-08-19 17:48:59 -04005817 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818}
5819
Francois Romieu07d3f512007-02-21 22:40:46 +01005820static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821{
5822 pci_unregister_driver(&rtl8169_pci_driver);
5823}
5824
5825module_init(rtl8169_init_module);
5826module_exit(rtl8169_cleanup_module);