blob: 44b40ea1d44361ccdd5f6de0448c4eba9c5dba49 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang70090422011-07-06 15:58:06 +080044#define FIRMWARE_8168E_3 "rtl_nic/rtl8168e-3.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080045#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#ifdef RTL8169_DEBUG
48#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020049 if (!(expr)) { \
50 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070051 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020052 }
Joe Perches06fa7352007-10-18 21:15:00 +020053#define dprintk(fmt, args...) \
54 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#else
56#define assert(expr) do {} while (0)
57#define dprintk(fmt, args...) do {} while (0)
58#endif /* RTL8169_DEBUG */
59
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020060#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070061 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define TX_BUFFS_AVAIL(tp) \
64 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
67 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050068static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/* MAC address length */
71#define MAC_ADDR_LEN 6
72
Francois Romieu9c14cea2008-07-05 00:21:15 +020073#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
76#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
77
78#define R8169_REGS_SIZE 256
79#define R8169_NAPI_WEIGHT 64
80#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
81#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
82#define RX_BUF_SIZE 1536 /* Rx Buffer size */
83#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
84#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
85
86#define RTL8169_TX_TIMEOUT (6*HZ)
87#define RTL8169_PHY_TIMEOUT (10*HZ)
88
françois romieuea8dbdd2009-03-15 01:10:50 +000089#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
90#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020091#define RTL_EEPROM_SIG_ADDR 0x0000
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* write/read MMIO register */
94#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
95#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
96#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
97#define RTL_R8(reg) readb (ioaddr + (reg))
98#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000099#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200102 RTL_GIGA_MAC_VER_01 = 0,
103 RTL_GIGA_MAC_VER_02,
104 RTL_GIGA_MAC_VER_03,
105 RTL_GIGA_MAC_VER_04,
106 RTL_GIGA_MAC_VER_05,
107 RTL_GIGA_MAC_VER_06,
108 RTL_GIGA_MAC_VER_07,
109 RTL_GIGA_MAC_VER_08,
110 RTL_GIGA_MAC_VER_09,
111 RTL_GIGA_MAC_VER_10,
112 RTL_GIGA_MAC_VER_11,
113 RTL_GIGA_MAC_VER_12,
114 RTL_GIGA_MAC_VER_13,
115 RTL_GIGA_MAC_VER_14,
116 RTL_GIGA_MAC_VER_15,
117 RTL_GIGA_MAC_VER_16,
118 RTL_GIGA_MAC_VER_17,
119 RTL_GIGA_MAC_VER_18,
120 RTL_GIGA_MAC_VER_19,
121 RTL_GIGA_MAC_VER_20,
122 RTL_GIGA_MAC_VER_21,
123 RTL_GIGA_MAC_VER_22,
124 RTL_GIGA_MAC_VER_23,
125 RTL_GIGA_MAC_VER_24,
126 RTL_GIGA_MAC_VER_25,
127 RTL_GIGA_MAC_VER_26,
128 RTL_GIGA_MAC_VER_27,
129 RTL_GIGA_MAC_VER_28,
130 RTL_GIGA_MAC_VER_29,
131 RTL_GIGA_MAC_VER_30,
132 RTL_GIGA_MAC_VER_31,
133 RTL_GIGA_MAC_VER_32,
134 RTL_GIGA_MAC_VER_33,
Hayes Wang70090422011-07-06 15:58:06 +0800135 RTL_GIGA_MAC_VER_34,
Francois Romieu85bffe62011-04-27 08:22:39 +0200136 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Francois Romieu2b7b4312011-04-18 22:53:24 -0700139enum rtl_tx_desc_version {
140 RTL_TD_0 = 0,
141 RTL_TD_1 = 1,
142};
143
Francois Romieu85bffe62011-04-27 08:22:39 +0200144#define _R(NAME,TD,FW) \
145 { .name = NAME, .txd_version = TD, .fw_name = FW }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800147static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700149 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200150 const char *fw_name;
151} rtl_chip_infos[] = {
152 /* PCI devices. */
153 [RTL_GIGA_MAC_VER_01] =
154 _R("RTL8169", RTL_TD_0, NULL),
155 [RTL_GIGA_MAC_VER_02] =
156 _R("RTL8169s", RTL_TD_0, NULL),
157 [RTL_GIGA_MAC_VER_03] =
158 _R("RTL8110s", RTL_TD_0, NULL),
159 [RTL_GIGA_MAC_VER_04] =
160 _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
161 [RTL_GIGA_MAC_VER_05] =
162 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
163 [RTL_GIGA_MAC_VER_06] =
164 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
165 /* PCI-E devices. */
166 [RTL_GIGA_MAC_VER_07] =
167 _R("RTL8102e", RTL_TD_1, NULL),
168 [RTL_GIGA_MAC_VER_08] =
169 _R("RTL8102e", RTL_TD_1, NULL),
170 [RTL_GIGA_MAC_VER_09] =
171 _R("RTL8102e", RTL_TD_1, NULL),
172 [RTL_GIGA_MAC_VER_10] =
173 _R("RTL8101e", RTL_TD_0, NULL),
174 [RTL_GIGA_MAC_VER_11] =
175 _R("RTL8168b/8111b", RTL_TD_0, NULL),
176 [RTL_GIGA_MAC_VER_12] =
177 _R("RTL8168b/8111b", RTL_TD_0, NULL),
178 [RTL_GIGA_MAC_VER_13] =
179 _R("RTL8101e", RTL_TD_0, NULL),
180 [RTL_GIGA_MAC_VER_14] =
181 _R("RTL8100e", RTL_TD_0, NULL),
182 [RTL_GIGA_MAC_VER_15] =
183 _R("RTL8100e", RTL_TD_0, NULL),
184 [RTL_GIGA_MAC_VER_16] =
185 _R("RTL8101e", RTL_TD_0, NULL),
186 [RTL_GIGA_MAC_VER_17] =
187 _R("RTL8168b/8111b", RTL_TD_0, NULL),
188 [RTL_GIGA_MAC_VER_18] =
189 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
190 [RTL_GIGA_MAC_VER_19] =
191 _R("RTL8168c/8111c", RTL_TD_1, NULL),
192 [RTL_GIGA_MAC_VER_20] =
193 _R("RTL8168c/8111c", RTL_TD_1, NULL),
194 [RTL_GIGA_MAC_VER_21] =
195 _R("RTL8168c/8111c", RTL_TD_1, NULL),
196 [RTL_GIGA_MAC_VER_22] =
197 _R("RTL8168c/8111c", RTL_TD_1, NULL),
198 [RTL_GIGA_MAC_VER_23] =
199 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
200 [RTL_GIGA_MAC_VER_24] =
201 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
202 [RTL_GIGA_MAC_VER_25] =
203 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
204 [RTL_GIGA_MAC_VER_26] =
205 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
206 [RTL_GIGA_MAC_VER_27] =
207 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
208 [RTL_GIGA_MAC_VER_28] =
209 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
210 [RTL_GIGA_MAC_VER_29] =
211 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
212 [RTL_GIGA_MAC_VER_30] =
213 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
214 [RTL_GIGA_MAC_VER_31] =
215 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
216 [RTL_GIGA_MAC_VER_32] =
217 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
218 [RTL_GIGA_MAC_VER_33] =
Hayes Wang70090422011-07-06 15:58:06 +0800219 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2),
220 [RTL_GIGA_MAC_VER_34] =
221 _R("RTL8168evl/8111evl",RTL_TD_1, FIRMWARE_8168E_3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222};
223#undef _R
224
Francois Romieubcf0bf92006-07-26 23:14:13 +0200225enum cfg_version {
226 RTL_CFG_0 = 0x00,
227 RTL_CFG_1,
228 RTL_CFG_2
229};
230
Francois Romieu07ce4062007-02-23 23:36:39 +0100231static void rtl_hw_start_8169(struct net_device *);
232static void rtl_hw_start_8168(struct net_device *);
233static void rtl_hw_start_8101(struct net_device *);
234
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000235static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200236 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200237 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200238 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100239 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200240 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
241 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen93a3aa22011-07-28 13:18:11 +0000242 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200243 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200244 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
245 { PCI_VENDOR_ID_LINKSYS, 0x1032,
246 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100247 { 0x0001, 0x8168,
248 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 {0,},
250};
251
252MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
253
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000254static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700255static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200256static struct {
257 u32 msg_enable;
258} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Francois Romieu07d3f512007-02-21 22:40:46 +0100260enum rtl_registers {
261 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100262 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100263 MAR0 = 8, /* Multicast filter. */
264 CounterAddrLow = 0x10,
265 CounterAddrHigh = 0x14,
266 TxDescStartAddrLow = 0x20,
267 TxDescStartAddrHigh = 0x24,
268 TxHDescStartAddrLow = 0x28,
269 TxHDescStartAddrHigh = 0x2c,
270 FLASH = 0x30,
271 ERSR = 0x36,
272 ChipCmd = 0x37,
273 TxPoll = 0x38,
274 IntrMask = 0x3c,
275 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700276
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800277 TxConfig = 0x40,
278#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
279#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
280
281 RxConfig = 0x44,
282#define RX128_INT_EN (1 << 15) /* 8111c and later */
283#define RX_MULTI_EN (1 << 14) /* 8111c only */
284#define RXCFG_FIFO_SHIFT 13
285 /* No threshold before first PCI xfer */
286#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
287#define RXCFG_DMA_SHIFT 8
288 /* Unlimited maximum PCI burst. */
289#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700290
Francois Romieu07d3f512007-02-21 22:40:46 +0100291 RxMissed = 0x4c,
292 Cfg9346 = 0x50,
293 Config0 = 0x51,
294 Config1 = 0x52,
295 Config2 = 0x53,
296 Config3 = 0x54,
297 Config4 = 0x55,
298 Config5 = 0x56,
299 MultiIntr = 0x5c,
300 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100301 PHYstatus = 0x6c,
302 RxMaxSize = 0xda,
303 CPlusCmd = 0xe0,
304 IntrMitigate = 0xe2,
305 RxDescAddrLow = 0xe4,
306 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000307 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
308
309#define NoEarlyTx 0x3f /* Max value : no early transmit. */
310
311 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
312
313#define TxPacketMax (8064 >> 7)
Hayes Wang3090bd92011-09-06 16:55:15 +0800314#define EarlySize 0x27
françois romieuf0298f82011-01-03 15:07:42 +0000315
Francois Romieu07d3f512007-02-21 22:40:46 +0100316 FuncEvent = 0xf0,
317 FuncEventMask = 0xf4,
318 FuncPresetState = 0xf8,
319 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
Francois Romieuf162a5d2008-06-01 22:37:49 +0200322enum rtl8110_registers {
323 TBICSR = 0x64,
324 TBI_ANAR = 0x68,
325 TBI_LPAR = 0x6a,
326};
327
328enum rtl8168_8101_registers {
329 CSIDR = 0x64,
330 CSIAR = 0x68,
331#define CSIAR_FLAG 0x80000000
332#define CSIAR_WRITE_CMD 0x80000000
333#define CSIAR_BYTE_ENABLE 0x0f
334#define CSIAR_BYTE_ENABLE_SHIFT 12
335#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000336 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200337 EPHYAR = 0x80,
338#define EPHYAR_FLAG 0x80000000
339#define EPHYAR_WRITE_CMD 0x80000000
340#define EPHYAR_REG_MASK 0x1f
341#define EPHYAR_REG_SHIFT 16
342#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800343 DLLPR = 0xd0,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800344#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200345 DBG_REG = 0xd1,
346#define FIX_NAK_1 (1 << 4)
347#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800348 TWSI = 0xd2,
349 MCU = 0xd3,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800350#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800351#define EN_NDP (1 << 3)
352#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000353 EFUSEAR = 0xdc,
354#define EFUSEAR_FLAG 0x80000000
355#define EFUSEAR_WRITE_CMD 0x80000000
356#define EFUSEAR_READ_CMD 0x00000000
357#define EFUSEAR_REG_MASK 0x03ff
358#define EFUSEAR_REG_SHIFT 8
359#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200360};
361
françois romieuc0e45c12011-01-03 15:08:04 +0000362enum rtl8168_registers {
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800363 LED_FREQ = 0x1a,
364 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000365 ERIDR = 0x70,
366 ERIAR = 0x74,
367#define ERIAR_FLAG 0x80000000
368#define ERIAR_WRITE_CMD 0x80000000
369#define ERIAR_READ_CMD 0x00000000
370#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000371#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800372#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
373#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
374#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
375#define ERIAR_MASK_SHIFT 12
376#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
377#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
378#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000379 EPHY_RXER_NUM = 0x7c,
380 OCPDR = 0xb0, /* OCP GPHY access */
381#define OCPDR_WRITE_CMD 0x80000000
382#define OCPDR_READ_CMD 0x00000000
383#define OCPDR_REG_MASK 0x7f
384#define OCPDR_GPHY_REG_SHIFT 16
385#define OCPDR_DATA_MASK 0xffff
386 OCPAR = 0xb4,
387#define OCPAR_FLAG 0x80000000
388#define OCPAR_GPHY_WRITE_CMD 0x8000f060
389#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000390 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
391 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200392#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800393#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000394};
395
Francois Romieu07d3f512007-02-21 22:40:46 +0100396enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100398 SYSErr = 0x8000,
399 PCSTimeout = 0x4000,
400 SWInt = 0x0100,
401 TxDescUnavail = 0x0080,
402 RxFIFOOver = 0x0040,
403 LinkChg = 0x0020,
404 RxOverflow = 0x0010,
405 TxErr = 0x0008,
406 TxOK = 0x0004,
407 RxErr = 0x0002,
408 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* RxStatusDesc */
David S. Miller8decf862011-09-22 03:23:13 -0400411 RxBOVF = (1 << 24),
Francois Romieu9dccf612006-05-14 12:31:17 +0200412 RxFOVF = (1 << 23),
413 RxRWT = (1 << 22),
414 RxRES = (1 << 21),
415 RxRUNT = (1 << 20),
416 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* ChipCmdBits */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800419 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100420 CmdReset = 0x10,
421 CmdRxEnb = 0x08,
422 CmdTxEnb = 0x04,
423 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Francois Romieu275391a2007-02-23 23:50:28 +0100425 /* TXPoll register p.5 */
426 HPQ = 0x80, /* Poll cmd on the high prio queue */
427 NPQ = 0x40, /* Poll cmd on the low prio queue */
428 FSWInt = 0x01, /* Forced software interrupt */
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100431 Cfg9346_Lock = 0x00,
432 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100435 AcceptErr = 0x20,
436 AcceptRunt = 0x10,
437 AcceptBroadcast = 0x08,
438 AcceptMulticast = 0x04,
439 AcceptMyPhys = 0x02,
440 AcceptAllPhys = 0x01,
Francois Romieu1687b562011-07-19 17:21:29 +0200441#define RX_CONFIG_ACCEPT_MASK 0x3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* TxConfigBits */
444 TxInterFrameGapShift = 24,
445 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
446
Francois Romieu5d06a992006-02-23 00:47:58 +0100447 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200448 LEDS1 = (1 << 7),
449 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200450 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200451 Speed_down = (1 << 4),
452 MEMMAP = (1 << 3),
453 IOMAP = (1 << 2),
454 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100455 PMEnable = (1 << 0), /* Power Management Enable */
456
Francois Romieu6dccd162007-02-13 23:38:05 +0100457 /* Config2 register p. 25 */
458 PCI_Clock_66MHz = 0x01,
459 PCI_Clock_33MHz = 0x00,
460
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100461 /* Config3 register p.25 */
462 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
463 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200464 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100465
Francois Romieu5d06a992006-02-23 00:47:58 +0100466 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100467 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
468 MWF = (1 << 5), /* Accept Multicast wakeup frame */
469 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200470 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100471 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100472 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /* TBICSR p.28 */
475 TBIReset = 0x80000000,
476 TBILoopback = 0x40000000,
477 TBINwEnable = 0x20000000,
478 TBINwRestart = 0x10000000,
479 TBILinkOk = 0x02000000,
480 TBINwComplete = 0x01000000,
481
482 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200483 EnableBist = (1 << 15), // 8168 8101
484 Mac_dbgo_oe = (1 << 14), // 8168 8101
485 Normal_mode = (1 << 13), // unused
486 Force_half_dup = (1 << 12), // 8168 8101
487 Force_rxflow_en = (1 << 11), // 8168 8101
488 Force_txflow_en = (1 << 10), // 8168 8101
489 Cxpl_dbg_sel = (1 << 9), // 8168 8101
490 ASF = (1 << 8), // 8168 8101
491 PktCntrDisable = (1 << 7), // 8168 8101
492 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 RxVlan = (1 << 6),
494 RxChkSum = (1 << 5),
495 PCIDAC = (1 << 4),
496 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100497 INTT_0 = 0x0000, // 8168
498 INTT_1 = 0x0001, // 8168
499 INTT_2 = 0x0002, // 8168
500 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100503 TBI_Enable = 0x80,
504 TxFlowCtrl = 0x40,
505 RxFlowCtrl = 0x20,
506 _1000bpsF = 0x10,
507 _100bps = 0x08,
508 _10bps = 0x04,
509 LinkStatus = 0x02,
510 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100513 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200514
515 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100516 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517};
518
Francois Romieu2b7b4312011-04-18 22:53:24 -0700519enum rtl_desc_bit {
520 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
522 RingEnd = (1 << 30), /* End of descriptor ring */
523 FirstFrag = (1 << 29), /* First segment of a packet */
524 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700525};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Francois Romieu2b7b4312011-04-18 22:53:24 -0700527/* Generic case. */
528enum rtl_tx_desc_bit {
529 /* First doubleword. */
530 TD_LSO = (1 << 27), /* Large Send Offload */
531#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Francois Romieu2b7b4312011-04-18 22:53:24 -0700533 /* Second doubleword. */
534 TxVlanTag = (1 << 17), /* Add VLAN tag */
535};
536
537/* 8169, 8168b and 810x except 8102e. */
538enum rtl_tx_desc_bit_0 {
539 /* First doubleword. */
540#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
541 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
542 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
543 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
544};
545
546/* 8102e, 8168c and beyond. */
547enum rtl_tx_desc_bit_1 {
548 /* Second doubleword. */
549#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
550 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
551 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
552 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
553};
554
555static const struct rtl_tx_desc_info {
556 struct {
557 u32 udp;
558 u32 tcp;
559 } checksum;
560 u16 mss_shift;
561 u16 opts_offset;
562} tx_desc_info [] = {
563 [RTL_TD_0] = {
564 .checksum = {
565 .udp = TD0_IP_CS | TD0_UDP_CS,
566 .tcp = TD0_IP_CS | TD0_TCP_CS
567 },
568 .mss_shift = TD0_MSS_SHIFT,
569 .opts_offset = 0
570 },
571 [RTL_TD_1] = {
572 .checksum = {
573 .udp = TD1_IP_CS | TD1_UDP_CS,
574 .tcp = TD1_IP_CS | TD1_TCP_CS
575 },
576 .mss_shift = TD1_MSS_SHIFT,
577 .opts_offset = 1
578 }
579};
580
581enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Rx private */
583 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
584 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
585
586#define RxProtoUDP (PID1)
587#define RxProtoTCP (PID0)
588#define RxProtoIP (PID1 | PID0)
589#define RxProtoMask RxProtoIP
590
591 IPFail = (1 << 16), /* IP checksum failed */
592 UDPFail = (1 << 15), /* UDP/IP checksum failed */
593 TCPFail = (1 << 14), /* TCP/IP checksum failed */
594 RxVlanTag = (1 << 16), /* VLAN tag available */
595};
596
597#define RsvdMask 0x3fffc000
598
599struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200600 __le32 opts1;
601 __le32 opts2;
602 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603};
604
605struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200606 __le32 opts1;
607 __le32 opts2;
608 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609};
610
611struct ring_info {
612 struct sk_buff *skb;
613 u32 len;
614 u8 __pad[sizeof(void *) - sizeof(u32)];
615};
616
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200617enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200618 RTL_FEATURE_WOL = (1 << 0),
619 RTL_FEATURE_MSI = (1 << 1),
620 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200621};
622
Ivan Vecera355423d2009-02-06 21:49:57 -0800623struct rtl8169_counters {
624 __le64 tx_packets;
625 __le64 rx_packets;
626 __le64 tx_errors;
627 __le32 rx_errors;
628 __le16 rx_missed;
629 __le16 align_errors;
630 __le32 tx_one_collision;
631 __le32 tx_multi_collision;
632 __le64 rx_unicast;
633 __le64 rx_broadcast;
634 __le32 rx_multicast;
635 __le16 tx_aborted;
636 __le16 tx_underun;
637};
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639struct rtl8169_private {
640 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200641 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000642 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700643 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200644 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200645 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700646 u16 txd_version;
647 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
649 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
650 u32 dirty_rx;
651 u32 dirty_tx;
652 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
653 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
654 dma_addr_t TxPhyAddr;
655 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000656 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct timer_list timer;
659 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100660 u16 intr_event;
661 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000663
664 struct mdio_ops {
665 void (*write)(void __iomem *, int, int);
666 int (*read)(void __iomem *, int);
667 } mdio_ops;
668
françois romieu065c27c2011-01-03 15:08:12 +0000669 struct pll_power_ops {
670 void (*down)(struct rtl8169_private *);
671 void (*up)(struct rtl8169_private *);
672 } pll_power_ops;
673
Oliver Neukum54405cd2011-01-06 21:55:13 +0100674 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200675 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000676 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100677 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000678 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800680 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
David Howellsc4028952006-11-22 14:57:56 +0000681 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200682 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200683
684 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800685 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000686 u32 saved_wolopts;
David S. Miller8decf862011-09-22 03:23:13 -0400687 u32 opts1_mask;
françois romieuf1e02ed2011-01-13 13:07:53 +0000688
Francois Romieub6ffd972011-06-17 17:00:05 +0200689 struct rtl_fw {
690 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200691
692#define RTL_VER_SIZE 32
693
694 char version[RTL_VER_SIZE];
695
696 struct rtl_fw_phy_action {
697 __le32 *code;
698 size_t size;
699 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200700 } *rtl_fw;
Phil Carmody497888c2011-07-14 15:07:13 +0300701#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702};
703
Ralf Baechle979b6c12005-06-13 14:30:40 -0700704MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700707MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200708module_param_named(debug, debug.msg_enable, int, 0);
709MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710MODULE_LICENSE("GPL");
711MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000712MODULE_FIRMWARE(FIRMWARE_8168D_1);
713MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000714MODULE_FIRMWARE(FIRMWARE_8168E_1);
715MODULE_FIRMWARE(FIRMWARE_8168E_2);
David S. Miller8decf862011-09-22 03:23:13 -0400716MODULE_FIRMWARE(FIRMWARE_8168E_3);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800717MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000720static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
721 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100722static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100724static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100726static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200728static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700730 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200731static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200733static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700734static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
françois romieub646d902011-01-03 15:08:21 +0000736static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
737{
738 void __iomem *ioaddr = tp->mmio_addr;
739 int i;
740
741 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
742 for (i = 0; i < 20; i++) {
743 udelay(100);
744 if (RTL_R32(OCPAR) & OCPAR_FLAG)
745 break;
746 }
747 return RTL_R32(OCPDR);
748}
749
750static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
751{
752 void __iomem *ioaddr = tp->mmio_addr;
753 int i;
754
755 RTL_W32(OCPDR, data);
756 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
757 for (i = 0; i < 20; i++) {
758 udelay(100);
759 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
760 break;
761 }
762}
763
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800764static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000765{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800766 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000767 int i;
768
769 RTL_W8(ERIDR, cmd);
770 RTL_W32(ERIAR, 0x800010e8);
771 msleep(2);
772 for (i = 0; i < 5; i++) {
773 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200774 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000775 break;
776 }
777
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800778 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000779}
780
781#define OOB_CMD_RESET 0x00
782#define OOB_CMD_DRIVER_START 0x05
783#define OOB_CMD_DRIVER_STOP 0x06
784
Francois Romieucecb5fd2011-04-01 10:21:07 +0200785static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
786{
787 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
788}
789
françois romieub646d902011-01-03 15:08:21 +0000790static void rtl8168_driver_start(struct rtl8169_private *tp)
791{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200792 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000793 int i;
794
795 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
796
Francois Romieucecb5fd2011-04-01 10:21:07 +0200797 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000798
françois romieub646d902011-01-03 15:08:21 +0000799 for (i = 0; i < 10; i++) {
800 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000801 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000802 break;
803 }
804}
805
806static void rtl8168_driver_stop(struct rtl8169_private *tp)
807{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200808 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000809 int i;
810
811 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
812
Francois Romieucecb5fd2011-04-01 10:21:07 +0200813 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000814
françois romieub646d902011-01-03 15:08:21 +0000815 for (i = 0; i < 10; i++) {
816 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000817 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000818 break;
819 }
820}
821
hayeswang4804b3b2011-03-21 01:50:29 +0000822static int r8168dp_check_dash(struct rtl8169_private *tp)
823{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200824 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000825
Francois Romieucecb5fd2011-04-01 10:21:07 +0200826 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000827}
françois romieub646d902011-01-03 15:08:21 +0000828
françois romieu4da19632011-01-03 15:07:55 +0000829static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 int i;
832
Francois Romieua6baf3a2007-11-08 23:23:21 +0100833 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Francois Romieu23714082006-01-29 00:49:09 +0100835 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100836 /*
837 * Check if the RTL8169 has completed writing to the specified
838 * MII register.
839 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200840 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 break;
Francois Romieu23714082006-01-29 00:49:09 +0100842 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700844 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700845 * According to hardware specs a 20us delay is required after write
846 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700847 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700848 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
françois romieu4da19632011-01-03 15:07:55 +0000851static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 int i, value = -1;
854
Francois Romieua6baf3a2007-11-08 23:23:21 +0100855 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Francois Romieu23714082006-01-29 00:49:09 +0100857 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100858 /*
859 * Check if the RTL8169 has completed retrieving data from
860 * the specified MII register.
861 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100863 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
865 }
Francois Romieu23714082006-01-29 00:49:09 +0100866 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700868 /*
869 * According to hardware specs a 20us delay is required after read
870 * complete indication, but before sending next command.
871 */
872 udelay(20);
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return value;
875}
876
françois romieuc0e45c12011-01-03 15:08:04 +0000877static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
878{
879 int i;
880
881 RTL_W32(OCPDR, data |
882 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
883 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
884 RTL_W32(EPHY_RXER_NUM, 0);
885
886 for (i = 0; i < 100; i++) {
887 mdelay(1);
888 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
889 break;
890 }
891}
892
893static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
894{
895 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
896 (value & OCPDR_DATA_MASK));
897}
898
899static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
900{
901 int i;
902
903 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
904
905 mdelay(1);
906 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
907 RTL_W32(EPHY_RXER_NUM, 0);
908
909 for (i = 0; i < 100; i++) {
910 mdelay(1);
911 if (RTL_R32(OCPAR) & OCPAR_FLAG)
912 break;
913 }
914
915 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
916}
917
françois romieue6de30d2011-01-03 15:08:37 +0000918#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
919
920static void r8168dp_2_mdio_start(void __iomem *ioaddr)
921{
922 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
923}
924
925static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
926{
927 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
928}
929
930static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
931{
932 r8168dp_2_mdio_start(ioaddr);
933
934 r8169_mdio_write(ioaddr, reg_addr, value);
935
936 r8168dp_2_mdio_stop(ioaddr);
937}
938
939static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
940{
941 int value;
942
943 r8168dp_2_mdio_start(ioaddr);
944
945 value = r8169_mdio_read(ioaddr, reg_addr);
946
947 r8168dp_2_mdio_stop(ioaddr);
948
949 return value;
950}
951
françois romieu4da19632011-01-03 15:07:55 +0000952static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200953{
françois romieuc0e45c12011-01-03 15:08:04 +0000954 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200955}
956
françois romieu4da19632011-01-03 15:07:55 +0000957static int rtl_readphy(struct rtl8169_private *tp, int location)
958{
françois romieuc0e45c12011-01-03 15:08:04 +0000959 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000960}
961
962static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
963{
964 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
965}
966
967static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000968{
969 int val;
970
françois romieu4da19632011-01-03 15:07:55 +0000971 val = rtl_readphy(tp, reg_addr);
972 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000973}
974
Francois Romieuccdffb92008-07-26 14:26:06 +0200975static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
976 int val)
977{
978 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200979
françois romieu4da19632011-01-03 15:07:55 +0000980 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200981}
982
983static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
984{
985 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200986
françois romieu4da19632011-01-03 15:07:55 +0000987 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200988}
989
Francois Romieudacf8152008-08-02 20:44:13 +0200990static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
991{
992 unsigned int i;
993
994 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
995 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
996
997 for (i = 0; i < 100; i++) {
998 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
999 break;
1000 udelay(10);
1001 }
1002}
1003
1004static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
1005{
1006 u16 value = 0xffff;
1007 unsigned int i;
1008
1009 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1010
1011 for (i = 0; i < 100; i++) {
1012 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1013 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1014 break;
1015 }
1016 udelay(10);
1017 }
1018
1019 return value;
1020}
1021
1022static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1023{
1024 unsigned int i;
1025
1026 RTL_W32(CSIDR, value);
1027 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1028 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1029
1030 for (i = 0; i < 100; i++) {
1031 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1032 break;
1033 udelay(10);
1034 }
1035}
1036
1037static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1038{
1039 u32 value = ~0x00;
1040 unsigned int i;
1041
1042 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1043 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1044
1045 for (i = 0; i < 100; i++) {
1046 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1047 value = RTL_R32(CSIDR);
1048 break;
1049 }
1050 udelay(10);
1051 }
1052
1053 return value;
1054}
1055
Hayes Wang133ac402011-07-06 15:58:05 +08001056static
1057void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1058{
1059 unsigned int i;
1060
1061 BUG_ON((addr & 3) || (mask == 0));
1062 RTL_W32(ERIDR, val);
1063 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1064
1065 for (i = 0; i < 100; i++) {
1066 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1067 break;
1068 udelay(100);
1069 }
1070}
1071
1072static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1073{
1074 u32 value = ~0x00;
1075 unsigned int i;
1076
1077 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1078
1079 for (i = 0; i < 100; i++) {
1080 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1081 value = RTL_R32(ERIDR);
1082 break;
1083 }
1084 udelay(100);
1085 }
1086
1087 return value;
1088}
1089
1090static void
1091rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1092{
1093 u32 val;
1094
1095 val = rtl_eri_read(ioaddr, addr, type);
1096 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1097}
1098
françois romieuc28aa382011-08-02 03:53:43 +00001099struct exgmac_reg {
1100 u16 addr;
1101 u16 mask;
1102 u32 val;
1103};
1104
1105static void rtl_write_exgmac_batch(void __iomem *ioaddr,
1106 const struct exgmac_reg *r, int len)
1107{
1108 while (len-- > 0) {
1109 rtl_eri_write(ioaddr, r->addr, r->mask, r->val, ERIAR_EXGMAC);
1110 r++;
1111 }
1112}
1113
françois romieudaf9df62009-10-07 12:44:20 +00001114static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1115{
1116 u8 value = 0xff;
1117 unsigned int i;
1118
1119 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1120
1121 for (i = 0; i < 300; i++) {
1122 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1123 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1124 break;
1125 }
1126 udelay(100);
1127 }
1128
1129 return value;
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1133{
1134 RTL_W16(IntrMask, 0x0000);
1135
1136 RTL_W16(IntrStatus, 0xffff);
1137}
1138
françois romieu4da19632011-01-03 15:07:55 +00001139static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
françois romieu4da19632011-01-03 15:07:55 +00001141 void __iomem *ioaddr = tp->mmio_addr;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return RTL_R32(TBICSR) & TBIReset;
1144}
1145
françois romieu4da19632011-01-03 15:07:55 +00001146static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
françois romieu4da19632011-01-03 15:07:55 +00001148 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1152{
1153 return RTL_R32(TBICSR) & TBILinkOk;
1154}
1155
1156static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1157{
1158 return RTL_R8(PHYstatus) & LinkStatus;
1159}
1160
françois romieu4da19632011-01-03 15:07:55 +00001161static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
françois romieu4da19632011-01-03 15:07:55 +00001163 void __iomem *ioaddr = tp->mmio_addr;
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1166}
1167
françois romieu4da19632011-01-03 15:07:55 +00001168static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 unsigned int val;
1171
françois romieu4da19632011-01-03 15:07:55 +00001172 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1173 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Hayes Wang70090422011-07-06 15:58:06 +08001176static void rtl_link_chg_patch(struct rtl8169_private *tp)
1177{
1178 void __iomem *ioaddr = tp->mmio_addr;
1179 struct net_device *dev = tp->dev;
1180
1181 if (!netif_running(dev))
1182 return;
1183
1184 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
1185 if (RTL_R8(PHYstatus) & _1000bpsF) {
1186 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1187 0x00000011, ERIAR_EXGMAC);
1188 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1189 0x00000005, ERIAR_EXGMAC);
1190 } else if (RTL_R8(PHYstatus) & _100bps) {
1191 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1192 0x0000001f, ERIAR_EXGMAC);
1193 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1194 0x00000005, ERIAR_EXGMAC);
1195 } else {
1196 rtl_eri_write(ioaddr, 0x1bc, ERIAR_MASK_1111,
1197 0x0000001f, ERIAR_EXGMAC);
1198 rtl_eri_write(ioaddr, 0x1dc, ERIAR_MASK_1111,
1199 0x0000003f, ERIAR_EXGMAC);
1200 }
1201 /* Reset packet filter */
1202 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x00, 0x01,
1203 ERIAR_EXGMAC);
1204 rtl_w1w0_eri(ioaddr, 0xdc, ERIAR_MASK_0001, 0x01, 0x00,
1205 ERIAR_EXGMAC);
1206 }
1207}
1208
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001209static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001210 struct rtl8169_private *tp,
1211 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 unsigned long flags;
1214
1215 spin_lock_irqsave(&tp->lock, flags);
1216 if (tp->link_ok(ioaddr)) {
Hayes Wang70090422011-07-06 15:58:06 +08001217 rtl_link_chg_patch(tp);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001218 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001219 if (pm)
1220 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001222 if (net_ratelimit())
1223 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001224 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001226 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001227 if (pm)
1228 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 spin_unlock_irqrestore(&tp->lock, flags);
1231}
1232
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001233static void rtl8169_check_link_status(struct net_device *dev,
1234 struct rtl8169_private *tp,
1235 void __iomem *ioaddr)
1236{
1237 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1238}
1239
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001240#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1241
1242static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1243{
1244 void __iomem *ioaddr = tp->mmio_addr;
1245 u8 options;
1246 u32 wolopts = 0;
1247
1248 options = RTL_R8(Config1);
1249 if (!(options & PMEnable))
1250 return 0;
1251
1252 options = RTL_R8(Config3);
1253 if (options & LinkUp)
1254 wolopts |= WAKE_PHY;
1255 if (options & MagicPacket)
1256 wolopts |= WAKE_MAGIC;
1257
1258 options = RTL_R8(Config5);
1259 if (options & UWF)
1260 wolopts |= WAKE_UCAST;
1261 if (options & BWF)
1262 wolopts |= WAKE_BCAST;
1263 if (options & MWF)
1264 wolopts |= WAKE_MCAST;
1265
1266 return wolopts;
1267}
1268
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001269static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1270{
1271 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001272
1273 spin_lock_irq(&tp->lock);
1274
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001275 wol->supported = WAKE_ANY;
1276 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001277
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001278 spin_unlock_irq(&tp->lock);
1279}
1280
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001281static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001282{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001283 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001284 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001285 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001286 u32 opt;
1287 u16 reg;
1288 u8 mask;
1289 } cfg[] = {
1290 { WAKE_ANY, Config1, PMEnable },
1291 { WAKE_PHY, Config3, LinkUp },
1292 { WAKE_MAGIC, Config3, MagicPacket },
1293 { WAKE_UCAST, Config5, UWF },
1294 { WAKE_BCAST, Config5, BWF },
1295 { WAKE_MCAST, Config5, MWF },
1296 { WAKE_ANY, Config5, LanWake }
1297 };
1298
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001299 RTL_W8(Cfg9346, Cfg9346_Unlock);
1300
1301 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1302 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001303 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001304 options |= cfg[i].mask;
1305 RTL_W8(cfg[i].reg, options);
1306 }
1307
1308 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001309}
1310
1311static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1312{
1313 struct rtl8169_private *tp = netdev_priv(dev);
1314
1315 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001316
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001317 if (wol->wolopts)
1318 tp->features |= RTL_FEATURE_WOL;
1319 else
1320 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001321 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001322 spin_unlock_irq(&tp->lock);
1323
françois romieuea809072010-11-08 13:23:58 +00001324 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1325
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001326 return 0;
1327}
1328
Francois Romieu31bd2042011-04-26 18:58:59 +02001329static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1330{
Francois Romieu85bffe62011-04-27 08:22:39 +02001331 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334static void rtl8169_get_drvinfo(struct net_device *dev,
1335 struct ethtool_drvinfo *info)
1336{
1337 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001338 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 strcpy(info->driver, MODULENAME);
1341 strcpy(info->version, RTL8169_VERSION);
1342 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001343 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1344 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1345 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
1348static int rtl8169_get_regs_len(struct net_device *dev)
1349{
1350 return R8169_REGS_SIZE;
1351}
1352
1353static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001354 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct rtl8169_private *tp = netdev_priv(dev);
1357 void __iomem *ioaddr = tp->mmio_addr;
1358 int ret = 0;
1359 u32 reg;
1360
1361 reg = RTL_R32(TBICSR);
1362 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1363 (duplex == DUPLEX_FULL)) {
1364 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1365 } else if (autoneg == AUTONEG_ENABLE)
1366 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1367 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001368 netif_warn(tp, link, dev,
1369 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 ret = -EOPNOTSUPP;
1371 }
1372
1373 return ret;
1374}
1375
1376static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001377 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001380 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001381 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Hayes Wang716b50a2011-02-22 17:26:18 +08001383 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001386 int auto_nego;
1387
françois romieu4da19632011-01-03 15:07:55 +00001388 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001389 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1390 ADVERTISE_100HALF | ADVERTISE_100FULL);
1391
1392 if (adv & ADVERTISED_10baseT_Half)
1393 auto_nego |= ADVERTISE_10HALF;
1394 if (adv & ADVERTISED_10baseT_Full)
1395 auto_nego |= ADVERTISE_10FULL;
1396 if (adv & ADVERTISED_100baseT_Half)
1397 auto_nego |= ADVERTISE_100HALF;
1398 if (adv & ADVERTISED_100baseT_Full)
1399 auto_nego |= ADVERTISE_100FULL;
1400
françois romieu3577aa12009-05-19 10:46:48 +00001401 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1402
françois romieu4da19632011-01-03 15:07:55 +00001403 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001404 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1405
1406 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001407 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001408 if (adv & ADVERTISED_1000baseT_Half)
1409 giga_ctrl |= ADVERTISE_1000HALF;
1410 if (adv & ADVERTISED_1000baseT_Full)
1411 giga_ctrl |= ADVERTISE_1000FULL;
1412 } else if (adv & (ADVERTISED_1000baseT_Half |
1413 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001414 netif_info(tp, link, dev,
1415 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001416 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
françois romieu3577aa12009-05-19 10:46:48 +00001419 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001420
françois romieu4da19632011-01-03 15:07:55 +00001421 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1422 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001423 } else {
1424 giga_ctrl = 0;
1425
1426 if (speed == SPEED_10)
1427 bmcr = 0;
1428 else if (speed == SPEED_100)
1429 bmcr = BMCR_SPEED100;
1430 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001431 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001432
1433 if (duplex == DUPLEX_FULL)
1434 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001435 }
1436
françois romieu4da19632011-01-03 15:07:55 +00001437 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001438
Francois Romieucecb5fd2011-04-01 10:21:07 +02001439 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1440 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001441 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001442 rtl_writephy(tp, 0x17, 0x2138);
1443 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001444 } else {
françois romieu4da19632011-01-03 15:07:55 +00001445 rtl_writephy(tp, 0x17, 0x2108);
1446 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001447 }
1448 }
1449
Oliver Neukum54405cd2011-01-06 21:55:13 +01001450 rc = 0;
1451out:
1452 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001456 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct rtl8169_private *tp = netdev_priv(dev);
1459 int ret;
1460
Oliver Neukum54405cd2011-01-06 21:55:13 +01001461 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001462 if (ret < 0)
1463 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Francois Romieu4876cc12011-03-11 21:07:11 +01001465 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1466 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001468 }
1469out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return ret;
1471}
1472
1473static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1474{
1475 struct rtl8169_private *tp = netdev_priv(dev);
1476 unsigned long flags;
1477 int ret;
1478
Francois Romieu4876cc12011-03-11 21:07:11 +01001479 del_timer_sync(&tp->timer);
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001482 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001483 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return ret;
1487}
1488
Michał Mirosław350fb322011-04-08 06:35:56 +00001489static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001491 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001492 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Michał Mirosław350fb322011-04-08 06:35:56 +00001494 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Michał Mirosław350fb322011-04-08 06:35:56 +00001497static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 struct rtl8169_private *tp = netdev_priv(dev);
1500 void __iomem *ioaddr = tp->mmio_addr;
1501 unsigned long flags;
1502
1503 spin_lock_irqsave(&tp->lock, flags);
1504
Michał Mirosław350fb322011-04-08 06:35:56 +00001505 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 tp->cp_cmd |= RxChkSum;
1507 else
1508 tp->cp_cmd &= ~RxChkSum;
1509
Michał Mirosław350fb322011-04-08 06:35:56 +00001510 if (dev->features & NETIF_F_HW_VLAN_RX)
1511 tp->cp_cmd |= RxVlan;
1512 else
1513 tp->cp_cmd &= ~RxVlan;
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 RTL_W16(CPlusCmd, tp->cp_cmd);
1516 RTL_R16(CPlusCmd);
1517
1518 spin_unlock_irqrestore(&tp->lock, flags);
1519
1520 return 0;
1521}
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1524 struct sk_buff *skb)
1525{
Jesse Grosseab6d182010-10-20 13:56:03 +00001526 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1528}
1529
Francois Romieu7a8fc772011-03-01 17:18:33 +01001530static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Francois Romieu7a8fc772011-03-01 17:18:33 +01001534 if (opts2 & RxVlanTag)
1535 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
Francois Romieuccdffb92008-07-26 14:26:06 +02001540static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct rtl8169_private *tp = netdev_priv(dev);
1543 void __iomem *ioaddr = tp->mmio_addr;
1544 u32 status;
1545
1546 cmd->supported =
1547 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1548 cmd->port = PORT_FIBRE;
1549 cmd->transceiver = XCVR_INTERNAL;
1550
1551 status = RTL_R32(TBICSR);
1552 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1553 cmd->autoneg = !!(status & TBINwEnable);
1554
David Decotigny70739492011-04-27 18:32:40 +00001555 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001557
1558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Francois Romieuccdffb92008-07-26 14:26:06 +02001561static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Francois Romieuccdffb92008-07-26 14:26:06 +02001565 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
1568static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1569{
1570 struct rtl8169_private *tp = netdev_priv(dev);
1571 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001572 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 spin_lock_irqsave(&tp->lock, flags);
1575
Francois Romieuccdffb92008-07-26 14:26:06 +02001576 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001579 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
1582static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1583 void *p)
1584{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001585 struct rtl8169_private *tp = netdev_priv(dev);
1586 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Francois Romieu5b0384f2006-08-16 16:00:01 +02001588 if (regs->len > R8169_REGS_SIZE)
1589 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Francois Romieu5b0384f2006-08-16 16:00:01 +02001591 spin_lock_irqsave(&tp->lock, flags);
1592 memcpy_fromio(p, tp->mmio_addr, regs->len);
1593 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001596static u32 rtl8169_get_msglevel(struct net_device *dev)
1597{
1598 struct rtl8169_private *tp = netdev_priv(dev);
1599
1600 return tp->msg_enable;
1601}
1602
1603static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1604{
1605 struct rtl8169_private *tp = netdev_priv(dev);
1606
1607 tp->msg_enable = value;
1608}
1609
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001610static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1611 "tx_packets",
1612 "rx_packets",
1613 "tx_errors",
1614 "rx_errors",
1615 "rx_missed",
1616 "align_errors",
1617 "tx_single_collisions",
1618 "tx_multi_collisions",
1619 "unicast",
1620 "broadcast",
1621 "multicast",
1622 "tx_aborted",
1623 "tx_underrun",
1624};
1625
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001626static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001627{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001628 switch (sset) {
1629 case ETH_SS_STATS:
1630 return ARRAY_SIZE(rtl8169_gstrings);
1631 default:
1632 return -EOPNOTSUPP;
1633 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001634}
1635
Ivan Vecera355423d2009-02-06 21:49:57 -08001636static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001637{
1638 struct rtl8169_private *tp = netdev_priv(dev);
1639 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001640 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001641 struct rtl8169_counters *counters;
1642 dma_addr_t paddr;
1643 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001644 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001645
Ivan Vecera355423d2009-02-06 21:49:57 -08001646 /*
1647 * Some chips are unable to dump tally counters when the receiver
1648 * is disabled.
1649 */
1650 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1651 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001652
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001653 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001654 if (!counters)
1655 return;
1656
1657 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001658 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001659 RTL_W32(CounterAddrLow, cmd);
1660 RTL_W32(CounterAddrLow, cmd | CounterDump);
1661
Ivan Vecera355423d2009-02-06 21:49:57 -08001662 while (wait--) {
1663 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001664 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001665 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001666 }
1667 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001668 }
1669
1670 RTL_W32(CounterAddrLow, 0);
1671 RTL_W32(CounterAddrHigh, 0);
1672
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001673 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001674}
1675
Ivan Vecera355423d2009-02-06 21:49:57 -08001676static void rtl8169_get_ethtool_stats(struct net_device *dev,
1677 struct ethtool_stats *stats, u64 *data)
1678{
1679 struct rtl8169_private *tp = netdev_priv(dev);
1680
1681 ASSERT_RTNL();
1682
1683 rtl8169_update_counters(dev);
1684
1685 data[0] = le64_to_cpu(tp->counters.tx_packets);
1686 data[1] = le64_to_cpu(tp->counters.rx_packets);
1687 data[2] = le64_to_cpu(tp->counters.tx_errors);
1688 data[3] = le32_to_cpu(tp->counters.rx_errors);
1689 data[4] = le16_to_cpu(tp->counters.rx_missed);
1690 data[5] = le16_to_cpu(tp->counters.align_errors);
1691 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1692 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1693 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1694 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1695 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1696 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1697 data[12] = le16_to_cpu(tp->counters.tx_underun);
1698}
1699
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001700static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1701{
1702 switch(stringset) {
1703 case ETH_SS_STATS:
1704 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1705 break;
1706 }
1707}
1708
Jeff Garzik7282d492006-09-13 14:30:00 -04001709static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 .get_drvinfo = rtl8169_get_drvinfo,
1711 .get_regs_len = rtl8169_get_regs_len,
1712 .get_link = ethtool_op_get_link,
1713 .get_settings = rtl8169_get_settings,
1714 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001715 .get_msglevel = rtl8169_get_msglevel,
1716 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001718 .get_wol = rtl8169_get_wol,
1719 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001720 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001721 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001722 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723};
1724
Francois Romieu07d3f512007-02-21 22:40:46 +01001725static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001726 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Francois Romieu5d320a22011-05-08 17:47:36 +02001728 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001729 /*
1730 * The driver currently handles the 8168Bf and the 8168Be identically
1731 * but they can be identified more specifically through the test below
1732 * if needed:
1733 *
1734 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001735 *
1736 * Same thing for the 8101Eb and the 8101Ec:
1737 *
1738 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001739 */
Francois Romieu37441002011-06-17 22:58:54 +02001740 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001742 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 int mac_version;
1744 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001745 /* 8168E family. */
Hayes Wang70090422011-07-06 15:58:06 +08001746 { 0x7c800000, 0x2c800000, RTL_GIGA_MAC_VER_34 },
hayeswang01dc7fe2011-03-21 01:50:28 +00001747 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1748 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1749 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1750
Francois Romieu5b538df2008-07-20 16:22:45 +02001751 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001752 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1753 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001754 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001755
françois romieue6de30d2011-01-03 15:08:37 +00001756 /* 8168DP family. */
1757 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1758 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001759 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001760
Francois Romieuef808d52008-06-29 13:10:54 +02001761 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001762 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001763 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001764 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001765 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001766 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1767 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001768 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001769 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001770 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001771
1772 /* 8168B family. */
1773 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1774 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1775 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1776 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1777
1778 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001779 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001780 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1781 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1782 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001783 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1784 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1785 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1786 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1787 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1788 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001789 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001790 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001791 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001792 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1793 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001794 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1795 /* FIXME: where did these entries come from ? -- FR */
1796 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1797 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1798
1799 /* 8110 family. */
1800 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1801 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1802 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1803 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1804 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1805 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1806
Jean Delvaref21b75e2009-05-26 20:54:48 -07001807 /* Catch-all */
1808 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001809 };
1810 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 u32 reg;
1812
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001813 reg = RTL_R32(TxConfig);
1814 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 p++;
1816 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001817
1818 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1819 netif_notice(tp, probe, dev,
1820 "unknown MAC, using family default\n");
1821 tp->mac_version = default_version;
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
1825static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1826{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001827 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Francois Romieu867763c2007-08-17 18:21:58 +02001830struct phy_reg {
1831 u16 reg;
1832 u16 val;
1833};
1834
françois romieu4da19632011-01-03 15:07:55 +00001835static void rtl_writephy_batch(struct rtl8169_private *tp,
1836 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001837{
1838 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001839 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001840 regs++;
1841 }
1842}
1843
françois romieubca03d52011-01-03 15:07:31 +00001844#define PHY_READ 0x00000000
1845#define PHY_DATA_OR 0x10000000
1846#define PHY_DATA_AND 0x20000000
1847#define PHY_BJMPN 0x30000000
1848#define PHY_READ_EFUSE 0x40000000
1849#define PHY_READ_MAC_BYTE 0x50000000
1850#define PHY_WRITE_MAC_BYTE 0x60000000
1851#define PHY_CLEAR_READCOUNT 0x70000000
1852#define PHY_WRITE 0x80000000
1853#define PHY_READCOUNT_EQ_SKIP 0x90000000
1854#define PHY_COMP_EQ_SKIPN 0xa0000000
1855#define PHY_COMP_NEQ_SKIPN 0xb0000000
1856#define PHY_WRITE_PREVIOUS 0xc0000000
1857#define PHY_SKIPN 0xd0000000
1858#define PHY_DELAY_MS 0xe0000000
1859#define PHY_WRITE_ERI_WORD 0xf0000000
1860
Hayes Wang960aee62011-06-18 11:37:48 +02001861struct fw_info {
1862 u32 magic;
1863 char version[RTL_VER_SIZE];
1864 __le32 fw_start;
1865 __le32 fw_len;
1866 u8 chksum;
1867} __packed;
1868
Francois Romieu1c361ef2011-06-17 17:16:24 +02001869#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1870
1871static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001872{
Francois Romieub6ffd972011-06-17 17:00:05 +02001873 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001874 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001875 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1876 char *version = rtl_fw->version;
1877 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001878
Francois Romieu1c361ef2011-06-17 17:16:24 +02001879 if (fw->size < FW_OPCODE_SIZE)
1880 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001881
1882 if (!fw_info->magic) {
1883 size_t i, size, start;
1884 u8 checksum = 0;
1885
1886 if (fw->size < sizeof(*fw_info))
1887 goto out;
1888
1889 for (i = 0; i < fw->size; i++)
1890 checksum += fw->data[i];
1891 if (checksum != 0)
1892 goto out;
1893
1894 start = le32_to_cpu(fw_info->fw_start);
1895 if (start > fw->size)
1896 goto out;
1897
1898 size = le32_to_cpu(fw_info->fw_len);
1899 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1900 goto out;
1901
1902 memcpy(version, fw_info->version, RTL_VER_SIZE);
1903
1904 pa->code = (__le32 *)(fw->data + start);
1905 pa->size = size;
1906 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001907 if (fw->size % FW_OPCODE_SIZE)
1908 goto out;
1909
1910 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1911
1912 pa->code = (__le32 *)fw->data;
1913 pa->size = fw->size / FW_OPCODE_SIZE;
1914 }
1915 version[RTL_VER_SIZE - 1] = 0;
1916
1917 rc = true;
1918out:
1919 return rc;
1920}
1921
Francois Romieufd112f22011-06-18 00:10:29 +02001922static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1923 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02001924{
Francois Romieufd112f22011-06-18 00:10:29 +02001925 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001926 size_t index;
1927
Francois Romieu1c361ef2011-06-17 17:16:24 +02001928 for (index = 0; index < pa->size; index++) {
1929 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00001930 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001931
hayeswang42b82dc2011-01-10 02:07:25 +00001932 switch(action & 0xf0000000) {
1933 case PHY_READ:
1934 case PHY_DATA_OR:
1935 case PHY_DATA_AND:
1936 case PHY_READ_EFUSE:
1937 case PHY_CLEAR_READCOUNT:
1938 case PHY_WRITE:
1939 case PHY_WRITE_PREVIOUS:
1940 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001941 break;
1942
hayeswang42b82dc2011-01-10 02:07:25 +00001943 case PHY_BJMPN:
1944 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02001945 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001946 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001947 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001948 }
1949 break;
1950 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001951 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001952 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001953 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001954 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001955 }
1956 break;
1957 case PHY_COMP_EQ_SKIPN:
1958 case PHY_COMP_NEQ_SKIPN:
1959 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001960 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001961 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001962 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001963 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001964 }
1965 break;
1966
1967 case PHY_READ_MAC_BYTE:
1968 case PHY_WRITE_MAC_BYTE:
1969 case PHY_WRITE_ERI_WORD:
1970 default:
Francois Romieufd112f22011-06-18 00:10:29 +02001971 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00001972 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02001973 goto out;
françois romieubca03d52011-01-03 15:07:31 +00001974 }
1975 }
Francois Romieufd112f22011-06-18 00:10:29 +02001976 rc = true;
1977out:
1978 return rc;
1979}
françois romieubca03d52011-01-03 15:07:31 +00001980
Francois Romieufd112f22011-06-18 00:10:29 +02001981static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1982{
1983 struct net_device *dev = tp->dev;
1984 int rc = -EINVAL;
1985
1986 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1987 netif_err(tp, ifup, dev, "invalid firwmare\n");
1988 goto out;
1989 }
1990
1991 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1992 rc = 0;
1993out:
1994 return rc;
1995}
1996
1997static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1998{
1999 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
2000 u32 predata, count;
2001 size_t index;
2002
2003 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00002004
Francois Romieu1c361ef2011-06-17 17:16:24 +02002005 for (index = 0; index < pa->size; ) {
2006 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00002007 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00002008 u32 regno = (action & 0x0fff0000) >> 16;
2009
2010 if (!action)
2011 break;
françois romieubca03d52011-01-03 15:07:31 +00002012
2013 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00002014 case PHY_READ:
2015 predata = rtl_readphy(tp, regno);
2016 count++;
2017 index++;
françois romieubca03d52011-01-03 15:07:31 +00002018 break;
hayeswang42b82dc2011-01-10 02:07:25 +00002019 case PHY_DATA_OR:
2020 predata |= data;
2021 index++;
2022 break;
2023 case PHY_DATA_AND:
2024 predata &= data;
2025 index++;
2026 break;
2027 case PHY_BJMPN:
2028 index -= regno;
2029 break;
2030 case PHY_READ_EFUSE:
2031 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
2032 index++;
2033 break;
2034 case PHY_CLEAR_READCOUNT:
2035 count = 0;
2036 index++;
2037 break;
2038 case PHY_WRITE:
2039 rtl_writephy(tp, regno, data);
2040 index++;
2041 break;
2042 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02002043 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00002044 break;
2045 case PHY_COMP_EQ_SKIPN:
2046 if (predata == data)
2047 index += regno;
2048 index++;
2049 break;
2050 case PHY_COMP_NEQ_SKIPN:
2051 if (predata != data)
2052 index += regno;
2053 index++;
2054 break;
2055 case PHY_WRITE_PREVIOUS:
2056 rtl_writephy(tp, regno, predata);
2057 index++;
2058 break;
2059 case PHY_SKIPN:
2060 index += regno + 1;
2061 break;
2062 case PHY_DELAY_MS:
2063 mdelay(data);
2064 index++;
2065 break;
2066
2067 case PHY_READ_MAC_BYTE:
2068 case PHY_WRITE_MAC_BYTE:
2069 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002070 default:
2071 BUG();
2072 }
2073 }
2074}
2075
françois romieuf1e02ed2011-01-13 13:07:53 +00002076static void rtl_release_firmware(struct rtl8169_private *tp)
2077{
Francois Romieub6ffd972011-06-17 17:00:05 +02002078 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2079 release_firmware(tp->rtl_fw->fw);
2080 kfree(tp->rtl_fw);
2081 }
2082 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002083}
2084
François Romieu953a12c2011-04-24 17:38:48 +02002085static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002086{
Francois Romieub6ffd972011-06-17 17:00:05 +02002087 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002088
2089 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002090 if (!IS_ERR_OR_NULL(rtl_fw))
2091 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002092}
2093
2094static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2095{
2096 if (rtl_readphy(tp, reg) != val)
2097 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2098 else
2099 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002100}
2101
françois romieu4da19632011-01-03 15:07:55 +00002102static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002104 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002105 { 0x1f, 0x0001 },
2106 { 0x06, 0x006e },
2107 { 0x08, 0x0708 },
2108 { 0x15, 0x4000 },
2109 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
françois romieu0b9b5712009-08-10 19:44:56 +00002111 { 0x1f, 0x0001 },
2112 { 0x03, 0x00a1 },
2113 { 0x02, 0x0008 },
2114 { 0x01, 0x0120 },
2115 { 0x00, 0x1000 },
2116 { 0x04, 0x0800 },
2117 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
françois romieu0b9b5712009-08-10 19:44:56 +00002119 { 0x03, 0xff41 },
2120 { 0x02, 0xdf60 },
2121 { 0x01, 0x0140 },
2122 { 0x00, 0x0077 },
2123 { 0x04, 0x7800 },
2124 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
françois romieu0b9b5712009-08-10 19:44:56 +00002126 { 0x03, 0x802f },
2127 { 0x02, 0x4f02 },
2128 { 0x01, 0x0409 },
2129 { 0x00, 0xf0f9 },
2130 { 0x04, 0x9800 },
2131 { 0x04, 0x9000 },
2132
2133 { 0x03, 0xdf01 },
2134 { 0x02, 0xdf20 },
2135 { 0x01, 0xff95 },
2136 { 0x00, 0xba00 },
2137 { 0x04, 0xa800 },
2138 { 0x04, 0xa000 },
2139
2140 { 0x03, 0xff41 },
2141 { 0x02, 0xdf20 },
2142 { 0x01, 0x0140 },
2143 { 0x00, 0x00bb },
2144 { 0x04, 0xb800 },
2145 { 0x04, 0xb000 },
2146
2147 { 0x03, 0xdf41 },
2148 { 0x02, 0xdc60 },
2149 { 0x01, 0x6340 },
2150 { 0x00, 0x007d },
2151 { 0x04, 0xd800 },
2152 { 0x04, 0xd000 },
2153
2154 { 0x03, 0xdf01 },
2155 { 0x02, 0xdf20 },
2156 { 0x01, 0x100a },
2157 { 0x00, 0xa0ff },
2158 { 0x04, 0xf800 },
2159 { 0x04, 0xf000 },
2160
2161 { 0x1f, 0x0000 },
2162 { 0x0b, 0x0000 },
2163 { 0x00, 0x9200 }
2164 };
2165
françois romieu4da19632011-01-03 15:07:55 +00002166 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
françois romieu4da19632011-01-03 15:07:55 +00002169static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002170{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002171 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002172 { 0x1f, 0x0002 },
2173 { 0x01, 0x90d0 },
2174 { 0x1f, 0x0000 }
2175 };
2176
françois romieu4da19632011-01-03 15:07:55 +00002177 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002178}
2179
françois romieu4da19632011-01-03 15:07:55 +00002180static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002181{
2182 struct pci_dev *pdev = tp->pci_dev;
françois romieu2e9558562009-08-10 19:44:19 +00002183
Sergei Shtylyovccbae552011-07-22 05:37:24 +00002184 if ((pdev->subsystem_vendor != PCI_VENDOR_ID_GIGABYTE) ||
2185 (pdev->subsystem_device != 0xe000))
françois romieu2e9558562009-08-10 19:44:19 +00002186 return;
2187
françois romieu4da19632011-01-03 15:07:55 +00002188 rtl_writephy(tp, 0x1f, 0x0001);
2189 rtl_writephy(tp, 0x10, 0xf01b);
2190 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002191}
2192
françois romieu4da19632011-01-03 15:07:55 +00002193static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002194{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002195 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002196 { 0x1f, 0x0001 },
2197 { 0x04, 0x0000 },
2198 { 0x03, 0x00a1 },
2199 { 0x02, 0x0008 },
2200 { 0x01, 0x0120 },
2201 { 0x00, 0x1000 },
2202 { 0x04, 0x0800 },
2203 { 0x04, 0x9000 },
2204 { 0x03, 0x802f },
2205 { 0x02, 0x4f02 },
2206 { 0x01, 0x0409 },
2207 { 0x00, 0xf099 },
2208 { 0x04, 0x9800 },
2209 { 0x04, 0xa000 },
2210 { 0x03, 0xdf01 },
2211 { 0x02, 0xdf20 },
2212 { 0x01, 0xff95 },
2213 { 0x00, 0xba00 },
2214 { 0x04, 0xa800 },
2215 { 0x04, 0xf000 },
2216 { 0x03, 0xdf01 },
2217 { 0x02, 0xdf20 },
2218 { 0x01, 0x101a },
2219 { 0x00, 0xa0ff },
2220 { 0x04, 0xf800 },
2221 { 0x04, 0x0000 },
2222 { 0x1f, 0x0000 },
2223
2224 { 0x1f, 0x0001 },
2225 { 0x10, 0xf41b },
2226 { 0x14, 0xfb54 },
2227 { 0x18, 0xf5c7 },
2228 { 0x1f, 0x0000 },
2229
2230 { 0x1f, 0x0001 },
2231 { 0x17, 0x0cc0 },
2232 { 0x1f, 0x0000 }
2233 };
2234
françois romieu4da19632011-01-03 15:07:55 +00002235 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002236
françois romieu4da19632011-01-03 15:07:55 +00002237 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002238}
2239
françois romieu4da19632011-01-03 15:07:55 +00002240static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002241{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002242 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002243 { 0x1f, 0x0001 },
2244 { 0x04, 0x0000 },
2245 { 0x03, 0x00a1 },
2246 { 0x02, 0x0008 },
2247 { 0x01, 0x0120 },
2248 { 0x00, 0x1000 },
2249 { 0x04, 0x0800 },
2250 { 0x04, 0x9000 },
2251 { 0x03, 0x802f },
2252 { 0x02, 0x4f02 },
2253 { 0x01, 0x0409 },
2254 { 0x00, 0xf099 },
2255 { 0x04, 0x9800 },
2256 { 0x04, 0xa000 },
2257 { 0x03, 0xdf01 },
2258 { 0x02, 0xdf20 },
2259 { 0x01, 0xff95 },
2260 { 0x00, 0xba00 },
2261 { 0x04, 0xa800 },
2262 { 0x04, 0xf000 },
2263 { 0x03, 0xdf01 },
2264 { 0x02, 0xdf20 },
2265 { 0x01, 0x101a },
2266 { 0x00, 0xa0ff },
2267 { 0x04, 0xf800 },
2268 { 0x04, 0x0000 },
2269 { 0x1f, 0x0000 },
2270
2271 { 0x1f, 0x0001 },
2272 { 0x0b, 0x8480 },
2273 { 0x1f, 0x0000 },
2274
2275 { 0x1f, 0x0001 },
2276 { 0x18, 0x67c7 },
2277 { 0x04, 0x2000 },
2278 { 0x03, 0x002f },
2279 { 0x02, 0x4360 },
2280 { 0x01, 0x0109 },
2281 { 0x00, 0x3022 },
2282 { 0x04, 0x2800 },
2283 { 0x1f, 0x0000 },
2284
2285 { 0x1f, 0x0001 },
2286 { 0x17, 0x0cc0 },
2287 { 0x1f, 0x0000 }
2288 };
2289
françois romieu4da19632011-01-03 15:07:55 +00002290 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002291}
2292
françois romieu4da19632011-01-03 15:07:55 +00002293static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002294{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002295 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002296 { 0x10, 0xf41b },
2297 { 0x1f, 0x0000 }
2298 };
2299
françois romieu4da19632011-01-03 15:07:55 +00002300 rtl_writephy(tp, 0x1f, 0x0001);
2301 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002302
françois romieu4da19632011-01-03 15:07:55 +00002303 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002304}
2305
françois romieu4da19632011-01-03 15:07:55 +00002306static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002307{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002308 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002309 { 0x1f, 0x0001 },
2310 { 0x10, 0xf41b },
2311 { 0x1f, 0x0000 }
2312 };
2313
françois romieu4da19632011-01-03 15:07:55 +00002314 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002315}
2316
françois romieu4da19632011-01-03 15:07:55 +00002317static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002318{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002319 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002320 { 0x1f, 0x0000 },
2321 { 0x1d, 0x0f00 },
2322 { 0x1f, 0x0002 },
2323 { 0x0c, 0x1ec8 },
2324 { 0x1f, 0x0000 }
2325 };
2326
françois romieu4da19632011-01-03 15:07:55 +00002327 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002328}
2329
françois romieu4da19632011-01-03 15:07:55 +00002330static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002331{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002332 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002333 { 0x1f, 0x0001 },
2334 { 0x1d, 0x3d98 },
2335 { 0x1f, 0x0000 }
2336 };
2337
françois romieu4da19632011-01-03 15:07:55 +00002338 rtl_writephy(tp, 0x1f, 0x0000);
2339 rtl_patchphy(tp, 0x14, 1 << 5);
2340 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002341
françois romieu4da19632011-01-03 15:07:55 +00002342 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002343}
2344
françois romieu4da19632011-01-03 15:07:55 +00002345static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002346{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002347 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002348 { 0x1f, 0x0001 },
2349 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002350 { 0x1f, 0x0002 },
2351 { 0x00, 0x88d4 },
2352 { 0x01, 0x82b1 },
2353 { 0x03, 0x7002 },
2354 { 0x08, 0x9e30 },
2355 { 0x09, 0x01f0 },
2356 { 0x0a, 0x5500 },
2357 { 0x0c, 0x00c8 },
2358 { 0x1f, 0x0003 },
2359 { 0x12, 0xc096 },
2360 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002361 { 0x1f, 0x0000 },
2362 { 0x1f, 0x0000 },
2363 { 0x09, 0x2000 },
2364 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002365 };
2366
françois romieu4da19632011-01-03 15:07:55 +00002367 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002368
françois romieu4da19632011-01-03 15:07:55 +00002369 rtl_patchphy(tp, 0x14, 1 << 5);
2370 rtl_patchphy(tp, 0x0d, 1 << 5);
2371 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002372}
2373
françois romieu4da19632011-01-03 15:07:55 +00002374static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002375{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002376 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002377 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002378 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002379 { 0x03, 0x802f },
2380 { 0x02, 0x4f02 },
2381 { 0x01, 0x0409 },
2382 { 0x00, 0xf099 },
2383 { 0x04, 0x9800 },
2384 { 0x04, 0x9000 },
2385 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002386 { 0x1f, 0x0002 },
2387 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002388 { 0x06, 0x0761 },
2389 { 0x1f, 0x0003 },
2390 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002391 { 0x1f, 0x0000 }
2392 };
2393
françois romieu4da19632011-01-03 15:07:55 +00002394 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002395
françois romieu4da19632011-01-03 15:07:55 +00002396 rtl_patchphy(tp, 0x16, 1 << 0);
2397 rtl_patchphy(tp, 0x14, 1 << 5);
2398 rtl_patchphy(tp, 0x0d, 1 << 5);
2399 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002400}
2401
françois romieu4da19632011-01-03 15:07:55 +00002402static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002403{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002404 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002405 { 0x1f, 0x0001 },
2406 { 0x12, 0x2300 },
2407 { 0x1d, 0x3d98 },
2408 { 0x1f, 0x0002 },
2409 { 0x0c, 0x7eb8 },
2410 { 0x06, 0x5461 },
2411 { 0x1f, 0x0003 },
2412 { 0x16, 0x0f0a },
2413 { 0x1f, 0x0000 }
2414 };
2415
françois romieu4da19632011-01-03 15:07:55 +00002416 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002417
françois romieu4da19632011-01-03 15:07:55 +00002418 rtl_patchphy(tp, 0x16, 1 << 0);
2419 rtl_patchphy(tp, 0x14, 1 << 5);
2420 rtl_patchphy(tp, 0x0d, 1 << 5);
2421 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002422}
2423
françois romieu4da19632011-01-03 15:07:55 +00002424static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002425{
françois romieu4da19632011-01-03 15:07:55 +00002426 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002427}
2428
françois romieubca03d52011-01-03 15:07:31 +00002429static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002430{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002431 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002432 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002433 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002434 { 0x06, 0x4064 },
2435 { 0x07, 0x2863 },
2436 { 0x08, 0x059c },
2437 { 0x09, 0x26b4 },
2438 { 0x0a, 0x6a19 },
2439 { 0x0b, 0xdcc8 },
2440 { 0x10, 0xf06d },
2441 { 0x14, 0x7f68 },
2442 { 0x18, 0x7fd9 },
2443 { 0x1c, 0xf0ff },
2444 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002445 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002446 { 0x12, 0xf49f },
2447 { 0x13, 0x070b },
2448 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002449 { 0x14, 0x94c0 },
2450
2451 /*
2452 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002453 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002454 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002455 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002456 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002457 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002458 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002459 { 0x06, 0x5561 },
2460
2461 /*
2462 * Can not link to 1Gbps with bad cable
2463 * Decrease SNR threshold form 21.07dB to 19.04dB
2464 */
2465 { 0x1f, 0x0001 },
2466 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002467
2468 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002469 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002470 };
françois romieubca03d52011-01-03 15:07:31 +00002471 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002472
françois romieu4da19632011-01-03 15:07:55 +00002473 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002474
françois romieubca03d52011-01-03 15:07:31 +00002475 /*
2476 * Rx Error Issue
2477 * Fine Tune Switching regulator parameter
2478 */
françois romieu4da19632011-01-03 15:07:55 +00002479 rtl_writephy(tp, 0x1f, 0x0002);
2480 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2481 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002482
françois romieudaf9df62009-10-07 12:44:20 +00002483 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002484 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002485 { 0x1f, 0x0002 },
2486 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002487 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002488 { 0x05, 0x8330 },
2489 { 0x06, 0x669a },
2490 { 0x1f, 0x0002 }
2491 };
2492 int val;
2493
françois romieu4da19632011-01-03 15:07:55 +00002494 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002495
françois romieu4da19632011-01-03 15:07:55 +00002496 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002497
2498 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002499 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002500 0x0065, 0x0066, 0x0067, 0x0068,
2501 0x0069, 0x006a, 0x006b, 0x006c
2502 };
2503 int i;
2504
françois romieu4da19632011-01-03 15:07:55 +00002505 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002506
2507 val &= 0xff00;
2508 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002509 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002510 }
2511 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002512 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002513 { 0x1f, 0x0002 },
2514 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002515 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002516 { 0x05, 0x8330 },
2517 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002518 };
2519
françois romieu4da19632011-01-03 15:07:55 +00002520 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002521 }
2522
françois romieubca03d52011-01-03 15:07:31 +00002523 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002524 rtl_writephy(tp, 0x1f, 0x0002);
2525 rtl_patchphy(tp, 0x0d, 0x0300);
2526 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002527
françois romieubca03d52011-01-03 15:07:31 +00002528 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002529 rtl_writephy(tp, 0x1f, 0x0002);
2530 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2531 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002532
françois romieu4da19632011-01-03 15:07:55 +00002533 rtl_writephy(tp, 0x1f, 0x0005);
2534 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002535
2536 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002537
françois romieu4da19632011-01-03 15:07:55 +00002538 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002539}
2540
françois romieubca03d52011-01-03 15:07:31 +00002541static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002542{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002543 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002544 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002545 { 0x1f, 0x0001 },
2546 { 0x06, 0x4064 },
2547 { 0x07, 0x2863 },
2548 { 0x08, 0x059c },
2549 { 0x09, 0x26b4 },
2550 { 0x0a, 0x6a19 },
2551 { 0x0b, 0xdcc8 },
2552 { 0x10, 0xf06d },
2553 { 0x14, 0x7f68 },
2554 { 0x18, 0x7fd9 },
2555 { 0x1c, 0xf0ff },
2556 { 0x1d, 0x3d9c },
2557 { 0x1f, 0x0003 },
2558 { 0x12, 0xf49f },
2559 { 0x13, 0x070b },
2560 { 0x1a, 0x05ad },
2561 { 0x14, 0x94c0 },
2562
françois romieubca03d52011-01-03 15:07:31 +00002563 /*
2564 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002565 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002566 */
françois romieudaf9df62009-10-07 12:44:20 +00002567 { 0x1f, 0x0002 },
2568 { 0x06, 0x5561 },
2569 { 0x1f, 0x0005 },
2570 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002571 { 0x06, 0x5561 },
2572
2573 /*
2574 * Can not link to 1Gbps with bad cable
2575 * Decrease SNR threshold form 21.07dB to 19.04dB
2576 */
2577 { 0x1f, 0x0001 },
2578 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002579
2580 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002581 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002582 };
françois romieubca03d52011-01-03 15:07:31 +00002583 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002584
françois romieu4da19632011-01-03 15:07:55 +00002585 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002586
2587 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002588 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002589 { 0x1f, 0x0002 },
2590 { 0x05, 0x669a },
2591 { 0x1f, 0x0005 },
2592 { 0x05, 0x8330 },
2593 { 0x06, 0x669a },
2594
2595 { 0x1f, 0x0002 }
2596 };
2597 int val;
2598
françois romieu4da19632011-01-03 15:07:55 +00002599 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002600
françois romieu4da19632011-01-03 15:07:55 +00002601 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002602 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002603 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002604 0x0065, 0x0066, 0x0067, 0x0068,
2605 0x0069, 0x006a, 0x006b, 0x006c
2606 };
2607 int i;
2608
françois romieu4da19632011-01-03 15:07:55 +00002609 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002610
2611 val &= 0xff00;
2612 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002613 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002614 }
2615 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002616 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002617 { 0x1f, 0x0002 },
2618 { 0x05, 0x2642 },
2619 { 0x1f, 0x0005 },
2620 { 0x05, 0x8330 },
2621 { 0x06, 0x2642 }
2622 };
2623
françois romieu4da19632011-01-03 15:07:55 +00002624 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002625 }
2626
françois romieubca03d52011-01-03 15:07:31 +00002627 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002628 rtl_writephy(tp, 0x1f, 0x0002);
2629 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2630 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002631
françois romieubca03d52011-01-03 15:07:31 +00002632 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002633 rtl_writephy(tp, 0x1f, 0x0002);
2634 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002635
françois romieu4da19632011-01-03 15:07:55 +00002636 rtl_writephy(tp, 0x1f, 0x0005);
2637 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002638
2639 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002640
françois romieu4da19632011-01-03 15:07:55 +00002641 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002642}
2643
françois romieu4da19632011-01-03 15:07:55 +00002644static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002645{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002646 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002647 { 0x1f, 0x0002 },
2648 { 0x10, 0x0008 },
2649 { 0x0d, 0x006c },
2650
2651 { 0x1f, 0x0000 },
2652 { 0x0d, 0xf880 },
2653
2654 { 0x1f, 0x0001 },
2655 { 0x17, 0x0cc0 },
2656
2657 { 0x1f, 0x0001 },
2658 { 0x0b, 0xa4d8 },
2659 { 0x09, 0x281c },
2660 { 0x07, 0x2883 },
2661 { 0x0a, 0x6b35 },
2662 { 0x1d, 0x3da4 },
2663 { 0x1c, 0xeffd },
2664 { 0x14, 0x7f52 },
2665 { 0x18, 0x7fc6 },
2666 { 0x08, 0x0601 },
2667 { 0x06, 0x4063 },
2668 { 0x10, 0xf074 },
2669 { 0x1f, 0x0003 },
2670 { 0x13, 0x0789 },
2671 { 0x12, 0xf4bd },
2672 { 0x1a, 0x04fd },
2673 { 0x14, 0x84b0 },
2674 { 0x1f, 0x0000 },
2675 { 0x00, 0x9200 },
2676
2677 { 0x1f, 0x0005 },
2678 { 0x01, 0x0340 },
2679 { 0x1f, 0x0001 },
2680 { 0x04, 0x4000 },
2681 { 0x03, 0x1d21 },
2682 { 0x02, 0x0c32 },
2683 { 0x01, 0x0200 },
2684 { 0x00, 0x5554 },
2685 { 0x04, 0x4800 },
2686 { 0x04, 0x4000 },
2687 { 0x04, 0xf000 },
2688 { 0x03, 0xdf01 },
2689 { 0x02, 0xdf20 },
2690 { 0x01, 0x101a },
2691 { 0x00, 0xa0ff },
2692 { 0x04, 0xf800 },
2693 { 0x04, 0xf000 },
2694 { 0x1f, 0x0000 },
2695
2696 { 0x1f, 0x0007 },
2697 { 0x1e, 0x0023 },
2698 { 0x16, 0x0000 },
2699 { 0x1f, 0x0000 }
2700 };
2701
françois romieu4da19632011-01-03 15:07:55 +00002702 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002703}
2704
françois romieue6de30d2011-01-03 15:08:37 +00002705static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2706{
2707 static const struct phy_reg phy_reg_init[] = {
2708 { 0x1f, 0x0001 },
2709 { 0x17, 0x0cc0 },
2710
2711 { 0x1f, 0x0007 },
2712 { 0x1e, 0x002d },
2713 { 0x18, 0x0040 },
2714 { 0x1f, 0x0000 }
2715 };
2716
2717 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2718 rtl_patchphy(tp, 0x0d, 1 << 5);
2719}
2720
Hayes Wang70090422011-07-06 15:58:06 +08002721static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
hayeswang01dc7fe2011-03-21 01:50:28 +00002722{
2723 static const struct phy_reg phy_reg_init[] = {
2724 /* Enable Delay cap */
2725 { 0x1f, 0x0005 },
2726 { 0x05, 0x8b80 },
2727 { 0x06, 0xc896 },
2728 { 0x1f, 0x0000 },
2729
2730 /* Channel estimation fine tune */
2731 { 0x1f, 0x0001 },
2732 { 0x0b, 0x6c20 },
2733 { 0x07, 0x2872 },
2734 { 0x1c, 0xefff },
2735 { 0x1f, 0x0003 },
2736 { 0x14, 0x6420 },
2737 { 0x1f, 0x0000 },
2738
2739 /* Update PFM & 10M TX idle timer */
2740 { 0x1f, 0x0007 },
2741 { 0x1e, 0x002f },
2742 { 0x15, 0x1919 },
2743 { 0x1f, 0x0000 },
2744
2745 { 0x1f, 0x0007 },
2746 { 0x1e, 0x00ac },
2747 { 0x18, 0x0006 },
2748 { 0x1f, 0x0000 }
2749 };
2750
Francois Romieu15ecd032011-04-27 13:52:22 -07002751 rtl_apply_firmware(tp);
2752
hayeswang01dc7fe2011-03-21 01:50:28 +00002753 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2754
2755 /* DCO enable for 10M IDLE Power */
2756 rtl_writephy(tp, 0x1f, 0x0007);
2757 rtl_writephy(tp, 0x1e, 0x0023);
2758 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2759 rtl_writephy(tp, 0x1f, 0x0000);
2760
2761 /* For impedance matching */
2762 rtl_writephy(tp, 0x1f, 0x0002);
2763 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002764 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002765
2766 /* PHY auto speed down */
2767 rtl_writephy(tp, 0x1f, 0x0007);
2768 rtl_writephy(tp, 0x1e, 0x002d);
2769 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2770 rtl_writephy(tp, 0x1f, 0x0000);
2771 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2772
2773 rtl_writephy(tp, 0x1f, 0x0005);
2774 rtl_writephy(tp, 0x05, 0x8b86);
2775 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2776 rtl_writephy(tp, 0x1f, 0x0000);
2777
2778 rtl_writephy(tp, 0x1f, 0x0005);
2779 rtl_writephy(tp, 0x05, 0x8b85);
2780 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2781 rtl_writephy(tp, 0x1f, 0x0007);
2782 rtl_writephy(tp, 0x1e, 0x0020);
2783 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2784 rtl_writephy(tp, 0x1f, 0x0006);
2785 rtl_writephy(tp, 0x00, 0x5a00);
2786 rtl_writephy(tp, 0x1f, 0x0000);
2787 rtl_writephy(tp, 0x0d, 0x0007);
2788 rtl_writephy(tp, 0x0e, 0x003c);
2789 rtl_writephy(tp, 0x0d, 0x4007);
2790 rtl_writephy(tp, 0x0e, 0x0000);
2791 rtl_writephy(tp, 0x0d, 0x0000);
2792}
2793
Hayes Wang70090422011-07-06 15:58:06 +08002794static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2795{
2796 static const struct phy_reg phy_reg_init[] = {
2797 /* Enable Delay cap */
2798 { 0x1f, 0x0004 },
2799 { 0x1f, 0x0007 },
2800 { 0x1e, 0x00ac },
2801 { 0x18, 0x0006 },
2802 { 0x1f, 0x0002 },
2803 { 0x1f, 0x0000 },
2804 { 0x1f, 0x0000 },
2805
2806 /* Channel estimation fine tune */
2807 { 0x1f, 0x0003 },
2808 { 0x09, 0xa20f },
2809 { 0x1f, 0x0000 },
2810 { 0x1f, 0x0000 },
2811
2812 /* Green Setting */
2813 { 0x1f, 0x0005 },
2814 { 0x05, 0x8b5b },
2815 { 0x06, 0x9222 },
2816 { 0x05, 0x8b6d },
2817 { 0x06, 0x8000 },
2818 { 0x05, 0x8b76 },
2819 { 0x06, 0x8000 },
2820 { 0x1f, 0x0000 }
2821 };
2822
2823 rtl_apply_firmware(tp);
2824
2825 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2826
2827 /* For 4-corner performance improve */
2828 rtl_writephy(tp, 0x1f, 0x0005);
2829 rtl_writephy(tp, 0x05, 0x8b80);
2830 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2831 rtl_writephy(tp, 0x1f, 0x0000);
2832
2833 /* PHY auto speed down */
2834 rtl_writephy(tp, 0x1f, 0x0004);
2835 rtl_writephy(tp, 0x1f, 0x0007);
2836 rtl_writephy(tp, 0x1e, 0x002d);
2837 rtl_w1w0_phy(tp, 0x18, 0x0010, 0x0000);
2838 rtl_writephy(tp, 0x1f, 0x0002);
2839 rtl_writephy(tp, 0x1f, 0x0000);
2840 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2841
2842 /* improve 10M EEE waveform */
2843 rtl_writephy(tp, 0x1f, 0x0005);
2844 rtl_writephy(tp, 0x05, 0x8b86);
2845 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2846 rtl_writephy(tp, 0x1f, 0x0000);
2847
2848 /* Improve 2-pair detection performance */
2849 rtl_writephy(tp, 0x1f, 0x0005);
2850 rtl_writephy(tp, 0x05, 0x8b85);
2851 rtl_w1w0_phy(tp, 0x06, 0x4000, 0x0000);
2852 rtl_writephy(tp, 0x1f, 0x0000);
2853
2854 /* EEE setting */
2855 rtl_w1w0_eri(tp->mmio_addr, 0x1b0, ERIAR_MASK_1111, 0x0000, 0x0003,
2856 ERIAR_EXGMAC);
2857 rtl_writephy(tp, 0x1f, 0x0005);
2858 rtl_writephy(tp, 0x05, 0x8b85);
2859 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2860 rtl_writephy(tp, 0x1f, 0x0004);
2861 rtl_writephy(tp, 0x1f, 0x0007);
2862 rtl_writephy(tp, 0x1e, 0x0020);
2863 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x0100);
2864 rtl_writephy(tp, 0x1f, 0x0002);
2865 rtl_writephy(tp, 0x1f, 0x0000);
2866 rtl_writephy(tp, 0x0d, 0x0007);
2867 rtl_writephy(tp, 0x0e, 0x003c);
2868 rtl_writephy(tp, 0x0d, 0x4007);
2869 rtl_writephy(tp, 0x0e, 0x0000);
2870 rtl_writephy(tp, 0x0d, 0x0000);
2871
2872 /* Green feature */
2873 rtl_writephy(tp, 0x1f, 0x0003);
2874 rtl_w1w0_phy(tp, 0x19, 0x0000, 0x0001);
2875 rtl_w1w0_phy(tp, 0x10, 0x0000, 0x0400);
2876 rtl_writephy(tp, 0x1f, 0x0000);
2877}
2878
françois romieu4da19632011-01-03 15:07:55 +00002879static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002880{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002881 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002882 { 0x1f, 0x0003 },
2883 { 0x08, 0x441d },
2884 { 0x01, 0x9100 },
2885 { 0x1f, 0x0000 }
2886 };
2887
françois romieu4da19632011-01-03 15:07:55 +00002888 rtl_writephy(tp, 0x1f, 0x0000);
2889 rtl_patchphy(tp, 0x11, 1 << 12);
2890 rtl_patchphy(tp, 0x19, 1 << 13);
2891 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002892
françois romieu4da19632011-01-03 15:07:55 +00002893 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002894}
2895
Hayes Wang5a5e4442011-02-22 17:26:21 +08002896static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2897{
2898 static const struct phy_reg phy_reg_init[] = {
2899 { 0x1f, 0x0005 },
2900 { 0x1a, 0x0000 },
2901 { 0x1f, 0x0000 },
2902
2903 { 0x1f, 0x0004 },
2904 { 0x1c, 0x0000 },
2905 { 0x1f, 0x0000 },
2906
2907 { 0x1f, 0x0001 },
2908 { 0x15, 0x7701 },
2909 { 0x1f, 0x0000 }
2910 };
2911
2912 /* Disable ALDPS before ram code */
2913 rtl_writephy(tp, 0x1f, 0x0000);
2914 rtl_writephy(tp, 0x18, 0x0310);
2915 msleep(100);
2916
François Romieu953a12c2011-04-24 17:38:48 +02002917 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002918
2919 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2920}
2921
Francois Romieu5615d9f2007-08-17 17:50:46 +02002922static void rtl_hw_phy_config(struct net_device *dev)
2923{
2924 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002925
2926 rtl8169_print_mac_version(tp);
2927
2928 switch (tp->mac_version) {
2929 case RTL_GIGA_MAC_VER_01:
2930 break;
2931 case RTL_GIGA_MAC_VER_02:
2932 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002933 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002934 break;
2935 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002936 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002937 break;
françois romieu2e9558562009-08-10 19:44:19 +00002938 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002939 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002940 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002941 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002942 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002943 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002944 case RTL_GIGA_MAC_VER_07:
2945 case RTL_GIGA_MAC_VER_08:
2946 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002947 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002948 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002949 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002950 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002951 break;
2952 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002953 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002954 break;
2955 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002956 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002957 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002958 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002959 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002960 break;
2961 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002962 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002963 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002964 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002965 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002966 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002967 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002968 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002969 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002970 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002971 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002972 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002973 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002974 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002975 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002976 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002977 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002978 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002979 break;
2980 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002981 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002982 break;
2983 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002984 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002985 break;
françois romieue6de30d2011-01-03 15:08:37 +00002986 case RTL_GIGA_MAC_VER_28:
2987 rtl8168d_4_hw_phy_config(tp);
2988 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002989 case RTL_GIGA_MAC_VER_29:
2990 case RTL_GIGA_MAC_VER_30:
2991 rtl8105e_hw_phy_config(tp);
2992 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002993 case RTL_GIGA_MAC_VER_31:
2994 /* None. */
2995 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002996 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002997 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08002998 rtl8168e_1_hw_phy_config(tp);
2999 break;
3000 case RTL_GIGA_MAC_VER_34:
3001 rtl8168e_2_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00003002 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02003003
Francois Romieu5615d9f2007-08-17 17:50:46 +02003004 default:
3005 break;
3006 }
3007}
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009static void rtl8169_phy_timer(unsigned long __opaque)
3010{
3011 struct net_device *dev = (struct net_device *)__opaque;
3012 struct rtl8169_private *tp = netdev_priv(dev);
3013 struct timer_list *timer = &tp->timer;
3014 void __iomem *ioaddr = tp->mmio_addr;
3015 unsigned long timeout = RTL8169_PHY_TIMEOUT;
3016
Francois Romieubcf0bf92006-07-26 23:14:13 +02003017 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 spin_lock_irq(&tp->lock);
3020
françois romieu4da19632011-01-03 15:07:55 +00003021 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02003022 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 * A busy loop could burn quite a few cycles on nowadays CPU.
3024 * Let's delay the execution of the timer for a few ticks.
3025 */
3026 timeout = HZ/10;
3027 goto out_mod_timer;
3028 }
3029
3030 if (tp->link_ok(ioaddr))
3031 goto out_unlock;
3032
Joe Perchesbf82c182010-02-09 11:49:50 +00003033 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
françois romieu4da19632011-01-03 15:07:55 +00003035 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037out_mod_timer:
3038 mod_timer(timer, jiffies + timeout);
3039out_unlock:
3040 spin_unlock_irq(&tp->lock);
3041}
3042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043#ifdef CONFIG_NET_POLL_CONTROLLER
3044/*
3045 * Polling 'interrupt' - used by things like netconsole to send skbs
3046 * without having to re-enable interrupts. It's not called while
3047 * the interrupt routine is executing.
3048 */
3049static void rtl8169_netpoll(struct net_device *dev)
3050{
3051 struct rtl8169_private *tp = netdev_priv(dev);
3052 struct pci_dev *pdev = tp->pci_dev;
3053
3054 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01003055 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 enable_irq(pdev->irq);
3057}
3058#endif
3059
3060static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
3061 void __iomem *ioaddr)
3062{
3063 iounmap(ioaddr);
3064 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003065 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 pci_disable_device(pdev);
3067 free_netdev(dev);
3068}
3069
Francois Romieubf793292006-11-01 00:53:05 +01003070static void rtl8169_phy_reset(struct net_device *dev,
3071 struct rtl8169_private *tp)
3072{
Francois Romieu07d3f512007-02-21 22:40:46 +01003073 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01003074
françois romieu4da19632011-01-03 15:07:55 +00003075 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01003076 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00003077 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01003078 return;
3079 msleep(1);
3080 }
Joe Perchesbf82c182010-02-09 11:49:50 +00003081 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01003082}
3083
David S. Miller8decf862011-09-22 03:23:13 -04003084static bool rtl_tbi_enabled(struct rtl8169_private *tp)
3085{
3086 void __iomem *ioaddr = tp->mmio_addr;
3087
3088 return (tp->mac_version == RTL_GIGA_MAC_VER_01) &&
3089 (RTL_R8(PHYstatus) & TBI_Enable);
3090}
3091
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003092static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003094 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003095
Francois Romieu5615d9f2007-08-17 17:50:46 +02003096 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003097
Marcus Sundberg773328942008-07-10 21:28:08 +02003098 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
3099 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3100 RTL_W8(0x82, 0x01);
3101 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003102
Francois Romieu6dccd162007-02-13 23:38:05 +01003103 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
3104
3105 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
3106 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003107
Francois Romieubcf0bf92006-07-26 23:14:13 +02003108 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003109 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
3110 RTL_W8(0x82, 0x01);
3111 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00003112 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003113 }
3114
Francois Romieubf793292006-11-01 00:53:05 +01003115 rtl8169_phy_reset(dev, tp);
3116
Oliver Neukum54405cd2011-01-06 21:55:13 +01003117 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02003118 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
3119 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
3120 (tp->mii.supports_gmii ?
3121 ADVERTISED_1000baseT_Half |
3122 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003123
David S. Miller8decf862011-09-22 03:23:13 -04003124 if (rtl_tbi_enabled(tp))
Joe Perchesbf82c182010-02-09 11:49:50 +00003125 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003126}
3127
Francois Romieu773d2022007-01-31 23:47:43 +01003128static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
3129{
3130 void __iomem *ioaddr = tp->mmio_addr;
3131 u32 high;
3132 u32 low;
3133
3134 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
3135 high = addr[4] | (addr[5] << 8);
3136
3137 spin_lock_irq(&tp->lock);
3138
3139 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00003140
Francois Romieu773d2022007-01-31 23:47:43 +01003141 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00003142 RTL_R32(MAC4);
3143
Francois Romieu78f1cd02010-03-27 19:35:46 -07003144 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00003145 RTL_R32(MAC0);
3146
françois romieuc28aa382011-08-02 03:53:43 +00003147 if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
3148 const struct exgmac_reg e[] = {
3149 { .addr = 0xe0, ERIAR_MASK_1111, .val = low },
3150 { .addr = 0xe4, ERIAR_MASK_1111, .val = high },
3151 { .addr = 0xf0, ERIAR_MASK_1111, .val = low << 16 },
3152 { .addr = 0xf4, ERIAR_MASK_1111, .val = high << 16 |
3153 low >> 16 },
3154 };
3155
3156 rtl_write_exgmac_batch(ioaddr, e, ARRAY_SIZE(e));
3157 }
3158
Francois Romieu773d2022007-01-31 23:47:43 +01003159 RTL_W8(Cfg9346, Cfg9346_Lock);
3160
3161 spin_unlock_irq(&tp->lock);
3162}
3163
3164static int rtl_set_mac_address(struct net_device *dev, void *p)
3165{
3166 struct rtl8169_private *tp = netdev_priv(dev);
3167 struct sockaddr *addr = p;
3168
3169 if (!is_valid_ether_addr(addr->sa_data))
3170 return -EADDRNOTAVAIL;
3171
3172 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3173
3174 rtl_rar_set(tp, dev->dev_addr);
3175
3176 return 0;
3177}
3178
Francois Romieu5f787a12006-08-17 13:02:36 +02003179static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3180{
3181 struct rtl8169_private *tp = netdev_priv(dev);
3182 struct mii_ioctl_data *data = if_mii(ifr);
3183
Francois Romieu8b4ab282008-11-19 22:05:25 -08003184 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3185}
Francois Romieu5f787a12006-08-17 13:02:36 +02003186
Francois Romieucecb5fd2011-04-01 10:21:07 +02003187static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3188 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003189{
Francois Romieu5f787a12006-08-17 13:02:36 +02003190 switch (cmd) {
3191 case SIOCGMIIPHY:
3192 data->phy_id = 32; /* Internal PHY */
3193 return 0;
3194
3195 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003196 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003197 return 0;
3198
3199 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003200 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003201 return 0;
3202 }
3203 return -EOPNOTSUPP;
3204}
3205
Francois Romieu8b4ab282008-11-19 22:05:25 -08003206static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3207{
3208 return -EOPNOTSUPP;
3209}
3210
Francois Romieu0e485152007-02-20 00:00:26 +01003211static const struct rtl_cfg_info {
3212 void (*hw_start)(struct net_device *);
3213 unsigned int region;
3214 unsigned int align;
3215 u16 intr_event;
3216 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003217 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003218 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003219} rtl_cfg_infos [] = {
3220 [RTL_CFG_0] = {
3221 .hw_start = rtl_hw_start_8169,
3222 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003223 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003224 .intr_event = SYSErr | LinkChg | RxOverflow |
3225 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003226 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003227 .features = RTL_FEATURE_GMII,
3228 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003229 },
3230 [RTL_CFG_1] = {
3231 .hw_start = rtl_hw_start_8168,
3232 .region = 2,
3233 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003234 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003235 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003236 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003237 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3238 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003239 },
3240 [RTL_CFG_2] = {
3241 .hw_start = rtl_hw_start_8101,
3242 .region = 2,
3243 .align = 8,
3244 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3245 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003246 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003247 .features = RTL_FEATURE_MSI,
3248 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003249 }
3250};
3251
Francois Romieufbac58f2007-10-04 22:51:38 +02003252/* Cfg9346_Unlock assumed. */
3253static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3254 const struct rtl_cfg_info *cfg)
3255{
3256 unsigned msi = 0;
3257 u8 cfg2;
3258
3259 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003260 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003261 if (pci_enable_msi(pdev)) {
3262 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3263 } else {
3264 cfg2 |= MSIEnable;
3265 msi = RTL_FEATURE_MSI;
3266 }
3267 }
3268 RTL_W8(Config2, cfg2);
3269 return msi;
3270}
3271
3272static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3273{
3274 if (tp->features & RTL_FEATURE_MSI) {
3275 pci_disable_msi(pdev);
3276 tp->features &= ~RTL_FEATURE_MSI;
3277 }
3278}
3279
Francois Romieu8b4ab282008-11-19 22:05:25 -08003280static const struct net_device_ops rtl8169_netdev_ops = {
3281 .ndo_open = rtl8169_open,
3282 .ndo_stop = rtl8169_close,
3283 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003284 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003285 .ndo_tx_timeout = rtl8169_tx_timeout,
3286 .ndo_validate_addr = eth_validate_addr,
3287 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003288 .ndo_fix_features = rtl8169_fix_features,
3289 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003290 .ndo_set_mac_address = rtl_set_mac_address,
3291 .ndo_do_ioctl = rtl8169_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003292 .ndo_set_rx_mode = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003293#ifdef CONFIG_NET_POLL_CONTROLLER
3294 .ndo_poll_controller = rtl8169_netpoll,
3295#endif
3296
3297};
3298
françois romieuc0e45c12011-01-03 15:08:04 +00003299static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3300{
3301 struct mdio_ops *ops = &tp->mdio_ops;
3302
3303 switch (tp->mac_version) {
3304 case RTL_GIGA_MAC_VER_27:
3305 ops->write = r8168dp_1_mdio_write;
3306 ops->read = r8168dp_1_mdio_read;
3307 break;
françois romieue6de30d2011-01-03 15:08:37 +00003308 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003309 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003310 ops->write = r8168dp_2_mdio_write;
3311 ops->read = r8168dp_2_mdio_read;
3312 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003313 default:
3314 ops->write = r8169_mdio_write;
3315 ops->read = r8169_mdio_read;
3316 break;
3317 }
3318}
3319
françois romieu065c27c2011-01-03 15:08:12 +00003320static void r810x_phy_power_down(struct rtl8169_private *tp)
3321{
3322 rtl_writephy(tp, 0x1f, 0x0000);
3323 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3324}
3325
3326static void r810x_phy_power_up(struct rtl8169_private *tp)
3327{
3328 rtl_writephy(tp, 0x1f, 0x0000);
3329 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3330}
3331
3332static void r810x_pll_power_down(struct rtl8169_private *tp)
3333{
David S. Miller8decf862011-09-22 03:23:13 -04003334 void __iomem *ioaddr = tp->mmio_addr;
3335
françois romieu065c27c2011-01-03 15:08:12 +00003336 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3337 rtl_writephy(tp, 0x1f, 0x0000);
3338 rtl_writephy(tp, MII_BMCR, 0x0000);
David S. Miller8decf862011-09-22 03:23:13 -04003339
3340 if (tp->mac_version == RTL_GIGA_MAC_VER_29 ||
3341 tp->mac_version == RTL_GIGA_MAC_VER_30)
3342 RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
3343 AcceptMulticast | AcceptMyPhys);
françois romieu065c27c2011-01-03 15:08:12 +00003344 return;
3345 }
3346
3347 r810x_phy_power_down(tp);
3348}
3349
3350static void r810x_pll_power_up(struct rtl8169_private *tp)
3351{
3352 r810x_phy_power_up(tp);
3353}
3354
3355static void r8168_phy_power_up(struct rtl8169_private *tp)
3356{
3357 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003358 switch (tp->mac_version) {
3359 case RTL_GIGA_MAC_VER_11:
3360 case RTL_GIGA_MAC_VER_12:
3361 case RTL_GIGA_MAC_VER_17:
3362 case RTL_GIGA_MAC_VER_18:
3363 case RTL_GIGA_MAC_VER_19:
3364 case RTL_GIGA_MAC_VER_20:
3365 case RTL_GIGA_MAC_VER_21:
3366 case RTL_GIGA_MAC_VER_22:
3367 case RTL_GIGA_MAC_VER_23:
3368 case RTL_GIGA_MAC_VER_24:
3369 case RTL_GIGA_MAC_VER_25:
3370 case RTL_GIGA_MAC_VER_26:
3371 case RTL_GIGA_MAC_VER_27:
3372 case RTL_GIGA_MAC_VER_28:
3373 case RTL_GIGA_MAC_VER_31:
3374 rtl_writephy(tp, 0x0e, 0x0000);
3375 break;
3376 default:
3377 break;
3378 }
françois romieu065c27c2011-01-03 15:08:12 +00003379 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3380}
3381
3382static void r8168_phy_power_down(struct rtl8169_private *tp)
3383{
3384 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003385 switch (tp->mac_version) {
3386 case RTL_GIGA_MAC_VER_32:
3387 case RTL_GIGA_MAC_VER_33:
3388 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3389 break;
3390
3391 case RTL_GIGA_MAC_VER_11:
3392 case RTL_GIGA_MAC_VER_12:
3393 case RTL_GIGA_MAC_VER_17:
3394 case RTL_GIGA_MAC_VER_18:
3395 case RTL_GIGA_MAC_VER_19:
3396 case RTL_GIGA_MAC_VER_20:
3397 case RTL_GIGA_MAC_VER_21:
3398 case RTL_GIGA_MAC_VER_22:
3399 case RTL_GIGA_MAC_VER_23:
3400 case RTL_GIGA_MAC_VER_24:
3401 case RTL_GIGA_MAC_VER_25:
3402 case RTL_GIGA_MAC_VER_26:
3403 case RTL_GIGA_MAC_VER_27:
3404 case RTL_GIGA_MAC_VER_28:
3405 case RTL_GIGA_MAC_VER_31:
3406 rtl_writephy(tp, 0x0e, 0x0200);
3407 default:
3408 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3409 break;
3410 }
françois romieu065c27c2011-01-03 15:08:12 +00003411}
3412
3413static void r8168_pll_power_down(struct rtl8169_private *tp)
3414{
3415 void __iomem *ioaddr = tp->mmio_addr;
3416
Francois Romieucecb5fd2011-04-01 10:21:07 +02003417 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3418 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3419 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003420 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003421 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003422 }
françois romieu065c27c2011-01-03 15:08:12 +00003423
Francois Romieucecb5fd2011-04-01 10:21:07 +02003424 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3425 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003426 (RTL_R16(CPlusCmd) & ASF)) {
3427 return;
3428 }
3429
hayeswang01dc7fe2011-03-21 01:50:28 +00003430 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3431 tp->mac_version == RTL_GIGA_MAC_VER_33)
3432 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3433
françois romieu065c27c2011-01-03 15:08:12 +00003434 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3435 rtl_writephy(tp, 0x1f, 0x0000);
3436 rtl_writephy(tp, MII_BMCR, 0x0000);
3437
Hayes Wangd4ed95d2011-07-06 15:58:07 +08003438 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
David S. Miller8decf862011-09-22 03:23:13 -04003439 tp->mac_version == RTL_GIGA_MAC_VER_33 ||
3440 tp->mac_version == RTL_GIGA_MAC_VER_34)
Hayes Wangd4ed95d2011-07-06 15:58:07 +08003441 RTL_W32(RxConfig, RTL_R32(RxConfig) | AcceptBroadcast |
3442 AcceptMulticast | AcceptMyPhys);
françois romieu065c27c2011-01-03 15:08:12 +00003443 return;
3444 }
3445
3446 r8168_phy_power_down(tp);
3447
3448 switch (tp->mac_version) {
3449 case RTL_GIGA_MAC_VER_25:
3450 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003451 case RTL_GIGA_MAC_VER_27:
3452 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003453 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003454 case RTL_GIGA_MAC_VER_32:
3455 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003456 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3457 break;
3458 }
3459}
3460
3461static void r8168_pll_power_up(struct rtl8169_private *tp)
3462{
3463 void __iomem *ioaddr = tp->mmio_addr;
3464
Francois Romieucecb5fd2011-04-01 10:21:07 +02003465 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3466 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3467 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003468 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003469 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003470 }
françois romieu065c27c2011-01-03 15:08:12 +00003471
3472 switch (tp->mac_version) {
3473 case RTL_GIGA_MAC_VER_25:
3474 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003475 case RTL_GIGA_MAC_VER_27:
3476 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003477 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003478 case RTL_GIGA_MAC_VER_32:
3479 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003480 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3481 break;
3482 }
3483
3484 r8168_phy_power_up(tp);
3485}
3486
3487static void rtl_pll_power_op(struct rtl8169_private *tp,
3488 void (*op)(struct rtl8169_private *))
3489{
3490 if (op)
3491 op(tp);
3492}
3493
3494static void rtl_pll_power_down(struct rtl8169_private *tp)
3495{
3496 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3497}
3498
3499static void rtl_pll_power_up(struct rtl8169_private *tp)
3500{
3501 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3502}
3503
3504static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3505{
3506 struct pll_power_ops *ops = &tp->pll_power_ops;
3507
3508 switch (tp->mac_version) {
3509 case RTL_GIGA_MAC_VER_07:
3510 case RTL_GIGA_MAC_VER_08:
3511 case RTL_GIGA_MAC_VER_09:
3512 case RTL_GIGA_MAC_VER_10:
3513 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003514 case RTL_GIGA_MAC_VER_29:
3515 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003516 ops->down = r810x_pll_power_down;
3517 ops->up = r810x_pll_power_up;
3518 break;
3519
3520 case RTL_GIGA_MAC_VER_11:
3521 case RTL_GIGA_MAC_VER_12:
3522 case RTL_GIGA_MAC_VER_17:
3523 case RTL_GIGA_MAC_VER_18:
3524 case RTL_GIGA_MAC_VER_19:
3525 case RTL_GIGA_MAC_VER_20:
3526 case RTL_GIGA_MAC_VER_21:
3527 case RTL_GIGA_MAC_VER_22:
3528 case RTL_GIGA_MAC_VER_23:
3529 case RTL_GIGA_MAC_VER_24:
3530 case RTL_GIGA_MAC_VER_25:
3531 case RTL_GIGA_MAC_VER_26:
3532 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003533 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003534 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003535 case RTL_GIGA_MAC_VER_32:
3536 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08003537 case RTL_GIGA_MAC_VER_34:
françois romieu065c27c2011-01-03 15:08:12 +00003538 ops->down = r8168_pll_power_down;
3539 ops->up = r8168_pll_power_up;
3540 break;
3541
3542 default:
3543 ops->down = NULL;
3544 ops->up = NULL;
3545 break;
3546 }
3547}
3548
Hayes Wange542a222011-07-06 15:58:04 +08003549static void rtl_init_rxcfg(struct rtl8169_private *tp)
3550{
3551 void __iomem *ioaddr = tp->mmio_addr;
3552
3553 switch (tp->mac_version) {
3554 case RTL_GIGA_MAC_VER_01:
3555 case RTL_GIGA_MAC_VER_02:
3556 case RTL_GIGA_MAC_VER_03:
3557 case RTL_GIGA_MAC_VER_04:
3558 case RTL_GIGA_MAC_VER_05:
3559 case RTL_GIGA_MAC_VER_06:
3560 case RTL_GIGA_MAC_VER_10:
3561 case RTL_GIGA_MAC_VER_11:
3562 case RTL_GIGA_MAC_VER_12:
3563 case RTL_GIGA_MAC_VER_13:
3564 case RTL_GIGA_MAC_VER_14:
3565 case RTL_GIGA_MAC_VER_15:
3566 case RTL_GIGA_MAC_VER_16:
3567 case RTL_GIGA_MAC_VER_17:
3568 RTL_W32(RxConfig, RX_FIFO_THRESH | RX_DMA_BURST);
3569 break;
3570 case RTL_GIGA_MAC_VER_18:
3571 case RTL_GIGA_MAC_VER_19:
3572 case RTL_GIGA_MAC_VER_20:
3573 case RTL_GIGA_MAC_VER_21:
3574 case RTL_GIGA_MAC_VER_22:
3575 case RTL_GIGA_MAC_VER_23:
3576 case RTL_GIGA_MAC_VER_24:
3577 RTL_W32(RxConfig, RX128_INT_EN | RX_MULTI_EN | RX_DMA_BURST);
3578 break;
3579 default:
3580 RTL_W32(RxConfig, RX128_INT_EN | RX_DMA_BURST);
3581 break;
3582 }
3583}
3584
Hayes Wang92fc43b2011-07-06 15:58:03 +08003585static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3586{
3587 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3588}
3589
Francois Romieu6f43adc2011-04-29 15:05:51 +02003590static void rtl_hw_reset(struct rtl8169_private *tp)
3591{
3592 void __iomem *ioaddr = tp->mmio_addr;
3593 int i;
3594
3595 /* Soft reset the chip. */
3596 RTL_W8(ChipCmd, CmdReset);
3597
3598 /* Check that the chip has finished the reset. */
3599 for (i = 0; i < 100; i++) {
3600 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3601 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003602 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003603 }
Hayes Wang92fc43b2011-07-06 15:58:03 +08003604
3605 rtl8169_init_ring_indexes(tp);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003606}
3607
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003608static int __devinit
3609rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3610{
Francois Romieu0e485152007-02-20 00:00:26 +01003611 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3612 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003614 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003615 struct net_device *dev;
3616 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003617 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003618 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003620 if (netif_msg_drv(&debug)) {
3621 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3622 MODULENAME, RTL8169_VERSION);
3623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003626 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003627 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003628 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003629 rc = -ENOMEM;
3630 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 }
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003634 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003636 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003637 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003638 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Francois Romieuccdffb92008-07-26 14:26:06 +02003640 mii = &tp->mii;
3641 mii->dev = dev;
3642 mii->mdio_read = rtl_mdio_read;
3643 mii->mdio_write = rtl_mdio_write;
3644 mii->phy_id_mask = 0x1f;
3645 mii->reg_num_mask = 0x1f;
3646 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3647
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003648 /* disable ASPM completely as that cause random device stop working
3649 * problems as well as full system hangs for some PCIe devices users */
3650 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3651 PCIE_LINK_STATE_CLKPM);
3652
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3654 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003655 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003656 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003657 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
3659
françois romieu87aeec72010-04-26 11:42:06 +00003660 if (pci_set_mwi(pdev) < 0)
3661 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003664 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003665 netif_err(tp, probe, dev,
3666 "region #%d not an MMIO resource, aborting\n",
3667 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003669 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003673 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003674 netif_err(tp, probe, dev,
3675 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003677 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
3679
3680 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003681 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003682 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003683 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 }
3685
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003686 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
3688 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003689 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 tp->cp_cmd |= PCIDAC;
3691 dev->features |= NETIF_F_HIGHDMA;
3692 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003693 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003695 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003696 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 }
3698 }
3699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003701 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003702 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003703 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003705 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003707 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Jon Masone44daad2011-06-27 07:46:31 +00003709 if (!pci_is_pcie(pdev))
3710 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07003711
Hayes Wange542a222011-07-06 15:58:04 +08003712 /* Identify chip attached to board */
3713 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
3714
3715 rtl_init_rxcfg(tp);
3716
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003717 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Francois Romieu6f43adc2011-04-29 15:05:51 +02003719 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003721 RTL_W16(IntrStatus, 0xffff);
3722
françois romieuca52efd2009-07-24 12:34:19 +00003723 pci_set_master(pdev);
3724
Francois Romieu7a8fc772011-03-01 17:18:33 +01003725 /*
3726 * Pretend we are using VLANs; This bypasses a nasty bug where
3727 * Interrupts stop flowing on high load on 8110SCd controllers.
3728 */
3729 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3730 tp->cp_cmd |= RxVlan;
3731
françois romieuc0e45c12011-01-03 15:08:04 +00003732 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003733 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Francois Romieu85bffe62011-04-27 08:22:39 +02003737 chipset = tp->mac_version;
3738 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
Francois Romieu5d06a992006-02-23 00:47:58 +01003740 RTL_W8(Cfg9346, Cfg9346_Unlock);
3741 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3742 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003743 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3744 tp->features |= RTL_FEATURE_WOL;
3745 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3746 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003747 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003748 RTL_W8(Cfg9346, Cfg9346_Lock);
3749
David S. Miller8decf862011-09-22 03:23:13 -04003750 if (rtl_tbi_enabled(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 tp->set_speed = rtl8169_set_speed_tbi;
3752 tp->get_settings = rtl8169_gset_tbi;
3753 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3754 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3755 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003756 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 } else {
3758 tp->set_speed = rtl8169_set_speed_xmii;
3759 tp->get_settings = rtl8169_gset_xmii;
3760 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3761 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3762 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003763 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 }
3765
Francois Romieudf58ef52008-10-09 14:35:58 -07003766 spin_lock_init(&tp->lock);
3767
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003768 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 for (i = 0; i < MAC_ADDR_LEN; i++)
3770 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003771 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3775 dev->irq = pdev->irq;
3776 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003778 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Michał Mirosław350fb322011-04-08 06:35:56 +00003780 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3781 * properly for all devices */
3782 dev->features |= NETIF_F_RXCSUM |
3783 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3784
3785 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3786 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3787 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3788 NETIF_F_HIGHDMA;
3789
3790 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3791 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3792 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
3794 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003795 tp->hw_start = cfg->hw_start;
3796 tp->intr_event = cfg->intr_event;
3797 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
David S. Miller8decf862011-09-22 03:23:13 -04003799 tp->opts1_mask = (tp->mac_version != RTL_GIGA_MAC_VER_01) ?
3800 ~(RxBOVF | RxFOVF) : ~0;
3801
Francois Romieu2efa53f2007-03-09 00:00:05 +01003802 init_timer(&tp->timer);
3803 tp->timer.data = (unsigned long) dev;
3804 tp->timer.function = rtl8169_phy_timer;
3805
Francois Romieub6ffd972011-06-17 17:00:05 +02003806 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003809 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003810 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
3812 pci_set_drvdata(pdev, dev);
3813
Joe Perchesbf82c182010-02-09 11:49:50 +00003814 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003815 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003816 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Francois Romieucecb5fd2011-04-01 10:21:07 +02003818 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3819 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3820 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003821 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003822 }
françois romieub646d902011-01-03 15:08:21 +00003823
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003824 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
Alan Sternf3ec4f82010-06-08 15:23:51 -04003826 if (pci_dev_run_wake(pdev))
3827 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003828
Ivan Vecera0d672e92011-02-15 02:08:39 +00003829 netif_carrier_off(dev);
3830
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003831out:
3832 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
françois romieu87aeec72010-04-26 11:42:06 +00003834err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003835 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003836 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003837err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003838 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003839err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003840 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003841 pci_disable_device(pdev);
3842err_out_free_dev_1:
3843 free_netdev(dev);
3844 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845}
3846
Francois Romieu07d3f512007-02-21 22:40:46 +01003847static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848{
3849 struct net_device *dev = pci_get_drvdata(pdev);
3850 struct rtl8169_private *tp = netdev_priv(dev);
3851
Francois Romieucecb5fd2011-04-01 10:21:07 +02003852 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3853 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3854 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003855 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003856 }
françois romieub646d902011-01-03 15:08:21 +00003857
Tejun Heo23f333a2010-12-12 16:45:14 +01003858 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003859
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003861
François Romieu953a12c2011-04-24 17:38:48 +02003862 rtl_release_firmware(tp);
3863
Alan Sternf3ec4f82010-06-08 15:23:51 -04003864 if (pci_dev_run_wake(pdev))
3865 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003866
Ivan Veceracc098dc2009-11-29 23:12:52 -08003867 /* restore original MAC address */
3868 rtl_rar_set(tp, dev->perm_addr);
3869
Francois Romieufbac58f2007-10-04 22:51:38 +02003870 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3872 pci_set_drvdata(pdev, NULL);
3873}
3874
Francois Romieub6ffd972011-06-17 17:00:05 +02003875static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3876{
3877 struct rtl_fw *rtl_fw;
3878 const char *name;
3879 int rc = -ENOMEM;
3880
3881 name = rtl_lookup_firmware_name(tp);
3882 if (!name)
3883 goto out_no_firmware;
3884
3885 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3886 if (!rtl_fw)
3887 goto err_warn;
3888
3889 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3890 if (rc < 0)
3891 goto err_free;
3892
Francois Romieufd112f22011-06-18 00:10:29 +02003893 rc = rtl_check_firmware(tp, rtl_fw);
3894 if (rc < 0)
3895 goto err_release_firmware;
3896
Francois Romieub6ffd972011-06-17 17:00:05 +02003897 tp->rtl_fw = rtl_fw;
3898out:
3899 return;
3900
Francois Romieufd112f22011-06-18 00:10:29 +02003901err_release_firmware:
3902 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003903err_free:
3904 kfree(rtl_fw);
3905err_warn:
3906 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3907 name, rc);
3908out_no_firmware:
3909 tp->rtl_fw = NULL;
3910 goto out;
3911}
3912
François Romieu953a12c2011-04-24 17:38:48 +02003913static void rtl_request_firmware(struct rtl8169_private *tp)
3914{
Francois Romieub6ffd972011-06-17 17:00:05 +02003915 if (IS_ERR(tp->rtl_fw))
3916 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003917}
3918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919static int rtl8169_open(struct net_device *dev)
3920{
3921 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003922 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003924 int retval = -ENOMEM;
3925
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003926 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
Neil Hormanc0cd8842010-03-29 13:16:02 -07003928 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003930 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003932 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3933 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003935 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003937 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3938 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003940 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
3942 retval = rtl8169_init_ring(dev);
3943 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003944 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
David Howellsc4028952006-11-22 14:57:56 +00003946 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
Francois Romieu99f252b2007-04-02 22:59:59 +02003948 smp_mb();
3949
François Romieu953a12c2011-04-24 17:38:48 +02003950 rtl_request_firmware(tp);
3951
Francois Romieufbac58f2007-10-04 22:51:38 +02003952 retval = request_irq(dev->irq, rtl8169_interrupt,
3953 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003954 dev->name, dev);
3955 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003956 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003957
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003958 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003959
françois romieueee3a962011-01-08 02:17:26 +00003960 rtl8169_init_phy(dev, tp);
3961
Michał Mirosław350fb322011-04-08 06:35:56 +00003962 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003963
françois romieu065c27c2011-01-03 15:08:12 +00003964 rtl_pll_power_up(tp);
3965
Francois Romieu07ce4062007-02-23 23:36:39 +01003966 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003968 tp->saved_wolopts = 0;
3969 pm_runtime_put_noidle(&pdev->dev);
3970
françois romieueee3a962011-01-08 02:17:26 +00003971 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972out:
3973 return retval;
3974
François Romieu953a12c2011-04-24 17:38:48 +02003975err_release_fw_2:
3976 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003977 rtl8169_rx_clear(tp);
3978err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003979 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3980 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003981 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003982err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003983 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3984 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003985 tp->TxDescArray = NULL;
3986err_pm_runtime_put:
3987 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 goto out;
3989}
3990
Hayes Wang92fc43b2011-07-06 15:58:03 +08003991static void rtl_rx_close(struct rtl8169_private *tp)
3992{
3993 void __iomem *ioaddr = tp->mmio_addr;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003994
Francois Romieu1687b562011-07-19 17:21:29 +02003995 RTL_W32(RxConfig, RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003996}
3997
françois romieue6de30d2011-01-03 15:08:37 +00003998static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
françois romieue6de30d2011-01-03 15:08:37 +00004000 void __iomem *ioaddr = tp->mmio_addr;
4001
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 /* Disable interrupts */
4003 rtl8169_irq_mask_and_ack(ioaddr);
4004
Hayes Wang92fc43b2011-07-06 15:58:03 +08004005 rtl_rx_close(tp);
4006
Hayes Wang5d2e1952011-02-22 17:26:22 +08004007 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00004008 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
4009 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00004010 while (RTL_R8(TxPoll) & NPQ)
4011 udelay(20);
Hayes Wang70090422011-07-06 15:58:06 +08004012 } else if (tp->mac_version == RTL_GIGA_MAC_VER_34) {
David S. Miller8decf862011-09-22 03:23:13 -04004013 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
Hayes Wang70090422011-07-06 15:58:06 +08004014 while (!(RTL_R32(TxConfig) & TXCFG_EMPTY))
4015 udelay(100);
Hayes Wang92fc43b2011-07-06 15:58:03 +08004016 } else {
4017 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
4018 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00004019 }
4020
Hayes Wang92fc43b2011-07-06 15:58:03 +08004021 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022}
4023
Francois Romieu7f796d832007-06-11 23:04:41 +02004024static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004025{
4026 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu9cb427b2006-11-02 00:10:16 +01004027
4028 /* Set DMA burst size and Interframe Gap Time */
4029 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4030 (InterFrameGap << TxInterFrameGapShift));
4031}
4032
Francois Romieu07ce4062007-02-23 23:36:39 +01004033static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
4035 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Francois Romieu07ce4062007-02-23 23:36:39 +01004037 tp->hw_start(dev);
4038
Francois Romieu07ce4062007-02-23 23:36:39 +01004039 netif_start_queue(dev);
4040}
4041
Francois Romieu7f796d832007-06-11 23:04:41 +02004042static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
4043 void __iomem *ioaddr)
4044{
4045 /*
4046 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
4047 * register to be written before TxDescAddrLow to work.
4048 * Switching from MMIO to I/O access fixes the issue as well.
4049 */
4050 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004051 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004052 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07004053 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02004054}
4055
4056static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
4057{
4058 u16 cmd;
4059
4060 cmd = RTL_R16(CPlusCmd);
4061 RTL_W16(CPlusCmd, cmd);
4062 return cmd;
4063}
4064
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07004065static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02004066{
4067 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00004068 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02004069}
4070
Francois Romieu6dccd162007-02-13 23:38:05 +01004071static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
4072{
Francois Romieu37441002011-06-17 22:58:54 +02004073 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01004074 u32 mac_version;
4075 u32 clk;
4076 u32 val;
4077 } cfg2_info [] = {
4078 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
4079 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
4080 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
4081 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02004082 };
4083 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01004084 unsigned int i;
4085 u32 clk;
4086
4087 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01004088 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01004089 if ((p->mac_version == mac_version) && (p->clk == clk)) {
4090 RTL_W32(0x7c, p->val);
4091 break;
4092 }
4093 }
4094}
4095
Francois Romieu07ce4062007-02-23 23:36:39 +01004096static void rtl_hw_start_8169(struct net_device *dev)
4097{
4098 struct rtl8169_private *tp = netdev_priv(dev);
4099 void __iomem *ioaddr = tp->mmio_addr;
4100 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01004101
Francois Romieu9cb427b2006-11-02 00:10:16 +01004102 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
4103 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
4104 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
4105 }
4106
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02004108 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4109 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4110 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4111 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01004112 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4113
Hayes Wange542a222011-07-06 15:58:04 +08004114 rtl_init_rxcfg(tp);
4115
françois romieuf0298f82011-01-03 15:07:42 +00004116 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004118 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Francois Romieucecb5fd2011-04-01 10:21:07 +02004120 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
4121 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4122 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
4123 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02004124 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Francois Romieu7f796d832007-06-11 23:04:41 +02004126 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004127
Francois Romieucecb5fd2011-04-01 10:21:07 +02004128 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
4129 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02004130 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02004132 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 }
4134
Francois Romieubcf0bf92006-07-26 23:14:13 +02004135 RTL_W16(CPlusCmd, tp->cp_cmd);
4136
Francois Romieu6dccd162007-02-13 23:38:05 +01004137 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 /*
4140 * Undocumented corner. Supposedly:
4141 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
4142 */
4143 RTL_W16(IntrMitigate, 0x0000);
4144
Francois Romieu7f796d832007-06-11 23:04:41 +02004145 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01004146
Francois Romieucecb5fd2011-04-01 10:21:07 +02004147 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
4148 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
4149 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
4150 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02004151 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4152 rtl_set_rx_tx_config_registers(tp);
4153 }
4154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02004156
4157 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
4158 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
4160 RTL_W32(RxMissed, 0);
4161
Francois Romieu07ce4062007-02-23 23:36:39 +01004162 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164 /* no early-rx interrupts */
4165 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004166
4167 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01004168 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004169}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Francois Romieu9c14cea2008-07-05 00:21:15 +02004171static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02004172{
Jon Masone44daad2011-06-27 07:46:31 +00004173 int cap = pci_pcie_cap(pdev);
Francois Romieu458a9f62008-08-02 15:50:02 +02004174
Francois Romieu9c14cea2008-07-05 00:21:15 +02004175 if (cap) {
4176 u16 ctl;
4177
4178 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
4179 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
4180 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
4181 }
Francois Romieu458a9f62008-08-02 15:50:02 +02004182}
4183
françois romieu650e8d52011-01-03 15:08:29 +00004184static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02004185{
4186 u32 csi;
4187
4188 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00004189 rtl_csi_write(ioaddr, 0x070c, csi | bits);
4190}
4191
françois romieue6de30d2011-01-03 15:08:37 +00004192static void rtl_csi_access_enable_1(void __iomem *ioaddr)
4193{
4194 rtl_csi_access_enable(ioaddr, 0x17000000);
4195}
4196
françois romieu650e8d52011-01-03 15:08:29 +00004197static void rtl_csi_access_enable_2(void __iomem *ioaddr)
4198{
4199 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02004200}
4201
4202struct ephy_info {
4203 unsigned int offset;
4204 u16 mask;
4205 u16 bits;
4206};
4207
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004208static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02004209{
4210 u16 w;
4211
4212 while (len-- > 0) {
4213 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4214 rtl_ephy_write(ioaddr, e->offset, w);
4215 e++;
4216 }
4217}
4218
Francois Romieub726e492008-06-28 12:22:59 +02004219static void rtl_disable_clock_request(struct pci_dev *pdev)
4220{
Jon Masone44daad2011-06-27 07:46:31 +00004221 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004222
4223 if (cap) {
4224 u16 ctl;
4225
4226 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4227 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4228 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4229 }
4230}
4231
françois romieue6de30d2011-01-03 15:08:37 +00004232static void rtl_enable_clock_request(struct pci_dev *pdev)
4233{
Jon Masone44daad2011-06-27 07:46:31 +00004234 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004235
4236 if (cap) {
4237 u16 ctl;
4238
4239 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4240 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4241 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4242 }
4243}
4244
Francois Romieub726e492008-06-28 12:22:59 +02004245#define R8168_CPCMD_QUIRK_MASK (\
4246 EnableBist | \
4247 Mac_dbgo_oe | \
4248 Force_half_dup | \
4249 Force_rxflow_en | \
4250 Force_txflow_en | \
4251 Cxpl_dbg_sel | \
4252 ASF | \
4253 PktCntrDisable | \
4254 Mac_dbgo_sel)
4255
Francois Romieu219a1e92008-06-28 11:58:39 +02004256static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4257{
Francois Romieub726e492008-06-28 12:22:59 +02004258 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4259
4260 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4261
Francois Romieu2e68ae42008-06-28 12:00:55 +02004262 rtl_tx_performance_tweak(pdev,
4263 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004264}
4265
4266static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4267{
4268 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004269
françois romieuf0298f82011-01-03 15:07:42 +00004270 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004271
4272 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004273}
4274
4275static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4276{
Francois Romieub726e492008-06-28 12:22:59 +02004277 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4278
4279 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4280
Francois Romieu219a1e92008-06-28 11:58:39 +02004281 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004282
4283 rtl_disable_clock_request(pdev);
4284
4285 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004286}
4287
Francois Romieuef3386f2008-06-29 12:24:30 +02004288static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004289{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004290 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004291 { 0x01, 0, 0x0001 },
4292 { 0x02, 0x0800, 0x1000 },
4293 { 0x03, 0, 0x0042 },
4294 { 0x06, 0x0080, 0x0000 },
4295 { 0x07, 0, 0x2000 }
4296 };
4297
françois romieu650e8d52011-01-03 15:08:29 +00004298 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004299
4300 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4301
Francois Romieu219a1e92008-06-28 11:58:39 +02004302 __rtl_hw_start_8168cp(ioaddr, pdev);
4303}
4304
Francois Romieuef3386f2008-06-29 12:24:30 +02004305static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4306{
françois romieu650e8d52011-01-03 15:08:29 +00004307 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004308
4309 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4310
4311 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4312
4313 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4314}
4315
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004316static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4317{
françois romieu650e8d52011-01-03 15:08:29 +00004318 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004319
4320 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4321
4322 /* Magic. */
4323 RTL_W8(DBG_REG, 0x20);
4324
françois romieuf0298f82011-01-03 15:07:42 +00004325 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004326
4327 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4328
4329 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4330}
4331
Francois Romieu219a1e92008-06-28 11:58:39 +02004332static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4333{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004334 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004335 { 0x02, 0x0800, 0x1000 },
4336 { 0x03, 0, 0x0002 },
4337 { 0x06, 0x0080, 0x0000 }
4338 };
4339
françois romieu650e8d52011-01-03 15:08:29 +00004340 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004341
4342 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4343
4344 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4345
Francois Romieu219a1e92008-06-28 11:58:39 +02004346 __rtl_hw_start_8168cp(ioaddr, pdev);
4347}
4348
4349static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4350{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004351 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004352 { 0x01, 0, 0x0001 },
4353 { 0x03, 0x0400, 0x0220 }
4354 };
4355
françois romieu650e8d52011-01-03 15:08:29 +00004356 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004357
4358 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4359
Francois Romieu219a1e92008-06-28 11:58:39 +02004360 __rtl_hw_start_8168cp(ioaddr, pdev);
4361}
4362
Francois Romieu197ff762008-06-28 13:16:02 +02004363static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4364{
4365 rtl_hw_start_8168c_2(ioaddr, pdev);
4366}
4367
Francois Romieu6fb07052008-06-29 11:54:28 +02004368static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4369{
françois romieu650e8d52011-01-03 15:08:29 +00004370 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004371
4372 __rtl_hw_start_8168cp(ioaddr, pdev);
4373}
4374
Francois Romieu5b538df2008-07-20 16:22:45 +02004375static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4376{
françois romieu650e8d52011-01-03 15:08:29 +00004377 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004378
4379 rtl_disable_clock_request(pdev);
4380
françois romieuf0298f82011-01-03 15:07:42 +00004381 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004382
4383 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4384
4385 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4386}
4387
hayeswang4804b3b2011-03-21 01:50:29 +00004388static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4389{
4390 rtl_csi_access_enable_1(ioaddr);
4391
4392 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4393
4394 RTL_W8(MaxTxPacketSize, TxPacketMax);
4395
4396 rtl_disable_clock_request(pdev);
4397}
4398
françois romieue6de30d2011-01-03 15:08:37 +00004399static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4400{
4401 static const struct ephy_info e_info_8168d_4[] = {
4402 { 0x0b, ~0, 0x48 },
4403 { 0x19, 0x20, 0x50 },
4404 { 0x0c, ~0, 0x20 }
4405 };
4406 int i;
4407
4408 rtl_csi_access_enable_1(ioaddr);
4409
4410 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4411
4412 RTL_W8(MaxTxPacketSize, TxPacketMax);
4413
4414 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4415 const struct ephy_info *e = e_info_8168d_4 + i;
4416 u16 w;
4417
4418 w = rtl_ephy_read(ioaddr, e->offset);
4419 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4420 }
4421
4422 rtl_enable_clock_request(pdev);
4423}
4424
Hayes Wang70090422011-07-06 15:58:06 +08004425static void rtl_hw_start_8168e_1(void __iomem *ioaddr, struct pci_dev *pdev)
hayeswang01dc7fe2011-03-21 01:50:28 +00004426{
Hayes Wang70090422011-07-06 15:58:06 +08004427 static const struct ephy_info e_info_8168e_1[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00004428 { 0x00, 0x0200, 0x0100 },
4429 { 0x00, 0x0000, 0x0004 },
4430 { 0x06, 0x0002, 0x0001 },
4431 { 0x06, 0x0000, 0x0030 },
4432 { 0x07, 0x0000, 0x2000 },
4433 { 0x00, 0x0000, 0x0020 },
4434 { 0x03, 0x5800, 0x2000 },
4435 { 0x03, 0x0000, 0x0001 },
4436 { 0x01, 0x0800, 0x1000 },
4437 { 0x07, 0x0000, 0x4000 },
4438 { 0x1e, 0x0000, 0x2000 },
4439 { 0x19, 0xffff, 0xfe6c },
4440 { 0x0a, 0x0000, 0x0040 }
4441 };
4442
4443 rtl_csi_access_enable_2(ioaddr);
4444
Hayes Wang70090422011-07-06 15:58:06 +08004445 rtl_ephy_init(ioaddr, e_info_8168e_1, ARRAY_SIZE(e_info_8168e_1));
hayeswang01dc7fe2011-03-21 01:50:28 +00004446
4447 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4448
4449 RTL_W8(MaxTxPacketSize, TxPacketMax);
4450
4451 rtl_disable_clock_request(pdev);
4452
4453 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004454 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4455 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004456
Francois Romieucecb5fd2011-04-01 10:21:07 +02004457 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004458}
4459
Hayes Wang70090422011-07-06 15:58:06 +08004460static void rtl_hw_start_8168e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4461{
4462 static const struct ephy_info e_info_8168e_2[] = {
4463 { 0x09, 0x0000, 0x0080 },
4464 { 0x19, 0x0000, 0x0224 }
4465 };
4466
4467 rtl_csi_access_enable_1(ioaddr);
4468
4469 rtl_ephy_init(ioaddr, e_info_8168e_2, ARRAY_SIZE(e_info_8168e_2));
4470
4471 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4472
4473 rtl_eri_write(ioaddr, 0xc0, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4474 rtl_eri_write(ioaddr, 0xb8, ERIAR_MASK_0011, 0x0000, ERIAR_EXGMAC);
4475 rtl_eri_write(ioaddr, 0xc8, ERIAR_MASK_1111, 0x00100002, ERIAR_EXGMAC);
4476 rtl_eri_write(ioaddr, 0xe8, ERIAR_MASK_1111, 0x00100006, ERIAR_EXGMAC);
4477 rtl_eri_write(ioaddr, 0xcc, ERIAR_MASK_1111, 0x00000050, ERIAR_EXGMAC);
4478 rtl_eri_write(ioaddr, 0xd0, ERIAR_MASK_1111, 0x07ff0060, ERIAR_EXGMAC);
4479 rtl_w1w0_eri(ioaddr, 0x1b0, ERIAR_MASK_0001, 0x10, 0x00, ERIAR_EXGMAC);
4480 rtl_w1w0_eri(ioaddr, 0x0d4, ERIAR_MASK_0011, 0x0c00, 0xff00,
4481 ERIAR_EXGMAC);
4482
Hayes Wang3090bd92011-09-06 16:55:15 +08004483 RTL_W8(MaxTxPacketSize, EarlySize);
Hayes Wang70090422011-07-06 15:58:06 +08004484
4485 rtl_disable_clock_request(pdev);
4486
4487 RTL_W32(TxConfig, RTL_R32(TxConfig) | TXCFG_AUTO_FIFO);
4488 RTL_W8(MCU, RTL_R8(MCU) & ~NOW_IS_OOB);
4489
4490 /* Adjust EEE LED frequency */
4491 RTL_W8(EEE_LED, RTL_R8(EEE_LED) & ~0x07);
4492
4493 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
4494 RTL_W32(MISC, RTL_R32(MISC) | PWM_EN);
4495 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
4496}
4497
Francois Romieu07ce4062007-02-23 23:36:39 +01004498static void rtl_hw_start_8168(struct net_device *dev)
4499{
Francois Romieu2dd99532007-06-11 23:22:52 +02004500 struct rtl8169_private *tp = netdev_priv(dev);
4501 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004502 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004503
4504 RTL_W8(Cfg9346, Cfg9346_Unlock);
4505
françois romieuf0298f82011-01-03 15:07:42 +00004506 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004507
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004508 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004509
Francois Romieu0e485152007-02-20 00:00:26 +01004510 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004511
4512 RTL_W16(CPlusCmd, tp->cp_cmd);
4513
Francois Romieu0e485152007-02-20 00:00:26 +01004514 RTL_W16(IntrMitigate, 0x5151);
4515
4516 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004517 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4518 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004519 tp->intr_event |= RxFIFOOver | PCSTimeout;
4520 tp->intr_event &= ~RxOverflow;
4521 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004522
4523 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4524
Francois Romieub8363902008-06-01 12:31:57 +02004525 rtl_set_rx_mode(dev);
4526
4527 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4528 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004529
4530 RTL_R8(IntrMask);
4531
Francois Romieu219a1e92008-06-28 11:58:39 +02004532 switch (tp->mac_version) {
4533 case RTL_GIGA_MAC_VER_11:
4534 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004535 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004536
4537 case RTL_GIGA_MAC_VER_12:
4538 case RTL_GIGA_MAC_VER_17:
4539 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004540 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004541
4542 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004543 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004544 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004545
4546 case RTL_GIGA_MAC_VER_19:
4547 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004548 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004549
4550 case RTL_GIGA_MAC_VER_20:
4551 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004552 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004553
Francois Romieu197ff762008-06-28 13:16:02 +02004554 case RTL_GIGA_MAC_VER_21:
4555 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004556 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004557
Francois Romieu6fb07052008-06-29 11:54:28 +02004558 case RTL_GIGA_MAC_VER_22:
4559 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004560 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004561
Francois Romieuef3386f2008-06-29 12:24:30 +02004562 case RTL_GIGA_MAC_VER_23:
4563 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004564 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004565
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004566 case RTL_GIGA_MAC_VER_24:
4567 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004568 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004569
Francois Romieu5b538df2008-07-20 16:22:45 +02004570 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004571 case RTL_GIGA_MAC_VER_26:
4572 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004573 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004574 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004575
françois romieue6de30d2011-01-03 15:08:37 +00004576 case RTL_GIGA_MAC_VER_28:
4577 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004578 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004579
hayeswang4804b3b2011-03-21 01:50:29 +00004580 case RTL_GIGA_MAC_VER_31:
4581 rtl_hw_start_8168dp(ioaddr, pdev);
4582 break;
4583
hayeswang01dc7fe2011-03-21 01:50:28 +00004584 case RTL_GIGA_MAC_VER_32:
4585 case RTL_GIGA_MAC_VER_33:
Hayes Wang70090422011-07-06 15:58:06 +08004586 rtl_hw_start_8168e_1(ioaddr, pdev);
4587 break;
4588 case RTL_GIGA_MAC_VER_34:
4589 rtl_hw_start_8168e_2(ioaddr, pdev);
hayeswang01dc7fe2011-03-21 01:50:28 +00004590 break;
françois romieue6de30d2011-01-03 15:08:37 +00004591
Francois Romieu219a1e92008-06-28 11:58:39 +02004592 default:
4593 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4594 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004595 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004596 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004597
Francois Romieu0e485152007-02-20 00:00:26 +01004598 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4599
Francois Romieub8363902008-06-01 12:31:57 +02004600 RTL_W8(Cfg9346, Cfg9346_Lock);
4601
Francois Romieu2dd99532007-06-11 23:22:52 +02004602 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004603
Francois Romieu0e485152007-02-20 00:00:26 +01004604 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004605}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
Francois Romieu2857ffb2008-08-02 21:08:49 +02004607#define R810X_CPCMD_QUIRK_MASK (\
4608 EnableBist | \
4609 Mac_dbgo_oe | \
4610 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004611 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004612 Force_txflow_en | \
4613 Cxpl_dbg_sel | \
4614 ASF | \
4615 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004616 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004617
4618static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4619{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004620 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004621 { 0x01, 0, 0x6e65 },
4622 { 0x02, 0, 0x091f },
4623 { 0x03, 0, 0xc2f9 },
4624 { 0x06, 0, 0xafb5 },
4625 { 0x07, 0, 0x0e00 },
4626 { 0x19, 0, 0xec80 },
4627 { 0x01, 0, 0x2e65 },
4628 { 0x01, 0, 0x6e65 }
4629 };
4630 u8 cfg1;
4631
françois romieu650e8d52011-01-03 15:08:29 +00004632 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004633
4634 RTL_W8(DBG_REG, FIX_NAK_1);
4635
4636 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4637
4638 RTL_W8(Config1,
4639 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4640 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4641
4642 cfg1 = RTL_R8(Config1);
4643 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4644 RTL_W8(Config1, cfg1 & ~LEDS0);
4645
Francois Romieu2857ffb2008-08-02 21:08:49 +02004646 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4647}
4648
4649static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4650{
françois romieu650e8d52011-01-03 15:08:29 +00004651 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004652
4653 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4654
4655 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4656 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004657}
4658
4659static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4660{
4661 rtl_hw_start_8102e_2(ioaddr, pdev);
4662
4663 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4664}
4665
Hayes Wang5a5e4442011-02-22 17:26:21 +08004666static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4667{
4668 static const struct ephy_info e_info_8105e_1[] = {
4669 { 0x07, 0, 0x4000 },
4670 { 0x19, 0, 0x0200 },
4671 { 0x19, 0, 0x0020 },
4672 { 0x1e, 0, 0x2000 },
4673 { 0x03, 0, 0x0001 },
4674 { 0x19, 0, 0x0100 },
4675 { 0x19, 0, 0x0004 },
4676 { 0x0a, 0, 0x0020 }
4677 };
4678
Francois Romieucecb5fd2011-04-01 10:21:07 +02004679 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004680 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4681
Francois Romieucecb5fd2011-04-01 10:21:07 +02004682 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004683 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4684
4685 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004686 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004687
4688 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4689}
4690
4691static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4692{
4693 rtl_hw_start_8105e_1(ioaddr, pdev);
4694 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4695}
4696
Francois Romieu07ce4062007-02-23 23:36:39 +01004697static void rtl_hw_start_8101(struct net_device *dev)
4698{
Francois Romieucdf1a602007-06-11 23:29:50 +02004699 struct rtl8169_private *tp = netdev_priv(dev);
4700 void __iomem *ioaddr = tp->mmio_addr;
4701 struct pci_dev *pdev = tp->pci_dev;
4702
Francois Romieucecb5fd2011-04-01 10:21:07 +02004703 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4704 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004705 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004706
4707 if (cap) {
4708 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4709 PCI_EXP_DEVCTL_NOSNOOP_EN);
4710 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004711 }
4712
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004713 RTL_W8(Cfg9346, Cfg9346_Unlock);
4714
Francois Romieu2857ffb2008-08-02 21:08:49 +02004715 switch (tp->mac_version) {
4716 case RTL_GIGA_MAC_VER_07:
4717 rtl_hw_start_8102e_1(ioaddr, pdev);
4718 break;
4719
4720 case RTL_GIGA_MAC_VER_08:
4721 rtl_hw_start_8102e_3(ioaddr, pdev);
4722 break;
4723
4724 case RTL_GIGA_MAC_VER_09:
4725 rtl_hw_start_8102e_2(ioaddr, pdev);
4726 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004727
4728 case RTL_GIGA_MAC_VER_29:
4729 rtl_hw_start_8105e_1(ioaddr, pdev);
4730 break;
4731 case RTL_GIGA_MAC_VER_30:
4732 rtl_hw_start_8105e_2(ioaddr, pdev);
4733 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004734 }
4735
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004736 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004737
françois romieuf0298f82011-01-03 15:07:42 +00004738 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004739
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004740 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004741
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004742 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004743 RTL_W16(CPlusCmd, tp->cp_cmd);
4744
4745 RTL_W16(IntrMitigate, 0x0000);
4746
4747 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4748
4749 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4750 rtl_set_rx_tx_config_registers(tp);
4751
Francois Romieucdf1a602007-06-11 23:29:50 +02004752 RTL_R8(IntrMask);
4753
Francois Romieucdf1a602007-06-11 23:29:50 +02004754 rtl_set_rx_mode(dev);
4755
4756 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004757
Francois Romieu0e485152007-02-20 00:00:26 +01004758 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759}
4760
4761static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4762{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4764 return -EINVAL;
4765
4766 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004767 netdev_update_features(dev);
4768
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770}
4771
4772static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4773{
Al Viro95e09182007-12-22 18:55:39 +00004774 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4776}
4777
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004778static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4779 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004781 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004782 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004783
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004784 kfree(*data_buff);
4785 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 rtl8169_make_unusable_by_asic(desc);
4787}
4788
4789static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4790{
4791 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4792
4793 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4794}
4795
4796static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4797 u32 rx_buf_sz)
4798{
4799 desc->addr = cpu_to_le64(mapping);
4800 wmb();
4801 rtl8169_mark_to_asic(desc, rx_buf_sz);
4802}
4803
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004804static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004806 return (void *)ALIGN((long)data, 16);
4807}
4808
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004809static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4810 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004811{
4812 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004814 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004815 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004816 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004818 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4819 if (!data)
4820 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004821
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004822 if (rtl8169_align(data) != data) {
4823 kfree(data);
4824 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4825 if (!data)
4826 return NULL;
4827 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004828
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004829 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004830 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004831 if (unlikely(dma_mapping_error(d, mapping))) {
4832 if (net_ratelimit())
4833 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004834 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004838 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004839
4840err_out:
4841 kfree(data);
4842 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843}
4844
4845static void rtl8169_rx_clear(struct rtl8169_private *tp)
4846{
Francois Romieu07d3f512007-02-21 22:40:46 +01004847 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
4849 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004850 if (tp->Rx_databuff[i]) {
4851 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 tp->RxDescArray + i);
4853 }
4854 }
4855}
4856
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004857static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004859 desc->opts1 |= cpu_to_le32(RingEnd);
4860}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004861
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004862static int rtl8169_rx_fill(struct rtl8169_private *tp)
4863{
4864 unsigned int i;
4865
4866 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004867 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004868
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004869 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004871
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004872 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004873 if (!data) {
4874 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004875 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004876 }
4877 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004880 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4881 return 0;
4882
4883err_out:
4884 rtl8169_rx_clear(tp);
4885 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
4887
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888static int rtl8169_init_ring(struct net_device *dev)
4889{
4890 struct rtl8169_private *tp = netdev_priv(dev);
4891
4892 rtl8169_init_ring_indexes(tp);
4893
4894 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004895 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004897 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898}
4899
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004900static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 struct TxDesc *desc)
4902{
4903 unsigned int len = tx_skb->len;
4904
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004905 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 desc->opts1 = 0x00;
4908 desc->opts2 = 0x00;
4909 desc->addr = 0x00;
4910 tx_skb->len = 0;
4911}
4912
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004913static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4914 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915{
4916 unsigned int i;
4917
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004918 for (i = 0; i < n; i++) {
4919 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 struct ring_info *tx_skb = tp->tx_skb + entry;
4921 unsigned int len = tx_skb->len;
4922
4923 if (len) {
4924 struct sk_buff *skb = tx_skb->skb;
4925
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004926 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 tp->TxDescArray + entry);
4928 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004929 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 dev_kfree_skb(skb);
4931 tx_skb->skb = NULL;
4932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 }
4934 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004935}
4936
4937static void rtl8169_tx_clear(struct rtl8169_private *tp)
4938{
4939 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 tp->cur_tx = tp->dirty_tx = 0;
4941}
4942
David Howellsc4028952006-11-22 14:57:56 +00004943static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944{
4945 struct rtl8169_private *tp = netdev_priv(dev);
4946
David Howellsc4028952006-11-22 14:57:56 +00004947 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 schedule_delayed_work(&tp->task, 4);
4949}
4950
4951static void rtl8169_wait_for_quiescence(struct net_device *dev)
4952{
4953 struct rtl8169_private *tp = netdev_priv(dev);
4954 void __iomem *ioaddr = tp->mmio_addr;
4955
4956 synchronize_irq(dev->irq);
4957
4958 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004959 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 rtl8169_irq_mask_and_ack(ioaddr);
4962
David S. Millerd1d08d12008-01-07 20:53:33 -08004963 tp->intr_mask = 0xffff;
4964 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004965 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966}
4967
David Howellsc4028952006-11-22 14:57:56 +00004968static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
David Howellsc4028952006-11-22 14:57:56 +00004970 struct rtl8169_private *tp =
4971 container_of(work, struct rtl8169_private, task.work);
4972 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 int ret;
4974
Francois Romieueb2a0212007-02-15 23:37:21 +01004975 rtnl_lock();
4976
4977 if (!netif_running(dev))
4978 goto out_unlock;
4979
4980 rtl8169_wait_for_quiescence(dev);
4981 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
4983 ret = rtl8169_open(dev);
4984 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004985 if (net_ratelimit())
4986 netif_err(tp, drv, dev,
4987 "reinit failure (status = %d). Rescheduling\n",
4988 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4990 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004991
4992out_unlock:
4993 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994}
4995
David Howellsc4028952006-11-22 14:57:56 +00004996static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997{
David Howellsc4028952006-11-22 14:57:56 +00004998 struct rtl8169_private *tp =
4999 container_of(work, struct rtl8169_private, task.work);
5000 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01005001 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
Francois Romieueb2a0212007-02-15 23:37:21 +01005003 rtnl_lock();
5004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01005006 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
5008 rtl8169_wait_for_quiescence(dev);
5009
Francois Romieu56de4142011-03-15 17:29:31 +01005010 for (i = 0; i < NUM_RX_DESC; i++)
5011 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
5012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 rtl8169_tx_clear(tp);
5014
Hayes Wang92fc43b2011-07-06 15:58:03 +08005015 rtl8169_hw_reset(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01005016 rtl_hw_start(dev);
5017 netif_wake_queue(dev);
5018 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01005019
5020out_unlock:
5021 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022}
5023
5024static void rtl8169_tx_timeout(struct net_device *dev)
5025{
5026 struct rtl8169_private *tp = netdev_priv(dev);
5027
françois romieue6de30d2011-01-03 15:08:37 +00005028 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 /* Let's wait a bit while any (async) irq lands on */
5031 rtl8169_schedule_work(dev, rtl8169_reset_task);
5032}
5033
5034static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07005035 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036{
5037 struct skb_shared_info *info = skb_shinfo(skb);
5038 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04005039 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005040 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
5042 entry = tp->cur_tx;
5043 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
5044 skb_frag_t *frag = info->frags + cur_frag;
5045 dma_addr_t mapping;
5046 u32 status, len;
5047 void *addr;
5048
5049 entry = (entry + 1) % NUM_TX_DESC;
5050
5051 txd = tp->TxDescArray + entry;
5052 len = frag->size;
Ian Campbell929f6182011-08-31 00:47:06 +00005053 addr = skb_frag_address(frag);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005054 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005055 if (unlikely(dma_mapping_error(d, mapping))) {
5056 if (net_ratelimit())
5057 netif_err(tp, drv, tp->dev,
5058 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005059 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
Francois Romieucecb5fd2011-04-01 10:21:07 +02005062 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005063 status = opts[0] | len |
5064 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
5066 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07005067 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 txd->addr = cpu_to_le64(mapping);
5069
5070 tp->tx_skb[entry].len = len;
5071 }
5072
5073 if (cur_frag) {
5074 tp->tx_skb[entry].skb = skb;
5075 txd->opts1 |= cpu_to_le32(LastFrag);
5076 }
5077
5078 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005079
5080err_out:
5081 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
5082 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083}
5084
Francois Romieu2b7b4312011-04-18 22:53:24 -07005085static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
5086 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087{
Francois Romieu2b7b4312011-04-18 22:53:24 -07005088 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00005089 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005090 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
Francois Romieu2b7b4312011-04-18 22:53:24 -07005092 if (mss) {
5093 opts[0] |= TD_LSO;
5094 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
5095 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005096 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
5098 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005099 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005101 opts[offset] |= info->checksum.udp;
5102 else
5103 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105}
5106
Stephen Hemminger613573252009-08-31 19:50:58 +00005107static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
5108 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109{
5110 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005111 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 struct TxDesc *txd = tp->TxDescArray + entry;
5113 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005114 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 dma_addr_t mapping;
5116 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07005117 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005118 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02005119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005121 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005122 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 }
5124
5125 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005126 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005128 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005129 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005130 if (unlikely(dma_mapping_error(d, mapping))) {
5131 if (net_ratelimit())
5132 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005133 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00005134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
5136 tp->tx_skb[entry].len = len;
5137 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
Francois Romieu2b7b4312011-04-18 22:53:24 -07005139 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
5140 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005141
Francois Romieu2b7b4312011-04-18 22:53:24 -07005142 rtl8169_tso_csum(tp, skb, opts);
5143
5144 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005145 if (frags < 0)
5146 goto err_dma_1;
5147 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07005148 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005149 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07005150 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005151 tp->tx_skb[entry].skb = skb;
5152 }
5153
Francois Romieu2b7b4312011-04-18 22:53:24 -07005154 txd->opts2 = cpu_to_le32(opts[1]);
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 wmb();
5157
Francois Romieucecb5fd2011-04-01 10:21:07 +02005158 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07005159 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 txd->opts1 = cpu_to_le32(status);
5161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 tp->cur_tx += frags + 1;
5163
David Dillow4c020a92010-03-03 16:33:10 +00005164 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
Francois Romieucecb5fd2011-04-01 10:21:07 +02005166 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
5168 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
5169 netif_stop_queue(dev);
5170 smp_rmb();
5171 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
5172 netif_wake_queue(dev);
5173 }
5174
Stephen Hemminger613573252009-08-31 19:50:58 +00005175 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005177err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005178 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00005179err_dma_0:
5180 dev_kfree_skb(skb);
5181 dev->stats.tx_dropped++;
5182 return NETDEV_TX_OK;
5183
5184err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005186 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00005187 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188}
5189
5190static void rtl8169_pcierr_interrupt(struct net_device *dev)
5191{
5192 struct rtl8169_private *tp = netdev_priv(dev);
5193 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 u16 pci_status, pci_cmd;
5195
5196 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
5197 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
5198
Joe Perchesbf82c182010-02-09 11:49:50 +00005199 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
5200 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
5202 /*
5203 * The recovery sequence below admits a very elaborated explanation:
5204 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01005205 * - I did not see what else could be done;
5206 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 *
5208 * Feel free to adjust to your needs.
5209 */
Francois Romieua27993f2006-12-18 00:04:19 +01005210 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01005211 pci_cmd &= ~PCI_COMMAND_PARITY;
5212 else
5213 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
5214
5215 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
5217 pci_write_config_word(pdev, PCI_STATUS,
5218 pci_status & (PCI_STATUS_DETECTED_PARITY |
5219 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
5220 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
5221
5222 /* The infamous DAC f*ckup only happens at boot time */
5223 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00005224 void __iomem *ioaddr = tp->mmio_addr;
5225
Joe Perchesbf82c182010-02-09 11:49:50 +00005226 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 tp->cp_cmd &= ~PCIDAC;
5228 RTL_W16(CPlusCmd, tp->cp_cmd);
5229 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 }
5231
françois romieue6de30d2011-01-03 15:08:37 +00005232 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01005233
5234 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235}
5236
Francois Romieu07d3f512007-02-21 22:40:46 +01005237static void rtl8169_tx_interrupt(struct net_device *dev,
5238 struct rtl8169_private *tp,
5239 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240{
5241 unsigned int dirty_tx, tx_left;
5242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 dirty_tx = tp->dirty_tx;
5244 smp_rmb();
5245 tx_left = tp->cur_tx - dirty_tx;
5246
5247 while (tx_left > 0) {
5248 unsigned int entry = dirty_tx % NUM_TX_DESC;
5249 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 u32 status;
5251
5252 rmb();
5253 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5254 if (status & DescOwn)
5255 break;
5256
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005257 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5258 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005260 dev->stats.tx_packets++;
5261 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005262 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 tx_skb->skb = NULL;
5264 }
5265 dirty_tx++;
5266 tx_left--;
5267 }
5268
5269 if (tp->dirty_tx != dirty_tx) {
5270 tp->dirty_tx = dirty_tx;
5271 smp_wmb();
5272 if (netif_queue_stopped(dev) &&
5273 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5274 netif_wake_queue(dev);
5275 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005276 /*
5277 * 8168 hack: TxPoll requests are lost when the Tx packets are
5278 * too close. Let's kick an extra TxPoll request when a burst
5279 * of start_xmit activity is detected (if it is not detected,
5280 * it is slow enough). -- FR
5281 */
5282 smp_rmb();
5283 if (tp->cur_tx != dirty_tx)
5284 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 }
5286}
5287
Francois Romieu126fa4b2005-05-12 20:09:17 -04005288static inline int rtl8169_fragmented_frame(u32 status)
5289{
5290 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5291}
5292
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005293static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 u32 status = opts1 & RxProtoMask;
5296
5297 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005298 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 skb->ip_summed = CHECKSUM_UNNECESSARY;
5300 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005301 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302}
5303
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005304static struct sk_buff *rtl8169_try_rx_copy(void *data,
5305 struct rtl8169_private *tp,
5306 int pkt_size,
5307 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005309 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005310 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005312 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005313 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005314 prefetch(data);
5315 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5316 if (skb)
5317 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005318 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5319
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005320 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321}
5322
Francois Romieu07d3f512007-02-21 22:40:46 +01005323static int rtl8169_rx_interrupt(struct net_device *dev,
5324 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005325 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326{
5327 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005328 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 cur_rx = tp->cur_rx;
5331 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005332 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005334 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005336 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 u32 status;
5338
5339 rmb();
David S. Miller8decf862011-09-22 03:23:13 -04005340 status = le32_to_cpu(desc->opts1) & tp->opts1_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
5342 if (status & DescOwn)
5343 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005344 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005345 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5346 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005347 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005349 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005351 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005352 if (status & RxFOVF) {
5353 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005354 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005355 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005356 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005358 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005359 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
Francois Romieu126fa4b2005-05-12 20:09:17 -04005362 /*
5363 * The driver does not support incoming fragmented
5364 * frames. They are seen as a symptom of over-mtu
5365 * sized frames.
5366 */
5367 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005368 dev->stats.rx_dropped++;
5369 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005370 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005371 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005372 }
5373
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005374 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5375 tp, pkt_size, addr);
5376 rtl8169_mark_to_asic(desc, rx_buf_sz);
5377 if (!skb) {
5378 dev->stats.rx_dropped++;
5379 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 }
5381
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005382 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 skb_put(skb, pkt_size);
5384 skb->protocol = eth_type_trans(skb, dev);
5385
Francois Romieu7a8fc772011-03-01 17:18:33 +01005386 rtl8169_rx_vlan_tag(desc, skb);
5387
Francois Romieu56de4142011-03-15 17:29:31 +01005388 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Francois Romieucebf8cc2007-10-18 12:06:54 +02005390 dev->stats.rx_bytes += pkt_size;
5391 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005393
5394 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005395 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005396 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5397 desc->opts2 = 0;
5398 cur_rx++;
5399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 }
5401
5402 count = cur_rx - tp->cur_rx;
5403 tp->cur_rx = cur_rx;
5404
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005405 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407 return count;
5408}
5409
Francois Romieu07d3f512007-02-21 22:40:46 +01005410static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411{
Francois Romieu07d3f512007-02-21 22:40:46 +01005412 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005416 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
David Dillowf11a3772009-05-22 15:29:34 +00005418 /* loop handling interrupts until we have no new ones or
5419 * we hit a invalid/hotplug case.
5420 */
Francois Romieu865c6522008-05-11 14:51:00 +02005421 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005422 while (status && status != 0xffff) {
5423 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
David Dillowf11a3772009-05-22 15:29:34 +00005425 /* Handle all of the error cases first. These will reset
5426 * the chip, so just exit the loop.
5427 */
5428 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005429 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 }
David Dillowf11a3772009-05-22 15:29:34 +00005432
Francois Romieu1519e572011-02-03 12:02:36 +01005433 if (unlikely(status & RxFIFOOver)) {
5434 switch (tp->mac_version) {
5435 /* Work around for rx fifo overflow */
5436 case RTL_GIGA_MAC_VER_11:
5437 case RTL_GIGA_MAC_VER_22:
5438 case RTL_GIGA_MAC_VER_26:
5439 netif_stop_queue(dev);
5440 rtl8169_tx_timeout(dev);
5441 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005442 /* Testers needed. */
5443 case RTL_GIGA_MAC_VER_17:
5444 case RTL_GIGA_MAC_VER_19:
5445 case RTL_GIGA_MAC_VER_20:
5446 case RTL_GIGA_MAC_VER_21:
5447 case RTL_GIGA_MAC_VER_23:
5448 case RTL_GIGA_MAC_VER_24:
5449 case RTL_GIGA_MAC_VER_27:
5450 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005451 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005452 /* Experimental science. Pktgen proof. */
5453 case RTL_GIGA_MAC_VER_12:
5454 case RTL_GIGA_MAC_VER_25:
5455 if (status == RxFIFOOver)
5456 goto done;
5457 break;
5458 default:
5459 break;
5460 }
David Dillowf11a3772009-05-22 15:29:34 +00005461 }
5462
5463 if (unlikely(status & SYSErr)) {
5464 rtl8169_pcierr_interrupt(dev);
5465 break;
5466 }
5467
5468 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005469 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005470
5471 /* We need to see the lastest version of tp->intr_mask to
5472 * avoid ignoring an MSI interrupt and having to wait for
5473 * another event which may never come.
5474 */
5475 smp_rmb();
5476 if (status & tp->intr_mask & tp->napi_event) {
5477 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5478 tp->intr_mask = ~tp->napi_event;
5479
5480 if (likely(napi_schedule_prep(&tp->napi)))
5481 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005482 else
5483 netif_info(tp, intr, dev,
5484 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005485 }
5486
5487 /* We only get a new MSI interrupt when all active irq
5488 * sources on the chip have been acknowledged. So, ack
5489 * everything we've seen and check if new sources have become
5490 * active to avoid blocking all interrupts from the chip.
5491 */
5492 RTL_W16(IntrStatus,
5493 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5494 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 }
Francois Romieu1519e572011-02-03 12:02:36 +01005496done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 return IRQ_RETVAL(handled);
5498}
5499
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005500static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005502 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5503 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005505 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005507 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 rtl8169_tx_interrupt(dev, tp, ioaddr);
5509
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005510 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005511 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005512
5513 /* We need for force the visibility of tp->intr_mask
5514 * for other CPUs, as we can loose an MSI interrupt
5515 * and potentially wait for a retransmit timeout if we don't.
5516 * The posted write to IntrMask is safe, as it will
5517 * eventually make it to the chip and we won't loose anything
5518 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 */
David Dillowf11a3772009-05-22 15:29:34 +00005520 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005521 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005522 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 }
5524
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005525 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Francois Romieu523a6092008-09-10 22:28:56 +02005528static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5529{
5530 struct rtl8169_private *tp = netdev_priv(dev);
5531
5532 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5533 return;
5534
5535 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5536 RTL_W32(RxMissed, 0);
5537}
5538
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539static void rtl8169_down(struct net_device *dev)
5540{
5541 struct rtl8169_private *tp = netdev_priv(dev);
5542 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
Francois Romieu4876cc12011-03-11 21:07:11 +01005544 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
5546 netif_stop_queue(dev);
5547
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005548 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005549
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 spin_lock_irq(&tp->lock);
5551
Hayes Wang92fc43b2011-07-06 15:58:03 +08005552 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005553 /*
5554 * At this point device interrupts can not be enabled in any function,
5555 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5556 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5557 */
Francois Romieu523a6092008-09-10 22:28:56 +02005558 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560 spin_unlock_irq(&tp->lock);
5561
5562 synchronize_irq(dev->irq);
5563
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005565 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 rtl8169_tx_clear(tp);
5568
5569 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005570
5571 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572}
5573
5574static int rtl8169_close(struct net_device *dev)
5575{
5576 struct rtl8169_private *tp = netdev_priv(dev);
5577 struct pci_dev *pdev = tp->pci_dev;
5578
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005579 pm_runtime_get_sync(&pdev->dev);
5580
Francois Romieucecb5fd2011-04-01 10:21:07 +02005581 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005582 rtl8169_update_counters(dev);
5583
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 rtl8169_down(dev);
5585
5586 free_irq(dev->irq, dev);
5587
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005588 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5589 tp->RxPhyAddr);
5590 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5591 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 tp->TxDescArray = NULL;
5593 tp->RxDescArray = NULL;
5594
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005595 pm_runtime_put_sync(&pdev->dev);
5596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 return 0;
5598}
5599
Francois Romieu07ce4062007-02-23 23:36:39 +01005600static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601{
5602 struct rtl8169_private *tp = netdev_priv(dev);
5603 void __iomem *ioaddr = tp->mmio_addr;
5604 unsigned long flags;
5605 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005606 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 u32 tmp = 0;
5608
5609 if (dev->flags & IFF_PROMISC) {
5610 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005611 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 rx_mode =
5613 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5614 AcceptAllPhys;
5615 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005616 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005617 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 /* Too many to filter perfectly -- accept all multicasts. */
5619 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5620 mc_filter[1] = mc_filter[0] = 0xffffffff;
5621 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005622 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005623
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 rx_mode = AcceptBroadcast | AcceptMyPhys;
5625 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005626 netdev_for_each_mc_addr(ha, dev) {
5627 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5629 rx_mode |= AcceptMulticast;
5630 }
5631 }
5632
5633 spin_lock_irqsave(&tp->lock, flags);
5634
Francois Romieu1687b562011-07-19 17:21:29 +02005635 tmp = (RTL_R32(RxConfig) & ~RX_CONFIG_ACCEPT_MASK) | rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Francois Romieuf887cce2008-07-17 22:24:18 +02005637 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005638 u32 data = mc_filter[0];
5639
5640 mc_filter[0] = swab32(mc_filter[1]);
5641 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005642 }
5643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005645 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Francois Romieu57a9f232007-06-04 22:10:15 +02005647 RTL_W32(RxConfig, tmp);
5648
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 spin_unlock_irqrestore(&tp->lock, flags);
5650}
5651
5652/**
5653 * rtl8169_get_stats - Get rtl8169 read/write statistics
5654 * @dev: The Ethernet Device to get statistics for
5655 *
5656 * Get TX/RX statistics for rtl8169
5657 */
5658static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5659{
5660 struct rtl8169_private *tp = netdev_priv(dev);
5661 void __iomem *ioaddr = tp->mmio_addr;
5662 unsigned long flags;
5663
5664 if (netif_running(dev)) {
5665 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005666 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 spin_unlock_irqrestore(&tp->lock, flags);
5668 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005669
Francois Romieucebf8cc2007-10-18 12:06:54 +02005670 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671}
5672
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005673static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005674{
françois romieu065c27c2011-01-03 15:08:12 +00005675 struct rtl8169_private *tp = netdev_priv(dev);
5676
Francois Romieu5d06a992006-02-23 00:47:58 +01005677 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005678 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005679
françois romieu065c27c2011-01-03 15:08:12 +00005680 rtl_pll_power_down(tp);
5681
Francois Romieu5d06a992006-02-23 00:47:58 +01005682 netif_device_detach(dev);
5683 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005684}
Francois Romieu5d06a992006-02-23 00:47:58 +01005685
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005686#ifdef CONFIG_PM
5687
5688static int rtl8169_suspend(struct device *device)
5689{
5690 struct pci_dev *pdev = to_pci_dev(device);
5691 struct net_device *dev = pci_get_drvdata(pdev);
5692
5693 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005694
Francois Romieu5d06a992006-02-23 00:47:58 +01005695 return 0;
5696}
5697
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005698static void __rtl8169_resume(struct net_device *dev)
5699{
françois romieu065c27c2011-01-03 15:08:12 +00005700 struct rtl8169_private *tp = netdev_priv(dev);
5701
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005702 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005703
5704 rtl_pll_power_up(tp);
5705
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005706 rtl8169_schedule_work(dev, rtl8169_reset_task);
5707}
5708
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005709static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005710{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005711 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005712 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005713 struct rtl8169_private *tp = netdev_priv(dev);
5714
5715 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005716
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005717 if (netif_running(dev))
5718 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005719
Francois Romieu5d06a992006-02-23 00:47:58 +01005720 return 0;
5721}
5722
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005723static int rtl8169_runtime_suspend(struct device *device)
5724{
5725 struct pci_dev *pdev = to_pci_dev(device);
5726 struct net_device *dev = pci_get_drvdata(pdev);
5727 struct rtl8169_private *tp = netdev_priv(dev);
5728
5729 if (!tp->TxDescArray)
5730 return 0;
5731
5732 spin_lock_irq(&tp->lock);
5733 tp->saved_wolopts = __rtl8169_get_wol(tp);
5734 __rtl8169_set_wol(tp, WAKE_ANY);
5735 spin_unlock_irq(&tp->lock);
5736
5737 rtl8169_net_suspend(dev);
5738
5739 return 0;
5740}
5741
5742static int rtl8169_runtime_resume(struct device *device)
5743{
5744 struct pci_dev *pdev = to_pci_dev(device);
5745 struct net_device *dev = pci_get_drvdata(pdev);
5746 struct rtl8169_private *tp = netdev_priv(dev);
5747
5748 if (!tp->TxDescArray)
5749 return 0;
5750
5751 spin_lock_irq(&tp->lock);
5752 __rtl8169_set_wol(tp, tp->saved_wolopts);
5753 tp->saved_wolopts = 0;
5754 spin_unlock_irq(&tp->lock);
5755
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005756 rtl8169_init_phy(dev, tp);
5757
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005758 __rtl8169_resume(dev);
5759
5760 return 0;
5761}
5762
5763static int rtl8169_runtime_idle(struct device *device)
5764{
5765 struct pci_dev *pdev = to_pci_dev(device);
5766 struct net_device *dev = pci_get_drvdata(pdev);
5767 struct rtl8169_private *tp = netdev_priv(dev);
5768
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005769 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005770}
5771
Alexey Dobriyan47145212009-12-14 18:00:08 -08005772static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005773 .suspend = rtl8169_suspend,
5774 .resume = rtl8169_resume,
5775 .freeze = rtl8169_suspend,
5776 .thaw = rtl8169_resume,
5777 .poweroff = rtl8169_suspend,
5778 .restore = rtl8169_resume,
5779 .runtime_suspend = rtl8169_runtime_suspend,
5780 .runtime_resume = rtl8169_runtime_resume,
5781 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005782};
5783
5784#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5785
5786#else /* !CONFIG_PM */
5787
5788#define RTL8169_PM_OPS NULL
5789
5790#endif /* !CONFIG_PM */
5791
Francois Romieu1765f952008-09-13 17:21:40 +02005792static void rtl_shutdown(struct pci_dev *pdev)
5793{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005794 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005795 struct rtl8169_private *tp = netdev_priv(dev);
5796 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005797
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005798 rtl8169_net_suspend(dev);
5799
Francois Romieucecb5fd2011-04-01 10:21:07 +02005800 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005801 rtl_rar_set(tp, dev->perm_addr);
5802
françois romieu4bb3f522009-06-17 11:41:45 +00005803 spin_lock_irq(&tp->lock);
5804
Hayes Wang92fc43b2011-07-06 15:58:03 +08005805 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005806
5807 spin_unlock_irq(&tp->lock);
5808
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005809 if (system_state == SYSTEM_POWER_OFF) {
Hayes Wangaaa89c02011-07-06 15:58:08 +08005810 /* WoL fails with 8168b when the receiver is disabled. */
5811 if ((tp->mac_version == RTL_GIGA_MAC_VER_11 ||
5812 tp->mac_version == RTL_GIGA_MAC_VER_12 ||
5813 tp->mac_version == RTL_GIGA_MAC_VER_17) &&
5814 (tp->features & RTL_FEATURE_WOL)) {
françois romieuca52efd2009-07-24 12:34:19 +00005815 pci_clear_master(pdev);
5816
5817 RTL_W8(ChipCmd, CmdRxEnb);
5818 /* PCI commit */
5819 RTL_R8(ChipCmd);
5820 }
5821
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005822 pci_wake_from_d3(pdev, true);
5823 pci_set_power_state(pdev, PCI_D3hot);
5824 }
5825}
Francois Romieu5d06a992006-02-23 00:47:58 +01005826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827static struct pci_driver rtl8169_pci_driver = {
5828 .name = MODULENAME,
5829 .id_table = rtl8169_pci_tbl,
5830 .probe = rtl8169_init_one,
5831 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005832 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005833 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834};
5835
Francois Romieu07d3f512007-02-21 22:40:46 +01005836static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837{
Jeff Garzik29917622006-08-19 17:48:59 -04005838 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839}
5840
Francois Romieu07d3f512007-02-21 22:40:46 +01005841static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842{
5843 pci_unregister_driver(&rtl8169_pci_driver);
5844}
5845
5846module_init(rtl8169_init_module);
5847module_exit(rtl8169_cleanup_module);