blob: a983eaaaf2e3797e6030655b0ff4f551d511ccac [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 Wang5a5e4442011-02-22 17:26:21 +080044#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#ifdef RTL8169_DEBUG
47#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020048 if (!(expr)) { \
49 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070050 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020051 }
Joe Perches06fa7352007-10-18 21:15:00 +020052#define dprintk(fmt, args...) \
53 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#else
55#define assert(expr) do {} while (0)
56#define dprintk(fmt, args...) do {} while (0)
57#endif /* RTL8169_DEBUG */
58
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070060 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define TX_BUFFS_AVAIL(tp) \
63 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
66 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050067static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* MAC address length */
70#define MAC_ADDR_LEN 6
71
Francois Romieu9c14cea2008-07-05 00:21:15 +020072#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76
77#define R8169_REGS_SIZE 256
78#define R8169_NAPI_WEIGHT 64
79#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81#define RX_BUF_SIZE 1536 /* Rx Buffer size */
82#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
84
85#define RTL8169_TX_TIMEOUT (6*HZ)
86#define RTL8169_PHY_TIMEOUT (10*HZ)
87
françois romieuea8dbdd2009-03-15 01:10:50 +000088#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020090#define RTL_EEPROM_SIG_ADDR 0x0000
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* write/read MMIO register */
93#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96#define RTL_R8(reg) readb (ioaddr + (reg))
97#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000098#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200101 RTL_GIGA_MAC_VER_01 = 0,
102 RTL_GIGA_MAC_VER_02,
103 RTL_GIGA_MAC_VER_03,
104 RTL_GIGA_MAC_VER_04,
105 RTL_GIGA_MAC_VER_05,
106 RTL_GIGA_MAC_VER_06,
107 RTL_GIGA_MAC_VER_07,
108 RTL_GIGA_MAC_VER_08,
109 RTL_GIGA_MAC_VER_09,
110 RTL_GIGA_MAC_VER_10,
111 RTL_GIGA_MAC_VER_11,
112 RTL_GIGA_MAC_VER_12,
113 RTL_GIGA_MAC_VER_13,
114 RTL_GIGA_MAC_VER_14,
115 RTL_GIGA_MAC_VER_15,
116 RTL_GIGA_MAC_VER_16,
117 RTL_GIGA_MAC_VER_17,
118 RTL_GIGA_MAC_VER_18,
119 RTL_GIGA_MAC_VER_19,
120 RTL_GIGA_MAC_VER_20,
121 RTL_GIGA_MAC_VER_21,
122 RTL_GIGA_MAC_VER_22,
123 RTL_GIGA_MAC_VER_23,
124 RTL_GIGA_MAC_VER_24,
125 RTL_GIGA_MAC_VER_25,
126 RTL_GIGA_MAC_VER_26,
127 RTL_GIGA_MAC_VER_27,
128 RTL_GIGA_MAC_VER_28,
129 RTL_GIGA_MAC_VER_29,
130 RTL_GIGA_MAC_VER_30,
131 RTL_GIGA_MAC_VER_31,
132 RTL_GIGA_MAC_VER_32,
133 RTL_GIGA_MAC_VER_33,
134 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Francois Romieu2b7b4312011-04-18 22:53:24 -0700137enum rtl_tx_desc_version {
138 RTL_TD_0 = 0,
139 RTL_TD_1 = 1,
140};
141
Francois Romieu85bffe62011-04-27 08:22:39 +0200142#define _R(NAME,TD,FW) \
143 { .name = NAME, .txd_version = TD, .fw_name = FW }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800145static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700147 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200148 const char *fw_name;
149} rtl_chip_infos[] = {
150 /* PCI devices. */
151 [RTL_GIGA_MAC_VER_01] =
152 _R("RTL8169", RTL_TD_0, NULL),
153 [RTL_GIGA_MAC_VER_02] =
154 _R("RTL8169s", RTL_TD_0, NULL),
155 [RTL_GIGA_MAC_VER_03] =
156 _R("RTL8110s", RTL_TD_0, NULL),
157 [RTL_GIGA_MAC_VER_04] =
158 _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
159 [RTL_GIGA_MAC_VER_05] =
160 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
161 [RTL_GIGA_MAC_VER_06] =
162 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
163 /* PCI-E devices. */
164 [RTL_GIGA_MAC_VER_07] =
165 _R("RTL8102e", RTL_TD_1, NULL),
166 [RTL_GIGA_MAC_VER_08] =
167 _R("RTL8102e", RTL_TD_1, NULL),
168 [RTL_GIGA_MAC_VER_09] =
169 _R("RTL8102e", RTL_TD_1, NULL),
170 [RTL_GIGA_MAC_VER_10] =
171 _R("RTL8101e", RTL_TD_0, NULL),
172 [RTL_GIGA_MAC_VER_11] =
173 _R("RTL8168b/8111b", RTL_TD_0, NULL),
174 [RTL_GIGA_MAC_VER_12] =
175 _R("RTL8168b/8111b", RTL_TD_0, NULL),
176 [RTL_GIGA_MAC_VER_13] =
177 _R("RTL8101e", RTL_TD_0, NULL),
178 [RTL_GIGA_MAC_VER_14] =
179 _R("RTL8100e", RTL_TD_0, NULL),
180 [RTL_GIGA_MAC_VER_15] =
181 _R("RTL8100e", RTL_TD_0, NULL),
182 [RTL_GIGA_MAC_VER_16] =
183 _R("RTL8101e", RTL_TD_0, NULL),
184 [RTL_GIGA_MAC_VER_17] =
185 _R("RTL8168b/8111b", RTL_TD_0, NULL),
186 [RTL_GIGA_MAC_VER_18] =
187 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
188 [RTL_GIGA_MAC_VER_19] =
189 _R("RTL8168c/8111c", RTL_TD_1, NULL),
190 [RTL_GIGA_MAC_VER_20] =
191 _R("RTL8168c/8111c", RTL_TD_1, NULL),
192 [RTL_GIGA_MAC_VER_21] =
193 _R("RTL8168c/8111c", RTL_TD_1, NULL),
194 [RTL_GIGA_MAC_VER_22] =
195 _R("RTL8168c/8111c", RTL_TD_1, NULL),
196 [RTL_GIGA_MAC_VER_23] =
197 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
198 [RTL_GIGA_MAC_VER_24] =
199 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
200 [RTL_GIGA_MAC_VER_25] =
201 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
202 [RTL_GIGA_MAC_VER_26] =
203 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
204 [RTL_GIGA_MAC_VER_27] =
205 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
206 [RTL_GIGA_MAC_VER_28] =
207 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
208 [RTL_GIGA_MAC_VER_29] =
209 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
210 [RTL_GIGA_MAC_VER_30] =
211 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
212 [RTL_GIGA_MAC_VER_31] =
213 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
214 [RTL_GIGA_MAC_VER_32] =
215 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
216 [RTL_GIGA_MAC_VER_33] =
217 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219#undef _R
220
Francois Romieubcf0bf92006-07-26 23:14:13 +0200221enum cfg_version {
222 RTL_CFG_0 = 0x00,
223 RTL_CFG_1,
224 RTL_CFG_2
225};
226
Francois Romieu07ce4062007-02-23 23:36:39 +0100227static void rtl_hw_start_8169(struct net_device *);
228static void rtl_hw_start_8168(struct net_device *);
229static void rtl_hw_start_8101(struct net_device *);
230
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000231static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200232 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200233 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200234 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100235 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200236 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
237 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200238 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200239 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
240 { PCI_VENDOR_ID_LINKSYS, 0x1032,
241 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100242 { 0x0001, 0x8168,
243 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 {0,},
245};
246
247MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
248
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000249static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700250static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200251static struct {
252 u32 msg_enable;
253} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Francois Romieu07d3f512007-02-21 22:40:46 +0100255enum rtl_registers {
256 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100257 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100258 MAR0 = 8, /* Multicast filter. */
259 CounterAddrLow = 0x10,
260 CounterAddrHigh = 0x14,
261 TxDescStartAddrLow = 0x20,
262 TxDescStartAddrHigh = 0x24,
263 TxHDescStartAddrLow = 0x28,
264 TxHDescStartAddrHigh = 0x2c,
265 FLASH = 0x30,
266 ERSR = 0x36,
267 ChipCmd = 0x37,
268 TxPoll = 0x38,
269 IntrMask = 0x3c,
270 IntrStatus = 0x3e,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700271
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800272 TxConfig = 0x40,
273#define TXCFG_AUTO_FIFO (1 << 7) /* 8111e-vl */
274#define TXCFG_EMPTY (1 << 11) /* 8111e-vl */
275
276 RxConfig = 0x44,
277#define RX128_INT_EN (1 << 15) /* 8111c and later */
278#define RX_MULTI_EN (1 << 14) /* 8111c only */
279#define RXCFG_FIFO_SHIFT 13
280 /* No threshold before first PCI xfer */
281#define RX_FIFO_THRESH (7 << RXCFG_FIFO_SHIFT)
282#define RXCFG_DMA_SHIFT 8
283 /* Unlimited maximum PCI burst. */
284#define RX_DMA_BURST (7 << RXCFG_DMA_SHIFT)
Francois Romieu2b7b4312011-04-18 22:53:24 -0700285#define RTL_RX_CONFIG_MASK 0xff7e1880u
286
Francois Romieu07d3f512007-02-21 22:40:46 +0100287 RxMissed = 0x4c,
288 Cfg9346 = 0x50,
289 Config0 = 0x51,
290 Config1 = 0x52,
291 Config2 = 0x53,
292 Config3 = 0x54,
293 Config4 = 0x55,
294 Config5 = 0x56,
295 MultiIntr = 0x5c,
296 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100297 PHYstatus = 0x6c,
298 RxMaxSize = 0xda,
299 CPlusCmd = 0xe0,
300 IntrMitigate = 0xe2,
301 RxDescAddrLow = 0xe4,
302 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000303 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
304
305#define NoEarlyTx 0x3f /* Max value : no early transmit. */
306
307 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
308
309#define TxPacketMax (8064 >> 7)
310
Francois Romieu07d3f512007-02-21 22:40:46 +0100311 FuncEvent = 0xf0,
312 FuncEventMask = 0xf4,
313 FuncPresetState = 0xf8,
314 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
Francois Romieuf162a5d2008-06-01 22:37:49 +0200317enum rtl8110_registers {
318 TBICSR = 0x64,
319 TBI_ANAR = 0x68,
320 TBI_LPAR = 0x6a,
321};
322
323enum rtl8168_8101_registers {
324 CSIDR = 0x64,
325 CSIAR = 0x68,
326#define CSIAR_FLAG 0x80000000
327#define CSIAR_WRITE_CMD 0x80000000
328#define CSIAR_BYTE_ENABLE 0x0f
329#define CSIAR_BYTE_ENABLE_SHIFT 12
330#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000331 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200332 EPHYAR = 0x80,
333#define EPHYAR_FLAG 0x80000000
334#define EPHYAR_WRITE_CMD 0x80000000
335#define EPHYAR_REG_MASK 0x1f
336#define EPHYAR_REG_SHIFT 16
337#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800338 DLLPR = 0xd0,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800339#define PFM_EN (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200340 DBG_REG = 0xd1,
341#define FIX_NAK_1 (1 << 4)
342#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800343 TWSI = 0xd2,
344 MCU = 0xd3,
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800345#define NOW_IS_OOB (1 << 7)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800346#define EN_NDP (1 << 3)
347#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000348 EFUSEAR = 0xdc,
349#define EFUSEAR_FLAG 0x80000000
350#define EFUSEAR_WRITE_CMD 0x80000000
351#define EFUSEAR_READ_CMD 0x00000000
352#define EFUSEAR_REG_MASK 0x03ff
353#define EFUSEAR_REG_SHIFT 8
354#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200355};
356
françois romieuc0e45c12011-01-03 15:08:04 +0000357enum rtl8168_registers {
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800358 LED_FREQ = 0x1a,
359 EEE_LED = 0x1b,
françois romieub646d902011-01-03 15:08:21 +0000360 ERIDR = 0x70,
361 ERIAR = 0x74,
362#define ERIAR_FLAG 0x80000000
363#define ERIAR_WRITE_CMD 0x80000000
364#define ERIAR_READ_CMD 0x00000000
365#define ERIAR_ADDR_BYTE_ALIGN 4
françois romieub646d902011-01-03 15:08:21 +0000366#define ERIAR_TYPE_SHIFT 16
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800367#define ERIAR_EXGMAC (0x00 << ERIAR_TYPE_SHIFT)
368#define ERIAR_MSIX (0x01 << ERIAR_TYPE_SHIFT)
369#define ERIAR_ASF (0x02 << ERIAR_TYPE_SHIFT)
370#define ERIAR_MASK_SHIFT 12
371#define ERIAR_MASK_0001 (0x1 << ERIAR_MASK_SHIFT)
372#define ERIAR_MASK_0011 (0x3 << ERIAR_MASK_SHIFT)
373#define ERIAR_MASK_1111 (0xf << ERIAR_MASK_SHIFT)
françois romieuc0e45c12011-01-03 15:08:04 +0000374 EPHY_RXER_NUM = 0x7c,
375 OCPDR = 0xb0, /* OCP GPHY access */
376#define OCPDR_WRITE_CMD 0x80000000
377#define OCPDR_READ_CMD 0x00000000
378#define OCPDR_REG_MASK 0x7f
379#define OCPDR_GPHY_REG_SHIFT 16
380#define OCPDR_DATA_MASK 0xffff
381 OCPAR = 0xb4,
382#define OCPAR_FLAG 0x80000000
383#define OCPAR_GPHY_WRITE_CMD 0x8000f060
384#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000385 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
386 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200387#define TXPLA_RST (1 << 29)
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800388#define PWM_EN (1 << 22)
françois romieuc0e45c12011-01-03 15:08:04 +0000389};
390
Francois Romieu07d3f512007-02-21 22:40:46 +0100391enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100393 SYSErr = 0x8000,
394 PCSTimeout = 0x4000,
395 SWInt = 0x0100,
396 TxDescUnavail = 0x0080,
397 RxFIFOOver = 0x0040,
398 LinkChg = 0x0020,
399 RxOverflow = 0x0010,
400 TxErr = 0x0008,
401 TxOK = 0x0004,
402 RxErr = 0x0002,
403 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200406 RxFOVF = (1 << 23),
407 RxRWT = (1 << 22),
408 RxRES = (1 << 21),
409 RxRUNT = (1 << 20),
410 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* ChipCmdBits */
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800413 StopReq = 0x80,
Francois Romieu07d3f512007-02-21 22:40:46 +0100414 CmdReset = 0x10,
415 CmdRxEnb = 0x08,
416 CmdTxEnb = 0x04,
417 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Francois Romieu275391a2007-02-23 23:50:28 +0100419 /* TXPoll register p.5 */
420 HPQ = 0x80, /* Poll cmd on the high prio queue */
421 NPQ = 0x40, /* Poll cmd on the low prio queue */
422 FSWInt = 0x01, /* Forced software interrupt */
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100425 Cfg9346_Lock = 0x00,
426 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100429 AcceptErr = 0x20,
430 AcceptRunt = 0x10,
431 AcceptBroadcast = 0x08,
432 AcceptMulticast = 0x04,
433 AcceptMyPhys = 0x02,
434 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* TxConfigBits */
437 TxInterFrameGapShift = 24,
438 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
439
Francois Romieu5d06a992006-02-23 00:47:58 +0100440 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200441 LEDS1 = (1 << 7),
442 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200443 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200444 Speed_down = (1 << 4),
445 MEMMAP = (1 << 3),
446 IOMAP = (1 << 2),
447 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100448 PMEnable = (1 << 0), /* Power Management Enable */
449
Francois Romieu6dccd162007-02-13 23:38:05 +0100450 /* Config2 register p. 25 */
451 PCI_Clock_66MHz = 0x01,
452 PCI_Clock_33MHz = 0x00,
453
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100454 /* Config3 register p.25 */
455 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
456 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200457 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100458
Francois Romieu5d06a992006-02-23 00:47:58 +0100459 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100460 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
461 MWF = (1 << 5), /* Accept Multicast wakeup frame */
462 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200463 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100464 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100465 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* TBICSR p.28 */
468 TBIReset = 0x80000000,
469 TBILoopback = 0x40000000,
470 TBINwEnable = 0x20000000,
471 TBINwRestart = 0x10000000,
472 TBILinkOk = 0x02000000,
473 TBINwComplete = 0x01000000,
474
475 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200476 EnableBist = (1 << 15), // 8168 8101
477 Mac_dbgo_oe = (1 << 14), // 8168 8101
478 Normal_mode = (1 << 13), // unused
479 Force_half_dup = (1 << 12), // 8168 8101
480 Force_rxflow_en = (1 << 11), // 8168 8101
481 Force_txflow_en = (1 << 10), // 8168 8101
482 Cxpl_dbg_sel = (1 << 9), // 8168 8101
483 ASF = (1 << 8), // 8168 8101
484 PktCntrDisable = (1 << 7), // 8168 8101
485 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 RxVlan = (1 << 6),
487 RxChkSum = (1 << 5),
488 PCIDAC = (1 << 4),
489 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100490 INTT_0 = 0x0000, // 8168
491 INTT_1 = 0x0001, // 8168
492 INTT_2 = 0x0002, // 8168
493 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100496 TBI_Enable = 0x80,
497 TxFlowCtrl = 0x40,
498 RxFlowCtrl = 0x20,
499 _1000bpsF = 0x10,
500 _100bps = 0x08,
501 _10bps = 0x04,
502 LinkStatus = 0x02,
503 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100506 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200507
508 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100509 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510};
511
Francois Romieu2b7b4312011-04-18 22:53:24 -0700512enum rtl_desc_bit {
513 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
515 RingEnd = (1 << 30), /* End of descriptor ring */
516 FirstFrag = (1 << 29), /* First segment of a packet */
517 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700518};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Francois Romieu2b7b4312011-04-18 22:53:24 -0700520/* Generic case. */
521enum rtl_tx_desc_bit {
522 /* First doubleword. */
523 TD_LSO = (1 << 27), /* Large Send Offload */
524#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Francois Romieu2b7b4312011-04-18 22:53:24 -0700526 /* Second doubleword. */
527 TxVlanTag = (1 << 17), /* Add VLAN tag */
528};
529
530/* 8169, 8168b and 810x except 8102e. */
531enum rtl_tx_desc_bit_0 {
532 /* First doubleword. */
533#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
534 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
535 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
536 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
537};
538
539/* 8102e, 8168c and beyond. */
540enum rtl_tx_desc_bit_1 {
541 /* Second doubleword. */
542#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
543 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
544 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
545 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
546};
547
548static const struct rtl_tx_desc_info {
549 struct {
550 u32 udp;
551 u32 tcp;
552 } checksum;
553 u16 mss_shift;
554 u16 opts_offset;
555} tx_desc_info [] = {
556 [RTL_TD_0] = {
557 .checksum = {
558 .udp = TD0_IP_CS | TD0_UDP_CS,
559 .tcp = TD0_IP_CS | TD0_TCP_CS
560 },
561 .mss_shift = TD0_MSS_SHIFT,
562 .opts_offset = 0
563 },
564 [RTL_TD_1] = {
565 .checksum = {
566 .udp = TD1_IP_CS | TD1_UDP_CS,
567 .tcp = TD1_IP_CS | TD1_TCP_CS
568 },
569 .mss_shift = TD1_MSS_SHIFT,
570 .opts_offset = 1
571 }
572};
573
574enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* Rx private */
576 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
577 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
578
579#define RxProtoUDP (PID1)
580#define RxProtoTCP (PID0)
581#define RxProtoIP (PID1 | PID0)
582#define RxProtoMask RxProtoIP
583
584 IPFail = (1 << 16), /* IP checksum failed */
585 UDPFail = (1 << 15), /* UDP/IP checksum failed */
586 TCPFail = (1 << 14), /* TCP/IP checksum failed */
587 RxVlanTag = (1 << 16), /* VLAN tag available */
588};
589
590#define RsvdMask 0x3fffc000
591
592struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200593 __le32 opts1;
594 __le32 opts2;
595 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596};
597
598struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200599 __le32 opts1;
600 __le32 opts2;
601 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602};
603
604struct ring_info {
605 struct sk_buff *skb;
606 u32 len;
607 u8 __pad[sizeof(void *) - sizeof(u32)];
608};
609
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200610enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200611 RTL_FEATURE_WOL = (1 << 0),
612 RTL_FEATURE_MSI = (1 << 1),
613 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200614};
615
Ivan Vecera355423d2009-02-06 21:49:57 -0800616struct rtl8169_counters {
617 __le64 tx_packets;
618 __le64 rx_packets;
619 __le64 tx_errors;
620 __le32 rx_errors;
621 __le16 rx_missed;
622 __le16 align_errors;
623 __le32 tx_one_collision;
624 __le32 tx_multi_collision;
625 __le64 rx_unicast;
626 __le64 rx_broadcast;
627 __le32 rx_multicast;
628 __le16 tx_aborted;
629 __le16 tx_underun;
630};
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632struct rtl8169_private {
633 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200634 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000635 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700636 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200637 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200638 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700639 u16 txd_version;
640 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
642 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
643 u32 dirty_rx;
644 u32 dirty_tx;
645 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
646 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
647 dma_addr_t TxPhyAddr;
648 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000649 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct timer_list timer;
652 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100653 u16 intr_event;
654 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000656
657 struct mdio_ops {
658 void (*write)(void __iomem *, int, int);
659 int (*read)(void __iomem *, int);
660 } mdio_ops;
661
françois romieu065c27c2011-01-03 15:08:12 +0000662 struct pll_power_ops {
663 void (*down)(struct rtl8169_private *);
664 void (*up)(struct rtl8169_private *);
665 } pll_power_ops;
666
Oliver Neukum54405cd2011-01-06 21:55:13 +0100667 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200668 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000669 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100670 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000671 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800673 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
David Howellsc4028952006-11-22 14:57:56 +0000674 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200675 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200676
677 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800678 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000679 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000680
Francois Romieub6ffd972011-06-17 17:00:05 +0200681 struct rtl_fw {
682 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200683
684#define RTL_VER_SIZE 32
685
686 char version[RTL_VER_SIZE];
687
688 struct rtl_fw_phy_action {
689 __le32 *code;
690 size_t size;
691 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200692 } *rtl_fw;
François Romieu953a12c2011-04-24 17:38:48 +0200693#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694};
695
Ralf Baechle979b6c12005-06-13 14:30:40 -0700696MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700699MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200700module_param_named(debug, debug.msg_enable, int, 0);
701MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702MODULE_LICENSE("GPL");
703MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000704MODULE_FIRMWARE(FIRMWARE_8168D_1);
705MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000706MODULE_FIRMWARE(FIRMWARE_8168E_1);
707MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800708MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000711static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
712 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100713static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100715static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100717static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200719static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700721 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200722static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200724static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700725static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Hayes Wang4f6b00e2011-07-06 15:58:02 +0800727static const unsigned int rtl8169_rx_config = RX_FIFO_THRESH | RX_DMA_BURST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
françois romieub646d902011-01-03 15:08:21 +0000729static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
730{
731 void __iomem *ioaddr = tp->mmio_addr;
732 int i;
733
734 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
735 for (i = 0; i < 20; i++) {
736 udelay(100);
737 if (RTL_R32(OCPAR) & OCPAR_FLAG)
738 break;
739 }
740 return RTL_R32(OCPDR);
741}
742
743static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
744{
745 void __iomem *ioaddr = tp->mmio_addr;
746 int i;
747
748 RTL_W32(OCPDR, data);
749 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
750 for (i = 0; i < 20; i++) {
751 udelay(100);
752 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
753 break;
754 }
755}
756
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800757static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000758{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800759 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000760 int i;
761
762 RTL_W8(ERIDR, cmd);
763 RTL_W32(ERIAR, 0x800010e8);
764 msleep(2);
765 for (i = 0; i < 5; i++) {
766 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200767 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000768 break;
769 }
770
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800771 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000772}
773
774#define OOB_CMD_RESET 0x00
775#define OOB_CMD_DRIVER_START 0x05
776#define OOB_CMD_DRIVER_STOP 0x06
777
Francois Romieucecb5fd2011-04-01 10:21:07 +0200778static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
779{
780 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
781}
782
françois romieub646d902011-01-03 15:08:21 +0000783static void rtl8168_driver_start(struct rtl8169_private *tp)
784{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200785 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000786 int i;
787
788 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
789
Francois Romieucecb5fd2011-04-01 10:21:07 +0200790 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000791
françois romieub646d902011-01-03 15:08:21 +0000792 for (i = 0; i < 10; i++) {
793 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000794 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000795 break;
796 }
797}
798
799static void rtl8168_driver_stop(struct rtl8169_private *tp)
800{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200801 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000802 int i;
803
804 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
805
Francois Romieucecb5fd2011-04-01 10:21:07 +0200806 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000807
françois romieub646d902011-01-03 15:08:21 +0000808 for (i = 0; i < 10; i++) {
809 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000810 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000811 break;
812 }
813}
814
hayeswang4804b3b2011-03-21 01:50:29 +0000815static int r8168dp_check_dash(struct rtl8169_private *tp)
816{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200817 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000818
Francois Romieucecb5fd2011-04-01 10:21:07 +0200819 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000820}
françois romieub646d902011-01-03 15:08:21 +0000821
françois romieu4da19632011-01-03 15:07:55 +0000822static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 int i;
825
Francois Romieua6baf3a2007-11-08 23:23:21 +0100826 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Francois Romieu23714082006-01-29 00:49:09 +0100828 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100829 /*
830 * Check if the RTL8169 has completed writing to the specified
831 * MII register.
832 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200833 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
Francois Romieu23714082006-01-29 00:49:09 +0100835 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700837 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700838 * According to hardware specs a 20us delay is required after write
839 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700840 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700841 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
françois romieu4da19632011-01-03 15:07:55 +0000844static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 int i, value = -1;
847
Francois Romieua6baf3a2007-11-08 23:23:21 +0100848 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Francois Romieu23714082006-01-29 00:49:09 +0100850 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100851 /*
852 * Check if the RTL8169 has completed retrieving data from
853 * the specified MII register.
854 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100856 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 break;
858 }
Francois Romieu23714082006-01-29 00:49:09 +0100859 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700861 /*
862 * According to hardware specs a 20us delay is required after read
863 * complete indication, but before sending next command.
864 */
865 udelay(20);
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return value;
868}
869
françois romieuc0e45c12011-01-03 15:08:04 +0000870static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
871{
872 int i;
873
874 RTL_W32(OCPDR, data |
875 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
876 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
877 RTL_W32(EPHY_RXER_NUM, 0);
878
879 for (i = 0; i < 100; i++) {
880 mdelay(1);
881 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
882 break;
883 }
884}
885
886static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
887{
888 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
889 (value & OCPDR_DATA_MASK));
890}
891
892static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
893{
894 int i;
895
896 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
897
898 mdelay(1);
899 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
900 RTL_W32(EPHY_RXER_NUM, 0);
901
902 for (i = 0; i < 100; i++) {
903 mdelay(1);
904 if (RTL_R32(OCPAR) & OCPAR_FLAG)
905 break;
906 }
907
908 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
909}
910
françois romieue6de30d2011-01-03 15:08:37 +0000911#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
912
913static void r8168dp_2_mdio_start(void __iomem *ioaddr)
914{
915 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
916}
917
918static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
919{
920 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
921}
922
923static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
924{
925 r8168dp_2_mdio_start(ioaddr);
926
927 r8169_mdio_write(ioaddr, reg_addr, value);
928
929 r8168dp_2_mdio_stop(ioaddr);
930}
931
932static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
933{
934 int value;
935
936 r8168dp_2_mdio_start(ioaddr);
937
938 value = r8169_mdio_read(ioaddr, reg_addr);
939
940 r8168dp_2_mdio_stop(ioaddr);
941
942 return value;
943}
944
françois romieu4da19632011-01-03 15:07:55 +0000945static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200946{
françois romieuc0e45c12011-01-03 15:08:04 +0000947 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200948}
949
françois romieu4da19632011-01-03 15:07:55 +0000950static int rtl_readphy(struct rtl8169_private *tp, int location)
951{
françois romieuc0e45c12011-01-03 15:08:04 +0000952 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000953}
954
955static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
956{
957 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
958}
959
960static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000961{
962 int val;
963
françois romieu4da19632011-01-03 15:07:55 +0000964 val = rtl_readphy(tp, reg_addr);
965 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000966}
967
Francois Romieuccdffb92008-07-26 14:26:06 +0200968static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
969 int val)
970{
971 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200972
françois romieu4da19632011-01-03 15:07:55 +0000973 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200974}
975
976static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
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 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200981}
982
Francois Romieudacf8152008-08-02 20:44:13 +0200983static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
984{
985 unsigned int i;
986
987 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
988 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
989
990 for (i = 0; i < 100; i++) {
991 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
992 break;
993 udelay(10);
994 }
995}
996
997static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
998{
999 u16 value = 0xffff;
1000 unsigned int i;
1001
1002 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
1003
1004 for (i = 0; i < 100; i++) {
1005 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
1006 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
1007 break;
1008 }
1009 udelay(10);
1010 }
1011
1012 return value;
1013}
1014
1015static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1016{
1017 unsigned int i;
1018
1019 RTL_W32(CSIDR, value);
1020 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1021 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1022
1023 for (i = 0; i < 100; i++) {
1024 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1025 break;
1026 udelay(10);
1027 }
1028}
1029
1030static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1031{
1032 u32 value = ~0x00;
1033 unsigned int i;
1034
1035 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1036 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1037
1038 for (i = 0; i < 100; i++) {
1039 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1040 value = RTL_R32(CSIDR);
1041 break;
1042 }
1043 udelay(10);
1044 }
1045
1046 return value;
1047}
1048
Hayes Wang133ac402011-07-06 15:58:05 +08001049static
1050void rtl_eri_write(void __iomem *ioaddr, int addr, u32 mask, u32 val, int type)
1051{
1052 unsigned int i;
1053
1054 BUG_ON((addr & 3) || (mask == 0));
1055 RTL_W32(ERIDR, val);
1056 RTL_W32(ERIAR, ERIAR_WRITE_CMD | type | mask | addr);
1057
1058 for (i = 0; i < 100; i++) {
1059 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
1060 break;
1061 udelay(100);
1062 }
1063}
1064
1065static u32 rtl_eri_read(void __iomem *ioaddr, int addr, int type)
1066{
1067 u32 value = ~0x00;
1068 unsigned int i;
1069
1070 RTL_W32(ERIAR, ERIAR_READ_CMD | type | ERIAR_MASK_1111 | addr);
1071
1072 for (i = 0; i < 100; i++) {
1073 if (RTL_R32(ERIAR) & ERIAR_FLAG) {
1074 value = RTL_R32(ERIDR);
1075 break;
1076 }
1077 udelay(100);
1078 }
1079
1080 return value;
1081}
1082
1083static void
1084rtl_w1w0_eri(void __iomem *ioaddr, int addr, u32 mask, u32 p, u32 m, int type)
1085{
1086 u32 val;
1087
1088 val = rtl_eri_read(ioaddr, addr, type);
1089 rtl_eri_write(ioaddr, addr, mask, (val & ~m) | p, type);
1090}
1091
françois romieudaf9df62009-10-07 12:44:20 +00001092static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1093{
1094 u8 value = 0xff;
1095 unsigned int i;
1096
1097 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1098
1099 for (i = 0; i < 300; i++) {
1100 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1101 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1102 break;
1103 }
1104 udelay(100);
1105 }
1106
1107 return value;
1108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1111{
1112 RTL_W16(IntrMask, 0x0000);
1113
1114 RTL_W16(IntrStatus, 0xffff);
1115}
1116
françois romieu4da19632011-01-03 15:07:55 +00001117static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
françois romieu4da19632011-01-03 15:07:55 +00001119 void __iomem *ioaddr = tp->mmio_addr;
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return RTL_R32(TBICSR) & TBIReset;
1122}
1123
françois romieu4da19632011-01-03 15:07:55 +00001124static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
françois romieu4da19632011-01-03 15:07:55 +00001126 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
1129static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1130{
1131 return RTL_R32(TBICSR) & TBILinkOk;
1132}
1133
1134static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1135{
1136 return RTL_R8(PHYstatus) & LinkStatus;
1137}
1138
françois romieu4da19632011-01-03 15:07:55 +00001139static void rtl8169_tbi_reset_enable(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 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1144}
1145
françois romieu4da19632011-01-03 15:07:55 +00001146static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 unsigned int val;
1149
françois romieu4da19632011-01-03 15:07:55 +00001150 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1151 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001154static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001155 struct rtl8169_private *tp,
1156 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 unsigned long flags;
1159
1160 spin_lock_irqsave(&tp->lock, flags);
1161 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001162 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001163 if (pm)
1164 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001166 if (net_ratelimit())
1167 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001168 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001170 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001171 if (pm)
1172 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 spin_unlock_irqrestore(&tp->lock, flags);
1175}
1176
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001177static void rtl8169_check_link_status(struct net_device *dev,
1178 struct rtl8169_private *tp,
1179 void __iomem *ioaddr)
1180{
1181 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1182}
1183
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001184#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1185
1186static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1187{
1188 void __iomem *ioaddr = tp->mmio_addr;
1189 u8 options;
1190 u32 wolopts = 0;
1191
1192 options = RTL_R8(Config1);
1193 if (!(options & PMEnable))
1194 return 0;
1195
1196 options = RTL_R8(Config3);
1197 if (options & LinkUp)
1198 wolopts |= WAKE_PHY;
1199 if (options & MagicPacket)
1200 wolopts |= WAKE_MAGIC;
1201
1202 options = RTL_R8(Config5);
1203 if (options & UWF)
1204 wolopts |= WAKE_UCAST;
1205 if (options & BWF)
1206 wolopts |= WAKE_BCAST;
1207 if (options & MWF)
1208 wolopts |= WAKE_MCAST;
1209
1210 return wolopts;
1211}
1212
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001213static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1214{
1215 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001216
1217 spin_lock_irq(&tp->lock);
1218
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001219 wol->supported = WAKE_ANY;
1220 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001221
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001222 spin_unlock_irq(&tp->lock);
1223}
1224
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001225static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001226{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001227 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001228 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001229 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001230 u32 opt;
1231 u16 reg;
1232 u8 mask;
1233 } cfg[] = {
1234 { WAKE_ANY, Config1, PMEnable },
1235 { WAKE_PHY, Config3, LinkUp },
1236 { WAKE_MAGIC, Config3, MagicPacket },
1237 { WAKE_UCAST, Config5, UWF },
1238 { WAKE_BCAST, Config5, BWF },
1239 { WAKE_MCAST, Config5, MWF },
1240 { WAKE_ANY, Config5, LanWake }
1241 };
1242
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001243 RTL_W8(Cfg9346, Cfg9346_Unlock);
1244
1245 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1246 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001247 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001248 options |= cfg[i].mask;
1249 RTL_W8(cfg[i].reg, options);
1250 }
1251
1252 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001253}
1254
1255static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1256{
1257 struct rtl8169_private *tp = netdev_priv(dev);
1258
1259 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001260
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001261 if (wol->wolopts)
1262 tp->features |= RTL_FEATURE_WOL;
1263 else
1264 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001265 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001266 spin_unlock_irq(&tp->lock);
1267
françois romieuea809072010-11-08 13:23:58 +00001268 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1269
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001270 return 0;
1271}
1272
Francois Romieu31bd2042011-04-26 18:58:59 +02001273static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1274{
Francois Romieu85bffe62011-04-27 08:22:39 +02001275 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278static void rtl8169_get_drvinfo(struct net_device *dev,
1279 struct ethtool_drvinfo *info)
1280{
1281 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001282 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 strcpy(info->driver, MODULENAME);
1285 strcpy(info->version, RTL8169_VERSION);
1286 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001287 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1288 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1289 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
1292static int rtl8169_get_regs_len(struct net_device *dev)
1293{
1294 return R8169_REGS_SIZE;
1295}
1296
1297static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001298 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 struct rtl8169_private *tp = netdev_priv(dev);
1301 void __iomem *ioaddr = tp->mmio_addr;
1302 int ret = 0;
1303 u32 reg;
1304
1305 reg = RTL_R32(TBICSR);
1306 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1307 (duplex == DUPLEX_FULL)) {
1308 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1309 } else if (autoneg == AUTONEG_ENABLE)
1310 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1311 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001312 netif_warn(tp, link, dev,
1313 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 ret = -EOPNOTSUPP;
1315 }
1316
1317 return ret;
1318}
1319
1320static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001321 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001324 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001325 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Hayes Wang716b50a2011-02-22 17:26:18 +08001327 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001330 int auto_nego;
1331
françois romieu4da19632011-01-03 15:07:55 +00001332 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001333 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1334 ADVERTISE_100HALF | ADVERTISE_100FULL);
1335
1336 if (adv & ADVERTISED_10baseT_Half)
1337 auto_nego |= ADVERTISE_10HALF;
1338 if (adv & ADVERTISED_10baseT_Full)
1339 auto_nego |= ADVERTISE_10FULL;
1340 if (adv & ADVERTISED_100baseT_Half)
1341 auto_nego |= ADVERTISE_100HALF;
1342 if (adv & ADVERTISED_100baseT_Full)
1343 auto_nego |= ADVERTISE_100FULL;
1344
françois romieu3577aa12009-05-19 10:46:48 +00001345 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1346
françois romieu4da19632011-01-03 15:07:55 +00001347 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001348 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1349
1350 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001351 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001352 if (adv & ADVERTISED_1000baseT_Half)
1353 giga_ctrl |= ADVERTISE_1000HALF;
1354 if (adv & ADVERTISED_1000baseT_Full)
1355 giga_ctrl |= ADVERTISE_1000FULL;
1356 } else if (adv & (ADVERTISED_1000baseT_Half |
1357 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001358 netif_info(tp, link, dev,
1359 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001360 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
françois romieu3577aa12009-05-19 10:46:48 +00001363 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001364
françois romieu4da19632011-01-03 15:07:55 +00001365 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1366 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001367 } else {
1368 giga_ctrl = 0;
1369
1370 if (speed == SPEED_10)
1371 bmcr = 0;
1372 else if (speed == SPEED_100)
1373 bmcr = BMCR_SPEED100;
1374 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001375 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001376
1377 if (duplex == DUPLEX_FULL)
1378 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001379 }
1380
françois romieu4da19632011-01-03 15:07:55 +00001381 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001382
Francois Romieucecb5fd2011-04-01 10:21:07 +02001383 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1384 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001385 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001386 rtl_writephy(tp, 0x17, 0x2138);
1387 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001388 } else {
françois romieu4da19632011-01-03 15:07:55 +00001389 rtl_writephy(tp, 0x17, 0x2108);
1390 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001391 }
1392 }
1393
Oliver Neukum54405cd2011-01-06 21:55:13 +01001394 rc = 0;
1395out:
1396 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
1399static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001400 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct rtl8169_private *tp = netdev_priv(dev);
1403 int ret;
1404
Oliver Neukum54405cd2011-01-06 21:55:13 +01001405 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001406 if (ret < 0)
1407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Francois Romieu4876cc12011-03-11 21:07:11 +01001409 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1410 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001412 }
1413out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return ret;
1415}
1416
1417static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1418{
1419 struct rtl8169_private *tp = netdev_priv(dev);
1420 unsigned long flags;
1421 int ret;
1422
Francois Romieu4876cc12011-03-11 21:07:11 +01001423 del_timer_sync(&tp->timer);
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001426 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001427 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return ret;
1431}
1432
Michał Mirosław350fb322011-04-08 06:35:56 +00001433static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001435 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001436 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Michał Mirosław350fb322011-04-08 06:35:56 +00001438 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Michał Mirosław350fb322011-04-08 06:35:56 +00001441static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct rtl8169_private *tp = netdev_priv(dev);
1444 void __iomem *ioaddr = tp->mmio_addr;
1445 unsigned long flags;
1446
1447 spin_lock_irqsave(&tp->lock, flags);
1448
Michał Mirosław350fb322011-04-08 06:35:56 +00001449 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 tp->cp_cmd |= RxChkSum;
1451 else
1452 tp->cp_cmd &= ~RxChkSum;
1453
Michał Mirosław350fb322011-04-08 06:35:56 +00001454 if (dev->features & NETIF_F_HW_VLAN_RX)
1455 tp->cp_cmd |= RxVlan;
1456 else
1457 tp->cp_cmd &= ~RxVlan;
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 RTL_W16(CPlusCmd, tp->cp_cmd);
1460 RTL_R16(CPlusCmd);
1461
1462 spin_unlock_irqrestore(&tp->lock, flags);
1463
1464 return 0;
1465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1468 struct sk_buff *skb)
1469{
Jesse Grosseab6d182010-10-20 13:56:03 +00001470 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1472}
1473
Francois Romieu7a8fc772011-03-01 17:18:33 +01001474static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Francois Romieu7a8fc772011-03-01 17:18:33 +01001478 if (opts2 & RxVlanTag)
1479 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Francois Romieuccdffb92008-07-26 14:26:06 +02001484static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 struct rtl8169_private *tp = netdev_priv(dev);
1487 void __iomem *ioaddr = tp->mmio_addr;
1488 u32 status;
1489
1490 cmd->supported =
1491 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1492 cmd->port = PORT_FIBRE;
1493 cmd->transceiver = XCVR_INTERNAL;
1494
1495 status = RTL_R32(TBICSR);
1496 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1497 cmd->autoneg = !!(status & TBINwEnable);
1498
David Decotigny70739492011-04-27 18:32:40 +00001499 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001501
1502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Francois Romieuccdffb92008-07-26 14:26:06 +02001505static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Francois Romieuccdffb92008-07-26 14:26:06 +02001509 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1513{
1514 struct rtl8169_private *tp = netdev_priv(dev);
1515 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001516 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 spin_lock_irqsave(&tp->lock, flags);
1519
Francois Romieuccdffb92008-07-26 14:26:06 +02001520 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001523 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1527 void *p)
1528{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001529 struct rtl8169_private *tp = netdev_priv(dev);
1530 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Francois Romieu5b0384f2006-08-16 16:00:01 +02001532 if (regs->len > R8169_REGS_SIZE)
1533 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Francois Romieu5b0384f2006-08-16 16:00:01 +02001535 spin_lock_irqsave(&tp->lock, flags);
1536 memcpy_fromio(p, tp->mmio_addr, regs->len);
1537 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001540static u32 rtl8169_get_msglevel(struct net_device *dev)
1541{
1542 struct rtl8169_private *tp = netdev_priv(dev);
1543
1544 return tp->msg_enable;
1545}
1546
1547static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1548{
1549 struct rtl8169_private *tp = netdev_priv(dev);
1550
1551 tp->msg_enable = value;
1552}
1553
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001554static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1555 "tx_packets",
1556 "rx_packets",
1557 "tx_errors",
1558 "rx_errors",
1559 "rx_missed",
1560 "align_errors",
1561 "tx_single_collisions",
1562 "tx_multi_collisions",
1563 "unicast",
1564 "broadcast",
1565 "multicast",
1566 "tx_aborted",
1567 "tx_underrun",
1568};
1569
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001570static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001571{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001572 switch (sset) {
1573 case ETH_SS_STATS:
1574 return ARRAY_SIZE(rtl8169_gstrings);
1575 default:
1576 return -EOPNOTSUPP;
1577 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001578}
1579
Ivan Vecera355423d2009-02-06 21:49:57 -08001580static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001581{
1582 struct rtl8169_private *tp = netdev_priv(dev);
1583 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001584 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001585 struct rtl8169_counters *counters;
1586 dma_addr_t paddr;
1587 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001588 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001589
Ivan Vecera355423d2009-02-06 21:49:57 -08001590 /*
1591 * Some chips are unable to dump tally counters when the receiver
1592 * is disabled.
1593 */
1594 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1595 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001596
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001597 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001598 if (!counters)
1599 return;
1600
1601 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001602 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001603 RTL_W32(CounterAddrLow, cmd);
1604 RTL_W32(CounterAddrLow, cmd | CounterDump);
1605
Ivan Vecera355423d2009-02-06 21:49:57 -08001606 while (wait--) {
1607 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001608 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001609 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001610 }
1611 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001612 }
1613
1614 RTL_W32(CounterAddrLow, 0);
1615 RTL_W32(CounterAddrHigh, 0);
1616
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001617 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001618}
1619
Ivan Vecera355423d2009-02-06 21:49:57 -08001620static void rtl8169_get_ethtool_stats(struct net_device *dev,
1621 struct ethtool_stats *stats, u64 *data)
1622{
1623 struct rtl8169_private *tp = netdev_priv(dev);
1624
1625 ASSERT_RTNL();
1626
1627 rtl8169_update_counters(dev);
1628
1629 data[0] = le64_to_cpu(tp->counters.tx_packets);
1630 data[1] = le64_to_cpu(tp->counters.rx_packets);
1631 data[2] = le64_to_cpu(tp->counters.tx_errors);
1632 data[3] = le32_to_cpu(tp->counters.rx_errors);
1633 data[4] = le16_to_cpu(tp->counters.rx_missed);
1634 data[5] = le16_to_cpu(tp->counters.align_errors);
1635 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1636 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1637 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1638 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1639 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1640 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1641 data[12] = le16_to_cpu(tp->counters.tx_underun);
1642}
1643
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001644static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1645{
1646 switch(stringset) {
1647 case ETH_SS_STATS:
1648 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1649 break;
1650 }
1651}
1652
Jeff Garzik7282d492006-09-13 14:30:00 -04001653static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 .get_drvinfo = rtl8169_get_drvinfo,
1655 .get_regs_len = rtl8169_get_regs_len,
1656 .get_link = ethtool_op_get_link,
1657 .get_settings = rtl8169_get_settings,
1658 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001659 .get_msglevel = rtl8169_get_msglevel,
1660 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001662 .get_wol = rtl8169_get_wol,
1663 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001664 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001665 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001666 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667};
1668
Francois Romieu07d3f512007-02-21 22:40:46 +01001669static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001670 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Francois Romieu5d320a22011-05-08 17:47:36 +02001672 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001673 /*
1674 * The driver currently handles the 8168Bf and the 8168Be identically
1675 * but they can be identified more specifically through the test below
1676 * if needed:
1677 *
1678 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001679 *
1680 * Same thing for the 8101Eb and the 8101Ec:
1681 *
1682 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001683 */
Francois Romieu37441002011-06-17 22:58:54 +02001684 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001686 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int mac_version;
1688 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001689 /* 8168E family. */
1690 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1691 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1692 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1693
Francois Romieu5b538df2008-07-20 16:22:45 +02001694 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001695 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1696 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001697 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001698
françois romieue6de30d2011-01-03 15:08:37 +00001699 /* 8168DP family. */
1700 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1701 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001702 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001703
Francois Romieuef808d52008-06-29 13:10:54 +02001704 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001705 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001706 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001707 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001708 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001709 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1710 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001711 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001712 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001713 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001714
1715 /* 8168B family. */
1716 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1717 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1718 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1719 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1720
1721 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001722 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001723 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1724 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1725 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001726 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1727 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1728 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1729 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1730 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1731 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001732 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001733 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001734 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001735 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1736 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001737 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1738 /* FIXME: where did these entries come from ? -- FR */
1739 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1740 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1741
1742 /* 8110 family. */
1743 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1744 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1745 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1746 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1747 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1748 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1749
Jean Delvaref21b75e2009-05-26 20:54:48 -07001750 /* Catch-all */
1751 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001752 };
1753 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 u32 reg;
1755
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001756 reg = RTL_R32(TxConfig);
1757 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 p++;
1759 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001760
1761 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1762 netif_notice(tp, probe, dev,
1763 "unknown MAC, using family default\n");
1764 tp->mac_version = default_version;
1765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
1768static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1769{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001770 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Francois Romieu867763c2007-08-17 18:21:58 +02001773struct phy_reg {
1774 u16 reg;
1775 u16 val;
1776};
1777
françois romieu4da19632011-01-03 15:07:55 +00001778static void rtl_writephy_batch(struct rtl8169_private *tp,
1779 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001780{
1781 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001782 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001783 regs++;
1784 }
1785}
1786
françois romieubca03d52011-01-03 15:07:31 +00001787#define PHY_READ 0x00000000
1788#define PHY_DATA_OR 0x10000000
1789#define PHY_DATA_AND 0x20000000
1790#define PHY_BJMPN 0x30000000
1791#define PHY_READ_EFUSE 0x40000000
1792#define PHY_READ_MAC_BYTE 0x50000000
1793#define PHY_WRITE_MAC_BYTE 0x60000000
1794#define PHY_CLEAR_READCOUNT 0x70000000
1795#define PHY_WRITE 0x80000000
1796#define PHY_READCOUNT_EQ_SKIP 0x90000000
1797#define PHY_COMP_EQ_SKIPN 0xa0000000
1798#define PHY_COMP_NEQ_SKIPN 0xb0000000
1799#define PHY_WRITE_PREVIOUS 0xc0000000
1800#define PHY_SKIPN 0xd0000000
1801#define PHY_DELAY_MS 0xe0000000
1802#define PHY_WRITE_ERI_WORD 0xf0000000
1803
Hayes Wang960aee62011-06-18 11:37:48 +02001804struct fw_info {
1805 u32 magic;
1806 char version[RTL_VER_SIZE];
1807 __le32 fw_start;
1808 __le32 fw_len;
1809 u8 chksum;
1810} __packed;
1811
Francois Romieu1c361ef2011-06-17 17:16:24 +02001812#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1813
1814static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001815{
Francois Romieub6ffd972011-06-17 17:00:05 +02001816 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001817 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001818 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1819 char *version = rtl_fw->version;
1820 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001821
Francois Romieu1c361ef2011-06-17 17:16:24 +02001822 if (fw->size < FW_OPCODE_SIZE)
1823 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001824
1825 if (!fw_info->magic) {
1826 size_t i, size, start;
1827 u8 checksum = 0;
1828
1829 if (fw->size < sizeof(*fw_info))
1830 goto out;
1831
1832 for (i = 0; i < fw->size; i++)
1833 checksum += fw->data[i];
1834 if (checksum != 0)
1835 goto out;
1836
1837 start = le32_to_cpu(fw_info->fw_start);
1838 if (start > fw->size)
1839 goto out;
1840
1841 size = le32_to_cpu(fw_info->fw_len);
1842 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1843 goto out;
1844
1845 memcpy(version, fw_info->version, RTL_VER_SIZE);
1846
1847 pa->code = (__le32 *)(fw->data + start);
1848 pa->size = size;
1849 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001850 if (fw->size % FW_OPCODE_SIZE)
1851 goto out;
1852
1853 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1854
1855 pa->code = (__le32 *)fw->data;
1856 pa->size = fw->size / FW_OPCODE_SIZE;
1857 }
1858 version[RTL_VER_SIZE - 1] = 0;
1859
1860 rc = true;
1861out:
1862 return rc;
1863}
1864
Francois Romieufd112f22011-06-18 00:10:29 +02001865static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1866 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02001867{
Francois Romieufd112f22011-06-18 00:10:29 +02001868 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001869 size_t index;
1870
Francois Romieu1c361ef2011-06-17 17:16:24 +02001871 for (index = 0; index < pa->size; index++) {
1872 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00001873 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001874
hayeswang42b82dc2011-01-10 02:07:25 +00001875 switch(action & 0xf0000000) {
1876 case PHY_READ:
1877 case PHY_DATA_OR:
1878 case PHY_DATA_AND:
1879 case PHY_READ_EFUSE:
1880 case PHY_CLEAR_READCOUNT:
1881 case PHY_WRITE:
1882 case PHY_WRITE_PREVIOUS:
1883 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001884 break;
1885
hayeswang42b82dc2011-01-10 02:07:25 +00001886 case PHY_BJMPN:
1887 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02001888 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001889 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001890 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001891 }
1892 break;
1893 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001894 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001895 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001896 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001897 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001898 }
1899 break;
1900 case PHY_COMP_EQ_SKIPN:
1901 case PHY_COMP_NEQ_SKIPN:
1902 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001903 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001904 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001905 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001906 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001907 }
1908 break;
1909
1910 case PHY_READ_MAC_BYTE:
1911 case PHY_WRITE_MAC_BYTE:
1912 case PHY_WRITE_ERI_WORD:
1913 default:
Francois Romieufd112f22011-06-18 00:10:29 +02001914 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00001915 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02001916 goto out;
françois romieubca03d52011-01-03 15:07:31 +00001917 }
1918 }
Francois Romieufd112f22011-06-18 00:10:29 +02001919 rc = true;
1920out:
1921 return rc;
1922}
françois romieubca03d52011-01-03 15:07:31 +00001923
Francois Romieufd112f22011-06-18 00:10:29 +02001924static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1925{
1926 struct net_device *dev = tp->dev;
1927 int rc = -EINVAL;
1928
1929 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1930 netif_err(tp, ifup, dev, "invalid firwmare\n");
1931 goto out;
1932 }
1933
1934 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1935 rc = 0;
1936out:
1937 return rc;
1938}
1939
1940static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1941{
1942 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1943 u32 predata, count;
1944 size_t index;
1945
1946 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00001947
Francois Romieu1c361ef2011-06-17 17:16:24 +02001948 for (index = 0; index < pa->size; ) {
1949 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00001950 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001951 u32 regno = (action & 0x0fff0000) >> 16;
1952
1953 if (!action)
1954 break;
françois romieubca03d52011-01-03 15:07:31 +00001955
1956 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001957 case PHY_READ:
1958 predata = rtl_readphy(tp, regno);
1959 count++;
1960 index++;
françois romieubca03d52011-01-03 15:07:31 +00001961 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001962 case PHY_DATA_OR:
1963 predata |= data;
1964 index++;
1965 break;
1966 case PHY_DATA_AND:
1967 predata &= data;
1968 index++;
1969 break;
1970 case PHY_BJMPN:
1971 index -= regno;
1972 break;
1973 case PHY_READ_EFUSE:
1974 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1975 index++;
1976 break;
1977 case PHY_CLEAR_READCOUNT:
1978 count = 0;
1979 index++;
1980 break;
1981 case PHY_WRITE:
1982 rtl_writephy(tp, regno, data);
1983 index++;
1984 break;
1985 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02001986 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00001987 break;
1988 case PHY_COMP_EQ_SKIPN:
1989 if (predata == data)
1990 index += regno;
1991 index++;
1992 break;
1993 case PHY_COMP_NEQ_SKIPN:
1994 if (predata != data)
1995 index += regno;
1996 index++;
1997 break;
1998 case PHY_WRITE_PREVIOUS:
1999 rtl_writephy(tp, regno, predata);
2000 index++;
2001 break;
2002 case PHY_SKIPN:
2003 index += regno + 1;
2004 break;
2005 case PHY_DELAY_MS:
2006 mdelay(data);
2007 index++;
2008 break;
2009
2010 case PHY_READ_MAC_BYTE:
2011 case PHY_WRITE_MAC_BYTE:
2012 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00002013 default:
2014 BUG();
2015 }
2016 }
2017}
2018
françois romieuf1e02ed2011-01-13 13:07:53 +00002019static void rtl_release_firmware(struct rtl8169_private *tp)
2020{
Francois Romieub6ffd972011-06-17 17:00:05 +02002021 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
2022 release_firmware(tp->rtl_fw->fw);
2023 kfree(tp->rtl_fw);
2024 }
2025 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00002026}
2027
François Romieu953a12c2011-04-24 17:38:48 +02002028static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00002029{
Francois Romieub6ffd972011-06-17 17:00:05 +02002030 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00002031
2032 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02002033 if (!IS_ERR_OR_NULL(rtl_fw))
2034 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02002035}
2036
2037static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2038{
2039 if (rtl_readphy(tp, reg) != val)
2040 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2041 else
2042 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002043}
2044
françois romieu4da19632011-01-03 15:07:55 +00002045static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002047 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002048 { 0x1f, 0x0001 },
2049 { 0x06, 0x006e },
2050 { 0x08, 0x0708 },
2051 { 0x15, 0x4000 },
2052 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
françois romieu0b9b5712009-08-10 19:44:56 +00002054 { 0x1f, 0x0001 },
2055 { 0x03, 0x00a1 },
2056 { 0x02, 0x0008 },
2057 { 0x01, 0x0120 },
2058 { 0x00, 0x1000 },
2059 { 0x04, 0x0800 },
2060 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
françois romieu0b9b5712009-08-10 19:44:56 +00002062 { 0x03, 0xff41 },
2063 { 0x02, 0xdf60 },
2064 { 0x01, 0x0140 },
2065 { 0x00, 0x0077 },
2066 { 0x04, 0x7800 },
2067 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
françois romieu0b9b5712009-08-10 19:44:56 +00002069 { 0x03, 0x802f },
2070 { 0x02, 0x4f02 },
2071 { 0x01, 0x0409 },
2072 { 0x00, 0xf0f9 },
2073 { 0x04, 0x9800 },
2074 { 0x04, 0x9000 },
2075
2076 { 0x03, 0xdf01 },
2077 { 0x02, 0xdf20 },
2078 { 0x01, 0xff95 },
2079 { 0x00, 0xba00 },
2080 { 0x04, 0xa800 },
2081 { 0x04, 0xa000 },
2082
2083 { 0x03, 0xff41 },
2084 { 0x02, 0xdf20 },
2085 { 0x01, 0x0140 },
2086 { 0x00, 0x00bb },
2087 { 0x04, 0xb800 },
2088 { 0x04, 0xb000 },
2089
2090 { 0x03, 0xdf41 },
2091 { 0x02, 0xdc60 },
2092 { 0x01, 0x6340 },
2093 { 0x00, 0x007d },
2094 { 0x04, 0xd800 },
2095 { 0x04, 0xd000 },
2096
2097 { 0x03, 0xdf01 },
2098 { 0x02, 0xdf20 },
2099 { 0x01, 0x100a },
2100 { 0x00, 0xa0ff },
2101 { 0x04, 0xf800 },
2102 { 0x04, 0xf000 },
2103
2104 { 0x1f, 0x0000 },
2105 { 0x0b, 0x0000 },
2106 { 0x00, 0x9200 }
2107 };
2108
françois romieu4da19632011-01-03 15:07:55 +00002109 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
françois romieu4da19632011-01-03 15:07:55 +00002112static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002113{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002114 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002115 { 0x1f, 0x0002 },
2116 { 0x01, 0x90d0 },
2117 { 0x1f, 0x0000 }
2118 };
2119
françois romieu4da19632011-01-03 15:07:55 +00002120 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002121}
2122
françois romieu4da19632011-01-03 15:07:55 +00002123static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002124{
2125 struct pci_dev *pdev = tp->pci_dev;
2126 u16 vendor_id, device_id;
2127
2128 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
2129 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
2130
2131 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
2132 return;
2133
françois romieu4da19632011-01-03 15:07:55 +00002134 rtl_writephy(tp, 0x1f, 0x0001);
2135 rtl_writephy(tp, 0x10, 0xf01b);
2136 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002137}
2138
françois romieu4da19632011-01-03 15:07:55 +00002139static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002140{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002141 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002142 { 0x1f, 0x0001 },
2143 { 0x04, 0x0000 },
2144 { 0x03, 0x00a1 },
2145 { 0x02, 0x0008 },
2146 { 0x01, 0x0120 },
2147 { 0x00, 0x1000 },
2148 { 0x04, 0x0800 },
2149 { 0x04, 0x9000 },
2150 { 0x03, 0x802f },
2151 { 0x02, 0x4f02 },
2152 { 0x01, 0x0409 },
2153 { 0x00, 0xf099 },
2154 { 0x04, 0x9800 },
2155 { 0x04, 0xa000 },
2156 { 0x03, 0xdf01 },
2157 { 0x02, 0xdf20 },
2158 { 0x01, 0xff95 },
2159 { 0x00, 0xba00 },
2160 { 0x04, 0xa800 },
2161 { 0x04, 0xf000 },
2162 { 0x03, 0xdf01 },
2163 { 0x02, 0xdf20 },
2164 { 0x01, 0x101a },
2165 { 0x00, 0xa0ff },
2166 { 0x04, 0xf800 },
2167 { 0x04, 0x0000 },
2168 { 0x1f, 0x0000 },
2169
2170 { 0x1f, 0x0001 },
2171 { 0x10, 0xf41b },
2172 { 0x14, 0xfb54 },
2173 { 0x18, 0xf5c7 },
2174 { 0x1f, 0x0000 },
2175
2176 { 0x1f, 0x0001 },
2177 { 0x17, 0x0cc0 },
2178 { 0x1f, 0x0000 }
2179 };
2180
françois romieu4da19632011-01-03 15:07:55 +00002181 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002182
françois romieu4da19632011-01-03 15:07:55 +00002183 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002184}
2185
françois romieu4da19632011-01-03 15:07:55 +00002186static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002187{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002188 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002189 { 0x1f, 0x0001 },
2190 { 0x04, 0x0000 },
2191 { 0x03, 0x00a1 },
2192 { 0x02, 0x0008 },
2193 { 0x01, 0x0120 },
2194 { 0x00, 0x1000 },
2195 { 0x04, 0x0800 },
2196 { 0x04, 0x9000 },
2197 { 0x03, 0x802f },
2198 { 0x02, 0x4f02 },
2199 { 0x01, 0x0409 },
2200 { 0x00, 0xf099 },
2201 { 0x04, 0x9800 },
2202 { 0x04, 0xa000 },
2203 { 0x03, 0xdf01 },
2204 { 0x02, 0xdf20 },
2205 { 0x01, 0xff95 },
2206 { 0x00, 0xba00 },
2207 { 0x04, 0xa800 },
2208 { 0x04, 0xf000 },
2209 { 0x03, 0xdf01 },
2210 { 0x02, 0xdf20 },
2211 { 0x01, 0x101a },
2212 { 0x00, 0xa0ff },
2213 { 0x04, 0xf800 },
2214 { 0x04, 0x0000 },
2215 { 0x1f, 0x0000 },
2216
2217 { 0x1f, 0x0001 },
2218 { 0x0b, 0x8480 },
2219 { 0x1f, 0x0000 },
2220
2221 { 0x1f, 0x0001 },
2222 { 0x18, 0x67c7 },
2223 { 0x04, 0x2000 },
2224 { 0x03, 0x002f },
2225 { 0x02, 0x4360 },
2226 { 0x01, 0x0109 },
2227 { 0x00, 0x3022 },
2228 { 0x04, 0x2800 },
2229 { 0x1f, 0x0000 },
2230
2231 { 0x1f, 0x0001 },
2232 { 0x17, 0x0cc0 },
2233 { 0x1f, 0x0000 }
2234 };
2235
françois romieu4da19632011-01-03 15:07:55 +00002236 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002237}
2238
françois romieu4da19632011-01-03 15:07:55 +00002239static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002240{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002241 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002242 { 0x10, 0xf41b },
2243 { 0x1f, 0x0000 }
2244 };
2245
françois romieu4da19632011-01-03 15:07:55 +00002246 rtl_writephy(tp, 0x1f, 0x0001);
2247 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002248
françois romieu4da19632011-01-03 15:07:55 +00002249 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002250}
2251
françois romieu4da19632011-01-03 15:07:55 +00002252static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002253{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002254 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002255 { 0x1f, 0x0001 },
2256 { 0x10, 0xf41b },
2257 { 0x1f, 0x0000 }
2258 };
2259
françois romieu4da19632011-01-03 15:07:55 +00002260 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002261}
2262
françois romieu4da19632011-01-03 15:07:55 +00002263static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002264{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002265 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002266 { 0x1f, 0x0000 },
2267 { 0x1d, 0x0f00 },
2268 { 0x1f, 0x0002 },
2269 { 0x0c, 0x1ec8 },
2270 { 0x1f, 0x0000 }
2271 };
2272
françois romieu4da19632011-01-03 15:07:55 +00002273 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002274}
2275
françois romieu4da19632011-01-03 15:07:55 +00002276static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002277{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002278 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002279 { 0x1f, 0x0001 },
2280 { 0x1d, 0x3d98 },
2281 { 0x1f, 0x0000 }
2282 };
2283
françois romieu4da19632011-01-03 15:07:55 +00002284 rtl_writephy(tp, 0x1f, 0x0000);
2285 rtl_patchphy(tp, 0x14, 1 << 5);
2286 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002287
françois romieu4da19632011-01-03 15:07:55 +00002288 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002289}
2290
françois romieu4da19632011-01-03 15:07:55 +00002291static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002292{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002293 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002294 { 0x1f, 0x0001 },
2295 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002296 { 0x1f, 0x0002 },
2297 { 0x00, 0x88d4 },
2298 { 0x01, 0x82b1 },
2299 { 0x03, 0x7002 },
2300 { 0x08, 0x9e30 },
2301 { 0x09, 0x01f0 },
2302 { 0x0a, 0x5500 },
2303 { 0x0c, 0x00c8 },
2304 { 0x1f, 0x0003 },
2305 { 0x12, 0xc096 },
2306 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002307 { 0x1f, 0x0000 },
2308 { 0x1f, 0x0000 },
2309 { 0x09, 0x2000 },
2310 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002311 };
2312
françois romieu4da19632011-01-03 15:07:55 +00002313 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002314
françois romieu4da19632011-01-03 15:07:55 +00002315 rtl_patchphy(tp, 0x14, 1 << 5);
2316 rtl_patchphy(tp, 0x0d, 1 << 5);
2317 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002318}
2319
françois romieu4da19632011-01-03 15:07:55 +00002320static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002321{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002322 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002323 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002324 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002325 { 0x03, 0x802f },
2326 { 0x02, 0x4f02 },
2327 { 0x01, 0x0409 },
2328 { 0x00, 0xf099 },
2329 { 0x04, 0x9800 },
2330 { 0x04, 0x9000 },
2331 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002332 { 0x1f, 0x0002 },
2333 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002334 { 0x06, 0x0761 },
2335 { 0x1f, 0x0003 },
2336 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002337 { 0x1f, 0x0000 }
2338 };
2339
françois romieu4da19632011-01-03 15:07:55 +00002340 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002341
françois romieu4da19632011-01-03 15:07:55 +00002342 rtl_patchphy(tp, 0x16, 1 << 0);
2343 rtl_patchphy(tp, 0x14, 1 << 5);
2344 rtl_patchphy(tp, 0x0d, 1 << 5);
2345 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002346}
2347
françois romieu4da19632011-01-03 15:07:55 +00002348static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002349{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002350 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002351 { 0x1f, 0x0001 },
2352 { 0x12, 0x2300 },
2353 { 0x1d, 0x3d98 },
2354 { 0x1f, 0x0002 },
2355 { 0x0c, 0x7eb8 },
2356 { 0x06, 0x5461 },
2357 { 0x1f, 0x0003 },
2358 { 0x16, 0x0f0a },
2359 { 0x1f, 0x0000 }
2360 };
2361
françois romieu4da19632011-01-03 15:07:55 +00002362 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002363
françois romieu4da19632011-01-03 15:07:55 +00002364 rtl_patchphy(tp, 0x16, 1 << 0);
2365 rtl_patchphy(tp, 0x14, 1 << 5);
2366 rtl_patchphy(tp, 0x0d, 1 << 5);
2367 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002368}
2369
françois romieu4da19632011-01-03 15:07:55 +00002370static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002371{
françois romieu4da19632011-01-03 15:07:55 +00002372 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002373}
2374
françois romieubca03d52011-01-03 15:07:31 +00002375static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002376{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002377 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002378 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002379 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002380 { 0x06, 0x4064 },
2381 { 0x07, 0x2863 },
2382 { 0x08, 0x059c },
2383 { 0x09, 0x26b4 },
2384 { 0x0a, 0x6a19 },
2385 { 0x0b, 0xdcc8 },
2386 { 0x10, 0xf06d },
2387 { 0x14, 0x7f68 },
2388 { 0x18, 0x7fd9 },
2389 { 0x1c, 0xf0ff },
2390 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002391 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002392 { 0x12, 0xf49f },
2393 { 0x13, 0x070b },
2394 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002395 { 0x14, 0x94c0 },
2396
2397 /*
2398 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002399 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002400 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002401 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002402 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002403 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002404 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002405 { 0x06, 0x5561 },
2406
2407 /*
2408 * Can not link to 1Gbps with bad cable
2409 * Decrease SNR threshold form 21.07dB to 19.04dB
2410 */
2411 { 0x1f, 0x0001 },
2412 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002413
2414 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002415 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002416 };
françois romieubca03d52011-01-03 15:07:31 +00002417 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002418
françois romieu4da19632011-01-03 15:07:55 +00002419 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002420
françois romieubca03d52011-01-03 15:07:31 +00002421 /*
2422 * Rx Error Issue
2423 * Fine Tune Switching regulator parameter
2424 */
françois romieu4da19632011-01-03 15:07:55 +00002425 rtl_writephy(tp, 0x1f, 0x0002);
2426 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2427 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002428
françois romieudaf9df62009-10-07 12:44:20 +00002429 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002430 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002431 { 0x1f, 0x0002 },
2432 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002433 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002434 { 0x05, 0x8330 },
2435 { 0x06, 0x669a },
2436 { 0x1f, 0x0002 }
2437 };
2438 int val;
2439
françois romieu4da19632011-01-03 15:07:55 +00002440 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002441
françois romieu4da19632011-01-03 15:07:55 +00002442 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002443
2444 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002445 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002446 0x0065, 0x0066, 0x0067, 0x0068,
2447 0x0069, 0x006a, 0x006b, 0x006c
2448 };
2449 int i;
2450
françois romieu4da19632011-01-03 15:07:55 +00002451 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002452
2453 val &= 0xff00;
2454 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002455 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002456 }
2457 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002458 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002459 { 0x1f, 0x0002 },
2460 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002461 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002462 { 0x05, 0x8330 },
2463 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002464 };
2465
françois romieu4da19632011-01-03 15:07:55 +00002466 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002467 }
2468
françois romieubca03d52011-01-03 15:07:31 +00002469 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002470 rtl_writephy(tp, 0x1f, 0x0002);
2471 rtl_patchphy(tp, 0x0d, 0x0300);
2472 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002473
françois romieubca03d52011-01-03 15:07:31 +00002474 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002475 rtl_writephy(tp, 0x1f, 0x0002);
2476 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2477 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002478
françois romieu4da19632011-01-03 15:07:55 +00002479 rtl_writephy(tp, 0x1f, 0x0005);
2480 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002481
2482 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002483
françois romieu4da19632011-01-03 15:07:55 +00002484 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002485}
2486
françois romieubca03d52011-01-03 15:07:31 +00002487static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002488{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002489 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002490 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002491 { 0x1f, 0x0001 },
2492 { 0x06, 0x4064 },
2493 { 0x07, 0x2863 },
2494 { 0x08, 0x059c },
2495 { 0x09, 0x26b4 },
2496 { 0x0a, 0x6a19 },
2497 { 0x0b, 0xdcc8 },
2498 { 0x10, 0xf06d },
2499 { 0x14, 0x7f68 },
2500 { 0x18, 0x7fd9 },
2501 { 0x1c, 0xf0ff },
2502 { 0x1d, 0x3d9c },
2503 { 0x1f, 0x0003 },
2504 { 0x12, 0xf49f },
2505 { 0x13, 0x070b },
2506 { 0x1a, 0x05ad },
2507 { 0x14, 0x94c0 },
2508
françois romieubca03d52011-01-03 15:07:31 +00002509 /*
2510 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002511 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002512 */
françois romieudaf9df62009-10-07 12:44:20 +00002513 { 0x1f, 0x0002 },
2514 { 0x06, 0x5561 },
2515 { 0x1f, 0x0005 },
2516 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002517 { 0x06, 0x5561 },
2518
2519 /*
2520 * Can not link to 1Gbps with bad cable
2521 * Decrease SNR threshold form 21.07dB to 19.04dB
2522 */
2523 { 0x1f, 0x0001 },
2524 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002525
2526 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002527 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002528 };
françois romieubca03d52011-01-03 15:07:31 +00002529 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002530
françois romieu4da19632011-01-03 15:07:55 +00002531 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002532
2533 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002534 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002535 { 0x1f, 0x0002 },
2536 { 0x05, 0x669a },
2537 { 0x1f, 0x0005 },
2538 { 0x05, 0x8330 },
2539 { 0x06, 0x669a },
2540
2541 { 0x1f, 0x0002 }
2542 };
2543 int val;
2544
françois romieu4da19632011-01-03 15:07:55 +00002545 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002546
françois romieu4da19632011-01-03 15:07:55 +00002547 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002548 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002549 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002550 0x0065, 0x0066, 0x0067, 0x0068,
2551 0x0069, 0x006a, 0x006b, 0x006c
2552 };
2553 int i;
2554
françois romieu4da19632011-01-03 15:07:55 +00002555 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002556
2557 val &= 0xff00;
2558 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002559 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002560 }
2561 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002562 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002563 { 0x1f, 0x0002 },
2564 { 0x05, 0x2642 },
2565 { 0x1f, 0x0005 },
2566 { 0x05, 0x8330 },
2567 { 0x06, 0x2642 }
2568 };
2569
françois romieu4da19632011-01-03 15:07:55 +00002570 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002571 }
2572
françois romieubca03d52011-01-03 15:07:31 +00002573 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002574 rtl_writephy(tp, 0x1f, 0x0002);
2575 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2576 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002577
françois romieubca03d52011-01-03 15:07:31 +00002578 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002579 rtl_writephy(tp, 0x1f, 0x0002);
2580 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002581
françois romieu4da19632011-01-03 15:07:55 +00002582 rtl_writephy(tp, 0x1f, 0x0005);
2583 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002584
2585 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002586
françois romieu4da19632011-01-03 15:07:55 +00002587 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002588}
2589
françois romieu4da19632011-01-03 15:07:55 +00002590static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002591{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002592 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002593 { 0x1f, 0x0002 },
2594 { 0x10, 0x0008 },
2595 { 0x0d, 0x006c },
2596
2597 { 0x1f, 0x0000 },
2598 { 0x0d, 0xf880 },
2599
2600 { 0x1f, 0x0001 },
2601 { 0x17, 0x0cc0 },
2602
2603 { 0x1f, 0x0001 },
2604 { 0x0b, 0xa4d8 },
2605 { 0x09, 0x281c },
2606 { 0x07, 0x2883 },
2607 { 0x0a, 0x6b35 },
2608 { 0x1d, 0x3da4 },
2609 { 0x1c, 0xeffd },
2610 { 0x14, 0x7f52 },
2611 { 0x18, 0x7fc6 },
2612 { 0x08, 0x0601 },
2613 { 0x06, 0x4063 },
2614 { 0x10, 0xf074 },
2615 { 0x1f, 0x0003 },
2616 { 0x13, 0x0789 },
2617 { 0x12, 0xf4bd },
2618 { 0x1a, 0x04fd },
2619 { 0x14, 0x84b0 },
2620 { 0x1f, 0x0000 },
2621 { 0x00, 0x9200 },
2622
2623 { 0x1f, 0x0005 },
2624 { 0x01, 0x0340 },
2625 { 0x1f, 0x0001 },
2626 { 0x04, 0x4000 },
2627 { 0x03, 0x1d21 },
2628 { 0x02, 0x0c32 },
2629 { 0x01, 0x0200 },
2630 { 0x00, 0x5554 },
2631 { 0x04, 0x4800 },
2632 { 0x04, 0x4000 },
2633 { 0x04, 0xf000 },
2634 { 0x03, 0xdf01 },
2635 { 0x02, 0xdf20 },
2636 { 0x01, 0x101a },
2637 { 0x00, 0xa0ff },
2638 { 0x04, 0xf800 },
2639 { 0x04, 0xf000 },
2640 { 0x1f, 0x0000 },
2641
2642 { 0x1f, 0x0007 },
2643 { 0x1e, 0x0023 },
2644 { 0x16, 0x0000 },
2645 { 0x1f, 0x0000 }
2646 };
2647
françois romieu4da19632011-01-03 15:07:55 +00002648 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002649}
2650
françois romieue6de30d2011-01-03 15:08:37 +00002651static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2652{
2653 static const struct phy_reg phy_reg_init[] = {
2654 { 0x1f, 0x0001 },
2655 { 0x17, 0x0cc0 },
2656
2657 { 0x1f, 0x0007 },
2658 { 0x1e, 0x002d },
2659 { 0x18, 0x0040 },
2660 { 0x1f, 0x0000 }
2661 };
2662
2663 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2664 rtl_patchphy(tp, 0x0d, 1 << 5);
2665}
2666
hayeswang01dc7fe2011-03-21 01:50:28 +00002667static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2668{
2669 static const struct phy_reg phy_reg_init[] = {
2670 /* Enable Delay cap */
2671 { 0x1f, 0x0005 },
2672 { 0x05, 0x8b80 },
2673 { 0x06, 0xc896 },
2674 { 0x1f, 0x0000 },
2675
2676 /* Channel estimation fine tune */
2677 { 0x1f, 0x0001 },
2678 { 0x0b, 0x6c20 },
2679 { 0x07, 0x2872 },
2680 { 0x1c, 0xefff },
2681 { 0x1f, 0x0003 },
2682 { 0x14, 0x6420 },
2683 { 0x1f, 0x0000 },
2684
2685 /* Update PFM & 10M TX idle timer */
2686 { 0x1f, 0x0007 },
2687 { 0x1e, 0x002f },
2688 { 0x15, 0x1919 },
2689 { 0x1f, 0x0000 },
2690
2691 { 0x1f, 0x0007 },
2692 { 0x1e, 0x00ac },
2693 { 0x18, 0x0006 },
2694 { 0x1f, 0x0000 }
2695 };
2696
Francois Romieu15ecd032011-04-27 13:52:22 -07002697 rtl_apply_firmware(tp);
2698
hayeswang01dc7fe2011-03-21 01:50:28 +00002699 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2700
2701 /* DCO enable for 10M IDLE Power */
2702 rtl_writephy(tp, 0x1f, 0x0007);
2703 rtl_writephy(tp, 0x1e, 0x0023);
2704 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2705 rtl_writephy(tp, 0x1f, 0x0000);
2706
2707 /* For impedance matching */
2708 rtl_writephy(tp, 0x1f, 0x0002);
2709 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002710 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002711
2712 /* PHY auto speed down */
2713 rtl_writephy(tp, 0x1f, 0x0007);
2714 rtl_writephy(tp, 0x1e, 0x002d);
2715 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2716 rtl_writephy(tp, 0x1f, 0x0000);
2717 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2718
2719 rtl_writephy(tp, 0x1f, 0x0005);
2720 rtl_writephy(tp, 0x05, 0x8b86);
2721 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2722 rtl_writephy(tp, 0x1f, 0x0000);
2723
2724 rtl_writephy(tp, 0x1f, 0x0005);
2725 rtl_writephy(tp, 0x05, 0x8b85);
2726 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2727 rtl_writephy(tp, 0x1f, 0x0007);
2728 rtl_writephy(tp, 0x1e, 0x0020);
2729 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2730 rtl_writephy(tp, 0x1f, 0x0006);
2731 rtl_writephy(tp, 0x00, 0x5a00);
2732 rtl_writephy(tp, 0x1f, 0x0000);
2733 rtl_writephy(tp, 0x0d, 0x0007);
2734 rtl_writephy(tp, 0x0e, 0x003c);
2735 rtl_writephy(tp, 0x0d, 0x4007);
2736 rtl_writephy(tp, 0x0e, 0x0000);
2737 rtl_writephy(tp, 0x0d, 0x0000);
2738}
2739
françois romieu4da19632011-01-03 15:07:55 +00002740static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002741{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002742 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002743 { 0x1f, 0x0003 },
2744 { 0x08, 0x441d },
2745 { 0x01, 0x9100 },
2746 { 0x1f, 0x0000 }
2747 };
2748
françois romieu4da19632011-01-03 15:07:55 +00002749 rtl_writephy(tp, 0x1f, 0x0000);
2750 rtl_patchphy(tp, 0x11, 1 << 12);
2751 rtl_patchphy(tp, 0x19, 1 << 13);
2752 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002753
françois romieu4da19632011-01-03 15:07:55 +00002754 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002755}
2756
Hayes Wang5a5e4442011-02-22 17:26:21 +08002757static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2758{
2759 static const struct phy_reg phy_reg_init[] = {
2760 { 0x1f, 0x0005 },
2761 { 0x1a, 0x0000 },
2762 { 0x1f, 0x0000 },
2763
2764 { 0x1f, 0x0004 },
2765 { 0x1c, 0x0000 },
2766 { 0x1f, 0x0000 },
2767
2768 { 0x1f, 0x0001 },
2769 { 0x15, 0x7701 },
2770 { 0x1f, 0x0000 }
2771 };
2772
2773 /* Disable ALDPS before ram code */
2774 rtl_writephy(tp, 0x1f, 0x0000);
2775 rtl_writephy(tp, 0x18, 0x0310);
2776 msleep(100);
2777
François Romieu953a12c2011-04-24 17:38:48 +02002778 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002779
2780 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2781}
2782
Francois Romieu5615d9f2007-08-17 17:50:46 +02002783static void rtl_hw_phy_config(struct net_device *dev)
2784{
2785 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002786
2787 rtl8169_print_mac_version(tp);
2788
2789 switch (tp->mac_version) {
2790 case RTL_GIGA_MAC_VER_01:
2791 break;
2792 case RTL_GIGA_MAC_VER_02:
2793 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002794 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002795 break;
2796 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002797 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002798 break;
françois romieu2e9558562009-08-10 19:44:19 +00002799 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002800 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002801 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002802 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002803 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002804 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002805 case RTL_GIGA_MAC_VER_07:
2806 case RTL_GIGA_MAC_VER_08:
2807 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002808 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002809 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002810 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002811 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002812 break;
2813 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002814 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002815 break;
2816 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002817 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002818 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002819 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002820 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002821 break;
2822 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002823 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002824 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002825 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002826 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002827 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002828 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002829 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002830 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002831 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002832 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002833 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002834 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002835 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002836 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002837 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002838 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002839 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002840 break;
2841 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002842 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002843 break;
2844 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002845 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002846 break;
françois romieue6de30d2011-01-03 15:08:37 +00002847 case RTL_GIGA_MAC_VER_28:
2848 rtl8168d_4_hw_phy_config(tp);
2849 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002850 case RTL_GIGA_MAC_VER_29:
2851 case RTL_GIGA_MAC_VER_30:
2852 rtl8105e_hw_phy_config(tp);
2853 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002854 case RTL_GIGA_MAC_VER_31:
2855 /* None. */
2856 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002857 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002858 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002859 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002860 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002861
Francois Romieu5615d9f2007-08-17 17:50:46 +02002862 default:
2863 break;
2864 }
2865}
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867static void rtl8169_phy_timer(unsigned long __opaque)
2868{
2869 struct net_device *dev = (struct net_device *)__opaque;
2870 struct rtl8169_private *tp = netdev_priv(dev);
2871 struct timer_list *timer = &tp->timer;
2872 void __iomem *ioaddr = tp->mmio_addr;
2873 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2874
Francois Romieubcf0bf92006-07-26 23:14:13 +02002875 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 spin_lock_irq(&tp->lock);
2878
françois romieu4da19632011-01-03 15:07:55 +00002879 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002880 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 * A busy loop could burn quite a few cycles on nowadays CPU.
2882 * Let's delay the execution of the timer for a few ticks.
2883 */
2884 timeout = HZ/10;
2885 goto out_mod_timer;
2886 }
2887
2888 if (tp->link_ok(ioaddr))
2889 goto out_unlock;
2890
Joe Perchesbf82c182010-02-09 11:49:50 +00002891 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
françois romieu4da19632011-01-03 15:07:55 +00002893 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895out_mod_timer:
2896 mod_timer(timer, jiffies + timeout);
2897out_unlock:
2898 spin_unlock_irq(&tp->lock);
2899}
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901#ifdef CONFIG_NET_POLL_CONTROLLER
2902/*
2903 * Polling 'interrupt' - used by things like netconsole to send skbs
2904 * without having to re-enable interrupts. It's not called while
2905 * the interrupt routine is executing.
2906 */
2907static void rtl8169_netpoll(struct net_device *dev)
2908{
2909 struct rtl8169_private *tp = netdev_priv(dev);
2910 struct pci_dev *pdev = tp->pci_dev;
2911
2912 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002913 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 enable_irq(pdev->irq);
2915}
2916#endif
2917
2918static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2919 void __iomem *ioaddr)
2920{
2921 iounmap(ioaddr);
2922 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002923 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 pci_disable_device(pdev);
2925 free_netdev(dev);
2926}
2927
Francois Romieubf793292006-11-01 00:53:05 +01002928static void rtl8169_phy_reset(struct net_device *dev,
2929 struct rtl8169_private *tp)
2930{
Francois Romieu07d3f512007-02-21 22:40:46 +01002931 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002932
françois romieu4da19632011-01-03 15:07:55 +00002933 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002934 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002935 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002936 return;
2937 msleep(1);
2938 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002939 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002940}
2941
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002942static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002944 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002945
Francois Romieu5615d9f2007-08-17 17:50:46 +02002946 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002947
Marcus Sundberg773328942008-07-10 21:28:08 +02002948 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2949 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2950 RTL_W8(0x82, 0x01);
2951 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002952
Francois Romieu6dccd162007-02-13 23:38:05 +01002953 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2954
2955 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2956 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002957
Francois Romieubcf0bf92006-07-26 23:14:13 +02002958 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002959 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2960 RTL_W8(0x82, 0x01);
2961 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002962 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002963 }
2964
Francois Romieubf793292006-11-01 00:53:05 +01002965 rtl8169_phy_reset(dev, tp);
2966
Oliver Neukum54405cd2011-01-06 21:55:13 +01002967 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002968 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2969 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2970 (tp->mii.supports_gmii ?
2971 ADVERTISED_1000baseT_Half |
2972 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002973
Joe Perchesbf82c182010-02-09 11:49:50 +00002974 if (RTL_R8(PHYstatus) & TBI_Enable)
2975 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002976}
2977
Francois Romieu773d2022007-01-31 23:47:43 +01002978static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2979{
2980 void __iomem *ioaddr = tp->mmio_addr;
2981 u32 high;
2982 u32 low;
2983
2984 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2985 high = addr[4] | (addr[5] << 8);
2986
2987 spin_lock_irq(&tp->lock);
2988
2989 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002990
Francois Romieu773d2022007-01-31 23:47:43 +01002991 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002992 RTL_R32(MAC4);
2993
Francois Romieu78f1cd02010-03-27 19:35:46 -07002994 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002995 RTL_R32(MAC0);
2996
Francois Romieu773d2022007-01-31 23:47:43 +01002997 RTL_W8(Cfg9346, Cfg9346_Lock);
2998
2999 spin_unlock_irq(&tp->lock);
3000}
3001
3002static int rtl_set_mac_address(struct net_device *dev, void *p)
3003{
3004 struct rtl8169_private *tp = netdev_priv(dev);
3005 struct sockaddr *addr = p;
3006
3007 if (!is_valid_ether_addr(addr->sa_data))
3008 return -EADDRNOTAVAIL;
3009
3010 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3011
3012 rtl_rar_set(tp, dev->dev_addr);
3013
3014 return 0;
3015}
3016
Francois Romieu5f787a12006-08-17 13:02:36 +02003017static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3018{
3019 struct rtl8169_private *tp = netdev_priv(dev);
3020 struct mii_ioctl_data *data = if_mii(ifr);
3021
Francois Romieu8b4ab282008-11-19 22:05:25 -08003022 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
3023}
Francois Romieu5f787a12006-08-17 13:02:36 +02003024
Francois Romieucecb5fd2011-04-01 10:21:07 +02003025static int rtl_xmii_ioctl(struct rtl8169_private *tp,
3026 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08003027{
Francois Romieu5f787a12006-08-17 13:02:36 +02003028 switch (cmd) {
3029 case SIOCGMIIPHY:
3030 data->phy_id = 32; /* Internal PHY */
3031 return 0;
3032
3033 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003034 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02003035 return 0;
3036
3037 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003038 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003039 return 0;
3040 }
3041 return -EOPNOTSUPP;
3042}
3043
Francois Romieu8b4ab282008-11-19 22:05:25 -08003044static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3045{
3046 return -EOPNOTSUPP;
3047}
3048
Francois Romieu0e485152007-02-20 00:00:26 +01003049static const struct rtl_cfg_info {
3050 void (*hw_start)(struct net_device *);
3051 unsigned int region;
3052 unsigned int align;
3053 u16 intr_event;
3054 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003055 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003056 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003057} rtl_cfg_infos [] = {
3058 [RTL_CFG_0] = {
3059 .hw_start = rtl_hw_start_8169,
3060 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003061 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003062 .intr_event = SYSErr | LinkChg | RxOverflow |
3063 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003064 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003065 .features = RTL_FEATURE_GMII,
3066 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003067 },
3068 [RTL_CFG_1] = {
3069 .hw_start = rtl_hw_start_8168,
3070 .region = 2,
3071 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003072 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003073 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003074 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003075 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3076 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003077 },
3078 [RTL_CFG_2] = {
3079 .hw_start = rtl_hw_start_8101,
3080 .region = 2,
3081 .align = 8,
3082 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3083 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003084 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003085 .features = RTL_FEATURE_MSI,
3086 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003087 }
3088};
3089
Francois Romieufbac58f2007-10-04 22:51:38 +02003090/* Cfg9346_Unlock assumed. */
3091static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3092 const struct rtl_cfg_info *cfg)
3093{
3094 unsigned msi = 0;
3095 u8 cfg2;
3096
3097 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003098 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003099 if (pci_enable_msi(pdev)) {
3100 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3101 } else {
3102 cfg2 |= MSIEnable;
3103 msi = RTL_FEATURE_MSI;
3104 }
3105 }
3106 RTL_W8(Config2, cfg2);
3107 return msi;
3108}
3109
3110static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3111{
3112 if (tp->features & RTL_FEATURE_MSI) {
3113 pci_disable_msi(pdev);
3114 tp->features &= ~RTL_FEATURE_MSI;
3115 }
3116}
3117
Francois Romieu8b4ab282008-11-19 22:05:25 -08003118static const struct net_device_ops rtl8169_netdev_ops = {
3119 .ndo_open = rtl8169_open,
3120 .ndo_stop = rtl8169_close,
3121 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003122 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003123 .ndo_tx_timeout = rtl8169_tx_timeout,
3124 .ndo_validate_addr = eth_validate_addr,
3125 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003126 .ndo_fix_features = rtl8169_fix_features,
3127 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003128 .ndo_set_mac_address = rtl_set_mac_address,
3129 .ndo_do_ioctl = rtl8169_ioctl,
3130 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003131#ifdef CONFIG_NET_POLL_CONTROLLER
3132 .ndo_poll_controller = rtl8169_netpoll,
3133#endif
3134
3135};
3136
françois romieuc0e45c12011-01-03 15:08:04 +00003137static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3138{
3139 struct mdio_ops *ops = &tp->mdio_ops;
3140
3141 switch (tp->mac_version) {
3142 case RTL_GIGA_MAC_VER_27:
3143 ops->write = r8168dp_1_mdio_write;
3144 ops->read = r8168dp_1_mdio_read;
3145 break;
françois romieue6de30d2011-01-03 15:08:37 +00003146 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003147 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003148 ops->write = r8168dp_2_mdio_write;
3149 ops->read = r8168dp_2_mdio_read;
3150 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003151 default:
3152 ops->write = r8169_mdio_write;
3153 ops->read = r8169_mdio_read;
3154 break;
3155 }
3156}
3157
françois romieu065c27c2011-01-03 15:08:12 +00003158static void r810x_phy_power_down(struct rtl8169_private *tp)
3159{
3160 rtl_writephy(tp, 0x1f, 0x0000);
3161 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3162}
3163
3164static void r810x_phy_power_up(struct rtl8169_private *tp)
3165{
3166 rtl_writephy(tp, 0x1f, 0x0000);
3167 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3168}
3169
3170static void r810x_pll_power_down(struct rtl8169_private *tp)
3171{
3172 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3173 rtl_writephy(tp, 0x1f, 0x0000);
3174 rtl_writephy(tp, MII_BMCR, 0x0000);
3175 return;
3176 }
3177
3178 r810x_phy_power_down(tp);
3179}
3180
3181static void r810x_pll_power_up(struct rtl8169_private *tp)
3182{
3183 r810x_phy_power_up(tp);
3184}
3185
3186static void r8168_phy_power_up(struct rtl8169_private *tp)
3187{
3188 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003189 switch (tp->mac_version) {
3190 case RTL_GIGA_MAC_VER_11:
3191 case RTL_GIGA_MAC_VER_12:
3192 case RTL_GIGA_MAC_VER_17:
3193 case RTL_GIGA_MAC_VER_18:
3194 case RTL_GIGA_MAC_VER_19:
3195 case RTL_GIGA_MAC_VER_20:
3196 case RTL_GIGA_MAC_VER_21:
3197 case RTL_GIGA_MAC_VER_22:
3198 case RTL_GIGA_MAC_VER_23:
3199 case RTL_GIGA_MAC_VER_24:
3200 case RTL_GIGA_MAC_VER_25:
3201 case RTL_GIGA_MAC_VER_26:
3202 case RTL_GIGA_MAC_VER_27:
3203 case RTL_GIGA_MAC_VER_28:
3204 case RTL_GIGA_MAC_VER_31:
3205 rtl_writephy(tp, 0x0e, 0x0000);
3206 break;
3207 default:
3208 break;
3209 }
françois romieu065c27c2011-01-03 15:08:12 +00003210 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3211}
3212
3213static void r8168_phy_power_down(struct rtl8169_private *tp)
3214{
3215 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003216 switch (tp->mac_version) {
3217 case RTL_GIGA_MAC_VER_32:
3218 case RTL_GIGA_MAC_VER_33:
3219 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3220 break;
3221
3222 case RTL_GIGA_MAC_VER_11:
3223 case RTL_GIGA_MAC_VER_12:
3224 case RTL_GIGA_MAC_VER_17:
3225 case RTL_GIGA_MAC_VER_18:
3226 case RTL_GIGA_MAC_VER_19:
3227 case RTL_GIGA_MAC_VER_20:
3228 case RTL_GIGA_MAC_VER_21:
3229 case RTL_GIGA_MAC_VER_22:
3230 case RTL_GIGA_MAC_VER_23:
3231 case RTL_GIGA_MAC_VER_24:
3232 case RTL_GIGA_MAC_VER_25:
3233 case RTL_GIGA_MAC_VER_26:
3234 case RTL_GIGA_MAC_VER_27:
3235 case RTL_GIGA_MAC_VER_28:
3236 case RTL_GIGA_MAC_VER_31:
3237 rtl_writephy(tp, 0x0e, 0x0200);
3238 default:
3239 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3240 break;
3241 }
françois romieu065c27c2011-01-03 15:08:12 +00003242}
3243
3244static void r8168_pll_power_down(struct rtl8169_private *tp)
3245{
3246 void __iomem *ioaddr = tp->mmio_addr;
3247
Francois Romieucecb5fd2011-04-01 10:21:07 +02003248 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3249 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3250 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003251 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003252 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003253 }
françois romieu065c27c2011-01-03 15:08:12 +00003254
Francois Romieucecb5fd2011-04-01 10:21:07 +02003255 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3256 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003257 (RTL_R16(CPlusCmd) & ASF)) {
3258 return;
3259 }
3260
hayeswang01dc7fe2011-03-21 01:50:28 +00003261 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3262 tp->mac_version == RTL_GIGA_MAC_VER_33)
3263 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3264
françois romieu065c27c2011-01-03 15:08:12 +00003265 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3266 rtl_writephy(tp, 0x1f, 0x0000);
3267 rtl_writephy(tp, MII_BMCR, 0x0000);
3268
3269 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3270 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3271 return;
3272 }
3273
3274 r8168_phy_power_down(tp);
3275
3276 switch (tp->mac_version) {
3277 case RTL_GIGA_MAC_VER_25:
3278 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003279 case RTL_GIGA_MAC_VER_27:
3280 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003281 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003282 case RTL_GIGA_MAC_VER_32:
3283 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003284 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3285 break;
3286 }
3287}
3288
3289static void r8168_pll_power_up(struct rtl8169_private *tp)
3290{
3291 void __iomem *ioaddr = tp->mmio_addr;
3292
Francois Romieucecb5fd2011-04-01 10:21:07 +02003293 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3294 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3295 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003296 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003297 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003298 }
françois romieu065c27c2011-01-03 15:08:12 +00003299
3300 switch (tp->mac_version) {
3301 case RTL_GIGA_MAC_VER_25:
3302 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003303 case RTL_GIGA_MAC_VER_27:
3304 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003305 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003306 case RTL_GIGA_MAC_VER_32:
3307 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003308 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3309 break;
3310 }
3311
3312 r8168_phy_power_up(tp);
3313}
3314
3315static void rtl_pll_power_op(struct rtl8169_private *tp,
3316 void (*op)(struct rtl8169_private *))
3317{
3318 if (op)
3319 op(tp);
3320}
3321
3322static void rtl_pll_power_down(struct rtl8169_private *tp)
3323{
3324 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3325}
3326
3327static void rtl_pll_power_up(struct rtl8169_private *tp)
3328{
3329 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3330}
3331
3332static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3333{
3334 struct pll_power_ops *ops = &tp->pll_power_ops;
3335
3336 switch (tp->mac_version) {
3337 case RTL_GIGA_MAC_VER_07:
3338 case RTL_GIGA_MAC_VER_08:
3339 case RTL_GIGA_MAC_VER_09:
3340 case RTL_GIGA_MAC_VER_10:
3341 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003342 case RTL_GIGA_MAC_VER_29:
3343 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003344 ops->down = r810x_pll_power_down;
3345 ops->up = r810x_pll_power_up;
3346 break;
3347
3348 case RTL_GIGA_MAC_VER_11:
3349 case RTL_GIGA_MAC_VER_12:
3350 case RTL_GIGA_MAC_VER_17:
3351 case RTL_GIGA_MAC_VER_18:
3352 case RTL_GIGA_MAC_VER_19:
3353 case RTL_GIGA_MAC_VER_20:
3354 case RTL_GIGA_MAC_VER_21:
3355 case RTL_GIGA_MAC_VER_22:
3356 case RTL_GIGA_MAC_VER_23:
3357 case RTL_GIGA_MAC_VER_24:
3358 case RTL_GIGA_MAC_VER_25:
3359 case RTL_GIGA_MAC_VER_26:
3360 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003361 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003362 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003363 case RTL_GIGA_MAC_VER_32:
3364 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003365 ops->down = r8168_pll_power_down;
3366 ops->up = r8168_pll_power_up;
3367 break;
3368
3369 default:
3370 ops->down = NULL;
3371 ops->up = NULL;
3372 break;
3373 }
3374}
3375
Hayes Wang92fc43b2011-07-06 15:58:03 +08003376static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
3377{
3378 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
3379}
3380
Francois Romieu6f43adc2011-04-29 15:05:51 +02003381static void rtl_hw_reset(struct rtl8169_private *tp)
3382{
3383 void __iomem *ioaddr = tp->mmio_addr;
3384 int i;
3385
3386 /* Soft reset the chip. */
3387 RTL_W8(ChipCmd, CmdReset);
3388
3389 /* Check that the chip has finished the reset. */
3390 for (i = 0; i < 100; i++) {
3391 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3392 break;
Hayes Wang92fc43b2011-07-06 15:58:03 +08003393 udelay(100);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003394 }
Hayes Wang92fc43b2011-07-06 15:58:03 +08003395
3396 rtl8169_init_ring_indexes(tp);
Francois Romieu6f43adc2011-04-29 15:05:51 +02003397}
3398
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003399static int __devinit
3400rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3401{
Francois Romieu0e485152007-02-20 00:00:26 +01003402 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3403 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003405 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003406 struct net_device *dev;
3407 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003408 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003409 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003411 if (netif_msg_drv(&debug)) {
3412 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3413 MODULENAME, RTL8169_VERSION);
3414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003417 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003418 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003419 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003420 rc = -ENOMEM;
3421 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 }
3423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003425 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003427 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003428 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003429 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Francois Romieuccdffb92008-07-26 14:26:06 +02003431 mii = &tp->mii;
3432 mii->dev = dev;
3433 mii->mdio_read = rtl_mdio_read;
3434 mii->mdio_write = rtl_mdio_write;
3435 mii->phy_id_mask = 0x1f;
3436 mii->reg_num_mask = 0x1f;
3437 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3438
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003439 /* disable ASPM completely as that cause random device stop working
3440 * problems as well as full system hangs for some PCIe devices users */
3441 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3442 PCIE_LINK_STATE_CLKPM);
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3445 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003446 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003447 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003448 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 }
3450
françois romieu87aeec72010-04-26 11:42:06 +00003451 if (pci_set_mwi(pdev) < 0)
3452 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003455 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003456 netif_err(tp, probe, dev,
3457 "region #%d not an MMIO resource, aborting\n",
3458 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003460 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003464 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003465 netif_err(tp, probe, dev,
3466 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003468 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 }
3470
3471 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003472 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003473 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003474 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 }
3476
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003477 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
3479 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003480 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 tp->cp_cmd |= PCIDAC;
3482 dev->features |= NETIF_F_HIGHDMA;
3483 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003484 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003486 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003487 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 }
3489 }
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003492 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003493 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003494 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003496 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003498 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Jon Masone44daad2011-06-27 07:46:31 +00003500 if (!pci_is_pcie(pdev))
3501 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07003502
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003503 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
Francois Romieu6f43adc2011-04-29 15:05:51 +02003505 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003507 RTL_W16(IntrStatus, 0xffff);
3508
françois romieuca52efd2009-07-24 12:34:19 +00003509 pci_set_master(pdev);
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 /* Identify chip attached to board */
Francois Romieu5d320a22011-05-08 17:47:36 +02003512 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Francois Romieu7a8fc772011-03-01 17:18:33 +01003514 /*
3515 * Pretend we are using VLANs; This bypasses a nasty bug where
3516 * Interrupts stop flowing on high load on 8110SCd controllers.
3517 */
3518 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3519 tp->cp_cmd |= RxVlan;
3520
françois romieuc0e45c12011-01-03 15:08:04 +00003521 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003522 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Francois Romieu85bffe62011-04-27 08:22:39 +02003526 chipset = tp->mac_version;
3527 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Francois Romieu5d06a992006-02-23 00:47:58 +01003529 RTL_W8(Cfg9346, Cfg9346_Unlock);
3530 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3531 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003532 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3533 tp->features |= RTL_FEATURE_WOL;
3534 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3535 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003536 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003537 RTL_W8(Cfg9346, Cfg9346_Lock);
3538
Francois Romieu66ec5d42007-11-06 22:56:10 +01003539 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3540 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 tp->set_speed = rtl8169_set_speed_tbi;
3542 tp->get_settings = rtl8169_gset_tbi;
3543 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3544 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3545 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003546 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 } else {
3548 tp->set_speed = rtl8169_set_speed_xmii;
3549 tp->get_settings = rtl8169_gset_xmii;
3550 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3551 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3552 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003553 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
3555
Francois Romieudf58ef52008-10-09 14:35:58 -07003556 spin_lock_init(&tp->lock);
3557
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003558 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 for (i = 0; i < MAC_ADDR_LEN; i++)
3560 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003561 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3565 dev->irq = pdev->irq;
3566 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003568 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Michał Mirosław350fb322011-04-08 06:35:56 +00003570 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3571 * properly for all devices */
3572 dev->features |= NETIF_F_RXCSUM |
3573 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3574
3575 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3576 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3577 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3578 NETIF_F_HIGHDMA;
3579
3580 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3581 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3582 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
3584 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003585 tp->hw_start = cfg->hw_start;
3586 tp->intr_event = cfg->intr_event;
3587 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
Francois Romieu2efa53f2007-03-09 00:00:05 +01003589 init_timer(&tp->timer);
3590 tp->timer.data = (unsigned long) dev;
3591 tp->timer.function = rtl8169_phy_timer;
3592
Francois Romieub6ffd972011-06-17 17:00:05 +02003593 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003596 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003597 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
3599 pci_set_drvdata(pdev, dev);
3600
Joe Perchesbf82c182010-02-09 11:49:50 +00003601 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003602 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003603 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
Francois Romieucecb5fd2011-04-01 10:21:07 +02003605 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3606 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3607 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003608 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003609 }
françois romieub646d902011-01-03 15:08:21 +00003610
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003611 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Alan Sternf3ec4f82010-06-08 15:23:51 -04003613 if (pci_dev_run_wake(pdev))
3614 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003615
Ivan Vecera0d672e92011-02-15 02:08:39 +00003616 netif_carrier_off(dev);
3617
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003618out:
3619 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
françois romieu87aeec72010-04-26 11:42:06 +00003621err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003622 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003623 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003624err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003625 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003626err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003627 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003628 pci_disable_device(pdev);
3629err_out_free_dev_1:
3630 free_netdev(dev);
3631 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
Francois Romieu07d3f512007-02-21 22:40:46 +01003634static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
3636 struct net_device *dev = pci_get_drvdata(pdev);
3637 struct rtl8169_private *tp = netdev_priv(dev);
3638
Francois Romieucecb5fd2011-04-01 10:21:07 +02003639 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3640 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3641 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003642 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003643 }
françois romieub646d902011-01-03 15:08:21 +00003644
Tejun Heo23f333a2010-12-12 16:45:14 +01003645 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003648
François Romieu953a12c2011-04-24 17:38:48 +02003649 rtl_release_firmware(tp);
3650
Alan Sternf3ec4f82010-06-08 15:23:51 -04003651 if (pci_dev_run_wake(pdev))
3652 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003653
Ivan Veceracc098dc2009-11-29 23:12:52 -08003654 /* restore original MAC address */
3655 rtl_rar_set(tp, dev->perm_addr);
3656
Francois Romieufbac58f2007-10-04 22:51:38 +02003657 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3659 pci_set_drvdata(pdev, NULL);
3660}
3661
Francois Romieub6ffd972011-06-17 17:00:05 +02003662static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3663{
3664 struct rtl_fw *rtl_fw;
3665 const char *name;
3666 int rc = -ENOMEM;
3667
3668 name = rtl_lookup_firmware_name(tp);
3669 if (!name)
3670 goto out_no_firmware;
3671
3672 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3673 if (!rtl_fw)
3674 goto err_warn;
3675
3676 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3677 if (rc < 0)
3678 goto err_free;
3679
Francois Romieufd112f22011-06-18 00:10:29 +02003680 rc = rtl_check_firmware(tp, rtl_fw);
3681 if (rc < 0)
3682 goto err_release_firmware;
3683
Francois Romieub6ffd972011-06-17 17:00:05 +02003684 tp->rtl_fw = rtl_fw;
3685out:
3686 return;
3687
Francois Romieufd112f22011-06-18 00:10:29 +02003688err_release_firmware:
3689 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003690err_free:
3691 kfree(rtl_fw);
3692err_warn:
3693 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3694 name, rc);
3695out_no_firmware:
3696 tp->rtl_fw = NULL;
3697 goto out;
3698}
3699
François Romieu953a12c2011-04-24 17:38:48 +02003700static void rtl_request_firmware(struct rtl8169_private *tp)
3701{
Francois Romieub6ffd972011-06-17 17:00:05 +02003702 if (IS_ERR(tp->rtl_fw))
3703 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003704}
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706static int rtl8169_open(struct net_device *dev)
3707{
3708 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003709 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003711 int retval = -ENOMEM;
3712
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003713 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Neil Hormanc0cd8842010-03-29 13:16:02 -07003715 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003717 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003719 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3720 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003722 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003724 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3725 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003727 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
3729 retval = rtl8169_init_ring(dev);
3730 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003731 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
David Howellsc4028952006-11-22 14:57:56 +00003733 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Francois Romieu99f252b2007-04-02 22:59:59 +02003735 smp_mb();
3736
François Romieu953a12c2011-04-24 17:38:48 +02003737 rtl_request_firmware(tp);
3738
Francois Romieufbac58f2007-10-04 22:51:38 +02003739 retval = request_irq(dev->irq, rtl8169_interrupt,
3740 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003741 dev->name, dev);
3742 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003743 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003744
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003745 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003746
françois romieueee3a962011-01-08 02:17:26 +00003747 rtl8169_init_phy(dev, tp);
3748
Michał Mirosław350fb322011-04-08 06:35:56 +00003749 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003750
françois romieu065c27c2011-01-03 15:08:12 +00003751 rtl_pll_power_up(tp);
3752
Francois Romieu07ce4062007-02-23 23:36:39 +01003753 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003755 tp->saved_wolopts = 0;
3756 pm_runtime_put_noidle(&pdev->dev);
3757
françois romieueee3a962011-01-08 02:17:26 +00003758 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759out:
3760 return retval;
3761
François Romieu953a12c2011-04-24 17:38:48 +02003762err_release_fw_2:
3763 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003764 rtl8169_rx_clear(tp);
3765err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003766 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3767 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003768 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003769err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003770 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3771 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003772 tp->TxDescArray = NULL;
3773err_pm_runtime_put:
3774 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 goto out;
3776}
3777
Hayes Wang92fc43b2011-07-06 15:58:03 +08003778static void rtl_rx_close(struct rtl8169_private *tp)
3779{
3780 void __iomem *ioaddr = tp->mmio_addr;
3781 u32 rxcfg = RTL_R32(RxConfig);
3782
3783 rxcfg &= ~(AcceptErr | AcceptRunt | AcceptBroadcast | AcceptMulticast |
3784 AcceptMyPhys | AcceptAllPhys);
3785 RTL_W32(RxConfig, rxcfg);
3786}
3787
françois romieue6de30d2011-01-03 15:08:37 +00003788static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
françois romieue6de30d2011-01-03 15:08:37 +00003790 void __iomem *ioaddr = tp->mmio_addr;
3791
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 /* Disable interrupts */
3793 rtl8169_irq_mask_and_ack(ioaddr);
3794
Hayes Wang92fc43b2011-07-06 15:58:03 +08003795 rtl_rx_close(tp);
3796
Hayes Wang5d2e1952011-02-22 17:26:22 +08003797 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003798 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3799 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003800 while (RTL_R8(TxPoll) & NPQ)
3801 udelay(20);
Hayes Wang92fc43b2011-07-06 15:58:03 +08003802 } else {
3803 RTL_W8(ChipCmd, RTL_R8(ChipCmd) | StopReq);
3804 udelay(100);
françois romieue6de30d2011-01-03 15:08:37 +00003805 }
3806
Hayes Wang92fc43b2011-07-06 15:58:03 +08003807 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808}
3809
Francois Romieu7f796d82007-06-11 23:04:41 +02003810static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003811{
3812 void __iomem *ioaddr = tp->mmio_addr;
3813 u32 cfg = rtl8169_rx_config;
3814
Francois Romieu2b7b4312011-04-18 22:53:24 -07003815 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003816 RTL_W32(RxConfig, cfg);
3817
3818 /* Set DMA burst size and Interframe Gap Time */
3819 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3820 (InterFrameGap << TxInterFrameGapShift));
3821}
3822
Francois Romieu07ce4062007-02-23 23:36:39 +01003823static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
3825 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Francois Romieu07ce4062007-02-23 23:36:39 +01003827 tp->hw_start(dev);
3828
Francois Romieu07ce4062007-02-23 23:36:39 +01003829 netif_start_queue(dev);
3830}
3831
Francois Romieu7f796d82007-06-11 23:04:41 +02003832static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3833 void __iomem *ioaddr)
3834{
3835 /*
3836 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3837 * register to be written before TxDescAddrLow to work.
3838 * Switching from MMIO to I/O access fixes the issue as well.
3839 */
3840 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003841 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003842 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003843 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003844}
3845
3846static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3847{
3848 u16 cmd;
3849
3850 cmd = RTL_R16(CPlusCmd);
3851 RTL_W16(CPlusCmd, cmd);
3852 return cmd;
3853}
3854
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003855static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003856{
3857 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003858 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003859}
3860
Francois Romieu6dccd162007-02-13 23:38:05 +01003861static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3862{
Francois Romieu37441002011-06-17 22:58:54 +02003863 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01003864 u32 mac_version;
3865 u32 clk;
3866 u32 val;
3867 } cfg2_info [] = {
3868 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3869 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3870 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3871 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02003872 };
3873 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01003874 unsigned int i;
3875 u32 clk;
3876
3877 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003878 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003879 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3880 RTL_W32(0x7c, p->val);
3881 break;
3882 }
3883 }
3884}
3885
Francois Romieu07ce4062007-02-23 23:36:39 +01003886static void rtl_hw_start_8169(struct net_device *dev)
3887{
3888 struct rtl8169_private *tp = netdev_priv(dev);
3889 void __iomem *ioaddr = tp->mmio_addr;
3890 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003891
Francois Romieu9cb427b2006-11-02 00:10:16 +01003892 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3893 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3894 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3895 }
3896
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003898 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3899 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3900 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3901 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003902 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3903
françois romieuf0298f82011-01-03 15:07:42 +00003904 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003906 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Francois Romieucecb5fd2011-04-01 10:21:07 +02003908 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3909 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3910 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3911 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02003912 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Francois Romieu7f796d82007-06-11 23:04:41 +02003914 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003915
Francois Romieucecb5fd2011-04-01 10:21:07 +02003916 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3917 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02003918 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003920 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 }
3922
Francois Romieubcf0bf92006-07-26 23:14:13 +02003923 RTL_W16(CPlusCmd, tp->cp_cmd);
3924
Francois Romieu6dccd162007-02-13 23:38:05 +01003925 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3926
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 /*
3928 * Undocumented corner. Supposedly:
3929 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3930 */
3931 RTL_W16(IntrMitigate, 0x0000);
3932
Francois Romieu7f796d82007-06-11 23:04:41 +02003933 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003934
Francois Romieucecb5fd2011-04-01 10:21:07 +02003935 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3936 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3937 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3938 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02003939 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3940 rtl_set_rx_tx_config_registers(tp);
3941 }
3942
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003944
3945 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3946 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 RTL_W32(RxMissed, 0);
3949
Francois Romieu07ce4062007-02-23 23:36:39 +01003950 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951
3952 /* no early-rx interrupts */
3953 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003954
3955 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003956 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003957}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
Francois Romieu9c14cea2008-07-05 00:21:15 +02003959static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003960{
Jon Masone44daad2011-06-27 07:46:31 +00003961 int cap = pci_pcie_cap(pdev);
Francois Romieu458a9f62008-08-02 15:50:02 +02003962
Francois Romieu9c14cea2008-07-05 00:21:15 +02003963 if (cap) {
3964 u16 ctl;
3965
3966 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3967 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3968 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3969 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003970}
3971
françois romieu650e8d52011-01-03 15:08:29 +00003972static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003973{
3974 u32 csi;
3975
3976 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003977 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3978}
3979
françois romieue6de30d2011-01-03 15:08:37 +00003980static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3981{
3982 rtl_csi_access_enable(ioaddr, 0x17000000);
3983}
3984
françois romieu650e8d52011-01-03 15:08:29 +00003985static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3986{
3987 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003988}
3989
3990struct ephy_info {
3991 unsigned int offset;
3992 u16 mask;
3993 u16 bits;
3994};
3995
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003996static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003997{
3998 u16 w;
3999
4000 while (len-- > 0) {
4001 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
4002 rtl_ephy_write(ioaddr, e->offset, w);
4003 e++;
4004 }
4005}
4006
Francois Romieub726e492008-06-28 12:22:59 +02004007static void rtl_disable_clock_request(struct pci_dev *pdev)
4008{
Jon Masone44daad2011-06-27 07:46:31 +00004009 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004010
4011 if (cap) {
4012 u16 ctl;
4013
4014 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4015 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
4016 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4017 }
4018}
4019
françois romieue6de30d2011-01-03 15:08:37 +00004020static void rtl_enable_clock_request(struct pci_dev *pdev)
4021{
Jon Masone44daad2011-06-27 07:46:31 +00004022 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00004023
4024 if (cap) {
4025 u16 ctl;
4026
4027 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
4028 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
4029 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
4030 }
4031}
4032
Francois Romieub726e492008-06-28 12:22:59 +02004033#define R8168_CPCMD_QUIRK_MASK (\
4034 EnableBist | \
4035 Mac_dbgo_oe | \
4036 Force_half_dup | \
4037 Force_rxflow_en | \
4038 Force_txflow_en | \
4039 Cxpl_dbg_sel | \
4040 ASF | \
4041 PktCntrDisable | \
4042 Mac_dbgo_sel)
4043
Francois Romieu219a1e92008-06-28 11:58:39 +02004044static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
4045{
Francois Romieub726e492008-06-28 12:22:59 +02004046 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4047
4048 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4049
Francois Romieu2e68ae42008-06-28 12:00:55 +02004050 rtl_tx_performance_tweak(pdev,
4051 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004052}
4053
4054static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4055{
4056 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004057
françois romieuf0298f82011-01-03 15:07:42 +00004058 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004059
4060 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004061}
4062
4063static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4064{
Francois Romieub726e492008-06-28 12:22:59 +02004065 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4066
4067 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4068
Francois Romieu219a1e92008-06-28 11:58:39 +02004069 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004070
4071 rtl_disable_clock_request(pdev);
4072
4073 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004074}
4075
Francois Romieuef3386f2008-06-29 12:24:30 +02004076static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004077{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004078 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004079 { 0x01, 0, 0x0001 },
4080 { 0x02, 0x0800, 0x1000 },
4081 { 0x03, 0, 0x0042 },
4082 { 0x06, 0x0080, 0x0000 },
4083 { 0x07, 0, 0x2000 }
4084 };
4085
françois romieu650e8d52011-01-03 15:08:29 +00004086 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004087
4088 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4089
Francois Romieu219a1e92008-06-28 11:58:39 +02004090 __rtl_hw_start_8168cp(ioaddr, pdev);
4091}
4092
Francois Romieuef3386f2008-06-29 12:24:30 +02004093static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4094{
françois romieu650e8d52011-01-03 15:08:29 +00004095 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004096
4097 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4098
4099 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4100
4101 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4102}
4103
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004104static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4105{
françois romieu650e8d52011-01-03 15:08:29 +00004106 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004107
4108 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4109
4110 /* Magic. */
4111 RTL_W8(DBG_REG, 0x20);
4112
françois romieuf0298f82011-01-03 15:07:42 +00004113 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004114
4115 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4116
4117 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4118}
4119
Francois Romieu219a1e92008-06-28 11:58:39 +02004120static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4121{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004122 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004123 { 0x02, 0x0800, 0x1000 },
4124 { 0x03, 0, 0x0002 },
4125 { 0x06, 0x0080, 0x0000 }
4126 };
4127
françois romieu650e8d52011-01-03 15:08:29 +00004128 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004129
4130 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4131
4132 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4133
Francois Romieu219a1e92008-06-28 11:58:39 +02004134 __rtl_hw_start_8168cp(ioaddr, pdev);
4135}
4136
4137static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4138{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004139 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004140 { 0x01, 0, 0x0001 },
4141 { 0x03, 0x0400, 0x0220 }
4142 };
4143
françois romieu650e8d52011-01-03 15:08:29 +00004144 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004145
4146 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4147
Francois Romieu219a1e92008-06-28 11:58:39 +02004148 __rtl_hw_start_8168cp(ioaddr, pdev);
4149}
4150
Francois Romieu197ff762008-06-28 13:16:02 +02004151static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4152{
4153 rtl_hw_start_8168c_2(ioaddr, pdev);
4154}
4155
Francois Romieu6fb07052008-06-29 11:54:28 +02004156static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4157{
françois romieu650e8d52011-01-03 15:08:29 +00004158 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004159
4160 __rtl_hw_start_8168cp(ioaddr, pdev);
4161}
4162
Francois Romieu5b538df2008-07-20 16:22:45 +02004163static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4164{
françois romieu650e8d52011-01-03 15:08:29 +00004165 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004166
4167 rtl_disable_clock_request(pdev);
4168
françois romieuf0298f82011-01-03 15:07:42 +00004169 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004170
4171 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4172
4173 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4174}
4175
hayeswang4804b3b2011-03-21 01:50:29 +00004176static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4177{
4178 rtl_csi_access_enable_1(ioaddr);
4179
4180 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4181
4182 RTL_W8(MaxTxPacketSize, TxPacketMax);
4183
4184 rtl_disable_clock_request(pdev);
4185}
4186
françois romieue6de30d2011-01-03 15:08:37 +00004187static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4188{
4189 static const struct ephy_info e_info_8168d_4[] = {
4190 { 0x0b, ~0, 0x48 },
4191 { 0x19, 0x20, 0x50 },
4192 { 0x0c, ~0, 0x20 }
4193 };
4194 int i;
4195
4196 rtl_csi_access_enable_1(ioaddr);
4197
4198 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4199
4200 RTL_W8(MaxTxPacketSize, TxPacketMax);
4201
4202 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4203 const struct ephy_info *e = e_info_8168d_4 + i;
4204 u16 w;
4205
4206 w = rtl_ephy_read(ioaddr, e->offset);
4207 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4208 }
4209
4210 rtl_enable_clock_request(pdev);
4211}
4212
hayeswang01dc7fe2011-03-21 01:50:28 +00004213static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4214{
4215 static const struct ephy_info e_info_8168e[] = {
4216 { 0x00, 0x0200, 0x0100 },
4217 { 0x00, 0x0000, 0x0004 },
4218 { 0x06, 0x0002, 0x0001 },
4219 { 0x06, 0x0000, 0x0030 },
4220 { 0x07, 0x0000, 0x2000 },
4221 { 0x00, 0x0000, 0x0020 },
4222 { 0x03, 0x5800, 0x2000 },
4223 { 0x03, 0x0000, 0x0001 },
4224 { 0x01, 0x0800, 0x1000 },
4225 { 0x07, 0x0000, 0x4000 },
4226 { 0x1e, 0x0000, 0x2000 },
4227 { 0x19, 0xffff, 0xfe6c },
4228 { 0x0a, 0x0000, 0x0040 }
4229 };
4230
4231 rtl_csi_access_enable_2(ioaddr);
4232
4233 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4234
4235 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4236
4237 RTL_W8(MaxTxPacketSize, TxPacketMax);
4238
4239 rtl_disable_clock_request(pdev);
4240
4241 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004242 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4243 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004244
Francois Romieucecb5fd2011-04-01 10:21:07 +02004245 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004246}
4247
Francois Romieu07ce4062007-02-23 23:36:39 +01004248static void rtl_hw_start_8168(struct net_device *dev)
4249{
Francois Romieu2dd99532007-06-11 23:22:52 +02004250 struct rtl8169_private *tp = netdev_priv(dev);
4251 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004252 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004253
4254 RTL_W8(Cfg9346, Cfg9346_Unlock);
4255
françois romieuf0298f82011-01-03 15:07:42 +00004256 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004257
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004258 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004259
Francois Romieu0e485152007-02-20 00:00:26 +01004260 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004261
4262 RTL_W16(CPlusCmd, tp->cp_cmd);
4263
Francois Romieu0e485152007-02-20 00:00:26 +01004264 RTL_W16(IntrMitigate, 0x5151);
4265
4266 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004267 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4268 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004269 tp->intr_event |= RxFIFOOver | PCSTimeout;
4270 tp->intr_event &= ~RxOverflow;
4271 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004272
4273 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4274
Francois Romieub8363902008-06-01 12:31:57 +02004275 rtl_set_rx_mode(dev);
4276
4277 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4278 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004279
4280 RTL_R8(IntrMask);
4281
Francois Romieu219a1e92008-06-28 11:58:39 +02004282 switch (tp->mac_version) {
4283 case RTL_GIGA_MAC_VER_11:
4284 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004285 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004286
4287 case RTL_GIGA_MAC_VER_12:
4288 case RTL_GIGA_MAC_VER_17:
4289 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004290 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004291
4292 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004293 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004294 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004295
4296 case RTL_GIGA_MAC_VER_19:
4297 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004298 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004299
4300 case RTL_GIGA_MAC_VER_20:
4301 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004302 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004303
Francois Romieu197ff762008-06-28 13:16:02 +02004304 case RTL_GIGA_MAC_VER_21:
4305 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004306 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004307
Francois Romieu6fb07052008-06-29 11:54:28 +02004308 case RTL_GIGA_MAC_VER_22:
4309 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004310 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004311
Francois Romieuef3386f2008-06-29 12:24:30 +02004312 case RTL_GIGA_MAC_VER_23:
4313 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004314 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004315
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004316 case RTL_GIGA_MAC_VER_24:
4317 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004318 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004319
Francois Romieu5b538df2008-07-20 16:22:45 +02004320 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004321 case RTL_GIGA_MAC_VER_26:
4322 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004323 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004324 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004325
françois romieue6de30d2011-01-03 15:08:37 +00004326 case RTL_GIGA_MAC_VER_28:
4327 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004328 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004329
hayeswang4804b3b2011-03-21 01:50:29 +00004330 case RTL_GIGA_MAC_VER_31:
4331 rtl_hw_start_8168dp(ioaddr, pdev);
4332 break;
4333
hayeswang01dc7fe2011-03-21 01:50:28 +00004334 case RTL_GIGA_MAC_VER_32:
4335 case RTL_GIGA_MAC_VER_33:
4336 rtl_hw_start_8168e(ioaddr, pdev);
4337 break;
françois romieue6de30d2011-01-03 15:08:37 +00004338
Francois Romieu219a1e92008-06-28 11:58:39 +02004339 default:
4340 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4341 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004342 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004343 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004344
Francois Romieu0e485152007-02-20 00:00:26 +01004345 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4346
Francois Romieub8363902008-06-01 12:31:57 +02004347 RTL_W8(Cfg9346, Cfg9346_Lock);
4348
Francois Romieu2dd99532007-06-11 23:22:52 +02004349 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004350
Francois Romieu0e485152007-02-20 00:00:26 +01004351 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004352}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Francois Romieu2857ffb2008-08-02 21:08:49 +02004354#define R810X_CPCMD_QUIRK_MASK (\
4355 EnableBist | \
4356 Mac_dbgo_oe | \
4357 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004358 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004359 Force_txflow_en | \
4360 Cxpl_dbg_sel | \
4361 ASF | \
4362 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004363 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004364
4365static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4366{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004367 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004368 { 0x01, 0, 0x6e65 },
4369 { 0x02, 0, 0x091f },
4370 { 0x03, 0, 0xc2f9 },
4371 { 0x06, 0, 0xafb5 },
4372 { 0x07, 0, 0x0e00 },
4373 { 0x19, 0, 0xec80 },
4374 { 0x01, 0, 0x2e65 },
4375 { 0x01, 0, 0x6e65 }
4376 };
4377 u8 cfg1;
4378
françois romieu650e8d52011-01-03 15:08:29 +00004379 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004380
4381 RTL_W8(DBG_REG, FIX_NAK_1);
4382
4383 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4384
4385 RTL_W8(Config1,
4386 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4387 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4388
4389 cfg1 = RTL_R8(Config1);
4390 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4391 RTL_W8(Config1, cfg1 & ~LEDS0);
4392
Francois Romieu2857ffb2008-08-02 21:08:49 +02004393 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4394}
4395
4396static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4397{
françois romieu650e8d52011-01-03 15:08:29 +00004398 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004399
4400 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4401
4402 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4403 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004404}
4405
4406static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4407{
4408 rtl_hw_start_8102e_2(ioaddr, pdev);
4409
4410 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4411}
4412
Hayes Wang5a5e4442011-02-22 17:26:21 +08004413static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4414{
4415 static const struct ephy_info e_info_8105e_1[] = {
4416 { 0x07, 0, 0x4000 },
4417 { 0x19, 0, 0x0200 },
4418 { 0x19, 0, 0x0020 },
4419 { 0x1e, 0, 0x2000 },
4420 { 0x03, 0, 0x0001 },
4421 { 0x19, 0, 0x0100 },
4422 { 0x19, 0, 0x0004 },
4423 { 0x0a, 0, 0x0020 }
4424 };
4425
Francois Romieucecb5fd2011-04-01 10:21:07 +02004426 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004427 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4428
Francois Romieucecb5fd2011-04-01 10:21:07 +02004429 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004430 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4431
4432 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004433 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004434
4435 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4436}
4437
4438static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4439{
4440 rtl_hw_start_8105e_1(ioaddr, pdev);
4441 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4442}
4443
Francois Romieu07ce4062007-02-23 23:36:39 +01004444static void rtl_hw_start_8101(struct net_device *dev)
4445{
Francois Romieucdf1a602007-06-11 23:29:50 +02004446 struct rtl8169_private *tp = netdev_priv(dev);
4447 void __iomem *ioaddr = tp->mmio_addr;
4448 struct pci_dev *pdev = tp->pci_dev;
4449
Francois Romieucecb5fd2011-04-01 10:21:07 +02004450 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4451 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004452 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004453
4454 if (cap) {
4455 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4456 PCI_EXP_DEVCTL_NOSNOOP_EN);
4457 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004458 }
4459
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004460 RTL_W8(Cfg9346, Cfg9346_Unlock);
4461
Francois Romieu2857ffb2008-08-02 21:08:49 +02004462 switch (tp->mac_version) {
4463 case RTL_GIGA_MAC_VER_07:
4464 rtl_hw_start_8102e_1(ioaddr, pdev);
4465 break;
4466
4467 case RTL_GIGA_MAC_VER_08:
4468 rtl_hw_start_8102e_3(ioaddr, pdev);
4469 break;
4470
4471 case RTL_GIGA_MAC_VER_09:
4472 rtl_hw_start_8102e_2(ioaddr, pdev);
4473 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004474
4475 case RTL_GIGA_MAC_VER_29:
4476 rtl_hw_start_8105e_1(ioaddr, pdev);
4477 break;
4478 case RTL_GIGA_MAC_VER_30:
4479 rtl_hw_start_8105e_2(ioaddr, pdev);
4480 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004481 }
4482
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004483 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004484
françois romieuf0298f82011-01-03 15:07:42 +00004485 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004486
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004487 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004488
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004489 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004490 RTL_W16(CPlusCmd, tp->cp_cmd);
4491
4492 RTL_W16(IntrMitigate, 0x0000);
4493
4494 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4495
4496 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4497 rtl_set_rx_tx_config_registers(tp);
4498
Francois Romieucdf1a602007-06-11 23:29:50 +02004499 RTL_R8(IntrMask);
4500
Francois Romieucdf1a602007-06-11 23:29:50 +02004501 rtl_set_rx_mode(dev);
4502
4503 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004504
Francois Romieu0e485152007-02-20 00:00:26 +01004505 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506}
4507
4508static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4509{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4511 return -EINVAL;
4512
4513 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004514 netdev_update_features(dev);
4515
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517}
4518
4519static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4520{
Al Viro95e09182007-12-22 18:55:39 +00004521 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4523}
4524
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004525static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4526 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004528 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004529 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004530
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004531 kfree(*data_buff);
4532 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 rtl8169_make_unusable_by_asic(desc);
4534}
4535
4536static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4537{
4538 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4539
4540 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4541}
4542
4543static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4544 u32 rx_buf_sz)
4545{
4546 desc->addr = cpu_to_le64(mapping);
4547 wmb();
4548 rtl8169_mark_to_asic(desc, rx_buf_sz);
4549}
4550
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004551static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004553 return (void *)ALIGN((long)data, 16);
4554}
4555
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004556static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4557 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004558{
4559 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004561 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004562 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004563 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004565 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4566 if (!data)
4567 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004568
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004569 if (rtl8169_align(data) != data) {
4570 kfree(data);
4571 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4572 if (!data)
4573 return NULL;
4574 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004575
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004576 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004577 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004578 if (unlikely(dma_mapping_error(d, mapping))) {
4579 if (net_ratelimit())
4580 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004581 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
4584 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004585 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004586
4587err_out:
4588 kfree(data);
4589 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590}
4591
4592static void rtl8169_rx_clear(struct rtl8169_private *tp)
4593{
Francois Romieu07d3f512007-02-21 22:40:46 +01004594 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
4596 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004597 if (tp->Rx_databuff[i]) {
4598 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 tp->RxDescArray + i);
4600 }
4601 }
4602}
4603
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004604static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004606 desc->opts1 |= cpu_to_le32(RingEnd);
4607}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004608
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004609static int rtl8169_rx_fill(struct rtl8169_private *tp)
4610{
4611 unsigned int i;
4612
4613 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004614 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004615
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004616 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004618
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004619 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004620 if (!data) {
4621 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004622 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004623 }
4624 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004627 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4628 return 0;
4629
4630err_out:
4631 rtl8169_rx_clear(tp);
4632 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633}
4634
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635static int rtl8169_init_ring(struct net_device *dev)
4636{
4637 struct rtl8169_private *tp = netdev_priv(dev);
4638
4639 rtl8169_init_ring_indexes(tp);
4640
4641 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004642 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004644 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
4646
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004647static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 struct TxDesc *desc)
4649{
4650 unsigned int len = tx_skb->len;
4651
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004652 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 desc->opts1 = 0x00;
4655 desc->opts2 = 0x00;
4656 desc->addr = 0x00;
4657 tx_skb->len = 0;
4658}
4659
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004660static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4661 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
4663 unsigned int i;
4664
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004665 for (i = 0; i < n; i++) {
4666 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 struct ring_info *tx_skb = tp->tx_skb + entry;
4668 unsigned int len = tx_skb->len;
4669
4670 if (len) {
4671 struct sk_buff *skb = tx_skb->skb;
4672
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004673 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 tp->TxDescArray + entry);
4675 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004676 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 dev_kfree_skb(skb);
4678 tx_skb->skb = NULL;
4679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 }
4681 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004682}
4683
4684static void rtl8169_tx_clear(struct rtl8169_private *tp)
4685{
4686 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 tp->cur_tx = tp->dirty_tx = 0;
4688}
4689
David Howellsc4028952006-11-22 14:57:56 +00004690static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691{
4692 struct rtl8169_private *tp = netdev_priv(dev);
4693
David Howellsc4028952006-11-22 14:57:56 +00004694 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 schedule_delayed_work(&tp->task, 4);
4696}
4697
4698static void rtl8169_wait_for_quiescence(struct net_device *dev)
4699{
4700 struct rtl8169_private *tp = netdev_priv(dev);
4701 void __iomem *ioaddr = tp->mmio_addr;
4702
4703 synchronize_irq(dev->irq);
4704
4705 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004706 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
4708 rtl8169_irq_mask_and_ack(ioaddr);
4709
David S. Millerd1d08d12008-01-07 20:53:33 -08004710 tp->intr_mask = 0xffff;
4711 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004712 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
David Howellsc4028952006-11-22 14:57:56 +00004715static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
David Howellsc4028952006-11-22 14:57:56 +00004717 struct rtl8169_private *tp =
4718 container_of(work, struct rtl8169_private, task.work);
4719 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 int ret;
4721
Francois Romieueb2a0212007-02-15 23:37:21 +01004722 rtnl_lock();
4723
4724 if (!netif_running(dev))
4725 goto out_unlock;
4726
4727 rtl8169_wait_for_quiescence(dev);
4728 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
4730 ret = rtl8169_open(dev);
4731 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004732 if (net_ratelimit())
4733 netif_err(tp, drv, dev,
4734 "reinit failure (status = %d). Rescheduling\n",
4735 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4737 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004738
4739out_unlock:
4740 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741}
4742
David Howellsc4028952006-11-22 14:57:56 +00004743static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744{
David Howellsc4028952006-11-22 14:57:56 +00004745 struct rtl8169_private *tp =
4746 container_of(work, struct rtl8169_private, task.work);
4747 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004748 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
Francois Romieueb2a0212007-02-15 23:37:21 +01004750 rtnl_lock();
4751
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004753 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
4755 rtl8169_wait_for_quiescence(dev);
4756
Francois Romieu56de4142011-03-15 17:29:31 +01004757 for (i = 0; i < NUM_RX_DESC; i++)
4758 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 rtl8169_tx_clear(tp);
4761
Hayes Wang92fc43b2011-07-06 15:58:03 +08004762 rtl8169_hw_reset(tp);
Francois Romieu56de4142011-03-15 17:29:31 +01004763 rtl_hw_start(dev);
4764 netif_wake_queue(dev);
4765 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004766
4767out_unlock:
4768 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769}
4770
4771static void rtl8169_tx_timeout(struct net_device *dev)
4772{
4773 struct rtl8169_private *tp = netdev_priv(dev);
4774
françois romieue6de30d2011-01-03 15:08:37 +00004775 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
4777 /* Let's wait a bit while any (async) irq lands on */
4778 rtl8169_schedule_work(dev, rtl8169_reset_task);
4779}
4780
4781static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004782 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
4784 struct skb_shared_info *info = skb_shinfo(skb);
4785 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004786 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004787 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
4789 entry = tp->cur_tx;
4790 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4791 skb_frag_t *frag = info->frags + cur_frag;
4792 dma_addr_t mapping;
4793 u32 status, len;
4794 void *addr;
4795
4796 entry = (entry + 1) % NUM_TX_DESC;
4797
4798 txd = tp->TxDescArray + entry;
4799 len = frag->size;
4800 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004801 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004802 if (unlikely(dma_mapping_error(d, mapping))) {
4803 if (net_ratelimit())
4804 netif_err(tp, drv, tp->dev,
4805 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004806 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Francois Romieucecb5fd2011-04-01 10:21:07 +02004809 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004810 status = opts[0] | len |
4811 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
4813 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004814 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 txd->addr = cpu_to_le64(mapping);
4816
4817 tp->tx_skb[entry].len = len;
4818 }
4819
4820 if (cur_frag) {
4821 tp->tx_skb[entry].skb = skb;
4822 txd->opts1 |= cpu_to_le32(LastFrag);
4823 }
4824
4825 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004826
4827err_out:
4828 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4829 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830}
4831
Francois Romieu2b7b4312011-04-18 22:53:24 -07004832static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4833 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004835 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004836 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004837 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Francois Romieu2b7b4312011-04-18 22:53:24 -07004839 if (mss) {
4840 opts[0] |= TD_LSO;
4841 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4842 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004843 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
4845 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004846 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004848 opts[offset] |= info->checksum.udp;
4849 else
4850 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
Stephen Hemminger613573252009-08-31 19:50:58 +00004854static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4855 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856{
4857 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004858 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 struct TxDesc *txd = tp->TxDescArray + entry;
4860 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004861 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 dma_addr_t mapping;
4863 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004864 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004865 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004868 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004869 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 }
4871
4872 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004873 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004875 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004876 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004877 if (unlikely(dma_mapping_error(d, mapping))) {
4878 if (net_ratelimit())
4879 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004880 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
4883 tp->tx_skb[entry].len = len;
4884 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
Francois Romieu2b7b4312011-04-18 22:53:24 -07004886 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4887 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004888
Francois Romieu2b7b4312011-04-18 22:53:24 -07004889 rtl8169_tso_csum(tp, skb, opts);
4890
4891 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004892 if (frags < 0)
4893 goto err_dma_1;
4894 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004895 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004896 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004897 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004898 tp->tx_skb[entry].skb = skb;
4899 }
4900
Francois Romieu2b7b4312011-04-18 22:53:24 -07004901 txd->opts2 = cpu_to_le32(opts[1]);
4902
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 wmb();
4904
Francois Romieucecb5fd2011-04-01 10:21:07 +02004905 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004906 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 txd->opts1 = cpu_to_le32(status);
4908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 tp->cur_tx += frags + 1;
4910
David Dillow4c020a92010-03-03 16:33:10 +00004911 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
Francois Romieucecb5fd2011-04-01 10:21:07 +02004913 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
4915 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4916 netif_stop_queue(dev);
4917 smp_rmb();
4918 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4919 netif_wake_queue(dev);
4920 }
4921
Stephen Hemminger613573252009-08-31 19:50:58 +00004922 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004924err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004925 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004926err_dma_0:
4927 dev_kfree_skb(skb);
4928 dev->stats.tx_dropped++;
4929 return NETDEV_TX_OK;
4930
4931err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004933 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004934 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935}
4936
4937static void rtl8169_pcierr_interrupt(struct net_device *dev)
4938{
4939 struct rtl8169_private *tp = netdev_priv(dev);
4940 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 u16 pci_status, pci_cmd;
4942
4943 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4944 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4945
Joe Perchesbf82c182010-02-09 11:49:50 +00004946 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4947 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949 /*
4950 * The recovery sequence below admits a very elaborated explanation:
4951 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004952 * - I did not see what else could be done;
4953 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 *
4955 * Feel free to adjust to your needs.
4956 */
Francois Romieua27993f2006-12-18 00:04:19 +01004957 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004958 pci_cmd &= ~PCI_COMMAND_PARITY;
4959 else
4960 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4961
4962 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
4964 pci_write_config_word(pdev, PCI_STATUS,
4965 pci_status & (PCI_STATUS_DETECTED_PARITY |
4966 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4967 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4968
4969 /* The infamous DAC f*ckup only happens at boot time */
4970 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004971 void __iomem *ioaddr = tp->mmio_addr;
4972
Joe Perchesbf82c182010-02-09 11:49:50 +00004973 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 tp->cp_cmd &= ~PCIDAC;
4975 RTL_W16(CPlusCmd, tp->cp_cmd);
4976 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 }
4978
françois romieue6de30d2011-01-03 15:08:37 +00004979 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004980
4981 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982}
4983
Francois Romieu07d3f512007-02-21 22:40:46 +01004984static void rtl8169_tx_interrupt(struct net_device *dev,
4985 struct rtl8169_private *tp,
4986 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987{
4988 unsigned int dirty_tx, tx_left;
4989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 dirty_tx = tp->dirty_tx;
4991 smp_rmb();
4992 tx_left = tp->cur_tx - dirty_tx;
4993
4994 while (tx_left > 0) {
4995 unsigned int entry = dirty_tx % NUM_TX_DESC;
4996 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 u32 status;
4998
4999 rmb();
5000 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
5001 if (status & DescOwn)
5002 break;
5003
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005004 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
5005 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00005007 dev->stats.tx_packets++;
5008 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00005009 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 tx_skb->skb = NULL;
5011 }
5012 dirty_tx++;
5013 tx_left--;
5014 }
5015
5016 if (tp->dirty_tx != dirty_tx) {
5017 tp->dirty_tx = dirty_tx;
5018 smp_wmb();
5019 if (netif_queue_stopped(dev) &&
5020 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
5021 netif_wake_queue(dev);
5022 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02005023 /*
5024 * 8168 hack: TxPoll requests are lost when the Tx packets are
5025 * too close. Let's kick an extra TxPoll request when a burst
5026 * of start_xmit activity is detected (if it is not detected,
5027 * it is slow enough). -- FR
5028 */
5029 smp_rmb();
5030 if (tp->cur_tx != dirty_tx)
5031 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 }
5033}
5034
Francois Romieu126fa4b2005-05-12 20:09:17 -04005035static inline int rtl8169_fragmented_frame(u32 status)
5036{
5037 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
5038}
5039
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005040static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 u32 status = opts1 & RxProtoMask;
5043
5044 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00005045 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 skb->ip_summed = CHECKSUM_UNNECESSARY;
5047 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005048 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
5050
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005051static struct sk_buff *rtl8169_try_rx_copy(void *data,
5052 struct rtl8169_private *tp,
5053 int pkt_size,
5054 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005056 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005057 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005059 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005060 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005061 prefetch(data);
5062 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5063 if (skb)
5064 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005065 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5066
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005067 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068}
5069
Francois Romieu07d3f512007-02-21 22:40:46 +01005070static int rtl8169_rx_interrupt(struct net_device *dev,
5071 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005072 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073{
5074 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005075 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 cur_rx = tp->cur_rx;
5078 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005079 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005081 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005083 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 u32 status;
5085
5086 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04005087 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
5089 if (status & DescOwn)
5090 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005091 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005092 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5093 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005094 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005096 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005098 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005099 if (status & RxFOVF) {
5100 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005101 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005102 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005103 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005105 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005106 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
Francois Romieu126fa4b2005-05-12 20:09:17 -04005109 /*
5110 * The driver does not support incoming fragmented
5111 * frames. They are seen as a symptom of over-mtu
5112 * sized frames.
5113 */
5114 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005115 dev->stats.rx_dropped++;
5116 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005117 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005118 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005119 }
5120
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005121 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5122 tp, pkt_size, addr);
5123 rtl8169_mark_to_asic(desc, rx_buf_sz);
5124 if (!skb) {
5125 dev->stats.rx_dropped++;
5126 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 }
5128
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005129 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 skb_put(skb, pkt_size);
5131 skb->protocol = eth_type_trans(skb, dev);
5132
Francois Romieu7a8fc772011-03-01 17:18:33 +01005133 rtl8169_rx_vlan_tag(desc, skb);
5134
Francois Romieu56de4142011-03-15 17:29:31 +01005135 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
Francois Romieucebf8cc2007-10-18 12:06:54 +02005137 dev->stats.rx_bytes += pkt_size;
5138 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005140
5141 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005142 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005143 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5144 desc->opts2 = 0;
5145 cur_rx++;
5146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 }
5148
5149 count = cur_rx - tp->cur_rx;
5150 tp->cur_rx = cur_rx;
5151
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005152 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
5154 return count;
5155}
5156
Francois Romieu07d3f512007-02-21 22:40:46 +01005157static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158{
Francois Romieu07d3f512007-02-21 22:40:46 +01005159 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005163 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
David Dillowf11a3772009-05-22 15:29:34 +00005165 /* loop handling interrupts until we have no new ones or
5166 * we hit a invalid/hotplug case.
5167 */
Francois Romieu865c6522008-05-11 14:51:00 +02005168 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005169 while (status && status != 0xffff) {
5170 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
David Dillowf11a3772009-05-22 15:29:34 +00005172 /* Handle all of the error cases first. These will reset
5173 * the chip, so just exit the loop.
5174 */
5175 if (unlikely(!netif_running(dev))) {
Hayes Wang92fc43b2011-07-06 15:58:03 +08005176 rtl8169_hw_reset(tp);
David Dillowf11a3772009-05-22 15:29:34 +00005177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 }
David Dillowf11a3772009-05-22 15:29:34 +00005179
Francois Romieu1519e572011-02-03 12:02:36 +01005180 if (unlikely(status & RxFIFOOver)) {
5181 switch (tp->mac_version) {
5182 /* Work around for rx fifo overflow */
5183 case RTL_GIGA_MAC_VER_11:
5184 case RTL_GIGA_MAC_VER_22:
5185 case RTL_GIGA_MAC_VER_26:
5186 netif_stop_queue(dev);
5187 rtl8169_tx_timeout(dev);
5188 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005189 /* Testers needed. */
5190 case RTL_GIGA_MAC_VER_17:
5191 case RTL_GIGA_MAC_VER_19:
5192 case RTL_GIGA_MAC_VER_20:
5193 case RTL_GIGA_MAC_VER_21:
5194 case RTL_GIGA_MAC_VER_23:
5195 case RTL_GIGA_MAC_VER_24:
5196 case RTL_GIGA_MAC_VER_27:
5197 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005198 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005199 /* Experimental science. Pktgen proof. */
5200 case RTL_GIGA_MAC_VER_12:
5201 case RTL_GIGA_MAC_VER_25:
5202 if (status == RxFIFOOver)
5203 goto done;
5204 break;
5205 default:
5206 break;
5207 }
David Dillowf11a3772009-05-22 15:29:34 +00005208 }
5209
5210 if (unlikely(status & SYSErr)) {
5211 rtl8169_pcierr_interrupt(dev);
5212 break;
5213 }
5214
5215 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005216 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005217
5218 /* We need to see the lastest version of tp->intr_mask to
5219 * avoid ignoring an MSI interrupt and having to wait for
5220 * another event which may never come.
5221 */
5222 smp_rmb();
5223 if (status & tp->intr_mask & tp->napi_event) {
5224 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5225 tp->intr_mask = ~tp->napi_event;
5226
5227 if (likely(napi_schedule_prep(&tp->napi)))
5228 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005229 else
5230 netif_info(tp, intr, dev,
5231 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005232 }
5233
5234 /* We only get a new MSI interrupt when all active irq
5235 * sources on the chip have been acknowledged. So, ack
5236 * everything we've seen and check if new sources have become
5237 * active to avoid blocking all interrupts from the chip.
5238 */
5239 RTL_W16(IntrStatus,
5240 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5241 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 }
Francois Romieu1519e572011-02-03 12:02:36 +01005243done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 return IRQ_RETVAL(handled);
5245}
5246
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005247static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005249 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5250 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005252 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005254 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 rtl8169_tx_interrupt(dev, tp, ioaddr);
5256
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005257 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005258 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005259
5260 /* We need for force the visibility of tp->intr_mask
5261 * for other CPUs, as we can loose an MSI interrupt
5262 * and potentially wait for a retransmit timeout if we don't.
5263 * The posted write to IntrMask is safe, as it will
5264 * eventually make it to the chip and we won't loose anything
5265 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 */
David Dillowf11a3772009-05-22 15:29:34 +00005267 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005268 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005269 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 }
5271
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005272 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
Francois Romieu523a6092008-09-10 22:28:56 +02005275static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5276{
5277 struct rtl8169_private *tp = netdev_priv(dev);
5278
5279 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5280 return;
5281
5282 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5283 RTL_W32(RxMissed, 0);
5284}
5285
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286static void rtl8169_down(struct net_device *dev)
5287{
5288 struct rtl8169_private *tp = netdev_priv(dev);
5289 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Francois Romieu4876cc12011-03-11 21:07:11 +01005291 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
5293 netif_stop_queue(dev);
5294
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005295 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005296
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 spin_lock_irq(&tp->lock);
5298
Hayes Wang92fc43b2011-07-06 15:58:03 +08005299 rtl8169_hw_reset(tp);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005300 /*
5301 * At this point device interrupts can not be enabled in any function,
5302 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5303 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5304 */
Francois Romieu523a6092008-09-10 22:28:56 +02005305 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
5307 spin_unlock_irq(&tp->lock);
5308
5309 synchronize_irq(dev->irq);
5310
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005312 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 rtl8169_tx_clear(tp);
5315
5316 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005317
5318 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319}
5320
5321static int rtl8169_close(struct net_device *dev)
5322{
5323 struct rtl8169_private *tp = netdev_priv(dev);
5324 struct pci_dev *pdev = tp->pci_dev;
5325
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005326 pm_runtime_get_sync(&pdev->dev);
5327
Francois Romieucecb5fd2011-04-01 10:21:07 +02005328 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005329 rtl8169_update_counters(dev);
5330
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 rtl8169_down(dev);
5332
5333 free_irq(dev->irq, dev);
5334
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005335 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5336 tp->RxPhyAddr);
5337 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5338 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 tp->TxDescArray = NULL;
5340 tp->RxDescArray = NULL;
5341
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005342 pm_runtime_put_sync(&pdev->dev);
5343
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 return 0;
5345}
5346
Francois Romieu07ce4062007-02-23 23:36:39 +01005347static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348{
5349 struct rtl8169_private *tp = netdev_priv(dev);
5350 void __iomem *ioaddr = tp->mmio_addr;
5351 unsigned long flags;
5352 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005353 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 u32 tmp = 0;
5355
5356 if (dev->flags & IFF_PROMISC) {
5357 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005358 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 rx_mode =
5360 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5361 AcceptAllPhys;
5362 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005363 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005364 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 /* Too many to filter perfectly -- accept all multicasts. */
5366 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5367 mc_filter[1] = mc_filter[0] = 0xffffffff;
5368 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005369 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005370
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 rx_mode = AcceptBroadcast | AcceptMyPhys;
5372 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005373 netdev_for_each_mc_addr(ha, dev) {
5374 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5376 rx_mode |= AcceptMulticast;
5377 }
5378 }
5379
5380 spin_lock_irqsave(&tp->lock, flags);
5381
5382 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005383 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384
Francois Romieuf887cce2008-07-17 22:24:18 +02005385 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005386 u32 data = mc_filter[0];
5387
5388 mc_filter[0] = swab32(mc_filter[1]);
5389 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005390 }
5391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005393 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394
Francois Romieu57a9f232007-06-04 22:10:15 +02005395 RTL_W32(RxConfig, tmp);
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 spin_unlock_irqrestore(&tp->lock, flags);
5398}
5399
5400/**
5401 * rtl8169_get_stats - Get rtl8169 read/write statistics
5402 * @dev: The Ethernet Device to get statistics for
5403 *
5404 * Get TX/RX statistics for rtl8169
5405 */
5406static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5407{
5408 struct rtl8169_private *tp = netdev_priv(dev);
5409 void __iomem *ioaddr = tp->mmio_addr;
5410 unsigned long flags;
5411
5412 if (netif_running(dev)) {
5413 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005414 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 spin_unlock_irqrestore(&tp->lock, flags);
5416 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005417
Francois Romieucebf8cc2007-10-18 12:06:54 +02005418 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419}
5420
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005421static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005422{
françois romieu065c27c2011-01-03 15:08:12 +00005423 struct rtl8169_private *tp = netdev_priv(dev);
5424
Francois Romieu5d06a992006-02-23 00:47:58 +01005425 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005426 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005427
françois romieu065c27c2011-01-03 15:08:12 +00005428 rtl_pll_power_down(tp);
5429
Francois Romieu5d06a992006-02-23 00:47:58 +01005430 netif_device_detach(dev);
5431 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005432}
Francois Romieu5d06a992006-02-23 00:47:58 +01005433
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005434#ifdef CONFIG_PM
5435
5436static int rtl8169_suspend(struct device *device)
5437{
5438 struct pci_dev *pdev = to_pci_dev(device);
5439 struct net_device *dev = pci_get_drvdata(pdev);
5440
5441 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005442
Francois Romieu5d06a992006-02-23 00:47:58 +01005443 return 0;
5444}
5445
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005446static void __rtl8169_resume(struct net_device *dev)
5447{
françois romieu065c27c2011-01-03 15:08:12 +00005448 struct rtl8169_private *tp = netdev_priv(dev);
5449
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005450 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005451
5452 rtl_pll_power_up(tp);
5453
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005454 rtl8169_schedule_work(dev, rtl8169_reset_task);
5455}
5456
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005457static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005458{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005459 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005460 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005461 struct rtl8169_private *tp = netdev_priv(dev);
5462
5463 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005464
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005465 if (netif_running(dev))
5466 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005467
Francois Romieu5d06a992006-02-23 00:47:58 +01005468 return 0;
5469}
5470
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005471static int rtl8169_runtime_suspend(struct device *device)
5472{
5473 struct pci_dev *pdev = to_pci_dev(device);
5474 struct net_device *dev = pci_get_drvdata(pdev);
5475 struct rtl8169_private *tp = netdev_priv(dev);
5476
5477 if (!tp->TxDescArray)
5478 return 0;
5479
5480 spin_lock_irq(&tp->lock);
5481 tp->saved_wolopts = __rtl8169_get_wol(tp);
5482 __rtl8169_set_wol(tp, WAKE_ANY);
5483 spin_unlock_irq(&tp->lock);
5484
5485 rtl8169_net_suspend(dev);
5486
5487 return 0;
5488}
5489
5490static int rtl8169_runtime_resume(struct device *device)
5491{
5492 struct pci_dev *pdev = to_pci_dev(device);
5493 struct net_device *dev = pci_get_drvdata(pdev);
5494 struct rtl8169_private *tp = netdev_priv(dev);
5495
5496 if (!tp->TxDescArray)
5497 return 0;
5498
5499 spin_lock_irq(&tp->lock);
5500 __rtl8169_set_wol(tp, tp->saved_wolopts);
5501 tp->saved_wolopts = 0;
5502 spin_unlock_irq(&tp->lock);
5503
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005504 rtl8169_init_phy(dev, tp);
5505
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005506 __rtl8169_resume(dev);
5507
5508 return 0;
5509}
5510
5511static int rtl8169_runtime_idle(struct device *device)
5512{
5513 struct pci_dev *pdev = to_pci_dev(device);
5514 struct net_device *dev = pci_get_drvdata(pdev);
5515 struct rtl8169_private *tp = netdev_priv(dev);
5516
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005517 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005518}
5519
Alexey Dobriyan47145212009-12-14 18:00:08 -08005520static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005521 .suspend = rtl8169_suspend,
5522 .resume = rtl8169_resume,
5523 .freeze = rtl8169_suspend,
5524 .thaw = rtl8169_resume,
5525 .poweroff = rtl8169_suspend,
5526 .restore = rtl8169_resume,
5527 .runtime_suspend = rtl8169_runtime_suspend,
5528 .runtime_resume = rtl8169_runtime_resume,
5529 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005530};
5531
5532#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5533
5534#else /* !CONFIG_PM */
5535
5536#define RTL8169_PM_OPS NULL
5537
5538#endif /* !CONFIG_PM */
5539
Francois Romieu1765f952008-09-13 17:21:40 +02005540static void rtl_shutdown(struct pci_dev *pdev)
5541{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005542 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005543 struct rtl8169_private *tp = netdev_priv(dev);
5544 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005545
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005546 rtl8169_net_suspend(dev);
5547
Francois Romieucecb5fd2011-04-01 10:21:07 +02005548 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005549 rtl_rar_set(tp, dev->perm_addr);
5550
françois romieu4bb3f522009-06-17 11:41:45 +00005551 spin_lock_irq(&tp->lock);
5552
Hayes Wang92fc43b2011-07-06 15:58:03 +08005553 rtl8169_hw_reset(tp);
françois romieu4bb3f522009-06-17 11:41:45 +00005554
5555 spin_unlock_irq(&tp->lock);
5556
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005557 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005558 /* WoL fails with some 8168 when the receiver is disabled. */
5559 if (tp->features & RTL_FEATURE_WOL) {
5560 pci_clear_master(pdev);
5561
5562 RTL_W8(ChipCmd, CmdRxEnb);
5563 /* PCI commit */
5564 RTL_R8(ChipCmd);
5565 }
5566
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005567 pci_wake_from_d3(pdev, true);
5568 pci_set_power_state(pdev, PCI_D3hot);
5569 }
5570}
Francois Romieu5d06a992006-02-23 00:47:58 +01005571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572static struct pci_driver rtl8169_pci_driver = {
5573 .name = MODULENAME,
5574 .id_table = rtl8169_pci_tbl,
5575 .probe = rtl8169_init_one,
5576 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005577 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005578 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579};
5580
Francois Romieu07d3f512007-02-21 22:40:46 +01005581static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582{
Jeff Garzik29917622006-08-19 17:48:59 -04005583 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584}
5585
Francois Romieu07d3f512007-02-21 22:40:46 +01005586static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
5588 pci_unregister_driver(&rtl8169_pci_driver);
5589}
5590
5591module_init(rtl8169_init_module);
5592module_exit(rtl8169_cleanup_module);