blob: 3ddd33936aa7f9d7127248b2f606c7df920a36fb [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 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200242 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200243 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
244 { PCI_VENDOR_ID_LINKSYS, 0x1032,
245 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100246 { 0x0001, 0x8168,
247 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 {0,},
249};
250
251MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
252
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000253static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700254static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200255static struct {
256 u32 msg_enable;
257} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Francois Romieu07d3f512007-02-21 22:40:46 +0100259enum rtl_registers {
260 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100261 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100262 MAR0 = 8, /* Multicast filter. */
263 CounterAddrLow = 0x10,
264 CounterAddrHigh = 0x14,
265 TxDescStartAddrLow = 0x20,
266 TxDescStartAddrHigh = 0x24,
267 TxHDescStartAddrLow = 0x28,
268 TxHDescStartAddrHigh = 0x2c,
269 FLASH = 0x30,
270 ERSR = 0x36,
271 ChipCmd = 0x37,
272 TxPoll = 0x38,
273 IntrMask = 0x3c,
274 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700275
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800276 TxConfig = 0x40,
277#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
278#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
279
280 RxConfig = 0x44,
281#define RX128_INT_EN (1 << 15) /* 8111c and later */
282#define RX_MULTI_EN (1 << 14) /* 8111c only */
283#define RXCFG_FIFO_SHIFT 13
284 /* No threshold before first PCI xfer */
285#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
286#define RXCFG_DMA_SHIFT 8
287 /* Unlimited maximum PCI burst. */
288#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700289
Francois Romieu07d3f512007-02-21 22:40:46 +0100290 RxMissed = 0x4c,
291 Cfg9346 = 0x50,
292 Config0 = 0x51,
293 Config1 = 0x52,
294 Config2 = 0x53,
295 Config3 = 0x54,
296 Config4 = 0x55,
297 Config5 = 0x56,
298 MultiIntr = 0x5c,
299 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100300 PHYstatus = 0x6c,
301 RxMaxSize = 0xda,
302 CPlusCmd = 0xe0,
303 IntrMitigate = 0xe2,
304 RxDescAddrLow = 0xe4,
305 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000306 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
307
308#define NoEarlyTx 0x3f /* Max value : no early transmit. */
309
310 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
311
312#define TxPacketMax (8064 >> 7)
313
Francois Romieu07d3f512007-02-21 22:40:46 +0100314 FuncEvent = 0xf0,
315 FuncEventMask = 0xf4,
316 FuncPresetState = 0xf8,
317 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
319
Francois Romieuf162a5d2008-06-01 22:37:49 +0200320enum rtl8110_registers {
321 TBICSR = 0x64,
322 TBI_ANAR = 0x68,
323 TBI_LPAR = 0x6a,
324};
325
326enum rtl8168_8101_registers {
327 CSIDR = 0x64,
328 CSIAR = 0x68,
329#define CSIAR_FLAG 0x80000000
330#define CSIAR_WRITE_CMD 0x80000000
331#define CSIAR_BYTE_ENABLE 0x0f
332#define CSIAR_BYTE_ENABLE_SHIFT 12
333#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000334 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200335 EPHYAR = 0x80,
336#define EPHYAR_FLAG 0x80000000
337#define EPHYAR_WRITE_CMD 0x80000000
338#define EPHYAR_REG_MASK 0x1f
339#define EPHYAR_REG_SHIFT 16
340#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800341 DLLPR = 0xd0,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800342#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200343 DBG_REG = 0xd1,
344#define FIX_NAK_1 (1 << 4)
345#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800346 TWSI = 0xd2,
347 MCU = 0xd3,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800348#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800349#define EN_NDP (1 << 3)
350#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000351 EFUSEAR = 0xdc,
352#define EFUSEAR_FLAG 0x80000000
353#define EFUSEAR_WRITE_CMD 0x80000000
354#define EFUSEAR_READ_CMD 0x00000000
355#define EFUSEAR_REG_MASK 0x03ff
356#define EFUSEAR_REG_SHIFT 8
357#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200358};
359
françois romieuc0e45c12011-01-03 15:08:04 +0000360enum rtl8168_registers {
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800361 LED_FREQ = 0x1a,
362 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000363 ERIDR = 0x70,
364 ERIAR = 0x74,
365#define ERIAR_FLAG 0x80000000
366#define ERIAR_WRITE_CMD 0x80000000
367#define ERIAR_READ_CMD 0x00000000
368#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000369#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800370#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
371#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
372#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
373#define ERIAR_MASK_SHIFT 12
374#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
375#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
376#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000377 EPHY_RXER_NUM = 0x7c,
378 OCPDR = 0xb0, /* OCP GPHY access */
379#define OCPDR_WRITE_CMD 0x80000000
380#define OCPDR_READ_CMD 0x00000000
381#define OCPDR_REG_MASK 0x7f
382#define OCPDR_GPHY_REG_SHIFT 16
383#define OCPDR_DATA_MASK 0xffff
384 OCPAR = 0xb4,
385#define OCPAR_FLAG 0x80000000
386#define OCPAR_GPHY_WRITE_CMD 0x8000f060
387#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000388 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
389 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200390#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800391#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000392};
393
Francois Romieu07d3f512007-02-21 22:40:46 +0100394enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100396 SYSErr = 0x8000,
397 PCSTimeout = 0x4000,
398 SWInt = 0x0100,
399 TxDescUnavail = 0x0080,
400 RxFIFOOver = 0x0040,
401 LinkChg = 0x0020,
402 RxOverflow = 0x0010,
403 TxErr = 0x0008,
404 TxOK = 0x0004,
405 RxErr = 0x0002,
406 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200409 RxFOVF = (1 << 23),
410 RxRWT = (1 << 22),
411 RxRES = (1 << 21),
412 RxRUNT = (1 << 20),
413 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* ChipCmdBits */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800416 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100417 CmdReset = 0x10,
418 CmdRxEnb = 0x08,
419 CmdTxEnb = 0x04,
420 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Francois Romieu275391a2007-02-23 23:50:28 +0100422 /* TXPoll register p.5 */
423 HPQ = 0x80, /* Poll cmd on the high prio queue */
424 NPQ = 0x40, /* Poll cmd on the low prio queue */
425 FSWInt = 0x01, /* Forced software interrupt */
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100428 Cfg9346_Lock = 0x00,
429 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100432 AcceptErr = 0x20,
433 AcceptRunt = 0x10,
434 AcceptBroadcast = 0x08,
435 AcceptMulticast = 0x04,
436 AcceptMyPhys = 0x02,
437 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /* TxConfigBits */
440 TxInterFrameGapShift = 24,
441 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
442
Francois Romieu5d06a992006-02-23 00:47:58 +0100443 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200444 LEDS1 = (1 << 7),
445 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200446 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200447 Speed_down = (1 << 4),
448 MEMMAP = (1 << 3),
449 IOMAP = (1 << 2),
450 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100451 PMEnable = (1 << 0), /* Power Management Enable */
452
Francois Romieu6dccd162007-02-13 23:38:05 +0100453 /* Config2 register p. 25 */
454 PCI_Clock_66MHz = 0x01,
455 PCI_Clock_33MHz = 0x00,
456
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100457 /* Config3 register p.25 */
458 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
459 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200460 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100461
Francois Romieu5d06a992006-02-23 00:47:58 +0100462 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100463 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
464 MWF = (1 << 5), /* Accept Multicast wakeup frame */
465 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200466 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100467 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100468 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* TBICSR p.28 */
471 TBIReset = 0x80000000,
472 TBILoopback = 0x40000000,
473 TBINwEnable = 0x20000000,
474 TBINwRestart = 0x10000000,
475 TBILinkOk = 0x02000000,
476 TBINwComplete = 0x01000000,
477
478 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200479 EnableBist = (1 << 15), // 8168 8101
480 Mac_dbgo_oe = (1 << 14), // 8168 8101
481 Normal_mode = (1 << 13), // unused
482 Force_half_dup = (1 << 12), // 8168 8101
483 Force_rxflow_en = (1 << 11), // 8168 8101
484 Force_txflow_en = (1 << 10), // 8168 8101
485 Cxpl_dbg_sel = (1 << 9), // 8168 8101
486 ASF = (1 << 8), // 8168 8101
487 PktCntrDisable = (1 << 7), // 8168 8101
488 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 RxVlan = (1 << 6),
490 RxChkSum = (1 << 5),
491 PCIDAC = (1 << 4),
492 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100493 INTT_0 = 0x0000, // 8168
494 INTT_1 = 0x0001, // 8168
495 INTT_2 = 0x0002, // 8168
496 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100499 TBI_Enable = 0x80,
500 TxFlowCtrl = 0x40,
501 RxFlowCtrl = 0x20,
502 _1000bpsF = 0x10,
503 _100bps = 0x08,
504 _10bps = 0x04,
505 LinkStatus = 0x02,
506 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100509 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200510
511 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100512 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513};
514
Francois Romieu2b7b4312011-04-18 22:53:24 -0700515enum rtl_desc_bit {
516 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
518 RingEnd = (1 << 30), /* End of descriptor ring */
519 FirstFrag = (1 << 29), /* First segment of a packet */
520 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700521};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Francois Romieu2b7b4312011-04-18 22:53:24 -0700523/* Generic case. */
524enum rtl_tx_desc_bit {
525 /* First doubleword. */
526 TD_LSO = (1 << 27), /* Large Send Offload */
527#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Francois Romieu2b7b4312011-04-18 22:53:24 -0700529 /* Second doubleword. */
530 TxVlanTag = (1 << 17), /* Add VLAN tag */
531};
532
533/* 8169, 8168b and 810x except 8102e. */
534enum rtl_tx_desc_bit_0 {
535 /* First doubleword. */
536#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
537 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
538 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
539 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
540};
541
542/* 8102e, 8168c and beyond. */
543enum rtl_tx_desc_bit_1 {
544 /* Second doubleword. */
545#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
546 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
547 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
548 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
549};
550
551static const struct rtl_tx_desc_info {
552 struct {
553 u32 udp;
554 u32 tcp;
555 } checksum;
556 u16 mss_shift;
557 u16 opts_offset;
558} tx_desc_info [] = {
559 [RTL_TD_0] = {
560 .checksum = {
561 .udp = TD0_IP_CS | TD0_UDP_CS,
562 .tcp = TD0_IP_CS | TD0_TCP_CS
563 },
564 .mss_shift = TD0_MSS_SHIFT,
565 .opts_offset = 0
566 },
567 [RTL_TD_1] = {
568 .checksum = {
569 .udp = TD1_IP_CS | TD1_UDP_CS,
570 .tcp = TD1_IP_CS | TD1_TCP_CS
571 },
572 .mss_shift = TD1_MSS_SHIFT,
573 .opts_offset = 1
574 }
575};
576
577enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* Rx private */
579 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
580 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
581
582#define RxProtoUDP (PID1)
583#define RxProtoTCP (PID0)
584#define RxProtoIP (PID1 | PID0)
585#define RxProtoMask RxProtoIP
586
587 IPFail = (1 << 16), /* IP checksum failed */
588 UDPFail = (1 << 15), /* UDP/IP checksum failed */
589 TCPFail = (1 << 14), /* TCP/IP checksum failed */
590 RxVlanTag = (1 << 16), /* VLAN tag available */
591};
592
593#define RsvdMask 0x3fffc000
594
595struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200596 __le32 opts1;
597 __le32 opts2;
598 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599};
600
601struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200602 __le32 opts1;
603 __le32 opts2;
604 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605};
606
607struct ring_info {
608 struct sk_buff *skb;
609 u32 len;
610 u8 __pad[sizeof(void *) - sizeof(u32)];
611};
612
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200613enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200614 RTL_FEATURE_WOL = (1 << 0),
615 RTL_FEATURE_MSI = (1 << 1),
616 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200617};
618
Ivan Vecera355423d2009-02-06 21:49:57 -0800619struct rtl8169_counters {
620 __le64 tx_packets;
621 __le64 rx_packets;
622 __le64 tx_errors;
623 __le32 rx_errors;
624 __le16 rx_missed;
625 __le16 align_errors;
626 __le32 tx_one_collision;
627 __le32 tx_multi_collision;
628 __le64 rx_unicast;
629 __le64 rx_broadcast;
630 __le32 rx_multicast;
631 __le16 tx_aborted;
632 __le16 tx_underun;
633};
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635struct rtl8169_private {
636 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200637 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000638 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700639 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200640 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200641 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700642 u16 txd_version;
643 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
645 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
646 u32 dirty_rx;
647 u32 dirty_tx;
648 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
649 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
650 dma_addr_t TxPhyAddr;
651 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000652 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct timer_list timer;
655 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100656 u16 intr_event;
657 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000659
660 struct mdio_ops {
661 void (*write)(void __iomem *, int, int);
662 int (*read)(void __iomem *, int);
663 } mdio_ops;
664
françois romieu065c27c2011-01-03 15:08:12 +0000665 struct pll_power_ops {
666 void (*down)(struct rtl8169_private *);
667 void (*up)(struct rtl8169_private *);
668 } pll_power_ops;
669
Oliver Neukum54405cd2011-01-06 21:55:13 +0100670 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200671 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000672 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100673 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000674 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800676 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
David Howellsc4028952006-11-22 14:57:56 +0000677 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200678 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200679
680 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800681 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000682 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000683
Francois Romieub6ffd972011-06-17 17:00:05 +0200684 struct rtl_fw {
685 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200686
687#define RTL_VER_SIZE 32
688
689 char version[RTL_VER_SIZE];
690
691 struct rtl_fw_phy_action {
692 __le32 *code;
693 size_t size;
694 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200695 } *rtl_fw;
François Romieu953a12c2011-04-24 17:38:48 +0200696#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697};
698
Ralf Baechle979b6c12005-06-13 14:30:40 -0700699MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700702MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200703module_param_named(debug, debug.msg_enable, int, 0);
704MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705MODULE_LICENSE("GPL");
706MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000707MODULE_FIRMWARE(FIRMWARE_8168D_1);
708MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000709MODULE_FIRMWARE(FIRMWARE_8168E_1);
710MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800711MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000714static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
715 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100716static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100718static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100720static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200722static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700724 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200725static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200727static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700728static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
françois romieub646d902011-01-03 15:08:21 +0000730static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
731{
732 void __iomem *ioaddr = tp->mmio_addr;
733 int i;
734
735 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
736 for (i = 0; i < 20; i++) {
737 udelay(100);
738 if (RTL_R32(OCPAR) & OCPAR_FLAG)
739 break;
740 }
741 return RTL_R32(OCPDR);
742}
743
744static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
745{
746 void __iomem *ioaddr = tp->mmio_addr;
747 int i;
748
749 RTL_W32(OCPDR, data);
750 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
751 for (i = 0; i < 20; i++) {
752 udelay(100);
753 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
754 break;
755 }
756}
757
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800758static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000759{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800760 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000761 int i;
762
763 RTL_W8(ERIDR, cmd);
764 RTL_W32(ERIAR, 0x800010e8);
765 msleep(2);
766 for (i = 0; i < 5; i++) {
767 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200768 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000769 break;
770 }
771
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800772 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000773}
774
775#define OOB_CMD_RESET 0x00
776#define OOB_CMD_DRIVER_START 0x05
777#define OOB_CMD_DRIVER_STOP 0x06
778
Francois Romieucecb5fd2011-04-01 10:21:07 +0200779static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
780{
781 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
782}
783
françois romieub646d902011-01-03 15:08:21 +0000784static void rtl8168_driver_start(struct rtl8169_private *tp)
785{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200786 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000787 int i;
788
789 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
790
Francois Romieucecb5fd2011-04-01 10:21:07 +0200791 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000792
françois romieub646d902011-01-03 15:08:21 +0000793 for (i = 0; i < 10; i++) {
794 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000795 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000796 break;
797 }
798}
799
800static void rtl8168_driver_stop(struct rtl8169_private *tp)
801{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200802 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000803 int i;
804
805 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
806
Francois Romieucecb5fd2011-04-01 10:21:07 +0200807 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000808
françois romieub646d902011-01-03 15:08:21 +0000809 for (i = 0; i < 10; i++) {
810 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000811 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000812 break;
813 }
814}
815
hayeswang4804b3b2011-03-21 01:50:29 +0000816static int r8168dp_check_dash(struct rtl8169_private *tp)
817{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200818 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000819
Francois Romieucecb5fd2011-04-01 10:21:07 +0200820 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000821}
françois romieub646d902011-01-03 15:08:21 +0000822
françois romieu4da19632011-01-03 15:07:55 +0000823static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 int i;
826
Francois Romieua6baf3a2007-11-08 23:23:21 +0100827 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Francois Romieu23714082006-01-29 00:49:09 +0100829 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100830 /*
831 * Check if the RTL8169 has completed writing to the specified
832 * MII register.
833 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200834 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 break;
Francois Romieu23714082006-01-29 00:49:09 +0100836 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700838 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700839 * According to hardware specs a 20us delay is required after write
840 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700841 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700842 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
françois romieu4da19632011-01-03 15:07:55 +0000845static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 int i, value = -1;
848
Francois Romieua6baf3a2007-11-08 23:23:21 +0100849 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Francois Romieu23714082006-01-29 00:49:09 +0100851 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100852 /*
853 * Check if the RTL8169 has completed retrieving data from
854 * the specified MII register.
855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100857 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859 }
Francois Romieu23714082006-01-29 00:49:09 +0100860 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700862 /*
863 * According to hardware specs a 20us delay is required after read
864 * complete indication, but before sending next command.
865 */
866 udelay(20);
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return value;
869}
870
françois romieuc0e45c12011-01-03 15:08:04 +0000871static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
872{
873 int i;
874
875 RTL_W32(OCPDR, data |
876 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
877 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
878 RTL_W32(EPHY_RXER_NUM, 0);
879
880 for (i = 0; i < 100; i++) {
881 mdelay(1);
882 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
883 break;
884 }
885}
886
887static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
888{
889 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
890 (value & OCPDR_DATA_MASK));
891}
892
893static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
894{
895 int i;
896
897 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
898
899 mdelay(1);
900 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
901 RTL_W32(EPHY_RXER_NUM, 0);
902
903 for (i = 0; i < 100; i++) {
904 mdelay(1);
905 if (RTL_R32(OCPAR) & OCPAR_FLAG)
906 break;
907 }
908
909 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
910}
911
françois romieue6de30d2011-01-03 15:08:37 +0000912#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
913
914static void r8168dp_2_mdio_start(void __iomem *ioaddr)
915{
916 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
917}
918
919static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
920{
921 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
922}
923
924static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
925{
926 r8168dp_2_mdio_start(ioaddr);
927
928 r8169_mdio_write(ioaddr, reg_addr, value);
929
930 r8168dp_2_mdio_stop(ioaddr);
931}
932
933static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
934{
935 int value;
936
937 r8168dp_2_mdio_start(ioaddr);
938
939 value = r8169_mdio_read(ioaddr, reg_addr);
940
941 r8168dp_2_mdio_stop(ioaddr);
942
943 return value;
944}
945
françois romieu4da19632011-01-03 15:07:55 +0000946static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200947{
françois romieuc0e45c12011-01-03 15:08:04 +0000948 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200949}
950
françois romieu4da19632011-01-03 15:07:55 +0000951static int rtl_readphy(struct rtl8169_private *tp, int location)
952{
françois romieuc0e45c12011-01-03 15:08:04 +0000953 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000954}
955
956static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
957{
958 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
959}
960
961static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000962{
963 int val;
964
françois romieu4da19632011-01-03 15:07:55 +0000965 val = rtl_readphy(tp, reg_addr);
966 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000967}
968
Francois Romieuccdffb92008-07-26 14:26:06 +0200969static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
970 int val)
971{
972 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200973
françois romieu4da19632011-01-03 15:07:55 +0000974 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200975}
976
977static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
978{
979 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200980
françois romieu4da19632011-01-03 15:07:55 +0000981 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200982}
983
Francois Romieudacf8152008-08-02 20:44:13 +0200984static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
985{
986 unsigned int i;
987
988 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
989 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
990
991 for (i = 0; i < 100; i++) {
992 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
993 break;
994 udelay(10);
995 }
996}
997
998static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
999{
1000 u16 value = 0xffff;
1001 unsigned int i;
1002
1003 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1004
1005 for (i = 0; i < 100; i++) {
1006 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1007 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1008 break;
1009 }
1010 udelay(10);
1011 }
1012
1013 return value;
1014}
1015
1016static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1017{
1018 unsigned int i;
1019
1020 RTL_W32(CSIDR, value);
1021 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1022 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1023
1024 for (i = 0; i < 100; i++) {
1025 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1026 break;
1027 udelay(10);
1028 }
1029}
1030
1031static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1032{
1033 u32 value = ~0x00;
1034 unsigned int i;
1035
1036 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1037 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1038
1039 for (i = 0; i < 100; i++) {
1040 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1041 value = RTL_R32(CSIDR);
1042 break;
1043 }
1044 udelay(10);
1045 }
1046
1047 return value;
1048}
1049
Hayes Wang133ac402011-07-06 15:58:05 +08001050static
1051void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1052{
1053 unsigned int i;
1054
1055 BUG_ON((addr & 3) || (mask == 0));
1056 RTL_W32(ERIDR, val);
1057 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1058
1059 for (i = 0; i < 100; i++) {
1060 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1061 break;
1062 udelay(100);
1063 }
1064}
1065
1066static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1067{
1068 u32 value = ~0x00;
1069 unsigned int i;
1070
1071 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1072
1073 for (i = 0; i < 100; i++) {
1074 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1075 value = RTL_R32(ERIDR);
1076 break;
1077 }
1078 udelay(100);
1079 }
1080
1081 return value;
1082}
1083
1084static void
1085rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1086{
1087 u32 val;
1088
1089 val = rtl_eri_read(ioaddr, addr, type);
1090 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1091}
1092
françois romieudaf9df62009-10-07 12:44:20 +00001093static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1094{
1095 u8 value = 0xff;
1096 unsigned int i;
1097
1098 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1099
1100 for (i = 0; i < 300; i++) {
1101 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1102 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1103 break;
1104 }
1105 udelay(100);
1106 }
1107
1108 return value;
1109}
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1112{
1113 RTL_W16(IntrMask, 0x0000);
1114
1115 RTL_W16(IntrStatus, 0xffff);
1116}
1117
françois romieu4da19632011-01-03 15:07:55 +00001118static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
françois romieu4da19632011-01-03 15:07:55 +00001120 void __iomem *ioaddr = tp->mmio_addr;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return RTL_R32(TBICSR) & TBIReset;
1123}
1124
françois romieu4da19632011-01-03 15:07:55 +00001125static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
françois romieu4da19632011-01-03 15:07:55 +00001127 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1131{
1132 return RTL_R32(TBICSR) & TBILinkOk;
1133}
1134
1135static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1136{
1137 return RTL_R8(PHYstatus) & LinkStatus;
1138}
1139
françois romieu4da19632011-01-03 15:07:55 +00001140static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
françois romieu4da19632011-01-03 15:07:55 +00001142 void __iomem *ioaddr = tp->mmio_addr;
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1145}
1146
françois romieu4da19632011-01-03 15:07:55 +00001147static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 unsigned int val;
1150
françois romieu4da19632011-01-03 15:07:55 +00001151 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1152 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Hayes Wang70090422011-07-06 15:58:06 +08001155static void rtl_link_chg_patch(struct rtl8169_private *tp)
1156{
1157 void __iomem *ioaddr = tp->mmio_addr;
1158 struct net_device *dev = tp->dev;
1159
1160 if (!netif_running(dev))
1161 return;
1162
1163 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1164 if (RTL_R8(PHYstatus) & _1000bpsF) {
1165 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1166 0x00000011, ERIAR_EXGMAC);
1167 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1168 0x00000005, ERIAR_EXGMAC);
1169 } else if (RTL_R8(PHYstatus) & _100bps) {
1170 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1171 0x0000001f, ERIAR_EXGMAC);
1172 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1173 0x00000005, ERIAR_EXGMAC);
1174 } else {
1175 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1176 0x0000001f, ERIAR_EXGMAC);
1177 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1178 0x0000003f, ERIAR_EXGMAC);
1179 }
1180 /* Reset packet filter */
1181 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1182 ERIAR_EXGMAC);
1183 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1184 ERIAR_EXGMAC);
1185 }
1186}
1187
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001188static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001189 struct rtl8169_private *tp,
1190 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 unsigned long flags;
1193
1194 spin_lock_irqsave(&tp->lock, flags);
1195 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001196 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001197 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001198 if (pm)
1199 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001201 if (net_ratelimit())
1202 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001203 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001205 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001206 if (pm)
1207 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 spin_unlock_irqrestore(&tp->lock, flags);
1210}
1211
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001212static void rtl8169_check_link_status(struct net_device *dev,
1213 struct rtl8169_private *tp,
1214 void __iomem *ioaddr)
1215{
1216 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1217}
1218
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001219#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1220
1221static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1222{
1223 void __iomem *ioaddr = tp->mmio_addr;
1224 u8 options;
1225 u32 wolopts = 0;
1226
1227 options = RTL_R8(Config1);
1228 if (!(options & PMEnable))
1229 return 0;
1230
1231 options = RTL_R8(Config3);
1232 if (options & LinkUp)
1233 wolopts |= WAKE_PHY;
1234 if (options & MagicPacket)
1235 wolopts |= WAKE_MAGIC;
1236
1237 options = RTL_R8(Config5);
1238 if (options & UWF)
1239 wolopts |= WAKE_UCAST;
1240 if (options & BWF)
1241 wolopts |= WAKE_BCAST;
1242 if (options & MWF)
1243 wolopts |= WAKE_MCAST;
1244
1245 return wolopts;
1246}
1247
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001248static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1249{
1250 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001251
1252 spin_lock_irq(&tp->lock);
1253
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001254 wol->supported = WAKE_ANY;
1255 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001256
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001257 spin_unlock_irq(&tp->lock);
1258}
1259
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001260static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001261{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001262 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001263 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001264 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001265 u32 opt;
1266 u16 reg;
1267 u8 mask;
1268 } cfg[] = {
1269 { WAKE_ANY, Config1, PMEnable },
1270 { WAKE_PHY, Config3, LinkUp },
1271 { WAKE_MAGIC, Config3, MagicPacket },
1272 { WAKE_UCAST, Config5, UWF },
1273 { WAKE_BCAST, Config5, BWF },
1274 { WAKE_MCAST, Config5, MWF },
1275 { WAKE_ANY, Config5, LanWake }
1276 };
1277
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001278 RTL_W8(Cfg9346, Cfg9346_Unlock);
1279
1280 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1281 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001282 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001283 options |= cfg[i].mask;
1284 RTL_W8(cfg[i].reg, options);
1285 }
1286
1287 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001288}
1289
1290static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1291{
1292 struct rtl8169_private *tp = netdev_priv(dev);
1293
1294 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001295
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001296 if (wol->wolopts)
1297 tp->features |= RTL_FEATURE_WOL;
1298 else
1299 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001300 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001301 spin_unlock_irq(&tp->lock);
1302
françois romieuea809072010-11-08 13:23:58 +00001303 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1304
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001305 return 0;
1306}
1307
Francois Romieu31bd2042011-04-26 18:58:59 +02001308static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1309{
Francois Romieu85bffe62011-04-27 08:22:39 +02001310 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001311}
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313static void rtl8169_get_drvinfo(struct net_device *dev,
1314 struct ethtool_drvinfo *info)
1315{
1316 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001317 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 strcpy(info->driver, MODULENAME);
1320 strcpy(info->version, RTL8169_VERSION);
1321 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001322 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1323 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1324 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327static int rtl8169_get_regs_len(struct net_device *dev)
1328{
1329 return R8169_REGS_SIZE;
1330}
1331
1332static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001333 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 struct rtl8169_private *tp = netdev_priv(dev);
1336 void __iomem *ioaddr = tp->mmio_addr;
1337 int ret = 0;
1338 u32 reg;
1339
1340 reg = RTL_R32(TBICSR);
1341 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1342 (duplex == DUPLEX_FULL)) {
1343 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1344 } else if (autoneg == AUTONEG_ENABLE)
1345 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1346 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001347 netif_warn(tp, link, dev,
1348 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 ret = -EOPNOTSUPP;
1350 }
1351
1352 return ret;
1353}
1354
1355static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001356 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001359 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001360 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Hayes Wang716b50a2011-02-22 17:26:18 +08001362 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001365 int auto_nego;
1366
françois romieu4da19632011-01-03 15:07:55 +00001367 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001368 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1369 ADVERTISE_100HALF | ADVERTISE_100FULL);
1370
1371 if (adv & ADVERTISED_10baseT_Half)
1372 auto_nego |= ADVERTISE_10HALF;
1373 if (adv & ADVERTISED_10baseT_Full)
1374 auto_nego |= ADVERTISE_10FULL;
1375 if (adv & ADVERTISED_100baseT_Half)
1376 auto_nego |= ADVERTISE_100HALF;
1377 if (adv & ADVERTISED_100baseT_Full)
1378 auto_nego |= ADVERTISE_100FULL;
1379
françois romieu3577aa12009-05-19 10:46:48 +00001380 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1381
françois romieu4da19632011-01-03 15:07:55 +00001382 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001383 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1384
1385 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001386 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001387 if (adv & ADVERTISED_1000baseT_Half)
1388 giga_ctrl |= ADVERTISE_1000HALF;
1389 if (adv & ADVERTISED_1000baseT_Full)
1390 giga_ctrl |= ADVERTISE_1000FULL;
1391 } else if (adv & (ADVERTISED_1000baseT_Half |
1392 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001393 netif_info(tp, link, dev,
1394 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001395 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
françois romieu3577aa12009-05-19 10:46:48 +00001398 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001399
françois romieu4da19632011-01-03 15:07:55 +00001400 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1401 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001402 } else {
1403 giga_ctrl = 0;
1404
1405 if (speed == SPEED_10)
1406 bmcr = 0;
1407 else if (speed == SPEED_100)
1408 bmcr = BMCR_SPEED100;
1409 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001410 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001411
1412 if (duplex == DUPLEX_FULL)
1413 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001414 }
1415
françois romieu4da19632011-01-03 15:07:55 +00001416 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001417
Francois Romieucecb5fd2011-04-01 10:21:07 +02001418 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1419 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001420 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001421 rtl_writephy(tp, 0x17, 0x2138);
1422 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001423 } else {
françois romieu4da19632011-01-03 15:07:55 +00001424 rtl_writephy(tp, 0x17, 0x2108);
1425 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001426 }
1427 }
1428
Oliver Neukum54405cd2011-01-06 21:55:13 +01001429 rc = 0;
1430out:
1431 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
1434static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001435 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 struct rtl8169_private *tp = netdev_priv(dev);
1438 int ret;
1439
Oliver Neukum54405cd2011-01-06 21:55:13 +01001440 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001441 if (ret < 0)
1442 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Francois Romieu4876cc12011-03-11 21:07:11 +01001444 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1445 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001447 }
1448out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return ret;
1450}
1451
1452static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1453{
1454 struct rtl8169_private *tp = netdev_priv(dev);
1455 unsigned long flags;
1456 int ret;
1457
Francois Romieu4876cc12011-03-11 21:07:11 +01001458 del_timer_sync(&tp->timer);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001461 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001462 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return ret;
1466}
1467
Michał Mirosław350fb322011-04-08 06:35:56 +00001468static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001470 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001471 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Michał Mirosław350fb322011-04-08 06:35:56 +00001473 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Michał Mirosław350fb322011-04-08 06:35:56 +00001476static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct rtl8169_private *tp = netdev_priv(dev);
1479 void __iomem *ioaddr = tp->mmio_addr;
1480 unsigned long flags;
1481
1482 spin_lock_irqsave(&tp->lock, flags);
1483
Michał Mirosław350fb322011-04-08 06:35:56 +00001484 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 tp->cp_cmd |= RxChkSum;
1486 else
1487 tp->cp_cmd &= ~RxChkSum;
1488
Michał Mirosław350fb322011-04-08 06:35:56 +00001489 if (dev->features & NETIF_F_HW_VLAN_RX)
1490 tp->cp_cmd |= RxVlan;
1491 else
1492 tp->cp_cmd &= ~RxVlan;
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 RTL_W16(CPlusCmd, tp->cp_cmd);
1495 RTL_R16(CPlusCmd);
1496
1497 spin_unlock_irqrestore(&tp->lock, flags);
1498
1499 return 0;
1500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1503 struct sk_buff *skb)
1504{
Jesse Grosseab6d182010-10-20 13:56:03 +00001505 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1507}
1508
Francois Romieu7a8fc772011-03-01 17:18:33 +01001509static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Francois Romieu7a8fc772011-03-01 17:18:33 +01001513 if (opts2 & RxVlanTag)
1514 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
Francois Romieuccdffb92008-07-26 14:26:06 +02001519static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 struct rtl8169_private *tp = netdev_priv(dev);
1522 void __iomem *ioaddr = tp->mmio_addr;
1523 u32 status;
1524
1525 cmd->supported =
1526 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1527 cmd->port = PORT_FIBRE;
1528 cmd->transceiver = XCVR_INTERNAL;
1529
1530 status = RTL_R32(TBICSR);
1531 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1532 cmd->autoneg = !!(status & TBINwEnable);
1533
David Decotigny70739492011-04-27 18:32:40 +00001534 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001536
1537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
Francois Romieuccdffb92008-07-26 14:26:06 +02001540static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Francois Romieuccdffb92008-07-26 14:26:06 +02001544 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
1547static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1548{
1549 struct rtl8169_private *tp = netdev_priv(dev);
1550 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001551 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 spin_lock_irqsave(&tp->lock, flags);
1554
Francois Romieuccdffb92008-07-26 14:26:06 +02001555 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001558 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1562 void *p)
1563{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001564 struct rtl8169_private *tp = netdev_priv(dev);
1565 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Francois Romieu5b0384f2006-08-16 16:00:01 +02001567 if (regs->len > R8169_REGS_SIZE)
1568 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Francois Romieu5b0384f2006-08-16 16:00:01 +02001570 spin_lock_irqsave(&tp->lock, flags);
1571 memcpy_fromio(p, tp->mmio_addr, regs->len);
1572 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573}
1574
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001575static u32 rtl8169_get_msglevel(struct net_device *dev)
1576{
1577 struct rtl8169_private *tp = netdev_priv(dev);
1578
1579 return tp->msg_enable;
1580}
1581
1582static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1583{
1584 struct rtl8169_private *tp = netdev_priv(dev);
1585
1586 tp->msg_enable = value;
1587}
1588
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001589static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1590 "tx_packets",
1591 "rx_packets",
1592 "tx_errors",
1593 "rx_errors",
1594 "rx_missed",
1595 "align_errors",
1596 "tx_single_collisions",
1597 "tx_multi_collisions",
1598 "unicast",
1599 "broadcast",
1600 "multicast",
1601 "tx_aborted",
1602 "tx_underrun",
1603};
1604
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001605static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001606{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001607 switch (sset) {
1608 case ETH_SS_STATS:
1609 return ARRAY_SIZE(rtl8169_gstrings);
1610 default:
1611 return -EOPNOTSUPP;
1612 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001613}
1614
Ivan Vecera355423d2009-02-06 21:49:57 -08001615static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001616{
1617 struct rtl8169_private *tp = netdev_priv(dev);
1618 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001619 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001620 struct rtl8169_counters *counters;
1621 dma_addr_t paddr;
1622 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001623 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001624
Ivan Vecera355423d2009-02-06 21:49:57 -08001625 /*
1626 * Some chips are unable to dump tally counters when the receiver
1627 * is disabled.
1628 */
1629 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1630 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001631
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001632 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001633 if (!counters)
1634 return;
1635
1636 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001637 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001638 RTL_W32(CounterAddrLow, cmd);
1639 RTL_W32(CounterAddrLow, cmd | CounterDump);
1640
Ivan Vecera355423d2009-02-06 21:49:57 -08001641 while (wait--) {
1642 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001643 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001644 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001645 }
1646 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001647 }
1648
1649 RTL_W32(CounterAddrLow, 0);
1650 RTL_W32(CounterAddrHigh, 0);
1651
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001652 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001653}
1654
Ivan Vecera355423d2009-02-06 21:49:57 -08001655static void rtl8169_get_ethtool_stats(struct net_device *dev,
1656 struct ethtool_stats *stats, u64 *data)
1657{
1658 struct rtl8169_private *tp = netdev_priv(dev);
1659
1660 ASSERT_RTNL();
1661
1662 rtl8169_update_counters(dev);
1663
1664 data[0] = le64_to_cpu(tp->counters.tx_packets);
1665 data[1] = le64_to_cpu(tp->counters.rx_packets);
1666 data[2] = le64_to_cpu(tp->counters.tx_errors);
1667 data[3] = le32_to_cpu(tp->counters.rx_errors);
1668 data[4] = le16_to_cpu(tp->counters.rx_missed);
1669 data[5] = le16_to_cpu(tp->counters.align_errors);
1670 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1671 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1672 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1673 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1674 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1675 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1676 data[12] = le16_to_cpu(tp->counters.tx_underun);
1677}
1678
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001679static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1680{
1681 switch(stringset) {
1682 case ETH_SS_STATS:
1683 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1684 break;
1685 }
1686}
1687
Jeff Garzik7282d492006-09-13 14:30:00 -04001688static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 .get_drvinfo = rtl8169_get_drvinfo,
1690 .get_regs_len = rtl8169_get_regs_len,
1691 .get_link = ethtool_op_get_link,
1692 .get_settings = rtl8169_get_settings,
1693 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001694 .get_msglevel = rtl8169_get_msglevel,
1695 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001697 .get_wol = rtl8169_get_wol,
1698 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001699 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001700 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001701 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702};
1703
Francois Romieu07d3f512007-02-21 22:40:46 +01001704static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001705 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Francois Romieu5d320a22011-05-08 17:47:36 +02001707 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001708 /*
1709 * The driver currently handles the 8168Bf and the 8168Be identically
1710 * but they can be identified more specifically through the test below
1711 * if needed:
1712 *
1713 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001714 *
1715 * Same thing for the 8101Eb and the 8101Ec:
1716 *
1717 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001718 */
Francois Romieu37441002011-06-17 22:58:54 +02001719 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001721 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 int mac_version;
1723 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001724 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001725 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001726 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1727 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1728 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1729
Francois Romieu5b538df2008-07-20 16:22:45 +02001730 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001731 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1732 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001733 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001734
françois romieue6de30d2011-01-03 15:08:37 +00001735 /* 8168DP family. */
1736 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1737 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001738 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001739
Francois Romieuef808d52008-06-29 13:10:54 +02001740 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001741 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001742 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001743 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001744 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001745 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1746 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001747 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001748 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001749 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001750
1751 /* 8168B family. */
1752 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1753 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1754 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1755 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1756
1757 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001758 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001759 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1760 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1761 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001762 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1763 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1764 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1765 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1766 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1767 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001768 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001769 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001770 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001771 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1772 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001773 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1774 /* FIXME: where did these entries come from ? -- FR */
1775 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1776 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1777
1778 /* 8110 family. */
1779 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1780 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1781 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1782 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1783 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1784 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1785
Jean Delvaref21b75e2009-05-26 20:54:48 -07001786 /* Catch-all */
1787 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001788 };
1789 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 u32 reg;
1791
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001792 reg = RTL_R32(TxConfig);
1793 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 p++;
1795 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001796
1797 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1798 netif_notice(tp, probe, dev,
1799 "unknown MAC, using family default\n");
1800 tp->mac_version = default_version;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
1804static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1805{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001806 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Francois Romieu867763c2007-08-17 18:21:58 +02001809struct phy_reg {
1810 u16 reg;
1811 u16 val;
1812};
1813
françois romieu4da19632011-01-03 15:07:55 +00001814static void rtl_writephy_batch(struct rtl8169_private *tp,
1815 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001816{
1817 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001818 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001819 regs++;
1820 }
1821}
1822
françois romieubca03d52011-01-03 15:07:31 +00001823#define PHY_READ 0x00000000
1824#define PHY_DATA_OR 0x10000000
1825#define PHY_DATA_AND 0x20000000
1826#define PHY_BJMPN 0x30000000
1827#define PHY_READ_EFUSE 0x40000000
1828#define PHY_READ_MAC_BYTE 0x50000000
1829#define PHY_WRITE_MAC_BYTE 0x60000000
1830#define PHY_CLEAR_READCOUNT 0x70000000
1831#define PHY_WRITE 0x80000000
1832#define PHY_READCOUNT_EQ_SKIP 0x90000000
1833#define PHY_COMP_EQ_SKIPN 0xa0000000
1834#define PHY_COMP_NEQ_SKIPN 0xb0000000
1835#define PHY_WRITE_PREVIOUS 0xc0000000
1836#define PHY_SKIPN 0xd0000000
1837#define PHY_DELAY_MS 0xe0000000
1838#define PHY_WRITE_ERI_WORD 0xf0000000
1839
Hayes Wang960aee62011-06-18 11:37:48 +02001840struct fw_info {
1841 u32 magic;
1842 char version[RTL_VER_SIZE];
1843 __le32 fw_start;
1844 __le32 fw_len;
1845 u8 chksum;
1846} __packed;
1847
Francois Romieu1c361ef2011-06-17 17:16:24 +02001848#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1849
1850static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001851{
Francois Romieub6ffd972011-06-17 17:00:05 +02001852 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001853 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001854 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1855 char *version = rtl_fw->version;
1856 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001857
Francois Romieu1c361ef2011-06-17 17:16:24 +02001858 if (fw->size < FW_OPCODE_SIZE)
1859 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001860
1861 if (!fw_info->magic) {
1862 size_t i, size, start;
1863 u8 checksum = 0;
1864
1865 if (fw->size < sizeof(*fw_info))
1866 goto out;
1867
1868 for (i = 0; i < fw->size; i++)
1869 checksum += fw->data[i];
1870 if (checksum != 0)
1871 goto out;
1872
1873 start = le32_to_cpu(fw_info->fw_start);
1874 if (start > fw->size)
1875 goto out;
1876
1877 size = le32_to_cpu(fw_info->fw_len);
1878 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1879 goto out;
1880
1881 memcpy(version, fw_info->version, RTL_VER_SIZE);
1882
1883 pa->code = (__le32 *)(fw->data + start);
1884 pa->size = size;
1885 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001886 if (fw->size % FW_OPCODE_SIZE)
1887 goto out;
1888
1889 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1890
1891 pa->code = (__le32 *)fw->data;
1892 pa->size = fw->size / FW_OPCODE_SIZE;
1893 }
1894 version[RTL_VER_SIZE - 1] = 0;
1895
1896 rc = true;
1897out:
1898 return rc;
1899}
1900
Francois Romieufd112f22011-06-18 00:10:29 +02001901static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1902 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02001903{
Francois Romieufd112f22011-06-18 00:10:29 +02001904 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001905 size_t index;
1906
Francois Romieu1c361ef2011-06-17 17:16:24 +02001907 for (index = 0; index < pa->size; index++) {
1908 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00001909 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001910
hayeswang42b82dc2011-01-10 02:07:25 +00001911 switch(action & 0xf0000000) {
1912 case PHY_READ:
1913 case PHY_DATA_OR:
1914 case PHY_DATA_AND:
1915 case PHY_READ_EFUSE:
1916 case PHY_CLEAR_READCOUNT:
1917 case PHY_WRITE:
1918 case PHY_WRITE_PREVIOUS:
1919 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001920 break;
1921
hayeswang42b82dc2011-01-10 02:07:25 +00001922 case PHY_BJMPN:
1923 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02001924 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001925 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001926 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001927 }
1928 break;
1929 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001930 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001931 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001932 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001933 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001934 }
1935 break;
1936 case PHY_COMP_EQ_SKIPN:
1937 case PHY_COMP_NEQ_SKIPN:
1938 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001939 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001940 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001941 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001942 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001943 }
1944 break;
1945
1946 case PHY_READ_MAC_BYTE:
1947 case PHY_WRITE_MAC_BYTE:
1948 case PHY_WRITE_ERI_WORD:
1949 default:
Francois Romieufd112f22011-06-18 00:10:29 +02001950 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00001951 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02001952 goto out;
françois romieubca03d52011-01-03 15:07:31 +00001953 }
1954 }
Francois Romieufd112f22011-06-18 00:10:29 +02001955 rc = true;
1956out:
1957 return rc;
1958}
françois romieubca03d52011-01-03 15:07:31 +00001959
Francois Romieufd112f22011-06-18 00:10:29 +02001960static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1961{
1962 struct net_device *dev = tp->dev;
1963 int rc = -EINVAL;
1964
1965 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1966 netif_err(tp, ifup, dev, "invalid firwmare\n");
1967 goto out;
1968 }
1969
1970 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1971 rc = 0;
1972out:
1973 return rc;
1974}
1975
1976static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1977{
1978 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1979 u32 predata, count;
1980 size_t index;
1981
1982 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00001983
Francois Romieu1c361ef2011-06-17 17:16:24 +02001984 for (index = 0; index < pa->size; ) {
1985 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00001986 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001987 u32 regno = (action & 0x0fff0000) >> 16;
1988
1989 if (!action)
1990 break;
françois romieubca03d52011-01-03 15:07:31 +00001991
1992 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001993 case PHY_READ:
1994 predata = rtl_readphy(tp, regno);
1995 count++;
1996 index++;
françois romieubca03d52011-01-03 15:07:31 +00001997 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001998 case PHY_DATA_OR:
1999 predata |= data;
2000 index++;
2001 break;
2002 case PHY_DATA_AND:
2003 predata &= data;
2004 index++;
2005 break;
2006 case PHY_BJMPN:
2007 index -= regno;
2008 break;
2009 case PHY_READ_EFUSE:
2010 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2011 index++;
2012 break;
2013 case PHY_CLEAR_READCOUNT:
2014 count = 0;
2015 index++;
2016 break;
2017 case PHY_WRITE:
2018 rtl_writephy(tp, regno, data);
2019 index++;
2020 break;
2021 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002022 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002023 break;
2024 case PHY_COMP_EQ_SKIPN:
2025 if (predata == data)
2026 index += regno;
2027 index++;
2028 break;
2029 case PHY_COMP_NEQ_SKIPN:
2030 if (predata != data)
2031 index += regno;
2032 index++;
2033 break;
2034 case PHY_WRITE_PREVIOUS:
2035 rtl_writephy(tp, regno, predata);
2036 index++;
2037 break;
2038 case PHY_SKIPN:
2039 index += regno + 1;
2040 break;
2041 case PHY_DELAY_MS:
2042 mdelay(data);
2043 index++;
2044 break;
2045
2046 case PHY_READ_MAC_BYTE:
2047 case PHY_WRITE_MAC_BYTE:
2048 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002049 default:
2050 BUG();
2051 }
2052 }
2053}
2054
françois romieuf1e02ed2011-01-13 13:07:53 +00002055static void rtl_release_firmware(struct rtl8169_private *tp)
2056{
Francois Romieub6ffd972011-06-17 17:00:05 +02002057 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2058 release_firmware(tp->rtl_fw->fw);
2059 kfree(tp->rtl_fw);
2060 }
2061 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002062}
2063
François Romieu953a12c2011-04-24 17:38:48 +02002064static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002065{
Francois Romieub6ffd972011-06-17 17:00:05 +02002066 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002067
2068 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002069 if (!IS_ERR_OR_NULL(rtl_fw))
2070 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002071}
2072
2073static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2074{
2075 if (rtl_readphy(tp, reg) != val)
2076 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2077 else
2078 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002079}
2080
françois romieu4da19632011-01-03 15:07:55 +00002081static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002083 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002084 { 0x1f, 0x0001 },
2085 { 0x06, 0x006e },
2086 { 0x08, 0x0708 },
2087 { 0x15, 0x4000 },
2088 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
françois romieu0b9b5712009-08-10 19:44:56 +00002090 { 0x1f, 0x0001 },
2091 { 0x03, 0x00a1 },
2092 { 0x02, 0x0008 },
2093 { 0x01, 0x0120 },
2094 { 0x00, 0x1000 },
2095 { 0x04, 0x0800 },
2096 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
françois romieu0b9b5712009-08-10 19:44:56 +00002098 { 0x03, 0xff41 },
2099 { 0x02, 0xdf60 },
2100 { 0x01, 0x0140 },
2101 { 0x00, 0x0077 },
2102 { 0x04, 0x7800 },
2103 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
françois romieu0b9b5712009-08-10 19:44:56 +00002105 { 0x03, 0x802f },
2106 { 0x02, 0x4f02 },
2107 { 0x01, 0x0409 },
2108 { 0x00, 0xf0f9 },
2109 { 0x04, 0x9800 },
2110 { 0x04, 0x9000 },
2111
2112 { 0x03, 0xdf01 },
2113 { 0x02, 0xdf20 },
2114 { 0x01, 0xff95 },
2115 { 0x00, 0xba00 },
2116 { 0x04, 0xa800 },
2117 { 0x04, 0xa000 },
2118
2119 { 0x03, 0xff41 },
2120 { 0x02, 0xdf20 },
2121 { 0x01, 0x0140 },
2122 { 0x00, 0x00bb },
2123 { 0x04, 0xb800 },
2124 { 0x04, 0xb000 },
2125
2126 { 0x03, 0xdf41 },
2127 { 0x02, 0xdc60 },
2128 { 0x01, 0x6340 },
2129 { 0x00, 0x007d },
2130 { 0x04, 0xd800 },
2131 { 0x04, 0xd000 },
2132
2133 { 0x03, 0xdf01 },
2134 { 0x02, 0xdf20 },
2135 { 0x01, 0x100a },
2136 { 0x00, 0xa0ff },
2137 { 0x04, 0xf800 },
2138 { 0x04, 0xf000 },
2139
2140 { 0x1f, 0x0000 },
2141 { 0x0b, 0x0000 },
2142 { 0x00, 0x9200 }
2143 };
2144
françois romieu4da19632011-01-03 15:07:55 +00002145 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
françois romieu4da19632011-01-03 15:07:55 +00002148static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002149{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002150 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002151 { 0x1f, 0x0002 },
2152 { 0x01, 0x90d0 },
2153 { 0x1f, 0x0000 }
2154 };
2155
françois romieu4da19632011-01-03 15:07:55 +00002156 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002157}
2158
françois romieu4da19632011-01-03 15:07:55 +00002159static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002160{
2161 struct pci_dev *pdev = tp->pci_dev;
2162 u16 vendor_id, device_id;
2163
2164 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
2165 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
2166
2167 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
2168 return;
2169
françois romieu4da19632011-01-03 15:07:55 +00002170 rtl_writephy(tp, 0x1f, 0x0001);
2171 rtl_writephy(tp, 0x10, 0xf01b);
2172 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002173}
2174
françois romieu4da19632011-01-03 15:07:55 +00002175static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002176{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002177 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002178 { 0x1f, 0x0001 },
2179 { 0x04, 0x0000 },
2180 { 0x03, 0x00a1 },
2181 { 0x02, 0x0008 },
2182 { 0x01, 0x0120 },
2183 { 0x00, 0x1000 },
2184 { 0x04, 0x0800 },
2185 { 0x04, 0x9000 },
2186 { 0x03, 0x802f },
2187 { 0x02, 0x4f02 },
2188 { 0x01, 0x0409 },
2189 { 0x00, 0xf099 },
2190 { 0x04, 0x9800 },
2191 { 0x04, 0xa000 },
2192 { 0x03, 0xdf01 },
2193 { 0x02, 0xdf20 },
2194 { 0x01, 0xff95 },
2195 { 0x00, 0xba00 },
2196 { 0x04, 0xa800 },
2197 { 0x04, 0xf000 },
2198 { 0x03, 0xdf01 },
2199 { 0x02, 0xdf20 },
2200 { 0x01, 0x101a },
2201 { 0x00, 0xa0ff },
2202 { 0x04, 0xf800 },
2203 { 0x04, 0x0000 },
2204 { 0x1f, 0x0000 },
2205
2206 { 0x1f, 0x0001 },
2207 { 0x10, 0xf41b },
2208 { 0x14, 0xfb54 },
2209 { 0x18, 0xf5c7 },
2210 { 0x1f, 0x0000 },
2211
2212 { 0x1f, 0x0001 },
2213 { 0x17, 0x0cc0 },
2214 { 0x1f, 0x0000 }
2215 };
2216
françois romieu4da19632011-01-03 15:07:55 +00002217 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002218
françois romieu4da19632011-01-03 15:07:55 +00002219 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002220}
2221
françois romieu4da19632011-01-03 15:07:55 +00002222static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002223{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002224 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002225 { 0x1f, 0x0001 },
2226 { 0x04, 0x0000 },
2227 { 0x03, 0x00a1 },
2228 { 0x02, 0x0008 },
2229 { 0x01, 0x0120 },
2230 { 0x00, 0x1000 },
2231 { 0x04, 0x0800 },
2232 { 0x04, 0x9000 },
2233 { 0x03, 0x802f },
2234 { 0x02, 0x4f02 },
2235 { 0x01, 0x0409 },
2236 { 0x00, 0xf099 },
2237 { 0x04, 0x9800 },
2238 { 0x04, 0xa000 },
2239 { 0x03, 0xdf01 },
2240 { 0x02, 0xdf20 },
2241 { 0x01, 0xff95 },
2242 { 0x00, 0xba00 },
2243 { 0x04, 0xa800 },
2244 { 0x04, 0xf000 },
2245 { 0x03, 0xdf01 },
2246 { 0x02, 0xdf20 },
2247 { 0x01, 0x101a },
2248 { 0x00, 0xa0ff },
2249 { 0x04, 0xf800 },
2250 { 0x04, 0x0000 },
2251 { 0x1f, 0x0000 },
2252
2253 { 0x1f, 0x0001 },
2254 { 0x0b, 0x8480 },
2255 { 0x1f, 0x0000 },
2256
2257 { 0x1f, 0x0001 },
2258 { 0x18, 0x67c7 },
2259 { 0x04, 0x2000 },
2260 { 0x03, 0x002f },
2261 { 0x02, 0x4360 },
2262 { 0x01, 0x0109 },
2263 { 0x00, 0x3022 },
2264 { 0x04, 0x2800 },
2265 { 0x1f, 0x0000 },
2266
2267 { 0x1f, 0x0001 },
2268 { 0x17, 0x0cc0 },
2269 { 0x1f, 0x0000 }
2270 };
2271
françois romieu4da19632011-01-03 15:07:55 +00002272 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002273}
2274
françois romieu4da19632011-01-03 15:07:55 +00002275static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002276{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002277 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002278 { 0x10, 0xf41b },
2279 { 0x1f, 0x0000 }
2280 };
2281
françois romieu4da19632011-01-03 15:07:55 +00002282 rtl_writephy(tp, 0x1f, 0x0001);
2283 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002284
françois romieu4da19632011-01-03 15:07:55 +00002285 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002286}
2287
françois romieu4da19632011-01-03 15:07:55 +00002288static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002289{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002290 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002291 { 0x1f, 0x0001 },
2292 { 0x10, 0xf41b },
2293 { 0x1f, 0x0000 }
2294 };
2295
françois romieu4da19632011-01-03 15:07:55 +00002296 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002297}
2298
françois romieu4da19632011-01-03 15:07:55 +00002299static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002300{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002301 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002302 { 0x1f, 0x0000 },
2303 { 0x1d, 0x0f00 },
2304 { 0x1f, 0x0002 },
2305 { 0x0c, 0x1ec8 },
2306 { 0x1f, 0x0000 }
2307 };
2308
françois romieu4da19632011-01-03 15:07:55 +00002309 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002310}
2311
françois romieu4da19632011-01-03 15:07:55 +00002312static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002313{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002314 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002315 { 0x1f, 0x0001 },
2316 { 0x1d, 0x3d98 },
2317 { 0x1f, 0x0000 }
2318 };
2319
françois romieu4da19632011-01-03 15:07:55 +00002320 rtl_writephy(tp, 0x1f, 0x0000);
2321 rtl_patchphy(tp, 0x14, 1 << 5);
2322 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002323
françois romieu4da19632011-01-03 15:07:55 +00002324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002325}
2326
françois romieu4da19632011-01-03 15:07:55 +00002327static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002328{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002329 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002330 { 0x1f, 0x0001 },
2331 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002332 { 0x1f, 0x0002 },
2333 { 0x00, 0x88d4 },
2334 { 0x01, 0x82b1 },
2335 { 0x03, 0x7002 },
2336 { 0x08, 0x9e30 },
2337 { 0x09, 0x01f0 },
2338 { 0x0a, 0x5500 },
2339 { 0x0c, 0x00c8 },
2340 { 0x1f, 0x0003 },
2341 { 0x12, 0xc096 },
2342 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002343 { 0x1f, 0x0000 },
2344 { 0x1f, 0x0000 },
2345 { 0x09, 0x2000 },
2346 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002347 };
2348
françois romieu4da19632011-01-03 15:07:55 +00002349 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002350
françois romieu4da19632011-01-03 15:07:55 +00002351 rtl_patchphy(tp, 0x14, 1 << 5);
2352 rtl_patchphy(tp, 0x0d, 1 << 5);
2353 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002354}
2355
françois romieu4da19632011-01-03 15:07:55 +00002356static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002357{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002358 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002359 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002360 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002361 { 0x03, 0x802f },
2362 { 0x02, 0x4f02 },
2363 { 0x01, 0x0409 },
2364 { 0x00, 0xf099 },
2365 { 0x04, 0x9800 },
2366 { 0x04, 0x9000 },
2367 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002368 { 0x1f, 0x0002 },
2369 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002370 { 0x06, 0x0761 },
2371 { 0x1f, 0x0003 },
2372 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002373 { 0x1f, 0x0000 }
2374 };
2375
françois romieu4da19632011-01-03 15:07:55 +00002376 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002377
françois romieu4da19632011-01-03 15:07:55 +00002378 rtl_patchphy(tp, 0x16, 1 << 0);
2379 rtl_patchphy(tp, 0x14, 1 << 5);
2380 rtl_patchphy(tp, 0x0d, 1 << 5);
2381 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002382}
2383
françois romieu4da19632011-01-03 15:07:55 +00002384static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002385{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002386 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002387 { 0x1f, 0x0001 },
2388 { 0x12, 0x2300 },
2389 { 0x1d, 0x3d98 },
2390 { 0x1f, 0x0002 },
2391 { 0x0c, 0x7eb8 },
2392 { 0x06, 0x5461 },
2393 { 0x1f, 0x0003 },
2394 { 0x16, 0x0f0a },
2395 { 0x1f, 0x0000 }
2396 };
2397
françois romieu4da19632011-01-03 15:07:55 +00002398 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002399
françois romieu4da19632011-01-03 15:07:55 +00002400 rtl_patchphy(tp, 0x16, 1 << 0);
2401 rtl_patchphy(tp, 0x14, 1 << 5);
2402 rtl_patchphy(tp, 0x0d, 1 << 5);
2403 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002404}
2405
françois romieu4da19632011-01-03 15:07:55 +00002406static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002407{
françois romieu4da19632011-01-03 15:07:55 +00002408 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002409}
2410
françois romieubca03d52011-01-03 15:07:31 +00002411static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002412{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002413 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002414 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002415 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002416 { 0x06, 0x4064 },
2417 { 0x07, 0x2863 },
2418 { 0x08, 0x059c },
2419 { 0x09, 0x26b4 },
2420 { 0x0a, 0x6a19 },
2421 { 0x0b, 0xdcc8 },
2422 { 0x10, 0xf06d },
2423 { 0x14, 0x7f68 },
2424 { 0x18, 0x7fd9 },
2425 { 0x1c, 0xf0ff },
2426 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002427 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002428 { 0x12, 0xf49f },
2429 { 0x13, 0x070b },
2430 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002431 { 0x14, 0x94c0 },
2432
2433 /*
2434 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002435 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002436 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002437 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002438 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002439 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002440 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002441 { 0x06, 0x5561 },
2442
2443 /*
2444 * Can not link to 1Gbps with bad cable
2445 * Decrease SNR threshold form 21.07dB to 19.04dB
2446 */
2447 { 0x1f, 0x0001 },
2448 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002449
2450 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002451 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002452 };
françois romieubca03d52011-01-03 15:07:31 +00002453 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002454
françois romieu4da19632011-01-03 15:07:55 +00002455 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002456
françois romieubca03d52011-01-03 15:07:31 +00002457 /*
2458 * Rx Error Issue
2459 * Fine Tune Switching regulator parameter
2460 */
françois romieu4da19632011-01-03 15:07:55 +00002461 rtl_writephy(tp, 0x1f, 0x0002);
2462 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2463 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002464
françois romieudaf9df62009-10-07 12:44:20 +00002465 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002466 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002467 { 0x1f, 0x0002 },
2468 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002469 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002470 { 0x05, 0x8330 },
2471 { 0x06, 0x669a },
2472 { 0x1f, 0x0002 }
2473 };
2474 int val;
2475
françois romieu4da19632011-01-03 15:07:55 +00002476 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002477
françois romieu4da19632011-01-03 15:07:55 +00002478 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002479
2480 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002481 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002482 0x0065, 0x0066, 0x0067, 0x0068,
2483 0x0069, 0x006a, 0x006b, 0x006c
2484 };
2485 int i;
2486
françois romieu4da19632011-01-03 15:07:55 +00002487 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002488
2489 val &= 0xff00;
2490 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002491 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002492 }
2493 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002494 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002495 { 0x1f, 0x0002 },
2496 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002497 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002498 { 0x05, 0x8330 },
2499 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002500 };
2501
françois romieu4da19632011-01-03 15:07:55 +00002502 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002503 }
2504
françois romieubca03d52011-01-03 15:07:31 +00002505 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002506 rtl_writephy(tp, 0x1f, 0x0002);
2507 rtl_patchphy(tp, 0x0d, 0x0300);
2508 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002509
françois romieubca03d52011-01-03 15:07:31 +00002510 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002511 rtl_writephy(tp, 0x1f, 0x0002);
2512 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2513 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002514
françois romieu4da19632011-01-03 15:07:55 +00002515 rtl_writephy(tp, 0x1f, 0x0005);
2516 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002517
2518 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002519
françois romieu4da19632011-01-03 15:07:55 +00002520 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002521}
2522
françois romieubca03d52011-01-03 15:07:31 +00002523static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002524{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002525 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002526 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002527 { 0x1f, 0x0001 },
2528 { 0x06, 0x4064 },
2529 { 0x07, 0x2863 },
2530 { 0x08, 0x059c },
2531 { 0x09, 0x26b4 },
2532 { 0x0a, 0x6a19 },
2533 { 0x0b, 0xdcc8 },
2534 { 0x10, 0xf06d },
2535 { 0x14, 0x7f68 },
2536 { 0x18, 0x7fd9 },
2537 { 0x1c, 0xf0ff },
2538 { 0x1d, 0x3d9c },
2539 { 0x1f, 0x0003 },
2540 { 0x12, 0xf49f },
2541 { 0x13, 0x070b },
2542 { 0x1a, 0x05ad },
2543 { 0x14, 0x94c0 },
2544
françois romieubca03d52011-01-03 15:07:31 +00002545 /*
2546 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002547 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002548 */
françois romieudaf9df62009-10-07 12:44:20 +00002549 { 0x1f, 0x0002 },
2550 { 0x06, 0x5561 },
2551 { 0x1f, 0x0005 },
2552 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002553 { 0x06, 0x5561 },
2554
2555 /*
2556 * Can not link to 1Gbps with bad cable
2557 * Decrease SNR threshold form 21.07dB to 19.04dB
2558 */
2559 { 0x1f, 0x0001 },
2560 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002561
2562 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002563 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002564 };
françois romieubca03d52011-01-03 15:07:31 +00002565 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002566
françois romieu4da19632011-01-03 15:07:55 +00002567 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002568
2569 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002570 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002571 { 0x1f, 0x0002 },
2572 { 0x05, 0x669a },
2573 { 0x1f, 0x0005 },
2574 { 0x05, 0x8330 },
2575 { 0x06, 0x669a },
2576
2577 { 0x1f, 0x0002 }
2578 };
2579 int val;
2580
françois romieu4da19632011-01-03 15:07:55 +00002581 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002582
françois romieu4da19632011-01-03 15:07:55 +00002583 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002584 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002585 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002586 0x0065, 0x0066, 0x0067, 0x0068,
2587 0x0069, 0x006a, 0x006b, 0x006c
2588 };
2589 int i;
2590
françois romieu4da19632011-01-03 15:07:55 +00002591 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002592
2593 val &= 0xff00;
2594 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002595 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002596 }
2597 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002598 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002599 { 0x1f, 0x0002 },
2600 { 0x05, 0x2642 },
2601 { 0x1f, 0x0005 },
2602 { 0x05, 0x8330 },
2603 { 0x06, 0x2642 }
2604 };
2605
françois romieu4da19632011-01-03 15:07:55 +00002606 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002607 }
2608
françois romieubca03d52011-01-03 15:07:31 +00002609 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002610 rtl_writephy(tp, 0x1f, 0x0002);
2611 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2612 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002613
françois romieubca03d52011-01-03 15:07:31 +00002614 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002615 rtl_writephy(tp, 0x1f, 0x0002);
2616 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002617
françois romieu4da19632011-01-03 15:07:55 +00002618 rtl_writephy(tp, 0x1f, 0x0005);
2619 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002620
2621 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002622
françois romieu4da19632011-01-03 15:07:55 +00002623 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002624}
2625
françois romieu4da19632011-01-03 15:07:55 +00002626static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002627{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002628 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002629 { 0x1f, 0x0002 },
2630 { 0x10, 0x0008 },
2631 { 0x0d, 0x006c },
2632
2633 { 0x1f, 0x0000 },
2634 { 0x0d, 0xf880 },
2635
2636 { 0x1f, 0x0001 },
2637 { 0x17, 0x0cc0 },
2638
2639 { 0x1f, 0x0001 },
2640 { 0x0b, 0xa4d8 },
2641 { 0x09, 0x281c },
2642 { 0x07, 0x2883 },
2643 { 0x0a, 0x6b35 },
2644 { 0x1d, 0x3da4 },
2645 { 0x1c, 0xeffd },
2646 { 0x14, 0x7f52 },
2647 { 0x18, 0x7fc6 },
2648 { 0x08, 0x0601 },
2649 { 0x06, 0x4063 },
2650 { 0x10, 0xf074 },
2651 { 0x1f, 0x0003 },
2652 { 0x13, 0x0789 },
2653 { 0x12, 0xf4bd },
2654 { 0x1a, 0x04fd },
2655 { 0x14, 0x84b0 },
2656 { 0x1f, 0x0000 },
2657 { 0x00, 0x9200 },
2658
2659 { 0x1f, 0x0005 },
2660 { 0x01, 0x0340 },
2661 { 0x1f, 0x0001 },
2662 { 0x04, 0x4000 },
2663 { 0x03, 0x1d21 },
2664 { 0x02, 0x0c32 },
2665 { 0x01, 0x0200 },
2666 { 0x00, 0x5554 },
2667 { 0x04, 0x4800 },
2668 { 0x04, 0x4000 },
2669 { 0x04, 0xf000 },
2670 { 0x03, 0xdf01 },
2671 { 0x02, 0xdf20 },
2672 { 0x01, 0x101a },
2673 { 0x00, 0xa0ff },
2674 { 0x04, 0xf800 },
2675 { 0x04, 0xf000 },
2676 { 0x1f, 0x0000 },
2677
2678 { 0x1f, 0x0007 },
2679 { 0x1e, 0x0023 },
2680 { 0x16, 0x0000 },
2681 { 0x1f, 0x0000 }
2682 };
2683
françois romieu4da19632011-01-03 15:07:55 +00002684 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002685}
2686
françois romieue6de30d2011-01-03 15:08:37 +00002687static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2688{
2689 static const struct phy_reg phy_reg_init[] = {
2690 { 0x1f, 0x0001 },
2691 { 0x17, 0x0cc0 },
2692
2693 { 0x1f, 0x0007 },
2694 { 0x1e, 0x002d },
2695 { 0x18, 0x0040 },
2696 { 0x1f, 0x0000 }
2697 };
2698
2699 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2700 rtl_patchphy(tp, 0x0d, 1 << 5);
2701}
2702
Hayes Wang70090422011-07-06 15:58:06 +08002703static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002704{
2705 static const struct phy_reg phy_reg_init[] = {
2706 /* Enable Delay cap */
2707 { 0x1f, 0x0005 },
2708 { 0x05, 0x8b80 },
2709 { 0x06, 0xc896 },
2710 { 0x1f, 0x0000 },
2711
2712 /* Channel estimation fine tune */
2713 { 0x1f, 0x0001 },
2714 { 0x0b, 0x6c20 },
2715 { 0x07, 0x2872 },
2716 { 0x1c, 0xefff },
2717 { 0x1f, 0x0003 },
2718 { 0x14, 0x6420 },
2719 { 0x1f, 0x0000 },
2720
2721 /* Update PFM & 10M TX idle timer */
2722 { 0x1f, 0x0007 },
2723 { 0x1e, 0x002f },
2724 { 0x15, 0x1919 },
2725 { 0x1f, 0x0000 },
2726
2727 { 0x1f, 0x0007 },
2728 { 0x1e, 0x00ac },
2729 { 0x18, 0x0006 },
2730 { 0x1f, 0x0000 }
2731 };
2732
Francois Romieu15ecd032011-04-27 13:52:22 -07002733 rtl_apply_firmware(tp);
2734
hayeswang01dc7fe2011-03-21 01:50:28 +00002735 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2736
2737 /* DCO enable for 10M IDLE Power */
2738 rtl_writephy(tp, 0x1f, 0x0007);
2739 rtl_writephy(tp, 0x1e, 0x0023);
2740 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2741 rtl_writephy(tp, 0x1f, 0x0000);
2742
2743 /* For impedance matching */
2744 rtl_writephy(tp, 0x1f, 0x0002);
2745 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002746 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002747
2748 /* PHY auto speed down */
2749 rtl_writephy(tp, 0x1f, 0x0007);
2750 rtl_writephy(tp, 0x1e, 0x002d);
2751 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2752 rtl_writephy(tp, 0x1f, 0x0000);
2753 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2754
2755 rtl_writephy(tp, 0x1f, 0x0005);
2756 rtl_writephy(tp, 0x05, 0x8b86);
2757 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2758 rtl_writephy(tp, 0x1f, 0x0000);
2759
2760 rtl_writephy(tp, 0x1f, 0x0005);
2761 rtl_writephy(tp, 0x05, 0x8b85);
2762 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2763 rtl_writephy(tp, 0x1f, 0x0007);
2764 rtl_writephy(tp, 0x1e, 0x0020);
2765 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2766 rtl_writephy(tp, 0x1f, 0x0006);
2767 rtl_writephy(tp, 0x00, 0x5a00);
2768 rtl_writephy(tp, 0x1f, 0x0000);
2769 rtl_writephy(tp, 0x0d, 0x0007);
2770 rtl_writephy(tp, 0x0e, 0x003c);
2771 rtl_writephy(tp, 0x0d, 0x4007);
2772 rtl_writephy(tp, 0x0e, 0x0000);
2773 rtl_writephy(tp, 0x0d, 0x0000);
2774}
2775
Hayes Wang70090422011-07-06 15:58:06 +08002776static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2777{
2778 static const struct phy_reg phy_reg_init[] = {
2779 /* Enable Delay cap */
2780 { 0x1f, 0x0004 },
2781 { 0x1f, 0x0007 },
2782 { 0x1e, 0x00ac },
2783 { 0x18, 0x0006 },
2784 { 0x1f, 0x0002 },
2785 { 0x1f, 0x0000 },
2786 { 0x1f, 0x0000 },
2787
2788 /* Channel estimation fine tune */
2789 { 0x1f, 0x0003 },
2790 { 0x09, 0xa20f },
2791 { 0x1f, 0x0000 },
2792 { 0x1f, 0x0000 },
2793
2794 /* Green Setting */
2795 { 0x1f, 0x0005 },
2796 { 0x05, 0x8b5b },
2797 { 0x06, 0x9222 },
2798 { 0x05, 0x8b6d },
2799 { 0x06, 0x8000 },
2800 { 0x05, 0x8b76 },
2801 { 0x06, 0x8000 },
2802 { 0x1f, 0x0000 }
2803 };
2804
2805 rtl_apply_firmware(tp);
2806
2807 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2808
2809 /* For 4-corner performance improve */
2810 rtl_writephy(tp, 0x1f, 0x0005);
2811 rtl_writephy(tp, 0x05, 0x8b80);
2812 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2813 rtl_writephy(tp, 0x1f, 0x0000);
2814
2815 /* PHY auto speed down */
2816 rtl_writephy(tp, 0x1f, 0x0004);
2817 rtl_writephy(tp, 0x1f, 0x0007);
2818 rtl_writephy(tp, 0x1e, 0x002d);
2819 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2820 rtl_writephy(tp, 0x1f, 0x0002);
2821 rtl_writephy(tp, 0x1f, 0x0000);
2822 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2823
2824 /* improve 10M EEE waveform */
2825 rtl_writephy(tp, 0x1f, 0x0005);
2826 rtl_writephy(tp, 0x05, 0x8b86);
2827 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2828 rtl_writephy(tp, 0x1f, 0x0000);
2829
2830 /* Improve 2-pair detection performance */
2831 rtl_writephy(tp, 0x1f, 0x0005);
2832 rtl_writephy(tp, 0x05, 0x8b85);
2833 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2834 rtl_writephy(tp, 0x1f, 0x0000);
2835
2836 /* EEE setting */
2837 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2838 ERIAR_EXGMAC);
2839 rtl_writephy(tp, 0x1f, 0x0005);
2840 rtl_writephy(tp, 0x05, 0x8b85);
2841 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2842 rtl_writephy(tp, 0x1f, 0x0004);
2843 rtl_writephy(tp, 0x1f, 0x0007);
2844 rtl_writephy(tp, 0x1e, 0x0020);
2845 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
2846 rtl_writephy(tp, 0x1f, 0x0002);
2847 rtl_writephy(tp, 0x1f, 0x0000);
2848 rtl_writephy(tp, 0x0d, 0x0007);
2849 rtl_writephy(tp, 0x0e, 0x003c);
2850 rtl_writephy(tp, 0x0d, 0x4007);
2851 rtl_writephy(tp, 0x0e, 0x0000);
2852 rtl_writephy(tp, 0x0d, 0x0000);
2853
2854 /* Green feature */
2855 rtl_writephy(tp, 0x1f, 0x0003);
2856 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2857 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2858 rtl_writephy(tp, 0x1f, 0x0000);
2859}
2860
françois romieu4da19632011-01-03 15:07:55 +00002861static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002862{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002863 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002864 { 0x1f, 0x0003 },
2865 { 0x08, 0x441d },
2866 { 0x01, 0x9100 },
2867 { 0x1f, 0x0000 }
2868 };
2869
françois romieu4da19632011-01-03 15:07:55 +00002870 rtl_writephy(tp, 0x1f, 0x0000);
2871 rtl_patchphy(tp, 0x11, 1 << 12);
2872 rtl_patchphy(tp, 0x19, 1 << 13);
2873 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002874
françois romieu4da19632011-01-03 15:07:55 +00002875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002876}
2877
Hayes Wang5a5e4442011-02-22 17:26:21 +08002878static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2879{
2880 static const struct phy_reg phy_reg_init[] = {
2881 { 0x1f, 0x0005 },
2882 { 0x1a, 0x0000 },
2883 { 0x1f, 0x0000 },
2884
2885 { 0x1f, 0x0004 },
2886 { 0x1c, 0x0000 },
2887 { 0x1f, 0x0000 },
2888
2889 { 0x1f, 0x0001 },
2890 { 0x15, 0x7701 },
2891 { 0x1f, 0x0000 }
2892 };
2893
2894 /* Disable ALDPS before ram code */
2895 rtl_writephy(tp, 0x1f, 0x0000);
2896 rtl_writephy(tp, 0x18, 0x0310);
2897 msleep(100);
2898
François Romieu953a12c2011-04-24 17:38:48 +02002899 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002900
2901 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2902}
2903
Francois Romieu5615d9f2007-08-17 17:50:46 +02002904static void rtl_hw_phy_config(struct net_device *dev)
2905{
2906 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002907
2908 rtl8169_print_mac_version(tp);
2909
2910 switch (tp->mac_version) {
2911 case RTL_GIGA_MAC_VER_01:
2912 break;
2913 case RTL_GIGA_MAC_VER_02:
2914 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002915 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002916 break;
2917 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002918 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002919 break;
françois romieu2e9558562009-08-10 19:44:19 +00002920 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002921 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002922 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002923 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002924 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002925 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002926 case RTL_GIGA_MAC_VER_07:
2927 case RTL_GIGA_MAC_VER_08:
2928 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002929 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002930 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002931 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002932 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002933 break;
2934 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002935 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002936 break;
2937 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002938 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002939 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002940 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002941 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002942 break;
2943 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002944 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002945 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002946 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002947 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002948 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002949 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002950 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002951 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002952 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002953 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002954 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002955 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002956 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002957 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002958 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002959 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002960 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002961 break;
2962 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002963 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002964 break;
2965 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002966 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002967 break;
françois romieue6de30d2011-01-03 15:08:37 +00002968 case RTL_GIGA_MAC_VER_28:
2969 rtl8168d_4_hw_phy_config(tp);
2970 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002971 case RTL_GIGA_MAC_VER_29:
2972 case RTL_GIGA_MAC_VER_30:
2973 rtl8105e_hw_phy_config(tp);
2974 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002975 case RTL_GIGA_MAC_VER_31:
2976 /* None. */
2977 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002978 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002979 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08002980 rtl8168e_1_hw_phy_config(tp);
2981 break;
2982 case RTL_GIGA_MAC_VER_34:
2983 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002984 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002985
Francois Romieu5615d9f2007-08-17 17:50:46 +02002986 default:
2987 break;
2988 }
2989}
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991static void rtl8169_phy_timer(unsigned long __opaque)
2992{
2993 struct net_device *dev = (struct net_device *)__opaque;
2994 struct rtl8169_private *tp = netdev_priv(dev);
2995 struct timer_list *timer = &tp->timer;
2996 void __iomem *ioaddr = tp->mmio_addr;
2997 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2998
Francois Romieubcf0bf92006-07-26 23:14:13 +02002999 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 spin_lock_irq(&tp->lock);
3002
françois romieu4da19632011-01-03 15:07:55 +00003003 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003004 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 * A busy loop could burn quite a few cycles on nowadays CPU.
3006 * Let's delay the execution of the timer for a few ticks.
3007 */
3008 timeout = HZ/10;
3009 goto out_mod_timer;
3010 }
3011
3012 if (tp->link_ok(ioaddr))
3013 goto out_unlock;
3014
Joe Perchesbf82c182010-02-09 11:49:50 +00003015 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
françois romieu4da19632011-01-03 15:07:55 +00003017 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019out_mod_timer:
3020 mod_timer(timer, jiffies + timeout);
3021out_unlock:
3022 spin_unlock_irq(&tp->lock);
3023}
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025#ifdef CONFIG_NET_POLL_CONTROLLER
3026/*
3027 * Polling 'interrupt' - used by things like netconsole to send skbs
3028 * without having to re-enable interrupts. It's not called while
3029 * the interrupt routine is executing.
3030 */
3031static void rtl8169_netpoll(struct net_device *dev)
3032{
3033 struct rtl8169_private *tp = netdev_priv(dev);
3034 struct pci_dev *pdev = tp->pci_dev;
3035
3036 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003037 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 enable_irq(pdev->irq);
3039}
3040#endif
3041
3042static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3043 void __iomem *ioaddr)
3044{
3045 iounmap(ioaddr);
3046 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003047 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 pci_disable_device(pdev);
3049 free_netdev(dev);
3050}
3051
Francois Romieubf793292006-11-01 00:53:05 +01003052static void rtl8169_phy_reset(struct net_device *dev,
3053 struct rtl8169_private *tp)
3054{
Francois Romieu07d3f512007-02-21 22:40:46 +01003055 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003056
françois romieu4da19632011-01-03 15:07:55 +00003057 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003058 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003059 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003060 return;
3061 msleep(1);
3062 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003063 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003064}
3065
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003066static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003068 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003069
Francois Romieu5615d9f2007-08-17 17:50:46 +02003070 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003071
Marcus Sundberg773328942008-07-10 21:28:08 +02003072 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3073 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3074 RTL_W8(0x82, 0x01);
3075 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003076
Francois Romieu6dccd162007-02-13 23:38:05 +01003077 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3078
3079 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3080 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003081
Francois Romieubcf0bf92006-07-26 23:14:13 +02003082 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003083 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3084 RTL_W8(0x82, 0x01);
3085 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003086 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003087 }
3088
Francois Romieubf793292006-11-01 00:53:05 +01003089 rtl8169_phy_reset(dev, tp);
3090
Oliver Neukum54405cd2011-01-06 21:55:13 +01003091 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003092 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3093 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3094 (tp->mii.supports_gmii ?
3095 ADVERTISED_1000baseT_Half |
3096 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003097
Joe Perchesbf82c182010-02-09 11:49:50 +00003098 if (RTL_R8(PHYstatus) & TBI_Enable)
3099 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003100}
3101
Francois Romieu773d2022007-01-31 23:47:43 +01003102static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3103{
3104 void __iomem *ioaddr = tp->mmio_addr;
3105 u32 high;
3106 u32 low;
3107
3108 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3109 high = addr[4] | (addr[5] << 8);
3110
3111 spin_lock_irq(&tp->lock);
3112
3113 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003114
Francois Romieu773d2022007-01-31 23:47:43 +01003115 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003116 RTL_R32(MAC4);
3117
Francois Romieu78f1cd02010-03-27 19:35:46 -07003118 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003119 RTL_R32(MAC0);
3120
Francois Romieu773d2022007-01-31 23:47:43 +01003121 RTL_W8(Cfg9346, Cfg9346_Lock);
3122
3123 spin_unlock_irq(&tp->lock);
3124}
3125
3126static int rtl_set_mac_address(struct net_device *dev, void *p)
3127{
3128 struct rtl8169_private *tp = netdev_priv(dev);
3129 struct sockaddr *addr = p;
3130
3131 if (!is_valid_ether_addr(addr->sa_data))
3132 return -EADDRNOTAVAIL;
3133
3134 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3135
3136 rtl_rar_set(tp, dev->dev_addr);
3137
3138 return 0;
3139}
3140
Francois Romieu5f787a12006-08-17 13:02:36 +02003141static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3142{
3143 struct rtl8169_private *tp = netdev_priv(dev);
3144 struct mii_ioctl_data *data = if_mii(ifr);
3145
Francois Romieu8b4ab282008-11-19 22:05:25 -08003146 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3147}
Francois Romieu5f787a12006-08-17 13:02:36 +02003148
Francois Romieucecb5fd2011-04-01 10:21:07 +02003149static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3150 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003151{
Francois Romieu5f787a12006-08-17 13:02:36 +02003152 switch (cmd) {
3153 case SIOCGMIIPHY:
3154 data->phy_id = 32; /* Internal PHY */
3155 return 0;
3156
3157 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003158 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003159 return 0;
3160
3161 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003162 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003163 return 0;
3164 }
3165 return -EOPNOTSUPP;
3166}
3167
Francois Romieu8b4ab282008-11-19 22:05:25 -08003168static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3169{
3170 return -EOPNOTSUPP;
3171}
3172
Francois Romieu0e485152007-02-20 00:00:26 +01003173static const struct rtl_cfg_info {
3174 void (*hw_start)(struct net_device *);
3175 unsigned int region;
3176 unsigned int align;
3177 u16 intr_event;
3178 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003179 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003180 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003181} rtl_cfg_infos [] = {
3182 [RTL_CFG_0] = {
3183 .hw_start = rtl_hw_start_8169,
3184 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003185 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003186 .intr_event = SYSErr | LinkChg | RxOverflow |
3187 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003188 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003189 .features = RTL_FEATURE_GMII,
3190 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003191 },
3192 [RTL_CFG_1] = {
3193 .hw_start = rtl_hw_start_8168,
3194 .region = 2,
3195 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003196 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003197 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003198 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003199 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3200 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003201 },
3202 [RTL_CFG_2] = {
3203 .hw_start = rtl_hw_start_8101,
3204 .region = 2,
3205 .align = 8,
3206 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3207 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003208 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003209 .features = RTL_FEATURE_MSI,
3210 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003211 }
3212};
3213
Francois Romieufbac58f2007-10-04 22:51:38 +02003214/* Cfg9346_Unlock assumed. */
3215static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3216 const struct rtl_cfg_info *cfg)
3217{
3218 unsigned msi = 0;
3219 u8 cfg2;
3220
3221 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003222 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003223 if (pci_enable_msi(pdev)) {
3224 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3225 } else {
3226 cfg2 |= MSIEnable;
3227 msi = RTL_FEATURE_MSI;
3228 }
3229 }
3230 RTL_W8(Config2, cfg2);
3231 return msi;
3232}
3233
3234static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3235{
3236 if (tp->features & RTL_FEATURE_MSI) {
3237 pci_disable_msi(pdev);
3238 tp->features &= ~RTL_FEATURE_MSI;
3239 }
3240}
3241
Francois Romieu8b4ab282008-11-19 22:05:25 -08003242static const struct net_device_ops rtl8169_netdev_ops = {
3243 .ndo_open = rtl8169_open,
3244 .ndo_stop = rtl8169_close,
3245 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003246 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003247 .ndo_tx_timeout = rtl8169_tx_timeout,
3248 .ndo_validate_addr = eth_validate_addr,
3249 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003250 .ndo_fix_features = rtl8169_fix_features,
3251 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003252 .ndo_set_mac_address = rtl_set_mac_address,
3253 .ndo_do_ioctl = rtl8169_ioctl,
3254 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003255#ifdef CONFIG_NET_POLL_CONTROLLER
3256 .ndo_poll_controller = rtl8169_netpoll,
3257#endif
3258
3259};
3260
françois romieuc0e45c12011-01-03 15:08:04 +00003261static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3262{
3263 struct mdio_ops *ops = &tp->mdio_ops;
3264
3265 switch (tp->mac_version) {
3266 case RTL_GIGA_MAC_VER_27:
3267 ops->write = r8168dp_1_mdio_write;
3268 ops->read = r8168dp_1_mdio_read;
3269 break;
françois romieue6de30d2011-01-03 15:08:37 +00003270 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003271 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003272 ops->write = r8168dp_2_mdio_write;
3273 ops->read = r8168dp_2_mdio_read;
3274 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003275 default:
3276 ops->write = r8169_mdio_write;
3277 ops->read = r8169_mdio_read;
3278 break;
3279 }
3280}
3281
françois romieu065c27c2011-01-03 15:08:12 +00003282static void r810x_phy_power_down(struct rtl8169_private *tp)
3283{
3284 rtl_writephy(tp, 0x1f, 0x0000);
3285 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3286}
3287
3288static void r810x_phy_power_up(struct rtl8169_private *tp)
3289{
3290 rtl_writephy(tp, 0x1f, 0x0000);
3291 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3292}
3293
3294static void r810x_pll_power_down(struct rtl8169_private *tp)
3295{
3296 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3297 rtl_writephy(tp, 0x1f, 0x0000);
3298 rtl_writephy(tp, MII_BMCR, 0x0000);
3299 return;
3300 }
3301
3302 r810x_phy_power_down(tp);
3303}
3304
3305static void r810x_pll_power_up(struct rtl8169_private *tp)
3306{
3307 r810x_phy_power_up(tp);
3308}
3309
3310static void r8168_phy_power_up(struct rtl8169_private *tp)
3311{
3312 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003313 switch (tp->mac_version) {
3314 case RTL_GIGA_MAC_VER_11:
3315 case RTL_GIGA_MAC_VER_12:
3316 case RTL_GIGA_MAC_VER_17:
3317 case RTL_GIGA_MAC_VER_18:
3318 case RTL_GIGA_MAC_VER_19:
3319 case RTL_GIGA_MAC_VER_20:
3320 case RTL_GIGA_MAC_VER_21:
3321 case RTL_GIGA_MAC_VER_22:
3322 case RTL_GIGA_MAC_VER_23:
3323 case RTL_GIGA_MAC_VER_24:
3324 case RTL_GIGA_MAC_VER_25:
3325 case RTL_GIGA_MAC_VER_26:
3326 case RTL_GIGA_MAC_VER_27:
3327 case RTL_GIGA_MAC_VER_28:
3328 case RTL_GIGA_MAC_VER_31:
3329 rtl_writephy(tp, 0x0e, 0x0000);
3330 break;
3331 default:
3332 break;
3333 }
françois romieu065c27c2011-01-03 15:08:12 +00003334 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3335}
3336
3337static void r8168_phy_power_down(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_32:
3342 case RTL_GIGA_MAC_VER_33:
3343 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3344 break;
3345
3346 case RTL_GIGA_MAC_VER_11:
3347 case RTL_GIGA_MAC_VER_12:
3348 case RTL_GIGA_MAC_VER_17:
3349 case RTL_GIGA_MAC_VER_18:
3350 case RTL_GIGA_MAC_VER_19:
3351 case RTL_GIGA_MAC_VER_20:
3352 case RTL_GIGA_MAC_VER_21:
3353 case RTL_GIGA_MAC_VER_22:
3354 case RTL_GIGA_MAC_VER_23:
3355 case RTL_GIGA_MAC_VER_24:
3356 case RTL_GIGA_MAC_VER_25:
3357 case RTL_GIGA_MAC_VER_26:
3358 case RTL_GIGA_MAC_VER_27:
3359 case RTL_GIGA_MAC_VER_28:
3360 case RTL_GIGA_MAC_VER_31:
3361 rtl_writephy(tp, 0x0e, 0x0200);
3362 default:
3363 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3364 break;
3365 }
françois romieu065c27c2011-01-03 15:08:12 +00003366}
3367
3368static void r8168_pll_power_down(struct rtl8169_private *tp)
3369{
3370 void __iomem *ioaddr = tp->mmio_addr;
3371
Francois Romieucecb5fd2011-04-01 10:21:07 +02003372 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3373 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3374 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003375 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003376 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003377 }
françois romieu065c27c2011-01-03 15:08:12 +00003378
Francois Romieucecb5fd2011-04-01 10:21:07 +02003379 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3380 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003381 (RTL_R16(CPlusCmd) & ASF)) {
3382 return;
3383 }
3384
hayeswang01dc7fe2011-03-21 01:50:28 +00003385 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3386 tp->mac_version == RTL_GIGA_MAC_VER_33)
3387 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3388
françois romieu065c27c2011-01-03 15:08:12 +00003389 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3390 rtl_writephy(tp, 0x1f, 0x0000);
3391 rtl_writephy(tp, MII_BMCR, 0x0000);
3392
Hayes Wangd4ed95d2011-07-06 15:58:07 +08003393 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3394 tp->mac_version == RTL_GIGA_MAC_VER_33)
3395 RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
3396 AcceptMulticast | AcceptMyPhys);
françois romieu065c27c2011-01-03 15:08:12 +00003397 return;
3398 }
3399
3400 r8168_phy_power_down(tp);
3401
3402 switch (tp->mac_version) {
3403 case RTL_GIGA_MAC_VER_25:
3404 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003405 case RTL_GIGA_MAC_VER_27:
3406 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003407 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003408 case RTL_GIGA_MAC_VER_32:
3409 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003410 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3411 break;
3412 }
3413}
3414
3415static void r8168_pll_power_up(struct rtl8169_private *tp)
3416{
3417 void __iomem *ioaddr = tp->mmio_addr;
3418
Francois Romieucecb5fd2011-04-01 10:21:07 +02003419 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3420 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3421 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003422 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003423 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003424 }
françois romieu065c27c2011-01-03 15:08:12 +00003425
3426 switch (tp->mac_version) {
3427 case RTL_GIGA_MAC_VER_25:
3428 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003429 case RTL_GIGA_MAC_VER_27:
3430 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003431 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003432 case RTL_GIGA_MAC_VER_32:
3433 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003434 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3435 break;
3436 }
3437
3438 r8168_phy_power_up(tp);
3439}
3440
3441static void rtl_pll_power_op(struct rtl8169_private *tp,
3442 void (*op)(struct rtl8169_private *))
3443{
3444 if (op)
3445 op(tp);
3446}
3447
3448static void rtl_pll_power_down(struct rtl8169_private *tp)
3449{
3450 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3451}
3452
3453static void rtl_pll_power_up(struct rtl8169_private *tp)
3454{
3455 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3456}
3457
3458static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3459{
3460 struct pll_power_ops *ops = &tp->pll_power_ops;
3461
3462 switch (tp->mac_version) {
3463 case RTL_GIGA_MAC_VER_07:
3464 case RTL_GIGA_MAC_VER_08:
3465 case RTL_GIGA_MAC_VER_09:
3466 case RTL_GIGA_MAC_VER_10:
3467 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003468 case RTL_GIGA_MAC_VER_29:
3469 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003470 ops->down = r810x_pll_power_down;
3471 ops->up = r810x_pll_power_up;
3472 break;
3473
3474 case RTL_GIGA_MAC_VER_11:
3475 case RTL_GIGA_MAC_VER_12:
3476 case RTL_GIGA_MAC_VER_17:
3477 case RTL_GIGA_MAC_VER_18:
3478 case RTL_GIGA_MAC_VER_19:
3479 case RTL_GIGA_MAC_VER_20:
3480 case RTL_GIGA_MAC_VER_21:
3481 case RTL_GIGA_MAC_VER_22:
3482 case RTL_GIGA_MAC_VER_23:
3483 case RTL_GIGA_MAC_VER_24:
3484 case RTL_GIGA_MAC_VER_25:
3485 case RTL_GIGA_MAC_VER_26:
3486 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003487 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003488 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003489 case RTL_GIGA_MAC_VER_32:
3490 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003491 case RTL_GIGA_MAC_VER_34:
françois romieu065c27c2011-01-03 15:08:12 +00003492 ops->down = r8168_pll_power_down;
3493 ops->up = r8168_pll_power_up;
3494 break;
3495
3496 default:
3497 ops->down = NULL;
3498 ops->up = NULL;
3499 break;
3500 }
3501}
3502
Hayes Wange542a222011-07-06 15:58:04 +08003503static void rtl_init_rxcfg(struct rtl8169_private *tp)
3504{
3505 void __iomem *ioaddr = tp->mmio_addr;
3506
3507 switch (tp->mac_version) {
3508 case RTL_GIGA_MAC_VER_01:
3509 case RTL_GIGA_MAC_VER_02:
3510 case RTL_GIGA_MAC_VER_03:
3511 case RTL_GIGA_MAC_VER_04:
3512 case RTL_GIGA_MAC_VER_05:
3513 case RTL_GIGA_MAC_VER_06:
3514 case RTL_GIGA_MAC_VER_10:
3515 case RTL_GIGA_MAC_VER_11:
3516 case RTL_GIGA_MAC_VER_12:
3517 case RTL_GIGA_MAC_VER_13:
3518 case RTL_GIGA_MAC_VER_14:
3519 case RTL_GIGA_MAC_VER_15:
3520 case RTL_GIGA_MAC_VER_16:
3521 case RTL_GIGA_MAC_VER_17:
3522 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3523 break;
3524 case RTL_GIGA_MAC_VER_18:
3525 case RTL_GIGA_MAC_VER_19:
3526 case RTL_GIGA_MAC_VER_20:
3527 case RTL_GIGA_MAC_VER_21:
3528 case RTL_GIGA_MAC_VER_22:
3529 case RTL_GIGA_MAC_VER_23:
3530 case RTL_GIGA_MAC_VER_24:
3531 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3532 break;
3533 default:
3534 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3535 break;
3536 }
3537}
3538
Hayes Wang92fc43b2011-07-06 15:58:03 +08003539static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3540{
3541 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3542}
3543
Francois Romieu6f43adc2011-04-29 15:05:51 +02003544static void rtl_hw_reset(struct rtl8169_private *tp)
3545{
3546 void __iomem *ioaddr = tp->mmio_addr;
3547 int i;
3548
3549 /* Soft reset the chip. */
3550 RTL_W8(ChipCmd, CmdReset);
3551
3552 /* Check that the chip has finished the reset. */
3553 for (i = 0; i < 100; i++) {
3554 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3555 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003556 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003557 }
Hayes Wang92fc43b2011-07-06 15:58:03 +08003558
3559 rtl8169_init_ring_indexes(tp);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003560}
3561
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003562static int __devinit
3563rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3564{
Francois Romieu0e485152007-02-20 00:00:26 +01003565 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3566 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003568 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003569 struct net_device *dev;
3570 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003571 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003572 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003574 if (netif_msg_drv(&debug)) {
3575 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3576 MODULENAME, RTL8169_VERSION);
3577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003580 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003581 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003582 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003583 rc = -ENOMEM;
3584 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
3586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003588 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003590 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003591 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003592 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Francois Romieuccdffb92008-07-26 14:26:06 +02003594 mii = &tp->mii;
3595 mii->dev = dev;
3596 mii->mdio_read = rtl_mdio_read;
3597 mii->mdio_write = rtl_mdio_write;
3598 mii->phy_id_mask = 0x1f;
3599 mii->reg_num_mask = 0x1f;
3600 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3601
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003602 /* disable ASPM completely as that cause random device stop working
3603 * problems as well as full system hangs for some PCIe devices users */
3604 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3605 PCIE_LINK_STATE_CLKPM);
3606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3608 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003609 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003610 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003611 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 }
3613
françois romieu87aeec72010-04-26 11:42:06 +00003614 if (pci_set_mwi(pdev) < 0)
3615 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003618 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003619 netif_err(tp, probe, dev,
3620 "region #%d not an MMIO resource, aborting\n",
3621 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003623 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003625
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003627 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003628 netif_err(tp, probe, dev,
3629 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003631 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 }
3633
3634 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003635 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003636 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003637 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 }
3639
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003640 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
3642 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003643 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 tp->cp_cmd |= PCIDAC;
3645 dev->features |= NETIF_F_HIGHDMA;
3646 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003647 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003649 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003650 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 }
3652 }
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003655 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003656 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003657 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003659 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003661 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Jon Masone44daad2011-06-27 07:46:31 +00003663 if (!pci_is_pcie(pdev))
3664 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07003665
Hayes Wange542a222011-07-06 15:58:04 +08003666 /* Identify chip attached to board */
3667 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
3668
3669 rtl_init_rxcfg(tp);
3670
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003671 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Francois Romieu6f43adc2011-04-29 15:05:51 +02003673 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003675 RTL_W16(IntrStatus, 0xffff);
3676
françois romieuca52efd2009-07-24 12:34:19 +00003677 pci_set_master(pdev);
3678
Francois Romieu7a8fc772011-03-01 17:18:33 +01003679 /*
3680 * Pretend we are using VLANs; This bypasses a nasty bug where
3681 * Interrupts stop flowing on high load on 8110SCd controllers.
3682 */
3683 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3684 tp->cp_cmd |= RxVlan;
3685
françois romieuc0e45c12011-01-03 15:08:04 +00003686 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003687 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
Francois Romieu85bffe62011-04-27 08:22:39 +02003691 chipset = tp->mac_version;
3692 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Francois Romieu5d06a992006-02-23 00:47:58 +01003694 RTL_W8(Cfg9346, Cfg9346_Unlock);
3695 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3696 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003697 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3698 tp->features |= RTL_FEATURE_WOL;
3699 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3700 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003701 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003702 RTL_W8(Cfg9346, Cfg9346_Lock);
3703
Francois Romieu66ec5d42007-11-06 22:56:10 +01003704 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3705 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 tp->set_speed = rtl8169_set_speed_tbi;
3707 tp->get_settings = rtl8169_gset_tbi;
3708 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3709 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3710 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003711 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 } else {
3713 tp->set_speed = rtl8169_set_speed_xmii;
3714 tp->get_settings = rtl8169_gset_xmii;
3715 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3716 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3717 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003718 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 }
3720
Francois Romieudf58ef52008-10-09 14:35:58 -07003721 spin_lock_init(&tp->lock);
3722
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003723 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 for (i = 0; i < MAC_ADDR_LEN; i++)
3725 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003726 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3730 dev->irq = pdev->irq;
3731 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003733 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Michał Mirosław350fb322011-04-08 06:35:56 +00003735 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3736 * properly for all devices */
3737 dev->features |= NETIF_F_RXCSUM |
3738 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3739
3740 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3741 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3742 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3743 NETIF_F_HIGHDMA;
3744
3745 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3746 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3747 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
3749 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003750 tp->hw_start = cfg->hw_start;
3751 tp->intr_event = cfg->intr_event;
3752 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Francois Romieu2efa53f2007-03-09 00:00:05 +01003754 init_timer(&tp->timer);
3755 tp->timer.data = (unsigned long) dev;
3756 tp->timer.function = rtl8169_phy_timer;
3757
Francois Romieub6ffd972011-06-17 17:00:05 +02003758 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003761 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003762 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
3764 pci_set_drvdata(pdev, dev);
3765
Joe Perchesbf82c182010-02-09 11:49:50 +00003766 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003767 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003768 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Francois Romieucecb5fd2011-04-01 10:21:07 +02003770 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3771 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3772 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003773 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003774 }
françois romieub646d902011-01-03 15:08:21 +00003775
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003776 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Alan Sternf3ec4f82010-06-08 15:23:51 -04003778 if (pci_dev_run_wake(pdev))
3779 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003780
Ivan Vecera0d672e92011-02-15 02:08:39 +00003781 netif_carrier_off(dev);
3782
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003783out:
3784 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
françois romieu87aeec72010-04-26 11:42:06 +00003786err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003787 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003788 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003789err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003790 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003791err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003792 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003793 pci_disable_device(pdev);
3794err_out_free_dev_1:
3795 free_netdev(dev);
3796 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
Francois Romieu07d3f512007-02-21 22:40:46 +01003799static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
3801 struct net_device *dev = pci_get_drvdata(pdev);
3802 struct rtl8169_private *tp = netdev_priv(dev);
3803
Francois Romieucecb5fd2011-04-01 10:21:07 +02003804 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3805 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3806 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003807 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003808 }
françois romieub646d902011-01-03 15:08:21 +00003809
Tejun Heo23f333a2010-12-12 16:45:14 +01003810 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003813
François Romieu953a12c2011-04-24 17:38:48 +02003814 rtl_release_firmware(tp);
3815
Alan Sternf3ec4f82010-06-08 15:23:51 -04003816 if (pci_dev_run_wake(pdev))
3817 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003818
Ivan Veceracc098dc2009-11-29 23:12:52 -08003819 /* restore original MAC address */
3820 rtl_rar_set(tp, dev->perm_addr);
3821
Francois Romieufbac58f2007-10-04 22:51:38 +02003822 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3824 pci_set_drvdata(pdev, NULL);
3825}
3826
Francois Romieub6ffd972011-06-17 17:00:05 +02003827static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3828{
3829 struct rtl_fw *rtl_fw;
3830 const char *name;
3831 int rc = -ENOMEM;
3832
3833 name = rtl_lookup_firmware_name(tp);
3834 if (!name)
3835 goto out_no_firmware;
3836
3837 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3838 if (!rtl_fw)
3839 goto err_warn;
3840
3841 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3842 if (rc < 0)
3843 goto err_free;
3844
Francois Romieufd112f22011-06-18 00:10:29 +02003845 rc = rtl_check_firmware(tp, rtl_fw);
3846 if (rc < 0)
3847 goto err_release_firmware;
3848
Francois Romieub6ffd972011-06-17 17:00:05 +02003849 tp->rtl_fw = rtl_fw;
3850out:
3851 return;
3852
Francois Romieufd112f22011-06-18 00:10:29 +02003853err_release_firmware:
3854 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003855err_free:
3856 kfree(rtl_fw);
3857err_warn:
3858 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3859 name, rc);
3860out_no_firmware:
3861 tp->rtl_fw = NULL;
3862 goto out;
3863}
3864
François Romieu953a12c2011-04-24 17:38:48 +02003865static void rtl_request_firmware(struct rtl8169_private *tp)
3866{
Francois Romieub6ffd972011-06-17 17:00:05 +02003867 if (IS_ERR(tp->rtl_fw))
3868 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003869}
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871static int rtl8169_open(struct net_device *dev)
3872{
3873 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003874 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003876 int retval = -ENOMEM;
3877
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003878 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Neil Hormanc0cd8842010-03-29 13:16:02 -07003880 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003882 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003884 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3885 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003887 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003889 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3890 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003892 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
3894 retval = rtl8169_init_ring(dev);
3895 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003896 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
David Howellsc4028952006-11-22 14:57:56 +00003898 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Francois Romieu99f252b2007-04-02 22:59:59 +02003900 smp_mb();
3901
François Romieu953a12c2011-04-24 17:38:48 +02003902 rtl_request_firmware(tp);
3903
Francois Romieufbac58f2007-10-04 22:51:38 +02003904 retval = request_irq(dev->irq, rtl8169_interrupt,
3905 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003906 dev->name, dev);
3907 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003908 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003909
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003910 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003911
françois romieueee3a962011-01-08 02:17:26 +00003912 rtl8169_init_phy(dev, tp);
3913
Michał Mirosław350fb322011-04-08 06:35:56 +00003914 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003915
françois romieu065c27c2011-01-03 15:08:12 +00003916 rtl_pll_power_up(tp);
3917
Francois Romieu07ce4062007-02-23 23:36:39 +01003918 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003920 tp->saved_wolopts = 0;
3921 pm_runtime_put_noidle(&pdev->dev);
3922
françois romieueee3a962011-01-08 02:17:26 +00003923 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924out:
3925 return retval;
3926
François Romieu953a12c2011-04-24 17:38:48 +02003927err_release_fw_2:
3928 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003929 rtl8169_rx_clear(tp);
3930err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003931 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3932 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003933 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003934err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003935 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3936 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003937 tp->TxDescArray = NULL;
3938err_pm_runtime_put:
3939 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 goto out;
3941}
3942
Hayes Wang92fc43b2011-07-06 15:58:03 +08003943static void rtl_rx_close(struct rtl8169_private *tp)
3944{
3945 void __iomem *ioaddr = tp->mmio_addr;
3946 u32 rxcfg = RTL_R32(RxConfig);
3947
3948 rxcfg &= ~(AcceptErr | AcceptRunt | AcceptBroadcast | AcceptMulticast |
3949 AcceptMyPhys | AcceptAllPhys);
3950 RTL_W32(RxConfig, rxcfg);
3951}
3952
françois romieue6de30d2011-01-03 15:08:37 +00003953static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
françois romieue6de30d2011-01-03 15:08:37 +00003955 void __iomem *ioaddr = tp->mmio_addr;
3956
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 /* Disable interrupts */
3958 rtl8169_irq_mask_and_ack(ioaddr);
3959
Hayes Wang92fc43b2011-07-06 15:58:03 +08003960 rtl_rx_close(tp);
3961
Hayes Wang5d2e1952011-02-22 17:26:22 +08003962 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003963 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3964 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003965 while (RTL_R8(TxPoll) & NPQ)
3966 udelay(20);
Hayes Wang70090422011-07-06 15:58:06 +08003967 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3968 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
3969 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003970 } else {
3971 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
3972 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00003973 }
3974
Hayes Wang92fc43b2011-07-06 15:58:03 +08003975 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976}
3977
Francois Romieu7f796d82007-06-11 23:04:41 +02003978static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003979{
3980 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01003981
3982 /* Set DMA burst size and Interframe Gap Time */
3983 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3984 (InterFrameGap << TxInterFrameGapShift));
3985}
3986
Francois Romieu07ce4062007-02-23 23:36:39 +01003987static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
3989 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Francois Romieu07ce4062007-02-23 23:36:39 +01003991 tp->hw_start(dev);
3992
Francois Romieu07ce4062007-02-23 23:36:39 +01003993 netif_start_queue(dev);
3994}
3995
Francois Romieu7f796d82007-06-11 23:04:41 +02003996static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3997 void __iomem *ioaddr)
3998{
3999 /*
4000 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4001 * register to be written before TxDescAddrLow to work.
4002 * Switching from MMIO to I/O access fixes the issue as well.
4003 */
4004 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004005 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02004006 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004007 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02004008}
4009
4010static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4011{
4012 u16 cmd;
4013
4014 cmd = RTL_R16(CPlusCmd);
4015 RTL_W16(CPlusCmd, cmd);
4016 return cmd;
4017}
4018
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004019static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02004020{
4021 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00004022 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02004023}
4024
Francois Romieu6dccd162007-02-13 23:38:05 +01004025static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4026{
Francois Romieu37441002011-06-17 22:58:54 +02004027 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004028 u32 mac_version;
4029 u32 clk;
4030 u32 val;
4031 } cfg2_info [] = {
4032 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4033 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4034 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4035 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004036 };
4037 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004038 unsigned int i;
4039 u32 clk;
4040
4041 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004042 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004043 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4044 RTL_W32(0x7c, p->val);
4045 break;
4046 }
4047 }
4048}
4049
Francois Romieu07ce4062007-02-23 23:36:39 +01004050static void rtl_hw_start_8169(struct net_device *dev)
4051{
4052 struct rtl8169_private *tp = netdev_priv(dev);
4053 void __iomem *ioaddr = tp->mmio_addr;
4054 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004055
Francois Romieu9cb427b2006-11-02 00:10:16 +01004056 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4057 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4058 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4059 }
4060
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004062 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4063 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4064 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4065 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004066 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4067
Hayes Wange542a222011-07-06 15:58:04 +08004068 rtl_init_rxcfg(tp);
4069
françois romieuf0298f82011-01-03 15:07:42 +00004070 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004072 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
Francois Romieucecb5fd2011-04-01 10:21:07 +02004074 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4075 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4076 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4077 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004078 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Francois Romieu7f796d82007-06-11 23:04:41 +02004080 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004081
Francois Romieucecb5fd2011-04-01 10:21:07 +02004082 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4083 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004084 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004086 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
4088
Francois Romieubcf0bf92006-07-26 23:14:13 +02004089 RTL_W16(CPlusCmd, tp->cp_cmd);
4090
Francois Romieu6dccd162007-02-13 23:38:05 +01004091 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 /*
4094 * Undocumented corner. Supposedly:
4095 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4096 */
4097 RTL_W16(IntrMitigate, 0x0000);
4098
Francois Romieu7f796d82007-06-11 23:04:41 +02004099 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004100
Francois Romieucecb5fd2011-04-01 10:21:07 +02004101 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4102 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4103 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4104 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004105 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4106 rtl_set_rx_tx_config_registers(tp);
4107 }
4108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004110
4111 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4112 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
4114 RTL_W32(RxMissed, 0);
4115
Francois Romieu07ce4062007-02-23 23:36:39 +01004116 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118 /* no early-rx interrupts */
4119 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004120
4121 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01004122 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004123}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Francois Romieu9c14cea2008-07-05 00:21:15 +02004125static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02004126{
Jon Masone44daad2011-06-27 07:46:31 +00004127 int cap = pci_pcie_cap(pdev);
Francois Romieu458a9f62008-08-02 15:50:02 +02004128
Francois Romieu9c14cea2008-07-05 00:21:15 +02004129 if (cap) {
4130 u16 ctl;
4131
4132 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
4133 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
4134 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
4135 }
Francois Romieu458a9f62008-08-02 15:50:02 +02004136}
4137
françois romieu650e8d52011-01-03 15:08:29 +00004138static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004139{
4140 u32 csi;
4141
4142 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004143 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4144}
4145
françois romieue6de30d2011-01-03 15:08:37 +00004146static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4147{
4148 rtl_csi_access_enable(ioaddr, 0x17000000);
4149}
4150
françois romieu650e8d52011-01-03 15:08:29 +00004151static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4152{
4153 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004154}
4155
4156struct ephy_info {
4157 unsigned int offset;
4158 u16 mask;
4159 u16 bits;
4160};
4161
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004162static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004163{
4164 u16 w;
4165
4166 while (len-- > 0) {
4167 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4168 rtl_ephy_write(ioaddr, e->offset, w);
4169 e++;
4170 }
4171}
4172
Francois Romieub726e492008-06-28 12:22:59 +02004173static void rtl_disable_clock_request(struct pci_dev *pdev)
4174{
Jon Masone44daad2011-06-27 07:46:31 +00004175 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004176
4177 if (cap) {
4178 u16 ctl;
4179
4180 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4181 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4182 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4183 }
4184}
4185
françois romieue6de30d2011-01-03 15:08:37 +00004186static void rtl_enable_clock_request(struct pci_dev *pdev)
4187{
Jon Masone44daad2011-06-27 07:46:31 +00004188 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004189
4190 if (cap) {
4191 u16 ctl;
4192
4193 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4194 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4195 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4196 }
4197}
4198
Francois Romieub726e492008-06-28 12:22:59 +02004199#define R8168_CPCMD_QUIRK_MASK (\
4200 EnableBist | \
4201 Mac_dbgo_oe | \
4202 Force_half_dup | \
4203 Force_rxflow_en | \
4204 Force_txflow_en | \
4205 Cxpl_dbg_sel | \
4206 ASF | \
4207 PktCntrDisable | \
4208 Mac_dbgo_sel)
4209
Francois Romieu219a1e92008-06-28 11:58:39 +02004210static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4211{
Francois Romieub726e492008-06-28 12:22:59 +02004212 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4213
4214 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4215
Francois Romieu2e68ae42008-06-28 12:00:55 +02004216 rtl_tx_performance_tweak(pdev,
4217 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004218}
4219
4220static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4221{
4222 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004223
françois romieuf0298f82011-01-03 15:07:42 +00004224 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004225
4226 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004227}
4228
4229static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4230{
Francois Romieub726e492008-06-28 12:22:59 +02004231 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4232
4233 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4234
Francois Romieu219a1e92008-06-28 11:58:39 +02004235 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004236
4237 rtl_disable_clock_request(pdev);
4238
4239 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004240}
4241
Francois Romieuef3386f2008-06-29 12:24:30 +02004242static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004243{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004244 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004245 { 0x01, 0, 0x0001 },
4246 { 0x02, 0x0800, 0x1000 },
4247 { 0x03, 0, 0x0042 },
4248 { 0x06, 0x0080, 0x0000 },
4249 { 0x07, 0, 0x2000 }
4250 };
4251
françois romieu650e8d52011-01-03 15:08:29 +00004252 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004253
4254 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4255
Francois Romieu219a1e92008-06-28 11:58:39 +02004256 __rtl_hw_start_8168cp(ioaddr, pdev);
4257}
4258
Francois Romieuef3386f2008-06-29 12:24:30 +02004259static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4260{
françois romieu650e8d52011-01-03 15:08:29 +00004261 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004262
4263 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4264
4265 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4266
4267 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4268}
4269
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004270static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4271{
françois romieu650e8d52011-01-03 15:08:29 +00004272 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004273
4274 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4275
4276 /* Magic. */
4277 RTL_W8(DBG_REG, 0x20);
4278
françois romieuf0298f82011-01-03 15:07:42 +00004279 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004280
4281 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4282
4283 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4284}
4285
Francois Romieu219a1e92008-06-28 11:58:39 +02004286static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4287{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004288 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004289 { 0x02, 0x0800, 0x1000 },
4290 { 0x03, 0, 0x0002 },
4291 { 0x06, 0x0080, 0x0000 }
4292 };
4293
françois romieu650e8d52011-01-03 15:08:29 +00004294 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004295
4296 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4297
4298 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4299
Francois Romieu219a1e92008-06-28 11:58:39 +02004300 __rtl_hw_start_8168cp(ioaddr, pdev);
4301}
4302
4303static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4304{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004305 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004306 { 0x01, 0, 0x0001 },
4307 { 0x03, 0x0400, 0x0220 }
4308 };
4309
françois romieu650e8d52011-01-03 15:08:29 +00004310 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004311
4312 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4313
Francois Romieu219a1e92008-06-28 11:58:39 +02004314 __rtl_hw_start_8168cp(ioaddr, pdev);
4315}
4316
Francois Romieu197ff762008-06-28 13:16:02 +02004317static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4318{
4319 rtl_hw_start_8168c_2(ioaddr, pdev);
4320}
4321
Francois Romieu6fb07052008-06-29 11:54:28 +02004322static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4323{
françois romieu650e8d52011-01-03 15:08:29 +00004324 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004325
4326 __rtl_hw_start_8168cp(ioaddr, pdev);
4327}
4328
Francois Romieu5b538df2008-07-20 16:22:45 +02004329static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4330{
françois romieu650e8d52011-01-03 15:08:29 +00004331 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004332
4333 rtl_disable_clock_request(pdev);
4334
françois romieuf0298f82011-01-03 15:07:42 +00004335 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004336
4337 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4338
4339 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4340}
4341
hayeswang4804b3b2011-03-21 01:50:29 +00004342static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4343{
4344 rtl_csi_access_enable_1(ioaddr);
4345
4346 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4347
4348 RTL_W8(MaxTxPacketSize, TxPacketMax);
4349
4350 rtl_disable_clock_request(pdev);
4351}
4352
françois romieue6de30d2011-01-03 15:08:37 +00004353static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4354{
4355 static const struct ephy_info e_info_8168d_4[] = {
4356 { 0x0b, ~0, 0x48 },
4357 { 0x19, 0x20, 0x50 },
4358 { 0x0c, ~0, 0x20 }
4359 };
4360 int i;
4361
4362 rtl_csi_access_enable_1(ioaddr);
4363
4364 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4365
4366 RTL_W8(MaxTxPacketSize, TxPacketMax);
4367
4368 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4369 const struct ephy_info *e = e_info_8168d_4 + i;
4370 u16 w;
4371
4372 w = rtl_ephy_read(ioaddr, e->offset);
4373 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4374 }
4375
4376 rtl_enable_clock_request(pdev);
4377}
4378
Hayes Wang70090422011-07-06 15:58:06 +08004379static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004380{
Hayes Wang70090422011-07-06 15:58:06 +08004381 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004382 { 0x00, 0x0200, 0x0100 },
4383 { 0x00, 0x0000, 0x0004 },
4384 { 0x06, 0x0002, 0x0001 },
4385 { 0x06, 0x0000, 0x0030 },
4386 { 0x07, 0x0000, 0x2000 },
4387 { 0x00, 0x0000, 0x0020 },
4388 { 0x03, 0x5800, 0x2000 },
4389 { 0x03, 0x0000, 0x0001 },
4390 { 0x01, 0x0800, 0x1000 },
4391 { 0x07, 0x0000, 0x4000 },
4392 { 0x1e, 0x0000, 0x2000 },
4393 { 0x19, 0xffff, 0xfe6c },
4394 { 0x0a, 0x0000, 0x0040 }
4395 };
4396
4397 rtl_csi_access_enable_2(ioaddr);
4398
Hayes Wang70090422011-07-06 15:58:06 +08004399 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004400
4401 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4402
4403 RTL_W8(MaxTxPacketSize, TxPacketMax);
4404
4405 rtl_disable_clock_request(pdev);
4406
4407 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004408 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4409 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004410
Francois Romieucecb5fd2011-04-01 10:21:07 +02004411 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004412}
4413
Hayes Wang70090422011-07-06 15:58:06 +08004414static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4415{
4416 static const struct ephy_info e_info_8168e_2[] = {
4417 { 0x09, 0x0000, 0x0080 },
4418 { 0x19, 0x0000, 0x0224 }
4419 };
4420
4421 rtl_csi_access_enable_1(ioaddr);
4422
4423 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4424
4425 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4426
4427 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4428 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4429 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4430 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4431 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4432 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4433 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4434 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4435 ERIAR_EXGMAC);
4436
4437 RTL_W8(MaxTxPacketSize, 0x27);
4438
4439 rtl_disable_clock_request(pdev);
4440
4441 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4442 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4443
4444 /* Adjust EEE LED frequency */
4445 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4446
4447 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4448 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4449 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4450}
4451
Francois Romieu07ce4062007-02-23 23:36:39 +01004452static void rtl_hw_start_8168(struct net_device *dev)
4453{
Francois Romieu2dd99532007-06-11 23:22:52 +02004454 struct rtl8169_private *tp = netdev_priv(dev);
4455 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004456 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004457
4458 RTL_W8(Cfg9346, Cfg9346_Unlock);
4459
françois romieuf0298f82011-01-03 15:07:42 +00004460 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004461
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004462 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004463
Francois Romieu0e485152007-02-20 00:00:26 +01004464 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004465
4466 RTL_W16(CPlusCmd, tp->cp_cmd);
4467
Francois Romieu0e485152007-02-20 00:00:26 +01004468 RTL_W16(IntrMitigate, 0x5151);
4469
4470 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004471 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4472 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004473 tp->intr_event |= RxFIFOOver | PCSTimeout;
4474 tp->intr_event &= ~RxOverflow;
4475 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004476
4477 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4478
Francois Romieub8363902008-06-01 12:31:57 +02004479 rtl_set_rx_mode(dev);
4480
4481 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4482 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004483
4484 RTL_R8(IntrMask);
4485
Francois Romieu219a1e92008-06-28 11:58:39 +02004486 switch (tp->mac_version) {
4487 case RTL_GIGA_MAC_VER_11:
4488 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004489 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004490
4491 case RTL_GIGA_MAC_VER_12:
4492 case RTL_GIGA_MAC_VER_17:
4493 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004494 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004495
4496 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004497 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004498 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004499
4500 case RTL_GIGA_MAC_VER_19:
4501 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004502 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004503
4504 case RTL_GIGA_MAC_VER_20:
4505 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004506 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004507
Francois Romieu197ff762008-06-28 13:16:02 +02004508 case RTL_GIGA_MAC_VER_21:
4509 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004510 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004511
Francois Romieu6fb07052008-06-29 11:54:28 +02004512 case RTL_GIGA_MAC_VER_22:
4513 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004514 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004515
Francois Romieuef3386f2008-06-29 12:24:30 +02004516 case RTL_GIGA_MAC_VER_23:
4517 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004518 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004519
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004520 case RTL_GIGA_MAC_VER_24:
4521 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004522 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004523
Francois Romieu5b538df2008-07-20 16:22:45 +02004524 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004525 case RTL_GIGA_MAC_VER_26:
4526 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004527 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004528 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004529
françois romieue6de30d2011-01-03 15:08:37 +00004530 case RTL_GIGA_MAC_VER_28:
4531 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004532 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004533
hayeswang4804b3b2011-03-21 01:50:29 +00004534 case RTL_GIGA_MAC_VER_31:
4535 rtl_hw_start_8168dp(ioaddr, pdev);
4536 break;
4537
hayeswang01dc7fe2011-03-21 01:50:28 +00004538 case RTL_GIGA_MAC_VER_32:
4539 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004540 rtl_hw_start_8168e_1(ioaddr, pdev);
4541 break;
4542 case RTL_GIGA_MAC_VER_34:
4543 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004544 break;
françois romieue6de30d2011-01-03 15:08:37 +00004545
Francois Romieu219a1e92008-06-28 11:58:39 +02004546 default:
4547 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4548 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004549 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004550 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004551
Francois Romieu0e485152007-02-20 00:00:26 +01004552 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4553
Francois Romieub8363902008-06-01 12:31:57 +02004554 RTL_W8(Cfg9346, Cfg9346_Lock);
4555
Francois Romieu2dd99532007-06-11 23:22:52 +02004556 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004557
Francois Romieu0e485152007-02-20 00:00:26 +01004558 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
Francois Romieu2857ffb2008-08-02 21:08:49 +02004561#define R810X_CPCMD_QUIRK_MASK (\
4562 EnableBist | \
4563 Mac_dbgo_oe | \
4564 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004565 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004566 Force_txflow_en | \
4567 Cxpl_dbg_sel | \
4568 ASF | \
4569 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004570 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004571
4572static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4573{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004574 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004575 { 0x01, 0, 0x6e65 },
4576 { 0x02, 0, 0x091f },
4577 { 0x03, 0, 0xc2f9 },
4578 { 0x06, 0, 0xafb5 },
4579 { 0x07, 0, 0x0e00 },
4580 { 0x19, 0, 0xec80 },
4581 { 0x01, 0, 0x2e65 },
4582 { 0x01, 0, 0x6e65 }
4583 };
4584 u8 cfg1;
4585
françois romieu650e8d52011-01-03 15:08:29 +00004586 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004587
4588 RTL_W8(DBG_REG, FIX_NAK_1);
4589
4590 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4591
4592 RTL_W8(Config1,
4593 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4594 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4595
4596 cfg1 = RTL_R8(Config1);
4597 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4598 RTL_W8(Config1, cfg1 & ~LEDS0);
4599
Francois Romieu2857ffb2008-08-02 21:08:49 +02004600 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4601}
4602
4603static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4604{
françois romieu650e8d52011-01-03 15:08:29 +00004605 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004606
4607 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4608
4609 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4610 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004611}
4612
4613static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4614{
4615 rtl_hw_start_8102e_2(ioaddr, pdev);
4616
4617 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4618}
4619
Hayes Wang5a5e4442011-02-22 17:26:21 +08004620static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4621{
4622 static const struct ephy_info e_info_8105e_1[] = {
4623 { 0x07, 0, 0x4000 },
4624 { 0x19, 0, 0x0200 },
4625 { 0x19, 0, 0x0020 },
4626 { 0x1e, 0, 0x2000 },
4627 { 0x03, 0, 0x0001 },
4628 { 0x19, 0, 0x0100 },
4629 { 0x19, 0, 0x0004 },
4630 { 0x0a, 0, 0x0020 }
4631 };
4632
Francois Romieucecb5fd2011-04-01 10:21:07 +02004633 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004634 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4635
Francois Romieucecb5fd2011-04-01 10:21:07 +02004636 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004637 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4638
4639 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004640 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004641
4642 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4643}
4644
4645static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4646{
4647 rtl_hw_start_8105e_1(ioaddr, pdev);
4648 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4649}
4650
Francois Romieu07ce4062007-02-23 23:36:39 +01004651static void rtl_hw_start_8101(struct net_device *dev)
4652{
Francois Romieucdf1a602007-06-11 23:29:50 +02004653 struct rtl8169_private *tp = netdev_priv(dev);
4654 void __iomem *ioaddr = tp->mmio_addr;
4655 struct pci_dev *pdev = tp->pci_dev;
4656
Francois Romieucecb5fd2011-04-01 10:21:07 +02004657 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4658 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004659 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004660
4661 if (cap) {
4662 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4663 PCI_EXP_DEVCTL_NOSNOOP_EN);
4664 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004665 }
4666
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004667 RTL_W8(Cfg9346, Cfg9346_Unlock);
4668
Francois Romieu2857ffb2008-08-02 21:08:49 +02004669 switch (tp->mac_version) {
4670 case RTL_GIGA_MAC_VER_07:
4671 rtl_hw_start_8102e_1(ioaddr, pdev);
4672 break;
4673
4674 case RTL_GIGA_MAC_VER_08:
4675 rtl_hw_start_8102e_3(ioaddr, pdev);
4676 break;
4677
4678 case RTL_GIGA_MAC_VER_09:
4679 rtl_hw_start_8102e_2(ioaddr, pdev);
4680 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004681
4682 case RTL_GIGA_MAC_VER_29:
4683 rtl_hw_start_8105e_1(ioaddr, pdev);
4684 break;
4685 case RTL_GIGA_MAC_VER_30:
4686 rtl_hw_start_8105e_2(ioaddr, pdev);
4687 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004688 }
4689
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004690 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004691
françois romieuf0298f82011-01-03 15:07:42 +00004692 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004693
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004694 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004695
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004696 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004697 RTL_W16(CPlusCmd, tp->cp_cmd);
4698
4699 RTL_W16(IntrMitigate, 0x0000);
4700
4701 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4702
4703 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4704 rtl_set_rx_tx_config_registers(tp);
4705
Francois Romieucdf1a602007-06-11 23:29:50 +02004706 RTL_R8(IntrMask);
4707
Francois Romieucdf1a602007-06-11 23:29:50 +02004708 rtl_set_rx_mode(dev);
4709
4710 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004711
Francois Romieu0e485152007-02-20 00:00:26 +01004712 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
4715static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4716{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4718 return -EINVAL;
4719
4720 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004721 netdev_update_features(dev);
4722
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004723 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724}
4725
4726static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4727{
Al Viro95e09182007-12-22 18:55:39 +00004728 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4730}
4731
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004732static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4733 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004735 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004736 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004737
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004738 kfree(*data_buff);
4739 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 rtl8169_make_unusable_by_asic(desc);
4741}
4742
4743static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4744{
4745 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4746
4747 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4748}
4749
4750static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4751 u32 rx_buf_sz)
4752{
4753 desc->addr = cpu_to_le64(mapping);
4754 wmb();
4755 rtl8169_mark_to_asic(desc, rx_buf_sz);
4756}
4757
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004758static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004760 return (void *)ALIGN((long)data, 16);
4761}
4762
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004763static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4764 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004765{
4766 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004768 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004769 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004770 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004772 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4773 if (!data)
4774 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004775
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004776 if (rtl8169_align(data) != data) {
4777 kfree(data);
4778 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4779 if (!data)
4780 return NULL;
4781 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004782
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004783 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004784 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004785 if (unlikely(dma_mapping_error(d, mapping))) {
4786 if (net_ratelimit())
4787 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004788 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
4791 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004792 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004793
4794err_out:
4795 kfree(data);
4796 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
4799static void rtl8169_rx_clear(struct rtl8169_private *tp)
4800{
Francois Romieu07d3f512007-02-21 22:40:46 +01004801 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
4803 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004804 if (tp->Rx_databuff[i]) {
4805 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 tp->RxDescArray + i);
4807 }
4808 }
4809}
4810
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004811static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004813 desc->opts1 |= cpu_to_le32(RingEnd);
4814}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004815
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004816static int rtl8169_rx_fill(struct rtl8169_private *tp)
4817{
4818 unsigned int i;
4819
4820 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004821 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004822
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004823 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004825
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004826 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004827 if (!data) {
4828 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004829 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004830 }
4831 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004834 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4835 return 0;
4836
4837err_out:
4838 rtl8169_rx_clear(tp);
4839 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840}
4841
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842static int rtl8169_init_ring(struct net_device *dev)
4843{
4844 struct rtl8169_private *tp = netdev_priv(dev);
4845
4846 rtl8169_init_ring_indexes(tp);
4847
4848 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004849 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004851 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004854static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 struct TxDesc *desc)
4856{
4857 unsigned int len = tx_skb->len;
4858
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004859 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 desc->opts1 = 0x00;
4862 desc->opts2 = 0x00;
4863 desc->addr = 0x00;
4864 tx_skb->len = 0;
4865}
4866
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004867static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4868 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869{
4870 unsigned int i;
4871
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004872 for (i = 0; i < n; i++) {
4873 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 struct ring_info *tx_skb = tp->tx_skb + entry;
4875 unsigned int len = tx_skb->len;
4876
4877 if (len) {
4878 struct sk_buff *skb = tx_skb->skb;
4879
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004880 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 tp->TxDescArray + entry);
4882 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004883 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 dev_kfree_skb(skb);
4885 tx_skb->skb = NULL;
4886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 }
4888 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004889}
4890
4891static void rtl8169_tx_clear(struct rtl8169_private *tp)
4892{
4893 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 tp->cur_tx = tp->dirty_tx = 0;
4895}
4896
David Howellsc4028952006-11-22 14:57:56 +00004897static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898{
4899 struct rtl8169_private *tp = netdev_priv(dev);
4900
David Howellsc4028952006-11-22 14:57:56 +00004901 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 schedule_delayed_work(&tp->task, 4);
4903}
4904
4905static void rtl8169_wait_for_quiescence(struct net_device *dev)
4906{
4907 struct rtl8169_private *tp = netdev_priv(dev);
4908 void __iomem *ioaddr = tp->mmio_addr;
4909
4910 synchronize_irq(dev->irq);
4911
4912 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004913 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
4915 rtl8169_irq_mask_and_ack(ioaddr);
4916
David S. Millerd1d08d12008-01-07 20:53:33 -08004917 tp->intr_mask = 0xffff;
4918 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004919 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920}
4921
David Howellsc4028952006-11-22 14:57:56 +00004922static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923{
David Howellsc4028952006-11-22 14:57:56 +00004924 struct rtl8169_private *tp =
4925 container_of(work, struct rtl8169_private, task.work);
4926 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 int ret;
4928
Francois Romieueb2a0212007-02-15 23:37:21 +01004929 rtnl_lock();
4930
4931 if (!netif_running(dev))
4932 goto out_unlock;
4933
4934 rtl8169_wait_for_quiescence(dev);
4935 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
4937 ret = rtl8169_open(dev);
4938 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004939 if (net_ratelimit())
4940 netif_err(tp, drv, dev,
4941 "reinit failure (status = %d). Rescheduling\n",
4942 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4944 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004945
4946out_unlock:
4947 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948}
4949
David Howellsc4028952006-11-22 14:57:56 +00004950static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951{
David Howellsc4028952006-11-22 14:57:56 +00004952 struct rtl8169_private *tp =
4953 container_of(work, struct rtl8169_private, task.work);
4954 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004955 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
Francois Romieueb2a0212007-02-15 23:37:21 +01004957 rtnl_lock();
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004960 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
4962 rtl8169_wait_for_quiescence(dev);
4963
Francois Romieu56de4142011-03-15 17:29:31 +01004964 for (i = 0; i < NUM_RX_DESC; i++)
4965 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 rtl8169_tx_clear(tp);
4968
Hayes Wang92fc43b2011-07-06 15:58:03 +08004969 rtl8169_hw_reset(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01004970 rtl_hw_start(dev);
4971 netif_wake_queue(dev);
4972 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004973
4974out_unlock:
4975 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976}
4977
4978static void rtl8169_tx_timeout(struct net_device *dev)
4979{
4980 struct rtl8169_private *tp = netdev_priv(dev);
4981
françois romieue6de30d2011-01-03 15:08:37 +00004982 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 /* Let's wait a bit while any (async) irq lands on */
4985 rtl8169_schedule_work(dev, rtl8169_reset_task);
4986}
4987
4988static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004989 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990{
4991 struct skb_shared_info *info = skb_shinfo(skb);
4992 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004993 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004994 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
4996 entry = tp->cur_tx;
4997 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4998 skb_frag_t *frag = info->frags + cur_frag;
4999 dma_addr_t mapping;
5000 u32 status, len;
5001 void *addr;
5002
5003 entry = (entry + 1) % NUM_TX_DESC;
5004
5005 txd = tp->TxDescArray + entry;
5006 len = frag->size;
5007 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005008 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005009 if (unlikely(dma_mapping_error(d, mapping))) {
5010 if (net_ratelimit())
5011 netif_err(tp, drv, tp->dev,
5012 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005013 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Francois Romieucecb5fd2011-04-01 10:21:07 +02005016 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005017 status = opts[0] | len |
5018 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
5020 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005021 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 txd->addr = cpu_to_le64(mapping);
5023
5024 tp->tx_skb[entry].len = len;
5025 }
5026
5027 if (cur_frag) {
5028 tp->tx_skb[entry].skb = skb;
5029 txd->opts1 |= cpu_to_le32(LastFrag);
5030 }
5031
5032 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005033
5034err_out:
5035 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5036 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037}
5038
Francois Romieu2b7b4312011-04-18 22:53:24 -07005039static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5040 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005042 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005043 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005044 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
Francois Romieu2b7b4312011-04-18 22:53:24 -07005046 if (mss) {
5047 opts[0] |= TD_LSO;
5048 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5049 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005050 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
5052 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005053 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005055 opts[offset] |= info->checksum.udp;
5056 else
5057 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059}
5060
Stephen Hemminger613573252009-08-31 19:50:58 +00005061static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5062 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
5064 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005065 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 struct TxDesc *txd = tp->TxDescArray + entry;
5067 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005068 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 dma_addr_t mapping;
5070 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005071 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005072 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005073
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005075 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005076 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 }
5078
5079 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005080 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005082 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005083 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005084 if (unlikely(dma_mapping_error(d, mapping))) {
5085 if (net_ratelimit())
5086 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005087 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
5090 tp->tx_skb[entry].len = len;
5091 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
Francois Romieu2b7b4312011-04-18 22:53:24 -07005093 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5094 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005095
Francois Romieu2b7b4312011-04-18 22:53:24 -07005096 rtl8169_tso_csum(tp, skb, opts);
5097
5098 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005099 if (frags < 0)
5100 goto err_dma_1;
5101 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005102 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005103 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005104 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005105 tp->tx_skb[entry].skb = skb;
5106 }
5107
Francois Romieu2b7b4312011-04-18 22:53:24 -07005108 txd->opts2 = cpu_to_le32(opts[1]);
5109
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 wmb();
5111
Francois Romieucecb5fd2011-04-01 10:21:07 +02005112 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005113 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 txd->opts1 = cpu_to_le32(status);
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 tp->cur_tx += frags + 1;
5117
David Dillow4c020a92010-03-03 16:33:10 +00005118 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Francois Romieucecb5fd2011-04-01 10:21:07 +02005120 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
5122 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5123 netif_stop_queue(dev);
5124 smp_rmb();
5125 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5126 netif_wake_queue(dev);
5127 }
5128
Stephen Hemminger613573252009-08-31 19:50:58 +00005129 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005131err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005132 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005133err_dma_0:
5134 dev_kfree_skb(skb);
5135 dev->stats.tx_dropped++;
5136 return NETDEV_TX_OK;
5137
5138err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005140 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005141 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142}
5143
5144static void rtl8169_pcierr_interrupt(struct net_device *dev)
5145{
5146 struct rtl8169_private *tp = netdev_priv(dev);
5147 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 u16 pci_status, pci_cmd;
5149
5150 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5151 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5152
Joe Perchesbf82c182010-02-09 11:49:50 +00005153 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5154 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
5156 /*
5157 * The recovery sequence below admits a very elaborated explanation:
5158 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005159 * - I did not see what else could be done;
5160 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 *
5162 * Feel free to adjust to your needs.
5163 */
Francois Romieua27993f2006-12-18 00:04:19 +01005164 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005165 pci_cmd &= ~PCI_COMMAND_PARITY;
5166 else
5167 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5168
5169 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
5171 pci_write_config_word(pdev, PCI_STATUS,
5172 pci_status & (PCI_STATUS_DETECTED_PARITY |
5173 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5174 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5175
5176 /* The infamous DAC f*ckup only happens at boot time */
5177 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005178 void __iomem *ioaddr = tp->mmio_addr;
5179
Joe Perchesbf82c182010-02-09 11:49:50 +00005180 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 tp->cp_cmd &= ~PCIDAC;
5182 RTL_W16(CPlusCmd, tp->cp_cmd);
5183 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 }
5185
françois romieue6de30d2011-01-03 15:08:37 +00005186 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005187
5188 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189}
5190
Francois Romieu07d3f512007-02-21 22:40:46 +01005191static void rtl8169_tx_interrupt(struct net_device *dev,
5192 struct rtl8169_private *tp,
5193 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194{
5195 unsigned int dirty_tx, tx_left;
5196
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 dirty_tx = tp->dirty_tx;
5198 smp_rmb();
5199 tx_left = tp->cur_tx - dirty_tx;
5200
5201 while (tx_left > 0) {
5202 unsigned int entry = dirty_tx % NUM_TX_DESC;
5203 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 u32 status;
5205
5206 rmb();
5207 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5208 if (status & DescOwn)
5209 break;
5210
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005211 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5212 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005214 dev->stats.tx_packets++;
5215 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005216 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 tx_skb->skb = NULL;
5218 }
5219 dirty_tx++;
5220 tx_left--;
5221 }
5222
5223 if (tp->dirty_tx != dirty_tx) {
5224 tp->dirty_tx = dirty_tx;
5225 smp_wmb();
5226 if (netif_queue_stopped(dev) &&
5227 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5228 netif_wake_queue(dev);
5229 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005230 /*
5231 * 8168 hack: TxPoll requests are lost when the Tx packets are
5232 * too close. Let's kick an extra TxPoll request when a burst
5233 * of start_xmit activity is detected (if it is not detected,
5234 * it is slow enough). -- FR
5235 */
5236 smp_rmb();
5237 if (tp->cur_tx != dirty_tx)
5238 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 }
5240}
5241
Francois Romieu126fa4b2005-05-12 20:09:17 -04005242static inline int rtl8169_fragmented_frame(u32 status)
5243{
5244 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5245}
5246
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005247static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 u32 status = opts1 & RxProtoMask;
5250
5251 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005252 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 skb->ip_summed = CHECKSUM_UNNECESSARY;
5254 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005255 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256}
5257
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005258static struct sk_buff *rtl8169_try_rx_copy(void *data,
5259 struct rtl8169_private *tp,
5260 int pkt_size,
5261 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005263 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005264 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005266 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005267 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005268 prefetch(data);
5269 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5270 if (skb)
5271 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005272 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5273
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005274 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275}
5276
Francois Romieu07d3f512007-02-21 22:40:46 +01005277static int rtl8169_rx_interrupt(struct net_device *dev,
5278 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005279 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280{
5281 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005282 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 cur_rx = tp->cur_rx;
5285 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005286 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005288 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005290 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 u32 status;
5292
5293 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04005294 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
5296 if (status & DescOwn)
5297 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005298 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005299 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5300 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005301 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005303 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005305 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005306 if (status & RxFOVF) {
5307 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005308 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005309 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005310 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005312 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005313 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315
Francois Romieu126fa4b2005-05-12 20:09:17 -04005316 /*
5317 * The driver does not support incoming fragmented
5318 * frames. They are seen as a symptom of over-mtu
5319 * sized frames.
5320 */
5321 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005322 dev->stats.rx_dropped++;
5323 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005324 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005325 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005326 }
5327
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005328 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5329 tp, pkt_size, addr);
5330 rtl8169_mark_to_asic(desc, rx_buf_sz);
5331 if (!skb) {
5332 dev->stats.rx_dropped++;
5333 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 }
5335
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005336 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 skb_put(skb, pkt_size);
5338 skb->protocol = eth_type_trans(skb, dev);
5339
Francois Romieu7a8fc772011-03-01 17:18:33 +01005340 rtl8169_rx_vlan_tag(desc, skb);
5341
Francois Romieu56de4142011-03-15 17:29:31 +01005342 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
Francois Romieucebf8cc2007-10-18 12:06:54 +02005344 dev->stats.rx_bytes += pkt_size;
5345 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005347
5348 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005349 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005350 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5351 desc->opts2 = 0;
5352 cur_rx++;
5353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 }
5355
5356 count = cur_rx - tp->cur_rx;
5357 tp->cur_rx = cur_rx;
5358
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005359 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
5361 return count;
5362}
5363
Francois Romieu07d3f512007-02-21 22:40:46 +01005364static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365{
Francois Romieu07d3f512007-02-21 22:40:46 +01005366 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005370 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
David Dillowf11a3772009-05-22 15:29:34 +00005372 /* loop handling interrupts until we have no new ones or
5373 * we hit a invalid/hotplug case.
5374 */
Francois Romieu865c6522008-05-11 14:51:00 +02005375 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005376 while (status && status != 0xffff) {
5377 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
David Dillowf11a3772009-05-22 15:29:34 +00005379 /* Handle all of the error cases first. These will reset
5380 * the chip, so just exit the loop.
5381 */
5382 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005383 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005384 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 }
David Dillowf11a3772009-05-22 15:29:34 +00005386
Francois Romieu1519e572011-02-03 12:02:36 +01005387 if (unlikely(status & RxFIFOOver)) {
5388 switch (tp->mac_version) {
5389 /* Work around for rx fifo overflow */
5390 case RTL_GIGA_MAC_VER_11:
5391 case RTL_GIGA_MAC_VER_22:
5392 case RTL_GIGA_MAC_VER_26:
5393 netif_stop_queue(dev);
5394 rtl8169_tx_timeout(dev);
5395 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005396 /* Testers needed. */
5397 case RTL_GIGA_MAC_VER_17:
5398 case RTL_GIGA_MAC_VER_19:
5399 case RTL_GIGA_MAC_VER_20:
5400 case RTL_GIGA_MAC_VER_21:
5401 case RTL_GIGA_MAC_VER_23:
5402 case RTL_GIGA_MAC_VER_24:
5403 case RTL_GIGA_MAC_VER_27:
5404 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005405 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005406 /* Experimental science. Pktgen proof. */
5407 case RTL_GIGA_MAC_VER_12:
5408 case RTL_GIGA_MAC_VER_25:
5409 if (status == RxFIFOOver)
5410 goto done;
5411 break;
5412 default:
5413 break;
5414 }
David Dillowf11a3772009-05-22 15:29:34 +00005415 }
5416
5417 if (unlikely(status & SYSErr)) {
5418 rtl8169_pcierr_interrupt(dev);
5419 break;
5420 }
5421
5422 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005423 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005424
5425 /* We need to see the lastest version of tp->intr_mask to
5426 * avoid ignoring an MSI interrupt and having to wait for
5427 * another event which may never come.
5428 */
5429 smp_rmb();
5430 if (status & tp->intr_mask & tp->napi_event) {
5431 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5432 tp->intr_mask = ~tp->napi_event;
5433
5434 if (likely(napi_schedule_prep(&tp->napi)))
5435 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005436 else
5437 netif_info(tp, intr, dev,
5438 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005439 }
5440
5441 /* We only get a new MSI interrupt when all active irq
5442 * sources on the chip have been acknowledged. So, ack
5443 * everything we've seen and check if new sources have become
5444 * active to avoid blocking all interrupts from the chip.
5445 */
5446 RTL_W16(IntrStatus,
5447 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5448 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 }
Francois Romieu1519e572011-02-03 12:02:36 +01005450done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 return IRQ_RETVAL(handled);
5452}
5453
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005454static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005456 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5457 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005459 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005461 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 rtl8169_tx_interrupt(dev, tp, ioaddr);
5463
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005464 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005465 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005466
5467 /* We need for force the visibility of tp->intr_mask
5468 * for other CPUs, as we can loose an MSI interrupt
5469 * and potentially wait for a retransmit timeout if we don't.
5470 * The posted write to IntrMask is safe, as it will
5471 * eventually make it to the chip and we won't loose anything
5472 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 */
David Dillowf11a3772009-05-22 15:29:34 +00005474 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005475 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005476 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 }
5478
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005479 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
Francois Romieu523a6092008-09-10 22:28:56 +02005482static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5483{
5484 struct rtl8169_private *tp = netdev_priv(dev);
5485
5486 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5487 return;
5488
5489 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5490 RTL_W32(RxMissed, 0);
5491}
5492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493static void rtl8169_down(struct net_device *dev)
5494{
5495 struct rtl8169_private *tp = netdev_priv(dev);
5496 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
Francois Romieu4876cc12011-03-11 21:07:11 +01005498 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
5500 netif_stop_queue(dev);
5501
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005502 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005503
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 spin_lock_irq(&tp->lock);
5505
Hayes Wang92fc43b2011-07-06 15:58:03 +08005506 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005507 /*
5508 * At this point device interrupts can not be enabled in any function,
5509 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5510 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5511 */
Francois Romieu523a6092008-09-10 22:28:56 +02005512 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
5514 spin_unlock_irq(&tp->lock);
5515
5516 synchronize_irq(dev->irq);
5517
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005519 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 rtl8169_tx_clear(tp);
5522
5523 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005524
5525 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526}
5527
5528static int rtl8169_close(struct net_device *dev)
5529{
5530 struct rtl8169_private *tp = netdev_priv(dev);
5531 struct pci_dev *pdev = tp->pci_dev;
5532
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005533 pm_runtime_get_sync(&pdev->dev);
5534
Francois Romieucecb5fd2011-04-01 10:21:07 +02005535 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005536 rtl8169_update_counters(dev);
5537
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 rtl8169_down(dev);
5539
5540 free_irq(dev->irq, dev);
5541
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005542 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5543 tp->RxPhyAddr);
5544 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5545 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 tp->TxDescArray = NULL;
5547 tp->RxDescArray = NULL;
5548
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005549 pm_runtime_put_sync(&pdev->dev);
5550
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 return 0;
5552}
5553
Francois Romieu07ce4062007-02-23 23:36:39 +01005554static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555{
5556 struct rtl8169_private *tp = netdev_priv(dev);
5557 void __iomem *ioaddr = tp->mmio_addr;
5558 unsigned long flags;
5559 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005560 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 u32 tmp = 0;
5562
5563 if (dev->flags & IFF_PROMISC) {
5564 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005565 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 rx_mode =
5567 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5568 AcceptAllPhys;
5569 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005570 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005571 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 /* Too many to filter perfectly -- accept all multicasts. */
5573 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5574 mc_filter[1] = mc_filter[0] = 0xffffffff;
5575 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005576 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005577
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 rx_mode = AcceptBroadcast | AcceptMyPhys;
5579 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005580 netdev_for_each_mc_addr(ha, dev) {
5581 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5583 rx_mode |= AcceptMulticast;
5584 }
5585 }
5586
5587 spin_lock_irqsave(&tp->lock, flags);
5588
Hayes Wange542a222011-07-06 15:58:04 +08005589 tmp = RTL_R32(RxConfig) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
Francois Romieuf887cce2008-07-17 22:24:18 +02005591 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005592 u32 data = mc_filter[0];
5593
5594 mc_filter[0] = swab32(mc_filter[1]);
5595 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005596 }
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005599 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Francois Romieu57a9f232007-06-04 22:10:15 +02005601 RTL_W32(RxConfig, tmp);
5602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 spin_unlock_irqrestore(&tp->lock, flags);
5604}
5605
5606/**
5607 * rtl8169_get_stats - Get rtl8169 read/write statistics
5608 * @dev: The Ethernet Device to get statistics for
5609 *
5610 * Get TX/RX statistics for rtl8169
5611 */
5612static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5613{
5614 struct rtl8169_private *tp = netdev_priv(dev);
5615 void __iomem *ioaddr = tp->mmio_addr;
5616 unsigned long flags;
5617
5618 if (netif_running(dev)) {
5619 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005620 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 spin_unlock_irqrestore(&tp->lock, flags);
5622 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005623
Francois Romieucebf8cc2007-10-18 12:06:54 +02005624 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625}
5626
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005627static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005628{
françois romieu065c27c2011-01-03 15:08:12 +00005629 struct rtl8169_private *tp = netdev_priv(dev);
5630
Francois Romieu5d06a992006-02-23 00:47:58 +01005631 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005632 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005633
françois romieu065c27c2011-01-03 15:08:12 +00005634 rtl_pll_power_down(tp);
5635
Francois Romieu5d06a992006-02-23 00:47:58 +01005636 netif_device_detach(dev);
5637 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005638}
Francois Romieu5d06a992006-02-23 00:47:58 +01005639
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005640#ifdef CONFIG_PM
5641
5642static int rtl8169_suspend(struct device *device)
5643{
5644 struct pci_dev *pdev = to_pci_dev(device);
5645 struct net_device *dev = pci_get_drvdata(pdev);
5646
5647 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005648
Francois Romieu5d06a992006-02-23 00:47:58 +01005649 return 0;
5650}
5651
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005652static void __rtl8169_resume(struct net_device *dev)
5653{
françois romieu065c27c2011-01-03 15:08:12 +00005654 struct rtl8169_private *tp = netdev_priv(dev);
5655
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005656 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005657
5658 rtl_pll_power_up(tp);
5659
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005660 rtl8169_schedule_work(dev, rtl8169_reset_task);
5661}
5662
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005663static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005664{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005665 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005666 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005667 struct rtl8169_private *tp = netdev_priv(dev);
5668
5669 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005670
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005671 if (netif_running(dev))
5672 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005673
Francois Romieu5d06a992006-02-23 00:47:58 +01005674 return 0;
5675}
5676
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005677static int rtl8169_runtime_suspend(struct device *device)
5678{
5679 struct pci_dev *pdev = to_pci_dev(device);
5680 struct net_device *dev = pci_get_drvdata(pdev);
5681 struct rtl8169_private *tp = netdev_priv(dev);
5682
5683 if (!tp->TxDescArray)
5684 return 0;
5685
5686 spin_lock_irq(&tp->lock);
5687 tp->saved_wolopts = __rtl8169_get_wol(tp);
5688 __rtl8169_set_wol(tp, WAKE_ANY);
5689 spin_unlock_irq(&tp->lock);
5690
5691 rtl8169_net_suspend(dev);
5692
5693 return 0;
5694}
5695
5696static int rtl8169_runtime_resume(struct device *device)
5697{
5698 struct pci_dev *pdev = to_pci_dev(device);
5699 struct net_device *dev = pci_get_drvdata(pdev);
5700 struct rtl8169_private *tp = netdev_priv(dev);
5701
5702 if (!tp->TxDescArray)
5703 return 0;
5704
5705 spin_lock_irq(&tp->lock);
5706 __rtl8169_set_wol(tp, tp->saved_wolopts);
5707 tp->saved_wolopts = 0;
5708 spin_unlock_irq(&tp->lock);
5709
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005710 rtl8169_init_phy(dev, tp);
5711
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005712 __rtl8169_resume(dev);
5713
5714 return 0;
5715}
5716
5717static int rtl8169_runtime_idle(struct device *device)
5718{
5719 struct pci_dev *pdev = to_pci_dev(device);
5720 struct net_device *dev = pci_get_drvdata(pdev);
5721 struct rtl8169_private *tp = netdev_priv(dev);
5722
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005723 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005724}
5725
Alexey Dobriyan47145212009-12-14 18:00:08 -08005726static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005727 .suspend = rtl8169_suspend,
5728 .resume = rtl8169_resume,
5729 .freeze = rtl8169_suspend,
5730 .thaw = rtl8169_resume,
5731 .poweroff = rtl8169_suspend,
5732 .restore = rtl8169_resume,
5733 .runtime_suspend = rtl8169_runtime_suspend,
5734 .runtime_resume = rtl8169_runtime_resume,
5735 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005736};
5737
5738#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5739
5740#else /* !CONFIG_PM */
5741
5742#define RTL8169_PM_OPS NULL
5743
5744#endif /* !CONFIG_PM */
5745
Francois Romieu1765f952008-09-13 17:21:40 +02005746static void rtl_shutdown(struct pci_dev *pdev)
5747{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005748 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005749 struct rtl8169_private *tp = netdev_priv(dev);
5750 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005751
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005752 rtl8169_net_suspend(dev);
5753
Francois Romieucecb5fd2011-04-01 10:21:07 +02005754 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005755 rtl_rar_set(tp, dev->perm_addr);
5756
françois romieu4bb3f522009-06-17 11:41:45 +00005757 spin_lock_irq(&tp->lock);
5758
Hayes Wang92fc43b2011-07-06 15:58:03 +08005759 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005760
5761 spin_unlock_irq(&tp->lock);
5762
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005763 if (system_state == SYSTEM_POWER_OFF) {
Hayes Wangaaa89c02011-07-06 15:58:08 +08005764 /* WoL fails with 8168b when the receiver is disabled. */
5765 if ((tp->mac_version == RTL_GIGA_MAC_VER_11 ||
5766 tp->mac_version == RTL_GIGA_MAC_VER_12 ||
5767 tp->mac_version == RTL_GIGA_MAC_VER_17) &&
5768 (tp->features & RTL_FEATURE_WOL)) {
françois romieuca52efd2009-07-24 12:34:19 +00005769 pci_clear_master(pdev);
5770
5771 RTL_W8(ChipCmd, CmdRxEnb);
5772 /* PCI commit */
5773 RTL_R8(ChipCmd);
5774 }
5775
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005776 pci_wake_from_d3(pdev, true);
5777 pci_set_power_state(pdev, PCI_D3hot);
5778 }
5779}
Francois Romieu5d06a992006-02-23 00:47:58 +01005780
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781static struct pci_driver rtl8169_pci_driver = {
5782 .name = MODULENAME,
5783 .id_table = rtl8169_pci_tbl,
5784 .probe = rtl8169_init_one,
5785 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005786 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005787 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788};
5789
Francois Romieu07d3f512007-02-21 22:40:46 +01005790static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791{
Jeff Garzik29917622006-08-19 17:48:59 -04005792 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793}
5794
Francois Romieu07d3f512007-02-21 22:40:46 +01005795static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796{
5797 pci_unregister_driver(&rtl8169_pci_driver);
5798}
5799
5800module_init(rtl8169_init_module);
5801module_exit(rtl8169_cleanup_module);