blob: ecfaf5634f53075b8337654238fa470a5f929c09 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080045#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#ifdef RTL8169_DEBUG
48#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020049 if (!(expr)) { \
50 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070051 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020052 }
Joe Perches06fa7352007-10-18 21:15:00 +020053#define dprintk(fmt, args...) \
54 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#else
56#define assert(expr) do {} while (0)
57#define dprintk(fmt, args...) do {} while (0)
58#endif /* RTL8169_DEBUG */
59
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020060#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070061 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define TX_BUFFS_AVAIL(tp) \
64 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
67 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050068static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* MAC address length */
71#define MAC_ADDR_LEN 6
72
Francois Romieu9c14cea2008-07-05 00:21:15 +020073#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
76#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
77
78#define R8169_REGS_SIZE 256
79#define R8169_NAPI_WEIGHT 64
80#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
81#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
82#define RX_BUF_SIZE 1536 /* Rx Buffer size */
83#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
84#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
85
86#define RTL8169_TX_TIMEOUT (6*HZ)
87#define RTL8169_PHY_TIMEOUT (10*HZ)
88
françois romieuea8dbdd2009-03-15 01:10:50 +000089#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
90#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020091#define RTL_EEPROM_SIG_ADDR 0x0000
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* write/read MMIO register */
94#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
95#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
96#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
97#define RTL_R8(reg) readb (ioaddr + (reg))
98#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000099#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200102 RTL_GIGA_MAC_VER_01 = 0,
103 RTL_GIGA_MAC_VER_02,
104 RTL_GIGA_MAC_VER_03,
105 RTL_GIGA_MAC_VER_04,
106 RTL_GIGA_MAC_VER_05,
107 RTL_GIGA_MAC_VER_06,
108 RTL_GIGA_MAC_VER_07,
109 RTL_GIGA_MAC_VER_08,
110 RTL_GIGA_MAC_VER_09,
111 RTL_GIGA_MAC_VER_10,
112 RTL_GIGA_MAC_VER_11,
113 RTL_GIGA_MAC_VER_12,
114 RTL_GIGA_MAC_VER_13,
115 RTL_GIGA_MAC_VER_14,
116 RTL_GIGA_MAC_VER_15,
117 RTL_GIGA_MAC_VER_16,
118 RTL_GIGA_MAC_VER_17,
119 RTL_GIGA_MAC_VER_18,
120 RTL_GIGA_MAC_VER_19,
121 RTL_GIGA_MAC_VER_20,
122 RTL_GIGA_MAC_VER_21,
123 RTL_GIGA_MAC_VER_22,
124 RTL_GIGA_MAC_VER_23,
125 RTL_GIGA_MAC_VER_24,
126 RTL_GIGA_MAC_VER_25,
127 RTL_GIGA_MAC_VER_26,
128 RTL_GIGA_MAC_VER_27,
129 RTL_GIGA_MAC_VER_28,
130 RTL_GIGA_MAC_VER_29,
131 RTL_GIGA_MAC_VER_30,
132 RTL_GIGA_MAC_VER_31,
133 RTL_GIGA_MAC_VER_32,
134 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800135 RTL_GIGA_MAC_VER_34,
Francois Romieu85bffe62011-04-27 08:22:39 +0200136 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Francois Romieu2b7b4312011-04-18 22:53:24 -0700139enum rtl_tx_desc_version {
140 RTL_TD_0 = 0,
141 RTL_TD_1 = 1,
142};
143
Francois Romieu85bffe62011-04-27 08:22:39 +0200144#define _R(NAME,TD,FW) \
145 { .name = NAME, .txd_version = TD, .fw_name = FW }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800147static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700149 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200150 const char *fw_name;
151} rtl_chip_infos[] = {
152 /* PCI devices. */
153 [RTL_GIGA_MAC_VER_01] =
154 _R("RTL8169", RTL_TD_0, NULL),
155 [RTL_GIGA_MAC_VER_02] =
156 _R("RTL8169s", RTL_TD_0, NULL),
157 [RTL_GIGA_MAC_VER_03] =
158 _R("RTL8110s", RTL_TD_0, NULL),
159 [RTL_GIGA_MAC_VER_04] =
160 _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
161 [RTL_GIGA_MAC_VER_05] =
162 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
163 [RTL_GIGA_MAC_VER_06] =
164 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
165 /* PCI-E devices. */
166 [RTL_GIGA_MAC_VER_07] =
167 _R("RTL8102e", RTL_TD_1, NULL),
168 [RTL_GIGA_MAC_VER_08] =
169 _R("RTL8102e", RTL_TD_1, NULL),
170 [RTL_GIGA_MAC_VER_09] =
171 _R("RTL8102e", RTL_TD_1, NULL),
172 [RTL_GIGA_MAC_VER_10] =
173 _R("RTL8101e", RTL_TD_0, NULL),
174 [RTL_GIGA_MAC_VER_11] =
175 _R("RTL8168b/8111b", RTL_TD_0, NULL),
176 [RTL_GIGA_MAC_VER_12] =
177 _R("RTL8168b/8111b", RTL_TD_0, NULL),
178 [RTL_GIGA_MAC_VER_13] =
179 _R("RTL8101e", RTL_TD_0, NULL),
180 [RTL_GIGA_MAC_VER_14] =
181 _R("RTL8100e", RTL_TD_0, NULL),
182 [RTL_GIGA_MAC_VER_15] =
183 _R("RTL8100e", RTL_TD_0, NULL),
184 [RTL_GIGA_MAC_VER_16] =
185 _R("RTL8101e", RTL_TD_0, NULL),
186 [RTL_GIGA_MAC_VER_17] =
187 _R("RTL8168b/8111b", RTL_TD_0, NULL),
188 [RTL_GIGA_MAC_VER_18] =
189 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
190 [RTL_GIGA_MAC_VER_19] =
191 _R("RTL8168c/8111c", RTL_TD_1, NULL),
192 [RTL_GIGA_MAC_VER_20] =
193 _R("RTL8168c/8111c", RTL_TD_1, NULL),
194 [RTL_GIGA_MAC_VER_21] =
195 _R("RTL8168c/8111c", RTL_TD_1, NULL),
196 [RTL_GIGA_MAC_VER_22] =
197 _R("RTL8168c/8111c", RTL_TD_1, NULL),
198 [RTL_GIGA_MAC_VER_23] =
199 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
200 [RTL_GIGA_MAC_VER_24] =
201 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
202 [RTL_GIGA_MAC_VER_25] =
203 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
204 [RTL_GIGA_MAC_VER_26] =
205 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
206 [RTL_GIGA_MAC_VER_27] =
207 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
208 [RTL_GIGA_MAC_VER_28] =
209 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
210 [RTL_GIGA_MAC_VER_29] =
211 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
212 [RTL_GIGA_MAC_VER_30] =
213 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
214 [RTL_GIGA_MAC_VER_31] =
215 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
216 [RTL_GIGA_MAC_VER_32] =
217 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
218 [RTL_GIGA_MAC_VER_33] =
Hayes Wang70090422011-07-06 15:58:06 +0800219 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2),
220 [RTL_GIGA_MAC_VER_34] =
221 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223#undef _R
224
Francois Romieubcf0bf92006-07-26 23:14:13 +0200225enum cfg_version {
226 RTL_CFG_0 = 0x00,
227 RTL_CFG_1,
228 RTL_CFG_2
229};
230
Francois Romieu07ce4062007-02-23 23:36:39 +0100231static void rtl_hw_start_8169(struct net_device *);
232static void rtl_hw_start_8168(struct net_device *);
233static void rtl_hw_start_8101(struct net_device *);
234
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000235static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200236 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200237 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200238 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100239 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200240 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
241 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200242 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200243 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
244 { PCI_VENDOR_ID_LINKSYS, 0x1032,
245 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100246 { 0x0001, 0x8168,
247 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 {0,},
249};
250
251MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
252
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000253static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700254static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200255static struct {
256 u32 msg_enable;
257} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Francois Romieu07d3f512007-02-21 22:40:46 +0100259enum rtl_registers {
260 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100261 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100262 MAR0 = 8, /* Multicast filter. */
263 CounterAddrLow = 0x10,
264 CounterAddrHigh = 0x14,
265 TxDescStartAddrLow = 0x20,
266 TxDescStartAddrHigh = 0x24,
267 TxHDescStartAddrLow = 0x28,
268 TxHDescStartAddrHigh = 0x2c,
269 FLASH = 0x30,
270 ERSR = 0x36,
271 ChipCmd = 0x37,
272 TxPoll = 0x38,
273 IntrMask = 0x3c,
274 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700275
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800276 TxConfig = 0x40,
277#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
278#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
279
280 RxConfig = 0x44,
281#define RX128_INT_EN (1 << 15) /* 8111c and later */
282#define RX_MULTI_EN (1 << 14) /* 8111c only */
283#define RXCFG_FIFO_SHIFT 13
284 /* No threshold before first PCI xfer */
285#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
286#define RXCFG_DMA_SHIFT 8
287 /* Unlimited maximum PCI burst. */
288#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700289#define RTL_RX_CONFIG_MASK 0xff7e1880u
290
Francois Romieu07d3f512007-02-21 22:40:46 +0100291 RxMissed = 0x4c,
292 Cfg9346 = 0x50,
293 Config0 = 0x51,
294 Config1 = 0x52,
295 Config2 = 0x53,
296 Config3 = 0x54,
297 Config4 = 0x55,
298 Config5 = 0x56,
299 MultiIntr = 0x5c,
300 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100301 PHYstatus = 0x6c,
302 RxMaxSize = 0xda,
303 CPlusCmd = 0xe0,
304 IntrMitigate = 0xe2,
305 RxDescAddrLow = 0xe4,
306 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000307 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
308
309#define NoEarlyTx 0x3f /* Max value : no early transmit. */
310
311 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
312
313#define TxPacketMax (8064 >> 7)
314
Francois Romieu07d3f512007-02-21 22:40:46 +0100315 FuncEvent = 0xf0,
316 FuncEventMask = 0xf4,
317 FuncPresetState = 0xf8,
318 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
Francois Romieuf162a5d2008-06-01 22:37:49 +0200321enum rtl8110_registers {
322 TBICSR = 0x64,
323 TBI_ANAR = 0x68,
324 TBI_LPAR = 0x6a,
325};
326
327enum rtl8168_8101_registers {
328 CSIDR = 0x64,
329 CSIAR = 0x68,
330#define CSIAR_FLAG 0x80000000
331#define CSIAR_WRITE_CMD 0x80000000
332#define CSIAR_BYTE_ENABLE 0x0f
333#define CSIAR_BYTE_ENABLE_SHIFT 12
334#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000335 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200336 EPHYAR = 0x80,
337#define EPHYAR_FLAG 0x80000000
338#define EPHYAR_WRITE_CMD 0x80000000
339#define EPHYAR_REG_MASK 0x1f
340#define EPHYAR_REG_SHIFT 16
341#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800342 DLLPR = 0xd0,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800343#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200344 DBG_REG = 0xd1,
345#define FIX_NAK_1 (1 << 4)
346#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800347 TWSI = 0xd2,
348 MCU = 0xd3,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800349#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800350#define EN_NDP (1 << 3)
351#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000352 EFUSEAR = 0xdc,
353#define EFUSEAR_FLAG 0x80000000
354#define EFUSEAR_WRITE_CMD 0x80000000
355#define EFUSEAR_READ_CMD 0x00000000
356#define EFUSEAR_REG_MASK 0x03ff
357#define EFUSEAR_REG_SHIFT 8
358#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200359};
360
françois romieuc0e45c12011-01-03 15:08:04 +0000361enum rtl8168_registers {
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800362 LED_FREQ = 0x1a,
363 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000364 ERIDR = 0x70,
365 ERIAR = 0x74,
366#define ERIAR_FLAG 0x80000000
367#define ERIAR_WRITE_CMD 0x80000000
368#define ERIAR_READ_CMD 0x00000000
369#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000370#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800371#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
372#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
373#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
374#define ERIAR_MASK_SHIFT 12
375#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
376#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
377#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000378 EPHY_RXER_NUM = 0x7c,
379 OCPDR = 0xb0, /* OCP GPHY access */
380#define OCPDR_WRITE_CMD 0x80000000
381#define OCPDR_READ_CMD 0x00000000
382#define OCPDR_REG_MASK 0x7f
383#define OCPDR_GPHY_REG_SHIFT 16
384#define OCPDR_DATA_MASK 0xffff
385 OCPAR = 0xb4,
386#define OCPAR_FLAG 0x80000000
387#define OCPAR_GPHY_WRITE_CMD 0x8000f060
388#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000389 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
390 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200391#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800392#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000393};
394
Francois Romieu07d3f512007-02-21 22:40:46 +0100395enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100397 SYSErr = 0x8000,
398 PCSTimeout = 0x4000,
399 SWInt = 0x0100,
400 TxDescUnavail = 0x0080,
401 RxFIFOOver = 0x0040,
402 LinkChg = 0x0020,
403 RxOverflow = 0x0010,
404 TxErr = 0x0008,
405 TxOK = 0x0004,
406 RxErr = 0x0002,
407 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200410 RxFOVF = (1 << 23),
411 RxRWT = (1 << 22),
412 RxRES = (1 << 21),
413 RxRUNT = (1 << 20),
414 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* ChipCmdBits */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800417 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100418 CmdReset = 0x10,
419 CmdRxEnb = 0x08,
420 CmdTxEnb = 0x04,
421 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Francois Romieu275391a2007-02-23 23:50:28 +0100423 /* TXPoll register p.5 */
424 HPQ = 0x80, /* Poll cmd on the high prio queue */
425 NPQ = 0x40, /* Poll cmd on the low prio queue */
426 FSWInt = 0x01, /* Forced software interrupt */
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100429 Cfg9346_Lock = 0x00,
430 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100433 AcceptErr = 0x20,
434 AcceptRunt = 0x10,
435 AcceptBroadcast = 0x08,
436 AcceptMulticast = 0x04,
437 AcceptMyPhys = 0x02,
438 AcceptAllPhys = 0x01,
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;
François Romieu953a12c2011-04-24 17:38:48 +0200697#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
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800731static const unsigned int rtl8169_rx_config = RX_FIFO_THRESH | RX_DMA_BURST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
françois romieub646d902011-01-03 15:08:21 +0000733static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
734{
735 void __iomem *ioaddr = tp->mmio_addr;
736 int i;
737
738 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
739 for (i = 0; i < 20; i++) {
740 udelay(100);
741 if (RTL_R32(OCPAR) & OCPAR_FLAG)
742 break;
743 }
744 return RTL_R32(OCPDR);
745}
746
747static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
748{
749 void __iomem *ioaddr = tp->mmio_addr;
750 int i;
751
752 RTL_W32(OCPDR, data);
753 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
754 for (i = 0; i < 20; i++) {
755 udelay(100);
756 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
757 break;
758 }
759}
760
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800761static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000762{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800763 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000764 int i;
765
766 RTL_W8(ERIDR, cmd);
767 RTL_W32(ERIAR, 0x800010e8);
768 msleep(2);
769 for (i = 0; i < 5; i++) {
770 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200771 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000772 break;
773 }
774
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800775 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000776}
777
778#define OOB_CMD_RESET 0x00
779#define OOB_CMD_DRIVER_START 0x05
780#define OOB_CMD_DRIVER_STOP 0x06
781
Francois Romieucecb5fd2011-04-01 10:21:07 +0200782static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
783{
784 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
785}
786
françois romieub646d902011-01-03 15:08:21 +0000787static void rtl8168_driver_start(struct rtl8169_private *tp)
788{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200789 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000790 int i;
791
792 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
793
Francois Romieucecb5fd2011-04-01 10:21:07 +0200794 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000795
françois romieub646d902011-01-03 15:08:21 +0000796 for (i = 0; i < 10; i++) {
797 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000798 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000799 break;
800 }
801}
802
803static void rtl8168_driver_stop(struct rtl8169_private *tp)
804{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200805 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000806 int i;
807
808 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
809
Francois Romieucecb5fd2011-04-01 10:21:07 +0200810 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000811
françois romieub646d902011-01-03 15:08:21 +0000812 for (i = 0; i < 10; i++) {
813 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000814 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000815 break;
816 }
817}
818
hayeswang4804b3b2011-03-21 01:50:29 +0000819static int r8168dp_check_dash(struct rtl8169_private *tp)
820{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200821 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000822
Francois Romieucecb5fd2011-04-01 10:21:07 +0200823 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000824}
françois romieub646d902011-01-03 15:08:21 +0000825
françois romieu4da19632011-01-03 15:07:55 +0000826static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 int i;
829
Francois Romieua6baf3a2007-11-08 23:23:21 +0100830 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Francois Romieu23714082006-01-29 00:49:09 +0100832 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100833 /*
834 * Check if the RTL8169 has completed writing to the specified
835 * MII register.
836 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200837 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 break;
Francois Romieu23714082006-01-29 00:49:09 +0100839 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700841 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700842 * According to hardware specs a 20us delay is required after write
843 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700844 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700845 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
françois romieu4da19632011-01-03 15:07:55 +0000848static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 int i, value = -1;
851
Francois Romieua6baf3a2007-11-08 23:23:21 +0100852 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Francois Romieu23714082006-01-29 00:49:09 +0100854 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100855 /*
856 * Check if the RTL8169 has completed retrieving data from
857 * the specified MII register.
858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100860 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 }
Francois Romieu23714082006-01-29 00:49:09 +0100863 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700865 /*
866 * According to hardware specs a 20us delay is required after read
867 * complete indication, but before sending next command.
868 */
869 udelay(20);
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return value;
872}
873
françois romieuc0e45c12011-01-03 15:08:04 +0000874static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
875{
876 int i;
877
878 RTL_W32(OCPDR, data |
879 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
880 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
881 RTL_W32(EPHY_RXER_NUM, 0);
882
883 for (i = 0; i < 100; i++) {
884 mdelay(1);
885 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
886 break;
887 }
888}
889
890static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
891{
892 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
893 (value & OCPDR_DATA_MASK));
894}
895
896static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
897{
898 int i;
899
900 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
901
902 mdelay(1);
903 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
904 RTL_W32(EPHY_RXER_NUM, 0);
905
906 for (i = 0; i < 100; i++) {
907 mdelay(1);
908 if (RTL_R32(OCPAR) & OCPAR_FLAG)
909 break;
910 }
911
912 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
913}
914
françois romieue6de30d2011-01-03 15:08:37 +0000915#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
916
917static void r8168dp_2_mdio_start(void __iomem *ioaddr)
918{
919 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
920}
921
922static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
923{
924 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
925}
926
927static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
928{
929 r8168dp_2_mdio_start(ioaddr);
930
931 r8169_mdio_write(ioaddr, reg_addr, value);
932
933 r8168dp_2_mdio_stop(ioaddr);
934}
935
936static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
937{
938 int value;
939
940 r8168dp_2_mdio_start(ioaddr);
941
942 value = r8169_mdio_read(ioaddr, reg_addr);
943
944 r8168dp_2_mdio_stop(ioaddr);
945
946 return value;
947}
948
françois romieu4da19632011-01-03 15:07:55 +0000949static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200950{
françois romieuc0e45c12011-01-03 15:08:04 +0000951 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200952}
953
françois romieu4da19632011-01-03 15:07:55 +0000954static int rtl_readphy(struct rtl8169_private *tp, int location)
955{
françois romieuc0e45c12011-01-03 15:08:04 +0000956 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000957}
958
959static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
960{
961 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
962}
963
964static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000965{
966 int val;
967
françois romieu4da19632011-01-03 15:07:55 +0000968 val = rtl_readphy(tp, reg_addr);
969 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000970}
971
Francois Romieuccdffb92008-07-26 14:26:06 +0200972static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
973 int val)
974{
975 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200976
françois romieu4da19632011-01-03 15:07:55 +0000977 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200978}
979
980static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
981{
982 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200983
françois romieu4da19632011-01-03 15:07:55 +0000984 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200985}
986
Francois Romieudacf8152008-08-02 20:44:13 +0200987static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
988{
989 unsigned int i;
990
991 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
992 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
993
994 for (i = 0; i < 100; i++) {
995 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
996 break;
997 udelay(10);
998 }
999}
1000
1001static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1002{
1003 u16 value = 0xffff;
1004 unsigned int i;
1005
1006 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1007
1008 for (i = 0; i < 100; i++) {
1009 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1010 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1011 break;
1012 }
1013 udelay(10);
1014 }
1015
1016 return value;
1017}
1018
1019static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1020{
1021 unsigned int i;
1022
1023 RTL_W32(CSIDR, value);
1024 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1025 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1026
1027 for (i = 0; i < 100; i++) {
1028 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1029 break;
1030 udelay(10);
1031 }
1032}
1033
1034static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1035{
1036 u32 value = ~0x00;
1037 unsigned int i;
1038
1039 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1040 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1041
1042 for (i = 0; i < 100; i++) {
1043 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1044 value = RTL_R32(CSIDR);
1045 break;
1046 }
1047 udelay(10);
1048 }
1049
1050 return value;
1051}
1052
Hayes Wang133ac402011-07-06 15:58:05 +08001053static
1054void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1055{
1056 unsigned int i;
1057
1058 BUG_ON((addr & 3) || (mask == 0));
1059 RTL_W32(ERIDR, val);
1060 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1061
1062 for (i = 0; i < 100; i++) {
1063 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1064 break;
1065 udelay(100);
1066 }
1067}
1068
1069static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1070{
1071 u32 value = ~0x00;
1072 unsigned int i;
1073
1074 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1075
1076 for (i = 0; i < 100; i++) {
1077 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1078 value = RTL_R32(ERIDR);
1079 break;
1080 }
1081 udelay(100);
1082 }
1083
1084 return value;
1085}
1086
1087static void
1088rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1089{
1090 u32 val;
1091
1092 val = rtl_eri_read(ioaddr, addr, type);
1093 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1094}
1095
françois romieudaf9df62009-10-07 12:44:20 +00001096static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1097{
1098 u8 value = 0xff;
1099 unsigned int i;
1100
1101 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1102
1103 for (i = 0; i < 300; i++) {
1104 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1105 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1106 break;
1107 }
1108 udelay(100);
1109 }
1110
1111 return value;
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1115{
1116 RTL_W16(IntrMask, 0x0000);
1117
1118 RTL_W16(IntrStatus, 0xffff);
1119}
1120
françois romieu4da19632011-01-03 15:07:55 +00001121static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
françois romieu4da19632011-01-03 15:07:55 +00001123 void __iomem *ioaddr = tp->mmio_addr;
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return RTL_R32(TBICSR) & TBIReset;
1126}
1127
françois romieu4da19632011-01-03 15:07:55 +00001128static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
françois romieu4da19632011-01-03 15:07:55 +00001130 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1134{
1135 return RTL_R32(TBICSR) & TBILinkOk;
1136}
1137
1138static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1139{
1140 return RTL_R8(PHYstatus) & LinkStatus;
1141}
1142
françois romieu4da19632011-01-03 15:07:55 +00001143static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
françois romieu4da19632011-01-03 15:07:55 +00001145 void __iomem *ioaddr = tp->mmio_addr;
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1148}
1149
françois romieu4da19632011-01-03 15:07:55 +00001150static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 unsigned int val;
1153
françois romieu4da19632011-01-03 15:07:55 +00001154 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1155 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
Hayes Wang70090422011-07-06 15:58:06 +08001158static void rtl_link_chg_patch(struct rtl8169_private *tp)
1159{
1160 void __iomem *ioaddr = tp->mmio_addr;
1161 struct net_device *dev = tp->dev;
1162
1163 if (!netif_running(dev))
1164 return;
1165
1166 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1167 if (RTL_R8(PHYstatus) & _1000bpsF) {
1168 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1169 0x00000011, ERIAR_EXGMAC);
1170 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1171 0x00000005, ERIAR_EXGMAC);
1172 } else if (RTL_R8(PHYstatus) & _100bps) {
1173 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1174 0x0000001f, ERIAR_EXGMAC);
1175 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1176 0x00000005, ERIAR_EXGMAC);
1177 } else {
1178 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1179 0x0000001f, ERIAR_EXGMAC);
1180 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1181 0x0000003f, ERIAR_EXGMAC);
1182 }
1183 /* Reset packet filter */
1184 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1185 ERIAR_EXGMAC);
1186 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1187 ERIAR_EXGMAC);
1188 }
1189}
1190
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001191static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001192 struct rtl8169_private *tp,
1193 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 unsigned long flags;
1196
1197 spin_lock_irqsave(&tp->lock, flags);
1198 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001199 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001200 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001201 if (pm)
1202 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001204 if (net_ratelimit())
1205 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001206 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001208 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001209 if (pm)
1210 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 spin_unlock_irqrestore(&tp->lock, flags);
1213}
1214
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001215static void rtl8169_check_link_status(struct net_device *dev,
1216 struct rtl8169_private *tp,
1217 void __iomem *ioaddr)
1218{
1219 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1220}
1221
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001222#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1223
1224static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1225{
1226 void __iomem *ioaddr = tp->mmio_addr;
1227 u8 options;
1228 u32 wolopts = 0;
1229
1230 options = RTL_R8(Config1);
1231 if (!(options & PMEnable))
1232 return 0;
1233
1234 options = RTL_R8(Config3);
1235 if (options & LinkUp)
1236 wolopts |= WAKE_PHY;
1237 if (options & MagicPacket)
1238 wolopts |= WAKE_MAGIC;
1239
1240 options = RTL_R8(Config5);
1241 if (options & UWF)
1242 wolopts |= WAKE_UCAST;
1243 if (options & BWF)
1244 wolopts |= WAKE_BCAST;
1245 if (options & MWF)
1246 wolopts |= WAKE_MCAST;
1247
1248 return wolopts;
1249}
1250
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001251static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1252{
1253 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001254
1255 spin_lock_irq(&tp->lock);
1256
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001257 wol->supported = WAKE_ANY;
1258 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001259
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001260 spin_unlock_irq(&tp->lock);
1261}
1262
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001263static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001264{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001265 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001266 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001267 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001268 u32 opt;
1269 u16 reg;
1270 u8 mask;
1271 } cfg[] = {
1272 { WAKE_ANY, Config1, PMEnable },
1273 { WAKE_PHY, Config3, LinkUp },
1274 { WAKE_MAGIC, Config3, MagicPacket },
1275 { WAKE_UCAST, Config5, UWF },
1276 { WAKE_BCAST, Config5, BWF },
1277 { WAKE_MCAST, Config5, MWF },
1278 { WAKE_ANY, Config5, LanWake }
1279 };
1280
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001281 RTL_W8(Cfg9346, Cfg9346_Unlock);
1282
1283 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1284 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001285 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001286 options |= cfg[i].mask;
1287 RTL_W8(cfg[i].reg, options);
1288 }
1289
1290 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001291}
1292
1293static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1294{
1295 struct rtl8169_private *tp = netdev_priv(dev);
1296
1297 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001298
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001299 if (wol->wolopts)
1300 tp->features |= RTL_FEATURE_WOL;
1301 else
1302 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001303 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001304 spin_unlock_irq(&tp->lock);
1305
françois romieuea809072010-11-08 13:23:58 +00001306 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1307
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001308 return 0;
1309}
1310
Francois Romieu31bd2042011-04-26 18:58:59 +02001311static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1312{
Francois Romieu85bffe62011-04-27 08:22:39 +02001313 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316static void rtl8169_get_drvinfo(struct net_device *dev,
1317 struct ethtool_drvinfo *info)
1318{
1319 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001320 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 strcpy(info->driver, MODULENAME);
1323 strcpy(info->version, RTL8169_VERSION);
1324 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001325 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1326 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1327 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330static int rtl8169_get_regs_len(struct net_device *dev)
1331{
1332 return R8169_REGS_SIZE;
1333}
1334
1335static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001336 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 struct rtl8169_private *tp = netdev_priv(dev);
1339 void __iomem *ioaddr = tp->mmio_addr;
1340 int ret = 0;
1341 u32 reg;
1342
1343 reg = RTL_R32(TBICSR);
1344 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1345 (duplex == DUPLEX_FULL)) {
1346 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1347 } else if (autoneg == AUTONEG_ENABLE)
1348 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1349 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001350 netif_warn(tp, link, dev,
1351 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 ret = -EOPNOTSUPP;
1353 }
1354
1355 return ret;
1356}
1357
1358static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001359 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001362 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001363 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Hayes Wang716b50a2011-02-22 17:26:18 +08001365 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001368 int auto_nego;
1369
françois romieu4da19632011-01-03 15:07:55 +00001370 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001371 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1372 ADVERTISE_100HALF | ADVERTISE_100FULL);
1373
1374 if (adv & ADVERTISED_10baseT_Half)
1375 auto_nego |= ADVERTISE_10HALF;
1376 if (adv & ADVERTISED_10baseT_Full)
1377 auto_nego |= ADVERTISE_10FULL;
1378 if (adv & ADVERTISED_100baseT_Half)
1379 auto_nego |= ADVERTISE_100HALF;
1380 if (adv & ADVERTISED_100baseT_Full)
1381 auto_nego |= ADVERTISE_100FULL;
1382
françois romieu3577aa12009-05-19 10:46:48 +00001383 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1384
françois romieu4da19632011-01-03 15:07:55 +00001385 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001386 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1387
1388 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001389 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001390 if (adv & ADVERTISED_1000baseT_Half)
1391 giga_ctrl |= ADVERTISE_1000HALF;
1392 if (adv & ADVERTISED_1000baseT_Full)
1393 giga_ctrl |= ADVERTISE_1000FULL;
1394 } else if (adv & (ADVERTISED_1000baseT_Half |
1395 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001396 netif_info(tp, link, dev,
1397 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001398 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
françois romieu3577aa12009-05-19 10:46:48 +00001401 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001402
françois romieu4da19632011-01-03 15:07:55 +00001403 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1404 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001405 } else {
1406 giga_ctrl = 0;
1407
1408 if (speed == SPEED_10)
1409 bmcr = 0;
1410 else if (speed == SPEED_100)
1411 bmcr = BMCR_SPEED100;
1412 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001413 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001414
1415 if (duplex == DUPLEX_FULL)
1416 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001417 }
1418
françois romieu4da19632011-01-03 15:07:55 +00001419 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001420
Francois Romieucecb5fd2011-04-01 10:21:07 +02001421 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1422 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001423 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001424 rtl_writephy(tp, 0x17, 0x2138);
1425 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001426 } else {
françois romieu4da19632011-01-03 15:07:55 +00001427 rtl_writephy(tp, 0x17, 0x2108);
1428 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001429 }
1430 }
1431
Oliver Neukum54405cd2011-01-06 21:55:13 +01001432 rc = 0;
1433out:
1434 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
1437static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001438 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct rtl8169_private *tp = netdev_priv(dev);
1441 int ret;
1442
Oliver Neukum54405cd2011-01-06 21:55:13 +01001443 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001444 if (ret < 0)
1445 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Francois Romieu4876cc12011-03-11 21:07:11 +01001447 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1448 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001450 }
1451out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return ret;
1453}
1454
1455static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1456{
1457 struct rtl8169_private *tp = netdev_priv(dev);
1458 unsigned long flags;
1459 int ret;
1460
Francois Romieu4876cc12011-03-11 21:07:11 +01001461 del_timer_sync(&tp->timer);
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001464 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001465 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return ret;
1469}
1470
Michał Mirosław350fb322011-04-08 06:35:56 +00001471static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001473 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001474 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Michał Mirosław350fb322011-04-08 06:35:56 +00001476 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Michał Mirosław350fb322011-04-08 06:35:56 +00001479static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 struct rtl8169_private *tp = netdev_priv(dev);
1482 void __iomem *ioaddr = tp->mmio_addr;
1483 unsigned long flags;
1484
1485 spin_lock_irqsave(&tp->lock, flags);
1486
Michał Mirosław350fb322011-04-08 06:35:56 +00001487 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 tp->cp_cmd |= RxChkSum;
1489 else
1490 tp->cp_cmd &= ~RxChkSum;
1491
Michał Mirosław350fb322011-04-08 06:35:56 +00001492 if (dev->features & NETIF_F_HW_VLAN_RX)
1493 tp->cp_cmd |= RxVlan;
1494 else
1495 tp->cp_cmd &= ~RxVlan;
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 RTL_W16(CPlusCmd, tp->cp_cmd);
1498 RTL_R16(CPlusCmd);
1499
1500 spin_unlock_irqrestore(&tp->lock, flags);
1501
1502 return 0;
1503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1506 struct sk_buff *skb)
1507{
Jesse Grosseab6d182010-10-20 13:56:03 +00001508 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1510}
1511
Francois Romieu7a8fc772011-03-01 17:18:33 +01001512static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Francois Romieu7a8fc772011-03-01 17:18:33 +01001516 if (opts2 & RxVlanTag)
1517 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
Francois Romieuccdffb92008-07-26 14:26:06 +02001522static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 struct rtl8169_private *tp = netdev_priv(dev);
1525 void __iomem *ioaddr = tp->mmio_addr;
1526 u32 status;
1527
1528 cmd->supported =
1529 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1530 cmd->port = PORT_FIBRE;
1531 cmd->transceiver = XCVR_INTERNAL;
1532
1533 status = RTL_R32(TBICSR);
1534 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1535 cmd->autoneg = !!(status & TBINwEnable);
1536
David Decotigny70739492011-04-27 18:32:40 +00001537 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001539
1540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
Francois Romieuccdffb92008-07-26 14:26:06 +02001543static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
1545 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Francois Romieuccdffb92008-07-26 14:26:06 +02001547 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1551{
1552 struct rtl8169_private *tp = netdev_priv(dev);
1553 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001554 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 spin_lock_irqsave(&tp->lock, flags);
1557
Francois Romieuccdffb92008-07-26 14:26:06 +02001558 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001561 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
1564static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1565 void *p)
1566{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001567 struct rtl8169_private *tp = netdev_priv(dev);
1568 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Francois Romieu5b0384f2006-08-16 16:00:01 +02001570 if (regs->len > R8169_REGS_SIZE)
1571 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Francois Romieu5b0384f2006-08-16 16:00:01 +02001573 spin_lock_irqsave(&tp->lock, flags);
1574 memcpy_fromio(p, tp->mmio_addr, regs->len);
1575 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001578static u32 rtl8169_get_msglevel(struct net_device *dev)
1579{
1580 struct rtl8169_private *tp = netdev_priv(dev);
1581
1582 return tp->msg_enable;
1583}
1584
1585static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1586{
1587 struct rtl8169_private *tp = netdev_priv(dev);
1588
1589 tp->msg_enable = value;
1590}
1591
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001592static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1593 "tx_packets",
1594 "rx_packets",
1595 "tx_errors",
1596 "rx_errors",
1597 "rx_missed",
1598 "align_errors",
1599 "tx_single_collisions",
1600 "tx_multi_collisions",
1601 "unicast",
1602 "broadcast",
1603 "multicast",
1604 "tx_aborted",
1605 "tx_underrun",
1606};
1607
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001608static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001609{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001610 switch (sset) {
1611 case ETH_SS_STATS:
1612 return ARRAY_SIZE(rtl8169_gstrings);
1613 default:
1614 return -EOPNOTSUPP;
1615 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001616}
1617
Ivan Vecera355423d2009-02-06 21:49:57 -08001618static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001619{
1620 struct rtl8169_private *tp = netdev_priv(dev);
1621 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001622 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001623 struct rtl8169_counters *counters;
1624 dma_addr_t paddr;
1625 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001626 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001627
Ivan Vecera355423d2009-02-06 21:49:57 -08001628 /*
1629 * Some chips are unable to dump tally counters when the receiver
1630 * is disabled.
1631 */
1632 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1633 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001634
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001635 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001636 if (!counters)
1637 return;
1638
1639 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001640 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001641 RTL_W32(CounterAddrLow, cmd);
1642 RTL_W32(CounterAddrLow, cmd | CounterDump);
1643
Ivan Vecera355423d2009-02-06 21:49:57 -08001644 while (wait--) {
1645 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001646 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001647 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001648 }
1649 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001650 }
1651
1652 RTL_W32(CounterAddrLow, 0);
1653 RTL_W32(CounterAddrHigh, 0);
1654
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001655 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001656}
1657
Ivan Vecera355423d2009-02-06 21:49:57 -08001658static void rtl8169_get_ethtool_stats(struct net_device *dev,
1659 struct ethtool_stats *stats, u64 *data)
1660{
1661 struct rtl8169_private *tp = netdev_priv(dev);
1662
1663 ASSERT_RTNL();
1664
1665 rtl8169_update_counters(dev);
1666
1667 data[0] = le64_to_cpu(tp->counters.tx_packets);
1668 data[1] = le64_to_cpu(tp->counters.rx_packets);
1669 data[2] = le64_to_cpu(tp->counters.tx_errors);
1670 data[3] = le32_to_cpu(tp->counters.rx_errors);
1671 data[4] = le16_to_cpu(tp->counters.rx_missed);
1672 data[5] = le16_to_cpu(tp->counters.align_errors);
1673 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1674 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1675 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1676 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1677 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1678 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1679 data[12] = le16_to_cpu(tp->counters.tx_underun);
1680}
1681
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001682static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1683{
1684 switch(stringset) {
1685 case ETH_SS_STATS:
1686 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1687 break;
1688 }
1689}
1690
Jeff Garzik7282d492006-09-13 14:30:00 -04001691static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 .get_drvinfo = rtl8169_get_drvinfo,
1693 .get_regs_len = rtl8169_get_regs_len,
1694 .get_link = ethtool_op_get_link,
1695 .get_settings = rtl8169_get_settings,
1696 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001697 .get_msglevel = rtl8169_get_msglevel,
1698 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001700 .get_wol = rtl8169_get_wol,
1701 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001702 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001703 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001704 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705};
1706
Francois Romieu07d3f512007-02-21 22:40:46 +01001707static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001708 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Francois Romieu5d320a22011-05-08 17:47:36 +02001710 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001711 /*
1712 * The driver currently handles the 8168Bf and the 8168Be identically
1713 * but they can be identified more specifically through the test below
1714 * if needed:
1715 *
1716 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001717 *
1718 * Same thing for the 8101Eb and the 8101Ec:
1719 *
1720 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001721 */
Francois Romieu37441002011-06-17 22:58:54 +02001722 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001724 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 int mac_version;
1726 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001727 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001728 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001729 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1730 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1731 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1732
Francois Romieu5b538df2008-07-20 16:22:45 +02001733 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001734 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1735 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001736 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001737
françois romieue6de30d2011-01-03 15:08:37 +00001738 /* 8168DP family. */
1739 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1740 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001741 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001742
Francois Romieuef808d52008-06-29 13:10:54 +02001743 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001744 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001745 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001746 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001747 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001748 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1749 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001750 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001751 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001752 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001753
1754 /* 8168B family. */
1755 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1756 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1757 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1758 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1759
1760 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001761 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001762 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1763 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1764 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001765 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1766 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1767 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1768 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1769 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1770 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001771 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001772 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001773 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001774 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1775 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001776 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1777 /* FIXME: where did these entries come from ? -- FR */
1778 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1779 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1780
1781 /* 8110 family. */
1782 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1783 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1784 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1785 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1786 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1787 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1788
Jean Delvaref21b75e2009-05-26 20:54:48 -07001789 /* Catch-all */
1790 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001791 };
1792 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 u32 reg;
1794
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001795 reg = RTL_R32(TxConfig);
1796 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 p++;
1798 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001799
1800 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1801 netif_notice(tp, probe, dev,
1802 "unknown MAC, using family default\n");
1803 tp->mac_version = default_version;
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
1807static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1808{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001809 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Francois Romieu867763c2007-08-17 18:21:58 +02001812struct phy_reg {
1813 u16 reg;
1814 u16 val;
1815};
1816
françois romieu4da19632011-01-03 15:07:55 +00001817static void rtl_writephy_batch(struct rtl8169_private *tp,
1818 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001819{
1820 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001821 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001822 regs++;
1823 }
1824}
1825
françois romieubca03d52011-01-03 15:07:31 +00001826#define PHY_READ 0x00000000
1827#define PHY_DATA_OR 0x10000000
1828#define PHY_DATA_AND 0x20000000
1829#define PHY_BJMPN 0x30000000
1830#define PHY_READ_EFUSE 0x40000000
1831#define PHY_READ_MAC_BYTE 0x50000000
1832#define PHY_WRITE_MAC_BYTE 0x60000000
1833#define PHY_CLEAR_READCOUNT 0x70000000
1834#define PHY_WRITE 0x80000000
1835#define PHY_READCOUNT_EQ_SKIP 0x90000000
1836#define PHY_COMP_EQ_SKIPN 0xa0000000
1837#define PHY_COMP_NEQ_SKIPN 0xb0000000
1838#define PHY_WRITE_PREVIOUS 0xc0000000
1839#define PHY_SKIPN 0xd0000000
1840#define PHY_DELAY_MS 0xe0000000
1841#define PHY_WRITE_ERI_WORD 0xf0000000
1842
Hayes Wang960aee62011-06-18 11:37:48 +02001843struct fw_info {
1844 u32 magic;
1845 char version[RTL_VER_SIZE];
1846 __le32 fw_start;
1847 __le32 fw_len;
1848 u8 chksum;
1849} __packed;
1850
Francois Romieu1c361ef2011-06-17 17:16:24 +02001851#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1852
1853static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001854{
Francois Romieub6ffd972011-06-17 17:00:05 +02001855 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001856 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001857 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1858 char *version = rtl_fw->version;
1859 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001860
Francois Romieu1c361ef2011-06-17 17:16:24 +02001861 if (fw->size < FW_OPCODE_SIZE)
1862 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001863
1864 if (!fw_info->magic) {
1865 size_t i, size, start;
1866 u8 checksum = 0;
1867
1868 if (fw->size < sizeof(*fw_info))
1869 goto out;
1870
1871 for (i = 0; i < fw->size; i++)
1872 checksum += fw->data[i];
1873 if (checksum != 0)
1874 goto out;
1875
1876 start = le32_to_cpu(fw_info->fw_start);
1877 if (start > fw->size)
1878 goto out;
1879
1880 size = le32_to_cpu(fw_info->fw_len);
1881 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1882 goto out;
1883
1884 memcpy(version, fw_info->version, RTL_VER_SIZE);
1885
1886 pa->code = (__le32 *)(fw->data + start);
1887 pa->size = size;
1888 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001889 if (fw->size % FW_OPCODE_SIZE)
1890 goto out;
1891
1892 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1893
1894 pa->code = (__le32 *)fw->data;
1895 pa->size = fw->size / FW_OPCODE_SIZE;
1896 }
1897 version[RTL_VER_SIZE - 1] = 0;
1898
1899 rc = true;
1900out:
1901 return rc;
1902}
1903
Francois Romieufd112f22011-06-18 00:10:29 +02001904static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1905 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02001906{
Francois Romieufd112f22011-06-18 00:10:29 +02001907 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001908 size_t index;
1909
Francois Romieu1c361ef2011-06-17 17:16:24 +02001910 for (index = 0; index < pa->size; index++) {
1911 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00001912 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001913
hayeswang42b82dc2011-01-10 02:07:25 +00001914 switch(action & 0xf0000000) {
1915 case PHY_READ:
1916 case PHY_DATA_OR:
1917 case PHY_DATA_AND:
1918 case PHY_READ_EFUSE:
1919 case PHY_CLEAR_READCOUNT:
1920 case PHY_WRITE:
1921 case PHY_WRITE_PREVIOUS:
1922 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001923 break;
1924
hayeswang42b82dc2011-01-10 02:07:25 +00001925 case PHY_BJMPN:
1926 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02001927 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001928 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001929 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001930 }
1931 break;
1932 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001933 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001934 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001935 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001936 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001937 }
1938 break;
1939 case PHY_COMP_EQ_SKIPN:
1940 case PHY_COMP_NEQ_SKIPN:
1941 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001942 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001943 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001944 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001945 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001946 }
1947 break;
1948
1949 case PHY_READ_MAC_BYTE:
1950 case PHY_WRITE_MAC_BYTE:
1951 case PHY_WRITE_ERI_WORD:
1952 default:
Francois Romieufd112f22011-06-18 00:10:29 +02001953 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00001954 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02001955 goto out;
françois romieubca03d52011-01-03 15:07:31 +00001956 }
1957 }
Francois Romieufd112f22011-06-18 00:10:29 +02001958 rc = true;
1959out:
1960 return rc;
1961}
françois romieubca03d52011-01-03 15:07:31 +00001962
Francois Romieufd112f22011-06-18 00:10:29 +02001963static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1964{
1965 struct net_device *dev = tp->dev;
1966 int rc = -EINVAL;
1967
1968 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1969 netif_err(tp, ifup, dev, "invalid firwmare\n");
1970 goto out;
1971 }
1972
1973 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1974 rc = 0;
1975out:
1976 return rc;
1977}
1978
1979static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1980{
1981 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1982 u32 predata, count;
1983 size_t index;
1984
1985 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00001986
Francois Romieu1c361ef2011-06-17 17:16:24 +02001987 for (index = 0; index < pa->size; ) {
1988 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00001989 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001990 u32 regno = (action & 0x0fff0000) >> 16;
1991
1992 if (!action)
1993 break;
françois romieubca03d52011-01-03 15:07:31 +00001994
1995 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001996 case PHY_READ:
1997 predata = rtl_readphy(tp, regno);
1998 count++;
1999 index++;
françois romieubca03d52011-01-03 15:07:31 +00002000 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002001 case PHY_DATA_OR:
2002 predata |= data;
2003 index++;
2004 break;
2005 case PHY_DATA_AND:
2006 predata &= data;
2007 index++;
2008 break;
2009 case PHY_BJMPN:
2010 index -= regno;
2011 break;
2012 case PHY_READ_EFUSE:
2013 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2014 index++;
2015 break;
2016 case PHY_CLEAR_READCOUNT:
2017 count = 0;
2018 index++;
2019 break;
2020 case PHY_WRITE:
2021 rtl_writephy(tp, regno, data);
2022 index++;
2023 break;
2024 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002025 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002026 break;
2027 case PHY_COMP_EQ_SKIPN:
2028 if (predata == data)
2029 index += regno;
2030 index++;
2031 break;
2032 case PHY_COMP_NEQ_SKIPN:
2033 if (predata != data)
2034 index += regno;
2035 index++;
2036 break;
2037 case PHY_WRITE_PREVIOUS:
2038 rtl_writephy(tp, regno, predata);
2039 index++;
2040 break;
2041 case PHY_SKIPN:
2042 index += regno + 1;
2043 break;
2044 case PHY_DELAY_MS:
2045 mdelay(data);
2046 index++;
2047 break;
2048
2049 case PHY_READ_MAC_BYTE:
2050 case PHY_WRITE_MAC_BYTE:
2051 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002052 default:
2053 BUG();
2054 }
2055 }
2056}
2057
françois romieuf1e02ed2011-01-13 13:07:53 +00002058static void rtl_release_firmware(struct rtl8169_private *tp)
2059{
Francois Romieub6ffd972011-06-17 17:00:05 +02002060 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2061 release_firmware(tp->rtl_fw->fw);
2062 kfree(tp->rtl_fw);
2063 }
2064 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002065}
2066
François Romieu953a12c2011-04-24 17:38:48 +02002067static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002068{
Francois Romieub6ffd972011-06-17 17:00:05 +02002069 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002070
2071 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002072 if (!IS_ERR_OR_NULL(rtl_fw))
2073 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002074}
2075
2076static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2077{
2078 if (rtl_readphy(tp, reg) != val)
2079 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2080 else
2081 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002082}
2083
françois romieu4da19632011-01-03 15:07:55 +00002084static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002086 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002087 { 0x1f, 0x0001 },
2088 { 0x06, 0x006e },
2089 { 0x08, 0x0708 },
2090 { 0x15, 0x4000 },
2091 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
françois romieu0b9b5712009-08-10 19:44:56 +00002093 { 0x1f, 0x0001 },
2094 { 0x03, 0x00a1 },
2095 { 0x02, 0x0008 },
2096 { 0x01, 0x0120 },
2097 { 0x00, 0x1000 },
2098 { 0x04, 0x0800 },
2099 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
françois romieu0b9b5712009-08-10 19:44:56 +00002101 { 0x03, 0xff41 },
2102 { 0x02, 0xdf60 },
2103 { 0x01, 0x0140 },
2104 { 0x00, 0x0077 },
2105 { 0x04, 0x7800 },
2106 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
françois romieu0b9b5712009-08-10 19:44:56 +00002108 { 0x03, 0x802f },
2109 { 0x02, 0x4f02 },
2110 { 0x01, 0x0409 },
2111 { 0x00, 0xf0f9 },
2112 { 0x04, 0x9800 },
2113 { 0x04, 0x9000 },
2114
2115 { 0x03, 0xdf01 },
2116 { 0x02, 0xdf20 },
2117 { 0x01, 0xff95 },
2118 { 0x00, 0xba00 },
2119 { 0x04, 0xa800 },
2120 { 0x04, 0xa000 },
2121
2122 { 0x03, 0xff41 },
2123 { 0x02, 0xdf20 },
2124 { 0x01, 0x0140 },
2125 { 0x00, 0x00bb },
2126 { 0x04, 0xb800 },
2127 { 0x04, 0xb000 },
2128
2129 { 0x03, 0xdf41 },
2130 { 0x02, 0xdc60 },
2131 { 0x01, 0x6340 },
2132 { 0x00, 0x007d },
2133 { 0x04, 0xd800 },
2134 { 0x04, 0xd000 },
2135
2136 { 0x03, 0xdf01 },
2137 { 0x02, 0xdf20 },
2138 { 0x01, 0x100a },
2139 { 0x00, 0xa0ff },
2140 { 0x04, 0xf800 },
2141 { 0x04, 0xf000 },
2142
2143 { 0x1f, 0x0000 },
2144 { 0x0b, 0x0000 },
2145 { 0x00, 0x9200 }
2146 };
2147
françois romieu4da19632011-01-03 15:07:55 +00002148 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
françois romieu4da19632011-01-03 15:07:55 +00002151static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002152{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002153 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002154 { 0x1f, 0x0002 },
2155 { 0x01, 0x90d0 },
2156 { 0x1f, 0x0000 }
2157 };
2158
françois romieu4da19632011-01-03 15:07:55 +00002159 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002160}
2161
françois romieu4da19632011-01-03 15:07:55 +00002162static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002163{
2164 struct pci_dev *pdev = tp->pci_dev;
2165 u16 vendor_id, device_id;
2166
2167 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
2168 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
2169
2170 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
2171 return;
2172
françois romieu4da19632011-01-03 15:07:55 +00002173 rtl_writephy(tp, 0x1f, 0x0001);
2174 rtl_writephy(tp, 0x10, 0xf01b);
2175 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002176}
2177
françois romieu4da19632011-01-03 15:07:55 +00002178static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002179{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002180 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002181 { 0x1f, 0x0001 },
2182 { 0x04, 0x0000 },
2183 { 0x03, 0x00a1 },
2184 { 0x02, 0x0008 },
2185 { 0x01, 0x0120 },
2186 { 0x00, 0x1000 },
2187 { 0x04, 0x0800 },
2188 { 0x04, 0x9000 },
2189 { 0x03, 0x802f },
2190 { 0x02, 0x4f02 },
2191 { 0x01, 0x0409 },
2192 { 0x00, 0xf099 },
2193 { 0x04, 0x9800 },
2194 { 0x04, 0xa000 },
2195 { 0x03, 0xdf01 },
2196 { 0x02, 0xdf20 },
2197 { 0x01, 0xff95 },
2198 { 0x00, 0xba00 },
2199 { 0x04, 0xa800 },
2200 { 0x04, 0xf000 },
2201 { 0x03, 0xdf01 },
2202 { 0x02, 0xdf20 },
2203 { 0x01, 0x101a },
2204 { 0x00, 0xa0ff },
2205 { 0x04, 0xf800 },
2206 { 0x04, 0x0000 },
2207 { 0x1f, 0x0000 },
2208
2209 { 0x1f, 0x0001 },
2210 { 0x10, 0xf41b },
2211 { 0x14, 0xfb54 },
2212 { 0x18, 0xf5c7 },
2213 { 0x1f, 0x0000 },
2214
2215 { 0x1f, 0x0001 },
2216 { 0x17, 0x0cc0 },
2217 { 0x1f, 0x0000 }
2218 };
2219
françois romieu4da19632011-01-03 15:07:55 +00002220 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002221
françois romieu4da19632011-01-03 15:07:55 +00002222 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002223}
2224
françois romieu4da19632011-01-03 15:07:55 +00002225static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002226{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002227 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002228 { 0x1f, 0x0001 },
2229 { 0x04, 0x0000 },
2230 { 0x03, 0x00a1 },
2231 { 0x02, 0x0008 },
2232 { 0x01, 0x0120 },
2233 { 0x00, 0x1000 },
2234 { 0x04, 0x0800 },
2235 { 0x04, 0x9000 },
2236 { 0x03, 0x802f },
2237 { 0x02, 0x4f02 },
2238 { 0x01, 0x0409 },
2239 { 0x00, 0xf099 },
2240 { 0x04, 0x9800 },
2241 { 0x04, 0xa000 },
2242 { 0x03, 0xdf01 },
2243 { 0x02, 0xdf20 },
2244 { 0x01, 0xff95 },
2245 { 0x00, 0xba00 },
2246 { 0x04, 0xa800 },
2247 { 0x04, 0xf000 },
2248 { 0x03, 0xdf01 },
2249 { 0x02, 0xdf20 },
2250 { 0x01, 0x101a },
2251 { 0x00, 0xa0ff },
2252 { 0x04, 0xf800 },
2253 { 0x04, 0x0000 },
2254 { 0x1f, 0x0000 },
2255
2256 { 0x1f, 0x0001 },
2257 { 0x0b, 0x8480 },
2258 { 0x1f, 0x0000 },
2259
2260 { 0x1f, 0x0001 },
2261 { 0x18, 0x67c7 },
2262 { 0x04, 0x2000 },
2263 { 0x03, 0x002f },
2264 { 0x02, 0x4360 },
2265 { 0x01, 0x0109 },
2266 { 0x00, 0x3022 },
2267 { 0x04, 0x2800 },
2268 { 0x1f, 0x0000 },
2269
2270 { 0x1f, 0x0001 },
2271 { 0x17, 0x0cc0 },
2272 { 0x1f, 0x0000 }
2273 };
2274
françois romieu4da19632011-01-03 15:07:55 +00002275 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002276}
2277
françois romieu4da19632011-01-03 15:07:55 +00002278static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002279{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002280 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002281 { 0x10, 0xf41b },
2282 { 0x1f, 0x0000 }
2283 };
2284
françois romieu4da19632011-01-03 15:07:55 +00002285 rtl_writephy(tp, 0x1f, 0x0001);
2286 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002287
françois romieu4da19632011-01-03 15:07:55 +00002288 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002289}
2290
françois romieu4da19632011-01-03 15:07:55 +00002291static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002292{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002293 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002294 { 0x1f, 0x0001 },
2295 { 0x10, 0xf41b },
2296 { 0x1f, 0x0000 }
2297 };
2298
françois romieu4da19632011-01-03 15:07:55 +00002299 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002300}
2301
françois romieu4da19632011-01-03 15:07:55 +00002302static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002303{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002304 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002305 { 0x1f, 0x0000 },
2306 { 0x1d, 0x0f00 },
2307 { 0x1f, 0x0002 },
2308 { 0x0c, 0x1ec8 },
2309 { 0x1f, 0x0000 }
2310 };
2311
françois romieu4da19632011-01-03 15:07:55 +00002312 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002313}
2314
françois romieu4da19632011-01-03 15:07:55 +00002315static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002316{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002317 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002318 { 0x1f, 0x0001 },
2319 { 0x1d, 0x3d98 },
2320 { 0x1f, 0x0000 }
2321 };
2322
françois romieu4da19632011-01-03 15:07:55 +00002323 rtl_writephy(tp, 0x1f, 0x0000);
2324 rtl_patchphy(tp, 0x14, 1 << 5);
2325 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002326
françois romieu4da19632011-01-03 15:07:55 +00002327 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002328}
2329
françois romieu4da19632011-01-03 15:07:55 +00002330static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002331{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002332 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002333 { 0x1f, 0x0001 },
2334 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002335 { 0x1f, 0x0002 },
2336 { 0x00, 0x88d4 },
2337 { 0x01, 0x82b1 },
2338 { 0x03, 0x7002 },
2339 { 0x08, 0x9e30 },
2340 { 0x09, 0x01f0 },
2341 { 0x0a, 0x5500 },
2342 { 0x0c, 0x00c8 },
2343 { 0x1f, 0x0003 },
2344 { 0x12, 0xc096 },
2345 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002346 { 0x1f, 0x0000 },
2347 { 0x1f, 0x0000 },
2348 { 0x09, 0x2000 },
2349 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002350 };
2351
françois romieu4da19632011-01-03 15:07:55 +00002352 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002353
françois romieu4da19632011-01-03 15:07:55 +00002354 rtl_patchphy(tp, 0x14, 1 << 5);
2355 rtl_patchphy(tp, 0x0d, 1 << 5);
2356 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002357}
2358
françois romieu4da19632011-01-03 15:07:55 +00002359static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002360{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002361 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002362 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002363 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002364 { 0x03, 0x802f },
2365 { 0x02, 0x4f02 },
2366 { 0x01, 0x0409 },
2367 { 0x00, 0xf099 },
2368 { 0x04, 0x9800 },
2369 { 0x04, 0x9000 },
2370 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002371 { 0x1f, 0x0002 },
2372 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002373 { 0x06, 0x0761 },
2374 { 0x1f, 0x0003 },
2375 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002376 { 0x1f, 0x0000 }
2377 };
2378
françois romieu4da19632011-01-03 15:07:55 +00002379 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002380
françois romieu4da19632011-01-03 15:07:55 +00002381 rtl_patchphy(tp, 0x16, 1 << 0);
2382 rtl_patchphy(tp, 0x14, 1 << 5);
2383 rtl_patchphy(tp, 0x0d, 1 << 5);
2384 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002385}
2386
françois romieu4da19632011-01-03 15:07:55 +00002387static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002388{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002389 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002390 { 0x1f, 0x0001 },
2391 { 0x12, 0x2300 },
2392 { 0x1d, 0x3d98 },
2393 { 0x1f, 0x0002 },
2394 { 0x0c, 0x7eb8 },
2395 { 0x06, 0x5461 },
2396 { 0x1f, 0x0003 },
2397 { 0x16, 0x0f0a },
2398 { 0x1f, 0x0000 }
2399 };
2400
françois romieu4da19632011-01-03 15:07:55 +00002401 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002402
françois romieu4da19632011-01-03 15:07:55 +00002403 rtl_patchphy(tp, 0x16, 1 << 0);
2404 rtl_patchphy(tp, 0x14, 1 << 5);
2405 rtl_patchphy(tp, 0x0d, 1 << 5);
2406 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002407}
2408
françois romieu4da19632011-01-03 15:07:55 +00002409static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002410{
françois romieu4da19632011-01-03 15:07:55 +00002411 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002412}
2413
françois romieubca03d52011-01-03 15:07:31 +00002414static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002415{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002416 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002417 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002418 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002419 { 0x06, 0x4064 },
2420 { 0x07, 0x2863 },
2421 { 0x08, 0x059c },
2422 { 0x09, 0x26b4 },
2423 { 0x0a, 0x6a19 },
2424 { 0x0b, 0xdcc8 },
2425 { 0x10, 0xf06d },
2426 { 0x14, 0x7f68 },
2427 { 0x18, 0x7fd9 },
2428 { 0x1c, 0xf0ff },
2429 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002430 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002431 { 0x12, 0xf49f },
2432 { 0x13, 0x070b },
2433 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002434 { 0x14, 0x94c0 },
2435
2436 /*
2437 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002438 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002439 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002440 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002441 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002442 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002443 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002444 { 0x06, 0x5561 },
2445
2446 /*
2447 * Can not link to 1Gbps with bad cable
2448 * Decrease SNR threshold form 21.07dB to 19.04dB
2449 */
2450 { 0x1f, 0x0001 },
2451 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002452
2453 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002454 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002455 };
françois romieubca03d52011-01-03 15:07:31 +00002456 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002457
françois romieu4da19632011-01-03 15:07:55 +00002458 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002459
françois romieubca03d52011-01-03 15:07:31 +00002460 /*
2461 * Rx Error Issue
2462 * Fine Tune Switching regulator parameter
2463 */
françois romieu4da19632011-01-03 15:07:55 +00002464 rtl_writephy(tp, 0x1f, 0x0002);
2465 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2466 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002467
françois romieudaf9df62009-10-07 12:44:20 +00002468 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002469 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002470 { 0x1f, 0x0002 },
2471 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002472 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002473 { 0x05, 0x8330 },
2474 { 0x06, 0x669a },
2475 { 0x1f, 0x0002 }
2476 };
2477 int val;
2478
françois romieu4da19632011-01-03 15:07:55 +00002479 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002480
françois romieu4da19632011-01-03 15:07:55 +00002481 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002482
2483 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002484 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002485 0x0065, 0x0066, 0x0067, 0x0068,
2486 0x0069, 0x006a, 0x006b, 0x006c
2487 };
2488 int i;
2489
françois romieu4da19632011-01-03 15:07:55 +00002490 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002491
2492 val &= 0xff00;
2493 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002494 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002495 }
2496 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002497 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002498 { 0x1f, 0x0002 },
2499 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002500 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002501 { 0x05, 0x8330 },
2502 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002503 };
2504
françois romieu4da19632011-01-03 15:07:55 +00002505 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002506 }
2507
françois romieubca03d52011-01-03 15:07:31 +00002508 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002509 rtl_writephy(tp, 0x1f, 0x0002);
2510 rtl_patchphy(tp, 0x0d, 0x0300);
2511 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002512
françois romieubca03d52011-01-03 15:07:31 +00002513 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002514 rtl_writephy(tp, 0x1f, 0x0002);
2515 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2516 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002517
françois romieu4da19632011-01-03 15:07:55 +00002518 rtl_writephy(tp, 0x1f, 0x0005);
2519 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002520
2521 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002522
françois romieu4da19632011-01-03 15:07:55 +00002523 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002524}
2525
françois romieubca03d52011-01-03 15:07:31 +00002526static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002527{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002528 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002529 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002530 { 0x1f, 0x0001 },
2531 { 0x06, 0x4064 },
2532 { 0x07, 0x2863 },
2533 { 0x08, 0x059c },
2534 { 0x09, 0x26b4 },
2535 { 0x0a, 0x6a19 },
2536 { 0x0b, 0xdcc8 },
2537 { 0x10, 0xf06d },
2538 { 0x14, 0x7f68 },
2539 { 0x18, 0x7fd9 },
2540 { 0x1c, 0xf0ff },
2541 { 0x1d, 0x3d9c },
2542 { 0x1f, 0x0003 },
2543 { 0x12, 0xf49f },
2544 { 0x13, 0x070b },
2545 { 0x1a, 0x05ad },
2546 { 0x14, 0x94c0 },
2547
françois romieubca03d52011-01-03 15:07:31 +00002548 /*
2549 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002550 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002551 */
françois romieudaf9df62009-10-07 12:44:20 +00002552 { 0x1f, 0x0002 },
2553 { 0x06, 0x5561 },
2554 { 0x1f, 0x0005 },
2555 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002556 { 0x06, 0x5561 },
2557
2558 /*
2559 * Can not link to 1Gbps with bad cable
2560 * Decrease SNR threshold form 21.07dB to 19.04dB
2561 */
2562 { 0x1f, 0x0001 },
2563 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002564
2565 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002566 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002567 };
françois romieubca03d52011-01-03 15:07:31 +00002568 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002569
françois romieu4da19632011-01-03 15:07:55 +00002570 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002571
2572 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002573 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002574 { 0x1f, 0x0002 },
2575 { 0x05, 0x669a },
2576 { 0x1f, 0x0005 },
2577 { 0x05, 0x8330 },
2578 { 0x06, 0x669a },
2579
2580 { 0x1f, 0x0002 }
2581 };
2582 int val;
2583
françois romieu4da19632011-01-03 15:07:55 +00002584 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002585
françois romieu4da19632011-01-03 15:07:55 +00002586 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002587 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002588 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002589 0x0065, 0x0066, 0x0067, 0x0068,
2590 0x0069, 0x006a, 0x006b, 0x006c
2591 };
2592 int i;
2593
françois romieu4da19632011-01-03 15:07:55 +00002594 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002595
2596 val &= 0xff00;
2597 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002598 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002599 }
2600 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002601 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002602 { 0x1f, 0x0002 },
2603 { 0x05, 0x2642 },
2604 { 0x1f, 0x0005 },
2605 { 0x05, 0x8330 },
2606 { 0x06, 0x2642 }
2607 };
2608
françois romieu4da19632011-01-03 15:07:55 +00002609 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002610 }
2611
françois romieubca03d52011-01-03 15:07:31 +00002612 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002613 rtl_writephy(tp, 0x1f, 0x0002);
2614 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2615 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002616
françois romieubca03d52011-01-03 15:07:31 +00002617 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002618 rtl_writephy(tp, 0x1f, 0x0002);
2619 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002620
françois romieu4da19632011-01-03 15:07:55 +00002621 rtl_writephy(tp, 0x1f, 0x0005);
2622 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002623
2624 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002625
françois romieu4da19632011-01-03 15:07:55 +00002626 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002627}
2628
françois romieu4da19632011-01-03 15:07:55 +00002629static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002630{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002631 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002632 { 0x1f, 0x0002 },
2633 { 0x10, 0x0008 },
2634 { 0x0d, 0x006c },
2635
2636 { 0x1f, 0x0000 },
2637 { 0x0d, 0xf880 },
2638
2639 { 0x1f, 0x0001 },
2640 { 0x17, 0x0cc0 },
2641
2642 { 0x1f, 0x0001 },
2643 { 0x0b, 0xa4d8 },
2644 { 0x09, 0x281c },
2645 { 0x07, 0x2883 },
2646 { 0x0a, 0x6b35 },
2647 { 0x1d, 0x3da4 },
2648 { 0x1c, 0xeffd },
2649 { 0x14, 0x7f52 },
2650 { 0x18, 0x7fc6 },
2651 { 0x08, 0x0601 },
2652 { 0x06, 0x4063 },
2653 { 0x10, 0xf074 },
2654 { 0x1f, 0x0003 },
2655 { 0x13, 0x0789 },
2656 { 0x12, 0xf4bd },
2657 { 0x1a, 0x04fd },
2658 { 0x14, 0x84b0 },
2659 { 0x1f, 0x0000 },
2660 { 0x00, 0x9200 },
2661
2662 { 0x1f, 0x0005 },
2663 { 0x01, 0x0340 },
2664 { 0x1f, 0x0001 },
2665 { 0x04, 0x4000 },
2666 { 0x03, 0x1d21 },
2667 { 0x02, 0x0c32 },
2668 { 0x01, 0x0200 },
2669 { 0x00, 0x5554 },
2670 { 0x04, 0x4800 },
2671 { 0x04, 0x4000 },
2672 { 0x04, 0xf000 },
2673 { 0x03, 0xdf01 },
2674 { 0x02, 0xdf20 },
2675 { 0x01, 0x101a },
2676 { 0x00, 0xa0ff },
2677 { 0x04, 0xf800 },
2678 { 0x04, 0xf000 },
2679 { 0x1f, 0x0000 },
2680
2681 { 0x1f, 0x0007 },
2682 { 0x1e, 0x0023 },
2683 { 0x16, 0x0000 },
2684 { 0x1f, 0x0000 }
2685 };
2686
françois romieu4da19632011-01-03 15:07:55 +00002687 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002688}
2689
françois romieue6de30d2011-01-03 15:08:37 +00002690static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2691{
2692 static const struct phy_reg phy_reg_init[] = {
2693 { 0x1f, 0x0001 },
2694 { 0x17, 0x0cc0 },
2695
2696 { 0x1f, 0x0007 },
2697 { 0x1e, 0x002d },
2698 { 0x18, 0x0040 },
2699 { 0x1f, 0x0000 }
2700 };
2701
2702 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2703 rtl_patchphy(tp, 0x0d, 1 << 5);
2704}
2705
Hayes Wang70090422011-07-06 15:58:06 +08002706static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002707{
2708 static const struct phy_reg phy_reg_init[] = {
2709 /* Enable Delay cap */
2710 { 0x1f, 0x0005 },
2711 { 0x05, 0x8b80 },
2712 { 0x06, 0xc896 },
2713 { 0x1f, 0x0000 },
2714
2715 /* Channel estimation fine tune */
2716 { 0x1f, 0x0001 },
2717 { 0x0b, 0x6c20 },
2718 { 0x07, 0x2872 },
2719 { 0x1c, 0xefff },
2720 { 0x1f, 0x0003 },
2721 { 0x14, 0x6420 },
2722 { 0x1f, 0x0000 },
2723
2724 /* Update PFM & 10M TX idle timer */
2725 { 0x1f, 0x0007 },
2726 { 0x1e, 0x002f },
2727 { 0x15, 0x1919 },
2728 { 0x1f, 0x0000 },
2729
2730 { 0x1f, 0x0007 },
2731 { 0x1e, 0x00ac },
2732 { 0x18, 0x0006 },
2733 { 0x1f, 0x0000 }
2734 };
2735
Francois Romieu15ecd032011-04-27 13:52:22 -07002736 rtl_apply_firmware(tp);
2737
hayeswang01dc7fe2011-03-21 01:50:28 +00002738 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2739
2740 /* DCO enable for 10M IDLE Power */
2741 rtl_writephy(tp, 0x1f, 0x0007);
2742 rtl_writephy(tp, 0x1e, 0x0023);
2743 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2744 rtl_writephy(tp, 0x1f, 0x0000);
2745
2746 /* For impedance matching */
2747 rtl_writephy(tp, 0x1f, 0x0002);
2748 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002749 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002750
2751 /* PHY auto speed down */
2752 rtl_writephy(tp, 0x1f, 0x0007);
2753 rtl_writephy(tp, 0x1e, 0x002d);
2754 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2755 rtl_writephy(tp, 0x1f, 0x0000);
2756 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2757
2758 rtl_writephy(tp, 0x1f, 0x0005);
2759 rtl_writephy(tp, 0x05, 0x8b86);
2760 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2761 rtl_writephy(tp, 0x1f, 0x0000);
2762
2763 rtl_writephy(tp, 0x1f, 0x0005);
2764 rtl_writephy(tp, 0x05, 0x8b85);
2765 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2766 rtl_writephy(tp, 0x1f, 0x0007);
2767 rtl_writephy(tp, 0x1e, 0x0020);
2768 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2769 rtl_writephy(tp, 0x1f, 0x0006);
2770 rtl_writephy(tp, 0x00, 0x5a00);
2771 rtl_writephy(tp, 0x1f, 0x0000);
2772 rtl_writephy(tp, 0x0d, 0x0007);
2773 rtl_writephy(tp, 0x0e, 0x003c);
2774 rtl_writephy(tp, 0x0d, 0x4007);
2775 rtl_writephy(tp, 0x0e, 0x0000);
2776 rtl_writephy(tp, 0x0d, 0x0000);
2777}
2778
Hayes Wang70090422011-07-06 15:58:06 +08002779static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2780{
2781 static const struct phy_reg phy_reg_init[] = {
2782 /* Enable Delay cap */
2783 { 0x1f, 0x0004 },
2784 { 0x1f, 0x0007 },
2785 { 0x1e, 0x00ac },
2786 { 0x18, 0x0006 },
2787 { 0x1f, 0x0002 },
2788 { 0x1f, 0x0000 },
2789 { 0x1f, 0x0000 },
2790
2791 /* Channel estimation fine tune */
2792 { 0x1f, 0x0003 },
2793 { 0x09, 0xa20f },
2794 { 0x1f, 0x0000 },
2795 { 0x1f, 0x0000 },
2796
2797 /* Green Setting */
2798 { 0x1f, 0x0005 },
2799 { 0x05, 0x8b5b },
2800 { 0x06, 0x9222 },
2801 { 0x05, 0x8b6d },
2802 { 0x06, 0x8000 },
2803 { 0x05, 0x8b76 },
2804 { 0x06, 0x8000 },
2805 { 0x1f, 0x0000 }
2806 };
2807
2808 rtl_apply_firmware(tp);
2809
2810 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2811
2812 /* For 4-corner performance improve */
2813 rtl_writephy(tp, 0x1f, 0x0005);
2814 rtl_writephy(tp, 0x05, 0x8b80);
2815 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2816 rtl_writephy(tp, 0x1f, 0x0000);
2817
2818 /* PHY auto speed down */
2819 rtl_writephy(tp, 0x1f, 0x0004);
2820 rtl_writephy(tp, 0x1f, 0x0007);
2821 rtl_writephy(tp, 0x1e, 0x002d);
2822 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2823 rtl_writephy(tp, 0x1f, 0x0002);
2824 rtl_writephy(tp, 0x1f, 0x0000);
2825 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2826
2827 /* improve 10M EEE waveform */
2828 rtl_writephy(tp, 0x1f, 0x0005);
2829 rtl_writephy(tp, 0x05, 0x8b86);
2830 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2831 rtl_writephy(tp, 0x1f, 0x0000);
2832
2833 /* Improve 2-pair detection performance */
2834 rtl_writephy(tp, 0x1f, 0x0005);
2835 rtl_writephy(tp, 0x05, 0x8b85);
2836 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2837 rtl_writephy(tp, 0x1f, 0x0000);
2838
2839 /* EEE setting */
2840 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2841 ERIAR_EXGMAC);
2842 rtl_writephy(tp, 0x1f, 0x0005);
2843 rtl_writephy(tp, 0x05, 0x8b85);
2844 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2845 rtl_writephy(tp, 0x1f, 0x0004);
2846 rtl_writephy(tp, 0x1f, 0x0007);
2847 rtl_writephy(tp, 0x1e, 0x0020);
2848 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
2849 rtl_writephy(tp, 0x1f, 0x0002);
2850 rtl_writephy(tp, 0x1f, 0x0000);
2851 rtl_writephy(tp, 0x0d, 0x0007);
2852 rtl_writephy(tp, 0x0e, 0x003c);
2853 rtl_writephy(tp, 0x0d, 0x4007);
2854 rtl_writephy(tp, 0x0e, 0x0000);
2855 rtl_writephy(tp, 0x0d, 0x0000);
2856
2857 /* Green feature */
2858 rtl_writephy(tp, 0x1f, 0x0003);
2859 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2860 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2861 rtl_writephy(tp, 0x1f, 0x0000);
2862}
2863
françois romieu4da19632011-01-03 15:07:55 +00002864static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002865{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002866 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002867 { 0x1f, 0x0003 },
2868 { 0x08, 0x441d },
2869 { 0x01, 0x9100 },
2870 { 0x1f, 0x0000 }
2871 };
2872
françois romieu4da19632011-01-03 15:07:55 +00002873 rtl_writephy(tp, 0x1f, 0x0000);
2874 rtl_patchphy(tp, 0x11, 1 << 12);
2875 rtl_patchphy(tp, 0x19, 1 << 13);
2876 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002877
françois romieu4da19632011-01-03 15:07:55 +00002878 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002879}
2880
Hayes Wang5a5e4442011-02-22 17:26:21 +08002881static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2882{
2883 static const struct phy_reg phy_reg_init[] = {
2884 { 0x1f, 0x0005 },
2885 { 0x1a, 0x0000 },
2886 { 0x1f, 0x0000 },
2887
2888 { 0x1f, 0x0004 },
2889 { 0x1c, 0x0000 },
2890 { 0x1f, 0x0000 },
2891
2892 { 0x1f, 0x0001 },
2893 { 0x15, 0x7701 },
2894 { 0x1f, 0x0000 }
2895 };
2896
2897 /* Disable ALDPS before ram code */
2898 rtl_writephy(tp, 0x1f, 0x0000);
2899 rtl_writephy(tp, 0x18, 0x0310);
2900 msleep(100);
2901
François Romieu953a12c2011-04-24 17:38:48 +02002902 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002903
2904 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2905}
2906
Francois Romieu5615d9f2007-08-17 17:50:46 +02002907static void rtl_hw_phy_config(struct net_device *dev)
2908{
2909 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002910
2911 rtl8169_print_mac_version(tp);
2912
2913 switch (tp->mac_version) {
2914 case RTL_GIGA_MAC_VER_01:
2915 break;
2916 case RTL_GIGA_MAC_VER_02:
2917 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002918 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002919 break;
2920 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002921 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002922 break;
françois romieu2e9558562009-08-10 19:44:19 +00002923 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002924 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002925 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002926 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002927 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002928 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002929 case RTL_GIGA_MAC_VER_07:
2930 case RTL_GIGA_MAC_VER_08:
2931 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002932 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002933 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002934 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002935 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002936 break;
2937 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002938 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002939 break;
2940 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002941 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002942 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002943 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002944 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002945 break;
2946 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002947 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002948 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002949 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002950 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002951 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002952 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002953 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002954 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002955 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002956 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002957 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002958 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002959 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002960 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002961 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002962 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002963 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002964 break;
2965 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002966 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002967 break;
2968 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002969 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002970 break;
françois romieue6de30d2011-01-03 15:08:37 +00002971 case RTL_GIGA_MAC_VER_28:
2972 rtl8168d_4_hw_phy_config(tp);
2973 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002974 case RTL_GIGA_MAC_VER_29:
2975 case RTL_GIGA_MAC_VER_30:
2976 rtl8105e_hw_phy_config(tp);
2977 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002978 case RTL_GIGA_MAC_VER_31:
2979 /* None. */
2980 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002981 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002982 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08002983 rtl8168e_1_hw_phy_config(tp);
2984 break;
2985 case RTL_GIGA_MAC_VER_34:
2986 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002987 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002988
Francois Romieu5615d9f2007-08-17 17:50:46 +02002989 default:
2990 break;
2991 }
2992}
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994static void rtl8169_phy_timer(unsigned long __opaque)
2995{
2996 struct net_device *dev = (struct net_device *)__opaque;
2997 struct rtl8169_private *tp = netdev_priv(dev);
2998 struct timer_list *timer = &tp->timer;
2999 void __iomem *ioaddr = tp->mmio_addr;
3000 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3001
Francois Romieubcf0bf92006-07-26 23:14:13 +02003002 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 spin_lock_irq(&tp->lock);
3005
françois romieu4da19632011-01-03 15:07:55 +00003006 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003007 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 * A busy loop could burn quite a few cycles on nowadays CPU.
3009 * Let's delay the execution of the timer for a few ticks.
3010 */
3011 timeout = HZ/10;
3012 goto out_mod_timer;
3013 }
3014
3015 if (tp->link_ok(ioaddr))
3016 goto out_unlock;
3017
Joe Perchesbf82c182010-02-09 11:49:50 +00003018 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
françois romieu4da19632011-01-03 15:07:55 +00003020 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022out_mod_timer:
3023 mod_timer(timer, jiffies + timeout);
3024out_unlock:
3025 spin_unlock_irq(&tp->lock);
3026}
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028#ifdef CONFIG_NET_POLL_CONTROLLER
3029/*
3030 * Polling 'interrupt' - used by things like netconsole to send skbs
3031 * without having to re-enable interrupts. It's not called while
3032 * the interrupt routine is executing.
3033 */
3034static void rtl8169_netpoll(struct net_device *dev)
3035{
3036 struct rtl8169_private *tp = netdev_priv(dev);
3037 struct pci_dev *pdev = tp->pci_dev;
3038
3039 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003040 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 enable_irq(pdev->irq);
3042}
3043#endif
3044
3045static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3046 void __iomem *ioaddr)
3047{
3048 iounmap(ioaddr);
3049 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003050 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 pci_disable_device(pdev);
3052 free_netdev(dev);
3053}
3054
Francois Romieubf793292006-11-01 00:53:05 +01003055static void rtl8169_phy_reset(struct net_device *dev,
3056 struct rtl8169_private *tp)
3057{
Francois Romieu07d3f512007-02-21 22:40:46 +01003058 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003059
françois romieu4da19632011-01-03 15:07:55 +00003060 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003061 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003062 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003063 return;
3064 msleep(1);
3065 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003066 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003067}
3068
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003069static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003071 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003072
Francois Romieu5615d9f2007-08-17 17:50:46 +02003073 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003074
Marcus Sundberg773328942008-07-10 21:28:08 +02003075 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3076 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3077 RTL_W8(0x82, 0x01);
3078 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003079
Francois Romieu6dccd162007-02-13 23:38:05 +01003080 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3081
3082 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3083 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003084
Francois Romieubcf0bf92006-07-26 23:14:13 +02003085 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003086 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3087 RTL_W8(0x82, 0x01);
3088 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003089 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003090 }
3091
Francois Romieubf793292006-11-01 00:53:05 +01003092 rtl8169_phy_reset(dev, tp);
3093
Oliver Neukum54405cd2011-01-06 21:55:13 +01003094 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003095 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3096 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3097 (tp->mii.supports_gmii ?
3098 ADVERTISED_1000baseT_Half |
3099 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003100
Joe Perchesbf82c182010-02-09 11:49:50 +00003101 if (RTL_R8(PHYstatus) & TBI_Enable)
3102 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003103}
3104
Francois Romieu773d2022007-01-31 23:47:43 +01003105static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3106{
3107 void __iomem *ioaddr = tp->mmio_addr;
3108 u32 high;
3109 u32 low;
3110
3111 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3112 high = addr[4] | (addr[5] << 8);
3113
3114 spin_lock_irq(&tp->lock);
3115
3116 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003117
Francois Romieu773d2022007-01-31 23:47:43 +01003118 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003119 RTL_R32(MAC4);
3120
Francois Romieu78f1cd02010-03-27 19:35:46 -07003121 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003122 RTL_R32(MAC0);
3123
Francois Romieu773d2022007-01-31 23:47:43 +01003124 RTL_W8(Cfg9346, Cfg9346_Lock);
3125
3126 spin_unlock_irq(&tp->lock);
3127}
3128
3129static int rtl_set_mac_address(struct net_device *dev, void *p)
3130{
3131 struct rtl8169_private *tp = netdev_priv(dev);
3132 struct sockaddr *addr = p;
3133
3134 if (!is_valid_ether_addr(addr->sa_data))
3135 return -EADDRNOTAVAIL;
3136
3137 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3138
3139 rtl_rar_set(tp, dev->dev_addr);
3140
3141 return 0;
3142}
3143
Francois Romieu5f787a12006-08-17 13:02:36 +02003144static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3145{
3146 struct rtl8169_private *tp = netdev_priv(dev);
3147 struct mii_ioctl_data *data = if_mii(ifr);
3148
Francois Romieu8b4ab282008-11-19 22:05:25 -08003149 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3150}
Francois Romieu5f787a12006-08-17 13:02:36 +02003151
Francois Romieucecb5fd2011-04-01 10:21:07 +02003152static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3153 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003154{
Francois Romieu5f787a12006-08-17 13:02:36 +02003155 switch (cmd) {
3156 case SIOCGMIIPHY:
3157 data->phy_id = 32; /* Internal PHY */
3158 return 0;
3159
3160 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003161 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003162 return 0;
3163
3164 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003165 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003166 return 0;
3167 }
3168 return -EOPNOTSUPP;
3169}
3170
Francois Romieu8b4ab282008-11-19 22:05:25 -08003171static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3172{
3173 return -EOPNOTSUPP;
3174}
3175
Francois Romieu0e485152007-02-20 00:00:26 +01003176static const struct rtl_cfg_info {
3177 void (*hw_start)(struct net_device *);
3178 unsigned int region;
3179 unsigned int align;
3180 u16 intr_event;
3181 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003182 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003183 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003184} rtl_cfg_infos [] = {
3185 [RTL_CFG_0] = {
3186 .hw_start = rtl_hw_start_8169,
3187 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003188 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003189 .intr_event = SYSErr | LinkChg | RxOverflow |
3190 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003191 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003192 .features = RTL_FEATURE_GMII,
3193 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003194 },
3195 [RTL_CFG_1] = {
3196 .hw_start = rtl_hw_start_8168,
3197 .region = 2,
3198 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003199 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003200 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003201 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003202 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3203 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003204 },
3205 [RTL_CFG_2] = {
3206 .hw_start = rtl_hw_start_8101,
3207 .region = 2,
3208 .align = 8,
3209 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3210 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003211 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003212 .features = RTL_FEATURE_MSI,
3213 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003214 }
3215};
3216
Francois Romieufbac58f2007-10-04 22:51:38 +02003217/* Cfg9346_Unlock assumed. */
3218static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3219 const struct rtl_cfg_info *cfg)
3220{
3221 unsigned msi = 0;
3222 u8 cfg2;
3223
3224 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003225 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003226 if (pci_enable_msi(pdev)) {
3227 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3228 } else {
3229 cfg2 |= MSIEnable;
3230 msi = RTL_FEATURE_MSI;
3231 }
3232 }
3233 RTL_W8(Config2, cfg2);
3234 return msi;
3235}
3236
3237static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3238{
3239 if (tp->features & RTL_FEATURE_MSI) {
3240 pci_disable_msi(pdev);
3241 tp->features &= ~RTL_FEATURE_MSI;
3242 }
3243}
3244
Francois Romieu8b4ab282008-11-19 22:05:25 -08003245static const struct net_device_ops rtl8169_netdev_ops = {
3246 .ndo_open = rtl8169_open,
3247 .ndo_stop = rtl8169_close,
3248 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003249 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003250 .ndo_tx_timeout = rtl8169_tx_timeout,
3251 .ndo_validate_addr = eth_validate_addr,
3252 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003253 .ndo_fix_features = rtl8169_fix_features,
3254 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003255 .ndo_set_mac_address = rtl_set_mac_address,
3256 .ndo_do_ioctl = rtl8169_ioctl,
3257 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003258#ifdef CONFIG_NET_POLL_CONTROLLER
3259 .ndo_poll_controller = rtl8169_netpoll,
3260#endif
3261
3262};
3263
françois romieuc0e45c12011-01-03 15:08:04 +00003264static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3265{
3266 struct mdio_ops *ops = &tp->mdio_ops;
3267
3268 switch (tp->mac_version) {
3269 case RTL_GIGA_MAC_VER_27:
3270 ops->write = r8168dp_1_mdio_write;
3271 ops->read = r8168dp_1_mdio_read;
3272 break;
françois romieue6de30d2011-01-03 15:08:37 +00003273 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003274 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003275 ops->write = r8168dp_2_mdio_write;
3276 ops->read = r8168dp_2_mdio_read;
3277 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003278 default:
3279 ops->write = r8169_mdio_write;
3280 ops->read = r8169_mdio_read;
3281 break;
3282 }
3283}
3284
françois romieu065c27c2011-01-03 15:08:12 +00003285static void r810x_phy_power_down(struct rtl8169_private *tp)
3286{
3287 rtl_writephy(tp, 0x1f, 0x0000);
3288 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3289}
3290
3291static void r810x_phy_power_up(struct rtl8169_private *tp)
3292{
3293 rtl_writephy(tp, 0x1f, 0x0000);
3294 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3295}
3296
3297static void r810x_pll_power_down(struct rtl8169_private *tp)
3298{
3299 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3300 rtl_writephy(tp, 0x1f, 0x0000);
3301 rtl_writephy(tp, MII_BMCR, 0x0000);
3302 return;
3303 }
3304
3305 r810x_phy_power_down(tp);
3306}
3307
3308static void r810x_pll_power_up(struct rtl8169_private *tp)
3309{
3310 r810x_phy_power_up(tp);
3311}
3312
3313static void r8168_phy_power_up(struct rtl8169_private *tp)
3314{
3315 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003316 switch (tp->mac_version) {
3317 case RTL_GIGA_MAC_VER_11:
3318 case RTL_GIGA_MAC_VER_12:
3319 case RTL_GIGA_MAC_VER_17:
3320 case RTL_GIGA_MAC_VER_18:
3321 case RTL_GIGA_MAC_VER_19:
3322 case RTL_GIGA_MAC_VER_20:
3323 case RTL_GIGA_MAC_VER_21:
3324 case RTL_GIGA_MAC_VER_22:
3325 case RTL_GIGA_MAC_VER_23:
3326 case RTL_GIGA_MAC_VER_24:
3327 case RTL_GIGA_MAC_VER_25:
3328 case RTL_GIGA_MAC_VER_26:
3329 case RTL_GIGA_MAC_VER_27:
3330 case RTL_GIGA_MAC_VER_28:
3331 case RTL_GIGA_MAC_VER_31:
3332 rtl_writephy(tp, 0x0e, 0x0000);
3333 break;
3334 default:
3335 break;
3336 }
françois romieu065c27c2011-01-03 15:08:12 +00003337 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3338}
3339
3340static void r8168_phy_power_down(struct rtl8169_private *tp)
3341{
3342 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003343 switch (tp->mac_version) {
3344 case RTL_GIGA_MAC_VER_32:
3345 case RTL_GIGA_MAC_VER_33:
3346 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3347 break;
3348
3349 case RTL_GIGA_MAC_VER_11:
3350 case RTL_GIGA_MAC_VER_12:
3351 case RTL_GIGA_MAC_VER_17:
3352 case RTL_GIGA_MAC_VER_18:
3353 case RTL_GIGA_MAC_VER_19:
3354 case RTL_GIGA_MAC_VER_20:
3355 case RTL_GIGA_MAC_VER_21:
3356 case RTL_GIGA_MAC_VER_22:
3357 case RTL_GIGA_MAC_VER_23:
3358 case RTL_GIGA_MAC_VER_24:
3359 case RTL_GIGA_MAC_VER_25:
3360 case RTL_GIGA_MAC_VER_26:
3361 case RTL_GIGA_MAC_VER_27:
3362 case RTL_GIGA_MAC_VER_28:
3363 case RTL_GIGA_MAC_VER_31:
3364 rtl_writephy(tp, 0x0e, 0x0200);
3365 default:
3366 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3367 break;
3368 }
françois romieu065c27c2011-01-03 15:08:12 +00003369}
3370
3371static void r8168_pll_power_down(struct rtl8169_private *tp)
3372{
3373 void __iomem *ioaddr = tp->mmio_addr;
3374
Francois Romieucecb5fd2011-04-01 10:21:07 +02003375 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3376 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3377 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003378 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003379 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003380 }
françois romieu065c27c2011-01-03 15:08:12 +00003381
Francois Romieucecb5fd2011-04-01 10:21:07 +02003382 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3383 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003384 (RTL_R16(CPlusCmd) & ASF)) {
3385 return;
3386 }
3387
hayeswang01dc7fe2011-03-21 01:50:28 +00003388 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3389 tp->mac_version == RTL_GIGA_MAC_VER_33)
3390 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3391
françois romieu065c27c2011-01-03 15:08:12 +00003392 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3393 rtl_writephy(tp, 0x1f, 0x0000);
3394 rtl_writephy(tp, MII_BMCR, 0x0000);
3395
3396 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3397 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3398 return;
3399 }
3400
3401 r8168_phy_power_down(tp);
3402
3403 switch (tp->mac_version) {
3404 case RTL_GIGA_MAC_VER_25:
3405 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003406 case RTL_GIGA_MAC_VER_27:
3407 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003408 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003409 case RTL_GIGA_MAC_VER_32:
3410 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003411 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3412 break;
3413 }
3414}
3415
3416static void r8168_pll_power_up(struct rtl8169_private *tp)
3417{
3418 void __iomem *ioaddr = tp->mmio_addr;
3419
Francois Romieucecb5fd2011-04-01 10:21:07 +02003420 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3421 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3422 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003423 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003424 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003425 }
françois romieu065c27c2011-01-03 15:08:12 +00003426
3427 switch (tp->mac_version) {
3428 case RTL_GIGA_MAC_VER_25:
3429 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003430 case RTL_GIGA_MAC_VER_27:
3431 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003432 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003433 case RTL_GIGA_MAC_VER_32:
3434 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003435 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3436 break;
3437 }
3438
3439 r8168_phy_power_up(tp);
3440}
3441
3442static void rtl_pll_power_op(struct rtl8169_private *tp,
3443 void (*op)(struct rtl8169_private *))
3444{
3445 if (op)
3446 op(tp);
3447}
3448
3449static void rtl_pll_power_down(struct rtl8169_private *tp)
3450{
3451 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3452}
3453
3454static void rtl_pll_power_up(struct rtl8169_private *tp)
3455{
3456 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3457}
3458
3459static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3460{
3461 struct pll_power_ops *ops = &tp->pll_power_ops;
3462
3463 switch (tp->mac_version) {
3464 case RTL_GIGA_MAC_VER_07:
3465 case RTL_GIGA_MAC_VER_08:
3466 case RTL_GIGA_MAC_VER_09:
3467 case RTL_GIGA_MAC_VER_10:
3468 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003469 case RTL_GIGA_MAC_VER_29:
3470 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003471 ops->down = r810x_pll_power_down;
3472 ops->up = r810x_pll_power_up;
3473 break;
3474
3475 case RTL_GIGA_MAC_VER_11:
3476 case RTL_GIGA_MAC_VER_12:
3477 case RTL_GIGA_MAC_VER_17:
3478 case RTL_GIGA_MAC_VER_18:
3479 case RTL_GIGA_MAC_VER_19:
3480 case RTL_GIGA_MAC_VER_20:
3481 case RTL_GIGA_MAC_VER_21:
3482 case RTL_GIGA_MAC_VER_22:
3483 case RTL_GIGA_MAC_VER_23:
3484 case RTL_GIGA_MAC_VER_24:
3485 case RTL_GIGA_MAC_VER_25:
3486 case RTL_GIGA_MAC_VER_26:
3487 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003488 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003489 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003490 case RTL_GIGA_MAC_VER_32:
3491 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003492 case RTL_GIGA_MAC_VER_34:
françois romieu065c27c2011-01-03 15:08:12 +00003493 ops->down = r8168_pll_power_down;
3494 ops->up = r8168_pll_power_up;
3495 break;
3496
3497 default:
3498 ops->down = NULL;
3499 ops->up = NULL;
3500 break;
3501 }
3502}
3503
Hayes Wang92fc43b2011-07-06 15:58:03 +08003504static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3505{
3506 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3507}
3508
Francois Romieu6f43adc2011-04-29 15:05:51 +02003509static void rtl_hw_reset(struct rtl8169_private *tp)
3510{
3511 void __iomem *ioaddr = tp->mmio_addr;
3512 int i;
3513
3514 /* Soft reset the chip. */
3515 RTL_W8(ChipCmd, CmdReset);
3516
3517 /* Check that the chip has finished the reset. */
3518 for (i = 0; i < 100; i++) {
3519 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3520 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003521 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003522 }
Hayes Wang92fc43b2011-07-06 15:58:03 +08003523
3524 rtl8169_init_ring_indexes(tp);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003525}
3526
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003527static int __devinit
3528rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3529{
Francois Romieu0e485152007-02-20 00:00:26 +01003530 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3531 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003533 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003534 struct net_device *dev;
3535 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003536 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003537 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003539 if (netif_msg_drv(&debug)) {
3540 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3541 MODULENAME, RTL8169_VERSION);
3542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003545 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003546 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003547 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003548 rc = -ENOMEM;
3549 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 }
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003553 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003555 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003556 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003557 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Francois Romieuccdffb92008-07-26 14:26:06 +02003559 mii = &tp->mii;
3560 mii->dev = dev;
3561 mii->mdio_read = rtl_mdio_read;
3562 mii->mdio_write = rtl_mdio_write;
3563 mii->phy_id_mask = 0x1f;
3564 mii->reg_num_mask = 0x1f;
3565 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3566
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003567 /* disable ASPM completely as that cause random device stop working
3568 * problems as well as full system hangs for some PCIe devices users */
3569 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3570 PCIE_LINK_STATE_CLKPM);
3571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3573 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003574 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003575 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003576 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 }
3578
françois romieu87aeec72010-04-26 11:42:06 +00003579 if (pci_set_mwi(pdev) < 0)
3580 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003583 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003584 netif_err(tp, probe, dev,
3585 "region #%d not an MMIO resource, aborting\n",
3586 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003588 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003592 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003593 netif_err(tp, probe, dev,
3594 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003596 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 }
3598
3599 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003600 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003601 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003602 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
3604
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003605 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
3607 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003608 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 tp->cp_cmd |= PCIDAC;
3610 dev->features |= NETIF_F_HIGHDMA;
3611 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003612 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003614 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003615 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
3617 }
3618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003620 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003621 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003622 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003624 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003626 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Jon Masone44daad2011-06-27 07:46:31 +00003628 if (!pci_is_pcie(pdev))
3629 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07003630
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003631 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Francois Romieu6f43adc2011-04-29 15:05:51 +02003633 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003635 RTL_W16(IntrStatus, 0xffff);
3636
françois romieuca52efd2009-07-24 12:34:19 +00003637 pci_set_master(pdev);
3638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 /* Identify chip attached to board */
Francois Romieu5d320a22011-05-08 17:47:36 +02003640 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Francois Romieu7a8fc772011-03-01 17:18:33 +01003642 /*
3643 * Pretend we are using VLANs; This bypasses a nasty bug where
3644 * Interrupts stop flowing on high load on 8110SCd controllers.
3645 */
3646 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3647 tp->cp_cmd |= RxVlan;
3648
françois romieuc0e45c12011-01-03 15:08:04 +00003649 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003650 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Francois Romieu85bffe62011-04-27 08:22:39 +02003654 chipset = tp->mac_version;
3655 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Francois Romieu5d06a992006-02-23 00:47:58 +01003657 RTL_W8(Cfg9346, Cfg9346_Unlock);
3658 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3659 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003660 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3661 tp->features |= RTL_FEATURE_WOL;
3662 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3663 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003664 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003665 RTL_W8(Cfg9346, Cfg9346_Lock);
3666
Francois Romieu66ec5d42007-11-06 22:56:10 +01003667 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3668 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 tp->set_speed = rtl8169_set_speed_tbi;
3670 tp->get_settings = rtl8169_gset_tbi;
3671 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3672 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3673 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003674 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 } else {
3676 tp->set_speed = rtl8169_set_speed_xmii;
3677 tp->get_settings = rtl8169_gset_xmii;
3678 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3679 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3680 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003681 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 }
3683
Francois Romieudf58ef52008-10-09 14:35:58 -07003684 spin_lock_init(&tp->lock);
3685
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003686 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 for (i = 0; i < MAC_ADDR_LEN; i++)
3688 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003689 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3693 dev->irq = pdev->irq;
3694 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003696 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Michał Mirosław350fb322011-04-08 06:35:56 +00003698 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3699 * properly for all devices */
3700 dev->features |= NETIF_F_RXCSUM |
3701 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3702
3703 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3704 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3705 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3706 NETIF_F_HIGHDMA;
3707
3708 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3709 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3710 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
3712 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003713 tp->hw_start = cfg->hw_start;
3714 tp->intr_event = cfg->intr_event;
3715 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
Francois Romieu2efa53f2007-03-09 00:00:05 +01003717 init_timer(&tp->timer);
3718 tp->timer.data = (unsigned long) dev;
3719 tp->timer.function = rtl8169_phy_timer;
3720
Francois Romieub6ffd972011-06-17 17:00:05 +02003721 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003724 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003725 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
3727 pci_set_drvdata(pdev, dev);
3728
Joe Perchesbf82c182010-02-09 11:49:50 +00003729 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003730 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003731 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Francois Romieucecb5fd2011-04-01 10:21:07 +02003733 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3734 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3735 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003736 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003737 }
françois romieub646d902011-01-03 15:08:21 +00003738
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003739 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Alan Sternf3ec4f82010-06-08 15:23:51 -04003741 if (pci_dev_run_wake(pdev))
3742 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003743
Ivan Vecera0d672e92011-02-15 02:08:39 +00003744 netif_carrier_off(dev);
3745
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003746out:
3747 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
françois romieu87aeec72010-04-26 11:42:06 +00003749err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003750 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003751 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003752err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003753 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003754err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003755 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003756 pci_disable_device(pdev);
3757err_out_free_dev_1:
3758 free_netdev(dev);
3759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760}
3761
Francois Romieu07d3f512007-02-21 22:40:46 +01003762static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763{
3764 struct net_device *dev = pci_get_drvdata(pdev);
3765 struct rtl8169_private *tp = netdev_priv(dev);
3766
Francois Romieucecb5fd2011-04-01 10:21:07 +02003767 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3768 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3769 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003770 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003771 }
françois romieub646d902011-01-03 15:08:21 +00003772
Tejun Heo23f333a2010-12-12 16:45:14 +01003773 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003774
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003776
François Romieu953a12c2011-04-24 17:38:48 +02003777 rtl_release_firmware(tp);
3778
Alan Sternf3ec4f82010-06-08 15:23:51 -04003779 if (pci_dev_run_wake(pdev))
3780 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003781
Ivan Veceracc098dc2009-11-29 23:12:52 -08003782 /* restore original MAC address */
3783 rtl_rar_set(tp, dev->perm_addr);
3784
Francois Romieufbac58f2007-10-04 22:51:38 +02003785 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3787 pci_set_drvdata(pdev, NULL);
3788}
3789
Francois Romieub6ffd972011-06-17 17:00:05 +02003790static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3791{
3792 struct rtl_fw *rtl_fw;
3793 const char *name;
3794 int rc = -ENOMEM;
3795
3796 name = rtl_lookup_firmware_name(tp);
3797 if (!name)
3798 goto out_no_firmware;
3799
3800 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3801 if (!rtl_fw)
3802 goto err_warn;
3803
3804 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3805 if (rc < 0)
3806 goto err_free;
3807
Francois Romieufd112f22011-06-18 00:10:29 +02003808 rc = rtl_check_firmware(tp, rtl_fw);
3809 if (rc < 0)
3810 goto err_release_firmware;
3811
Francois Romieub6ffd972011-06-17 17:00:05 +02003812 tp->rtl_fw = rtl_fw;
3813out:
3814 return;
3815
Francois Romieufd112f22011-06-18 00:10:29 +02003816err_release_firmware:
3817 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003818err_free:
3819 kfree(rtl_fw);
3820err_warn:
3821 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3822 name, rc);
3823out_no_firmware:
3824 tp->rtl_fw = NULL;
3825 goto out;
3826}
3827
François Romieu953a12c2011-04-24 17:38:48 +02003828static void rtl_request_firmware(struct rtl8169_private *tp)
3829{
Francois Romieub6ffd972011-06-17 17:00:05 +02003830 if (IS_ERR(tp->rtl_fw))
3831 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003832}
3833
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834static int rtl8169_open(struct net_device *dev)
3835{
3836 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003837 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003839 int retval = -ENOMEM;
3840
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003841 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Neil Hormanc0cd8842010-03-29 13:16:02 -07003843 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003845 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003847 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3848 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003850 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003852 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3853 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003855 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
3857 retval = rtl8169_init_ring(dev);
3858 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003859 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
David Howellsc4028952006-11-22 14:57:56 +00003861 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Francois Romieu99f252b2007-04-02 22:59:59 +02003863 smp_mb();
3864
François Romieu953a12c2011-04-24 17:38:48 +02003865 rtl_request_firmware(tp);
3866
Francois Romieufbac58f2007-10-04 22:51:38 +02003867 retval = request_irq(dev->irq, rtl8169_interrupt,
3868 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003869 dev->name, dev);
3870 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003871 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003872
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003873 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003874
françois romieueee3a962011-01-08 02:17:26 +00003875 rtl8169_init_phy(dev, tp);
3876
Michał Mirosław350fb322011-04-08 06:35:56 +00003877 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003878
françois romieu065c27c2011-01-03 15:08:12 +00003879 rtl_pll_power_up(tp);
3880
Francois Romieu07ce4062007-02-23 23:36:39 +01003881 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003883 tp->saved_wolopts = 0;
3884 pm_runtime_put_noidle(&pdev->dev);
3885
françois romieueee3a962011-01-08 02:17:26 +00003886 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887out:
3888 return retval;
3889
François Romieu953a12c2011-04-24 17:38:48 +02003890err_release_fw_2:
3891 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003892 rtl8169_rx_clear(tp);
3893err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003894 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3895 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003896 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003897err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003898 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3899 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003900 tp->TxDescArray = NULL;
3901err_pm_runtime_put:
3902 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 goto out;
3904}
3905
Hayes Wang92fc43b2011-07-06 15:58:03 +08003906static void rtl_rx_close(struct rtl8169_private *tp)
3907{
3908 void __iomem *ioaddr = tp->mmio_addr;
3909 u32 rxcfg = RTL_R32(RxConfig);
3910
3911 rxcfg &= ~(AcceptErr | AcceptRunt | AcceptBroadcast | AcceptMulticast |
3912 AcceptMyPhys | AcceptAllPhys);
3913 RTL_W32(RxConfig, rxcfg);
3914}
3915
françois romieue6de30d2011-01-03 15:08:37 +00003916static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917{
françois romieue6de30d2011-01-03 15:08:37 +00003918 void __iomem *ioaddr = tp->mmio_addr;
3919
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 /* Disable interrupts */
3921 rtl8169_irq_mask_and_ack(ioaddr);
3922
Hayes Wang92fc43b2011-07-06 15:58:03 +08003923 rtl_rx_close(tp);
3924
Hayes Wang5d2e1952011-02-22 17:26:22 +08003925 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003926 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3927 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003928 while (RTL_R8(TxPoll) & NPQ)
3929 udelay(20);
Hayes Wang70090422011-07-06 15:58:06 +08003930 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3931 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
3932 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003933 } else {
3934 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
3935 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00003936 }
3937
Hayes Wang92fc43b2011-07-06 15:58:03 +08003938 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939}
3940
Francois Romieu7f796d82007-06-11 23:04:41 +02003941static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003942{
3943 void __iomem *ioaddr = tp->mmio_addr;
3944 u32 cfg = rtl8169_rx_config;
3945
Francois Romieu2b7b4312011-04-18 22:53:24 -07003946 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003947 RTL_W32(RxConfig, cfg);
3948
3949 /* Set DMA burst size and Interframe Gap Time */
3950 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3951 (InterFrameGap << TxInterFrameGapShift));
3952}
3953
Francois Romieu07ce4062007-02-23 23:36:39 +01003954static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955{
3956 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Francois Romieu07ce4062007-02-23 23:36:39 +01003958 tp->hw_start(dev);
3959
Francois Romieu07ce4062007-02-23 23:36:39 +01003960 netif_start_queue(dev);
3961}
3962
Francois Romieu7f796d82007-06-11 23:04:41 +02003963static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3964 void __iomem *ioaddr)
3965{
3966 /*
3967 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3968 * register to be written before TxDescAddrLow to work.
3969 * Switching from MMIO to I/O access fixes the issue as well.
3970 */
3971 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003972 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003973 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003974 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003975}
3976
3977static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3978{
3979 u16 cmd;
3980
3981 cmd = RTL_R16(CPlusCmd);
3982 RTL_W16(CPlusCmd, cmd);
3983 return cmd;
3984}
3985
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003986static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003987{
3988 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003989 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003990}
3991
Francois Romieu6dccd162007-02-13 23:38:05 +01003992static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3993{
Francois Romieu37441002011-06-17 22:58:54 +02003994 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01003995 u32 mac_version;
3996 u32 clk;
3997 u32 val;
3998 } cfg2_info [] = {
3999 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4000 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4001 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4002 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004003 };
4004 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004005 unsigned int i;
4006 u32 clk;
4007
4008 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004009 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004010 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4011 RTL_W32(0x7c, p->val);
4012 break;
4013 }
4014 }
4015}
4016
Francois Romieu07ce4062007-02-23 23:36:39 +01004017static void rtl_hw_start_8169(struct net_device *dev)
4018{
4019 struct rtl8169_private *tp = netdev_priv(dev);
4020 void __iomem *ioaddr = tp->mmio_addr;
4021 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004022
Francois Romieu9cb427b2006-11-02 00:10:16 +01004023 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4024 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4025 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4026 }
4027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004029 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4030 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4031 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4032 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004033 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4034
françois romieuf0298f82011-01-03 15:07:42 +00004035 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004037 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Francois Romieucecb5fd2011-04-01 10:21:07 +02004039 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4040 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4041 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4042 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004043 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Francois Romieu7f796d82007-06-11 23:04:41 +02004045 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004046
Francois Romieucecb5fd2011-04-01 10:21:07 +02004047 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4048 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004049 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004051 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 }
4053
Francois Romieubcf0bf92006-07-26 23:14:13 +02004054 RTL_W16(CPlusCmd, tp->cp_cmd);
4055
Francois Romieu6dccd162007-02-13 23:38:05 +01004056 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4057
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 /*
4059 * Undocumented corner. Supposedly:
4060 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4061 */
4062 RTL_W16(IntrMitigate, 0x0000);
4063
Francois Romieu7f796d82007-06-11 23:04:41 +02004064 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004065
Francois Romieucecb5fd2011-04-01 10:21:07 +02004066 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4067 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4068 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4069 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004070 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4071 rtl_set_rx_tx_config_registers(tp);
4072 }
4073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004075
4076 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4077 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079 RTL_W32(RxMissed, 0);
4080
Francois Romieu07ce4062007-02-23 23:36:39 +01004081 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
4083 /* no early-rx interrupts */
4084 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004085
4086 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01004087 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004088}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Francois Romieu9c14cea2008-07-05 00:21:15 +02004090static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02004091{
Jon Masone44daad2011-06-27 07:46:31 +00004092 int cap = pci_pcie_cap(pdev);
Francois Romieu458a9f62008-08-02 15:50:02 +02004093
Francois Romieu9c14cea2008-07-05 00:21:15 +02004094 if (cap) {
4095 u16 ctl;
4096
4097 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
4098 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
4099 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
4100 }
Francois Romieu458a9f62008-08-02 15:50:02 +02004101}
4102
françois romieu650e8d52011-01-03 15:08:29 +00004103static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004104{
4105 u32 csi;
4106
4107 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004108 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4109}
4110
françois romieue6de30d2011-01-03 15:08:37 +00004111static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4112{
4113 rtl_csi_access_enable(ioaddr, 0x17000000);
4114}
4115
françois romieu650e8d52011-01-03 15:08:29 +00004116static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4117{
4118 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004119}
4120
4121struct ephy_info {
4122 unsigned int offset;
4123 u16 mask;
4124 u16 bits;
4125};
4126
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004127static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004128{
4129 u16 w;
4130
4131 while (len-- > 0) {
4132 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4133 rtl_ephy_write(ioaddr, e->offset, w);
4134 e++;
4135 }
4136}
4137
Francois Romieub726e492008-06-28 12:22:59 +02004138static void rtl_disable_clock_request(struct pci_dev *pdev)
4139{
Jon Masone44daad2011-06-27 07:46:31 +00004140 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004141
4142 if (cap) {
4143 u16 ctl;
4144
4145 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4146 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4147 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4148 }
4149}
4150
françois romieue6de30d2011-01-03 15:08:37 +00004151static void rtl_enable_clock_request(struct pci_dev *pdev)
4152{
Jon Masone44daad2011-06-27 07:46:31 +00004153 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004154
4155 if (cap) {
4156 u16 ctl;
4157
4158 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4159 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4160 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4161 }
4162}
4163
Francois Romieub726e492008-06-28 12:22:59 +02004164#define R8168_CPCMD_QUIRK_MASK (\
4165 EnableBist | \
4166 Mac_dbgo_oe | \
4167 Force_half_dup | \
4168 Force_rxflow_en | \
4169 Force_txflow_en | \
4170 Cxpl_dbg_sel | \
4171 ASF | \
4172 PktCntrDisable | \
4173 Mac_dbgo_sel)
4174
Francois Romieu219a1e92008-06-28 11:58:39 +02004175static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4176{
Francois Romieub726e492008-06-28 12:22:59 +02004177 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4178
4179 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4180
Francois Romieu2e68ae42008-06-28 12:00:55 +02004181 rtl_tx_performance_tweak(pdev,
4182 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004183}
4184
4185static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4186{
4187 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004188
françois romieuf0298f82011-01-03 15:07:42 +00004189 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004190
4191 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004192}
4193
4194static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4195{
Francois Romieub726e492008-06-28 12:22:59 +02004196 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4197
4198 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4199
Francois Romieu219a1e92008-06-28 11:58:39 +02004200 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004201
4202 rtl_disable_clock_request(pdev);
4203
4204 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004205}
4206
Francois Romieuef3386f2008-06-29 12:24:30 +02004207static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004208{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004209 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004210 { 0x01, 0, 0x0001 },
4211 { 0x02, 0x0800, 0x1000 },
4212 { 0x03, 0, 0x0042 },
4213 { 0x06, 0x0080, 0x0000 },
4214 { 0x07, 0, 0x2000 }
4215 };
4216
françois romieu650e8d52011-01-03 15:08:29 +00004217 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004218
4219 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4220
Francois Romieu219a1e92008-06-28 11:58:39 +02004221 __rtl_hw_start_8168cp(ioaddr, pdev);
4222}
4223
Francois Romieuef3386f2008-06-29 12:24:30 +02004224static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4225{
françois romieu650e8d52011-01-03 15:08:29 +00004226 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004227
4228 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4229
4230 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4231
4232 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4233}
4234
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004235static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4236{
françois romieu650e8d52011-01-03 15:08:29 +00004237 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004238
4239 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4240
4241 /* Magic. */
4242 RTL_W8(DBG_REG, 0x20);
4243
françois romieuf0298f82011-01-03 15:07:42 +00004244 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004245
4246 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4247
4248 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4249}
4250
Francois Romieu219a1e92008-06-28 11:58:39 +02004251static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4252{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004253 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004254 { 0x02, 0x0800, 0x1000 },
4255 { 0x03, 0, 0x0002 },
4256 { 0x06, 0x0080, 0x0000 }
4257 };
4258
françois romieu650e8d52011-01-03 15:08:29 +00004259 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004260
4261 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4262
4263 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4264
Francois Romieu219a1e92008-06-28 11:58:39 +02004265 __rtl_hw_start_8168cp(ioaddr, pdev);
4266}
4267
4268static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4269{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004270 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004271 { 0x01, 0, 0x0001 },
4272 { 0x03, 0x0400, 0x0220 }
4273 };
4274
françois romieu650e8d52011-01-03 15:08:29 +00004275 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004276
4277 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4278
Francois Romieu219a1e92008-06-28 11:58:39 +02004279 __rtl_hw_start_8168cp(ioaddr, pdev);
4280}
4281
Francois Romieu197ff762008-06-28 13:16:02 +02004282static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4283{
4284 rtl_hw_start_8168c_2(ioaddr, pdev);
4285}
4286
Francois Romieu6fb07052008-06-29 11:54:28 +02004287static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4288{
françois romieu650e8d52011-01-03 15:08:29 +00004289 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004290
4291 __rtl_hw_start_8168cp(ioaddr, pdev);
4292}
4293
Francois Romieu5b538df2008-07-20 16:22:45 +02004294static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4295{
françois romieu650e8d52011-01-03 15:08:29 +00004296 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004297
4298 rtl_disable_clock_request(pdev);
4299
françois romieuf0298f82011-01-03 15:07:42 +00004300 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004301
4302 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4303
4304 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4305}
4306
hayeswang4804b3b2011-03-21 01:50:29 +00004307static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4308{
4309 rtl_csi_access_enable_1(ioaddr);
4310
4311 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4312
4313 RTL_W8(MaxTxPacketSize, TxPacketMax);
4314
4315 rtl_disable_clock_request(pdev);
4316}
4317
françois romieue6de30d2011-01-03 15:08:37 +00004318static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4319{
4320 static const struct ephy_info e_info_8168d_4[] = {
4321 { 0x0b, ~0, 0x48 },
4322 { 0x19, 0x20, 0x50 },
4323 { 0x0c, ~0, 0x20 }
4324 };
4325 int i;
4326
4327 rtl_csi_access_enable_1(ioaddr);
4328
4329 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4330
4331 RTL_W8(MaxTxPacketSize, TxPacketMax);
4332
4333 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4334 const struct ephy_info *e = e_info_8168d_4 + i;
4335 u16 w;
4336
4337 w = rtl_ephy_read(ioaddr, e->offset);
4338 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4339 }
4340
4341 rtl_enable_clock_request(pdev);
4342}
4343
Hayes Wang70090422011-07-06 15:58:06 +08004344static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004345{
Hayes Wang70090422011-07-06 15:58:06 +08004346 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004347 { 0x00, 0x0200, 0x0100 },
4348 { 0x00, 0x0000, 0x0004 },
4349 { 0x06, 0x0002, 0x0001 },
4350 { 0x06, 0x0000, 0x0030 },
4351 { 0x07, 0x0000, 0x2000 },
4352 { 0x00, 0x0000, 0x0020 },
4353 { 0x03, 0x5800, 0x2000 },
4354 { 0x03, 0x0000, 0x0001 },
4355 { 0x01, 0x0800, 0x1000 },
4356 { 0x07, 0x0000, 0x4000 },
4357 { 0x1e, 0x0000, 0x2000 },
4358 { 0x19, 0xffff, 0xfe6c },
4359 { 0x0a, 0x0000, 0x0040 }
4360 };
4361
4362 rtl_csi_access_enable_2(ioaddr);
4363
Hayes Wang70090422011-07-06 15:58:06 +08004364 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004365
4366 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4367
4368 RTL_W8(MaxTxPacketSize, TxPacketMax);
4369
4370 rtl_disable_clock_request(pdev);
4371
4372 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004373 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4374 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004375
Francois Romieucecb5fd2011-04-01 10:21:07 +02004376 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004377}
4378
Hayes Wang70090422011-07-06 15:58:06 +08004379static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4380{
4381 static const struct ephy_info e_info_8168e_2[] = {
4382 { 0x09, 0x0000, 0x0080 },
4383 { 0x19, 0x0000, 0x0224 }
4384 };
4385
4386 rtl_csi_access_enable_1(ioaddr);
4387
4388 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4389
4390 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4391
4392 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4393 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4394 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4395 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4396 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4397 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4398 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4399 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4400 ERIAR_EXGMAC);
4401
4402 RTL_W8(MaxTxPacketSize, 0x27);
4403
4404 rtl_disable_clock_request(pdev);
4405
4406 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4407 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4408
4409 /* Adjust EEE LED frequency */
4410 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4411
4412 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4413 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4414 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4415}
4416
Francois Romieu07ce4062007-02-23 23:36:39 +01004417static void rtl_hw_start_8168(struct net_device *dev)
4418{
Francois Romieu2dd99532007-06-11 23:22:52 +02004419 struct rtl8169_private *tp = netdev_priv(dev);
4420 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004421 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004422
4423 RTL_W8(Cfg9346, Cfg9346_Unlock);
4424
françois romieuf0298f82011-01-03 15:07:42 +00004425 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004426
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004427 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004428
Francois Romieu0e485152007-02-20 00:00:26 +01004429 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004430
4431 RTL_W16(CPlusCmd, tp->cp_cmd);
4432
Francois Romieu0e485152007-02-20 00:00:26 +01004433 RTL_W16(IntrMitigate, 0x5151);
4434
4435 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004436 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4437 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004438 tp->intr_event |= RxFIFOOver | PCSTimeout;
4439 tp->intr_event &= ~RxOverflow;
4440 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004441
4442 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4443
Francois Romieub8363902008-06-01 12:31:57 +02004444 rtl_set_rx_mode(dev);
4445
4446 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4447 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004448
4449 RTL_R8(IntrMask);
4450
Francois Romieu219a1e92008-06-28 11:58:39 +02004451 switch (tp->mac_version) {
4452 case RTL_GIGA_MAC_VER_11:
4453 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004454 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004455
4456 case RTL_GIGA_MAC_VER_12:
4457 case RTL_GIGA_MAC_VER_17:
4458 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004459 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004460
4461 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004462 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004463 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004464
4465 case RTL_GIGA_MAC_VER_19:
4466 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004467 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004468
4469 case RTL_GIGA_MAC_VER_20:
4470 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004471 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004472
Francois Romieu197ff762008-06-28 13:16:02 +02004473 case RTL_GIGA_MAC_VER_21:
4474 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004475 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004476
Francois Romieu6fb07052008-06-29 11:54:28 +02004477 case RTL_GIGA_MAC_VER_22:
4478 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004479 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004480
Francois Romieuef3386f2008-06-29 12:24:30 +02004481 case RTL_GIGA_MAC_VER_23:
4482 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004483 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004484
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004485 case RTL_GIGA_MAC_VER_24:
4486 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004487 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004488
Francois Romieu5b538df2008-07-20 16:22:45 +02004489 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004490 case RTL_GIGA_MAC_VER_26:
4491 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004492 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004493 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004494
françois romieue6de30d2011-01-03 15:08:37 +00004495 case RTL_GIGA_MAC_VER_28:
4496 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004497 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004498
hayeswang4804b3b2011-03-21 01:50:29 +00004499 case RTL_GIGA_MAC_VER_31:
4500 rtl_hw_start_8168dp(ioaddr, pdev);
4501 break;
4502
hayeswang01dc7fe2011-03-21 01:50:28 +00004503 case RTL_GIGA_MAC_VER_32:
4504 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004505 rtl_hw_start_8168e_1(ioaddr, pdev);
4506 break;
4507 case RTL_GIGA_MAC_VER_34:
4508 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004509 break;
françois romieue6de30d2011-01-03 15:08:37 +00004510
Francois Romieu219a1e92008-06-28 11:58:39 +02004511 default:
4512 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4513 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004514 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004515 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004516
Francois Romieu0e485152007-02-20 00:00:26 +01004517 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4518
Francois Romieub8363902008-06-01 12:31:57 +02004519 RTL_W8(Cfg9346, Cfg9346_Lock);
4520
Francois Romieu2dd99532007-06-11 23:22:52 +02004521 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004522
Francois Romieu0e485152007-02-20 00:00:26 +01004523 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
Francois Romieu2857ffb2008-08-02 21:08:49 +02004526#define R810X_CPCMD_QUIRK_MASK (\
4527 EnableBist | \
4528 Mac_dbgo_oe | \
4529 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004530 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004531 Force_txflow_en | \
4532 Cxpl_dbg_sel | \
4533 ASF | \
4534 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004535 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004536
4537static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4538{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004539 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004540 { 0x01, 0, 0x6e65 },
4541 { 0x02, 0, 0x091f },
4542 { 0x03, 0, 0xc2f9 },
4543 { 0x06, 0, 0xafb5 },
4544 { 0x07, 0, 0x0e00 },
4545 { 0x19, 0, 0xec80 },
4546 { 0x01, 0, 0x2e65 },
4547 { 0x01, 0, 0x6e65 }
4548 };
4549 u8 cfg1;
4550
françois romieu650e8d52011-01-03 15:08:29 +00004551 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004552
4553 RTL_W8(DBG_REG, FIX_NAK_1);
4554
4555 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4556
4557 RTL_W8(Config1,
4558 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4559 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4560
4561 cfg1 = RTL_R8(Config1);
4562 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4563 RTL_W8(Config1, cfg1 & ~LEDS0);
4564
Francois Romieu2857ffb2008-08-02 21:08:49 +02004565 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4566}
4567
4568static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4569{
françois romieu650e8d52011-01-03 15:08:29 +00004570 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004571
4572 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4573
4574 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4575 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004576}
4577
4578static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4579{
4580 rtl_hw_start_8102e_2(ioaddr, pdev);
4581
4582 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4583}
4584
Hayes Wang5a5e4442011-02-22 17:26:21 +08004585static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4586{
4587 static const struct ephy_info e_info_8105e_1[] = {
4588 { 0x07, 0, 0x4000 },
4589 { 0x19, 0, 0x0200 },
4590 { 0x19, 0, 0x0020 },
4591 { 0x1e, 0, 0x2000 },
4592 { 0x03, 0, 0x0001 },
4593 { 0x19, 0, 0x0100 },
4594 { 0x19, 0, 0x0004 },
4595 { 0x0a, 0, 0x0020 }
4596 };
4597
Francois Romieucecb5fd2011-04-01 10:21:07 +02004598 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004599 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4600
Francois Romieucecb5fd2011-04-01 10:21:07 +02004601 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004602 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4603
4604 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004605 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004606
4607 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4608}
4609
4610static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4611{
4612 rtl_hw_start_8105e_1(ioaddr, pdev);
4613 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4614}
4615
Francois Romieu07ce4062007-02-23 23:36:39 +01004616static void rtl_hw_start_8101(struct net_device *dev)
4617{
Francois Romieucdf1a602007-06-11 23:29:50 +02004618 struct rtl8169_private *tp = netdev_priv(dev);
4619 void __iomem *ioaddr = tp->mmio_addr;
4620 struct pci_dev *pdev = tp->pci_dev;
4621
Francois Romieucecb5fd2011-04-01 10:21:07 +02004622 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4623 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004624 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004625
4626 if (cap) {
4627 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4628 PCI_EXP_DEVCTL_NOSNOOP_EN);
4629 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004630 }
4631
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004632 RTL_W8(Cfg9346, Cfg9346_Unlock);
4633
Francois Romieu2857ffb2008-08-02 21:08:49 +02004634 switch (tp->mac_version) {
4635 case RTL_GIGA_MAC_VER_07:
4636 rtl_hw_start_8102e_1(ioaddr, pdev);
4637 break;
4638
4639 case RTL_GIGA_MAC_VER_08:
4640 rtl_hw_start_8102e_3(ioaddr, pdev);
4641 break;
4642
4643 case RTL_GIGA_MAC_VER_09:
4644 rtl_hw_start_8102e_2(ioaddr, pdev);
4645 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004646
4647 case RTL_GIGA_MAC_VER_29:
4648 rtl_hw_start_8105e_1(ioaddr, pdev);
4649 break;
4650 case RTL_GIGA_MAC_VER_30:
4651 rtl_hw_start_8105e_2(ioaddr, pdev);
4652 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004653 }
4654
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004655 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004656
françois romieuf0298f82011-01-03 15:07:42 +00004657 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004658
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004659 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004660
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004661 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004662 RTL_W16(CPlusCmd, tp->cp_cmd);
4663
4664 RTL_W16(IntrMitigate, 0x0000);
4665
4666 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4667
4668 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4669 rtl_set_rx_tx_config_registers(tp);
4670
Francois Romieucdf1a602007-06-11 23:29:50 +02004671 RTL_R8(IntrMask);
4672
Francois Romieucdf1a602007-06-11 23:29:50 +02004673 rtl_set_rx_mode(dev);
4674
4675 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004676
Francois Romieu0e485152007-02-20 00:00:26 +01004677 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678}
4679
4680static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4683 return -EINVAL;
4684
4685 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004686 netdev_update_features(dev);
4687
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689}
4690
4691static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4692{
Al Viro95e09182007-12-22 18:55:39 +00004693 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4695}
4696
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004697static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4698 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004700 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004701 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004702
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004703 kfree(*data_buff);
4704 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 rtl8169_make_unusable_by_asic(desc);
4706}
4707
4708static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4709{
4710 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4711
4712 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4713}
4714
4715static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4716 u32 rx_buf_sz)
4717{
4718 desc->addr = cpu_to_le64(mapping);
4719 wmb();
4720 rtl8169_mark_to_asic(desc, rx_buf_sz);
4721}
4722
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004723static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004725 return (void *)ALIGN((long)data, 16);
4726}
4727
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004728static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4729 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004730{
4731 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004733 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004734 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004735 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004737 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4738 if (!data)
4739 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004740
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004741 if (rtl8169_align(data) != data) {
4742 kfree(data);
4743 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4744 if (!data)
4745 return NULL;
4746 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004747
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004748 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004749 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004750 if (unlikely(dma_mapping_error(d, mapping))) {
4751 if (net_ratelimit())
4752 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004753 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
4756 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004757 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004758
4759err_out:
4760 kfree(data);
4761 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
4764static void rtl8169_rx_clear(struct rtl8169_private *tp)
4765{
Francois Romieu07d3f512007-02-21 22:40:46 +01004766 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
4768 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004769 if (tp->Rx_databuff[i]) {
4770 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 tp->RxDescArray + i);
4772 }
4773 }
4774}
4775
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004776static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004778 desc->opts1 |= cpu_to_le32(RingEnd);
4779}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004780
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004781static int rtl8169_rx_fill(struct rtl8169_private *tp)
4782{
4783 unsigned int i;
4784
4785 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004786 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004787
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004788 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004790
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004791 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004792 if (!data) {
4793 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004794 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004795 }
4796 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004799 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4800 return 0;
4801
4802err_out:
4803 rtl8169_rx_clear(tp);
4804 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807static int rtl8169_init_ring(struct net_device *dev)
4808{
4809 struct rtl8169_private *tp = netdev_priv(dev);
4810
4811 rtl8169_init_ring_indexes(tp);
4812
4813 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004814 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004816 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817}
4818
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004819static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 struct TxDesc *desc)
4821{
4822 unsigned int len = tx_skb->len;
4823
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004824 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4825
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 desc->opts1 = 0x00;
4827 desc->opts2 = 0x00;
4828 desc->addr = 0x00;
4829 tx_skb->len = 0;
4830}
4831
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004832static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4833 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
4835 unsigned int i;
4836
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004837 for (i = 0; i < n; i++) {
4838 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 struct ring_info *tx_skb = tp->tx_skb + entry;
4840 unsigned int len = tx_skb->len;
4841
4842 if (len) {
4843 struct sk_buff *skb = tx_skb->skb;
4844
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004845 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 tp->TxDescArray + entry);
4847 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004848 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 dev_kfree_skb(skb);
4850 tx_skb->skb = NULL;
4851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 }
4853 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004854}
4855
4856static void rtl8169_tx_clear(struct rtl8169_private *tp)
4857{
4858 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 tp->cur_tx = tp->dirty_tx = 0;
4860}
4861
David Howellsc4028952006-11-22 14:57:56 +00004862static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863{
4864 struct rtl8169_private *tp = netdev_priv(dev);
4865
David Howellsc4028952006-11-22 14:57:56 +00004866 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 schedule_delayed_work(&tp->task, 4);
4868}
4869
4870static void rtl8169_wait_for_quiescence(struct net_device *dev)
4871{
4872 struct rtl8169_private *tp = netdev_priv(dev);
4873 void __iomem *ioaddr = tp->mmio_addr;
4874
4875 synchronize_irq(dev->irq);
4876
4877 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004878 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
4880 rtl8169_irq_mask_and_ack(ioaddr);
4881
David S. Millerd1d08d12008-01-07 20:53:33 -08004882 tp->intr_mask = 0xffff;
4883 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004884 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885}
4886
David Howellsc4028952006-11-22 14:57:56 +00004887static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888{
David Howellsc4028952006-11-22 14:57:56 +00004889 struct rtl8169_private *tp =
4890 container_of(work, struct rtl8169_private, task.work);
4891 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 int ret;
4893
Francois Romieueb2a0212007-02-15 23:37:21 +01004894 rtnl_lock();
4895
4896 if (!netif_running(dev))
4897 goto out_unlock;
4898
4899 rtl8169_wait_for_quiescence(dev);
4900 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 ret = rtl8169_open(dev);
4903 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004904 if (net_ratelimit())
4905 netif_err(tp, drv, dev,
4906 "reinit failure (status = %d). Rescheduling\n",
4907 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4909 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004910
4911out_unlock:
4912 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913}
4914
David Howellsc4028952006-11-22 14:57:56 +00004915static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916{
David Howellsc4028952006-11-22 14:57:56 +00004917 struct rtl8169_private *tp =
4918 container_of(work, struct rtl8169_private, task.work);
4919 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004920 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Francois Romieueb2a0212007-02-15 23:37:21 +01004922 rtnl_lock();
4923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004925 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 rtl8169_wait_for_quiescence(dev);
4928
Francois Romieu56de4142011-03-15 17:29:31 +01004929 for (i = 0; i < NUM_RX_DESC; i++)
4930 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4931
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 rtl8169_tx_clear(tp);
4933
Hayes Wang92fc43b2011-07-06 15:58:03 +08004934 rtl8169_hw_reset(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01004935 rtl_hw_start(dev);
4936 netif_wake_queue(dev);
4937 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004938
4939out_unlock:
4940 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941}
4942
4943static void rtl8169_tx_timeout(struct net_device *dev)
4944{
4945 struct rtl8169_private *tp = netdev_priv(dev);
4946
françois romieue6de30d2011-01-03 15:08:37 +00004947 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949 /* Let's wait a bit while any (async) irq lands on */
4950 rtl8169_schedule_work(dev, rtl8169_reset_task);
4951}
4952
4953static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004954 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
4956 struct skb_shared_info *info = skb_shinfo(skb);
4957 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004958 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004959 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 entry = tp->cur_tx;
4962 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4963 skb_frag_t *frag = info->frags + cur_frag;
4964 dma_addr_t mapping;
4965 u32 status, len;
4966 void *addr;
4967
4968 entry = (entry + 1) % NUM_TX_DESC;
4969
4970 txd = tp->TxDescArray + entry;
4971 len = frag->size;
4972 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004973 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004974 if (unlikely(dma_mapping_error(d, mapping))) {
4975 if (net_ratelimit())
4976 netif_err(tp, drv, tp->dev,
4977 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004978 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
Francois Romieucecb5fd2011-04-01 10:21:07 +02004981 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004982 status = opts[0] | len |
4983 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
4985 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004986 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 txd->addr = cpu_to_le64(mapping);
4988
4989 tp->tx_skb[entry].len = len;
4990 }
4991
4992 if (cur_frag) {
4993 tp->tx_skb[entry].skb = skb;
4994 txd->opts1 |= cpu_to_le32(LastFrag);
4995 }
4996
4997 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004998
4999err_out:
5000 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5001 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002}
5003
Francois Romieu2b7b4312011-04-18 22:53:24 -07005004static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5005 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005007 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005008 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005009 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
Francois Romieu2b7b4312011-04-18 22:53:24 -07005011 if (mss) {
5012 opts[0] |= TD_LSO;
5013 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5014 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005015 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
5017 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005018 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005020 opts[offset] |= info->checksum.udp;
5021 else
5022 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024}
5025
Stephen Hemminger613573252009-08-31 19:50:58 +00005026static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5027 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
5029 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005030 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 struct TxDesc *txd = tp->TxDescArray + entry;
5032 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005033 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 dma_addr_t mapping;
5035 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005036 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005037 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005040 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005041 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 }
5043
5044 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005045 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005047 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005048 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005049 if (unlikely(dma_mapping_error(d, mapping))) {
5050 if (net_ratelimit())
5051 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005052 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
5055 tp->tx_skb[entry].len = len;
5056 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
Francois Romieu2b7b4312011-04-18 22:53:24 -07005058 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5059 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005060
Francois Romieu2b7b4312011-04-18 22:53:24 -07005061 rtl8169_tso_csum(tp, skb, opts);
5062
5063 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005064 if (frags < 0)
5065 goto err_dma_1;
5066 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005067 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005068 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005069 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005070 tp->tx_skb[entry].skb = skb;
5071 }
5072
Francois Romieu2b7b4312011-04-18 22:53:24 -07005073 txd->opts2 = cpu_to_le32(opts[1]);
5074
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 wmb();
5076
Francois Romieucecb5fd2011-04-01 10:21:07 +02005077 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005078 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 txd->opts1 = cpu_to_le32(status);
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 tp->cur_tx += frags + 1;
5082
David Dillow4c020a92010-03-03 16:33:10 +00005083 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Francois Romieucecb5fd2011-04-01 10:21:07 +02005085 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
5087 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5088 netif_stop_queue(dev);
5089 smp_rmb();
5090 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5091 netif_wake_queue(dev);
5092 }
5093
Stephen Hemminger613573252009-08-31 19:50:58 +00005094 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005096err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005097 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005098err_dma_0:
5099 dev_kfree_skb(skb);
5100 dev->stats.tx_dropped++;
5101 return NETDEV_TX_OK;
5102
5103err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005105 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005106 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107}
5108
5109static void rtl8169_pcierr_interrupt(struct net_device *dev)
5110{
5111 struct rtl8169_private *tp = netdev_priv(dev);
5112 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 u16 pci_status, pci_cmd;
5114
5115 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5116 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5117
Joe Perchesbf82c182010-02-09 11:49:50 +00005118 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5119 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
5121 /*
5122 * The recovery sequence below admits a very elaborated explanation:
5123 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005124 * - I did not see what else could be done;
5125 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 *
5127 * Feel free to adjust to your needs.
5128 */
Francois Romieua27993f2006-12-18 00:04:19 +01005129 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005130 pci_cmd &= ~PCI_COMMAND_PARITY;
5131 else
5132 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5133
5134 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
5136 pci_write_config_word(pdev, PCI_STATUS,
5137 pci_status & (PCI_STATUS_DETECTED_PARITY |
5138 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5139 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5140
5141 /* The infamous DAC f*ckup only happens at boot time */
5142 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005143 void __iomem *ioaddr = tp->mmio_addr;
5144
Joe Perchesbf82c182010-02-09 11:49:50 +00005145 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 tp->cp_cmd &= ~PCIDAC;
5147 RTL_W16(CPlusCmd, tp->cp_cmd);
5148 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 }
5150
françois romieue6de30d2011-01-03 15:08:37 +00005151 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005152
5153 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154}
5155
Francois Romieu07d3f512007-02-21 22:40:46 +01005156static void rtl8169_tx_interrupt(struct net_device *dev,
5157 struct rtl8169_private *tp,
5158 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159{
5160 unsigned int dirty_tx, tx_left;
5161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 dirty_tx = tp->dirty_tx;
5163 smp_rmb();
5164 tx_left = tp->cur_tx - dirty_tx;
5165
5166 while (tx_left > 0) {
5167 unsigned int entry = dirty_tx % NUM_TX_DESC;
5168 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 u32 status;
5170
5171 rmb();
5172 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5173 if (status & DescOwn)
5174 break;
5175
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005176 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5177 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005179 dev->stats.tx_packets++;
5180 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005181 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 tx_skb->skb = NULL;
5183 }
5184 dirty_tx++;
5185 tx_left--;
5186 }
5187
5188 if (tp->dirty_tx != dirty_tx) {
5189 tp->dirty_tx = dirty_tx;
5190 smp_wmb();
5191 if (netif_queue_stopped(dev) &&
5192 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5193 netif_wake_queue(dev);
5194 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005195 /*
5196 * 8168 hack: TxPoll requests are lost when the Tx packets are
5197 * too close. Let's kick an extra TxPoll request when a burst
5198 * of start_xmit activity is detected (if it is not detected,
5199 * it is slow enough). -- FR
5200 */
5201 smp_rmb();
5202 if (tp->cur_tx != dirty_tx)
5203 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 }
5205}
5206
Francois Romieu126fa4b2005-05-12 20:09:17 -04005207static inline int rtl8169_fragmented_frame(u32 status)
5208{
5209 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5210}
5211
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005212static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 u32 status = opts1 & RxProtoMask;
5215
5216 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005217 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 skb->ip_summed = CHECKSUM_UNNECESSARY;
5219 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005220 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221}
5222
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005223static struct sk_buff *rtl8169_try_rx_copy(void *data,
5224 struct rtl8169_private *tp,
5225 int pkt_size,
5226 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005228 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005229 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005231 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005232 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005233 prefetch(data);
5234 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5235 if (skb)
5236 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005237 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5238
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005239 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240}
5241
Francois Romieu07d3f512007-02-21 22:40:46 +01005242static int rtl8169_rx_interrupt(struct net_device *dev,
5243 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005244 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245{
5246 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005247 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 cur_rx = tp->cur_rx;
5250 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005251 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005253 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005255 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 u32 status;
5257
5258 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04005259 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
5261 if (status & DescOwn)
5262 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005263 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005264 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5265 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005266 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005268 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005270 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005271 if (status & RxFOVF) {
5272 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005273 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005274 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005275 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005277 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005278 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280
Francois Romieu126fa4b2005-05-12 20:09:17 -04005281 /*
5282 * The driver does not support incoming fragmented
5283 * frames. They are seen as a symptom of over-mtu
5284 * sized frames.
5285 */
5286 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005287 dev->stats.rx_dropped++;
5288 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005289 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005290 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005291 }
5292
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005293 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5294 tp, pkt_size, addr);
5295 rtl8169_mark_to_asic(desc, rx_buf_sz);
5296 if (!skb) {
5297 dev->stats.rx_dropped++;
5298 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 }
5300
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005301 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 skb_put(skb, pkt_size);
5303 skb->protocol = eth_type_trans(skb, dev);
5304
Francois Romieu7a8fc772011-03-01 17:18:33 +01005305 rtl8169_rx_vlan_tag(desc, skb);
5306
Francois Romieu56de4142011-03-15 17:29:31 +01005307 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308
Francois Romieucebf8cc2007-10-18 12:06:54 +02005309 dev->stats.rx_bytes += pkt_size;
5310 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005312
5313 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005314 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005315 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5316 desc->opts2 = 0;
5317 cur_rx++;
5318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 }
5320
5321 count = cur_rx - tp->cur_rx;
5322 tp->cur_rx = cur_rx;
5323
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005324 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
5326 return count;
5327}
5328
Francois Romieu07d3f512007-02-21 22:40:46 +01005329static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330{
Francois Romieu07d3f512007-02-21 22:40:46 +01005331 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005335 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336
David Dillowf11a3772009-05-22 15:29:34 +00005337 /* loop handling interrupts until we have no new ones or
5338 * we hit a invalid/hotplug case.
5339 */
Francois Romieu865c6522008-05-11 14:51:00 +02005340 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005341 while (status && status != 0xffff) {
5342 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
David Dillowf11a3772009-05-22 15:29:34 +00005344 /* Handle all of the error cases first. These will reset
5345 * the chip, so just exit the loop.
5346 */
5347 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005348 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 }
David Dillowf11a3772009-05-22 15:29:34 +00005351
Francois Romieu1519e572011-02-03 12:02:36 +01005352 if (unlikely(status & RxFIFOOver)) {
5353 switch (tp->mac_version) {
5354 /* Work around for rx fifo overflow */
5355 case RTL_GIGA_MAC_VER_11:
5356 case RTL_GIGA_MAC_VER_22:
5357 case RTL_GIGA_MAC_VER_26:
5358 netif_stop_queue(dev);
5359 rtl8169_tx_timeout(dev);
5360 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005361 /* Testers needed. */
5362 case RTL_GIGA_MAC_VER_17:
5363 case RTL_GIGA_MAC_VER_19:
5364 case RTL_GIGA_MAC_VER_20:
5365 case RTL_GIGA_MAC_VER_21:
5366 case RTL_GIGA_MAC_VER_23:
5367 case RTL_GIGA_MAC_VER_24:
5368 case RTL_GIGA_MAC_VER_27:
5369 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005370 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005371 /* Experimental science. Pktgen proof. */
5372 case RTL_GIGA_MAC_VER_12:
5373 case RTL_GIGA_MAC_VER_25:
5374 if (status == RxFIFOOver)
5375 goto done;
5376 break;
5377 default:
5378 break;
5379 }
David Dillowf11a3772009-05-22 15:29:34 +00005380 }
5381
5382 if (unlikely(status & SYSErr)) {
5383 rtl8169_pcierr_interrupt(dev);
5384 break;
5385 }
5386
5387 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005388 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005389
5390 /* We need to see the lastest version of tp->intr_mask to
5391 * avoid ignoring an MSI interrupt and having to wait for
5392 * another event which may never come.
5393 */
5394 smp_rmb();
5395 if (status & tp->intr_mask & tp->napi_event) {
5396 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5397 tp->intr_mask = ~tp->napi_event;
5398
5399 if (likely(napi_schedule_prep(&tp->napi)))
5400 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005401 else
5402 netif_info(tp, intr, dev,
5403 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005404 }
5405
5406 /* We only get a new MSI interrupt when all active irq
5407 * sources on the chip have been acknowledged. So, ack
5408 * everything we've seen and check if new sources have become
5409 * active to avoid blocking all interrupts from the chip.
5410 */
5411 RTL_W16(IntrStatus,
5412 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5413 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 }
Francois Romieu1519e572011-02-03 12:02:36 +01005415done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 return IRQ_RETVAL(handled);
5417}
5418
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005419static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005421 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5422 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005424 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005426 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 rtl8169_tx_interrupt(dev, tp, ioaddr);
5428
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005429 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005430 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005431
5432 /* We need for force the visibility of tp->intr_mask
5433 * for other CPUs, as we can loose an MSI interrupt
5434 * and potentially wait for a retransmit timeout if we don't.
5435 * The posted write to IntrMask is safe, as it will
5436 * eventually make it to the chip and we won't loose anything
5437 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 */
David Dillowf11a3772009-05-22 15:29:34 +00005439 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005440 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005441 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 }
5443
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005444 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Francois Romieu523a6092008-09-10 22:28:56 +02005447static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5448{
5449 struct rtl8169_private *tp = netdev_priv(dev);
5450
5451 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5452 return;
5453
5454 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5455 RTL_W32(RxMissed, 0);
5456}
5457
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458static void rtl8169_down(struct net_device *dev)
5459{
5460 struct rtl8169_private *tp = netdev_priv(dev);
5461 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Francois Romieu4876cc12011-03-11 21:07:11 +01005463 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
5465 netif_stop_queue(dev);
5466
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005467 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 spin_lock_irq(&tp->lock);
5470
Hayes Wang92fc43b2011-07-06 15:58:03 +08005471 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005472 /*
5473 * At this point device interrupts can not be enabled in any function,
5474 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5475 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5476 */
Francois Romieu523a6092008-09-10 22:28:56 +02005477 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
5479 spin_unlock_irq(&tp->lock);
5480
5481 synchronize_irq(dev->irq);
5482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005484 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 rtl8169_tx_clear(tp);
5487
5488 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005489
5490 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491}
5492
5493static int rtl8169_close(struct net_device *dev)
5494{
5495 struct rtl8169_private *tp = netdev_priv(dev);
5496 struct pci_dev *pdev = tp->pci_dev;
5497
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005498 pm_runtime_get_sync(&pdev->dev);
5499
Francois Romieucecb5fd2011-04-01 10:21:07 +02005500 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005501 rtl8169_update_counters(dev);
5502
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 rtl8169_down(dev);
5504
5505 free_irq(dev->irq, dev);
5506
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005507 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5508 tp->RxPhyAddr);
5509 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5510 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 tp->TxDescArray = NULL;
5512 tp->RxDescArray = NULL;
5513
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005514 pm_runtime_put_sync(&pdev->dev);
5515
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 return 0;
5517}
5518
Francois Romieu07ce4062007-02-23 23:36:39 +01005519static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520{
5521 struct rtl8169_private *tp = netdev_priv(dev);
5522 void __iomem *ioaddr = tp->mmio_addr;
5523 unsigned long flags;
5524 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005525 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 u32 tmp = 0;
5527
5528 if (dev->flags & IFF_PROMISC) {
5529 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005530 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 rx_mode =
5532 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5533 AcceptAllPhys;
5534 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005535 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005536 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 /* Too many to filter perfectly -- accept all multicasts. */
5538 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5539 mc_filter[1] = mc_filter[0] = 0xffffffff;
5540 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005541 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005542
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 rx_mode = AcceptBroadcast | AcceptMyPhys;
5544 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005545 netdev_for_each_mc_addr(ha, dev) {
5546 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5548 rx_mode |= AcceptMulticast;
5549 }
5550 }
5551
5552 spin_lock_irqsave(&tp->lock, flags);
5553
5554 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005555 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
Francois Romieuf887cce2008-07-17 22:24:18 +02005557 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005558 u32 data = mc_filter[0];
5559
5560 mc_filter[0] = swab32(mc_filter[1]);
5561 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005562 }
5563
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005565 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Francois Romieu57a9f232007-06-04 22:10:15 +02005567 RTL_W32(RxConfig, tmp);
5568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 spin_unlock_irqrestore(&tp->lock, flags);
5570}
5571
5572/**
5573 * rtl8169_get_stats - Get rtl8169 read/write statistics
5574 * @dev: The Ethernet Device to get statistics for
5575 *
5576 * Get TX/RX statistics for rtl8169
5577 */
5578static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5579{
5580 struct rtl8169_private *tp = netdev_priv(dev);
5581 void __iomem *ioaddr = tp->mmio_addr;
5582 unsigned long flags;
5583
5584 if (netif_running(dev)) {
5585 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005586 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 spin_unlock_irqrestore(&tp->lock, flags);
5588 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005589
Francois Romieucebf8cc2007-10-18 12:06:54 +02005590 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591}
5592
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005593static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005594{
françois romieu065c27c2011-01-03 15:08:12 +00005595 struct rtl8169_private *tp = netdev_priv(dev);
5596
Francois Romieu5d06a992006-02-23 00:47:58 +01005597 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005598 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005599
françois romieu065c27c2011-01-03 15:08:12 +00005600 rtl_pll_power_down(tp);
5601
Francois Romieu5d06a992006-02-23 00:47:58 +01005602 netif_device_detach(dev);
5603 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005604}
Francois Romieu5d06a992006-02-23 00:47:58 +01005605
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005606#ifdef CONFIG_PM
5607
5608static int rtl8169_suspend(struct device *device)
5609{
5610 struct pci_dev *pdev = to_pci_dev(device);
5611 struct net_device *dev = pci_get_drvdata(pdev);
5612
5613 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005614
Francois Romieu5d06a992006-02-23 00:47:58 +01005615 return 0;
5616}
5617
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005618static void __rtl8169_resume(struct net_device *dev)
5619{
françois romieu065c27c2011-01-03 15:08:12 +00005620 struct rtl8169_private *tp = netdev_priv(dev);
5621
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005622 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005623
5624 rtl_pll_power_up(tp);
5625
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005626 rtl8169_schedule_work(dev, rtl8169_reset_task);
5627}
5628
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005629static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005630{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005631 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005632 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005633 struct rtl8169_private *tp = netdev_priv(dev);
5634
5635 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005636
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005637 if (netif_running(dev))
5638 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005639
Francois Romieu5d06a992006-02-23 00:47:58 +01005640 return 0;
5641}
5642
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005643static int rtl8169_runtime_suspend(struct device *device)
5644{
5645 struct pci_dev *pdev = to_pci_dev(device);
5646 struct net_device *dev = pci_get_drvdata(pdev);
5647 struct rtl8169_private *tp = netdev_priv(dev);
5648
5649 if (!tp->TxDescArray)
5650 return 0;
5651
5652 spin_lock_irq(&tp->lock);
5653 tp->saved_wolopts = __rtl8169_get_wol(tp);
5654 __rtl8169_set_wol(tp, WAKE_ANY);
5655 spin_unlock_irq(&tp->lock);
5656
5657 rtl8169_net_suspend(dev);
5658
5659 return 0;
5660}
5661
5662static int rtl8169_runtime_resume(struct device *device)
5663{
5664 struct pci_dev *pdev = to_pci_dev(device);
5665 struct net_device *dev = pci_get_drvdata(pdev);
5666 struct rtl8169_private *tp = netdev_priv(dev);
5667
5668 if (!tp->TxDescArray)
5669 return 0;
5670
5671 spin_lock_irq(&tp->lock);
5672 __rtl8169_set_wol(tp, tp->saved_wolopts);
5673 tp->saved_wolopts = 0;
5674 spin_unlock_irq(&tp->lock);
5675
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005676 rtl8169_init_phy(dev, tp);
5677
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005678 __rtl8169_resume(dev);
5679
5680 return 0;
5681}
5682
5683static int rtl8169_runtime_idle(struct device *device)
5684{
5685 struct pci_dev *pdev = to_pci_dev(device);
5686 struct net_device *dev = pci_get_drvdata(pdev);
5687 struct rtl8169_private *tp = netdev_priv(dev);
5688
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005689 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005690}
5691
Alexey Dobriyan47145212009-12-14 18:00:08 -08005692static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005693 .suspend = rtl8169_suspend,
5694 .resume = rtl8169_resume,
5695 .freeze = rtl8169_suspend,
5696 .thaw = rtl8169_resume,
5697 .poweroff = rtl8169_suspend,
5698 .restore = rtl8169_resume,
5699 .runtime_suspend = rtl8169_runtime_suspend,
5700 .runtime_resume = rtl8169_runtime_resume,
5701 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005702};
5703
5704#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5705
5706#else /* !CONFIG_PM */
5707
5708#define RTL8169_PM_OPS NULL
5709
5710#endif /* !CONFIG_PM */
5711
Francois Romieu1765f952008-09-13 17:21:40 +02005712static void rtl_shutdown(struct pci_dev *pdev)
5713{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005714 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005715 struct rtl8169_private *tp = netdev_priv(dev);
5716 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005717
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005718 rtl8169_net_suspend(dev);
5719
Francois Romieucecb5fd2011-04-01 10:21:07 +02005720 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005721 rtl_rar_set(tp, dev->perm_addr);
5722
françois romieu4bb3f522009-06-17 11:41:45 +00005723 spin_lock_irq(&tp->lock);
5724
Hayes Wang92fc43b2011-07-06 15:58:03 +08005725 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005726
5727 spin_unlock_irq(&tp->lock);
5728
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005729 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005730 /* WoL fails with some 8168 when the receiver is disabled. */
5731 if (tp->features & RTL_FEATURE_WOL) {
5732 pci_clear_master(pdev);
5733
5734 RTL_W8(ChipCmd, CmdRxEnb);
5735 /* PCI commit */
5736 RTL_R8(ChipCmd);
5737 }
5738
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005739 pci_wake_from_d3(pdev, true);
5740 pci_set_power_state(pdev, PCI_D3hot);
5741 }
5742}
Francois Romieu5d06a992006-02-23 00:47:58 +01005743
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744static struct pci_driver rtl8169_pci_driver = {
5745 .name = MODULENAME,
5746 .id_table = rtl8169_pci_tbl,
5747 .probe = rtl8169_init_one,
5748 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005749 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005750 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751};
5752
Francois Romieu07d3f512007-02-21 22:40:46 +01005753static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754{
Jeff Garzik29917622006-08-19 17:48:59 -04005755 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756}
5757
Francois Romieu07d3f512007-02-21 22:40:46 +01005758static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759{
5760 pci_unregister_driver(&rtl8169_pci_driver);
5761}
5762
5763module_init(rtl8169_init_module);
5764module_exit(rtl8169_cleanup_module);