blob: 52ad6cea4e7ad512befd88d3a7d8572ef295c004 [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
françois romieudaf9df62009-10-07 12:44:20 +00001049static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1050{
1051 u8 value = 0xff;
1052 unsigned int i;
1053
1054 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1055
1056 for (i = 0; i < 300; i++) {
1057 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1058 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1059 break;
1060 }
1061 udelay(100);
1062 }
1063
1064 return value;
1065}
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1068{
1069 RTL_W16(IntrMask, 0x0000);
1070
1071 RTL_W16(IntrStatus, 0xffff);
1072}
1073
1074static void rtl8169_asic_down(void __iomem *ioaddr)
1075{
1076 RTL_W8(ChipCmd, 0x00);
1077 rtl8169_irq_mask_and_ack(ioaddr);
1078 RTL_R16(CPlusCmd);
1079}
1080
françois romieu4da19632011-01-03 15:07:55 +00001081static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
françois romieu4da19632011-01-03 15:07:55 +00001083 void __iomem *ioaddr = tp->mmio_addr;
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return RTL_R32(TBICSR) & TBIReset;
1086}
1087
françois romieu4da19632011-01-03 15:07:55 +00001088static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
françois romieu4da19632011-01-03 15:07:55 +00001090 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1094{
1095 return RTL_R32(TBICSR) & TBILinkOk;
1096}
1097
1098static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1099{
1100 return RTL_R8(PHYstatus) & LinkStatus;
1101}
1102
françois romieu4da19632011-01-03 15:07:55 +00001103static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
françois romieu4da19632011-01-03 15:07:55 +00001105 void __iomem *ioaddr = tp->mmio_addr;
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1108}
1109
françois romieu4da19632011-01-03 15:07:55 +00001110static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 unsigned int val;
1113
françois romieu4da19632011-01-03 15:07:55 +00001114 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1115 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001118static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001119 struct rtl8169_private *tp,
1120 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 unsigned long flags;
1123
1124 spin_lock_irqsave(&tp->lock, flags);
1125 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001126 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001127 if (pm)
1128 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001130 if (net_ratelimit())
1131 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001132 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001134 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001135 if (pm)
1136 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 spin_unlock_irqrestore(&tp->lock, flags);
1139}
1140
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001141static void rtl8169_check_link_status(struct net_device *dev,
1142 struct rtl8169_private *tp,
1143 void __iomem *ioaddr)
1144{
1145 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1146}
1147
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001148#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1149
1150static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1151{
1152 void __iomem *ioaddr = tp->mmio_addr;
1153 u8 options;
1154 u32 wolopts = 0;
1155
1156 options = RTL_R8(Config1);
1157 if (!(options & PMEnable))
1158 return 0;
1159
1160 options = RTL_R8(Config3);
1161 if (options & LinkUp)
1162 wolopts |= WAKE_PHY;
1163 if (options & MagicPacket)
1164 wolopts |= WAKE_MAGIC;
1165
1166 options = RTL_R8(Config5);
1167 if (options & UWF)
1168 wolopts |= WAKE_UCAST;
1169 if (options & BWF)
1170 wolopts |= WAKE_BCAST;
1171 if (options & MWF)
1172 wolopts |= WAKE_MCAST;
1173
1174 return wolopts;
1175}
1176
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001177static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1178{
1179 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001180
1181 spin_lock_irq(&tp->lock);
1182
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001183 wol->supported = WAKE_ANY;
1184 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001185
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001186 spin_unlock_irq(&tp->lock);
1187}
1188
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001189static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001190{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001191 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001192 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001193 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001194 u32 opt;
1195 u16 reg;
1196 u8 mask;
1197 } cfg[] = {
1198 { WAKE_ANY, Config1, PMEnable },
1199 { WAKE_PHY, Config3, LinkUp },
1200 { WAKE_MAGIC, Config3, MagicPacket },
1201 { WAKE_UCAST, Config5, UWF },
1202 { WAKE_BCAST, Config5, BWF },
1203 { WAKE_MCAST, Config5, MWF },
1204 { WAKE_ANY, Config5, LanWake }
1205 };
1206
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001207 RTL_W8(Cfg9346, Cfg9346_Unlock);
1208
1209 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1210 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001211 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001212 options |= cfg[i].mask;
1213 RTL_W8(cfg[i].reg, options);
1214 }
1215
1216 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001217}
1218
1219static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1220{
1221 struct rtl8169_private *tp = netdev_priv(dev);
1222
1223 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001224
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001225 if (wol->wolopts)
1226 tp->features |= RTL_FEATURE_WOL;
1227 else
1228 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001229 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001230 spin_unlock_irq(&tp->lock);
1231
françois romieuea809072010-11-08 13:23:58 +00001232 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1233
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001234 return 0;
1235}
1236
Francois Romieu31bd2042011-04-26 18:58:59 +02001237static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1238{
Francois Romieu85bffe62011-04-27 08:22:39 +02001239 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001240}
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242static void rtl8169_get_drvinfo(struct net_device *dev,
1243 struct ethtool_drvinfo *info)
1244{
1245 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001246 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 strcpy(info->driver, MODULENAME);
1249 strcpy(info->version, RTL8169_VERSION);
1250 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001251 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1252 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1253 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256static int rtl8169_get_regs_len(struct net_device *dev)
1257{
1258 return R8169_REGS_SIZE;
1259}
1260
1261static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001262 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 struct rtl8169_private *tp = netdev_priv(dev);
1265 void __iomem *ioaddr = tp->mmio_addr;
1266 int ret = 0;
1267 u32 reg;
1268
1269 reg = RTL_R32(TBICSR);
1270 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1271 (duplex == DUPLEX_FULL)) {
1272 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1273 } else if (autoneg == AUTONEG_ENABLE)
1274 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1275 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001276 netif_warn(tp, link, dev,
1277 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 ret = -EOPNOTSUPP;
1279 }
1280
1281 return ret;
1282}
1283
1284static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001285 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001288 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001289 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Hayes Wang716b50a2011-02-22 17:26:18 +08001291 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001294 int auto_nego;
1295
françois romieu4da19632011-01-03 15:07:55 +00001296 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001297 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1298 ADVERTISE_100HALF | ADVERTISE_100FULL);
1299
1300 if (adv & ADVERTISED_10baseT_Half)
1301 auto_nego |= ADVERTISE_10HALF;
1302 if (adv & ADVERTISED_10baseT_Full)
1303 auto_nego |= ADVERTISE_10FULL;
1304 if (adv & ADVERTISED_100baseT_Half)
1305 auto_nego |= ADVERTISE_100HALF;
1306 if (adv & ADVERTISED_100baseT_Full)
1307 auto_nego |= ADVERTISE_100FULL;
1308
françois romieu3577aa12009-05-19 10:46:48 +00001309 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1310
françois romieu4da19632011-01-03 15:07:55 +00001311 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001312 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1313
1314 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001315 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001316 if (adv & ADVERTISED_1000baseT_Half)
1317 giga_ctrl |= ADVERTISE_1000HALF;
1318 if (adv & ADVERTISED_1000baseT_Full)
1319 giga_ctrl |= ADVERTISE_1000FULL;
1320 } else if (adv & (ADVERTISED_1000baseT_Half |
1321 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001322 netif_info(tp, link, dev,
1323 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001324 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
françois romieu3577aa12009-05-19 10:46:48 +00001327 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001328
françois romieu4da19632011-01-03 15:07:55 +00001329 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1330 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001331 } else {
1332 giga_ctrl = 0;
1333
1334 if (speed == SPEED_10)
1335 bmcr = 0;
1336 else if (speed == SPEED_100)
1337 bmcr = BMCR_SPEED100;
1338 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001339 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001340
1341 if (duplex == DUPLEX_FULL)
1342 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001343 }
1344
françois romieu4da19632011-01-03 15:07:55 +00001345 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001346
Francois Romieucecb5fd2011-04-01 10:21:07 +02001347 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1348 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001349 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001350 rtl_writephy(tp, 0x17, 0x2138);
1351 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001352 } else {
françois romieu4da19632011-01-03 15:07:55 +00001353 rtl_writephy(tp, 0x17, 0x2108);
1354 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001355 }
1356 }
1357
Oliver Neukum54405cd2011-01-06 21:55:13 +01001358 rc = 0;
1359out:
1360 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001364 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 struct rtl8169_private *tp = netdev_priv(dev);
1367 int ret;
1368
Oliver Neukum54405cd2011-01-06 21:55:13 +01001369 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001370 if (ret < 0)
1371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Francois Romieu4876cc12011-03-11 21:07:11 +01001373 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1374 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001376 }
1377out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return ret;
1379}
1380
1381static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1382{
1383 struct rtl8169_private *tp = netdev_priv(dev);
1384 unsigned long flags;
1385 int ret;
1386
Francois Romieu4876cc12011-03-11 21:07:11 +01001387 del_timer_sync(&tp->timer);
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001390 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001391 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return ret;
1395}
1396
Michał Mirosław350fb322011-04-08 06:35:56 +00001397static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001399 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001400 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Michał Mirosław350fb322011-04-08 06:35:56 +00001402 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Michał Mirosław350fb322011-04-08 06:35:56 +00001405static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 struct rtl8169_private *tp = netdev_priv(dev);
1408 void __iomem *ioaddr = tp->mmio_addr;
1409 unsigned long flags;
1410
1411 spin_lock_irqsave(&tp->lock, flags);
1412
Michał Mirosław350fb322011-04-08 06:35:56 +00001413 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 tp->cp_cmd |= RxChkSum;
1415 else
1416 tp->cp_cmd &= ~RxChkSum;
1417
Michał Mirosław350fb322011-04-08 06:35:56 +00001418 if (dev->features & NETIF_F_HW_VLAN_RX)
1419 tp->cp_cmd |= RxVlan;
1420 else
1421 tp->cp_cmd &= ~RxVlan;
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 RTL_W16(CPlusCmd, tp->cp_cmd);
1424 RTL_R16(CPlusCmd);
1425
1426 spin_unlock_irqrestore(&tp->lock, flags);
1427
1428 return 0;
1429}
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1432 struct sk_buff *skb)
1433{
Jesse Grosseab6d182010-10-20 13:56:03 +00001434 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1436}
1437
Francois Romieu7a8fc772011-03-01 17:18:33 +01001438static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Francois Romieu7a8fc772011-03-01 17:18:33 +01001442 if (opts2 & RxVlanTag)
1443 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Francois Romieuccdffb92008-07-26 14:26:06 +02001448static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 struct rtl8169_private *tp = netdev_priv(dev);
1451 void __iomem *ioaddr = tp->mmio_addr;
1452 u32 status;
1453
1454 cmd->supported =
1455 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1456 cmd->port = PORT_FIBRE;
1457 cmd->transceiver = XCVR_INTERNAL;
1458
1459 status = RTL_R32(TBICSR);
1460 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1461 cmd->autoneg = !!(status & TBINwEnable);
1462
David Decotigny70739492011-04-27 18:32:40 +00001463 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001465
1466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Francois Romieuccdffb92008-07-26 14:26:06 +02001469static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Francois Romieuccdffb92008-07-26 14:26:06 +02001473 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
1476static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1477{
1478 struct rtl8169_private *tp = netdev_priv(dev);
1479 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001480 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 spin_lock_irqsave(&tp->lock, flags);
1483
Francois Romieuccdffb92008-07-26 14:26:06 +02001484 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001487 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
1490static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1491 void *p)
1492{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001493 struct rtl8169_private *tp = netdev_priv(dev);
1494 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Francois Romieu5b0384f2006-08-16 16:00:01 +02001496 if (regs->len > R8169_REGS_SIZE)
1497 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Francois Romieu5b0384f2006-08-16 16:00:01 +02001499 spin_lock_irqsave(&tp->lock, flags);
1500 memcpy_fromio(p, tp->mmio_addr, regs->len);
1501 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001504static u32 rtl8169_get_msglevel(struct net_device *dev)
1505{
1506 struct rtl8169_private *tp = netdev_priv(dev);
1507
1508 return tp->msg_enable;
1509}
1510
1511static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1512{
1513 struct rtl8169_private *tp = netdev_priv(dev);
1514
1515 tp->msg_enable = value;
1516}
1517
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001518static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1519 "tx_packets",
1520 "rx_packets",
1521 "tx_errors",
1522 "rx_errors",
1523 "rx_missed",
1524 "align_errors",
1525 "tx_single_collisions",
1526 "tx_multi_collisions",
1527 "unicast",
1528 "broadcast",
1529 "multicast",
1530 "tx_aborted",
1531 "tx_underrun",
1532};
1533
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001534static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001535{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001536 switch (sset) {
1537 case ETH_SS_STATS:
1538 return ARRAY_SIZE(rtl8169_gstrings);
1539 default:
1540 return -EOPNOTSUPP;
1541 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001542}
1543
Ivan Vecera355423d2009-02-06 21:49:57 -08001544static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001545{
1546 struct rtl8169_private *tp = netdev_priv(dev);
1547 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001548 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001549 struct rtl8169_counters *counters;
1550 dma_addr_t paddr;
1551 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001552 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001553
Ivan Vecera355423d2009-02-06 21:49:57 -08001554 /*
1555 * Some chips are unable to dump tally counters when the receiver
1556 * is disabled.
1557 */
1558 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1559 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001560
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001561 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001562 if (!counters)
1563 return;
1564
1565 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001566 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001567 RTL_W32(CounterAddrLow, cmd);
1568 RTL_W32(CounterAddrLow, cmd | CounterDump);
1569
Ivan Vecera355423d2009-02-06 21:49:57 -08001570 while (wait--) {
1571 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001572 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001573 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001574 }
1575 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001576 }
1577
1578 RTL_W32(CounterAddrLow, 0);
1579 RTL_W32(CounterAddrHigh, 0);
1580
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001581 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001582}
1583
Ivan Vecera355423d2009-02-06 21:49:57 -08001584static void rtl8169_get_ethtool_stats(struct net_device *dev,
1585 struct ethtool_stats *stats, u64 *data)
1586{
1587 struct rtl8169_private *tp = netdev_priv(dev);
1588
1589 ASSERT_RTNL();
1590
1591 rtl8169_update_counters(dev);
1592
1593 data[0] = le64_to_cpu(tp->counters.tx_packets);
1594 data[1] = le64_to_cpu(tp->counters.rx_packets);
1595 data[2] = le64_to_cpu(tp->counters.tx_errors);
1596 data[3] = le32_to_cpu(tp->counters.rx_errors);
1597 data[4] = le16_to_cpu(tp->counters.rx_missed);
1598 data[5] = le16_to_cpu(tp->counters.align_errors);
1599 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1600 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1601 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1602 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1603 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1604 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1605 data[12] = le16_to_cpu(tp->counters.tx_underun);
1606}
1607
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001608static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1609{
1610 switch(stringset) {
1611 case ETH_SS_STATS:
1612 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1613 break;
1614 }
1615}
1616
Jeff Garzik7282d492006-09-13 14:30:00 -04001617static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 .get_drvinfo = rtl8169_get_drvinfo,
1619 .get_regs_len = rtl8169_get_regs_len,
1620 .get_link = ethtool_op_get_link,
1621 .get_settings = rtl8169_get_settings,
1622 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001623 .get_msglevel = rtl8169_get_msglevel,
1624 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001626 .get_wol = rtl8169_get_wol,
1627 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001628 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001629 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001630 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631};
1632
Francois Romieu07d3f512007-02-21 22:40:46 +01001633static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001634 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Francois Romieu5d320a22011-05-08 17:47:36 +02001636 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001637 /*
1638 * The driver currently handles the 8168Bf and the 8168Be identically
1639 * but they can be identified more specifically through the test below
1640 * if needed:
1641 *
1642 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001643 *
1644 * Same thing for the 8101Eb and the 8101Ec:
1645 *
1646 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001647 */
Francois Romieu37441002011-06-17 22:58:54 +02001648 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001650 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int mac_version;
1652 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001653 /* 8168E family. */
1654 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1655 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1656 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1657
Francois Romieu5b538df2008-07-20 16:22:45 +02001658 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001659 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1660 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001661 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001662
françois romieue6de30d2011-01-03 15:08:37 +00001663 /* 8168DP family. */
1664 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1665 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001666 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001667
Francois Romieuef808d52008-06-29 13:10:54 +02001668 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001669 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001670 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001671 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001672 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001673 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1674 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001675 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001676 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001677 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001678
1679 /* 8168B family. */
1680 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1681 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1682 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1683 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1684
1685 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001686 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001687 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1688 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1689 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001690 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1691 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1692 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1693 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1694 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1695 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001696 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001697 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001698 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001699 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1700 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001701 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1702 /* FIXME: where did these entries come from ? -- FR */
1703 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1704 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1705
1706 /* 8110 family. */
1707 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1708 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1709 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1710 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1711 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1712 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1713
Jean Delvaref21b75e2009-05-26 20:54:48 -07001714 /* Catch-all */
1715 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001716 };
1717 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 u32 reg;
1719
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001720 reg = RTL_R32(TxConfig);
1721 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 p++;
1723 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001724
1725 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1726 netif_notice(tp, probe, dev,
1727 "unknown MAC, using family default\n");
1728 tp->mac_version = default_version;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
1732static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1733{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001734 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
Francois Romieu867763c2007-08-17 18:21:58 +02001737struct phy_reg {
1738 u16 reg;
1739 u16 val;
1740};
1741
françois romieu4da19632011-01-03 15:07:55 +00001742static void rtl_writephy_batch(struct rtl8169_private *tp,
1743 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001744{
1745 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001746 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001747 regs++;
1748 }
1749}
1750
françois romieubca03d52011-01-03 15:07:31 +00001751#define PHY_READ 0x00000000
1752#define PHY_DATA_OR 0x10000000
1753#define PHY_DATA_AND 0x20000000
1754#define PHY_BJMPN 0x30000000
1755#define PHY_READ_EFUSE 0x40000000
1756#define PHY_READ_MAC_BYTE 0x50000000
1757#define PHY_WRITE_MAC_BYTE 0x60000000
1758#define PHY_CLEAR_READCOUNT 0x70000000
1759#define PHY_WRITE 0x80000000
1760#define PHY_READCOUNT_EQ_SKIP 0x90000000
1761#define PHY_COMP_EQ_SKIPN 0xa0000000
1762#define PHY_COMP_NEQ_SKIPN 0xb0000000
1763#define PHY_WRITE_PREVIOUS 0xc0000000
1764#define PHY_SKIPN 0xd0000000
1765#define PHY_DELAY_MS 0xe0000000
1766#define PHY_WRITE_ERI_WORD 0xf0000000
1767
Hayes Wang960aee62011-06-18 11:37:48 +02001768struct fw_info {
1769 u32 magic;
1770 char version[RTL_VER_SIZE];
1771 __le32 fw_start;
1772 __le32 fw_len;
1773 u8 chksum;
1774} __packed;
1775
Francois Romieu1c361ef2011-06-17 17:16:24 +02001776#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1777
1778static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001779{
Francois Romieub6ffd972011-06-17 17:00:05 +02001780 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001781 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001782 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1783 char *version = rtl_fw->version;
1784 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001785
Francois Romieu1c361ef2011-06-17 17:16:24 +02001786 if (fw->size < FW_OPCODE_SIZE)
1787 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001788
1789 if (!fw_info->magic) {
1790 size_t i, size, start;
1791 u8 checksum = 0;
1792
1793 if (fw->size < sizeof(*fw_info))
1794 goto out;
1795
1796 for (i = 0; i < fw->size; i++)
1797 checksum += fw->data[i];
1798 if (checksum != 0)
1799 goto out;
1800
1801 start = le32_to_cpu(fw_info->fw_start);
1802 if (start > fw->size)
1803 goto out;
1804
1805 size = le32_to_cpu(fw_info->fw_len);
1806 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1807 goto out;
1808
1809 memcpy(version, fw_info->version, RTL_VER_SIZE);
1810
1811 pa->code = (__le32 *)(fw->data + start);
1812 pa->size = size;
1813 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001814 if (fw->size % FW_OPCODE_SIZE)
1815 goto out;
1816
1817 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1818
1819 pa->code = (__le32 *)fw->data;
1820 pa->size = fw->size / FW_OPCODE_SIZE;
1821 }
1822 version[RTL_VER_SIZE - 1] = 0;
1823
1824 rc = true;
1825out:
1826 return rc;
1827}
1828
Francois Romieufd112f22011-06-18 00:10:29 +02001829static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1830 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02001831{
Francois Romieufd112f22011-06-18 00:10:29 +02001832 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001833 size_t index;
1834
Francois Romieu1c361ef2011-06-17 17:16:24 +02001835 for (index = 0; index < pa->size; index++) {
1836 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00001837 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001838
hayeswang42b82dc2011-01-10 02:07:25 +00001839 switch(action & 0xf0000000) {
1840 case PHY_READ:
1841 case PHY_DATA_OR:
1842 case PHY_DATA_AND:
1843 case PHY_READ_EFUSE:
1844 case PHY_CLEAR_READCOUNT:
1845 case PHY_WRITE:
1846 case PHY_WRITE_PREVIOUS:
1847 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001848 break;
1849
hayeswang42b82dc2011-01-10 02:07:25 +00001850 case PHY_BJMPN:
1851 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02001852 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001853 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001854 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001855 }
1856 break;
1857 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001858 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001859 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001860 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001861 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001862 }
1863 break;
1864 case PHY_COMP_EQ_SKIPN:
1865 case PHY_COMP_NEQ_SKIPN:
1866 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001867 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001868 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001869 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001870 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001871 }
1872 break;
1873
1874 case PHY_READ_MAC_BYTE:
1875 case PHY_WRITE_MAC_BYTE:
1876 case PHY_WRITE_ERI_WORD:
1877 default:
Francois Romieufd112f22011-06-18 00:10:29 +02001878 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00001879 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02001880 goto out;
françois romieubca03d52011-01-03 15:07:31 +00001881 }
1882 }
Francois Romieufd112f22011-06-18 00:10:29 +02001883 rc = true;
1884out:
1885 return rc;
1886}
françois romieubca03d52011-01-03 15:07:31 +00001887
Francois Romieufd112f22011-06-18 00:10:29 +02001888static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1889{
1890 struct net_device *dev = tp->dev;
1891 int rc = -EINVAL;
1892
1893 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1894 netif_err(tp, ifup, dev, "invalid firwmare\n");
1895 goto out;
1896 }
1897
1898 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1899 rc = 0;
1900out:
1901 return rc;
1902}
1903
1904static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1905{
1906 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1907 u32 predata, count;
1908 size_t index;
1909
1910 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00001911
Francois Romieu1c361ef2011-06-17 17:16:24 +02001912 for (index = 0; index < pa->size; ) {
1913 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00001914 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001915 u32 regno = (action & 0x0fff0000) >> 16;
1916
1917 if (!action)
1918 break;
françois romieubca03d52011-01-03 15:07:31 +00001919
1920 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001921 case PHY_READ:
1922 predata = rtl_readphy(tp, regno);
1923 count++;
1924 index++;
françois romieubca03d52011-01-03 15:07:31 +00001925 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001926 case PHY_DATA_OR:
1927 predata |= data;
1928 index++;
1929 break;
1930 case PHY_DATA_AND:
1931 predata &= data;
1932 index++;
1933 break;
1934 case PHY_BJMPN:
1935 index -= regno;
1936 break;
1937 case PHY_READ_EFUSE:
1938 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1939 index++;
1940 break;
1941 case PHY_CLEAR_READCOUNT:
1942 count = 0;
1943 index++;
1944 break;
1945 case PHY_WRITE:
1946 rtl_writephy(tp, regno, data);
1947 index++;
1948 break;
1949 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02001950 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00001951 break;
1952 case PHY_COMP_EQ_SKIPN:
1953 if (predata == data)
1954 index += regno;
1955 index++;
1956 break;
1957 case PHY_COMP_NEQ_SKIPN:
1958 if (predata != data)
1959 index += regno;
1960 index++;
1961 break;
1962 case PHY_WRITE_PREVIOUS:
1963 rtl_writephy(tp, regno, predata);
1964 index++;
1965 break;
1966 case PHY_SKIPN:
1967 index += regno + 1;
1968 break;
1969 case PHY_DELAY_MS:
1970 mdelay(data);
1971 index++;
1972 break;
1973
1974 case PHY_READ_MAC_BYTE:
1975 case PHY_WRITE_MAC_BYTE:
1976 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001977 default:
1978 BUG();
1979 }
1980 }
1981}
1982
françois romieuf1e02ed2011-01-13 13:07:53 +00001983static void rtl_release_firmware(struct rtl8169_private *tp)
1984{
Francois Romieub6ffd972011-06-17 17:00:05 +02001985 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
1986 release_firmware(tp->rtl_fw->fw);
1987 kfree(tp->rtl_fw);
1988 }
1989 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001990}
1991
François Romieu953a12c2011-04-24 17:38:48 +02001992static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001993{
Francois Romieub6ffd972011-06-17 17:00:05 +02001994 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001995
1996 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02001997 if (!IS_ERR_OR_NULL(rtl_fw))
1998 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02001999}
2000
2001static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
2002{
2003 if (rtl_readphy(tp, reg) != val)
2004 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
2005 else
2006 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00002007}
2008
françois romieu4da19632011-01-03 15:07:55 +00002009static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002011 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002012 { 0x1f, 0x0001 },
2013 { 0x06, 0x006e },
2014 { 0x08, 0x0708 },
2015 { 0x15, 0x4000 },
2016 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
françois romieu0b9b5712009-08-10 19:44:56 +00002018 { 0x1f, 0x0001 },
2019 { 0x03, 0x00a1 },
2020 { 0x02, 0x0008 },
2021 { 0x01, 0x0120 },
2022 { 0x00, 0x1000 },
2023 { 0x04, 0x0800 },
2024 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
françois romieu0b9b5712009-08-10 19:44:56 +00002026 { 0x03, 0xff41 },
2027 { 0x02, 0xdf60 },
2028 { 0x01, 0x0140 },
2029 { 0x00, 0x0077 },
2030 { 0x04, 0x7800 },
2031 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
françois romieu0b9b5712009-08-10 19:44:56 +00002033 { 0x03, 0x802f },
2034 { 0x02, 0x4f02 },
2035 { 0x01, 0x0409 },
2036 { 0x00, 0xf0f9 },
2037 { 0x04, 0x9800 },
2038 { 0x04, 0x9000 },
2039
2040 { 0x03, 0xdf01 },
2041 { 0x02, 0xdf20 },
2042 { 0x01, 0xff95 },
2043 { 0x00, 0xba00 },
2044 { 0x04, 0xa800 },
2045 { 0x04, 0xa000 },
2046
2047 { 0x03, 0xff41 },
2048 { 0x02, 0xdf20 },
2049 { 0x01, 0x0140 },
2050 { 0x00, 0x00bb },
2051 { 0x04, 0xb800 },
2052 { 0x04, 0xb000 },
2053
2054 { 0x03, 0xdf41 },
2055 { 0x02, 0xdc60 },
2056 { 0x01, 0x6340 },
2057 { 0x00, 0x007d },
2058 { 0x04, 0xd800 },
2059 { 0x04, 0xd000 },
2060
2061 { 0x03, 0xdf01 },
2062 { 0x02, 0xdf20 },
2063 { 0x01, 0x100a },
2064 { 0x00, 0xa0ff },
2065 { 0x04, 0xf800 },
2066 { 0x04, 0xf000 },
2067
2068 { 0x1f, 0x0000 },
2069 { 0x0b, 0x0000 },
2070 { 0x00, 0x9200 }
2071 };
2072
françois romieu4da19632011-01-03 15:07:55 +00002073 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
françois romieu4da19632011-01-03 15:07:55 +00002076static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002077{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002078 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002079 { 0x1f, 0x0002 },
2080 { 0x01, 0x90d0 },
2081 { 0x1f, 0x0000 }
2082 };
2083
françois romieu4da19632011-01-03 15:07:55 +00002084 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002085}
2086
françois romieu4da19632011-01-03 15:07:55 +00002087static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002088{
2089 struct pci_dev *pdev = tp->pci_dev;
2090 u16 vendor_id, device_id;
2091
2092 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
2093 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
2094
2095 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
2096 return;
2097
françois romieu4da19632011-01-03 15:07:55 +00002098 rtl_writephy(tp, 0x1f, 0x0001);
2099 rtl_writephy(tp, 0x10, 0xf01b);
2100 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002101}
2102
françois romieu4da19632011-01-03 15:07:55 +00002103static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002104{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002105 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002106 { 0x1f, 0x0001 },
2107 { 0x04, 0x0000 },
2108 { 0x03, 0x00a1 },
2109 { 0x02, 0x0008 },
2110 { 0x01, 0x0120 },
2111 { 0x00, 0x1000 },
2112 { 0x04, 0x0800 },
2113 { 0x04, 0x9000 },
2114 { 0x03, 0x802f },
2115 { 0x02, 0x4f02 },
2116 { 0x01, 0x0409 },
2117 { 0x00, 0xf099 },
2118 { 0x04, 0x9800 },
2119 { 0x04, 0xa000 },
2120 { 0x03, 0xdf01 },
2121 { 0x02, 0xdf20 },
2122 { 0x01, 0xff95 },
2123 { 0x00, 0xba00 },
2124 { 0x04, 0xa800 },
2125 { 0x04, 0xf000 },
2126 { 0x03, 0xdf01 },
2127 { 0x02, 0xdf20 },
2128 { 0x01, 0x101a },
2129 { 0x00, 0xa0ff },
2130 { 0x04, 0xf800 },
2131 { 0x04, 0x0000 },
2132 { 0x1f, 0x0000 },
2133
2134 { 0x1f, 0x0001 },
2135 { 0x10, 0xf41b },
2136 { 0x14, 0xfb54 },
2137 { 0x18, 0xf5c7 },
2138 { 0x1f, 0x0000 },
2139
2140 { 0x1f, 0x0001 },
2141 { 0x17, 0x0cc0 },
2142 { 0x1f, 0x0000 }
2143 };
2144
françois romieu4da19632011-01-03 15:07:55 +00002145 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002146
françois romieu4da19632011-01-03 15:07:55 +00002147 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002148}
2149
françois romieu4da19632011-01-03 15:07:55 +00002150static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002151{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002152 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002153 { 0x1f, 0x0001 },
2154 { 0x04, 0x0000 },
2155 { 0x03, 0x00a1 },
2156 { 0x02, 0x0008 },
2157 { 0x01, 0x0120 },
2158 { 0x00, 0x1000 },
2159 { 0x04, 0x0800 },
2160 { 0x04, 0x9000 },
2161 { 0x03, 0x802f },
2162 { 0x02, 0x4f02 },
2163 { 0x01, 0x0409 },
2164 { 0x00, 0xf099 },
2165 { 0x04, 0x9800 },
2166 { 0x04, 0xa000 },
2167 { 0x03, 0xdf01 },
2168 { 0x02, 0xdf20 },
2169 { 0x01, 0xff95 },
2170 { 0x00, 0xba00 },
2171 { 0x04, 0xa800 },
2172 { 0x04, 0xf000 },
2173 { 0x03, 0xdf01 },
2174 { 0x02, 0xdf20 },
2175 { 0x01, 0x101a },
2176 { 0x00, 0xa0ff },
2177 { 0x04, 0xf800 },
2178 { 0x04, 0x0000 },
2179 { 0x1f, 0x0000 },
2180
2181 { 0x1f, 0x0001 },
2182 { 0x0b, 0x8480 },
2183 { 0x1f, 0x0000 },
2184
2185 { 0x1f, 0x0001 },
2186 { 0x18, 0x67c7 },
2187 { 0x04, 0x2000 },
2188 { 0x03, 0x002f },
2189 { 0x02, 0x4360 },
2190 { 0x01, 0x0109 },
2191 { 0x00, 0x3022 },
2192 { 0x04, 0x2800 },
2193 { 0x1f, 0x0000 },
2194
2195 { 0x1f, 0x0001 },
2196 { 0x17, 0x0cc0 },
2197 { 0x1f, 0x0000 }
2198 };
2199
françois romieu4da19632011-01-03 15:07:55 +00002200 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002201}
2202
françois romieu4da19632011-01-03 15:07:55 +00002203static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002204{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002205 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002206 { 0x10, 0xf41b },
2207 { 0x1f, 0x0000 }
2208 };
2209
françois romieu4da19632011-01-03 15:07:55 +00002210 rtl_writephy(tp, 0x1f, 0x0001);
2211 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002212
françois romieu4da19632011-01-03 15:07:55 +00002213 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002214}
2215
françois romieu4da19632011-01-03 15:07:55 +00002216static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002217{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002218 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002219 { 0x1f, 0x0001 },
2220 { 0x10, 0xf41b },
2221 { 0x1f, 0x0000 }
2222 };
2223
françois romieu4da19632011-01-03 15:07:55 +00002224 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002225}
2226
françois romieu4da19632011-01-03 15:07:55 +00002227static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002228{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002229 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002230 { 0x1f, 0x0000 },
2231 { 0x1d, 0x0f00 },
2232 { 0x1f, 0x0002 },
2233 { 0x0c, 0x1ec8 },
2234 { 0x1f, 0x0000 }
2235 };
2236
françois romieu4da19632011-01-03 15:07:55 +00002237 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002238}
2239
françois romieu4da19632011-01-03 15:07:55 +00002240static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002241{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002242 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002243 { 0x1f, 0x0001 },
2244 { 0x1d, 0x3d98 },
2245 { 0x1f, 0x0000 }
2246 };
2247
françois romieu4da19632011-01-03 15:07:55 +00002248 rtl_writephy(tp, 0x1f, 0x0000);
2249 rtl_patchphy(tp, 0x14, 1 << 5);
2250 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002251
françois romieu4da19632011-01-03 15:07:55 +00002252 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002253}
2254
françois romieu4da19632011-01-03 15:07:55 +00002255static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002256{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002257 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002258 { 0x1f, 0x0001 },
2259 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002260 { 0x1f, 0x0002 },
2261 { 0x00, 0x88d4 },
2262 { 0x01, 0x82b1 },
2263 { 0x03, 0x7002 },
2264 { 0x08, 0x9e30 },
2265 { 0x09, 0x01f0 },
2266 { 0x0a, 0x5500 },
2267 { 0x0c, 0x00c8 },
2268 { 0x1f, 0x0003 },
2269 { 0x12, 0xc096 },
2270 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002271 { 0x1f, 0x0000 },
2272 { 0x1f, 0x0000 },
2273 { 0x09, 0x2000 },
2274 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002275 };
2276
françois romieu4da19632011-01-03 15:07:55 +00002277 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002278
françois romieu4da19632011-01-03 15:07:55 +00002279 rtl_patchphy(tp, 0x14, 1 << 5);
2280 rtl_patchphy(tp, 0x0d, 1 << 5);
2281 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002282}
2283
françois romieu4da19632011-01-03 15:07:55 +00002284static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002285{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002286 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002287 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002288 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002289 { 0x03, 0x802f },
2290 { 0x02, 0x4f02 },
2291 { 0x01, 0x0409 },
2292 { 0x00, 0xf099 },
2293 { 0x04, 0x9800 },
2294 { 0x04, 0x9000 },
2295 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002296 { 0x1f, 0x0002 },
2297 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002298 { 0x06, 0x0761 },
2299 { 0x1f, 0x0003 },
2300 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002301 { 0x1f, 0x0000 }
2302 };
2303
françois romieu4da19632011-01-03 15:07:55 +00002304 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002305
françois romieu4da19632011-01-03 15:07:55 +00002306 rtl_patchphy(tp, 0x16, 1 << 0);
2307 rtl_patchphy(tp, 0x14, 1 << 5);
2308 rtl_patchphy(tp, 0x0d, 1 << 5);
2309 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002310}
2311
françois romieu4da19632011-01-03 15:07:55 +00002312static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002313{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002314 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002315 { 0x1f, 0x0001 },
2316 { 0x12, 0x2300 },
2317 { 0x1d, 0x3d98 },
2318 { 0x1f, 0x0002 },
2319 { 0x0c, 0x7eb8 },
2320 { 0x06, 0x5461 },
2321 { 0x1f, 0x0003 },
2322 { 0x16, 0x0f0a },
2323 { 0x1f, 0x0000 }
2324 };
2325
françois romieu4da19632011-01-03 15:07:55 +00002326 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002327
françois romieu4da19632011-01-03 15:07:55 +00002328 rtl_patchphy(tp, 0x16, 1 << 0);
2329 rtl_patchphy(tp, 0x14, 1 << 5);
2330 rtl_patchphy(tp, 0x0d, 1 << 5);
2331 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002332}
2333
françois romieu4da19632011-01-03 15:07:55 +00002334static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002335{
françois romieu4da19632011-01-03 15:07:55 +00002336 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002337}
2338
françois romieubca03d52011-01-03 15:07:31 +00002339static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002340{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002341 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002342 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002343 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002344 { 0x06, 0x4064 },
2345 { 0x07, 0x2863 },
2346 { 0x08, 0x059c },
2347 { 0x09, 0x26b4 },
2348 { 0x0a, 0x6a19 },
2349 { 0x0b, 0xdcc8 },
2350 { 0x10, 0xf06d },
2351 { 0x14, 0x7f68 },
2352 { 0x18, 0x7fd9 },
2353 { 0x1c, 0xf0ff },
2354 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002355 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002356 { 0x12, 0xf49f },
2357 { 0x13, 0x070b },
2358 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002359 { 0x14, 0x94c0 },
2360
2361 /*
2362 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002363 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002364 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002365 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002366 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002367 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002368 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002369 { 0x06, 0x5561 },
2370
2371 /*
2372 * Can not link to 1Gbps with bad cable
2373 * Decrease SNR threshold form 21.07dB to 19.04dB
2374 */
2375 { 0x1f, 0x0001 },
2376 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002377
2378 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002379 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002380 };
françois romieubca03d52011-01-03 15:07:31 +00002381 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002382
françois romieu4da19632011-01-03 15:07:55 +00002383 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002384
françois romieubca03d52011-01-03 15:07:31 +00002385 /*
2386 * Rx Error Issue
2387 * Fine Tune Switching regulator parameter
2388 */
françois romieu4da19632011-01-03 15:07:55 +00002389 rtl_writephy(tp, 0x1f, 0x0002);
2390 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2391 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002392
françois romieudaf9df62009-10-07 12:44:20 +00002393 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002394 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002395 { 0x1f, 0x0002 },
2396 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002397 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002398 { 0x05, 0x8330 },
2399 { 0x06, 0x669a },
2400 { 0x1f, 0x0002 }
2401 };
2402 int val;
2403
françois romieu4da19632011-01-03 15:07:55 +00002404 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002405
françois romieu4da19632011-01-03 15:07:55 +00002406 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002407
2408 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002409 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002410 0x0065, 0x0066, 0x0067, 0x0068,
2411 0x0069, 0x006a, 0x006b, 0x006c
2412 };
2413 int i;
2414
françois romieu4da19632011-01-03 15:07:55 +00002415 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002416
2417 val &= 0xff00;
2418 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002419 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002420 }
2421 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002422 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002423 { 0x1f, 0x0002 },
2424 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002425 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002426 { 0x05, 0x8330 },
2427 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002428 };
2429
françois romieu4da19632011-01-03 15:07:55 +00002430 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002431 }
2432
françois romieubca03d52011-01-03 15:07:31 +00002433 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002434 rtl_writephy(tp, 0x1f, 0x0002);
2435 rtl_patchphy(tp, 0x0d, 0x0300);
2436 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002437
françois romieubca03d52011-01-03 15:07:31 +00002438 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002439 rtl_writephy(tp, 0x1f, 0x0002);
2440 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2441 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002442
françois romieu4da19632011-01-03 15:07:55 +00002443 rtl_writephy(tp, 0x1f, 0x0005);
2444 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002445
2446 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002447
françois romieu4da19632011-01-03 15:07:55 +00002448 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002449}
2450
françois romieubca03d52011-01-03 15:07:31 +00002451static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002452{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002453 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002454 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002455 { 0x1f, 0x0001 },
2456 { 0x06, 0x4064 },
2457 { 0x07, 0x2863 },
2458 { 0x08, 0x059c },
2459 { 0x09, 0x26b4 },
2460 { 0x0a, 0x6a19 },
2461 { 0x0b, 0xdcc8 },
2462 { 0x10, 0xf06d },
2463 { 0x14, 0x7f68 },
2464 { 0x18, 0x7fd9 },
2465 { 0x1c, 0xf0ff },
2466 { 0x1d, 0x3d9c },
2467 { 0x1f, 0x0003 },
2468 { 0x12, 0xf49f },
2469 { 0x13, 0x070b },
2470 { 0x1a, 0x05ad },
2471 { 0x14, 0x94c0 },
2472
françois romieubca03d52011-01-03 15:07:31 +00002473 /*
2474 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002475 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002476 */
françois romieudaf9df62009-10-07 12:44:20 +00002477 { 0x1f, 0x0002 },
2478 { 0x06, 0x5561 },
2479 { 0x1f, 0x0005 },
2480 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002481 { 0x06, 0x5561 },
2482
2483 /*
2484 * Can not link to 1Gbps with bad cable
2485 * Decrease SNR threshold form 21.07dB to 19.04dB
2486 */
2487 { 0x1f, 0x0001 },
2488 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002489
2490 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002491 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002492 };
françois romieubca03d52011-01-03 15:07:31 +00002493 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002494
françois romieu4da19632011-01-03 15:07:55 +00002495 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002496
2497 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002498 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002499 { 0x1f, 0x0002 },
2500 { 0x05, 0x669a },
2501 { 0x1f, 0x0005 },
2502 { 0x05, 0x8330 },
2503 { 0x06, 0x669a },
2504
2505 { 0x1f, 0x0002 }
2506 };
2507 int val;
2508
françois romieu4da19632011-01-03 15:07:55 +00002509 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002510
françois romieu4da19632011-01-03 15:07:55 +00002511 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002512 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002513 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002514 0x0065, 0x0066, 0x0067, 0x0068,
2515 0x0069, 0x006a, 0x006b, 0x006c
2516 };
2517 int i;
2518
françois romieu4da19632011-01-03 15:07:55 +00002519 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002520
2521 val &= 0xff00;
2522 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002523 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002524 }
2525 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002526 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002527 { 0x1f, 0x0002 },
2528 { 0x05, 0x2642 },
2529 { 0x1f, 0x0005 },
2530 { 0x05, 0x8330 },
2531 { 0x06, 0x2642 }
2532 };
2533
françois romieu4da19632011-01-03 15:07:55 +00002534 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002535 }
2536
françois romieubca03d52011-01-03 15:07:31 +00002537 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002538 rtl_writephy(tp, 0x1f, 0x0002);
2539 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2540 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002541
françois romieubca03d52011-01-03 15:07:31 +00002542 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002543 rtl_writephy(tp, 0x1f, 0x0002);
2544 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002545
françois romieu4da19632011-01-03 15:07:55 +00002546 rtl_writephy(tp, 0x1f, 0x0005);
2547 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002548
2549 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002550
françois romieu4da19632011-01-03 15:07:55 +00002551 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002552}
2553
françois romieu4da19632011-01-03 15:07:55 +00002554static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002555{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002556 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002557 { 0x1f, 0x0002 },
2558 { 0x10, 0x0008 },
2559 { 0x0d, 0x006c },
2560
2561 { 0x1f, 0x0000 },
2562 { 0x0d, 0xf880 },
2563
2564 { 0x1f, 0x0001 },
2565 { 0x17, 0x0cc0 },
2566
2567 { 0x1f, 0x0001 },
2568 { 0x0b, 0xa4d8 },
2569 { 0x09, 0x281c },
2570 { 0x07, 0x2883 },
2571 { 0x0a, 0x6b35 },
2572 { 0x1d, 0x3da4 },
2573 { 0x1c, 0xeffd },
2574 { 0x14, 0x7f52 },
2575 { 0x18, 0x7fc6 },
2576 { 0x08, 0x0601 },
2577 { 0x06, 0x4063 },
2578 { 0x10, 0xf074 },
2579 { 0x1f, 0x0003 },
2580 { 0x13, 0x0789 },
2581 { 0x12, 0xf4bd },
2582 { 0x1a, 0x04fd },
2583 { 0x14, 0x84b0 },
2584 { 0x1f, 0x0000 },
2585 { 0x00, 0x9200 },
2586
2587 { 0x1f, 0x0005 },
2588 { 0x01, 0x0340 },
2589 { 0x1f, 0x0001 },
2590 { 0x04, 0x4000 },
2591 { 0x03, 0x1d21 },
2592 { 0x02, 0x0c32 },
2593 { 0x01, 0x0200 },
2594 { 0x00, 0x5554 },
2595 { 0x04, 0x4800 },
2596 { 0x04, 0x4000 },
2597 { 0x04, 0xf000 },
2598 { 0x03, 0xdf01 },
2599 { 0x02, 0xdf20 },
2600 { 0x01, 0x101a },
2601 { 0x00, 0xa0ff },
2602 { 0x04, 0xf800 },
2603 { 0x04, 0xf000 },
2604 { 0x1f, 0x0000 },
2605
2606 { 0x1f, 0x0007 },
2607 { 0x1e, 0x0023 },
2608 { 0x16, 0x0000 },
2609 { 0x1f, 0x0000 }
2610 };
2611
françois romieu4da19632011-01-03 15:07:55 +00002612 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002613}
2614
françois romieue6de30d2011-01-03 15:08:37 +00002615static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2616{
2617 static const struct phy_reg phy_reg_init[] = {
2618 { 0x1f, 0x0001 },
2619 { 0x17, 0x0cc0 },
2620
2621 { 0x1f, 0x0007 },
2622 { 0x1e, 0x002d },
2623 { 0x18, 0x0040 },
2624 { 0x1f, 0x0000 }
2625 };
2626
2627 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2628 rtl_patchphy(tp, 0x0d, 1 << 5);
2629}
2630
hayeswang01dc7fe2011-03-21 01:50:28 +00002631static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2632{
2633 static const struct phy_reg phy_reg_init[] = {
2634 /* Enable Delay cap */
2635 { 0x1f, 0x0005 },
2636 { 0x05, 0x8b80 },
2637 { 0x06, 0xc896 },
2638 { 0x1f, 0x0000 },
2639
2640 /* Channel estimation fine tune */
2641 { 0x1f, 0x0001 },
2642 { 0x0b, 0x6c20 },
2643 { 0x07, 0x2872 },
2644 { 0x1c, 0xefff },
2645 { 0x1f, 0x0003 },
2646 { 0x14, 0x6420 },
2647 { 0x1f, 0x0000 },
2648
2649 /* Update PFM & 10M TX idle timer */
2650 { 0x1f, 0x0007 },
2651 { 0x1e, 0x002f },
2652 { 0x15, 0x1919 },
2653 { 0x1f, 0x0000 },
2654
2655 { 0x1f, 0x0007 },
2656 { 0x1e, 0x00ac },
2657 { 0x18, 0x0006 },
2658 { 0x1f, 0x0000 }
2659 };
2660
Francois Romieu15ecd032011-04-27 13:52:22 -07002661 rtl_apply_firmware(tp);
2662
hayeswang01dc7fe2011-03-21 01:50:28 +00002663 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2664
2665 /* DCO enable for 10M IDLE Power */
2666 rtl_writephy(tp, 0x1f, 0x0007);
2667 rtl_writephy(tp, 0x1e, 0x0023);
2668 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2669 rtl_writephy(tp, 0x1f, 0x0000);
2670
2671 /* For impedance matching */
2672 rtl_writephy(tp, 0x1f, 0x0002);
2673 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002674 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002675
2676 /* PHY auto speed down */
2677 rtl_writephy(tp, 0x1f, 0x0007);
2678 rtl_writephy(tp, 0x1e, 0x002d);
2679 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2680 rtl_writephy(tp, 0x1f, 0x0000);
2681 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2682
2683 rtl_writephy(tp, 0x1f, 0x0005);
2684 rtl_writephy(tp, 0x05, 0x8b86);
2685 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2686 rtl_writephy(tp, 0x1f, 0x0000);
2687
2688 rtl_writephy(tp, 0x1f, 0x0005);
2689 rtl_writephy(tp, 0x05, 0x8b85);
2690 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2691 rtl_writephy(tp, 0x1f, 0x0007);
2692 rtl_writephy(tp, 0x1e, 0x0020);
2693 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2694 rtl_writephy(tp, 0x1f, 0x0006);
2695 rtl_writephy(tp, 0x00, 0x5a00);
2696 rtl_writephy(tp, 0x1f, 0x0000);
2697 rtl_writephy(tp, 0x0d, 0x0007);
2698 rtl_writephy(tp, 0x0e, 0x003c);
2699 rtl_writephy(tp, 0x0d, 0x4007);
2700 rtl_writephy(tp, 0x0e, 0x0000);
2701 rtl_writephy(tp, 0x0d, 0x0000);
2702}
2703
françois romieu4da19632011-01-03 15:07:55 +00002704static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002705{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002706 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002707 { 0x1f, 0x0003 },
2708 { 0x08, 0x441d },
2709 { 0x01, 0x9100 },
2710 { 0x1f, 0x0000 }
2711 };
2712
françois romieu4da19632011-01-03 15:07:55 +00002713 rtl_writephy(tp, 0x1f, 0x0000);
2714 rtl_patchphy(tp, 0x11, 1 << 12);
2715 rtl_patchphy(tp, 0x19, 1 << 13);
2716 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002717
françois romieu4da19632011-01-03 15:07:55 +00002718 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002719}
2720
Hayes Wang5a5e4442011-02-22 17:26:21 +08002721static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2722{
2723 static const struct phy_reg phy_reg_init[] = {
2724 { 0x1f, 0x0005 },
2725 { 0x1a, 0x0000 },
2726 { 0x1f, 0x0000 },
2727
2728 { 0x1f, 0x0004 },
2729 { 0x1c, 0x0000 },
2730 { 0x1f, 0x0000 },
2731
2732 { 0x1f, 0x0001 },
2733 { 0x15, 0x7701 },
2734 { 0x1f, 0x0000 }
2735 };
2736
2737 /* Disable ALDPS before ram code */
2738 rtl_writephy(tp, 0x1f, 0x0000);
2739 rtl_writephy(tp, 0x18, 0x0310);
2740 msleep(100);
2741
François Romieu953a12c2011-04-24 17:38:48 +02002742 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002743
2744 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2745}
2746
Francois Romieu5615d9f2007-08-17 17:50:46 +02002747static void rtl_hw_phy_config(struct net_device *dev)
2748{
2749 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002750
2751 rtl8169_print_mac_version(tp);
2752
2753 switch (tp->mac_version) {
2754 case RTL_GIGA_MAC_VER_01:
2755 break;
2756 case RTL_GIGA_MAC_VER_02:
2757 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002758 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002759 break;
2760 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002761 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002762 break;
françois romieu2e9558562009-08-10 19:44:19 +00002763 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002764 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002765 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002766 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002767 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002768 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002769 case RTL_GIGA_MAC_VER_07:
2770 case RTL_GIGA_MAC_VER_08:
2771 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002772 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002773 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002774 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002775 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002776 break;
2777 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002778 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002779 break;
2780 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002781 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002782 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002783 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002784 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002785 break;
2786 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002787 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002788 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002789 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002790 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002791 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002792 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002793 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002794 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002795 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002796 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002797 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002798 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002799 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002800 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002801 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002802 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002803 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002804 break;
2805 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002806 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002807 break;
2808 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002809 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002810 break;
françois romieue6de30d2011-01-03 15:08:37 +00002811 case RTL_GIGA_MAC_VER_28:
2812 rtl8168d_4_hw_phy_config(tp);
2813 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002814 case RTL_GIGA_MAC_VER_29:
2815 case RTL_GIGA_MAC_VER_30:
2816 rtl8105e_hw_phy_config(tp);
2817 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002818 case RTL_GIGA_MAC_VER_31:
2819 /* None. */
2820 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002821 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002822 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002823 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002824 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002825
Francois Romieu5615d9f2007-08-17 17:50:46 +02002826 default:
2827 break;
2828 }
2829}
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831static void rtl8169_phy_timer(unsigned long __opaque)
2832{
2833 struct net_device *dev = (struct net_device *)__opaque;
2834 struct rtl8169_private *tp = netdev_priv(dev);
2835 struct timer_list *timer = &tp->timer;
2836 void __iomem *ioaddr = tp->mmio_addr;
2837 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2838
Francois Romieubcf0bf92006-07-26 23:14:13 +02002839 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 spin_lock_irq(&tp->lock);
2842
françois romieu4da19632011-01-03 15:07:55 +00002843 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002844 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 * A busy loop could burn quite a few cycles on nowadays CPU.
2846 * Let's delay the execution of the timer for a few ticks.
2847 */
2848 timeout = HZ/10;
2849 goto out_mod_timer;
2850 }
2851
2852 if (tp->link_ok(ioaddr))
2853 goto out_unlock;
2854
Joe Perchesbf82c182010-02-09 11:49:50 +00002855 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
françois romieu4da19632011-01-03 15:07:55 +00002857 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859out_mod_timer:
2860 mod_timer(timer, jiffies + timeout);
2861out_unlock:
2862 spin_unlock_irq(&tp->lock);
2863}
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865#ifdef CONFIG_NET_POLL_CONTROLLER
2866/*
2867 * Polling 'interrupt' - used by things like netconsole to send skbs
2868 * without having to re-enable interrupts. It's not called while
2869 * the interrupt routine is executing.
2870 */
2871static void rtl8169_netpoll(struct net_device *dev)
2872{
2873 struct rtl8169_private *tp = netdev_priv(dev);
2874 struct pci_dev *pdev = tp->pci_dev;
2875
2876 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002877 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 enable_irq(pdev->irq);
2879}
2880#endif
2881
2882static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2883 void __iomem *ioaddr)
2884{
2885 iounmap(ioaddr);
2886 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002887 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 pci_disable_device(pdev);
2889 free_netdev(dev);
2890}
2891
Francois Romieubf793292006-11-01 00:53:05 +01002892static void rtl8169_phy_reset(struct net_device *dev,
2893 struct rtl8169_private *tp)
2894{
Francois Romieu07d3f512007-02-21 22:40:46 +01002895 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002896
françois romieu4da19632011-01-03 15:07:55 +00002897 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002898 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002899 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002900 return;
2901 msleep(1);
2902 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002903 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002904}
2905
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002906static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002908 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002909
Francois Romieu5615d9f2007-08-17 17:50:46 +02002910 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002911
Marcus Sundberg773328942008-07-10 21:28:08 +02002912 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2913 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2914 RTL_W8(0x82, 0x01);
2915 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002916
Francois Romieu6dccd162007-02-13 23:38:05 +01002917 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2918
2919 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2920 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002921
Francois Romieubcf0bf92006-07-26 23:14:13 +02002922 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002923 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2924 RTL_W8(0x82, 0x01);
2925 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002926 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002927 }
2928
Francois Romieubf793292006-11-01 00:53:05 +01002929 rtl8169_phy_reset(dev, tp);
2930
Oliver Neukum54405cd2011-01-06 21:55:13 +01002931 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002932 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2933 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2934 (tp->mii.supports_gmii ?
2935 ADVERTISED_1000baseT_Half |
2936 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002937
Joe Perchesbf82c182010-02-09 11:49:50 +00002938 if (RTL_R8(PHYstatus) & TBI_Enable)
2939 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002940}
2941
Francois Romieu773d2022007-01-31 23:47:43 +01002942static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2943{
2944 void __iomem *ioaddr = tp->mmio_addr;
2945 u32 high;
2946 u32 low;
2947
2948 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2949 high = addr[4] | (addr[5] << 8);
2950
2951 spin_lock_irq(&tp->lock);
2952
2953 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002954
Francois Romieu773d2022007-01-31 23:47:43 +01002955 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002956 RTL_R32(MAC4);
2957
Francois Romieu78f1cd02010-03-27 19:35:46 -07002958 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002959 RTL_R32(MAC0);
2960
Francois Romieu773d2022007-01-31 23:47:43 +01002961 RTL_W8(Cfg9346, Cfg9346_Lock);
2962
2963 spin_unlock_irq(&tp->lock);
2964}
2965
2966static int rtl_set_mac_address(struct net_device *dev, void *p)
2967{
2968 struct rtl8169_private *tp = netdev_priv(dev);
2969 struct sockaddr *addr = p;
2970
2971 if (!is_valid_ether_addr(addr->sa_data))
2972 return -EADDRNOTAVAIL;
2973
2974 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2975
2976 rtl_rar_set(tp, dev->dev_addr);
2977
2978 return 0;
2979}
2980
Francois Romieu5f787a12006-08-17 13:02:36 +02002981static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2982{
2983 struct rtl8169_private *tp = netdev_priv(dev);
2984 struct mii_ioctl_data *data = if_mii(ifr);
2985
Francois Romieu8b4ab282008-11-19 22:05:25 -08002986 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2987}
Francois Romieu5f787a12006-08-17 13:02:36 +02002988
Francois Romieucecb5fd2011-04-01 10:21:07 +02002989static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2990 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08002991{
Francois Romieu5f787a12006-08-17 13:02:36 +02002992 switch (cmd) {
2993 case SIOCGMIIPHY:
2994 data->phy_id = 32; /* Internal PHY */
2995 return 0;
2996
2997 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002998 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002999 return 0;
3000
3001 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00003002 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02003003 return 0;
3004 }
3005 return -EOPNOTSUPP;
3006}
3007
Francois Romieu8b4ab282008-11-19 22:05:25 -08003008static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
3009{
3010 return -EOPNOTSUPP;
3011}
3012
Francois Romieu0e485152007-02-20 00:00:26 +01003013static const struct rtl_cfg_info {
3014 void (*hw_start)(struct net_device *);
3015 unsigned int region;
3016 unsigned int align;
3017 u16 intr_event;
3018 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003019 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003020 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003021} rtl_cfg_infos [] = {
3022 [RTL_CFG_0] = {
3023 .hw_start = rtl_hw_start_8169,
3024 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003025 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003026 .intr_event = SYSErr | LinkChg | RxOverflow |
3027 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003028 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003029 .features = RTL_FEATURE_GMII,
3030 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003031 },
3032 [RTL_CFG_1] = {
3033 .hw_start = rtl_hw_start_8168,
3034 .region = 2,
3035 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003036 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003037 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003038 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003039 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3040 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003041 },
3042 [RTL_CFG_2] = {
3043 .hw_start = rtl_hw_start_8101,
3044 .region = 2,
3045 .align = 8,
3046 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3047 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003048 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003049 .features = RTL_FEATURE_MSI,
3050 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003051 }
3052};
3053
Francois Romieufbac58f2007-10-04 22:51:38 +02003054/* Cfg9346_Unlock assumed. */
3055static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3056 const struct rtl_cfg_info *cfg)
3057{
3058 unsigned msi = 0;
3059 u8 cfg2;
3060
3061 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003062 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003063 if (pci_enable_msi(pdev)) {
3064 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3065 } else {
3066 cfg2 |= MSIEnable;
3067 msi = RTL_FEATURE_MSI;
3068 }
3069 }
3070 RTL_W8(Config2, cfg2);
3071 return msi;
3072}
3073
3074static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3075{
3076 if (tp->features & RTL_FEATURE_MSI) {
3077 pci_disable_msi(pdev);
3078 tp->features &= ~RTL_FEATURE_MSI;
3079 }
3080}
3081
Francois Romieu8b4ab282008-11-19 22:05:25 -08003082static const struct net_device_ops rtl8169_netdev_ops = {
3083 .ndo_open = rtl8169_open,
3084 .ndo_stop = rtl8169_close,
3085 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003086 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003087 .ndo_tx_timeout = rtl8169_tx_timeout,
3088 .ndo_validate_addr = eth_validate_addr,
3089 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003090 .ndo_fix_features = rtl8169_fix_features,
3091 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003092 .ndo_set_mac_address = rtl_set_mac_address,
3093 .ndo_do_ioctl = rtl8169_ioctl,
3094 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003095#ifdef CONFIG_NET_POLL_CONTROLLER
3096 .ndo_poll_controller = rtl8169_netpoll,
3097#endif
3098
3099};
3100
françois romieuc0e45c12011-01-03 15:08:04 +00003101static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3102{
3103 struct mdio_ops *ops = &tp->mdio_ops;
3104
3105 switch (tp->mac_version) {
3106 case RTL_GIGA_MAC_VER_27:
3107 ops->write = r8168dp_1_mdio_write;
3108 ops->read = r8168dp_1_mdio_read;
3109 break;
françois romieue6de30d2011-01-03 15:08:37 +00003110 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003111 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003112 ops->write = r8168dp_2_mdio_write;
3113 ops->read = r8168dp_2_mdio_read;
3114 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003115 default:
3116 ops->write = r8169_mdio_write;
3117 ops->read = r8169_mdio_read;
3118 break;
3119 }
3120}
3121
françois romieu065c27c2011-01-03 15:08:12 +00003122static void r810x_phy_power_down(struct rtl8169_private *tp)
3123{
3124 rtl_writephy(tp, 0x1f, 0x0000);
3125 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3126}
3127
3128static void r810x_phy_power_up(struct rtl8169_private *tp)
3129{
3130 rtl_writephy(tp, 0x1f, 0x0000);
3131 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3132}
3133
3134static void r810x_pll_power_down(struct rtl8169_private *tp)
3135{
3136 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3137 rtl_writephy(tp, 0x1f, 0x0000);
3138 rtl_writephy(tp, MII_BMCR, 0x0000);
3139 return;
3140 }
3141
3142 r810x_phy_power_down(tp);
3143}
3144
3145static void r810x_pll_power_up(struct rtl8169_private *tp)
3146{
3147 r810x_phy_power_up(tp);
3148}
3149
3150static void r8168_phy_power_up(struct rtl8169_private *tp)
3151{
3152 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003153 switch (tp->mac_version) {
3154 case RTL_GIGA_MAC_VER_11:
3155 case RTL_GIGA_MAC_VER_12:
3156 case RTL_GIGA_MAC_VER_17:
3157 case RTL_GIGA_MAC_VER_18:
3158 case RTL_GIGA_MAC_VER_19:
3159 case RTL_GIGA_MAC_VER_20:
3160 case RTL_GIGA_MAC_VER_21:
3161 case RTL_GIGA_MAC_VER_22:
3162 case RTL_GIGA_MAC_VER_23:
3163 case RTL_GIGA_MAC_VER_24:
3164 case RTL_GIGA_MAC_VER_25:
3165 case RTL_GIGA_MAC_VER_26:
3166 case RTL_GIGA_MAC_VER_27:
3167 case RTL_GIGA_MAC_VER_28:
3168 case RTL_GIGA_MAC_VER_31:
3169 rtl_writephy(tp, 0x0e, 0x0000);
3170 break;
3171 default:
3172 break;
3173 }
françois romieu065c27c2011-01-03 15:08:12 +00003174 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3175}
3176
3177static void r8168_phy_power_down(struct rtl8169_private *tp)
3178{
3179 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003180 switch (tp->mac_version) {
3181 case RTL_GIGA_MAC_VER_32:
3182 case RTL_GIGA_MAC_VER_33:
3183 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3184 break;
3185
3186 case RTL_GIGA_MAC_VER_11:
3187 case RTL_GIGA_MAC_VER_12:
3188 case RTL_GIGA_MAC_VER_17:
3189 case RTL_GIGA_MAC_VER_18:
3190 case RTL_GIGA_MAC_VER_19:
3191 case RTL_GIGA_MAC_VER_20:
3192 case RTL_GIGA_MAC_VER_21:
3193 case RTL_GIGA_MAC_VER_22:
3194 case RTL_GIGA_MAC_VER_23:
3195 case RTL_GIGA_MAC_VER_24:
3196 case RTL_GIGA_MAC_VER_25:
3197 case RTL_GIGA_MAC_VER_26:
3198 case RTL_GIGA_MAC_VER_27:
3199 case RTL_GIGA_MAC_VER_28:
3200 case RTL_GIGA_MAC_VER_31:
3201 rtl_writephy(tp, 0x0e, 0x0200);
3202 default:
3203 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3204 break;
3205 }
françois romieu065c27c2011-01-03 15:08:12 +00003206}
3207
3208static void r8168_pll_power_down(struct rtl8169_private *tp)
3209{
3210 void __iomem *ioaddr = tp->mmio_addr;
3211
Francois Romieucecb5fd2011-04-01 10:21:07 +02003212 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3213 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3214 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003215 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003216 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003217 }
françois romieu065c27c2011-01-03 15:08:12 +00003218
Francois Romieucecb5fd2011-04-01 10:21:07 +02003219 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3220 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003221 (RTL_R16(CPlusCmd) & ASF)) {
3222 return;
3223 }
3224
hayeswang01dc7fe2011-03-21 01:50:28 +00003225 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3226 tp->mac_version == RTL_GIGA_MAC_VER_33)
3227 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3228
françois romieu065c27c2011-01-03 15:08:12 +00003229 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3230 rtl_writephy(tp, 0x1f, 0x0000);
3231 rtl_writephy(tp, MII_BMCR, 0x0000);
3232
3233 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3234 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3235 return;
3236 }
3237
3238 r8168_phy_power_down(tp);
3239
3240 switch (tp->mac_version) {
3241 case RTL_GIGA_MAC_VER_25:
3242 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003243 case RTL_GIGA_MAC_VER_27:
3244 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003245 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003246 case RTL_GIGA_MAC_VER_32:
3247 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003248 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3249 break;
3250 }
3251}
3252
3253static void r8168_pll_power_up(struct rtl8169_private *tp)
3254{
3255 void __iomem *ioaddr = tp->mmio_addr;
3256
Francois Romieucecb5fd2011-04-01 10:21:07 +02003257 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3258 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3259 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003260 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003261 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003262 }
françois romieu065c27c2011-01-03 15:08:12 +00003263
3264 switch (tp->mac_version) {
3265 case RTL_GIGA_MAC_VER_25:
3266 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003267 case RTL_GIGA_MAC_VER_27:
3268 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003269 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003270 case RTL_GIGA_MAC_VER_32:
3271 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003272 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3273 break;
3274 }
3275
3276 r8168_phy_power_up(tp);
3277}
3278
3279static void rtl_pll_power_op(struct rtl8169_private *tp,
3280 void (*op)(struct rtl8169_private *))
3281{
3282 if (op)
3283 op(tp);
3284}
3285
3286static void rtl_pll_power_down(struct rtl8169_private *tp)
3287{
3288 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3289}
3290
3291static void rtl_pll_power_up(struct rtl8169_private *tp)
3292{
3293 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3294}
3295
3296static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3297{
3298 struct pll_power_ops *ops = &tp->pll_power_ops;
3299
3300 switch (tp->mac_version) {
3301 case RTL_GIGA_MAC_VER_07:
3302 case RTL_GIGA_MAC_VER_08:
3303 case RTL_GIGA_MAC_VER_09:
3304 case RTL_GIGA_MAC_VER_10:
3305 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003306 case RTL_GIGA_MAC_VER_29:
3307 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003308 ops->down = r810x_pll_power_down;
3309 ops->up = r810x_pll_power_up;
3310 break;
3311
3312 case RTL_GIGA_MAC_VER_11:
3313 case RTL_GIGA_MAC_VER_12:
3314 case RTL_GIGA_MAC_VER_17:
3315 case RTL_GIGA_MAC_VER_18:
3316 case RTL_GIGA_MAC_VER_19:
3317 case RTL_GIGA_MAC_VER_20:
3318 case RTL_GIGA_MAC_VER_21:
3319 case RTL_GIGA_MAC_VER_22:
3320 case RTL_GIGA_MAC_VER_23:
3321 case RTL_GIGA_MAC_VER_24:
3322 case RTL_GIGA_MAC_VER_25:
3323 case RTL_GIGA_MAC_VER_26:
3324 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003325 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003326 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003327 case RTL_GIGA_MAC_VER_32:
3328 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003329 ops->down = r8168_pll_power_down;
3330 ops->up = r8168_pll_power_up;
3331 break;
3332
3333 default:
3334 ops->down = NULL;
3335 ops->up = NULL;
3336 break;
3337 }
3338}
3339
Francois Romieu6f43adc2011-04-29 15:05:51 +02003340static void rtl_hw_reset(struct rtl8169_private *tp)
3341{
3342 void __iomem *ioaddr = tp->mmio_addr;
3343 int i;
3344
3345 /* Soft reset the chip. */
3346 RTL_W8(ChipCmd, CmdReset);
3347
3348 /* Check that the chip has finished the reset. */
3349 for (i = 0; i < 100; i++) {
3350 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3351 break;
3352 msleep_interruptible(1);
3353 }
3354}
3355
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003356static int __devinit
3357rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3358{
Francois Romieu0e485152007-02-20 00:00:26 +01003359 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3360 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003362 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003363 struct net_device *dev;
3364 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003365 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003366 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003368 if (netif_msg_drv(&debug)) {
3369 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3370 MODULENAME, RTL8169_VERSION);
3371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003374 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003375 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003376 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003377 rc = -ENOMEM;
3378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 }
3380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003382 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003384 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003385 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003386 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Francois Romieuccdffb92008-07-26 14:26:06 +02003388 mii = &tp->mii;
3389 mii->dev = dev;
3390 mii->mdio_read = rtl_mdio_read;
3391 mii->mdio_write = rtl_mdio_write;
3392 mii->phy_id_mask = 0x1f;
3393 mii->reg_num_mask = 0x1f;
3394 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3395
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003396 /* disable ASPM completely as that cause random device stop working
3397 * problems as well as full system hangs for some PCIe devices users */
3398 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3399 PCIE_LINK_STATE_CLKPM);
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3402 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003403 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003404 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003405 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 }
3407
françois romieu87aeec72010-04-26 11:42:06 +00003408 if (pci_set_mwi(pdev) < 0)
3409 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003412 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003413 netif_err(tp, probe, dev,
3414 "region #%d not an MMIO resource, aborting\n",
3415 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003417 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003419
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003421 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003422 netif_err(tp, probe, dev,
3423 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003425 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 }
3427
3428 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003429 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003430 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003431 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
3433
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003434 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
3436 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003437 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 tp->cp_cmd |= PCIDAC;
3439 dev->features |= NETIF_F_HIGHDMA;
3440 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003441 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003443 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003444 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 }
3446 }
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003449 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003450 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003451 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003453 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003455 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Jon Masone44daad2011-06-27 07:46:31 +00003457 if (!pci_is_pcie(pdev))
3458 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07003459
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003460 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Francois Romieu6f43adc2011-04-29 15:05:51 +02003462 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003464 RTL_W16(IntrStatus, 0xffff);
3465
françois romieuca52efd2009-07-24 12:34:19 +00003466 pci_set_master(pdev);
3467
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 /* Identify chip attached to board */
Francois Romieu5d320a22011-05-08 17:47:36 +02003469 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Francois Romieu7a8fc772011-03-01 17:18:33 +01003471 /*
3472 * Pretend we are using VLANs; This bypasses a nasty bug where
3473 * Interrupts stop flowing on high load on 8110SCd controllers.
3474 */
3475 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3476 tp->cp_cmd |= RxVlan;
3477
françois romieuc0e45c12011-01-03 15:08:04 +00003478 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003479 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Francois Romieu85bffe62011-04-27 08:22:39 +02003483 chipset = tp->mac_version;
3484 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Francois Romieu5d06a992006-02-23 00:47:58 +01003486 RTL_W8(Cfg9346, Cfg9346_Unlock);
3487 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3488 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003489 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3490 tp->features |= RTL_FEATURE_WOL;
3491 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3492 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003493 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003494 RTL_W8(Cfg9346, Cfg9346_Lock);
3495
Francois Romieu66ec5d42007-11-06 22:56:10 +01003496 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3497 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 tp->set_speed = rtl8169_set_speed_tbi;
3499 tp->get_settings = rtl8169_gset_tbi;
3500 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3501 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3502 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003503 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 } else {
3505 tp->set_speed = rtl8169_set_speed_xmii;
3506 tp->get_settings = rtl8169_gset_xmii;
3507 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3508 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3509 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003510 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 }
3512
Francois Romieudf58ef52008-10-09 14:35:58 -07003513 spin_lock_init(&tp->lock);
3514
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003515 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 for (i = 0; i < MAC_ADDR_LEN; i++)
3517 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003518 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3522 dev->irq = pdev->irq;
3523 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003525 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Michał Mirosław350fb322011-04-08 06:35:56 +00003527 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3528 * properly for all devices */
3529 dev->features |= NETIF_F_RXCSUM |
3530 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3531
3532 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3533 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3534 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3535 NETIF_F_HIGHDMA;
3536
3537 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3538 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3539 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003542 tp->hw_start = cfg->hw_start;
3543 tp->intr_event = cfg->intr_event;
3544 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Francois Romieu2efa53f2007-03-09 00:00:05 +01003546 init_timer(&tp->timer);
3547 tp->timer.data = (unsigned long) dev;
3548 tp->timer.function = rtl8169_phy_timer;
3549
Francois Romieub6ffd972011-06-17 17:00:05 +02003550 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003553 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003554 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 pci_set_drvdata(pdev, dev);
3557
Joe Perchesbf82c182010-02-09 11:49:50 +00003558 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003559 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003560 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Francois Romieucecb5fd2011-04-01 10:21:07 +02003562 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3563 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3564 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003565 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003566 }
françois romieub646d902011-01-03 15:08:21 +00003567
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003568 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Alan Sternf3ec4f82010-06-08 15:23:51 -04003570 if (pci_dev_run_wake(pdev))
3571 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003572
Ivan Vecera0d672e92011-02-15 02:08:39 +00003573 netif_carrier_off(dev);
3574
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003575out:
3576 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
françois romieu87aeec72010-04-26 11:42:06 +00003578err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003579 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003580 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003581err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003582 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003583err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003584 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003585 pci_disable_device(pdev);
3586err_out_free_dev_1:
3587 free_netdev(dev);
3588 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Francois Romieu07d3f512007-02-21 22:40:46 +01003591static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 struct net_device *dev = pci_get_drvdata(pdev);
3594 struct rtl8169_private *tp = netdev_priv(dev);
3595
Francois Romieucecb5fd2011-04-01 10:21:07 +02003596 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3597 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3598 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003599 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003600 }
françois romieub646d902011-01-03 15:08:21 +00003601
Tejun Heo23f333a2010-12-12 16:45:14 +01003602 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003605
François Romieu953a12c2011-04-24 17:38:48 +02003606 rtl_release_firmware(tp);
3607
Alan Sternf3ec4f82010-06-08 15:23:51 -04003608 if (pci_dev_run_wake(pdev))
3609 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003610
Ivan Veceracc098dc2009-11-29 23:12:52 -08003611 /* restore original MAC address */
3612 rtl_rar_set(tp, dev->perm_addr);
3613
Francois Romieufbac58f2007-10-04 22:51:38 +02003614 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3616 pci_set_drvdata(pdev, NULL);
3617}
3618
Francois Romieub6ffd972011-06-17 17:00:05 +02003619static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3620{
3621 struct rtl_fw *rtl_fw;
3622 const char *name;
3623 int rc = -ENOMEM;
3624
3625 name = rtl_lookup_firmware_name(tp);
3626 if (!name)
3627 goto out_no_firmware;
3628
3629 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3630 if (!rtl_fw)
3631 goto err_warn;
3632
3633 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3634 if (rc < 0)
3635 goto err_free;
3636
Francois Romieufd112f22011-06-18 00:10:29 +02003637 rc = rtl_check_firmware(tp, rtl_fw);
3638 if (rc < 0)
3639 goto err_release_firmware;
3640
Francois Romieub6ffd972011-06-17 17:00:05 +02003641 tp->rtl_fw = rtl_fw;
3642out:
3643 return;
3644
Francois Romieufd112f22011-06-18 00:10:29 +02003645err_release_firmware:
3646 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003647err_free:
3648 kfree(rtl_fw);
3649err_warn:
3650 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3651 name, rc);
3652out_no_firmware:
3653 tp->rtl_fw = NULL;
3654 goto out;
3655}
3656
François Romieu953a12c2011-04-24 17:38:48 +02003657static void rtl_request_firmware(struct rtl8169_private *tp)
3658{
Francois Romieub6ffd972011-06-17 17:00:05 +02003659 if (IS_ERR(tp->rtl_fw))
3660 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003661}
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663static int rtl8169_open(struct net_device *dev)
3664{
3665 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003666 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003668 int retval = -ENOMEM;
3669
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003670 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Neil Hormanc0cd8842010-03-29 13:16:02 -07003672 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003674 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003676 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3677 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003679 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003681 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3682 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003684 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
3686 retval = rtl8169_init_ring(dev);
3687 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003688 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
David Howellsc4028952006-11-22 14:57:56 +00003690 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
Francois Romieu99f252b2007-04-02 22:59:59 +02003692 smp_mb();
3693
François Romieu953a12c2011-04-24 17:38:48 +02003694 rtl_request_firmware(tp);
3695
Francois Romieufbac58f2007-10-04 22:51:38 +02003696 retval = request_irq(dev->irq, rtl8169_interrupt,
3697 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003698 dev->name, dev);
3699 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003700 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003701
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003702 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003703
françois romieueee3a962011-01-08 02:17:26 +00003704 rtl8169_init_phy(dev, tp);
3705
Michał Mirosław350fb322011-04-08 06:35:56 +00003706 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003707
françois romieu065c27c2011-01-03 15:08:12 +00003708 rtl_pll_power_up(tp);
3709
Francois Romieu07ce4062007-02-23 23:36:39 +01003710 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003712 tp->saved_wolopts = 0;
3713 pm_runtime_put_noidle(&pdev->dev);
3714
françois romieueee3a962011-01-08 02:17:26 +00003715 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716out:
3717 return retval;
3718
François Romieu953a12c2011-04-24 17:38:48 +02003719err_release_fw_2:
3720 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003721 rtl8169_rx_clear(tp);
3722err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003723 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3724 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003725 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003726err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003727 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3728 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003729 tp->TxDescArray = NULL;
3730err_pm_runtime_put:
3731 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 goto out;
3733}
3734
françois romieue6de30d2011-01-03 15:08:37 +00003735static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
françois romieue6de30d2011-01-03 15:08:37 +00003737 void __iomem *ioaddr = tp->mmio_addr;
3738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 /* Disable interrupts */
3740 rtl8169_irq_mask_and_ack(ioaddr);
3741
Hayes Wang5d2e1952011-02-22 17:26:22 +08003742 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003743 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3744 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003745 while (RTL_R8(TxPoll) & NPQ)
3746 udelay(20);
3747
3748 }
3749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 /* Reset the chipset */
3751 RTL_W8(ChipCmd, CmdReset);
3752
3753 /* PCI commit */
3754 RTL_R8(ChipCmd);
3755}
3756
Francois Romieu7f796d82007-06-11 23:04:41 +02003757static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003758{
3759 void __iomem *ioaddr = tp->mmio_addr;
3760 u32 cfg = rtl8169_rx_config;
3761
Francois Romieu2b7b4312011-04-18 22:53:24 -07003762 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003763 RTL_W32(RxConfig, cfg);
3764
3765 /* Set DMA burst size and Interframe Gap Time */
3766 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3767 (InterFrameGap << TxInterFrameGapShift));
3768}
3769
Francois Romieu07ce4062007-02-23 23:36:39 +01003770static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
3772 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Francois Romieu6f43adc2011-04-29 15:05:51 +02003774 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Francois Romieu07ce4062007-02-23 23:36:39 +01003776 tp->hw_start(dev);
3777
Francois Romieu07ce4062007-02-23 23:36:39 +01003778 netif_start_queue(dev);
3779}
3780
Francois Romieu7f796d82007-06-11 23:04:41 +02003781static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3782 void __iomem *ioaddr)
3783{
3784 /*
3785 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3786 * register to be written before TxDescAddrLow to work.
3787 * Switching from MMIO to I/O access fixes the issue as well.
3788 */
3789 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003790 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003791 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003792 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003793}
3794
3795static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3796{
3797 u16 cmd;
3798
3799 cmd = RTL_R16(CPlusCmd);
3800 RTL_W16(CPlusCmd, cmd);
3801 return cmd;
3802}
3803
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003804static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003805{
3806 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003807 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003808}
3809
Francois Romieu6dccd162007-02-13 23:38:05 +01003810static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3811{
Francois Romieu37441002011-06-17 22:58:54 +02003812 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01003813 u32 mac_version;
3814 u32 clk;
3815 u32 val;
3816 } cfg2_info [] = {
3817 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3818 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3819 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3820 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02003821 };
3822 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01003823 unsigned int i;
3824 u32 clk;
3825
3826 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003827 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003828 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3829 RTL_W32(0x7c, p->val);
3830 break;
3831 }
3832 }
3833}
3834
Francois Romieu07ce4062007-02-23 23:36:39 +01003835static void rtl_hw_start_8169(struct net_device *dev)
3836{
3837 struct rtl8169_private *tp = netdev_priv(dev);
3838 void __iomem *ioaddr = tp->mmio_addr;
3839 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003840
Francois Romieu9cb427b2006-11-02 00:10:16 +01003841 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3842 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3843 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3844 }
3845
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003847 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3848 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3849 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3850 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003851 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3852
françois romieuf0298f82011-01-03 15:07:42 +00003853 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003855 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Francois Romieucecb5fd2011-04-01 10:21:07 +02003857 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3858 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3859 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3860 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02003861 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
Francois Romieu7f796d82007-06-11 23:04:41 +02003863 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003864
Francois Romieucecb5fd2011-04-01 10:21:07 +02003865 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3866 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02003867 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003869 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
3871
Francois Romieubcf0bf92006-07-26 23:14:13 +02003872 RTL_W16(CPlusCmd, tp->cp_cmd);
3873
Francois Romieu6dccd162007-02-13 23:38:05 +01003874 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 /*
3877 * Undocumented corner. Supposedly:
3878 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3879 */
3880 RTL_W16(IntrMitigate, 0x0000);
3881
Francois Romieu7f796d82007-06-11 23:04:41 +02003882 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003883
Francois Romieucecb5fd2011-04-01 10:21:07 +02003884 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3885 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3886 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3887 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02003888 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3889 rtl_set_rx_tx_config_registers(tp);
3890 }
3891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003893
3894 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3895 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
3897 RTL_W32(RxMissed, 0);
3898
Francois Romieu07ce4062007-02-23 23:36:39 +01003899 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
3901 /* no early-rx interrupts */
3902 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003903
3904 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003905 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003906}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
Francois Romieu9c14cea2008-07-05 00:21:15 +02003908static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003909{
Jon Masone44daad2011-06-27 07:46:31 +00003910 int cap = pci_pcie_cap(pdev);
Francois Romieu458a9f62008-08-02 15:50:02 +02003911
Francois Romieu9c14cea2008-07-05 00:21:15 +02003912 if (cap) {
3913 u16 ctl;
3914
3915 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3916 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3917 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3918 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003919}
3920
françois romieu650e8d52011-01-03 15:08:29 +00003921static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003922{
3923 u32 csi;
3924
3925 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003926 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3927}
3928
françois romieue6de30d2011-01-03 15:08:37 +00003929static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3930{
3931 rtl_csi_access_enable(ioaddr, 0x17000000);
3932}
3933
françois romieu650e8d52011-01-03 15:08:29 +00003934static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3935{
3936 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003937}
3938
3939struct ephy_info {
3940 unsigned int offset;
3941 u16 mask;
3942 u16 bits;
3943};
3944
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003945static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003946{
3947 u16 w;
3948
3949 while (len-- > 0) {
3950 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3951 rtl_ephy_write(ioaddr, e->offset, w);
3952 e++;
3953 }
3954}
3955
Francois Romieub726e492008-06-28 12:22:59 +02003956static void rtl_disable_clock_request(struct pci_dev *pdev)
3957{
Jon Masone44daad2011-06-27 07:46:31 +00003958 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003959
3960 if (cap) {
3961 u16 ctl;
3962
3963 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3964 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3965 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3966 }
3967}
3968
françois romieue6de30d2011-01-03 15:08:37 +00003969static void rtl_enable_clock_request(struct pci_dev *pdev)
3970{
Jon Masone44daad2011-06-27 07:46:31 +00003971 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00003972
3973 if (cap) {
3974 u16 ctl;
3975
3976 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3977 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3978 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3979 }
3980}
3981
Francois Romieub726e492008-06-28 12:22:59 +02003982#define R8168_CPCMD_QUIRK_MASK (\
3983 EnableBist | \
3984 Mac_dbgo_oe | \
3985 Force_half_dup | \
3986 Force_rxflow_en | \
3987 Force_txflow_en | \
3988 Cxpl_dbg_sel | \
3989 ASF | \
3990 PktCntrDisable | \
3991 Mac_dbgo_sel)
3992
Francois Romieu219a1e92008-06-28 11:58:39 +02003993static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3994{
Francois Romieub726e492008-06-28 12:22:59 +02003995 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3996
3997 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3998
Francois Romieu2e68ae42008-06-28 12:00:55 +02003999 rtl_tx_performance_tweak(pdev,
4000 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02004001}
4002
4003static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4004{
4005 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004006
françois romieuf0298f82011-01-03 15:07:42 +00004007 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004008
4009 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004010}
4011
4012static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4013{
Francois Romieub726e492008-06-28 12:22:59 +02004014 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4015
4016 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4017
Francois Romieu219a1e92008-06-28 11:58:39 +02004018 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004019
4020 rtl_disable_clock_request(pdev);
4021
4022 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004023}
4024
Francois Romieuef3386f2008-06-29 12:24:30 +02004025static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004026{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004027 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004028 { 0x01, 0, 0x0001 },
4029 { 0x02, 0x0800, 0x1000 },
4030 { 0x03, 0, 0x0042 },
4031 { 0x06, 0x0080, 0x0000 },
4032 { 0x07, 0, 0x2000 }
4033 };
4034
françois romieu650e8d52011-01-03 15:08:29 +00004035 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004036
4037 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4038
Francois Romieu219a1e92008-06-28 11:58:39 +02004039 __rtl_hw_start_8168cp(ioaddr, pdev);
4040}
4041
Francois Romieuef3386f2008-06-29 12:24:30 +02004042static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4043{
françois romieu650e8d52011-01-03 15:08:29 +00004044 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004045
4046 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4047
4048 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4049
4050 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4051}
4052
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004053static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4054{
françois romieu650e8d52011-01-03 15:08:29 +00004055 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004056
4057 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4058
4059 /* Magic. */
4060 RTL_W8(DBG_REG, 0x20);
4061
françois romieuf0298f82011-01-03 15:07:42 +00004062 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004063
4064 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4065
4066 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4067}
4068
Francois Romieu219a1e92008-06-28 11:58:39 +02004069static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4070{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004071 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004072 { 0x02, 0x0800, 0x1000 },
4073 { 0x03, 0, 0x0002 },
4074 { 0x06, 0x0080, 0x0000 }
4075 };
4076
françois romieu650e8d52011-01-03 15:08:29 +00004077 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004078
4079 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4080
4081 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4082
Francois Romieu219a1e92008-06-28 11:58:39 +02004083 __rtl_hw_start_8168cp(ioaddr, pdev);
4084}
4085
4086static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4087{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004088 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004089 { 0x01, 0, 0x0001 },
4090 { 0x03, 0x0400, 0x0220 }
4091 };
4092
françois romieu650e8d52011-01-03 15:08:29 +00004093 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004094
4095 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4096
Francois Romieu219a1e92008-06-28 11:58:39 +02004097 __rtl_hw_start_8168cp(ioaddr, pdev);
4098}
4099
Francois Romieu197ff762008-06-28 13:16:02 +02004100static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4101{
4102 rtl_hw_start_8168c_2(ioaddr, pdev);
4103}
4104
Francois Romieu6fb07052008-06-29 11:54:28 +02004105static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4106{
françois romieu650e8d52011-01-03 15:08:29 +00004107 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004108
4109 __rtl_hw_start_8168cp(ioaddr, pdev);
4110}
4111
Francois Romieu5b538df2008-07-20 16:22:45 +02004112static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4113{
françois romieu650e8d52011-01-03 15:08:29 +00004114 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004115
4116 rtl_disable_clock_request(pdev);
4117
françois romieuf0298f82011-01-03 15:07:42 +00004118 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004119
4120 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4121
4122 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4123}
4124
hayeswang4804b3b2011-03-21 01:50:29 +00004125static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4126{
4127 rtl_csi_access_enable_1(ioaddr);
4128
4129 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4130
4131 RTL_W8(MaxTxPacketSize, TxPacketMax);
4132
4133 rtl_disable_clock_request(pdev);
4134}
4135
françois romieue6de30d2011-01-03 15:08:37 +00004136static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4137{
4138 static const struct ephy_info e_info_8168d_4[] = {
4139 { 0x0b, ~0, 0x48 },
4140 { 0x19, 0x20, 0x50 },
4141 { 0x0c, ~0, 0x20 }
4142 };
4143 int i;
4144
4145 rtl_csi_access_enable_1(ioaddr);
4146
4147 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4148
4149 RTL_W8(MaxTxPacketSize, TxPacketMax);
4150
4151 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4152 const struct ephy_info *e = e_info_8168d_4 + i;
4153 u16 w;
4154
4155 w = rtl_ephy_read(ioaddr, e->offset);
4156 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4157 }
4158
4159 rtl_enable_clock_request(pdev);
4160}
4161
hayeswang01dc7fe2011-03-21 01:50:28 +00004162static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4163{
4164 static const struct ephy_info e_info_8168e[] = {
4165 { 0x00, 0x0200, 0x0100 },
4166 { 0x00, 0x0000, 0x0004 },
4167 { 0x06, 0x0002, 0x0001 },
4168 { 0x06, 0x0000, 0x0030 },
4169 { 0x07, 0x0000, 0x2000 },
4170 { 0x00, 0x0000, 0x0020 },
4171 { 0x03, 0x5800, 0x2000 },
4172 { 0x03, 0x0000, 0x0001 },
4173 { 0x01, 0x0800, 0x1000 },
4174 { 0x07, 0x0000, 0x4000 },
4175 { 0x1e, 0x0000, 0x2000 },
4176 { 0x19, 0xffff, 0xfe6c },
4177 { 0x0a, 0x0000, 0x0040 }
4178 };
4179
4180 rtl_csi_access_enable_2(ioaddr);
4181
4182 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4183
4184 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4185
4186 RTL_W8(MaxTxPacketSize, TxPacketMax);
4187
4188 rtl_disable_clock_request(pdev);
4189
4190 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004191 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4192 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004193
Francois Romieucecb5fd2011-04-01 10:21:07 +02004194 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004195}
4196
Francois Romieu07ce4062007-02-23 23:36:39 +01004197static void rtl_hw_start_8168(struct net_device *dev)
4198{
Francois Romieu2dd99532007-06-11 23:22:52 +02004199 struct rtl8169_private *tp = netdev_priv(dev);
4200 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004201 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004202
4203 RTL_W8(Cfg9346, Cfg9346_Unlock);
4204
françois romieuf0298f82011-01-03 15:07:42 +00004205 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004206
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004207 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004208
Francois Romieu0e485152007-02-20 00:00:26 +01004209 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004210
4211 RTL_W16(CPlusCmd, tp->cp_cmd);
4212
Francois Romieu0e485152007-02-20 00:00:26 +01004213 RTL_W16(IntrMitigate, 0x5151);
4214
4215 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004216 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4217 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004218 tp->intr_event |= RxFIFOOver | PCSTimeout;
4219 tp->intr_event &= ~RxOverflow;
4220 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004221
4222 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4223
Francois Romieub8363902008-06-01 12:31:57 +02004224 rtl_set_rx_mode(dev);
4225
4226 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4227 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004228
4229 RTL_R8(IntrMask);
4230
Francois Romieu219a1e92008-06-28 11:58:39 +02004231 switch (tp->mac_version) {
4232 case RTL_GIGA_MAC_VER_11:
4233 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004234 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004235
4236 case RTL_GIGA_MAC_VER_12:
4237 case RTL_GIGA_MAC_VER_17:
4238 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004239 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004240
4241 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004242 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004243 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004244
4245 case RTL_GIGA_MAC_VER_19:
4246 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004247 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004248
4249 case RTL_GIGA_MAC_VER_20:
4250 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004251 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004252
Francois Romieu197ff762008-06-28 13:16:02 +02004253 case RTL_GIGA_MAC_VER_21:
4254 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004255 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004256
Francois Romieu6fb07052008-06-29 11:54:28 +02004257 case RTL_GIGA_MAC_VER_22:
4258 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004259 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004260
Francois Romieuef3386f2008-06-29 12:24:30 +02004261 case RTL_GIGA_MAC_VER_23:
4262 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004263 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004264
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004265 case RTL_GIGA_MAC_VER_24:
4266 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004267 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004268
Francois Romieu5b538df2008-07-20 16:22:45 +02004269 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004270 case RTL_GIGA_MAC_VER_26:
4271 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004272 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004273 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004274
françois romieue6de30d2011-01-03 15:08:37 +00004275 case RTL_GIGA_MAC_VER_28:
4276 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004277 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004278
hayeswang4804b3b2011-03-21 01:50:29 +00004279 case RTL_GIGA_MAC_VER_31:
4280 rtl_hw_start_8168dp(ioaddr, pdev);
4281 break;
4282
hayeswang01dc7fe2011-03-21 01:50:28 +00004283 case RTL_GIGA_MAC_VER_32:
4284 case RTL_GIGA_MAC_VER_33:
4285 rtl_hw_start_8168e(ioaddr, pdev);
4286 break;
françois romieue6de30d2011-01-03 15:08:37 +00004287
Francois Romieu219a1e92008-06-28 11:58:39 +02004288 default:
4289 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4290 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004291 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004292 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004293
Francois Romieu0e485152007-02-20 00:00:26 +01004294 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4295
Francois Romieub8363902008-06-01 12:31:57 +02004296 RTL_W8(Cfg9346, Cfg9346_Lock);
4297
Francois Romieu2dd99532007-06-11 23:22:52 +02004298 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004299
Francois Romieu0e485152007-02-20 00:00:26 +01004300 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004301}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Francois Romieu2857ffb2008-08-02 21:08:49 +02004303#define R810X_CPCMD_QUIRK_MASK (\
4304 EnableBist | \
4305 Mac_dbgo_oe | \
4306 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004307 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004308 Force_txflow_en | \
4309 Cxpl_dbg_sel | \
4310 ASF | \
4311 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004312 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004313
4314static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4315{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004316 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004317 { 0x01, 0, 0x6e65 },
4318 { 0x02, 0, 0x091f },
4319 { 0x03, 0, 0xc2f9 },
4320 { 0x06, 0, 0xafb5 },
4321 { 0x07, 0, 0x0e00 },
4322 { 0x19, 0, 0xec80 },
4323 { 0x01, 0, 0x2e65 },
4324 { 0x01, 0, 0x6e65 }
4325 };
4326 u8 cfg1;
4327
françois romieu650e8d52011-01-03 15:08:29 +00004328 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004329
4330 RTL_W8(DBG_REG, FIX_NAK_1);
4331
4332 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4333
4334 RTL_W8(Config1,
4335 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4336 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4337
4338 cfg1 = RTL_R8(Config1);
4339 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4340 RTL_W8(Config1, cfg1 & ~LEDS0);
4341
Francois Romieu2857ffb2008-08-02 21:08:49 +02004342 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4343}
4344
4345static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4346{
françois romieu650e8d52011-01-03 15:08:29 +00004347 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004348
4349 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4350
4351 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4352 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004353}
4354
4355static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4356{
4357 rtl_hw_start_8102e_2(ioaddr, pdev);
4358
4359 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4360}
4361
Hayes Wang5a5e4442011-02-22 17:26:21 +08004362static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4363{
4364 static const struct ephy_info e_info_8105e_1[] = {
4365 { 0x07, 0, 0x4000 },
4366 { 0x19, 0, 0x0200 },
4367 { 0x19, 0, 0x0020 },
4368 { 0x1e, 0, 0x2000 },
4369 { 0x03, 0, 0x0001 },
4370 { 0x19, 0, 0x0100 },
4371 { 0x19, 0, 0x0004 },
4372 { 0x0a, 0, 0x0020 }
4373 };
4374
Francois Romieucecb5fd2011-04-01 10:21:07 +02004375 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004376 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4377
Francois Romieucecb5fd2011-04-01 10:21:07 +02004378 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004379 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4380
4381 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
Hayes Wang4f6b00e2011-07-06 15:58:02 +08004382 RTL_W8(DLLPR, RTL_R8(DLLPR) | PFM_EN);
Hayes Wang5a5e4442011-02-22 17:26:21 +08004383
4384 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4385}
4386
4387static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4388{
4389 rtl_hw_start_8105e_1(ioaddr, pdev);
4390 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4391}
4392
Francois Romieu07ce4062007-02-23 23:36:39 +01004393static void rtl_hw_start_8101(struct net_device *dev)
4394{
Francois Romieucdf1a602007-06-11 23:29:50 +02004395 struct rtl8169_private *tp = netdev_priv(dev);
4396 void __iomem *ioaddr = tp->mmio_addr;
4397 struct pci_dev *pdev = tp->pci_dev;
4398
Francois Romieucecb5fd2011-04-01 10:21:07 +02004399 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4400 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004401 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004402
4403 if (cap) {
4404 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4405 PCI_EXP_DEVCTL_NOSNOOP_EN);
4406 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004407 }
4408
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004409 RTL_W8(Cfg9346, Cfg9346_Unlock);
4410
Francois Romieu2857ffb2008-08-02 21:08:49 +02004411 switch (tp->mac_version) {
4412 case RTL_GIGA_MAC_VER_07:
4413 rtl_hw_start_8102e_1(ioaddr, pdev);
4414 break;
4415
4416 case RTL_GIGA_MAC_VER_08:
4417 rtl_hw_start_8102e_3(ioaddr, pdev);
4418 break;
4419
4420 case RTL_GIGA_MAC_VER_09:
4421 rtl_hw_start_8102e_2(ioaddr, pdev);
4422 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004423
4424 case RTL_GIGA_MAC_VER_29:
4425 rtl_hw_start_8105e_1(ioaddr, pdev);
4426 break;
4427 case RTL_GIGA_MAC_VER_30:
4428 rtl_hw_start_8105e_2(ioaddr, pdev);
4429 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004430 }
4431
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004432 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004433
françois romieuf0298f82011-01-03 15:07:42 +00004434 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004435
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004436 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004437
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004438 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004439 RTL_W16(CPlusCmd, tp->cp_cmd);
4440
4441 RTL_W16(IntrMitigate, 0x0000);
4442
4443 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4444
4445 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4446 rtl_set_rx_tx_config_registers(tp);
4447
Francois Romieucdf1a602007-06-11 23:29:50 +02004448 RTL_R8(IntrMask);
4449
Francois Romieucdf1a602007-06-11 23:29:50 +02004450 rtl_set_rx_mode(dev);
4451
4452 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004453
Francois Romieu0e485152007-02-20 00:00:26 +01004454 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455}
4456
4457static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4458{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4460 return -EINVAL;
4461
4462 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004463 netdev_update_features(dev);
4464
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
4467
4468static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4469{
Al Viro95e09182007-12-22 18:55:39 +00004470 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4472}
4473
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004474static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4475 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004477 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004478 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004479
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004480 kfree(*data_buff);
4481 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 rtl8169_make_unusable_by_asic(desc);
4483}
4484
4485static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4486{
4487 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4488
4489 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4490}
4491
4492static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4493 u32 rx_buf_sz)
4494{
4495 desc->addr = cpu_to_le64(mapping);
4496 wmb();
4497 rtl8169_mark_to_asic(desc, rx_buf_sz);
4498}
4499
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004500static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004502 return (void *)ALIGN((long)data, 16);
4503}
4504
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004505static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4506 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004507{
4508 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004510 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004511 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004512 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004514 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4515 if (!data)
4516 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004517
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004518 if (rtl8169_align(data) != data) {
4519 kfree(data);
4520 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4521 if (!data)
4522 return NULL;
4523 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004524
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004525 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004526 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004527 if (unlikely(dma_mapping_error(d, mapping))) {
4528 if (net_ratelimit())
4529 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004530 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
4533 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004534 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004535
4536err_out:
4537 kfree(data);
4538 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539}
4540
4541static void rtl8169_rx_clear(struct rtl8169_private *tp)
4542{
Francois Romieu07d3f512007-02-21 22:40:46 +01004543 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
4545 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004546 if (tp->Rx_databuff[i]) {
4547 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 tp->RxDescArray + i);
4549 }
4550 }
4551}
4552
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004553static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004555 desc->opts1 |= cpu_to_le32(RingEnd);
4556}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004557
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004558static int rtl8169_rx_fill(struct rtl8169_private *tp)
4559{
4560 unsigned int i;
4561
4562 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004563 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004564
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004565 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004567
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004568 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004569 if (!data) {
4570 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004571 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004572 }
4573 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004576 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4577 return 0;
4578
4579err_out:
4580 rtl8169_rx_clear(tp);
4581 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582}
4583
4584static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4585{
4586 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4587}
4588
4589static int rtl8169_init_ring(struct net_device *dev)
4590{
4591 struct rtl8169_private *tp = netdev_priv(dev);
4592
4593 rtl8169_init_ring_indexes(tp);
4594
4595 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004596 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004598 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599}
4600
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004601static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 struct TxDesc *desc)
4603{
4604 unsigned int len = tx_skb->len;
4605
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004606 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 desc->opts1 = 0x00;
4609 desc->opts2 = 0x00;
4610 desc->addr = 0x00;
4611 tx_skb->len = 0;
4612}
4613
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004614static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4615 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616{
4617 unsigned int i;
4618
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004619 for (i = 0; i < n; i++) {
4620 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 struct ring_info *tx_skb = tp->tx_skb + entry;
4622 unsigned int len = tx_skb->len;
4623
4624 if (len) {
4625 struct sk_buff *skb = tx_skb->skb;
4626
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004627 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 tp->TxDescArray + entry);
4629 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004630 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 dev_kfree_skb(skb);
4632 tx_skb->skb = NULL;
4633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 }
4635 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004636}
4637
4638static void rtl8169_tx_clear(struct rtl8169_private *tp)
4639{
4640 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 tp->cur_tx = tp->dirty_tx = 0;
4642}
4643
David Howellsc4028952006-11-22 14:57:56 +00004644static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
4646 struct rtl8169_private *tp = netdev_priv(dev);
4647
David Howellsc4028952006-11-22 14:57:56 +00004648 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 schedule_delayed_work(&tp->task, 4);
4650}
4651
4652static void rtl8169_wait_for_quiescence(struct net_device *dev)
4653{
4654 struct rtl8169_private *tp = netdev_priv(dev);
4655 void __iomem *ioaddr = tp->mmio_addr;
4656
4657 synchronize_irq(dev->irq);
4658
4659 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004660 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
4662 rtl8169_irq_mask_and_ack(ioaddr);
4663
David S. Millerd1d08d12008-01-07 20:53:33 -08004664 tp->intr_mask = 0xffff;
4665 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004666 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667}
4668
David Howellsc4028952006-11-22 14:57:56 +00004669static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
David Howellsc4028952006-11-22 14:57:56 +00004671 struct rtl8169_private *tp =
4672 container_of(work, struct rtl8169_private, task.work);
4673 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 int ret;
4675
Francois Romieueb2a0212007-02-15 23:37:21 +01004676 rtnl_lock();
4677
4678 if (!netif_running(dev))
4679 goto out_unlock;
4680
4681 rtl8169_wait_for_quiescence(dev);
4682 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
4684 ret = rtl8169_open(dev);
4685 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004686 if (net_ratelimit())
4687 netif_err(tp, drv, dev,
4688 "reinit failure (status = %d). Rescheduling\n",
4689 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4691 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004692
4693out_unlock:
4694 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
David Howellsc4028952006-11-22 14:57:56 +00004697static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698{
David Howellsc4028952006-11-22 14:57:56 +00004699 struct rtl8169_private *tp =
4700 container_of(work, struct rtl8169_private, task.work);
4701 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004702 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Francois Romieueb2a0212007-02-15 23:37:21 +01004704 rtnl_lock();
4705
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004707 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 rtl8169_wait_for_quiescence(dev);
4710
Francois Romieu56de4142011-03-15 17:29:31 +01004711 for (i = 0; i < NUM_RX_DESC; i++)
4712 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4713
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 rtl8169_tx_clear(tp);
4715
Francois Romieu56de4142011-03-15 17:29:31 +01004716 rtl8169_init_ring_indexes(tp);
4717 rtl_hw_start(dev);
4718 netif_wake_queue(dev);
4719 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004720
4721out_unlock:
4722 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723}
4724
4725static void rtl8169_tx_timeout(struct net_device *dev)
4726{
4727 struct rtl8169_private *tp = netdev_priv(dev);
4728
françois romieue6de30d2011-01-03 15:08:37 +00004729 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
4731 /* Let's wait a bit while any (async) irq lands on */
4732 rtl8169_schedule_work(dev, rtl8169_reset_task);
4733}
4734
4735static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004736 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
4738 struct skb_shared_info *info = skb_shinfo(skb);
4739 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004740 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004741 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
4743 entry = tp->cur_tx;
4744 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4745 skb_frag_t *frag = info->frags + cur_frag;
4746 dma_addr_t mapping;
4747 u32 status, len;
4748 void *addr;
4749
4750 entry = (entry + 1) % NUM_TX_DESC;
4751
4752 txd = tp->TxDescArray + entry;
4753 len = frag->size;
4754 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004755 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004756 if (unlikely(dma_mapping_error(d, mapping))) {
4757 if (net_ratelimit())
4758 netif_err(tp, drv, tp->dev,
4759 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004760 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Francois Romieucecb5fd2011-04-01 10:21:07 +02004763 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004764 status = opts[0] | len |
4765 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
4767 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004768 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 txd->addr = cpu_to_le64(mapping);
4770
4771 tp->tx_skb[entry].len = len;
4772 }
4773
4774 if (cur_frag) {
4775 tp->tx_skb[entry].skb = skb;
4776 txd->opts1 |= cpu_to_le32(LastFrag);
4777 }
4778
4779 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004780
4781err_out:
4782 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4783 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784}
4785
Francois Romieu2b7b4312011-04-18 22:53:24 -07004786static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4787 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004789 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004790 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004791 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Francois Romieu2b7b4312011-04-18 22:53:24 -07004793 if (mss) {
4794 opts[0] |= TD_LSO;
4795 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4796 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004797 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
4799 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004800 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004802 opts[offset] |= info->checksum.udp;
4803 else
4804 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806}
4807
Stephen Hemminger613573252009-08-31 19:50:58 +00004808static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4809 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810{
4811 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004812 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 struct TxDesc *txd = tp->TxDescArray + entry;
4814 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004815 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 dma_addr_t mapping;
4817 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004818 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004819 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004822 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004823 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 }
4825
4826 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004827 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004829 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004830 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004831 if (unlikely(dma_mapping_error(d, mapping))) {
4832 if (net_ratelimit())
4833 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004834 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 tp->tx_skb[entry].len = len;
4838 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
Francois Romieu2b7b4312011-04-18 22:53:24 -07004840 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4841 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004842
Francois Romieu2b7b4312011-04-18 22:53:24 -07004843 rtl8169_tso_csum(tp, skb, opts);
4844
4845 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004846 if (frags < 0)
4847 goto err_dma_1;
4848 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004849 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004850 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004851 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004852 tp->tx_skb[entry].skb = skb;
4853 }
4854
Francois Romieu2b7b4312011-04-18 22:53:24 -07004855 txd->opts2 = cpu_to_le32(opts[1]);
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 wmb();
4858
Francois Romieucecb5fd2011-04-01 10:21:07 +02004859 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004860 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 txd->opts1 = cpu_to_le32(status);
4862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 tp->cur_tx += frags + 1;
4864
David Dillow4c020a92010-03-03 16:33:10 +00004865 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Francois Romieucecb5fd2011-04-01 10:21:07 +02004867 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
4869 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4870 netif_stop_queue(dev);
4871 smp_rmb();
4872 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4873 netif_wake_queue(dev);
4874 }
4875
Stephen Hemminger613573252009-08-31 19:50:58 +00004876 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004878err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004879 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004880err_dma_0:
4881 dev_kfree_skb(skb);
4882 dev->stats.tx_dropped++;
4883 return NETDEV_TX_OK;
4884
4885err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004887 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004888 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889}
4890
4891static void rtl8169_pcierr_interrupt(struct net_device *dev)
4892{
4893 struct rtl8169_private *tp = netdev_priv(dev);
4894 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 u16 pci_status, pci_cmd;
4896
4897 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4898 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4899
Joe Perchesbf82c182010-02-09 11:49:50 +00004900 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4901 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
4903 /*
4904 * The recovery sequence below admits a very elaborated explanation:
4905 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004906 * - I did not see what else could be done;
4907 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 *
4909 * Feel free to adjust to your needs.
4910 */
Francois Romieua27993f2006-12-18 00:04:19 +01004911 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004912 pci_cmd &= ~PCI_COMMAND_PARITY;
4913 else
4914 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4915
4916 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
4918 pci_write_config_word(pdev, PCI_STATUS,
4919 pci_status & (PCI_STATUS_DETECTED_PARITY |
4920 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4921 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4922
4923 /* The infamous DAC f*ckup only happens at boot time */
4924 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004925 void __iomem *ioaddr = tp->mmio_addr;
4926
Joe Perchesbf82c182010-02-09 11:49:50 +00004927 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 tp->cp_cmd &= ~PCIDAC;
4929 RTL_W16(CPlusCmd, tp->cp_cmd);
4930 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 }
4932
françois romieue6de30d2011-01-03 15:08:37 +00004933 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004934
4935 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936}
4937
Francois Romieu07d3f512007-02-21 22:40:46 +01004938static void rtl8169_tx_interrupt(struct net_device *dev,
4939 struct rtl8169_private *tp,
4940 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941{
4942 unsigned int dirty_tx, tx_left;
4943
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 dirty_tx = tp->dirty_tx;
4945 smp_rmb();
4946 tx_left = tp->cur_tx - dirty_tx;
4947
4948 while (tx_left > 0) {
4949 unsigned int entry = dirty_tx % NUM_TX_DESC;
4950 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 u32 status;
4952
4953 rmb();
4954 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4955 if (status & DescOwn)
4956 break;
4957
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004958 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4959 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004961 dev->stats.tx_packets++;
4962 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004963 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 tx_skb->skb = NULL;
4965 }
4966 dirty_tx++;
4967 tx_left--;
4968 }
4969
4970 if (tp->dirty_tx != dirty_tx) {
4971 tp->dirty_tx = dirty_tx;
4972 smp_wmb();
4973 if (netif_queue_stopped(dev) &&
4974 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4975 netif_wake_queue(dev);
4976 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004977 /*
4978 * 8168 hack: TxPoll requests are lost when the Tx packets are
4979 * too close. Let's kick an extra TxPoll request when a burst
4980 * of start_xmit activity is detected (if it is not detected,
4981 * it is slow enough). -- FR
4982 */
4983 smp_rmb();
4984 if (tp->cur_tx != dirty_tx)
4985 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 }
4987}
4988
Francois Romieu126fa4b2005-05-12 20:09:17 -04004989static inline int rtl8169_fragmented_frame(u32 status)
4990{
4991 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4992}
4993
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004994static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 u32 status = opts1 & RxProtoMask;
4997
4998 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004999 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 skb->ip_summed = CHECKSUM_UNNECESSARY;
5001 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005002 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003}
5004
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005005static struct sk_buff *rtl8169_try_rx_copy(void *data,
5006 struct rtl8169_private *tp,
5007 int pkt_size,
5008 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005010 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005011 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005013 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005014 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005015 prefetch(data);
5016 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5017 if (skb)
5018 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005019 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5020
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005021 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022}
5023
Francois Romieu07d3f512007-02-21 22:40:46 +01005024static int rtl8169_rx_interrupt(struct net_device *dev,
5025 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005026 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027{
5028 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005029 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 cur_rx = tp->cur_rx;
5032 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005033 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005035 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005037 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 u32 status;
5039
5040 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04005041 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
5043 if (status & DescOwn)
5044 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005045 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005046 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5047 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005048 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005050 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005052 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005053 if (status & RxFOVF) {
5054 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005055 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005056 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005057 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005059 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005060 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
Francois Romieu126fa4b2005-05-12 20:09:17 -04005063 /*
5064 * The driver does not support incoming fragmented
5065 * frames. They are seen as a symptom of over-mtu
5066 * sized frames.
5067 */
5068 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005069 dev->stats.rx_dropped++;
5070 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005071 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005072 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005073 }
5074
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005075 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5076 tp, pkt_size, addr);
5077 rtl8169_mark_to_asic(desc, rx_buf_sz);
5078 if (!skb) {
5079 dev->stats.rx_dropped++;
5080 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 }
5082
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005083 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 skb_put(skb, pkt_size);
5085 skb->protocol = eth_type_trans(skb, dev);
5086
Francois Romieu7a8fc772011-03-01 17:18:33 +01005087 rtl8169_rx_vlan_tag(desc, skb);
5088
Francois Romieu56de4142011-03-15 17:29:31 +01005089 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Francois Romieucebf8cc2007-10-18 12:06:54 +02005091 dev->stats.rx_bytes += pkt_size;
5092 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005094
5095 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005096 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005097 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5098 desc->opts2 = 0;
5099 cur_rx++;
5100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 }
5102
5103 count = cur_rx - tp->cur_rx;
5104 tp->cur_rx = cur_rx;
5105
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005106 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
5108 return count;
5109}
5110
Francois Romieu07d3f512007-02-21 22:40:46 +01005111static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112{
Francois Romieu07d3f512007-02-21 22:40:46 +01005113 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005117 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
David Dillowf11a3772009-05-22 15:29:34 +00005119 /* loop handling interrupts until we have no new ones or
5120 * we hit a invalid/hotplug case.
5121 */
Francois Romieu865c6522008-05-11 14:51:00 +02005122 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005123 while (status && status != 0xffff) {
5124 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
David Dillowf11a3772009-05-22 15:29:34 +00005126 /* Handle all of the error cases first. These will reset
5127 * the chip, so just exit the loop.
5128 */
5129 if (unlikely(!netif_running(dev))) {
5130 rtl8169_asic_down(ioaddr);
5131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 }
David Dillowf11a3772009-05-22 15:29:34 +00005133
Francois Romieu1519e572011-02-03 12:02:36 +01005134 if (unlikely(status & RxFIFOOver)) {
5135 switch (tp->mac_version) {
5136 /* Work around for rx fifo overflow */
5137 case RTL_GIGA_MAC_VER_11:
5138 case RTL_GIGA_MAC_VER_22:
5139 case RTL_GIGA_MAC_VER_26:
5140 netif_stop_queue(dev);
5141 rtl8169_tx_timeout(dev);
5142 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005143 /* Testers needed. */
5144 case RTL_GIGA_MAC_VER_17:
5145 case RTL_GIGA_MAC_VER_19:
5146 case RTL_GIGA_MAC_VER_20:
5147 case RTL_GIGA_MAC_VER_21:
5148 case RTL_GIGA_MAC_VER_23:
5149 case RTL_GIGA_MAC_VER_24:
5150 case RTL_GIGA_MAC_VER_27:
5151 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005152 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005153 /* Experimental science. Pktgen proof. */
5154 case RTL_GIGA_MAC_VER_12:
5155 case RTL_GIGA_MAC_VER_25:
5156 if (status == RxFIFOOver)
5157 goto done;
5158 break;
5159 default:
5160 break;
5161 }
David Dillowf11a3772009-05-22 15:29:34 +00005162 }
5163
5164 if (unlikely(status & SYSErr)) {
5165 rtl8169_pcierr_interrupt(dev);
5166 break;
5167 }
5168
5169 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005170 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005171
5172 /* We need to see the lastest version of tp->intr_mask to
5173 * avoid ignoring an MSI interrupt and having to wait for
5174 * another event which may never come.
5175 */
5176 smp_rmb();
5177 if (status & tp->intr_mask & tp->napi_event) {
5178 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5179 tp->intr_mask = ~tp->napi_event;
5180
5181 if (likely(napi_schedule_prep(&tp->napi)))
5182 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005183 else
5184 netif_info(tp, intr, dev,
5185 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005186 }
5187
5188 /* We only get a new MSI interrupt when all active irq
5189 * sources on the chip have been acknowledged. So, ack
5190 * everything we've seen and check if new sources have become
5191 * active to avoid blocking all interrupts from the chip.
5192 */
5193 RTL_W16(IntrStatus,
5194 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5195 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 }
Francois Romieu1519e572011-02-03 12:02:36 +01005197done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 return IRQ_RETVAL(handled);
5199}
5200
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005201static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005203 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5204 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005206 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005208 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 rtl8169_tx_interrupt(dev, tp, ioaddr);
5210
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005211 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005212 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005213
5214 /* We need for force the visibility of tp->intr_mask
5215 * for other CPUs, as we can loose an MSI interrupt
5216 * and potentially wait for a retransmit timeout if we don't.
5217 * The posted write to IntrMask is safe, as it will
5218 * eventually make it to the chip and we won't loose anything
5219 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 */
David Dillowf11a3772009-05-22 15:29:34 +00005221 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005222 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005223 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 }
5225
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005226 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
Francois Romieu523a6092008-09-10 22:28:56 +02005229static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5230{
5231 struct rtl8169_private *tp = netdev_priv(dev);
5232
5233 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5234 return;
5235
5236 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5237 RTL_W32(RxMissed, 0);
5238}
5239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240static void rtl8169_down(struct net_device *dev)
5241{
5242 struct rtl8169_private *tp = netdev_priv(dev);
5243 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244
Francois Romieu4876cc12011-03-11 21:07:11 +01005245 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
5247 netif_stop_queue(dev);
5248
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005249 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005250
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 spin_lock_irq(&tp->lock);
5252
5253 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005254 /*
5255 * At this point device interrupts can not be enabled in any function,
5256 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5257 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5258 */
Francois Romieu523a6092008-09-10 22:28:56 +02005259 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
5261 spin_unlock_irq(&tp->lock);
5262
5263 synchronize_irq(dev->irq);
5264
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005266 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 rtl8169_tx_clear(tp);
5269
5270 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005271
5272 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273}
5274
5275static int rtl8169_close(struct net_device *dev)
5276{
5277 struct rtl8169_private *tp = netdev_priv(dev);
5278 struct pci_dev *pdev = tp->pci_dev;
5279
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005280 pm_runtime_get_sync(&pdev->dev);
5281
Francois Romieucecb5fd2011-04-01 10:21:07 +02005282 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005283 rtl8169_update_counters(dev);
5284
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 rtl8169_down(dev);
5286
5287 free_irq(dev->irq, dev);
5288
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005289 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5290 tp->RxPhyAddr);
5291 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5292 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 tp->TxDescArray = NULL;
5294 tp->RxDescArray = NULL;
5295
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005296 pm_runtime_put_sync(&pdev->dev);
5297
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 return 0;
5299}
5300
Francois Romieu07ce4062007-02-23 23:36:39 +01005301static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302{
5303 struct rtl8169_private *tp = netdev_priv(dev);
5304 void __iomem *ioaddr = tp->mmio_addr;
5305 unsigned long flags;
5306 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005307 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 u32 tmp = 0;
5309
5310 if (dev->flags & IFF_PROMISC) {
5311 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005312 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 rx_mode =
5314 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5315 AcceptAllPhys;
5316 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005317 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005318 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 /* Too many to filter perfectly -- accept all multicasts. */
5320 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5321 mc_filter[1] = mc_filter[0] = 0xffffffff;
5322 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005323 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005324
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 rx_mode = AcceptBroadcast | AcceptMyPhys;
5326 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005327 netdev_for_each_mc_addr(ha, dev) {
5328 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5330 rx_mode |= AcceptMulticast;
5331 }
5332 }
5333
5334 spin_lock_irqsave(&tp->lock, flags);
5335
5336 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005337 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
Francois Romieuf887cce2008-07-17 22:24:18 +02005339 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005340 u32 data = mc_filter[0];
5341
5342 mc_filter[0] = swab32(mc_filter[1]);
5343 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005344 }
5345
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005347 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
Francois Romieu57a9f232007-06-04 22:10:15 +02005349 RTL_W32(RxConfig, tmp);
5350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 spin_unlock_irqrestore(&tp->lock, flags);
5352}
5353
5354/**
5355 * rtl8169_get_stats - Get rtl8169 read/write statistics
5356 * @dev: The Ethernet Device to get statistics for
5357 *
5358 * Get TX/RX statistics for rtl8169
5359 */
5360static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5361{
5362 struct rtl8169_private *tp = netdev_priv(dev);
5363 void __iomem *ioaddr = tp->mmio_addr;
5364 unsigned long flags;
5365
5366 if (netif_running(dev)) {
5367 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005368 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 spin_unlock_irqrestore(&tp->lock, flags);
5370 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005371
Francois Romieucebf8cc2007-10-18 12:06:54 +02005372 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373}
5374
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005375static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005376{
françois romieu065c27c2011-01-03 15:08:12 +00005377 struct rtl8169_private *tp = netdev_priv(dev);
5378
Francois Romieu5d06a992006-02-23 00:47:58 +01005379 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005380 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005381
françois romieu065c27c2011-01-03 15:08:12 +00005382 rtl_pll_power_down(tp);
5383
Francois Romieu5d06a992006-02-23 00:47:58 +01005384 netif_device_detach(dev);
5385 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005386}
Francois Romieu5d06a992006-02-23 00:47:58 +01005387
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005388#ifdef CONFIG_PM
5389
5390static int rtl8169_suspend(struct device *device)
5391{
5392 struct pci_dev *pdev = to_pci_dev(device);
5393 struct net_device *dev = pci_get_drvdata(pdev);
5394
5395 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005396
Francois Romieu5d06a992006-02-23 00:47:58 +01005397 return 0;
5398}
5399
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005400static void __rtl8169_resume(struct net_device *dev)
5401{
françois romieu065c27c2011-01-03 15:08:12 +00005402 struct rtl8169_private *tp = netdev_priv(dev);
5403
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005404 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005405
5406 rtl_pll_power_up(tp);
5407
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005408 rtl8169_schedule_work(dev, rtl8169_reset_task);
5409}
5410
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005411static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005412{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005413 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005414 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005415 struct rtl8169_private *tp = netdev_priv(dev);
5416
5417 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005418
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005419 if (netif_running(dev))
5420 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005421
Francois Romieu5d06a992006-02-23 00:47:58 +01005422 return 0;
5423}
5424
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005425static int rtl8169_runtime_suspend(struct device *device)
5426{
5427 struct pci_dev *pdev = to_pci_dev(device);
5428 struct net_device *dev = pci_get_drvdata(pdev);
5429 struct rtl8169_private *tp = netdev_priv(dev);
5430
5431 if (!tp->TxDescArray)
5432 return 0;
5433
5434 spin_lock_irq(&tp->lock);
5435 tp->saved_wolopts = __rtl8169_get_wol(tp);
5436 __rtl8169_set_wol(tp, WAKE_ANY);
5437 spin_unlock_irq(&tp->lock);
5438
5439 rtl8169_net_suspend(dev);
5440
5441 return 0;
5442}
5443
5444static int rtl8169_runtime_resume(struct device *device)
5445{
5446 struct pci_dev *pdev = to_pci_dev(device);
5447 struct net_device *dev = pci_get_drvdata(pdev);
5448 struct rtl8169_private *tp = netdev_priv(dev);
5449
5450 if (!tp->TxDescArray)
5451 return 0;
5452
5453 spin_lock_irq(&tp->lock);
5454 __rtl8169_set_wol(tp, tp->saved_wolopts);
5455 tp->saved_wolopts = 0;
5456 spin_unlock_irq(&tp->lock);
5457
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005458 rtl8169_init_phy(dev, tp);
5459
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005460 __rtl8169_resume(dev);
5461
5462 return 0;
5463}
5464
5465static int rtl8169_runtime_idle(struct device *device)
5466{
5467 struct pci_dev *pdev = to_pci_dev(device);
5468 struct net_device *dev = pci_get_drvdata(pdev);
5469 struct rtl8169_private *tp = netdev_priv(dev);
5470
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005471 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005472}
5473
Alexey Dobriyan47145212009-12-14 18:00:08 -08005474static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005475 .suspend = rtl8169_suspend,
5476 .resume = rtl8169_resume,
5477 .freeze = rtl8169_suspend,
5478 .thaw = rtl8169_resume,
5479 .poweroff = rtl8169_suspend,
5480 .restore = rtl8169_resume,
5481 .runtime_suspend = rtl8169_runtime_suspend,
5482 .runtime_resume = rtl8169_runtime_resume,
5483 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005484};
5485
5486#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5487
5488#else /* !CONFIG_PM */
5489
5490#define RTL8169_PM_OPS NULL
5491
5492#endif /* !CONFIG_PM */
5493
Francois Romieu1765f952008-09-13 17:21:40 +02005494static void rtl_shutdown(struct pci_dev *pdev)
5495{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005496 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005497 struct rtl8169_private *tp = netdev_priv(dev);
5498 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005499
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005500 rtl8169_net_suspend(dev);
5501
Francois Romieucecb5fd2011-04-01 10:21:07 +02005502 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005503 rtl_rar_set(tp, dev->perm_addr);
5504
françois romieu4bb3f522009-06-17 11:41:45 +00005505 spin_lock_irq(&tp->lock);
5506
5507 rtl8169_asic_down(ioaddr);
5508
5509 spin_unlock_irq(&tp->lock);
5510
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005511 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005512 /* WoL fails with some 8168 when the receiver is disabled. */
5513 if (tp->features & RTL_FEATURE_WOL) {
5514 pci_clear_master(pdev);
5515
5516 RTL_W8(ChipCmd, CmdRxEnb);
5517 /* PCI commit */
5518 RTL_R8(ChipCmd);
5519 }
5520
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005521 pci_wake_from_d3(pdev, true);
5522 pci_set_power_state(pdev, PCI_D3hot);
5523 }
5524}
Francois Romieu5d06a992006-02-23 00:47:58 +01005525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526static struct pci_driver rtl8169_pci_driver = {
5527 .name = MODULENAME,
5528 .id_table = rtl8169_pci_tbl,
5529 .probe = rtl8169_init_one,
5530 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005531 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005532 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533};
5534
Francois Romieu07d3f512007-02-21 22:40:46 +01005535static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536{
Jeff Garzik29917622006-08-19 17:48:59 -04005537 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538}
5539
Francois Romieu07d3f512007-02-21 22:40:46 +01005540static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541{
5542 pci_unregister_driver(&rtl8169_pci_driver);
5543}
5544
5545module_init(rtl8169_init_module);
5546module_exit(rtl8169_cleanup_module);