blob: 7d9c650f395e1399a7185c9338f26b5fd8713db7 [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 Wang4f6b00e52011-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 Wang4f6b00e52011-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 Wang4f6b00e52011-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 Wang4f6b00e52011-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 Wang4f6b00e52011-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 Wang4f6b00e52011-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 Wang4f6b00e52011-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,
Francois Romieu1687b562011-07-19 17:21:29 +0200438#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* TxConfigBits */
441 TxInterFrameGapShift = 24,
442 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
443
Francois Romieu5d06a992006-02-23 00:47:58 +0100444 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200445 LEDS1 = (1 << 7),
446 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200447 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200448 Speed_down = (1 << 4),
449 MEMMAP = (1 << 3),
450 IOMAP = (1 << 2),
451 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100452 PMEnable = (1 << 0), /* Power Management Enable */
453
Francois Romieu6dccd162007-02-13 23:38:05 +0100454 /* Config2 register p. 25 */
455 PCI_Clock_66MHz = 0x01,
456 PCI_Clock_33MHz = 0x00,
457
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100458 /* Config3 register p.25 */
459 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
460 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200461 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100462
Francois Romieu5d06a992006-02-23 00:47:58 +0100463 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100464 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
465 MWF = (1 << 5), /* Accept Multicast wakeup frame */
466 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200467 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100468 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100469 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /* TBICSR p.28 */
472 TBIReset = 0x80000000,
473 TBILoopback = 0x40000000,
474 TBINwEnable = 0x20000000,
475 TBINwRestart = 0x10000000,
476 TBILinkOk = 0x02000000,
477 TBINwComplete = 0x01000000,
478
479 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200480 EnableBist = (1 << 15), // 8168 8101
481 Mac_dbgo_oe = (1 << 14), // 8168 8101
482 Normal_mode = (1 << 13), // unused
483 Force_half_dup = (1 << 12), // 8168 8101
484 Force_rxflow_en = (1 << 11), // 8168 8101
485 Force_txflow_en = (1 << 10), // 8168 8101
486 Cxpl_dbg_sel = (1 << 9), // 8168 8101
487 ASF = (1 << 8), // 8168 8101
488 PktCntrDisable = (1 << 7), // 8168 8101
489 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 RxVlan = (1 << 6),
491 RxChkSum = (1 << 5),
492 PCIDAC = (1 << 4),
493 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100494 INTT_0 = 0x0000, // 8168
495 INTT_1 = 0x0001, // 8168
496 INTT_2 = 0x0002, // 8168
497 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100500 TBI_Enable = 0x80,
501 TxFlowCtrl = 0x40,
502 RxFlowCtrl = 0x20,
503 _1000bpsF = 0x10,
504 _100bps = 0x08,
505 _10bps = 0x04,
506 LinkStatus = 0x02,
507 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100510 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200511
512 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100513 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514};
515
Francois Romieu2b7b4312011-04-18 22:53:24 -0700516enum rtl_desc_bit {
517 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
519 RingEnd = (1 << 30), /* End of descriptor ring */
520 FirstFrag = (1 << 29), /* First segment of a packet */
521 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700522};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Francois Romieu2b7b4312011-04-18 22:53:24 -0700524/* Generic case. */
525enum rtl_tx_desc_bit {
526 /* First doubleword. */
527 TD_LSO = (1 << 27), /* Large Send Offload */
528#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Francois Romieu2b7b4312011-04-18 22:53:24 -0700530 /* Second doubleword. */
531 TxVlanTag = (1 << 17), /* Add VLAN tag */
532};
533
534/* 8169, 8168b and 810x except 8102e. */
535enum rtl_tx_desc_bit_0 {
536 /* First doubleword. */
537#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
538 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
539 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
540 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
541};
542
543/* 8102e, 8168c and beyond. */
544enum rtl_tx_desc_bit_1 {
545 /* Second doubleword. */
546#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
547 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
548 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
549 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
550};
551
552static const struct rtl_tx_desc_info {
553 struct {
554 u32 udp;
555 u32 tcp;
556 } checksum;
557 u16 mss_shift;
558 u16 opts_offset;
559} tx_desc_info [] = {
560 [RTL_TD_0] = {
561 .checksum = {
562 .udp = TD0_IP_CS | TD0_UDP_CS,
563 .tcp = TD0_IP_CS | TD0_TCP_CS
564 },
565 .mss_shift = TD0_MSS_SHIFT,
566 .opts_offset = 0
567 },
568 [RTL_TD_1] = {
569 .checksum = {
570 .udp = TD1_IP_CS | TD1_UDP_CS,
571 .tcp = TD1_IP_CS | TD1_TCP_CS
572 },
573 .mss_shift = TD1_MSS_SHIFT,
574 .opts_offset = 1
575 }
576};
577
578enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Rx private */
580 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
581 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
582
583#define RxProtoUDP (PID1)
584#define RxProtoTCP (PID0)
585#define RxProtoIP (PID1 | PID0)
586#define RxProtoMask RxProtoIP
587
588 IPFail = (1 << 16), /* IP checksum failed */
589 UDPFail = (1 << 15), /* UDP/IP checksum failed */
590 TCPFail = (1 << 14), /* TCP/IP checksum failed */
591 RxVlanTag = (1 << 16), /* VLAN tag available */
592};
593
594#define RsvdMask 0x3fffc000
595
596struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200597 __le32 opts1;
598 __le32 opts2;
599 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600};
601
602struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200603 __le32 opts1;
604 __le32 opts2;
605 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606};
607
608struct ring_info {
609 struct sk_buff *skb;
610 u32 len;
611 u8 __pad[sizeof(void *) - sizeof(u32)];
612};
613
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200614enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200615 RTL_FEATURE_WOL = (1 << 0),
616 RTL_FEATURE_MSI = (1 << 1),
617 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200618};
619
Ivan Vecera355423d2009-02-06 21:49:57 -0800620struct rtl8169_counters {
621 __le64 tx_packets;
622 __le64 rx_packets;
623 __le64 tx_errors;
624 __le32 rx_errors;
625 __le16 rx_missed;
626 __le16 align_errors;
627 __le32 tx_one_collision;
628 __le32 tx_multi_collision;
629 __le64 rx_unicast;
630 __le64 rx_broadcast;
631 __le32 rx_multicast;
632 __le16 tx_aborted;
633 __le16 tx_underun;
634};
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636struct rtl8169_private {
637 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200638 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000639 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700640 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200641 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200642 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700643 u16 txd_version;
644 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
646 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
647 u32 dirty_rx;
648 u32 dirty_tx;
649 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
650 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
651 dma_addr_t TxPhyAddr;
652 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000653 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct timer_list timer;
656 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100657 u16 intr_event;
658 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000660
661 struct mdio_ops {
662 void (*write)(void __iomem *, int, int);
663 int (*read)(void __iomem *, int);
664 } mdio_ops;
665
françois romieu065c27c2011-01-03 15:08:12 +0000666 struct pll_power_ops {
667 void (*down)(struct rtl8169_private *);
668 void (*up)(struct rtl8169_private *);
669 } pll_power_ops;
670
Oliver Neukum54405cd2011-01-06 21:55:13 +0100671 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200672 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000673 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100674 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000675 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800677 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
David Howellsc4028952006-11-22 14:57:56 +0000678 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200679 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200680
681 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800682 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000683 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000684
Francois Romieub6ffd972011-06-17 17:00:05 +0200685 struct rtl_fw {
686 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200687
688#define RTL_VER_SIZE 32
689
690 char version[RTL_VER_SIZE];
691
692 struct rtl_fw_phy_action {
693 __le32 *code;
694 size_t size;
695 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200696 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300697#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698};
699
Ralf Baechle979b6c12005-06-13 14:30:40 -0700700MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700703MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200704module_param_named(debug, debug.msg_enable, int, 0);
705MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706MODULE_LICENSE("GPL");
707MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000708MODULE_FIRMWARE(FIRMWARE_8168D_1);
709MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000710MODULE_FIRMWARE(FIRMWARE_8168E_1);
711MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800712MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000715static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
716 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100717static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100719static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100721static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200723static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700725 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200726static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200728static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700729static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
françois romieub646d902011-01-03 15:08:21 +0000731static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
732{
733 void __iomem *ioaddr = tp->mmio_addr;
734 int i;
735
736 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
737 for (i = 0; i < 20; i++) {
738 udelay(100);
739 if (RTL_R32(OCPAR) & OCPAR_FLAG)
740 break;
741 }
742 return RTL_R32(OCPDR);
743}
744
745static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
746{
747 void __iomem *ioaddr = tp->mmio_addr;
748 int i;
749
750 RTL_W32(OCPDR, data);
751 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
752 for (i = 0; i < 20; i++) {
753 udelay(100);
754 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
755 break;
756 }
757}
758
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800759static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000760{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800761 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000762 int i;
763
764 RTL_W8(ERIDR, cmd);
765 RTL_W32(ERIAR, 0x800010e8);
766 msleep(2);
767 for (i = 0; i < 5; i++) {
768 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200769 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000770 break;
771 }
772
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800773 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000774}
775
776#define OOB_CMD_RESET 0x00
777#define OOB_CMD_DRIVER_START 0x05
778#define OOB_CMD_DRIVER_STOP 0x06
779
Francois Romieucecb5fd2011-04-01 10:21:07 +0200780static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
781{
782 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
783}
784
françois romieub646d902011-01-03 15:08:21 +0000785static void rtl8168_driver_start(struct rtl8169_private *tp)
786{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200787 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000788 int i;
789
790 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
791
Francois Romieucecb5fd2011-04-01 10:21:07 +0200792 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000793
françois romieub646d902011-01-03 15:08:21 +0000794 for (i = 0; i < 10; i++) {
795 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000796 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000797 break;
798 }
799}
800
801static void rtl8168_driver_stop(struct rtl8169_private *tp)
802{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200803 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000804 int i;
805
806 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
807
Francois Romieucecb5fd2011-04-01 10:21:07 +0200808 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000809
françois romieub646d902011-01-03 15:08:21 +0000810 for (i = 0; i < 10; i++) {
811 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000812 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000813 break;
814 }
815}
816
hayeswang4804b3b2011-03-21 01:50:29 +0000817static int r8168dp_check_dash(struct rtl8169_private *tp)
818{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200819 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000820
Francois Romieucecb5fd2011-04-01 10:21:07 +0200821 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000822}
françois romieub646d902011-01-03 15:08:21 +0000823
françois romieu4da19632011-01-03 15:07:55 +0000824static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 int i;
827
Francois Romieua6baf3a2007-11-08 23:23:21 +0100828 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Francois Romieu23714082006-01-29 00:49:09 +0100830 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100831 /*
832 * Check if the RTL8169 has completed writing to the specified
833 * MII register.
834 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200835 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
Francois Romieu23714082006-01-29 00:49:09 +0100837 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700839 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700840 * According to hardware specs a 20us delay is required after write
841 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700842 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700843 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
françois romieu4da19632011-01-03 15:07:55 +0000846static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 int i, value = -1;
849
Francois Romieua6baf3a2007-11-08 23:23:21 +0100850 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Francois Romieu23714082006-01-29 00:49:09 +0100852 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100853 /*
854 * Check if the RTL8169 has completed retrieving data from
855 * the specified MII register.
856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100858 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 break;
860 }
Francois Romieu23714082006-01-29 00:49:09 +0100861 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700863 /*
864 * According to hardware specs a 20us delay is required after read
865 * complete indication, but before sending next command.
866 */
867 udelay(20);
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return value;
870}
871
françois romieuc0e45c12011-01-03 15:08:04 +0000872static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
873{
874 int i;
875
876 RTL_W32(OCPDR, data |
877 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
878 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
879 RTL_W32(EPHY_RXER_NUM, 0);
880
881 for (i = 0; i < 100; i++) {
882 mdelay(1);
883 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
884 break;
885 }
886}
887
888static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
889{
890 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
891 (value & OCPDR_DATA_MASK));
892}
893
894static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
895{
896 int i;
897
898 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
899
900 mdelay(1);
901 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
902 RTL_W32(EPHY_RXER_NUM, 0);
903
904 for (i = 0; i < 100; i++) {
905 mdelay(1);
906 if (RTL_R32(OCPAR) & OCPAR_FLAG)
907 break;
908 }
909
910 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
911}
912
françois romieue6de30d2011-01-03 15:08:37 +0000913#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
914
915static void r8168dp_2_mdio_start(void __iomem *ioaddr)
916{
917 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
918}
919
920static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
921{
922 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
923}
924
925static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
926{
927 r8168dp_2_mdio_start(ioaddr);
928
929 r8169_mdio_write(ioaddr, reg_addr, value);
930
931 r8168dp_2_mdio_stop(ioaddr);
932}
933
934static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
935{
936 int value;
937
938 r8168dp_2_mdio_start(ioaddr);
939
940 value = r8169_mdio_read(ioaddr, reg_addr);
941
942 r8168dp_2_mdio_stop(ioaddr);
943
944 return value;
945}
946
françois romieu4da19632011-01-03 15:07:55 +0000947static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200948{
françois romieuc0e45c12011-01-03 15:08:04 +0000949 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200950}
951
françois romieu4da19632011-01-03 15:07:55 +0000952static int rtl_readphy(struct rtl8169_private *tp, int location)
953{
françois romieuc0e45c12011-01-03 15:08:04 +0000954 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000955}
956
957static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
958{
959 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
960}
961
962static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000963{
964 int val;
965
françois romieu4da19632011-01-03 15:07:55 +0000966 val = rtl_readphy(tp, reg_addr);
967 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000968}
969
Francois Romieuccdffb92008-07-26 14:26:06 +0200970static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
971 int val)
972{
973 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200974
françois romieu4da19632011-01-03 15:07:55 +0000975 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200976}
977
978static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
979{
980 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200981
françois romieu4da19632011-01-03 15:07:55 +0000982 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200983}
984
Francois Romieudacf8152008-08-02 20:44:13 +0200985static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
986{
987 unsigned int i;
988
989 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
990 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
991
992 for (i = 0; i < 100; i++) {
993 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
994 break;
995 udelay(10);
996 }
997}
998
999static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1000{
1001 u16 value = 0xffff;
1002 unsigned int i;
1003
1004 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1005
1006 for (i = 0; i < 100; i++) {
1007 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1008 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1009 break;
1010 }
1011 udelay(10);
1012 }
1013
1014 return value;
1015}
1016
1017static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1018{
1019 unsigned int i;
1020
1021 RTL_W32(CSIDR, value);
1022 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1023 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1024
1025 for (i = 0; i < 100; i++) {
1026 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1027 break;
1028 udelay(10);
1029 }
1030}
1031
1032static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1033{
1034 u32 value = ~0x00;
1035 unsigned int i;
1036
1037 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1038 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1039
1040 for (i = 0; i < 100; i++) {
1041 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1042 value = RTL_R32(CSIDR);
1043 break;
1044 }
1045 udelay(10);
1046 }
1047
1048 return value;
1049}
1050
Hayes Wang133ac402011-07-06 15:58:05 +08001051static
1052void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1053{
1054 unsigned int i;
1055
1056 BUG_ON((addr & 3) || (mask == 0));
1057 RTL_W32(ERIDR, val);
1058 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1059
1060 for (i = 0; i < 100; i++) {
1061 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1062 break;
1063 udelay(100);
1064 }
1065}
1066
1067static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1068{
1069 u32 value = ~0x00;
1070 unsigned int i;
1071
1072 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1073
1074 for (i = 0; i < 100; i++) {
1075 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1076 value = RTL_R32(ERIDR);
1077 break;
1078 }
1079 udelay(100);
1080 }
1081
1082 return value;
1083}
1084
1085static void
1086rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1087{
1088 u32 val;
1089
1090 val = rtl_eri_read(ioaddr, addr, type);
1091 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1092}
1093
françois romieudaf9df62009-10-07 12:44:20 +00001094static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1095{
1096 u8 value = 0xff;
1097 unsigned int i;
1098
1099 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1100
1101 for (i = 0; i < 300; i++) {
1102 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1103 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1104 break;
1105 }
1106 udelay(100);
1107 }
1108
1109 return value;
1110}
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1113{
1114 RTL_W16(IntrMask, 0x0000);
1115
1116 RTL_W16(IntrStatus, 0xffff);
1117}
1118
françois romieu4da19632011-01-03 15:07:55 +00001119static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
françois romieu4da19632011-01-03 15:07:55 +00001121 void __iomem *ioaddr = tp->mmio_addr;
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return RTL_R32(TBICSR) & TBIReset;
1124}
1125
françois romieu4da19632011-01-03 15:07:55 +00001126static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
françois romieu4da19632011-01-03 15:07:55 +00001128 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1132{
1133 return RTL_R32(TBICSR) & TBILinkOk;
1134}
1135
1136static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1137{
1138 return RTL_R8(PHYstatus) & LinkStatus;
1139}
1140
françois romieu4da19632011-01-03 15:07:55 +00001141static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
françois romieu4da19632011-01-03 15:07:55 +00001143 void __iomem *ioaddr = tp->mmio_addr;
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1146}
1147
françois romieu4da19632011-01-03 15:07:55 +00001148static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 unsigned int val;
1151
françois romieu4da19632011-01-03 15:07:55 +00001152 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1153 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Hayes Wang70090422011-07-06 15:58:06 +08001156static void rtl_link_chg_patch(struct rtl8169_private *tp)
1157{
1158 void __iomem *ioaddr = tp->mmio_addr;
1159 struct net_device *dev = tp->dev;
1160
1161 if (!netif_running(dev))
1162 return;
1163
1164 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1165 if (RTL_R8(PHYstatus) & _1000bpsF) {
1166 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1167 0x00000011, ERIAR_EXGMAC);
1168 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1169 0x00000005, ERIAR_EXGMAC);
1170 } else if (RTL_R8(PHYstatus) & _100bps) {
1171 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1172 0x0000001f, ERIAR_EXGMAC);
1173 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1174 0x00000005, ERIAR_EXGMAC);
1175 } else {
1176 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1177 0x0000001f, ERIAR_EXGMAC);
1178 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1179 0x0000003f, ERIAR_EXGMAC);
1180 }
1181 /* Reset packet filter */
1182 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1183 ERIAR_EXGMAC);
1184 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1185 ERIAR_EXGMAC);
1186 }
1187}
1188
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001189static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001190 struct rtl8169_private *tp,
1191 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 unsigned long flags;
1194
1195 spin_lock_irqsave(&tp->lock, flags);
1196 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001197 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001198 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001199 if (pm)
1200 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001202 if (net_ratelimit())
1203 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001204 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001206 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001207 if (pm)
1208 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 spin_unlock_irqrestore(&tp->lock, flags);
1211}
1212
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001213static void rtl8169_check_link_status(struct net_device *dev,
1214 struct rtl8169_private *tp,
1215 void __iomem *ioaddr)
1216{
1217 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1218}
1219
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001220#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1221
1222static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1223{
1224 void __iomem *ioaddr = tp->mmio_addr;
1225 u8 options;
1226 u32 wolopts = 0;
1227
1228 options = RTL_R8(Config1);
1229 if (!(options & PMEnable))
1230 return 0;
1231
1232 options = RTL_R8(Config3);
1233 if (options & LinkUp)
1234 wolopts |= WAKE_PHY;
1235 if (options & MagicPacket)
1236 wolopts |= WAKE_MAGIC;
1237
1238 options = RTL_R8(Config5);
1239 if (options & UWF)
1240 wolopts |= WAKE_UCAST;
1241 if (options & BWF)
1242 wolopts |= WAKE_BCAST;
1243 if (options & MWF)
1244 wolopts |= WAKE_MCAST;
1245
1246 return wolopts;
1247}
1248
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001249static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1250{
1251 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001252
1253 spin_lock_irq(&tp->lock);
1254
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001255 wol->supported = WAKE_ANY;
1256 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001257
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001258 spin_unlock_irq(&tp->lock);
1259}
1260
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001261static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001262{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001263 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001264 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001265 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001266 u32 opt;
1267 u16 reg;
1268 u8 mask;
1269 } cfg[] = {
1270 { WAKE_ANY, Config1, PMEnable },
1271 { WAKE_PHY, Config3, LinkUp },
1272 { WAKE_MAGIC, Config3, MagicPacket },
1273 { WAKE_UCAST, Config5, UWF },
1274 { WAKE_BCAST, Config5, BWF },
1275 { WAKE_MCAST, Config5, MWF },
1276 { WAKE_ANY, Config5, LanWake }
1277 };
1278
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001279 RTL_W8(Cfg9346, Cfg9346_Unlock);
1280
1281 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1282 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001283 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001284 options |= cfg[i].mask;
1285 RTL_W8(cfg[i].reg, options);
1286 }
1287
1288 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001289}
1290
1291static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1292{
1293 struct rtl8169_private *tp = netdev_priv(dev);
1294
1295 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001296
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001297 if (wol->wolopts)
1298 tp->features |= RTL_FEATURE_WOL;
1299 else
1300 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001301 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001302 spin_unlock_irq(&tp->lock);
1303
françois romieuea809072010-11-08 13:23:58 +00001304 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1305
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001306 return 0;
1307}
1308
Francois Romieu31bd2042011-04-26 18:58:59 +02001309static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1310{
Francois Romieu85bffe62011-04-27 08:22:39 +02001311 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001312}
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314static void rtl8169_get_drvinfo(struct net_device *dev,
1315 struct ethtool_drvinfo *info)
1316{
1317 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001318 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 strcpy(info->driver, MODULENAME);
1321 strcpy(info->version, RTL8169_VERSION);
1322 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001323 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1324 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1325 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328static int rtl8169_get_regs_len(struct net_device *dev)
1329{
1330 return R8169_REGS_SIZE;
1331}
1332
1333static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001334 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 struct rtl8169_private *tp = netdev_priv(dev);
1337 void __iomem *ioaddr = tp->mmio_addr;
1338 int ret = 0;
1339 u32 reg;
1340
1341 reg = RTL_R32(TBICSR);
1342 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1343 (duplex == DUPLEX_FULL)) {
1344 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1345 } else if (autoneg == AUTONEG_ENABLE)
1346 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1347 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001348 netif_warn(tp, link, dev,
1349 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 ret = -EOPNOTSUPP;
1351 }
1352
1353 return ret;
1354}
1355
1356static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001357 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001360 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001361 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Hayes Wang716b50a2011-02-22 17:26:18 +08001363 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001366 int auto_nego;
1367
françois romieu4da19632011-01-03 15:07:55 +00001368 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001369 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1370 ADVERTISE_100HALF | ADVERTISE_100FULL);
1371
1372 if (adv & ADVERTISED_10baseT_Half)
1373 auto_nego |= ADVERTISE_10HALF;
1374 if (adv & ADVERTISED_10baseT_Full)
1375 auto_nego |= ADVERTISE_10FULL;
1376 if (adv & ADVERTISED_100baseT_Half)
1377 auto_nego |= ADVERTISE_100HALF;
1378 if (adv & ADVERTISED_100baseT_Full)
1379 auto_nego |= ADVERTISE_100FULL;
1380
françois romieu3577aa12009-05-19 10:46:48 +00001381 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1382
françois romieu4da19632011-01-03 15:07:55 +00001383 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001384 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1385
1386 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001387 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001388 if (adv & ADVERTISED_1000baseT_Half)
1389 giga_ctrl |= ADVERTISE_1000HALF;
1390 if (adv & ADVERTISED_1000baseT_Full)
1391 giga_ctrl |= ADVERTISE_1000FULL;
1392 } else if (adv & (ADVERTISED_1000baseT_Half |
1393 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001394 netif_info(tp, link, dev,
1395 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001396 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
françois romieu3577aa12009-05-19 10:46:48 +00001399 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001400
françois romieu4da19632011-01-03 15:07:55 +00001401 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1402 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001403 } else {
1404 giga_ctrl = 0;
1405
1406 if (speed == SPEED_10)
1407 bmcr = 0;
1408 else if (speed == SPEED_100)
1409 bmcr = BMCR_SPEED100;
1410 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001411 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001412
1413 if (duplex == DUPLEX_FULL)
1414 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001415 }
1416
françois romieu4da19632011-01-03 15:07:55 +00001417 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001418
Francois Romieucecb5fd2011-04-01 10:21:07 +02001419 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1420 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001421 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001422 rtl_writephy(tp, 0x17, 0x2138);
1423 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001424 } else {
françois romieu4da19632011-01-03 15:07:55 +00001425 rtl_writephy(tp, 0x17, 0x2108);
1426 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001427 }
1428 }
1429
Oliver Neukum54405cd2011-01-06 21:55:13 +01001430 rc = 0;
1431out:
1432 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001436 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct rtl8169_private *tp = netdev_priv(dev);
1439 int ret;
1440
Oliver Neukum54405cd2011-01-06 21:55:13 +01001441 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001442 if (ret < 0)
1443 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Francois Romieu4876cc12011-03-11 21:07:11 +01001445 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1446 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001448 }
1449out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return ret;
1451}
1452
1453static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1454{
1455 struct rtl8169_private *tp = netdev_priv(dev);
1456 unsigned long flags;
1457 int ret;
1458
Francois Romieu4876cc12011-03-11 21:07:11 +01001459 del_timer_sync(&tp->timer);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001462 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001463 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return ret;
1467}
1468
Michał Mirosław350fb322011-04-08 06:35:56 +00001469static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001471 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001472 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Michał Mirosław350fb322011-04-08 06:35:56 +00001474 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Michał Mirosław350fb322011-04-08 06:35:56 +00001477static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct rtl8169_private *tp = netdev_priv(dev);
1480 void __iomem *ioaddr = tp->mmio_addr;
1481 unsigned long flags;
1482
1483 spin_lock_irqsave(&tp->lock, flags);
1484
Michał Mirosław350fb322011-04-08 06:35:56 +00001485 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 tp->cp_cmd |= RxChkSum;
1487 else
1488 tp->cp_cmd &= ~RxChkSum;
1489
Michał Mirosław350fb322011-04-08 06:35:56 +00001490 if (dev->features & NETIF_F_HW_VLAN_RX)
1491 tp->cp_cmd |= RxVlan;
1492 else
1493 tp->cp_cmd &= ~RxVlan;
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 RTL_W16(CPlusCmd, tp->cp_cmd);
1496 RTL_R16(CPlusCmd);
1497
1498 spin_unlock_irqrestore(&tp->lock, flags);
1499
1500 return 0;
1501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1504 struct sk_buff *skb)
1505{
Jesse Grosseab6d182010-10-20 13:56:03 +00001506 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1508}
1509
Francois Romieu7a8fc772011-03-01 17:18:33 +01001510static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Francois Romieu7a8fc772011-03-01 17:18:33 +01001514 if (opts2 & RxVlanTag)
1515 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
Francois Romieuccdffb92008-07-26 14:26:06 +02001520static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct rtl8169_private *tp = netdev_priv(dev);
1523 void __iomem *ioaddr = tp->mmio_addr;
1524 u32 status;
1525
1526 cmd->supported =
1527 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1528 cmd->port = PORT_FIBRE;
1529 cmd->transceiver = XCVR_INTERNAL;
1530
1531 status = RTL_R32(TBICSR);
1532 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1533 cmd->autoneg = !!(status & TBINwEnable);
1534
David Decotigny70739492011-04-27 18:32:40 +00001535 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001537
1538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Francois Romieuccdffb92008-07-26 14:26:06 +02001541static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Francois Romieuccdffb92008-07-26 14:26:06 +02001545 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1549{
1550 struct rtl8169_private *tp = netdev_priv(dev);
1551 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001552 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 spin_lock_irqsave(&tp->lock, flags);
1555
Francois Romieuccdffb92008-07-26 14:26:06 +02001556 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001559 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
1562static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1563 void *p)
1564{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001565 struct rtl8169_private *tp = netdev_priv(dev);
1566 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Francois Romieu5b0384f2006-08-16 16:00:01 +02001568 if (regs->len > R8169_REGS_SIZE)
1569 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Francois Romieu5b0384f2006-08-16 16:00:01 +02001571 spin_lock_irqsave(&tp->lock, flags);
1572 memcpy_fromio(p, tp->mmio_addr, regs->len);
1573 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001576static u32 rtl8169_get_msglevel(struct net_device *dev)
1577{
1578 struct rtl8169_private *tp = netdev_priv(dev);
1579
1580 return tp->msg_enable;
1581}
1582
1583static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1584{
1585 struct rtl8169_private *tp = netdev_priv(dev);
1586
1587 tp->msg_enable = value;
1588}
1589
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001590static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1591 "tx_packets",
1592 "rx_packets",
1593 "tx_errors",
1594 "rx_errors",
1595 "rx_missed",
1596 "align_errors",
1597 "tx_single_collisions",
1598 "tx_multi_collisions",
1599 "unicast",
1600 "broadcast",
1601 "multicast",
1602 "tx_aborted",
1603 "tx_underrun",
1604};
1605
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001606static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001607{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001608 switch (sset) {
1609 case ETH_SS_STATS:
1610 return ARRAY_SIZE(rtl8169_gstrings);
1611 default:
1612 return -EOPNOTSUPP;
1613 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001614}
1615
Ivan Vecera355423d2009-02-06 21:49:57 -08001616static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001617{
1618 struct rtl8169_private *tp = netdev_priv(dev);
1619 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001620 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001621 struct rtl8169_counters *counters;
1622 dma_addr_t paddr;
1623 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001624 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001625
Ivan Vecera355423d2009-02-06 21:49:57 -08001626 /*
1627 * Some chips are unable to dump tally counters when the receiver
1628 * is disabled.
1629 */
1630 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1631 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001632
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001633 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001634 if (!counters)
1635 return;
1636
1637 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001638 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001639 RTL_W32(CounterAddrLow, cmd);
1640 RTL_W32(CounterAddrLow, cmd | CounterDump);
1641
Ivan Vecera355423d2009-02-06 21:49:57 -08001642 while (wait--) {
1643 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001644 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001645 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001646 }
1647 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001648 }
1649
1650 RTL_W32(CounterAddrLow, 0);
1651 RTL_W32(CounterAddrHigh, 0);
1652
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001653 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001654}
1655
Ivan Vecera355423d2009-02-06 21:49:57 -08001656static void rtl8169_get_ethtool_stats(struct net_device *dev,
1657 struct ethtool_stats *stats, u64 *data)
1658{
1659 struct rtl8169_private *tp = netdev_priv(dev);
1660
1661 ASSERT_RTNL();
1662
1663 rtl8169_update_counters(dev);
1664
1665 data[0] = le64_to_cpu(tp->counters.tx_packets);
1666 data[1] = le64_to_cpu(tp->counters.rx_packets);
1667 data[2] = le64_to_cpu(tp->counters.tx_errors);
1668 data[3] = le32_to_cpu(tp->counters.rx_errors);
1669 data[4] = le16_to_cpu(tp->counters.rx_missed);
1670 data[5] = le16_to_cpu(tp->counters.align_errors);
1671 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1672 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1673 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1674 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1675 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1676 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1677 data[12] = le16_to_cpu(tp->counters.tx_underun);
1678}
1679
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001680static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1681{
1682 switch(stringset) {
1683 case ETH_SS_STATS:
1684 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1685 break;
1686 }
1687}
1688
Jeff Garzik7282d492006-09-13 14:30:00 -04001689static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 .get_drvinfo = rtl8169_get_drvinfo,
1691 .get_regs_len = rtl8169_get_regs_len,
1692 .get_link = ethtool_op_get_link,
1693 .get_settings = rtl8169_get_settings,
1694 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001695 .get_msglevel = rtl8169_get_msglevel,
1696 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001698 .get_wol = rtl8169_get_wol,
1699 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001700 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001701 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001702 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703};
1704
Francois Romieu07d3f512007-02-21 22:40:46 +01001705static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001706 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Francois Romieu5d320a22011-05-08 17:47:36 +02001708 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001709 /*
1710 * The driver currently handles the 8168Bf and the 8168Be identically
1711 * but they can be identified more specifically through the test below
1712 * if needed:
1713 *
1714 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001715 *
1716 * Same thing for the 8101Eb and the 8101Ec:
1717 *
1718 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001719 */
Francois Romieu37441002011-06-17 22:58:54 +02001720 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001722 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int mac_version;
1724 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001725 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001726 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001727 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1728 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1729 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1730
Francois Romieu5b538df2008-07-20 16:22:45 +02001731 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001732 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1733 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001734 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001735
françois romieue6de30d2011-01-03 15:08:37 +00001736 /* 8168DP family. */
1737 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1738 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001739 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001740
Francois Romieuef808d52008-06-29 13:10:54 +02001741 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001742 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001743 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001744 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001745 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001746 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1747 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001748 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001749 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001750 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001751
1752 /* 8168B family. */
1753 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1754 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1755 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1756 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1757
1758 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001759 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001760 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1761 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1762 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001763 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1764 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1765 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1766 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1767 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1768 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001769 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001770 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001771 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001772 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1773 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001774 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1775 /* FIXME: where did these entries come from ? -- FR */
1776 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1777 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1778
1779 /* 8110 family. */
1780 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1781 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1782 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1783 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1784 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1785 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1786
Jean Delvaref21b75e2009-05-26 20:54:48 -07001787 /* Catch-all */
1788 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001789 };
1790 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 u32 reg;
1792
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001793 reg = RTL_R32(TxConfig);
1794 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 p++;
1796 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001797
1798 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1799 netif_notice(tp, probe, dev,
1800 "unknown MAC, using family default\n");
1801 tp->mac_version = default_version;
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
1805static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1806{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001807 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
Francois Romieu867763c2007-08-17 18:21:58 +02001810struct phy_reg {
1811 u16 reg;
1812 u16 val;
1813};
1814
françois romieu4da19632011-01-03 15:07:55 +00001815static void rtl_writephy_batch(struct rtl8169_private *tp,
1816 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001817{
1818 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001819 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001820 regs++;
1821 }
1822}
1823
françois romieubca03d52011-01-03 15:07:31 +00001824#define PHY_READ 0x00000000
1825#define PHY_DATA_OR 0x10000000
1826#define PHY_DATA_AND 0x20000000
1827#define PHY_BJMPN 0x30000000
1828#define PHY_READ_EFUSE 0x40000000
1829#define PHY_READ_MAC_BYTE 0x50000000
1830#define PHY_WRITE_MAC_BYTE 0x60000000
1831#define PHY_CLEAR_READCOUNT 0x70000000
1832#define PHY_WRITE 0x80000000
1833#define PHY_READCOUNT_EQ_SKIP 0x90000000
1834#define PHY_COMP_EQ_SKIPN 0xa0000000
1835#define PHY_COMP_NEQ_SKIPN 0xb0000000
1836#define PHY_WRITE_PREVIOUS 0xc0000000
1837#define PHY_SKIPN 0xd0000000
1838#define PHY_DELAY_MS 0xe0000000
1839#define PHY_WRITE_ERI_WORD 0xf0000000
1840
Hayes Wang960aee62011-06-18 11:37:48 +02001841struct fw_info {
1842 u32 magic;
1843 char version[RTL_VER_SIZE];
1844 __le32 fw_start;
1845 __le32 fw_len;
1846 u8 chksum;
1847} __packed;
1848
Francois Romieu1c361ef2011-06-17 17:16:24 +02001849#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1850
1851static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001852{
Francois Romieub6ffd972011-06-17 17:00:05 +02001853 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001854 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001855 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1856 char *version = rtl_fw->version;
1857 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001858
Francois Romieu1c361ef2011-06-17 17:16:24 +02001859 if (fw->size < FW_OPCODE_SIZE)
1860 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001861
1862 if (!fw_info->magic) {
1863 size_t i, size, start;
1864 u8 checksum = 0;
1865
1866 if (fw->size < sizeof(*fw_info))
1867 goto out;
1868
1869 for (i = 0; i < fw->size; i++)
1870 checksum += fw->data[i];
1871 if (checksum != 0)
1872 goto out;
1873
1874 start = le32_to_cpu(fw_info->fw_start);
1875 if (start > fw->size)
1876 goto out;
1877
1878 size = le32_to_cpu(fw_info->fw_len);
1879 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1880 goto out;
1881
1882 memcpy(version, fw_info->version, RTL_VER_SIZE);
1883
1884 pa->code = (__le32 *)(fw->data + start);
1885 pa->size = size;
1886 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001887 if (fw->size % FW_OPCODE_SIZE)
1888 goto out;
1889
1890 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1891
1892 pa->code = (__le32 *)fw->data;
1893 pa->size = fw->size / FW_OPCODE_SIZE;
1894 }
1895 version[RTL_VER_SIZE - 1] = 0;
1896
1897 rc = true;
1898out:
1899 return rc;
1900}
1901
Francois Romieufd112f22011-06-18 00:10:29 +02001902static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1903 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02001904{
Francois Romieufd112f22011-06-18 00:10:29 +02001905 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001906 size_t index;
1907
Francois Romieu1c361ef2011-06-17 17:16:24 +02001908 for (index = 0; index < pa->size; index++) {
1909 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00001910 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001911
hayeswang42b82dc2011-01-10 02:07:25 +00001912 switch(action & 0xf0000000) {
1913 case PHY_READ:
1914 case PHY_DATA_OR:
1915 case PHY_DATA_AND:
1916 case PHY_READ_EFUSE:
1917 case PHY_CLEAR_READCOUNT:
1918 case PHY_WRITE:
1919 case PHY_WRITE_PREVIOUS:
1920 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001921 break;
1922
hayeswang42b82dc2011-01-10 02:07:25 +00001923 case PHY_BJMPN:
1924 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02001925 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001926 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001927 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001928 }
1929 break;
1930 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001931 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001932 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001933 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001934 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001935 }
1936 break;
1937 case PHY_COMP_EQ_SKIPN:
1938 case PHY_COMP_NEQ_SKIPN:
1939 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001940 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001941 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001942 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001943 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001944 }
1945 break;
1946
1947 case PHY_READ_MAC_BYTE:
1948 case PHY_WRITE_MAC_BYTE:
1949 case PHY_WRITE_ERI_WORD:
1950 default:
Francois Romieufd112f22011-06-18 00:10:29 +02001951 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00001952 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02001953 goto out;
françois romieubca03d52011-01-03 15:07:31 +00001954 }
1955 }
Francois Romieufd112f22011-06-18 00:10:29 +02001956 rc = true;
1957out:
1958 return rc;
1959}
françois romieubca03d52011-01-03 15:07:31 +00001960
Francois Romieufd112f22011-06-18 00:10:29 +02001961static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1962{
1963 struct net_device *dev = tp->dev;
1964 int rc = -EINVAL;
1965
1966 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1967 netif_err(tp, ifup, dev, "invalid firwmare\n");
1968 goto out;
1969 }
1970
1971 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1972 rc = 0;
1973out:
1974 return rc;
1975}
1976
1977static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1978{
1979 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1980 u32 predata, count;
1981 size_t index;
1982
1983 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00001984
Francois Romieu1c361ef2011-06-17 17:16:24 +02001985 for (index = 0; index < pa->size; ) {
1986 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00001987 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001988 u32 regno = (action & 0x0fff0000) >> 16;
1989
1990 if (!action)
1991 break;
françois romieubca03d52011-01-03 15:07:31 +00001992
1993 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001994 case PHY_READ:
1995 predata = rtl_readphy(tp, regno);
1996 count++;
1997 index++;
françois romieubca03d52011-01-03 15:07:31 +00001998 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001999 case PHY_DATA_OR:
2000 predata |= data;
2001 index++;
2002 break;
2003 case PHY_DATA_AND:
2004 predata &= data;
2005 index++;
2006 break;
2007 case PHY_BJMPN:
2008 index -= regno;
2009 break;
2010 case PHY_READ_EFUSE:
2011 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2012 index++;
2013 break;
2014 case PHY_CLEAR_READCOUNT:
2015 count = 0;
2016 index++;
2017 break;
2018 case PHY_WRITE:
2019 rtl_writephy(tp, regno, data);
2020 index++;
2021 break;
2022 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002023 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002024 break;
2025 case PHY_COMP_EQ_SKIPN:
2026 if (predata == data)
2027 index += regno;
2028 index++;
2029 break;
2030 case PHY_COMP_NEQ_SKIPN:
2031 if (predata != data)
2032 index += regno;
2033 index++;
2034 break;
2035 case PHY_WRITE_PREVIOUS:
2036 rtl_writephy(tp, regno, predata);
2037 index++;
2038 break;
2039 case PHY_SKIPN:
2040 index += regno + 1;
2041 break;
2042 case PHY_DELAY_MS:
2043 mdelay(data);
2044 index++;
2045 break;
2046
2047 case PHY_READ_MAC_BYTE:
2048 case PHY_WRITE_MAC_BYTE:
2049 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002050 default:
2051 BUG();
2052 }
2053 }
2054}
2055
françois romieuf1e02ed2011-01-13 13:07:53 +00002056static void rtl_release_firmware(struct rtl8169_private *tp)
2057{
Francois Romieub6ffd972011-06-17 17:00:05 +02002058 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2059 release_firmware(tp->rtl_fw->fw);
2060 kfree(tp->rtl_fw);
2061 }
2062 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002063}
2064
François Romieu953a12c2011-04-24 17:38:48 +02002065static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002066{
Francois Romieub6ffd972011-06-17 17:00:05 +02002067 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002068
2069 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002070 if (!IS_ERR_OR_NULL(rtl_fw))
2071 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002072}
2073
2074static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2075{
2076 if (rtl_readphy(tp, reg) != val)
2077 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2078 else
2079 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002080}
2081
françois romieu4da19632011-01-03 15:07:55 +00002082static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002084 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002085 { 0x1f, 0x0001 },
2086 { 0x06, 0x006e },
2087 { 0x08, 0x0708 },
2088 { 0x15, 0x4000 },
2089 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
françois romieu0b9b5712009-08-10 19:44:56 +00002091 { 0x1f, 0x0001 },
2092 { 0x03, 0x00a1 },
2093 { 0x02, 0x0008 },
2094 { 0x01, 0x0120 },
2095 { 0x00, 0x1000 },
2096 { 0x04, 0x0800 },
2097 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
françois romieu0b9b5712009-08-10 19:44:56 +00002099 { 0x03, 0xff41 },
2100 { 0x02, 0xdf60 },
2101 { 0x01, 0x0140 },
2102 { 0x00, 0x0077 },
2103 { 0x04, 0x7800 },
2104 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
françois romieu0b9b5712009-08-10 19:44:56 +00002106 { 0x03, 0x802f },
2107 { 0x02, 0x4f02 },
2108 { 0x01, 0x0409 },
2109 { 0x00, 0xf0f9 },
2110 { 0x04, 0x9800 },
2111 { 0x04, 0x9000 },
2112
2113 { 0x03, 0xdf01 },
2114 { 0x02, 0xdf20 },
2115 { 0x01, 0xff95 },
2116 { 0x00, 0xba00 },
2117 { 0x04, 0xa800 },
2118 { 0x04, 0xa000 },
2119
2120 { 0x03, 0xff41 },
2121 { 0x02, 0xdf20 },
2122 { 0x01, 0x0140 },
2123 { 0x00, 0x00bb },
2124 { 0x04, 0xb800 },
2125 { 0x04, 0xb000 },
2126
2127 { 0x03, 0xdf41 },
2128 { 0x02, 0xdc60 },
2129 { 0x01, 0x6340 },
2130 { 0x00, 0x007d },
2131 { 0x04, 0xd800 },
2132 { 0x04, 0xd000 },
2133
2134 { 0x03, 0xdf01 },
2135 { 0x02, 0xdf20 },
2136 { 0x01, 0x100a },
2137 { 0x00, 0xa0ff },
2138 { 0x04, 0xf800 },
2139 { 0x04, 0xf000 },
2140
2141 { 0x1f, 0x0000 },
2142 { 0x0b, 0x0000 },
2143 { 0x00, 0x9200 }
2144 };
2145
françois romieu4da19632011-01-03 15:07:55 +00002146 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
françois romieu4da19632011-01-03 15:07:55 +00002149static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002150{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002151 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002152 { 0x1f, 0x0002 },
2153 { 0x01, 0x90d0 },
2154 { 0x1f, 0x0000 }
2155 };
2156
françois romieu4da19632011-01-03 15:07:55 +00002157 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002158}
2159
françois romieu4da19632011-01-03 15:07:55 +00002160static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002161{
2162 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002163
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002164 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2165 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002166 return;
2167
françois romieu4da19632011-01-03 15:07:55 +00002168 rtl_writephy(tp, 0x1f, 0x0001);
2169 rtl_writephy(tp, 0x10, 0xf01b);
2170 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002171}
2172
françois romieu4da19632011-01-03 15:07:55 +00002173static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002174{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002175 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002176 { 0x1f, 0x0001 },
2177 { 0x04, 0x0000 },
2178 { 0x03, 0x00a1 },
2179 { 0x02, 0x0008 },
2180 { 0x01, 0x0120 },
2181 { 0x00, 0x1000 },
2182 { 0x04, 0x0800 },
2183 { 0x04, 0x9000 },
2184 { 0x03, 0x802f },
2185 { 0x02, 0x4f02 },
2186 { 0x01, 0x0409 },
2187 { 0x00, 0xf099 },
2188 { 0x04, 0x9800 },
2189 { 0x04, 0xa000 },
2190 { 0x03, 0xdf01 },
2191 { 0x02, 0xdf20 },
2192 { 0x01, 0xff95 },
2193 { 0x00, 0xba00 },
2194 { 0x04, 0xa800 },
2195 { 0x04, 0xf000 },
2196 { 0x03, 0xdf01 },
2197 { 0x02, 0xdf20 },
2198 { 0x01, 0x101a },
2199 { 0x00, 0xa0ff },
2200 { 0x04, 0xf800 },
2201 { 0x04, 0x0000 },
2202 { 0x1f, 0x0000 },
2203
2204 { 0x1f, 0x0001 },
2205 { 0x10, 0xf41b },
2206 { 0x14, 0xfb54 },
2207 { 0x18, 0xf5c7 },
2208 { 0x1f, 0x0000 },
2209
2210 { 0x1f, 0x0001 },
2211 { 0x17, 0x0cc0 },
2212 { 0x1f, 0x0000 }
2213 };
2214
françois romieu4da19632011-01-03 15:07:55 +00002215 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002216
françois romieu4da19632011-01-03 15:07:55 +00002217 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002218}
2219
françois romieu4da19632011-01-03 15:07:55 +00002220static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002221{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002222 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002223 { 0x1f, 0x0001 },
2224 { 0x04, 0x0000 },
2225 { 0x03, 0x00a1 },
2226 { 0x02, 0x0008 },
2227 { 0x01, 0x0120 },
2228 { 0x00, 0x1000 },
2229 { 0x04, 0x0800 },
2230 { 0x04, 0x9000 },
2231 { 0x03, 0x802f },
2232 { 0x02, 0x4f02 },
2233 { 0x01, 0x0409 },
2234 { 0x00, 0xf099 },
2235 { 0x04, 0x9800 },
2236 { 0x04, 0xa000 },
2237 { 0x03, 0xdf01 },
2238 { 0x02, 0xdf20 },
2239 { 0x01, 0xff95 },
2240 { 0x00, 0xba00 },
2241 { 0x04, 0xa800 },
2242 { 0x04, 0xf000 },
2243 { 0x03, 0xdf01 },
2244 { 0x02, 0xdf20 },
2245 { 0x01, 0x101a },
2246 { 0x00, 0xa0ff },
2247 { 0x04, 0xf800 },
2248 { 0x04, 0x0000 },
2249 { 0x1f, 0x0000 },
2250
2251 { 0x1f, 0x0001 },
2252 { 0x0b, 0x8480 },
2253 { 0x1f, 0x0000 },
2254
2255 { 0x1f, 0x0001 },
2256 { 0x18, 0x67c7 },
2257 { 0x04, 0x2000 },
2258 { 0x03, 0x002f },
2259 { 0x02, 0x4360 },
2260 { 0x01, 0x0109 },
2261 { 0x00, 0x3022 },
2262 { 0x04, 0x2800 },
2263 { 0x1f, 0x0000 },
2264
2265 { 0x1f, 0x0001 },
2266 { 0x17, 0x0cc0 },
2267 { 0x1f, 0x0000 }
2268 };
2269
françois romieu4da19632011-01-03 15:07:55 +00002270 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002271}
2272
françois romieu4da19632011-01-03 15:07:55 +00002273static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002274{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002275 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002276 { 0x10, 0xf41b },
2277 { 0x1f, 0x0000 }
2278 };
2279
françois romieu4da19632011-01-03 15:07:55 +00002280 rtl_writephy(tp, 0x1f, 0x0001);
2281 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002282
françois romieu4da19632011-01-03 15:07:55 +00002283 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002284}
2285
françois romieu4da19632011-01-03 15:07:55 +00002286static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002287{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002288 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002289 { 0x1f, 0x0001 },
2290 { 0x10, 0xf41b },
2291 { 0x1f, 0x0000 }
2292 };
2293
françois romieu4da19632011-01-03 15:07:55 +00002294 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002295}
2296
françois romieu4da19632011-01-03 15:07:55 +00002297static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002298{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002299 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002300 { 0x1f, 0x0000 },
2301 { 0x1d, 0x0f00 },
2302 { 0x1f, 0x0002 },
2303 { 0x0c, 0x1ec8 },
2304 { 0x1f, 0x0000 }
2305 };
2306
françois romieu4da19632011-01-03 15:07:55 +00002307 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002308}
2309
françois romieu4da19632011-01-03 15:07:55 +00002310static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002311{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002312 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002313 { 0x1f, 0x0001 },
2314 { 0x1d, 0x3d98 },
2315 { 0x1f, 0x0000 }
2316 };
2317
françois romieu4da19632011-01-03 15:07:55 +00002318 rtl_writephy(tp, 0x1f, 0x0000);
2319 rtl_patchphy(tp, 0x14, 1 << 5);
2320 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002321
françois romieu4da19632011-01-03 15:07:55 +00002322 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002323}
2324
françois romieu4da19632011-01-03 15:07:55 +00002325static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002326{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002327 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002328 { 0x1f, 0x0001 },
2329 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002330 { 0x1f, 0x0002 },
2331 { 0x00, 0x88d4 },
2332 { 0x01, 0x82b1 },
2333 { 0x03, 0x7002 },
2334 { 0x08, 0x9e30 },
2335 { 0x09, 0x01f0 },
2336 { 0x0a, 0x5500 },
2337 { 0x0c, 0x00c8 },
2338 { 0x1f, 0x0003 },
2339 { 0x12, 0xc096 },
2340 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002341 { 0x1f, 0x0000 },
2342 { 0x1f, 0x0000 },
2343 { 0x09, 0x2000 },
2344 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002345 };
2346
françois romieu4da19632011-01-03 15:07:55 +00002347 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002348
françois romieu4da19632011-01-03 15:07:55 +00002349 rtl_patchphy(tp, 0x14, 1 << 5);
2350 rtl_patchphy(tp, 0x0d, 1 << 5);
2351 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002352}
2353
françois romieu4da19632011-01-03 15:07:55 +00002354static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002355{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002356 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002357 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002358 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002359 { 0x03, 0x802f },
2360 { 0x02, 0x4f02 },
2361 { 0x01, 0x0409 },
2362 { 0x00, 0xf099 },
2363 { 0x04, 0x9800 },
2364 { 0x04, 0x9000 },
2365 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002366 { 0x1f, 0x0002 },
2367 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002368 { 0x06, 0x0761 },
2369 { 0x1f, 0x0003 },
2370 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002371 { 0x1f, 0x0000 }
2372 };
2373
françois romieu4da19632011-01-03 15:07:55 +00002374 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002375
françois romieu4da19632011-01-03 15:07:55 +00002376 rtl_patchphy(tp, 0x16, 1 << 0);
2377 rtl_patchphy(tp, 0x14, 1 << 5);
2378 rtl_patchphy(tp, 0x0d, 1 << 5);
2379 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002380}
2381
françois romieu4da19632011-01-03 15:07:55 +00002382static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002383{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002384 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002385 { 0x1f, 0x0001 },
2386 { 0x12, 0x2300 },
2387 { 0x1d, 0x3d98 },
2388 { 0x1f, 0x0002 },
2389 { 0x0c, 0x7eb8 },
2390 { 0x06, 0x5461 },
2391 { 0x1f, 0x0003 },
2392 { 0x16, 0x0f0a },
2393 { 0x1f, 0x0000 }
2394 };
2395
françois romieu4da19632011-01-03 15:07:55 +00002396 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002397
françois romieu4da19632011-01-03 15:07:55 +00002398 rtl_patchphy(tp, 0x16, 1 << 0);
2399 rtl_patchphy(tp, 0x14, 1 << 5);
2400 rtl_patchphy(tp, 0x0d, 1 << 5);
2401 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002402}
2403
françois romieu4da19632011-01-03 15:07:55 +00002404static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002405{
françois romieu4da19632011-01-03 15:07:55 +00002406 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002407}
2408
françois romieubca03d52011-01-03 15:07:31 +00002409static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002410{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002411 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002412 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002413 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002414 { 0x06, 0x4064 },
2415 { 0x07, 0x2863 },
2416 { 0x08, 0x059c },
2417 { 0x09, 0x26b4 },
2418 { 0x0a, 0x6a19 },
2419 { 0x0b, 0xdcc8 },
2420 { 0x10, 0xf06d },
2421 { 0x14, 0x7f68 },
2422 { 0x18, 0x7fd9 },
2423 { 0x1c, 0xf0ff },
2424 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002425 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002426 { 0x12, 0xf49f },
2427 { 0x13, 0x070b },
2428 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002429 { 0x14, 0x94c0 },
2430
2431 /*
2432 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002433 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002434 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002435 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002436 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002437 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002438 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002439 { 0x06, 0x5561 },
2440
2441 /*
2442 * Can not link to 1Gbps with bad cable
2443 * Decrease SNR threshold form 21.07dB to 19.04dB
2444 */
2445 { 0x1f, 0x0001 },
2446 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002447
2448 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002449 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002450 };
françois romieubca03d52011-01-03 15:07:31 +00002451 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002452
françois romieu4da19632011-01-03 15:07:55 +00002453 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002454
françois romieubca03d52011-01-03 15:07:31 +00002455 /*
2456 * Rx Error Issue
2457 * Fine Tune Switching regulator parameter
2458 */
françois romieu4da19632011-01-03 15:07:55 +00002459 rtl_writephy(tp, 0x1f, 0x0002);
2460 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2461 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002462
françois romieudaf9df62009-10-07 12:44:20 +00002463 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002464 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002465 { 0x1f, 0x0002 },
2466 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002467 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002468 { 0x05, 0x8330 },
2469 { 0x06, 0x669a },
2470 { 0x1f, 0x0002 }
2471 };
2472 int val;
2473
françois romieu4da19632011-01-03 15:07:55 +00002474 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002475
françois romieu4da19632011-01-03 15:07:55 +00002476 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002477
2478 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002479 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002480 0x0065, 0x0066, 0x0067, 0x0068,
2481 0x0069, 0x006a, 0x006b, 0x006c
2482 };
2483 int i;
2484
françois romieu4da19632011-01-03 15:07:55 +00002485 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002486
2487 val &= 0xff00;
2488 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002489 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002490 }
2491 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002492 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002493 { 0x1f, 0x0002 },
2494 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002495 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002496 { 0x05, 0x8330 },
2497 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002498 };
2499
françois romieu4da19632011-01-03 15:07:55 +00002500 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002501 }
2502
françois romieubca03d52011-01-03 15:07:31 +00002503 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002504 rtl_writephy(tp, 0x1f, 0x0002);
2505 rtl_patchphy(tp, 0x0d, 0x0300);
2506 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002507
françois romieubca03d52011-01-03 15:07:31 +00002508 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002509 rtl_writephy(tp, 0x1f, 0x0002);
2510 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2511 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002512
françois romieu4da19632011-01-03 15:07:55 +00002513 rtl_writephy(tp, 0x1f, 0x0005);
2514 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002515
2516 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002517
françois romieu4da19632011-01-03 15:07:55 +00002518 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002519}
2520
françois romieubca03d52011-01-03 15:07:31 +00002521static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002522{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002523 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002524 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002525 { 0x1f, 0x0001 },
2526 { 0x06, 0x4064 },
2527 { 0x07, 0x2863 },
2528 { 0x08, 0x059c },
2529 { 0x09, 0x26b4 },
2530 { 0x0a, 0x6a19 },
2531 { 0x0b, 0xdcc8 },
2532 { 0x10, 0xf06d },
2533 { 0x14, 0x7f68 },
2534 { 0x18, 0x7fd9 },
2535 { 0x1c, 0xf0ff },
2536 { 0x1d, 0x3d9c },
2537 { 0x1f, 0x0003 },
2538 { 0x12, 0xf49f },
2539 { 0x13, 0x070b },
2540 { 0x1a, 0x05ad },
2541 { 0x14, 0x94c0 },
2542
françois romieubca03d52011-01-03 15:07:31 +00002543 /*
2544 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002545 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002546 */
françois romieudaf9df62009-10-07 12:44:20 +00002547 { 0x1f, 0x0002 },
2548 { 0x06, 0x5561 },
2549 { 0x1f, 0x0005 },
2550 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002551 { 0x06, 0x5561 },
2552
2553 /*
2554 * Can not link to 1Gbps with bad cable
2555 * Decrease SNR threshold form 21.07dB to 19.04dB
2556 */
2557 { 0x1f, 0x0001 },
2558 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002559
2560 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002561 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002562 };
françois romieubca03d52011-01-03 15:07:31 +00002563 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002564
françois romieu4da19632011-01-03 15:07:55 +00002565 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002566
2567 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002568 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002569 { 0x1f, 0x0002 },
2570 { 0x05, 0x669a },
2571 { 0x1f, 0x0005 },
2572 { 0x05, 0x8330 },
2573 { 0x06, 0x669a },
2574
2575 { 0x1f, 0x0002 }
2576 };
2577 int val;
2578
françois romieu4da19632011-01-03 15:07:55 +00002579 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002580
françois romieu4da19632011-01-03 15:07:55 +00002581 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002582 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002583 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002584 0x0065, 0x0066, 0x0067, 0x0068,
2585 0x0069, 0x006a, 0x006b, 0x006c
2586 };
2587 int i;
2588
françois romieu4da19632011-01-03 15:07:55 +00002589 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002590
2591 val &= 0xff00;
2592 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002593 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002594 }
2595 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002596 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002597 { 0x1f, 0x0002 },
2598 { 0x05, 0x2642 },
2599 { 0x1f, 0x0005 },
2600 { 0x05, 0x8330 },
2601 { 0x06, 0x2642 }
2602 };
2603
françois romieu4da19632011-01-03 15:07:55 +00002604 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002605 }
2606
françois romieubca03d52011-01-03 15:07:31 +00002607 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002608 rtl_writephy(tp, 0x1f, 0x0002);
2609 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2610 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002611
françois romieubca03d52011-01-03 15:07:31 +00002612 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002613 rtl_writephy(tp, 0x1f, 0x0002);
2614 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002615
françois romieu4da19632011-01-03 15:07:55 +00002616 rtl_writephy(tp, 0x1f, 0x0005);
2617 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002618
2619 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002620
françois romieu4da19632011-01-03 15:07:55 +00002621 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002622}
2623
françois romieu4da19632011-01-03 15:07:55 +00002624static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002625{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002626 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002627 { 0x1f, 0x0002 },
2628 { 0x10, 0x0008 },
2629 { 0x0d, 0x006c },
2630
2631 { 0x1f, 0x0000 },
2632 { 0x0d, 0xf880 },
2633
2634 { 0x1f, 0x0001 },
2635 { 0x17, 0x0cc0 },
2636
2637 { 0x1f, 0x0001 },
2638 { 0x0b, 0xa4d8 },
2639 { 0x09, 0x281c },
2640 { 0x07, 0x2883 },
2641 { 0x0a, 0x6b35 },
2642 { 0x1d, 0x3da4 },
2643 { 0x1c, 0xeffd },
2644 { 0x14, 0x7f52 },
2645 { 0x18, 0x7fc6 },
2646 { 0x08, 0x0601 },
2647 { 0x06, 0x4063 },
2648 { 0x10, 0xf074 },
2649 { 0x1f, 0x0003 },
2650 { 0x13, 0x0789 },
2651 { 0x12, 0xf4bd },
2652 { 0x1a, 0x04fd },
2653 { 0x14, 0x84b0 },
2654 { 0x1f, 0x0000 },
2655 { 0x00, 0x9200 },
2656
2657 { 0x1f, 0x0005 },
2658 { 0x01, 0x0340 },
2659 { 0x1f, 0x0001 },
2660 { 0x04, 0x4000 },
2661 { 0x03, 0x1d21 },
2662 { 0x02, 0x0c32 },
2663 { 0x01, 0x0200 },
2664 { 0x00, 0x5554 },
2665 { 0x04, 0x4800 },
2666 { 0x04, 0x4000 },
2667 { 0x04, 0xf000 },
2668 { 0x03, 0xdf01 },
2669 { 0x02, 0xdf20 },
2670 { 0x01, 0x101a },
2671 { 0x00, 0xa0ff },
2672 { 0x04, 0xf800 },
2673 { 0x04, 0xf000 },
2674 { 0x1f, 0x0000 },
2675
2676 { 0x1f, 0x0007 },
2677 { 0x1e, 0x0023 },
2678 { 0x16, 0x0000 },
2679 { 0x1f, 0x0000 }
2680 };
2681
françois romieu4da19632011-01-03 15:07:55 +00002682 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002683}
2684
françois romieue6de30d2011-01-03 15:08:37 +00002685static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2686{
2687 static const struct phy_reg phy_reg_init[] = {
2688 { 0x1f, 0x0001 },
2689 { 0x17, 0x0cc0 },
2690
2691 { 0x1f, 0x0007 },
2692 { 0x1e, 0x002d },
2693 { 0x18, 0x0040 },
2694 { 0x1f, 0x0000 }
2695 };
2696
2697 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2698 rtl_patchphy(tp, 0x0d, 1 << 5);
2699}
2700
Hayes Wang70090422011-07-06 15:58:06 +08002701static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002702{
2703 static const struct phy_reg phy_reg_init[] = {
2704 /* Enable Delay cap */
2705 { 0x1f, 0x0005 },
2706 { 0x05, 0x8b80 },
2707 { 0x06, 0xc896 },
2708 { 0x1f, 0x0000 },
2709
2710 /* Channel estimation fine tune */
2711 { 0x1f, 0x0001 },
2712 { 0x0b, 0x6c20 },
2713 { 0x07, 0x2872 },
2714 { 0x1c, 0xefff },
2715 { 0x1f, 0x0003 },
2716 { 0x14, 0x6420 },
2717 { 0x1f, 0x0000 },
2718
2719 /* Update PFM & 10M TX idle timer */
2720 { 0x1f, 0x0007 },
2721 { 0x1e, 0x002f },
2722 { 0x15, 0x1919 },
2723 { 0x1f, 0x0000 },
2724
2725 { 0x1f, 0x0007 },
2726 { 0x1e, 0x00ac },
2727 { 0x18, 0x0006 },
2728 { 0x1f, 0x0000 }
2729 };
2730
Francois Romieu15ecd032011-04-27 13:52:22 -07002731 rtl_apply_firmware(tp);
2732
hayeswang01dc7fe2011-03-21 01:50:28 +00002733 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2734
2735 /* DCO enable for 10M IDLE Power */
2736 rtl_writephy(tp, 0x1f, 0x0007);
2737 rtl_writephy(tp, 0x1e, 0x0023);
2738 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2739 rtl_writephy(tp, 0x1f, 0x0000);
2740
2741 /* For impedance matching */
2742 rtl_writephy(tp, 0x1f, 0x0002);
2743 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002744 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002745
2746 /* PHY auto speed down */
2747 rtl_writephy(tp, 0x1f, 0x0007);
2748 rtl_writephy(tp, 0x1e, 0x002d);
2749 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2750 rtl_writephy(tp, 0x1f, 0x0000);
2751 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2752
2753 rtl_writephy(tp, 0x1f, 0x0005);
2754 rtl_writephy(tp, 0x05, 0x8b86);
2755 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2756 rtl_writephy(tp, 0x1f, 0x0000);
2757
2758 rtl_writephy(tp, 0x1f, 0x0005);
2759 rtl_writephy(tp, 0x05, 0x8b85);
2760 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2761 rtl_writephy(tp, 0x1f, 0x0007);
2762 rtl_writephy(tp, 0x1e, 0x0020);
2763 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2764 rtl_writephy(tp, 0x1f, 0x0006);
2765 rtl_writephy(tp, 0x00, 0x5a00);
2766 rtl_writephy(tp, 0x1f, 0x0000);
2767 rtl_writephy(tp, 0x0d, 0x0007);
2768 rtl_writephy(tp, 0x0e, 0x003c);
2769 rtl_writephy(tp, 0x0d, 0x4007);
2770 rtl_writephy(tp, 0x0e, 0x0000);
2771 rtl_writephy(tp, 0x0d, 0x0000);
2772}
2773
Hayes Wang70090422011-07-06 15:58:06 +08002774static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2775{
2776 static const struct phy_reg phy_reg_init[] = {
2777 /* Enable Delay cap */
2778 { 0x1f, 0x0004 },
2779 { 0x1f, 0x0007 },
2780 { 0x1e, 0x00ac },
2781 { 0x18, 0x0006 },
2782 { 0x1f, 0x0002 },
2783 { 0x1f, 0x0000 },
2784 { 0x1f, 0x0000 },
2785
2786 /* Channel estimation fine tune */
2787 { 0x1f, 0x0003 },
2788 { 0x09, 0xa20f },
2789 { 0x1f, 0x0000 },
2790 { 0x1f, 0x0000 },
2791
2792 /* Green Setting */
2793 { 0x1f, 0x0005 },
2794 { 0x05, 0x8b5b },
2795 { 0x06, 0x9222 },
2796 { 0x05, 0x8b6d },
2797 { 0x06, 0x8000 },
2798 { 0x05, 0x8b76 },
2799 { 0x06, 0x8000 },
2800 { 0x1f, 0x0000 }
2801 };
2802
2803 rtl_apply_firmware(tp);
2804
2805 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2806
2807 /* For 4-corner performance improve */
2808 rtl_writephy(tp, 0x1f, 0x0005);
2809 rtl_writephy(tp, 0x05, 0x8b80);
2810 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2811 rtl_writephy(tp, 0x1f, 0x0000);
2812
2813 /* PHY auto speed down */
2814 rtl_writephy(tp, 0x1f, 0x0004);
2815 rtl_writephy(tp, 0x1f, 0x0007);
2816 rtl_writephy(tp, 0x1e, 0x002d);
2817 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2818 rtl_writephy(tp, 0x1f, 0x0002);
2819 rtl_writephy(tp, 0x1f, 0x0000);
2820 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2821
2822 /* improve 10M EEE waveform */
2823 rtl_writephy(tp, 0x1f, 0x0005);
2824 rtl_writephy(tp, 0x05, 0x8b86);
2825 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2826 rtl_writephy(tp, 0x1f, 0x0000);
2827
2828 /* Improve 2-pair detection performance */
2829 rtl_writephy(tp, 0x1f, 0x0005);
2830 rtl_writephy(tp, 0x05, 0x8b85);
2831 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2832 rtl_writephy(tp, 0x1f, 0x0000);
2833
2834 /* EEE setting */
2835 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2836 ERIAR_EXGMAC);
2837 rtl_writephy(tp, 0x1f, 0x0005);
2838 rtl_writephy(tp, 0x05, 0x8b85);
2839 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2840 rtl_writephy(tp, 0x1f, 0x0004);
2841 rtl_writephy(tp, 0x1f, 0x0007);
2842 rtl_writephy(tp, 0x1e, 0x0020);
2843 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
2844 rtl_writephy(tp, 0x1f, 0x0002);
2845 rtl_writephy(tp, 0x1f, 0x0000);
2846 rtl_writephy(tp, 0x0d, 0x0007);
2847 rtl_writephy(tp, 0x0e, 0x003c);
2848 rtl_writephy(tp, 0x0d, 0x4007);
2849 rtl_writephy(tp, 0x0e, 0x0000);
2850 rtl_writephy(tp, 0x0d, 0x0000);
2851
2852 /* Green feature */
2853 rtl_writephy(tp, 0x1f, 0x0003);
2854 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2855 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2856 rtl_writephy(tp, 0x1f, 0x0000);
2857}
2858
françois romieu4da19632011-01-03 15:07:55 +00002859static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002860{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002861 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002862 { 0x1f, 0x0003 },
2863 { 0x08, 0x441d },
2864 { 0x01, 0x9100 },
2865 { 0x1f, 0x0000 }
2866 };
2867
françois romieu4da19632011-01-03 15:07:55 +00002868 rtl_writephy(tp, 0x1f, 0x0000);
2869 rtl_patchphy(tp, 0x11, 1 << 12);
2870 rtl_patchphy(tp, 0x19, 1 << 13);
2871 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002872
françois romieu4da19632011-01-03 15:07:55 +00002873 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002874}
2875
Hayes Wang5a5e4442011-02-22 17:26:21 +08002876static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2877{
2878 static const struct phy_reg phy_reg_init[] = {
2879 { 0x1f, 0x0005 },
2880 { 0x1a, 0x0000 },
2881 { 0x1f, 0x0000 },
2882
2883 { 0x1f, 0x0004 },
2884 { 0x1c, 0x0000 },
2885 { 0x1f, 0x0000 },
2886
2887 { 0x1f, 0x0001 },
2888 { 0x15, 0x7701 },
2889 { 0x1f, 0x0000 }
2890 };
2891
2892 /* Disable ALDPS before ram code */
2893 rtl_writephy(tp, 0x1f, 0x0000);
2894 rtl_writephy(tp, 0x18, 0x0310);
2895 msleep(100);
2896
François Romieu953a12c2011-04-24 17:38:48 +02002897 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002898
2899 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2900}
2901
Francois Romieu5615d9f2007-08-17 17:50:46 +02002902static void rtl_hw_phy_config(struct net_device *dev)
2903{
2904 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002905
2906 rtl8169_print_mac_version(tp);
2907
2908 switch (tp->mac_version) {
2909 case RTL_GIGA_MAC_VER_01:
2910 break;
2911 case RTL_GIGA_MAC_VER_02:
2912 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002913 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002914 break;
2915 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002916 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002917 break;
françois romieu2e9558562009-08-10 19:44:19 +00002918 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002919 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002920 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002921 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002922 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002923 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002924 case RTL_GIGA_MAC_VER_07:
2925 case RTL_GIGA_MAC_VER_08:
2926 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002927 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002928 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002929 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002930 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002931 break;
2932 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002933 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002934 break;
2935 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002936 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002937 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002938 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002939 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002940 break;
2941 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002942 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002943 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002944 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002945 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002946 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002947 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002948 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002949 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002950 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002951 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002952 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002953 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002954 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002955 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002956 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002957 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002958 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002959 break;
2960 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002961 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002962 break;
2963 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002964 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002965 break;
françois romieue6de30d2011-01-03 15:08:37 +00002966 case RTL_GIGA_MAC_VER_28:
2967 rtl8168d_4_hw_phy_config(tp);
2968 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002969 case RTL_GIGA_MAC_VER_29:
2970 case RTL_GIGA_MAC_VER_30:
2971 rtl8105e_hw_phy_config(tp);
2972 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002973 case RTL_GIGA_MAC_VER_31:
2974 /* None. */
2975 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002976 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002977 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08002978 rtl8168e_1_hw_phy_config(tp);
2979 break;
2980 case RTL_GIGA_MAC_VER_34:
2981 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002982 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002983
Francois Romieu5615d9f2007-08-17 17:50:46 +02002984 default:
2985 break;
2986 }
2987}
2988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989static void rtl8169_phy_timer(unsigned long __opaque)
2990{
2991 struct net_device *dev = (struct net_device *)__opaque;
2992 struct rtl8169_private *tp = netdev_priv(dev);
2993 struct timer_list *timer = &tp->timer;
2994 void __iomem *ioaddr = tp->mmio_addr;
2995 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2996
Francois Romieubcf0bf92006-07-26 23:14:13 +02002997 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 spin_lock_irq(&tp->lock);
3000
françois romieu4da19632011-01-03 15:07:55 +00003001 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003002 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 * A busy loop could burn quite a few cycles on nowadays CPU.
3004 * Let's delay the execution of the timer for a few ticks.
3005 */
3006 timeout = HZ/10;
3007 goto out_mod_timer;
3008 }
3009
3010 if (tp->link_ok(ioaddr))
3011 goto out_unlock;
3012
Joe Perchesbf82c182010-02-09 11:49:50 +00003013 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
françois romieu4da19632011-01-03 15:07:55 +00003015 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017out_mod_timer:
3018 mod_timer(timer, jiffies + timeout);
3019out_unlock:
3020 spin_unlock_irq(&tp->lock);
3021}
3022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023#ifdef CONFIG_NET_POLL_CONTROLLER
3024/*
3025 * Polling 'interrupt' - used by things like netconsole to send skbs
3026 * without having to re-enable interrupts. It's not called while
3027 * the interrupt routine is executing.
3028 */
3029static void rtl8169_netpoll(struct net_device *dev)
3030{
3031 struct rtl8169_private *tp = netdev_priv(dev);
3032 struct pci_dev *pdev = tp->pci_dev;
3033
3034 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003035 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 enable_irq(pdev->irq);
3037}
3038#endif
3039
3040static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3041 void __iomem *ioaddr)
3042{
3043 iounmap(ioaddr);
3044 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003045 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 pci_disable_device(pdev);
3047 free_netdev(dev);
3048}
3049
Francois Romieubf793292006-11-01 00:53:05 +01003050static void rtl8169_phy_reset(struct net_device *dev,
3051 struct rtl8169_private *tp)
3052{
Francois Romieu07d3f512007-02-21 22:40:46 +01003053 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003054
françois romieu4da19632011-01-03 15:07:55 +00003055 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003056 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003057 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003058 return;
3059 msleep(1);
3060 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003061 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003062}
3063
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003064static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003066 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003067
Francois Romieu5615d9f2007-08-17 17:50:46 +02003068 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003069
Marcus Sundberg773328942008-07-10 21:28:08 +02003070 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3071 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3072 RTL_W8(0x82, 0x01);
3073 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003074
Francois Romieu6dccd162007-02-13 23:38:05 +01003075 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3076
3077 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3078 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003079
Francois Romieubcf0bf92006-07-26 23:14:13 +02003080 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003081 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3082 RTL_W8(0x82, 0x01);
3083 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003084 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003085 }
3086
Francois Romieubf793292006-11-01 00:53:05 +01003087 rtl8169_phy_reset(dev, tp);
3088
Oliver Neukum54405cd2011-01-06 21:55:13 +01003089 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003090 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3091 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3092 (tp->mii.supports_gmii ?
3093 ADVERTISED_1000baseT_Half |
3094 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003095
Joe Perchesbf82c182010-02-09 11:49:50 +00003096 if (RTL_R8(PHYstatus) & TBI_Enable)
3097 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003098}
3099
Francois Romieu773d2022007-01-31 23:47:43 +01003100static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3101{
3102 void __iomem *ioaddr = tp->mmio_addr;
3103 u32 high;
3104 u32 low;
3105
3106 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3107 high = addr[4] | (addr[5] << 8);
3108
3109 spin_lock_irq(&tp->lock);
3110
3111 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003112
Francois Romieu773d2022007-01-31 23:47:43 +01003113 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003114 RTL_R32(MAC4);
3115
Francois Romieu78f1cd02010-03-27 19:35:46 -07003116 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003117 RTL_R32(MAC0);
3118
Francois Romieu773d2022007-01-31 23:47:43 +01003119 RTL_W8(Cfg9346, Cfg9346_Lock);
3120
3121 spin_unlock_irq(&tp->lock);
3122}
3123
3124static int rtl_set_mac_address(struct net_device *dev, void *p)
3125{
3126 struct rtl8169_private *tp = netdev_priv(dev);
3127 struct sockaddr *addr = p;
3128
3129 if (!is_valid_ether_addr(addr->sa_data))
3130 return -EADDRNOTAVAIL;
3131
3132 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3133
3134 rtl_rar_set(tp, dev->dev_addr);
3135
3136 return 0;
3137}
3138
Francois Romieu5f787a12006-08-17 13:02:36 +02003139static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3140{
3141 struct rtl8169_private *tp = netdev_priv(dev);
3142 struct mii_ioctl_data *data = if_mii(ifr);
3143
Francois Romieu8b4ab282008-11-19 22:05:25 -08003144 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3145}
Francois Romieu5f787a12006-08-17 13:02:36 +02003146
Francois Romieucecb5fd2011-04-01 10:21:07 +02003147static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3148 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003149{
Francois Romieu5f787a12006-08-17 13:02:36 +02003150 switch (cmd) {
3151 case SIOCGMIIPHY:
3152 data->phy_id = 32; /* Internal PHY */
3153 return 0;
3154
3155 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003156 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003157 return 0;
3158
3159 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003160 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003161 return 0;
3162 }
3163 return -EOPNOTSUPP;
3164}
3165
Francois Romieu8b4ab282008-11-19 22:05:25 -08003166static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3167{
3168 return -EOPNOTSUPP;
3169}
3170
Francois Romieu0e485152007-02-20 00:00:26 +01003171static const struct rtl_cfg_info {
3172 void (*hw_start)(struct net_device *);
3173 unsigned int region;
3174 unsigned int align;
3175 u16 intr_event;
3176 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003177 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003178 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003179} rtl_cfg_infos [] = {
3180 [RTL_CFG_0] = {
3181 .hw_start = rtl_hw_start_8169,
3182 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003183 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003184 .intr_event = SYSErr | LinkChg | RxOverflow |
3185 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003186 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003187 .features = RTL_FEATURE_GMII,
3188 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003189 },
3190 [RTL_CFG_1] = {
3191 .hw_start = rtl_hw_start_8168,
3192 .region = 2,
3193 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003194 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003195 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003196 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003197 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3198 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003199 },
3200 [RTL_CFG_2] = {
3201 .hw_start = rtl_hw_start_8101,
3202 .region = 2,
3203 .align = 8,
3204 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3205 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003206 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003207 .features = RTL_FEATURE_MSI,
3208 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003209 }
3210};
3211
Francois Romieufbac58f2007-10-04 22:51:38 +02003212/* Cfg9346_Unlock assumed. */
3213static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3214 const struct rtl_cfg_info *cfg)
3215{
3216 unsigned msi = 0;
3217 u8 cfg2;
3218
3219 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003220 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003221 if (pci_enable_msi(pdev)) {
3222 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3223 } else {
3224 cfg2 |= MSIEnable;
3225 msi = RTL_FEATURE_MSI;
3226 }
3227 }
3228 RTL_W8(Config2, cfg2);
3229 return msi;
3230}
3231
3232static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3233{
3234 if (tp->features & RTL_FEATURE_MSI) {
3235 pci_disable_msi(pdev);
3236 tp->features &= ~RTL_FEATURE_MSI;
3237 }
3238}
3239
Francois Romieu8b4ab282008-11-19 22:05:25 -08003240static const struct net_device_ops rtl8169_netdev_ops = {
3241 .ndo_open = rtl8169_open,
3242 .ndo_stop = rtl8169_close,
3243 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003244 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003245 .ndo_tx_timeout = rtl8169_tx_timeout,
3246 .ndo_validate_addr = eth_validate_addr,
3247 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003248 .ndo_fix_features = rtl8169_fix_features,
3249 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003250 .ndo_set_mac_address = rtl_set_mac_address,
3251 .ndo_do_ioctl = rtl8169_ioctl,
3252 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003253#ifdef CONFIG_NET_POLL_CONTROLLER
3254 .ndo_poll_controller = rtl8169_netpoll,
3255#endif
3256
3257};
3258
françois romieuc0e45c12011-01-03 15:08:04 +00003259static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3260{
3261 struct mdio_ops *ops = &tp->mdio_ops;
3262
3263 switch (tp->mac_version) {
3264 case RTL_GIGA_MAC_VER_27:
3265 ops->write = r8168dp_1_mdio_write;
3266 ops->read = r8168dp_1_mdio_read;
3267 break;
françois romieue6de30d2011-01-03 15:08:37 +00003268 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003269 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003270 ops->write = r8168dp_2_mdio_write;
3271 ops->read = r8168dp_2_mdio_read;
3272 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003273 default:
3274 ops->write = r8169_mdio_write;
3275 ops->read = r8169_mdio_read;
3276 break;
3277 }
3278}
3279
françois romieu065c27c2011-01-03 15:08:12 +00003280static void r810x_phy_power_down(struct rtl8169_private *tp)
3281{
3282 rtl_writephy(tp, 0x1f, 0x0000);
3283 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3284}
3285
3286static void r810x_phy_power_up(struct rtl8169_private *tp)
3287{
3288 rtl_writephy(tp, 0x1f, 0x0000);
3289 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3290}
3291
3292static void r810x_pll_power_down(struct rtl8169_private *tp)
3293{
3294 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3295 rtl_writephy(tp, 0x1f, 0x0000);
3296 rtl_writephy(tp, MII_BMCR, 0x0000);
3297 return;
3298 }
3299
3300 r810x_phy_power_down(tp);
3301}
3302
3303static void r810x_pll_power_up(struct rtl8169_private *tp)
3304{
3305 r810x_phy_power_up(tp);
3306}
3307
3308static void r8168_phy_power_up(struct rtl8169_private *tp)
3309{
3310 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003311 switch (tp->mac_version) {
3312 case RTL_GIGA_MAC_VER_11:
3313 case RTL_GIGA_MAC_VER_12:
3314 case RTL_GIGA_MAC_VER_17:
3315 case RTL_GIGA_MAC_VER_18:
3316 case RTL_GIGA_MAC_VER_19:
3317 case RTL_GIGA_MAC_VER_20:
3318 case RTL_GIGA_MAC_VER_21:
3319 case RTL_GIGA_MAC_VER_22:
3320 case RTL_GIGA_MAC_VER_23:
3321 case RTL_GIGA_MAC_VER_24:
3322 case RTL_GIGA_MAC_VER_25:
3323 case RTL_GIGA_MAC_VER_26:
3324 case RTL_GIGA_MAC_VER_27:
3325 case RTL_GIGA_MAC_VER_28:
3326 case RTL_GIGA_MAC_VER_31:
3327 rtl_writephy(tp, 0x0e, 0x0000);
3328 break;
3329 default:
3330 break;
3331 }
françois romieu065c27c2011-01-03 15:08:12 +00003332 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3333}
3334
3335static void r8168_phy_power_down(struct rtl8169_private *tp)
3336{
3337 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003338 switch (tp->mac_version) {
3339 case RTL_GIGA_MAC_VER_32:
3340 case RTL_GIGA_MAC_VER_33:
3341 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3342 break;
3343
3344 case RTL_GIGA_MAC_VER_11:
3345 case RTL_GIGA_MAC_VER_12:
3346 case RTL_GIGA_MAC_VER_17:
3347 case RTL_GIGA_MAC_VER_18:
3348 case RTL_GIGA_MAC_VER_19:
3349 case RTL_GIGA_MAC_VER_20:
3350 case RTL_GIGA_MAC_VER_21:
3351 case RTL_GIGA_MAC_VER_22:
3352 case RTL_GIGA_MAC_VER_23:
3353 case RTL_GIGA_MAC_VER_24:
3354 case RTL_GIGA_MAC_VER_25:
3355 case RTL_GIGA_MAC_VER_26:
3356 case RTL_GIGA_MAC_VER_27:
3357 case RTL_GIGA_MAC_VER_28:
3358 case RTL_GIGA_MAC_VER_31:
3359 rtl_writephy(tp, 0x0e, 0x0200);
3360 default:
3361 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3362 break;
3363 }
françois romieu065c27c2011-01-03 15:08:12 +00003364}
3365
3366static void r8168_pll_power_down(struct rtl8169_private *tp)
3367{
3368 void __iomem *ioaddr = tp->mmio_addr;
3369
Francois Romieucecb5fd2011-04-01 10:21:07 +02003370 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3371 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3372 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003373 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003374 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003375 }
françois romieu065c27c2011-01-03 15:08:12 +00003376
Francois Romieucecb5fd2011-04-01 10:21:07 +02003377 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3378 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003379 (RTL_R16(CPlusCmd) & ASF)) {
3380 return;
3381 }
3382
hayeswang01dc7fe2011-03-21 01:50:28 +00003383 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3384 tp->mac_version == RTL_GIGA_MAC_VER_33)
3385 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3386
françois romieu065c27c2011-01-03 15:08:12 +00003387 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3388 rtl_writephy(tp, 0x1f, 0x0000);
3389 rtl_writephy(tp, MII_BMCR, 0x0000);
3390
Hayes Wangd4ed95d2011-07-06 15:58:07 +08003391 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3392 tp->mac_version == RTL_GIGA_MAC_VER_33)
3393 RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
3394 AcceptMulticast | AcceptMyPhys);
françois romieu065c27c2011-01-03 15:08:12 +00003395 return;
3396 }
3397
3398 r8168_phy_power_down(tp);
3399
3400 switch (tp->mac_version) {
3401 case RTL_GIGA_MAC_VER_25:
3402 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003403 case RTL_GIGA_MAC_VER_27:
3404 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003405 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003406 case RTL_GIGA_MAC_VER_32:
3407 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003408 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3409 break;
3410 }
3411}
3412
3413static void r8168_pll_power_up(struct rtl8169_private *tp)
3414{
3415 void __iomem *ioaddr = tp->mmio_addr;
3416
Francois Romieucecb5fd2011-04-01 10:21:07 +02003417 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3418 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3419 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003420 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003421 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003422 }
françois romieu065c27c2011-01-03 15:08:12 +00003423
3424 switch (tp->mac_version) {
3425 case RTL_GIGA_MAC_VER_25:
3426 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003427 case RTL_GIGA_MAC_VER_27:
3428 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003429 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003430 case RTL_GIGA_MAC_VER_32:
3431 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003432 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3433 break;
3434 }
3435
3436 r8168_phy_power_up(tp);
3437}
3438
3439static void rtl_pll_power_op(struct rtl8169_private *tp,
3440 void (*op)(struct rtl8169_private *))
3441{
3442 if (op)
3443 op(tp);
3444}
3445
3446static void rtl_pll_power_down(struct rtl8169_private *tp)
3447{
3448 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3449}
3450
3451static void rtl_pll_power_up(struct rtl8169_private *tp)
3452{
3453 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3454}
3455
3456static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3457{
3458 struct pll_power_ops *ops = &tp->pll_power_ops;
3459
3460 switch (tp->mac_version) {
3461 case RTL_GIGA_MAC_VER_07:
3462 case RTL_GIGA_MAC_VER_08:
3463 case RTL_GIGA_MAC_VER_09:
3464 case RTL_GIGA_MAC_VER_10:
3465 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003466 case RTL_GIGA_MAC_VER_29:
3467 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003468 ops->down = r810x_pll_power_down;
3469 ops->up = r810x_pll_power_up;
3470 break;
3471
3472 case RTL_GIGA_MAC_VER_11:
3473 case RTL_GIGA_MAC_VER_12:
3474 case RTL_GIGA_MAC_VER_17:
3475 case RTL_GIGA_MAC_VER_18:
3476 case RTL_GIGA_MAC_VER_19:
3477 case RTL_GIGA_MAC_VER_20:
3478 case RTL_GIGA_MAC_VER_21:
3479 case RTL_GIGA_MAC_VER_22:
3480 case RTL_GIGA_MAC_VER_23:
3481 case RTL_GIGA_MAC_VER_24:
3482 case RTL_GIGA_MAC_VER_25:
3483 case RTL_GIGA_MAC_VER_26:
3484 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003485 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003486 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003487 case RTL_GIGA_MAC_VER_32:
3488 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003489 case RTL_GIGA_MAC_VER_34:
françois romieu065c27c2011-01-03 15:08:12 +00003490 ops->down = r8168_pll_power_down;
3491 ops->up = r8168_pll_power_up;
3492 break;
3493
3494 default:
3495 ops->down = NULL;
3496 ops->up = NULL;
3497 break;
3498 }
3499}
3500
Hayes Wange542a222011-07-06 15:58:04 +08003501static void rtl_init_rxcfg(struct rtl8169_private *tp)
3502{
3503 void __iomem *ioaddr = tp->mmio_addr;
3504
3505 switch (tp->mac_version) {
3506 case RTL_GIGA_MAC_VER_01:
3507 case RTL_GIGA_MAC_VER_02:
3508 case RTL_GIGA_MAC_VER_03:
3509 case RTL_GIGA_MAC_VER_04:
3510 case RTL_GIGA_MAC_VER_05:
3511 case RTL_GIGA_MAC_VER_06:
3512 case RTL_GIGA_MAC_VER_10:
3513 case RTL_GIGA_MAC_VER_11:
3514 case RTL_GIGA_MAC_VER_12:
3515 case RTL_GIGA_MAC_VER_13:
3516 case RTL_GIGA_MAC_VER_14:
3517 case RTL_GIGA_MAC_VER_15:
3518 case RTL_GIGA_MAC_VER_16:
3519 case RTL_GIGA_MAC_VER_17:
3520 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3521 break;
3522 case RTL_GIGA_MAC_VER_18:
3523 case RTL_GIGA_MAC_VER_19:
3524 case RTL_GIGA_MAC_VER_20:
3525 case RTL_GIGA_MAC_VER_21:
3526 case RTL_GIGA_MAC_VER_22:
3527 case RTL_GIGA_MAC_VER_23:
3528 case RTL_GIGA_MAC_VER_24:
3529 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3530 break;
3531 default:
3532 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3533 break;
3534 }
3535}
3536
Hayes Wang92fc43b2011-07-06 15:58:03 +08003537static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3538{
3539 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3540}
3541
Francois Romieu6f43adc2011-04-29 15:05:51 +02003542static void rtl_hw_reset(struct rtl8169_private *tp)
3543{
3544 void __iomem *ioaddr = tp->mmio_addr;
3545 int i;
3546
3547 /* Soft reset the chip. */
3548 RTL_W8(ChipCmd, CmdReset);
3549
3550 /* Check that the chip has finished the reset. */
3551 for (i = 0; i < 100; i++) {
3552 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3553 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003554 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003555 }
Hayes Wang92fc43b2011-07-06 15:58:03 +08003556
3557 rtl8169_init_ring_indexes(tp);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003558}
3559
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003560static int __devinit
3561rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3562{
Francois Romieu0e485152007-02-20 00:00:26 +01003563 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3564 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003566 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003567 struct net_device *dev;
3568 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003569 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003570 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003572 if (netif_msg_drv(&debug)) {
3573 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3574 MODULENAME, RTL8169_VERSION);
3575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003578 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003579 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003580 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003581 rc = -ENOMEM;
3582 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 }
3584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003586 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003588 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003589 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003590 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Francois Romieuccdffb92008-07-26 14:26:06 +02003592 mii = &tp->mii;
3593 mii->dev = dev;
3594 mii->mdio_read = rtl_mdio_read;
3595 mii->mdio_write = rtl_mdio_write;
3596 mii->phy_id_mask = 0x1f;
3597 mii->reg_num_mask = 0x1f;
3598 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3599
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003600 /* disable ASPM completely as that cause random device stop working
3601 * problems as well as full system hangs for some PCIe devices users */
3602 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3603 PCIE_LINK_STATE_CLKPM);
3604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3606 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003607 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003608 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003609 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 }
3611
françois romieu87aeec72010-04-26 11:42:06 +00003612 if (pci_set_mwi(pdev) < 0)
3613 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003616 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003617 netif_err(tp, probe, dev,
3618 "region #%d not an MMIO resource, aborting\n",
3619 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003621 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003625 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003626 netif_err(tp, probe, dev,
3627 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003629 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 }
3631
3632 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003633 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003634 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003635 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 }
3637
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003638 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003641 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 tp->cp_cmd |= PCIDAC;
3643 dev->features |= NETIF_F_HIGHDMA;
3644 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003645 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003647 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003648 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
3650 }
3651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003653 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003654 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003655 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003657 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003659 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Jon Masone44daad2011-06-27 07:46:31 +00003661 if (!pci_is_pcie(pdev))
3662 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07003663
Hayes Wange542a222011-07-06 15:58:04 +08003664 /* Identify chip attached to board */
3665 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
3666
3667 rtl_init_rxcfg(tp);
3668
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003669 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Francois Romieu6f43adc2011-04-29 15:05:51 +02003671 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003673 RTL_W16(IntrStatus, 0xffff);
3674
françois romieuca52efd2009-07-24 12:34:19 +00003675 pci_set_master(pdev);
3676
Francois Romieu7a8fc772011-03-01 17:18:33 +01003677 /*
3678 * Pretend we are using VLANs; This bypasses a nasty bug where
3679 * Interrupts stop flowing on high load on 8110SCd controllers.
3680 */
3681 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3682 tp->cp_cmd |= RxVlan;
3683
françois romieuc0e45c12011-01-03 15:08:04 +00003684 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003685 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Francois Romieu85bffe62011-04-27 08:22:39 +02003689 chipset = tp->mac_version;
3690 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Francois Romieu5d06a992006-02-23 00:47:58 +01003692 RTL_W8(Cfg9346, Cfg9346_Unlock);
3693 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3694 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003695 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3696 tp->features |= RTL_FEATURE_WOL;
3697 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3698 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003699 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003700 RTL_W8(Cfg9346, Cfg9346_Lock);
3701
Francois Romieu66ec5d42007-11-06 22:56:10 +01003702 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3703 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 tp->set_speed = rtl8169_set_speed_tbi;
3705 tp->get_settings = rtl8169_gset_tbi;
3706 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3707 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3708 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003709 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 } else {
3711 tp->set_speed = rtl8169_set_speed_xmii;
3712 tp->get_settings = rtl8169_gset_xmii;
3713 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3714 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3715 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003716 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 }
3718
Francois Romieudf58ef512008-10-09 14:35:58 -07003719 spin_lock_init(&tp->lock);
3720
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003721 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 for (i = 0; i < MAC_ADDR_LEN; i++)
3723 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003724 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3728 dev->irq = pdev->irq;
3729 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003731 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Michał Mirosław350fb322011-04-08 06:35:56 +00003733 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3734 * properly for all devices */
3735 dev->features |= NETIF_F_RXCSUM |
3736 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3737
3738 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3739 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3740 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3741 NETIF_F_HIGHDMA;
3742
3743 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3744 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3745 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
3747 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003748 tp->hw_start = cfg->hw_start;
3749 tp->intr_event = cfg->intr_event;
3750 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Francois Romieu2efa53f2007-03-09 00:00:05 +01003752 init_timer(&tp->timer);
3753 tp->timer.data = (unsigned long) dev;
3754 tp->timer.function = rtl8169_phy_timer;
3755
Francois Romieub6ffd972011-06-17 17:00:05 +02003756 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003757
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003759 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003760 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
3762 pci_set_drvdata(pdev, dev);
3763
Joe Perchesbf82c182010-02-09 11:49:50 +00003764 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003765 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003766 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Francois Romieucecb5fd2011-04-01 10:21:07 +02003768 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3769 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3770 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003771 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003772 }
françois romieub646d902011-01-03 15:08:21 +00003773
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003774 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Alan Sternf3ec4f82010-06-08 15:23:51 -04003776 if (pci_dev_run_wake(pdev))
3777 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003778
Ivan Vecera0d672e92011-02-15 02:08:39 +00003779 netif_carrier_off(dev);
3780
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003781out:
3782 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
françois romieu87aeec72010-04-26 11:42:06 +00003784err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003785 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003786 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003787err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003788 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003789err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003790 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003791 pci_disable_device(pdev);
3792err_out_free_dev_1:
3793 free_netdev(dev);
3794 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
Francois Romieu07d3f512007-02-21 22:40:46 +01003797static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
3799 struct net_device *dev = pci_get_drvdata(pdev);
3800 struct rtl8169_private *tp = netdev_priv(dev);
3801
Francois Romieucecb5fd2011-04-01 10:21:07 +02003802 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3803 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3804 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003805 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003806 }
françois romieub646d902011-01-03 15:08:21 +00003807
Tejun Heo23f333a2010-12-12 16:45:14 +01003808 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003809
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003811
François Romieu953a12c2011-04-24 17:38:48 +02003812 rtl_release_firmware(tp);
3813
Alan Sternf3ec4f82010-06-08 15:23:51 -04003814 if (pci_dev_run_wake(pdev))
3815 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003816
Ivan Veceracc098dc2009-11-29 23:12:52 -08003817 /* restore original MAC address */
3818 rtl_rar_set(tp, dev->perm_addr);
3819
Francois Romieufbac58f2007-10-04 22:51:38 +02003820 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3822 pci_set_drvdata(pdev, NULL);
3823}
3824
Francois Romieub6ffd972011-06-17 17:00:05 +02003825static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3826{
3827 struct rtl_fw *rtl_fw;
3828 const char *name;
3829 int rc = -ENOMEM;
3830
3831 name = rtl_lookup_firmware_name(tp);
3832 if (!name)
3833 goto out_no_firmware;
3834
3835 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3836 if (!rtl_fw)
3837 goto err_warn;
3838
3839 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3840 if (rc < 0)
3841 goto err_free;
3842
Francois Romieufd112f22011-06-18 00:10:29 +02003843 rc = rtl_check_firmware(tp, rtl_fw);
3844 if (rc < 0)
3845 goto err_release_firmware;
3846
Francois Romieub6ffd972011-06-17 17:00:05 +02003847 tp->rtl_fw = rtl_fw;
3848out:
3849 return;
3850
Francois Romieufd112f22011-06-18 00:10:29 +02003851err_release_firmware:
3852 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003853err_free:
3854 kfree(rtl_fw);
3855err_warn:
3856 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3857 name, rc);
3858out_no_firmware:
3859 tp->rtl_fw = NULL;
3860 goto out;
3861}
3862
François Romieu953a12c2011-04-24 17:38:48 +02003863static void rtl_request_firmware(struct rtl8169_private *tp)
3864{
Francois Romieub6ffd972011-06-17 17:00:05 +02003865 if (IS_ERR(tp->rtl_fw))
3866 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003867}
3868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869static int rtl8169_open(struct net_device *dev)
3870{
3871 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003872 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003874 int retval = -ENOMEM;
3875
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003876 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Neil Hormanc0cd8842010-03-29 13:16:02 -07003878 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003880 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003882 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3883 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003885 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003887 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3888 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003890 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
3892 retval = rtl8169_init_ring(dev);
3893 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003894 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
David Howellsc4028952006-11-22 14:57:56 +00003896 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Francois Romieu99f252b2007-04-02 22:59:59 +02003898 smp_mb();
3899
François Romieu953a12c2011-04-24 17:38:48 +02003900 rtl_request_firmware(tp);
3901
Francois Romieufbac58f2007-10-04 22:51:38 +02003902 retval = request_irq(dev->irq, rtl8169_interrupt,
3903 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003904 dev->name, dev);
3905 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003906 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003907
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003908 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003909
françois romieueee3a962011-01-08 02:17:26 +00003910 rtl8169_init_phy(dev, tp);
3911
Michał Mirosław350fb322011-04-08 06:35:56 +00003912 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003913
françois romieu065c27c2011-01-03 15:08:12 +00003914 rtl_pll_power_up(tp);
3915
Francois Romieu07ce4062007-02-23 23:36:39 +01003916 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003918 tp->saved_wolopts = 0;
3919 pm_runtime_put_noidle(&pdev->dev);
3920
françois romieueee3a962011-01-08 02:17:26 +00003921 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922out:
3923 return retval;
3924
François Romieu953a12c2011-04-24 17:38:48 +02003925err_release_fw_2:
3926 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003927 rtl8169_rx_clear(tp);
3928err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003929 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3930 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003931 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003932err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003933 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3934 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003935 tp->TxDescArray = NULL;
3936err_pm_runtime_put:
3937 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 goto out;
3939}
3940
Hayes Wang92fc43b2011-07-06 15:58:03 +08003941static void rtl_rx_close(struct rtl8169_private *tp)
3942{
3943 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003944
Francois Romieu1687b562011-07-19 17:21:29 +02003945 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003946}
3947
françois romieue6de30d2011-01-03 15:08:37 +00003948static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949{
françois romieue6de30d2011-01-03 15:08:37 +00003950 void __iomem *ioaddr = tp->mmio_addr;
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 /* Disable interrupts */
3953 rtl8169_irq_mask_and_ack(ioaddr);
3954
Hayes Wang92fc43b2011-07-06 15:58:03 +08003955 rtl_rx_close(tp);
3956
Hayes Wang5d2e1952011-02-22 17:26:22 +08003957 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003958 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3959 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003960 while (RTL_R8(TxPoll) & NPQ)
3961 udelay(20);
Hayes Wang70090422011-07-06 15:58:06 +08003962 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3963 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
3964 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003965 } else {
3966 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
3967 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00003968 }
3969
Hayes Wang92fc43b2011-07-06 15:58:03 +08003970 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971}
3972
Francois Romieu7f796d832007-06-11 23:04:41 +02003973static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003974{
3975 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01003976
3977 /* Set DMA burst size and Interframe Gap Time */
3978 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3979 (InterFrameGap << TxInterFrameGapShift));
3980}
3981
Francois Romieu07ce4062007-02-23 23:36:39 +01003982static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
3984 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Francois Romieu07ce4062007-02-23 23:36:39 +01003986 tp->hw_start(dev);
3987
Francois Romieu07ce4062007-02-23 23:36:39 +01003988 netif_start_queue(dev);
3989}
3990
Francois Romieu7f796d832007-06-11 23:04:41 +02003991static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3992 void __iomem *ioaddr)
3993{
3994 /*
3995 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3996 * register to be written before TxDescAddrLow to work.
3997 * Switching from MMIO to I/O access fixes the issue as well.
3998 */
3999 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004000 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004001 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004002 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004003}
4004
4005static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4006{
4007 u16 cmd;
4008
4009 cmd = RTL_R16(CPlusCmd);
4010 RTL_W16(CPlusCmd, cmd);
4011 return cmd;
4012}
4013
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004014static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004015{
4016 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00004017 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004018}
4019
Francois Romieu6dccd162007-02-13 23:38:05 +01004020static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4021{
Francois Romieu37441002011-06-17 22:58:54 +02004022 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004023 u32 mac_version;
4024 u32 clk;
4025 u32 val;
4026 } cfg2_info [] = {
4027 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4028 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4029 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4030 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004031 };
4032 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004033 unsigned int i;
4034 u32 clk;
4035
4036 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004037 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004038 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4039 RTL_W32(0x7c, p->val);
4040 break;
4041 }
4042 }
4043}
4044
Francois Romieu07ce4062007-02-23 23:36:39 +01004045static void rtl_hw_start_8169(struct net_device *dev)
4046{
4047 struct rtl8169_private *tp = netdev_priv(dev);
4048 void __iomem *ioaddr = tp->mmio_addr;
4049 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004050
Francois Romieu9cb427b2006-11-02 00:10:16 +01004051 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4052 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4053 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4054 }
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004057 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4058 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4059 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4060 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004061 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4062
Hayes Wange542a222011-07-06 15:58:04 +08004063 rtl_init_rxcfg(tp);
4064
françois romieuf0298f82011-01-03 15:07:42 +00004065 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004067 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Francois Romieucecb5fd2011-04-01 10:21:07 +02004069 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4070 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4071 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4072 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004073 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Francois Romieu7f796d832007-06-11 23:04:41 +02004075 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004076
Francois Romieucecb5fd2011-04-01 10:21:07 +02004077 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4078 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004079 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004081 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 }
4083
Francois Romieubcf0bf92006-07-26 23:14:13 +02004084 RTL_W16(CPlusCmd, tp->cp_cmd);
4085
Francois Romieu6dccd162007-02-13 23:38:05 +01004086 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 /*
4089 * Undocumented corner. Supposedly:
4090 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4091 */
4092 RTL_W16(IntrMitigate, 0x0000);
4093
Francois Romieu7f796d832007-06-11 23:04:41 +02004094 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004095
Francois Romieucecb5fd2011-04-01 10:21:07 +02004096 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4097 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4098 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4099 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004100 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4101 rtl_set_rx_tx_config_registers(tp);
4102 }
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004105
4106 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4107 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
4109 RTL_W32(RxMissed, 0);
4110
Francois Romieu07ce4062007-02-23 23:36:39 +01004111 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
4113 /* no early-rx interrupts */
4114 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004115
4116 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01004117 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Francois Romieu9c14cea2008-07-05 00:21:15 +02004120static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02004121{
Jon Masone44daad2011-06-27 07:46:31 +00004122 int cap = pci_pcie_cap(pdev);
Francois Romieu458a9f62008-08-02 15:50:02 +02004123
Francois Romieu9c14cea2008-07-05 00:21:15 +02004124 if (cap) {
4125 u16 ctl;
4126
4127 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
4128 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
4129 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
4130 }
Francois Romieu458a9f62008-08-02 15:50:02 +02004131}
4132
françois romieu650e8d52011-01-03 15:08:29 +00004133static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004134{
4135 u32 csi;
4136
4137 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004138 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4139}
4140
françois romieue6de30d2011-01-03 15:08:37 +00004141static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4142{
4143 rtl_csi_access_enable(ioaddr, 0x17000000);
4144}
4145
françois romieu650e8d52011-01-03 15:08:29 +00004146static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4147{
4148 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004149}
4150
4151struct ephy_info {
4152 unsigned int offset;
4153 u16 mask;
4154 u16 bits;
4155};
4156
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004157static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004158{
4159 u16 w;
4160
4161 while (len-- > 0) {
4162 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4163 rtl_ephy_write(ioaddr, e->offset, w);
4164 e++;
4165 }
4166}
4167
Francois Romieub726e492008-06-28 12:22:59 +02004168static void rtl_disable_clock_request(struct pci_dev *pdev)
4169{
Jon Masone44daad2011-06-27 07:46:31 +00004170 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004171
4172 if (cap) {
4173 u16 ctl;
4174
4175 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4176 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4177 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4178 }
4179}
4180
françois romieue6de30d2011-01-03 15:08:37 +00004181static void rtl_enable_clock_request(struct pci_dev *pdev)
4182{
Jon Masone44daad2011-06-27 07:46:31 +00004183 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004184
4185 if (cap) {
4186 u16 ctl;
4187
4188 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4189 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4190 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4191 }
4192}
4193
Francois Romieub726e492008-06-28 12:22:59 +02004194#define R8168_CPCMD_QUIRK_MASK (\
4195 EnableBist | \
4196 Mac_dbgo_oe | \
4197 Force_half_dup | \
4198 Force_rxflow_en | \
4199 Force_txflow_en | \
4200 Cxpl_dbg_sel | \
4201 ASF | \
4202 PktCntrDisable | \
4203 Mac_dbgo_sel)
4204
Francois Romieu219a1e92008-06-28 11:58:39 +02004205static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4206{
Francois Romieub726e492008-06-28 12:22:59 +02004207 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4208
4209 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4210
Francois Romieu2e68ae42008-06-28 12:00:55 +02004211 rtl_tx_performance_tweak(pdev,
4212 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004213}
4214
4215static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4216{
4217 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004218
françois romieuf0298f82011-01-03 15:07:42 +00004219 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004220
4221 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004222}
4223
4224static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4225{
Francois Romieub726e492008-06-28 12:22:59 +02004226 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4227
4228 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4229
Francois Romieu219a1e92008-06-28 11:58:39 +02004230 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004231
4232 rtl_disable_clock_request(pdev);
4233
4234 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004235}
4236
Francois Romieuef3386f2008-06-29 12:24:30 +02004237static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004238{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004239 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004240 { 0x01, 0, 0x0001 },
4241 { 0x02, 0x0800, 0x1000 },
4242 { 0x03, 0, 0x0042 },
4243 { 0x06, 0x0080, 0x0000 },
4244 { 0x07, 0, 0x2000 }
4245 };
4246
françois romieu650e8d52011-01-03 15:08:29 +00004247 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004248
4249 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4250
Francois Romieu219a1e92008-06-28 11:58:39 +02004251 __rtl_hw_start_8168cp(ioaddr, pdev);
4252}
4253
Francois Romieuef3386f2008-06-29 12:24:30 +02004254static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4255{
françois romieu650e8d52011-01-03 15:08:29 +00004256 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004257
4258 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4259
4260 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4261
4262 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4263}
4264
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004265static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4266{
françois romieu650e8d52011-01-03 15:08:29 +00004267 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004268
4269 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4270
4271 /* Magic. */
4272 RTL_W8(DBG_REG, 0x20);
4273
françois romieuf0298f82011-01-03 15:07:42 +00004274 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004275
4276 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4277
4278 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4279}
4280
Francois Romieu219a1e92008-06-28 11:58:39 +02004281static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4282{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004283 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004284 { 0x02, 0x0800, 0x1000 },
4285 { 0x03, 0, 0x0002 },
4286 { 0x06, 0x0080, 0x0000 }
4287 };
4288
françois romieu650e8d52011-01-03 15:08:29 +00004289 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004290
4291 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4292
4293 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4294
Francois Romieu219a1e92008-06-28 11:58:39 +02004295 __rtl_hw_start_8168cp(ioaddr, pdev);
4296}
4297
4298static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4299{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004300 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004301 { 0x01, 0, 0x0001 },
4302 { 0x03, 0x0400, 0x0220 }
4303 };
4304
françois romieu650e8d52011-01-03 15:08:29 +00004305 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004306
4307 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4308
Francois Romieu219a1e92008-06-28 11:58:39 +02004309 __rtl_hw_start_8168cp(ioaddr, pdev);
4310}
4311
Francois Romieu197ff762008-06-28 13:16:02 +02004312static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4313{
4314 rtl_hw_start_8168c_2(ioaddr, pdev);
4315}
4316
Francois Romieu6fb07052008-06-29 11:54:28 +02004317static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4318{
françois romieu650e8d52011-01-03 15:08:29 +00004319 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004320
4321 __rtl_hw_start_8168cp(ioaddr, pdev);
4322}
4323
Francois Romieu5b538df2008-07-20 16:22:45 +02004324static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4325{
françois romieu650e8d52011-01-03 15:08:29 +00004326 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004327
4328 rtl_disable_clock_request(pdev);
4329
françois romieuf0298f82011-01-03 15:07:42 +00004330 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004331
4332 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4333
4334 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4335}
4336
hayeswang4804b3b2011-03-21 01:50:29 +00004337static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4338{
4339 rtl_csi_access_enable_1(ioaddr);
4340
4341 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4342
4343 RTL_W8(MaxTxPacketSize, TxPacketMax);
4344
4345 rtl_disable_clock_request(pdev);
4346}
4347
françois romieue6de30d2011-01-03 15:08:37 +00004348static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4349{
4350 static const struct ephy_info e_info_8168d_4[] = {
4351 { 0x0b, ~0, 0x48 },
4352 { 0x19, 0x20, 0x50 },
4353 { 0x0c, ~0, 0x20 }
4354 };
4355 int i;
4356
4357 rtl_csi_access_enable_1(ioaddr);
4358
4359 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4360
4361 RTL_W8(MaxTxPacketSize, TxPacketMax);
4362
4363 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4364 const struct ephy_info *e = e_info_8168d_4 + i;
4365 u16 w;
4366
4367 w = rtl_ephy_read(ioaddr, e->offset);
4368 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4369 }
4370
4371 rtl_enable_clock_request(pdev);
4372}
4373
Hayes Wang70090422011-07-06 15:58:06 +08004374static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004375{
Hayes Wang70090422011-07-06 15:58:06 +08004376 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004377 { 0x00, 0x0200, 0x0100 },
4378 { 0x00, 0x0000, 0x0004 },
4379 { 0x06, 0x0002, 0x0001 },
4380 { 0x06, 0x0000, 0x0030 },
4381 { 0x07, 0x0000, 0x2000 },
4382 { 0x00, 0x0000, 0x0020 },
4383 { 0x03, 0x5800, 0x2000 },
4384 { 0x03, 0x0000, 0x0001 },
4385 { 0x01, 0x0800, 0x1000 },
4386 { 0x07, 0x0000, 0x4000 },
4387 { 0x1e, 0x0000, 0x2000 },
4388 { 0x19, 0xffff, 0xfe6c },
4389 { 0x0a, 0x0000, 0x0040 }
4390 };
4391
4392 rtl_csi_access_enable_2(ioaddr);
4393
Hayes Wang70090422011-07-06 15:58:06 +08004394 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004395
4396 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4397
4398 RTL_W8(MaxTxPacketSize, TxPacketMax);
4399
4400 rtl_disable_clock_request(pdev);
4401
4402 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004403 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4404 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004405
Francois Romieucecb5fd2011-04-01 10:21:07 +02004406 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004407}
4408
Hayes Wang70090422011-07-06 15:58:06 +08004409static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4410{
4411 static const struct ephy_info e_info_8168e_2[] = {
4412 { 0x09, 0x0000, 0x0080 },
4413 { 0x19, 0x0000, 0x0224 }
4414 };
4415
4416 rtl_csi_access_enable_1(ioaddr);
4417
4418 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4419
4420 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4421
4422 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4423 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4424 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4425 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4426 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4427 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4428 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4429 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4430 ERIAR_EXGMAC);
4431
4432 RTL_W8(MaxTxPacketSize, 0x27);
4433
4434 rtl_disable_clock_request(pdev);
4435
4436 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4437 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4438
4439 /* Adjust EEE LED frequency */
4440 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4441
4442 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4443 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4444 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4445}
4446
Francois Romieu07ce4062007-02-23 23:36:39 +01004447static void rtl_hw_start_8168(struct net_device *dev)
4448{
Francois Romieu2dd99532007-06-11 23:22:52 +02004449 struct rtl8169_private *tp = netdev_priv(dev);
4450 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004451 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004452
4453 RTL_W8(Cfg9346, Cfg9346_Unlock);
4454
françois romieuf0298f82011-01-03 15:07:42 +00004455 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004456
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004457 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004458
Francois Romieu0e485152007-02-20 00:00:26 +01004459 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004460
4461 RTL_W16(CPlusCmd, tp->cp_cmd);
4462
Francois Romieu0e485152007-02-20 00:00:26 +01004463 RTL_W16(IntrMitigate, 0x5151);
4464
4465 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004466 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4467 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004468 tp->intr_event |= RxFIFOOver | PCSTimeout;
4469 tp->intr_event &= ~RxOverflow;
4470 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004471
4472 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4473
Francois Romieub8363902008-06-01 12:31:57 +02004474 rtl_set_rx_mode(dev);
4475
4476 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4477 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004478
4479 RTL_R8(IntrMask);
4480
Francois Romieu219a1e92008-06-28 11:58:39 +02004481 switch (tp->mac_version) {
4482 case RTL_GIGA_MAC_VER_11:
4483 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004484 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004485
4486 case RTL_GIGA_MAC_VER_12:
4487 case RTL_GIGA_MAC_VER_17:
4488 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004489 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004490
4491 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004492 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004493 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004494
4495 case RTL_GIGA_MAC_VER_19:
4496 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004497 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004498
4499 case RTL_GIGA_MAC_VER_20:
4500 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004501 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004502
Francois Romieu197ff762008-06-28 13:16:02 +02004503 case RTL_GIGA_MAC_VER_21:
4504 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004505 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004506
Francois Romieu6fb07052008-06-29 11:54:28 +02004507 case RTL_GIGA_MAC_VER_22:
4508 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004509 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004510
Francois Romieuef3386f2008-06-29 12:24:30 +02004511 case RTL_GIGA_MAC_VER_23:
4512 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004513 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004514
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004515 case RTL_GIGA_MAC_VER_24:
4516 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004517 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004518
Francois Romieu5b538df2008-07-20 16:22:45 +02004519 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004520 case RTL_GIGA_MAC_VER_26:
4521 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004522 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004523 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004524
françois romieue6de30d2011-01-03 15:08:37 +00004525 case RTL_GIGA_MAC_VER_28:
4526 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004527 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004528
hayeswang4804b3b2011-03-21 01:50:29 +00004529 case RTL_GIGA_MAC_VER_31:
4530 rtl_hw_start_8168dp(ioaddr, pdev);
4531 break;
4532
hayeswang01dc7fe2011-03-21 01:50:28 +00004533 case RTL_GIGA_MAC_VER_32:
4534 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004535 rtl_hw_start_8168e_1(ioaddr, pdev);
4536 break;
4537 case RTL_GIGA_MAC_VER_34:
4538 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004539 break;
françois romieue6de30d2011-01-03 15:08:37 +00004540
Francois Romieu219a1e92008-06-28 11:58:39 +02004541 default:
4542 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4543 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004544 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004545 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004546
Francois Romieu0e485152007-02-20 00:00:26 +01004547 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4548
Francois Romieub8363902008-06-01 12:31:57 +02004549 RTL_W8(Cfg9346, Cfg9346_Lock);
4550
Francois Romieu2dd99532007-06-11 23:22:52 +02004551 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004552
Francois Romieu0e485152007-02-20 00:00:26 +01004553 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004554}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Francois Romieu2857ffb2008-08-02 21:08:49 +02004556#define R810X_CPCMD_QUIRK_MASK (\
4557 EnableBist | \
4558 Mac_dbgo_oe | \
4559 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004560 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004561 Force_txflow_en | \
4562 Cxpl_dbg_sel | \
4563 ASF | \
4564 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004565 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004566
4567static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4568{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004569 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004570 { 0x01, 0, 0x6e65 },
4571 { 0x02, 0, 0x091f },
4572 { 0x03, 0, 0xc2f9 },
4573 { 0x06, 0, 0xafb5 },
4574 { 0x07, 0, 0x0e00 },
4575 { 0x19, 0, 0xec80 },
4576 { 0x01, 0, 0x2e65 },
4577 { 0x01, 0, 0x6e65 }
4578 };
4579 u8 cfg1;
4580
françois romieu650e8d52011-01-03 15:08:29 +00004581 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004582
4583 RTL_W8(DBG_REG, FIX_NAK_1);
4584
4585 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4586
4587 RTL_W8(Config1,
4588 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4589 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4590
4591 cfg1 = RTL_R8(Config1);
4592 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4593 RTL_W8(Config1, cfg1 & ~LEDS0);
4594
Francois Romieu2857ffb2008-08-02 21:08:49 +02004595 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4596}
4597
4598static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4599{
françois romieu650e8d52011-01-03 15:08:29 +00004600 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004601
4602 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4603
4604 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4605 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004606}
4607
4608static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4609{
4610 rtl_hw_start_8102e_2(ioaddr, pdev);
4611
4612 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4613}
4614
Hayes Wang5a5e4442011-02-22 17:26:21 +08004615static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4616{
4617 static const struct ephy_info e_info_8105e_1[] = {
4618 { 0x07, 0, 0x4000 },
4619 { 0x19, 0, 0x0200 },
4620 { 0x19, 0, 0x0020 },
4621 { 0x1e, 0, 0x2000 },
4622 { 0x03, 0, 0x0001 },
4623 { 0x19, 0, 0x0100 },
4624 { 0x19, 0, 0x0004 },
4625 { 0x0a, 0, 0x0020 }
4626 };
4627
Francois Romieucecb5fd2011-04-01 10:21:07 +02004628 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004629 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4630
Francois Romieucecb5fd2011-04-01 10:21:07 +02004631 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004632 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4633
4634 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e52011-07-06 15:58:02 +08004635 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004636
4637 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4638}
4639
4640static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4641{
4642 rtl_hw_start_8105e_1(ioaddr, pdev);
4643 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4644}
4645
Francois Romieu07ce4062007-02-23 23:36:39 +01004646static void rtl_hw_start_8101(struct net_device *dev)
4647{
Francois Romieucdf1a602007-06-11 23:29:50 +02004648 struct rtl8169_private *tp = netdev_priv(dev);
4649 void __iomem *ioaddr = tp->mmio_addr;
4650 struct pci_dev *pdev = tp->pci_dev;
4651
Francois Romieucecb5fd2011-04-01 10:21:07 +02004652 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4653 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004654 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004655
4656 if (cap) {
4657 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4658 PCI_EXP_DEVCTL_NOSNOOP_EN);
4659 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004660 }
4661
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004662 RTL_W8(Cfg9346, Cfg9346_Unlock);
4663
Francois Romieu2857ffb2008-08-02 21:08:49 +02004664 switch (tp->mac_version) {
4665 case RTL_GIGA_MAC_VER_07:
4666 rtl_hw_start_8102e_1(ioaddr, pdev);
4667 break;
4668
4669 case RTL_GIGA_MAC_VER_08:
4670 rtl_hw_start_8102e_3(ioaddr, pdev);
4671 break;
4672
4673 case RTL_GIGA_MAC_VER_09:
4674 rtl_hw_start_8102e_2(ioaddr, pdev);
4675 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004676
4677 case RTL_GIGA_MAC_VER_29:
4678 rtl_hw_start_8105e_1(ioaddr, pdev);
4679 break;
4680 case RTL_GIGA_MAC_VER_30:
4681 rtl_hw_start_8105e_2(ioaddr, pdev);
4682 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004683 }
4684
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004685 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004686
françois romieuf0298f82011-01-03 15:07:42 +00004687 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004688
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004689 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004690
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004691 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004692 RTL_W16(CPlusCmd, tp->cp_cmd);
4693
4694 RTL_W16(IntrMitigate, 0x0000);
4695
4696 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4697
4698 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4699 rtl_set_rx_tx_config_registers(tp);
4700
Francois Romieucdf1a602007-06-11 23:29:50 +02004701 RTL_R8(IntrMask);
4702
Francois Romieucdf1a602007-06-11 23:29:50 +02004703 rtl_set_rx_mode(dev);
4704
4705 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004706
Francois Romieu0e485152007-02-20 00:00:26 +01004707 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708}
4709
4710static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4711{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4713 return -EINVAL;
4714
4715 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004716 netdev_update_features(dev);
4717
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719}
4720
4721static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4722{
Al Viro95e09182007-12-22 18:55:39 +00004723 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4725}
4726
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004727static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4728 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004730 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004731 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004732
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004733 kfree(*data_buff);
4734 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 rtl8169_make_unusable_by_asic(desc);
4736}
4737
4738static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4739{
4740 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4741
4742 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4743}
4744
4745static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4746 u32 rx_buf_sz)
4747{
4748 desc->addr = cpu_to_le64(mapping);
4749 wmb();
4750 rtl8169_mark_to_asic(desc, rx_buf_sz);
4751}
4752
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004753static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004755 return (void *)ALIGN((long)data, 16);
4756}
4757
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004758static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4759 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004760{
4761 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004763 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004764 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004765 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004767 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4768 if (!data)
4769 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004770
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004771 if (rtl8169_align(data) != data) {
4772 kfree(data);
4773 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4774 if (!data)
4775 return NULL;
4776 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004777
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004778 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004779 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004780 if (unlikely(dma_mapping_error(d, mapping))) {
4781 if (net_ratelimit())
4782 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004783 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
4786 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004787 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004788
4789err_out:
4790 kfree(data);
4791 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792}
4793
4794static void rtl8169_rx_clear(struct rtl8169_private *tp)
4795{
Francois Romieu07d3f512007-02-21 22:40:46 +01004796 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
4798 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004799 if (tp->Rx_databuff[i]) {
4800 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 tp->RxDescArray + i);
4802 }
4803 }
4804}
4805
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004806static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004808 desc->opts1 |= cpu_to_le32(RingEnd);
4809}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004810
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004811static int rtl8169_rx_fill(struct rtl8169_private *tp)
4812{
4813 unsigned int i;
4814
4815 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004816 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004817
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004818 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004820
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004821 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004822 if (!data) {
4823 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004824 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004825 }
4826 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004829 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4830 return 0;
4831
4832err_out:
4833 rtl8169_rx_clear(tp);
4834 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837static int rtl8169_init_ring(struct net_device *dev)
4838{
4839 struct rtl8169_private *tp = netdev_priv(dev);
4840
4841 rtl8169_init_ring_indexes(tp);
4842
4843 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004844 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004846 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847}
4848
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004849static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 struct TxDesc *desc)
4851{
4852 unsigned int len = tx_skb->len;
4853
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004854 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 desc->opts1 = 0x00;
4857 desc->opts2 = 0x00;
4858 desc->addr = 0x00;
4859 tx_skb->len = 0;
4860}
4861
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004862static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4863 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
4865 unsigned int i;
4866
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004867 for (i = 0; i < n; i++) {
4868 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 struct ring_info *tx_skb = tp->tx_skb + entry;
4870 unsigned int len = tx_skb->len;
4871
4872 if (len) {
4873 struct sk_buff *skb = tx_skb->skb;
4874
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004875 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 tp->TxDescArray + entry);
4877 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004878 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 dev_kfree_skb(skb);
4880 tx_skb->skb = NULL;
4881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 }
4883 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004884}
4885
4886static void rtl8169_tx_clear(struct rtl8169_private *tp)
4887{
4888 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 tp->cur_tx = tp->dirty_tx = 0;
4890}
4891
David Howellsc4028952006-11-22 14:57:56 +00004892static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893{
4894 struct rtl8169_private *tp = netdev_priv(dev);
4895
David Howellsc4028952006-11-22 14:57:56 +00004896 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 schedule_delayed_work(&tp->task, 4);
4898}
4899
4900static void rtl8169_wait_for_quiescence(struct net_device *dev)
4901{
4902 struct rtl8169_private *tp = netdev_priv(dev);
4903 void __iomem *ioaddr = tp->mmio_addr;
4904
4905 synchronize_irq(dev->irq);
4906
4907 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004908 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
4910 rtl8169_irq_mask_and_ack(ioaddr);
4911
David S. Millerd1d08d12008-01-07 20:53:33 -08004912 tp->intr_mask = 0xffff;
4913 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004914 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915}
4916
David Howellsc4028952006-11-22 14:57:56 +00004917static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
David Howellsc4028952006-11-22 14:57:56 +00004919 struct rtl8169_private *tp =
4920 container_of(work, struct rtl8169_private, task.work);
4921 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 int ret;
4923
Francois Romieueb2a0212007-02-15 23:37:21 +01004924 rtnl_lock();
4925
4926 if (!netif_running(dev))
4927 goto out_unlock;
4928
4929 rtl8169_wait_for_quiescence(dev);
4930 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
4932 ret = rtl8169_open(dev);
4933 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004934 if (net_ratelimit())
4935 netif_err(tp, drv, dev,
4936 "reinit failure (status = %d). Rescheduling\n",
4937 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4939 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004940
4941out_unlock:
4942 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943}
4944
David Howellsc4028952006-11-22 14:57:56 +00004945static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946{
David Howellsc4028952006-11-22 14:57:56 +00004947 struct rtl8169_private *tp =
4948 container_of(work, struct rtl8169_private, task.work);
4949 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004950 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
Francois Romieueb2a0212007-02-15 23:37:21 +01004952 rtnl_lock();
4953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004955 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
4957 rtl8169_wait_for_quiescence(dev);
4958
Francois Romieu56de4142011-03-15 17:29:31 +01004959 for (i = 0; i < NUM_RX_DESC; i++)
4960 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 rtl8169_tx_clear(tp);
4963
Hayes Wang92fc43b2011-07-06 15:58:03 +08004964 rtl8169_hw_reset(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01004965 rtl_hw_start(dev);
4966 netif_wake_queue(dev);
4967 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004968
4969out_unlock:
4970 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971}
4972
4973static void rtl8169_tx_timeout(struct net_device *dev)
4974{
4975 struct rtl8169_private *tp = netdev_priv(dev);
4976
françois romieue6de30d2011-01-03 15:08:37 +00004977 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
4979 /* Let's wait a bit while any (async) irq lands on */
4980 rtl8169_schedule_work(dev, rtl8169_reset_task);
4981}
4982
4983static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004984 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985{
4986 struct skb_shared_info *info = skb_shinfo(skb);
4987 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004988 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004989 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
4991 entry = tp->cur_tx;
4992 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4993 skb_frag_t *frag = info->frags + cur_frag;
4994 dma_addr_t mapping;
4995 u32 status, len;
4996 void *addr;
4997
4998 entry = (entry + 1) % NUM_TX_DESC;
4999
5000 txd = tp->TxDescArray + entry;
5001 len = frag->size;
5002 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005003 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005004 if (unlikely(dma_mapping_error(d, mapping))) {
5005 if (net_ratelimit())
5006 netif_err(tp, drv, tp->dev,
5007 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005008 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
Francois Romieucecb5fd2011-04-01 10:21:07 +02005011 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005012 status = opts[0] | len |
5013 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
5015 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005016 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 txd->addr = cpu_to_le64(mapping);
5018
5019 tp->tx_skb[entry].len = len;
5020 }
5021
5022 if (cur_frag) {
5023 tp->tx_skb[entry].skb = skb;
5024 txd->opts1 |= cpu_to_le32(LastFrag);
5025 }
5026
5027 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005028
5029err_out:
5030 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5031 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032}
5033
Francois Romieu2b7b4312011-04-18 22:53:24 -07005034static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5035 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005037 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005038 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005039 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
Francois Romieu2b7b4312011-04-18 22:53:24 -07005041 if (mss) {
5042 opts[0] |= TD_LSO;
5043 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5044 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005045 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
5047 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005048 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005050 opts[offset] |= info->checksum.udp;
5051 else
5052 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054}
5055
Stephen Hemminger613573252009-08-31 19:50:58 +00005056static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5057 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
5059 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005060 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 struct TxDesc *txd = tp->TxDescArray + entry;
5062 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005063 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 dma_addr_t mapping;
5065 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005066 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005067 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005068
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005070 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005071 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 }
5073
5074 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005075 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005077 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005078 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005079 if (unlikely(dma_mapping_error(d, mapping))) {
5080 if (net_ratelimit())
5081 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005082 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
5085 tp->tx_skb[entry].len = len;
5086 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Francois Romieu2b7b4312011-04-18 22:53:24 -07005088 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5089 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005090
Francois Romieu2b7b4312011-04-18 22:53:24 -07005091 rtl8169_tso_csum(tp, skb, opts);
5092
5093 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005094 if (frags < 0)
5095 goto err_dma_1;
5096 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005097 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005098 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005099 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005100 tp->tx_skb[entry].skb = skb;
5101 }
5102
Francois Romieu2b7b4312011-04-18 22:53:24 -07005103 txd->opts2 = cpu_to_le32(opts[1]);
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 wmb();
5106
Francois Romieucecb5fd2011-04-01 10:21:07 +02005107 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005108 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 txd->opts1 = cpu_to_le32(status);
5110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 tp->cur_tx += frags + 1;
5112
David Dillow4c020a92010-03-03 16:33:10 +00005113 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
Francois Romieucecb5fd2011-04-01 10:21:07 +02005115 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116
5117 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5118 netif_stop_queue(dev);
5119 smp_rmb();
5120 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5121 netif_wake_queue(dev);
5122 }
5123
Stephen Hemminger613573252009-08-31 19:50:58 +00005124 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005126err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005127 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005128err_dma_0:
5129 dev_kfree_skb(skb);
5130 dev->stats.tx_dropped++;
5131 return NETDEV_TX_OK;
5132
5133err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005135 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005136 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137}
5138
5139static void rtl8169_pcierr_interrupt(struct net_device *dev)
5140{
5141 struct rtl8169_private *tp = netdev_priv(dev);
5142 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 u16 pci_status, pci_cmd;
5144
5145 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5146 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5147
Joe Perchesbf82c182010-02-09 11:49:50 +00005148 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5149 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
5151 /*
5152 * The recovery sequence below admits a very elaborated explanation:
5153 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005154 * - I did not see what else could be done;
5155 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 *
5157 * Feel free to adjust to your needs.
5158 */
Francois Romieua27993f2006-12-18 00:04:19 +01005159 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005160 pci_cmd &= ~PCI_COMMAND_PARITY;
5161 else
5162 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5163
5164 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
5166 pci_write_config_word(pdev, PCI_STATUS,
5167 pci_status & (PCI_STATUS_DETECTED_PARITY |
5168 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5169 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5170
5171 /* The infamous DAC f*ckup only happens at boot time */
5172 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005173 void __iomem *ioaddr = tp->mmio_addr;
5174
Joe Perchesbf82c182010-02-09 11:49:50 +00005175 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 tp->cp_cmd &= ~PCIDAC;
5177 RTL_W16(CPlusCmd, tp->cp_cmd);
5178 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 }
5180
françois romieue6de30d2011-01-03 15:08:37 +00005181 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005182
5183 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184}
5185
Francois Romieu07d3f512007-02-21 22:40:46 +01005186static void rtl8169_tx_interrupt(struct net_device *dev,
5187 struct rtl8169_private *tp,
5188 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189{
5190 unsigned int dirty_tx, tx_left;
5191
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 dirty_tx = tp->dirty_tx;
5193 smp_rmb();
5194 tx_left = tp->cur_tx - dirty_tx;
5195
5196 while (tx_left > 0) {
5197 unsigned int entry = dirty_tx % NUM_TX_DESC;
5198 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 u32 status;
5200
5201 rmb();
5202 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5203 if (status & DescOwn)
5204 break;
5205
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005206 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5207 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005209 dev->stats.tx_packets++;
5210 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005211 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 tx_skb->skb = NULL;
5213 }
5214 dirty_tx++;
5215 tx_left--;
5216 }
5217
5218 if (tp->dirty_tx != dirty_tx) {
5219 tp->dirty_tx = dirty_tx;
5220 smp_wmb();
5221 if (netif_queue_stopped(dev) &&
5222 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5223 netif_wake_queue(dev);
5224 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005225 /*
5226 * 8168 hack: TxPoll requests are lost when the Tx packets are
5227 * too close. Let's kick an extra TxPoll request when a burst
5228 * of start_xmit activity is detected (if it is not detected,
5229 * it is slow enough). -- FR
5230 */
5231 smp_rmb();
5232 if (tp->cur_tx != dirty_tx)
5233 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 }
5235}
5236
Francois Romieu126fa4b2005-05-12 20:09:17 -04005237static inline int rtl8169_fragmented_frame(u32 status)
5238{
5239 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5240}
5241
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005242static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 u32 status = opts1 & RxProtoMask;
5245
5246 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005247 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 skb->ip_summed = CHECKSUM_UNNECESSARY;
5249 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005250 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251}
5252
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005253static struct sk_buff *rtl8169_try_rx_copy(void *data,
5254 struct rtl8169_private *tp,
5255 int pkt_size,
5256 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005258 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005259 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005261 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005262 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005263 prefetch(data);
5264 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5265 if (skb)
5266 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005267 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5268
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005269 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270}
5271
Francois Romieu07d3f512007-02-21 22:40:46 +01005272static int rtl8169_rx_interrupt(struct net_device *dev,
5273 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005274 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275{
5276 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005277 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 cur_rx = tp->cur_rx;
5280 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005281 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005283 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005285 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 u32 status;
5287
5288 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04005289 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
5291 if (status & DescOwn)
5292 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005293 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005294 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5295 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005296 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005298 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005300 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005301 if (status & RxFOVF) {
5302 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005303 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005304 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005305 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005307 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005308 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Francois Romieu126fa4b2005-05-12 20:09:17 -04005311 /*
5312 * The driver does not support incoming fragmented
5313 * frames. They are seen as a symptom of over-mtu
5314 * sized frames.
5315 */
5316 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005317 dev->stats.rx_dropped++;
5318 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005319 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005320 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005321 }
5322
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005323 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5324 tp, pkt_size, addr);
5325 rtl8169_mark_to_asic(desc, rx_buf_sz);
5326 if (!skb) {
5327 dev->stats.rx_dropped++;
5328 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 }
5330
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005331 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 skb_put(skb, pkt_size);
5333 skb->protocol = eth_type_trans(skb, dev);
5334
Francois Romieu7a8fc772011-03-01 17:18:33 +01005335 rtl8169_rx_vlan_tag(desc, skb);
5336
Francois Romieu56de4142011-03-15 17:29:31 +01005337 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
Francois Romieucebf8cc2007-10-18 12:06:54 +02005339 dev->stats.rx_bytes += pkt_size;
5340 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005342
5343 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005344 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005345 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5346 desc->opts2 = 0;
5347 cur_rx++;
5348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 }
5350
5351 count = cur_rx - tp->cur_rx;
5352 tp->cur_rx = cur_rx;
5353
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005354 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
5356 return count;
5357}
5358
Francois Romieu07d3f512007-02-21 22:40:46 +01005359static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360{
Francois Romieu07d3f512007-02-21 22:40:46 +01005361 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005365 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
David Dillowf11a3772009-05-22 15:29:34 +00005367 /* loop handling interrupts until we have no new ones or
5368 * we hit a invalid/hotplug case.
5369 */
Francois Romieu865c6522008-05-11 14:51:00 +02005370 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005371 while (status && status != 0xffff) {
5372 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
David Dillowf11a3772009-05-22 15:29:34 +00005374 /* Handle all of the error cases first. These will reset
5375 * the chip, so just exit the loop.
5376 */
5377 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005378 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 }
David Dillowf11a3772009-05-22 15:29:34 +00005381
Francois Romieu1519e572011-02-03 12:02:36 +01005382 if (unlikely(status & RxFIFOOver)) {
5383 switch (tp->mac_version) {
5384 /* Work around for rx fifo overflow */
5385 case RTL_GIGA_MAC_VER_11:
5386 case RTL_GIGA_MAC_VER_22:
5387 case RTL_GIGA_MAC_VER_26:
5388 netif_stop_queue(dev);
5389 rtl8169_tx_timeout(dev);
5390 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005391 /* Testers needed. */
5392 case RTL_GIGA_MAC_VER_17:
5393 case RTL_GIGA_MAC_VER_19:
5394 case RTL_GIGA_MAC_VER_20:
5395 case RTL_GIGA_MAC_VER_21:
5396 case RTL_GIGA_MAC_VER_23:
5397 case RTL_GIGA_MAC_VER_24:
5398 case RTL_GIGA_MAC_VER_27:
5399 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005400 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005401 /* Experimental science. Pktgen proof. */
5402 case RTL_GIGA_MAC_VER_12:
5403 case RTL_GIGA_MAC_VER_25:
5404 if (status == RxFIFOOver)
5405 goto done;
5406 break;
5407 default:
5408 break;
5409 }
David Dillowf11a3772009-05-22 15:29:34 +00005410 }
5411
5412 if (unlikely(status & SYSErr)) {
5413 rtl8169_pcierr_interrupt(dev);
5414 break;
5415 }
5416
5417 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005418 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005419
5420 /* We need to see the lastest version of tp->intr_mask to
5421 * avoid ignoring an MSI interrupt and having to wait for
5422 * another event which may never come.
5423 */
5424 smp_rmb();
5425 if (status & tp->intr_mask & tp->napi_event) {
5426 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5427 tp->intr_mask = ~tp->napi_event;
5428
5429 if (likely(napi_schedule_prep(&tp->napi)))
5430 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005431 else
5432 netif_info(tp, intr, dev,
5433 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005434 }
5435
5436 /* We only get a new MSI interrupt when all active irq
5437 * sources on the chip have been acknowledged. So, ack
5438 * everything we've seen and check if new sources have become
5439 * active to avoid blocking all interrupts from the chip.
5440 */
5441 RTL_W16(IntrStatus,
5442 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5443 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 }
Francois Romieu1519e572011-02-03 12:02:36 +01005445done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 return IRQ_RETVAL(handled);
5447}
5448
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005449static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005451 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5452 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005454 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005456 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 rtl8169_tx_interrupt(dev, tp, ioaddr);
5458
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005459 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005460 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005461
5462 /* We need for force the visibility of tp->intr_mask
5463 * for other CPUs, as we can loose an MSI interrupt
5464 * and potentially wait for a retransmit timeout if we don't.
5465 * The posted write to IntrMask is safe, as it will
5466 * eventually make it to the chip and we won't loose anything
5467 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 */
David Dillowf11a3772009-05-22 15:29:34 +00005469 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005470 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005471 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 }
5473
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005474 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
Francois Romieu523a6092008-09-10 22:28:56 +02005477static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5478{
5479 struct rtl8169_private *tp = netdev_priv(dev);
5480
5481 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5482 return;
5483
5484 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5485 RTL_W32(RxMissed, 0);
5486}
5487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488static void rtl8169_down(struct net_device *dev)
5489{
5490 struct rtl8169_private *tp = netdev_priv(dev);
5491 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
Francois Romieu4876cc12011-03-11 21:07:11 +01005493 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
5495 netif_stop_queue(dev);
5496
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005497 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005498
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 spin_lock_irq(&tp->lock);
5500
Hayes Wang92fc43b2011-07-06 15:58:03 +08005501 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005502 /*
5503 * At this point device interrupts can not be enabled in any function,
5504 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5505 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5506 */
Francois Romieu523a6092008-09-10 22:28:56 +02005507 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
5509 spin_unlock_irq(&tp->lock);
5510
5511 synchronize_irq(dev->irq);
5512
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005514 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 rtl8169_tx_clear(tp);
5517
5518 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005519
5520 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521}
5522
5523static int rtl8169_close(struct net_device *dev)
5524{
5525 struct rtl8169_private *tp = netdev_priv(dev);
5526 struct pci_dev *pdev = tp->pci_dev;
5527
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005528 pm_runtime_get_sync(&pdev->dev);
5529
Francois Romieucecb5fd2011-04-01 10:21:07 +02005530 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005531 rtl8169_update_counters(dev);
5532
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 rtl8169_down(dev);
5534
5535 free_irq(dev->irq, dev);
5536
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005537 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5538 tp->RxPhyAddr);
5539 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5540 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 tp->TxDescArray = NULL;
5542 tp->RxDescArray = NULL;
5543
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005544 pm_runtime_put_sync(&pdev->dev);
5545
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 return 0;
5547}
5548
Francois Romieu07ce4062007-02-23 23:36:39 +01005549static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550{
5551 struct rtl8169_private *tp = netdev_priv(dev);
5552 void __iomem *ioaddr = tp->mmio_addr;
5553 unsigned long flags;
5554 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005555 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 u32 tmp = 0;
5557
5558 if (dev->flags & IFF_PROMISC) {
5559 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005560 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 rx_mode =
5562 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5563 AcceptAllPhys;
5564 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005565 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005566 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 /* Too many to filter perfectly -- accept all multicasts. */
5568 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5569 mc_filter[1] = mc_filter[0] = 0xffffffff;
5570 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00005571 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 rx_mode = AcceptBroadcast | AcceptMyPhys;
5574 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00005575 netdev_for_each_mc_addr(ha, dev) {
5576 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5578 rx_mode |= AcceptMulticast;
5579 }
5580 }
5581
5582 spin_lock_irqsave(&tp->lock, flags);
5583
Francois Romieu1687b562011-07-19 17:21:29 +02005584 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
Francois Romieuf887cce2008-07-17 22:24:18 +02005586 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005587 u32 data = mc_filter[0];
5588
5589 mc_filter[0] = swab32(mc_filter[1]);
5590 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005591 }
5592
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005594 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Francois Romieu57a9f232007-06-04 22:10:15 +02005596 RTL_W32(RxConfig, tmp);
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 spin_unlock_irqrestore(&tp->lock, flags);
5599}
5600
5601/**
5602 * rtl8169_get_stats - Get rtl8169 read/write statistics
5603 * @dev: The Ethernet Device to get statistics for
5604 *
5605 * Get TX/RX statistics for rtl8169
5606 */
5607static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5608{
5609 struct rtl8169_private *tp = netdev_priv(dev);
5610 void __iomem *ioaddr = tp->mmio_addr;
5611 unsigned long flags;
5612
5613 if (netif_running(dev)) {
5614 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005615 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 spin_unlock_irqrestore(&tp->lock, flags);
5617 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005618
Francois Romieucebf8cc2007-10-18 12:06:54 +02005619 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620}
5621
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005622static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005623{
françois romieu065c27c2011-01-03 15:08:12 +00005624 struct rtl8169_private *tp = netdev_priv(dev);
5625
Francois Romieu5d06a992006-02-23 00:47:58 +01005626 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005627 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005628
françois romieu065c27c2011-01-03 15:08:12 +00005629 rtl_pll_power_down(tp);
5630
Francois Romieu5d06a992006-02-23 00:47:58 +01005631 netif_device_detach(dev);
5632 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005633}
Francois Romieu5d06a992006-02-23 00:47:58 +01005634
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005635#ifdef CONFIG_PM
5636
5637static int rtl8169_suspend(struct device *device)
5638{
5639 struct pci_dev *pdev = to_pci_dev(device);
5640 struct net_device *dev = pci_get_drvdata(pdev);
5641
5642 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005643
Francois Romieu5d06a992006-02-23 00:47:58 +01005644 return 0;
5645}
5646
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005647static void __rtl8169_resume(struct net_device *dev)
5648{
françois romieu065c27c2011-01-03 15:08:12 +00005649 struct rtl8169_private *tp = netdev_priv(dev);
5650
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005651 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005652
5653 rtl_pll_power_up(tp);
5654
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005655 rtl8169_schedule_work(dev, rtl8169_reset_task);
5656}
5657
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005658static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005659{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005660 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005661 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005662 struct rtl8169_private *tp = netdev_priv(dev);
5663
5664 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005665
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005666 if (netif_running(dev))
5667 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005668
Francois Romieu5d06a992006-02-23 00:47:58 +01005669 return 0;
5670}
5671
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005672static int rtl8169_runtime_suspend(struct device *device)
5673{
5674 struct pci_dev *pdev = to_pci_dev(device);
5675 struct net_device *dev = pci_get_drvdata(pdev);
5676 struct rtl8169_private *tp = netdev_priv(dev);
5677
5678 if (!tp->TxDescArray)
5679 return 0;
5680
5681 spin_lock_irq(&tp->lock);
5682 tp->saved_wolopts = __rtl8169_get_wol(tp);
5683 __rtl8169_set_wol(tp, WAKE_ANY);
5684 spin_unlock_irq(&tp->lock);
5685
5686 rtl8169_net_suspend(dev);
5687
5688 return 0;
5689}
5690
5691static int rtl8169_runtime_resume(struct device *device)
5692{
5693 struct pci_dev *pdev = to_pci_dev(device);
5694 struct net_device *dev = pci_get_drvdata(pdev);
5695 struct rtl8169_private *tp = netdev_priv(dev);
5696
5697 if (!tp->TxDescArray)
5698 return 0;
5699
5700 spin_lock_irq(&tp->lock);
5701 __rtl8169_set_wol(tp, tp->saved_wolopts);
5702 tp->saved_wolopts = 0;
5703 spin_unlock_irq(&tp->lock);
5704
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005705 rtl8169_init_phy(dev, tp);
5706
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005707 __rtl8169_resume(dev);
5708
5709 return 0;
5710}
5711
5712static int rtl8169_runtime_idle(struct device *device)
5713{
5714 struct pci_dev *pdev = to_pci_dev(device);
5715 struct net_device *dev = pci_get_drvdata(pdev);
5716 struct rtl8169_private *tp = netdev_priv(dev);
5717
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005718 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005719}
5720
Alexey Dobriyan47145212009-12-14 18:00:08 -08005721static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005722 .suspend = rtl8169_suspend,
5723 .resume = rtl8169_resume,
5724 .freeze = rtl8169_suspend,
5725 .thaw = rtl8169_resume,
5726 .poweroff = rtl8169_suspend,
5727 .restore = rtl8169_resume,
5728 .runtime_suspend = rtl8169_runtime_suspend,
5729 .runtime_resume = rtl8169_runtime_resume,
5730 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005731};
5732
5733#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5734
5735#else /* !CONFIG_PM */
5736
5737#define RTL8169_PM_OPS NULL
5738
5739#endif /* !CONFIG_PM */
5740
Francois Romieu1765f952008-09-13 17:21:40 +02005741static void rtl_shutdown(struct pci_dev *pdev)
5742{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005743 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005744 struct rtl8169_private *tp = netdev_priv(dev);
5745 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005746
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005747 rtl8169_net_suspend(dev);
5748
Francois Romieucecb5fd2011-04-01 10:21:07 +02005749 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005750 rtl_rar_set(tp, dev->perm_addr);
5751
françois romieu4bb3f522009-06-17 11:41:45 +00005752 spin_lock_irq(&tp->lock);
5753
Hayes Wang92fc43b2011-07-06 15:58:03 +08005754 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005755
5756 spin_unlock_irq(&tp->lock);
5757
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005758 if (system_state == SYSTEM_POWER_OFF) {
Hayes Wangaaa89c02011-07-06 15:58:08 +08005759 /* WoL fails with 8168b when the receiver is disabled. */
5760 if ((tp->mac_version == RTL_GIGA_MAC_VER_11 ||
5761 tp->mac_version == RTL_GIGA_MAC_VER_12 ||
5762 tp->mac_version == RTL_GIGA_MAC_VER_17) &&
5763 (tp->features & RTL_FEATURE_WOL)) {
françois romieuca52efd2009-07-24 12:34:19 +00005764 pci_clear_master(pdev);
5765
5766 RTL_W8(ChipCmd, CmdRxEnb);
5767 /* PCI commit */
5768 RTL_R8(ChipCmd);
5769 }
5770
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005771 pci_wake_from_d3(pdev, true);
5772 pci_set_power_state(pdev, PCI_D3hot);
5773 }
5774}
Francois Romieu5d06a992006-02-23 00:47:58 +01005775
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776static struct pci_driver rtl8169_pci_driver = {
5777 .name = MODULENAME,
5778 .id_table = rtl8169_pci_tbl,
5779 .probe = rtl8169_init_one,
5780 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005781 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005782 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783};
5784
Francois Romieu07d3f512007-02-21 22:40:46 +01005785static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786{
Jeff Garzik29917622006-08-19 17:48:59 -04005787 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788}
5789
Francois Romieu07d3f512007-02-21 22:40:46 +01005790static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791{
5792 pci_unregister_driver(&rtl8169_pci_driver);
5793}
5794
5795module_init(rtl8169_init_module);
5796module_exit(rtl8169_cleanup_module);