blob: 2543edd9a2ccd1405e7ab183964d543f0c444a2c [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>
25#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Francois Romieu99f252b2007-04-02 22:59:59 +020029#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
31#include <asm/irq.h>
32
Francois Romieu865c6522008-05-11 14:51:00 +020033#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define MODULENAME "r8169"
35#define PFX MODULENAME ": "
36
françois romieubca03d52011-01-03 15:07:31 +000037#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
38#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080039#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef RTL8169_DEBUG
42#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020043 if (!(expr)) { \
44 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070045 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020046 }
Joe Perches06fa7352007-10-18 21:15:00 +020047#define dprintk(fmt, args...) \
48 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#else
50#define assert(expr) do {} while (0)
51#define dprintk(fmt, args...) do {} while (0)
52#endif /* RTL8169_DEBUG */
53
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020054#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070055 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define TX_BUFFS_AVAIL(tp) \
58 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
61 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050062static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/* MAC address length */
65#define MAC_ADDR_LEN 6
66
Francois Romieu9c14cea2008-07-05 00:21:15 +020067#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
69#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
70#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
72#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
73
74#define R8169_REGS_SIZE 256
75#define R8169_NAPI_WEIGHT 64
76#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
77#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
78#define RX_BUF_SIZE 1536 /* Rx Buffer size */
79#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
80#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
81
82#define RTL8169_TX_TIMEOUT (6*HZ)
83#define RTL8169_PHY_TIMEOUT (10*HZ)
84
françois romieuea8dbdd2009-03-15 01:10:50 +000085#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
86#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020087#define RTL_EEPROM_SIG_ADDR 0x0000
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* write/read MMIO register */
90#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
91#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
92#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
93#define RTL_R8(reg) readb (ioaddr + (reg))
94#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000095#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97enum mac_version {
Jean Delvaref21b75e2009-05-26 20:54:48 -070098 RTL_GIGA_MAC_NONE = 0x00,
Francois Romieuba6eb6e2007-06-11 23:35:18 +020099 RTL_GIGA_MAC_VER_01 = 0x01, // 8169
100 RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
101 RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
102 RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
103 RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +0100104 RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
Francois Romieu2857ffb2008-08-02 21:08:49 +0200105 RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
106 RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
107 RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
108 RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
Francois Romieu2dd99532007-06-11 23:22:52 +0200109 RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200110 RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
111 RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
112 RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
113 RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
114 RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
115 RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
116 RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
117 RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
Francois Romieu197ff762008-06-28 13:16:02 +0200118 RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
Francois Romieu6fb07052008-06-29 11:54:28 +0200119 RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
Francois Romieuef3386f2008-06-29 12:24:30 +0200120 RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
Francois Romieu7f3e3d32008-07-20 18:53:20 +0200121 RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
Francois Romieu5b538df2008-07-20 16:22:45 +0200122 RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
françois romieudaf9df62009-10-07 12:44:20 +0000123 RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
124 RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
françois romieue6de30d2011-01-03 15:08:37 +0000125 RTL_GIGA_MAC_VER_27 = 0x1b, // 8168DP
126 RTL_GIGA_MAC_VER_28 = 0x1c, // 8168DP
Hayes Wang5a5e4442011-02-22 17:26:21 +0800127 RTL_GIGA_MAC_VER_29 = 0x1d, // 8105E
128 RTL_GIGA_MAC_VER_30 = 0x1e, // 8105E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#define _R(NAME,MAC,MASK) \
132 { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
133
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800134static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 const char *name;
136 u8 mac_version;
137 u32 RxConfigMask; /* Clears the bits supported by this chip */
138} rtl_chip_info[] = {
Francois Romieuba6eb6e2007-06-11 23:35:18 +0200139 _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
140 _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
141 _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
142 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
143 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +0100144 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
Francois Romieu2857ffb2008-08-02 21:08:49 +0200145 _R("RTL8102e", RTL_GIGA_MAC_VER_07, 0xff7e1880), // PCI-E
146 _R("RTL8102e", RTL_GIGA_MAC_VER_08, 0xff7e1880), // PCI-E
147 _R("RTL8102e", RTL_GIGA_MAC_VER_09, 0xff7e1880), // PCI-E
148 _R("RTL8101e", RTL_GIGA_MAC_VER_10, 0xff7e1880), // PCI-E
Francois Romieubcf0bf92006-07-26 23:14:13 +0200149 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
150 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
151 _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
152 _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200153 _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880), // PCI-E 8139
154 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880), // PCI-E
155 _R("RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880), // PCI-E
156 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880), // PCI-E
157 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880), // PCI-E
Francois Romieu197ff762008-06-28 13:16:02 +0200158 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880), // PCI-E
Francois Romieu6fb07052008-06-29 11:54:28 +0200159 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, 0xff7e1880), // PCI-E
Francois Romieuef3386f2008-06-29 12:24:30 +0200160 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, 0xff7e1880), // PCI-E
Francois Romieu7f3e3d32008-07-20 18:53:20 +0200161 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, 0xff7e1880), // PCI-E
Francois Romieu5b538df2008-07-20 16:22:45 +0200162 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, 0xff7e1880), // PCI-E
françois romieudaf9df62009-10-07 12:44:20 +0000163 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, 0xff7e1880), // PCI-E
164 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, 0xff7e1880), // PCI-E
françois romieue6de30d2011-01-03 15:08:37 +0000165 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, 0xff7e1880), // PCI-E
Hayes Wang5a5e4442011-02-22 17:26:21 +0800166 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28, 0xff7e1880), // PCI-E
167 _R("RTL8105e", RTL_GIGA_MAC_VER_29, 0xff7e1880), // PCI-E
168 _R("RTL8105e", RTL_GIGA_MAC_VER_30, 0xff7e1880) // PCI-E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170#undef _R
171
Francois Romieubcf0bf92006-07-26 23:14:13 +0200172enum cfg_version {
173 RTL_CFG_0 = 0x00,
174 RTL_CFG_1,
175 RTL_CFG_2
176};
177
Francois Romieu07ce4062007-02-23 23:36:39 +0100178static void rtl_hw_start_8169(struct net_device *);
179static void rtl_hw_start_8168(struct net_device *);
180static void rtl_hw_start_8101(struct net_device *);
181
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000182static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200183 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200184 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200185 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100186 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200187 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
188 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200189 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200190 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
191 { PCI_VENDOR_ID_LINKSYS, 0x1032,
192 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100193 { 0x0001, 0x8168,
194 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 {0,},
196};
197
198MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
199
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000200static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700201static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200202static struct {
203 u32 msg_enable;
204} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Francois Romieu07d3f512007-02-21 22:40:46 +0100206enum rtl_registers {
207 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100208 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100209 MAR0 = 8, /* Multicast filter. */
210 CounterAddrLow = 0x10,
211 CounterAddrHigh = 0x14,
212 TxDescStartAddrLow = 0x20,
213 TxDescStartAddrHigh = 0x24,
214 TxHDescStartAddrLow = 0x28,
215 TxHDescStartAddrHigh = 0x2c,
216 FLASH = 0x30,
217 ERSR = 0x36,
218 ChipCmd = 0x37,
219 TxPoll = 0x38,
220 IntrMask = 0x3c,
221 IntrStatus = 0x3e,
222 TxConfig = 0x40,
223 RxConfig = 0x44,
224 RxMissed = 0x4c,
225 Cfg9346 = 0x50,
226 Config0 = 0x51,
227 Config1 = 0x52,
228 Config2 = 0x53,
229 Config3 = 0x54,
230 Config4 = 0x55,
231 Config5 = 0x56,
232 MultiIntr = 0x5c,
233 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100234 PHYstatus = 0x6c,
235 RxMaxSize = 0xda,
236 CPlusCmd = 0xe0,
237 IntrMitigate = 0xe2,
238 RxDescAddrLow = 0xe4,
239 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000240 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
241
242#define NoEarlyTx 0x3f /* Max value : no early transmit. */
243
244 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
245
246#define TxPacketMax (8064 >> 7)
247
Francois Romieu07d3f512007-02-21 22:40:46 +0100248 FuncEvent = 0xf0,
249 FuncEventMask = 0xf4,
250 FuncPresetState = 0xf8,
251 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
Francois Romieuf162a5d2008-06-01 22:37:49 +0200254enum rtl8110_registers {
255 TBICSR = 0x64,
256 TBI_ANAR = 0x68,
257 TBI_LPAR = 0x6a,
258};
259
260enum rtl8168_8101_registers {
261 CSIDR = 0x64,
262 CSIAR = 0x68,
263#define CSIAR_FLAG 0x80000000
264#define CSIAR_WRITE_CMD 0x80000000
265#define CSIAR_BYTE_ENABLE 0x0f
266#define CSIAR_BYTE_ENABLE_SHIFT 12
267#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000268 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200269 EPHYAR = 0x80,
270#define EPHYAR_FLAG 0x80000000
271#define EPHYAR_WRITE_CMD 0x80000000
272#define EPHYAR_REG_MASK 0x1f
273#define EPHYAR_REG_SHIFT 16
274#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800275 DLLPR = 0xd0,
276#define PM_SWITCH (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200277 DBG_REG = 0xd1,
278#define FIX_NAK_1 (1 << 4)
279#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800280 TWSI = 0xd2,
281 MCU = 0xd3,
282#define EN_NDP (1 << 3)
283#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000284 EFUSEAR = 0xdc,
285#define EFUSEAR_FLAG 0x80000000
286#define EFUSEAR_WRITE_CMD 0x80000000
287#define EFUSEAR_READ_CMD 0x00000000
288#define EFUSEAR_REG_MASK 0x03ff
289#define EFUSEAR_REG_SHIFT 8
290#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200291};
292
françois romieuc0e45c12011-01-03 15:08:04 +0000293enum rtl8168_registers {
françois romieub646d902011-01-03 15:08:21 +0000294 ERIDR = 0x70,
295 ERIAR = 0x74,
296#define ERIAR_FLAG 0x80000000
297#define ERIAR_WRITE_CMD 0x80000000
298#define ERIAR_READ_CMD 0x00000000
299#define ERIAR_ADDR_BYTE_ALIGN 4
300#define ERIAR_EXGMAC 0
301#define ERIAR_MSIX 1
302#define ERIAR_ASF 2
303#define ERIAR_TYPE_SHIFT 16
304#define ERIAR_BYTEEN 0x0f
305#define ERIAR_BYTEEN_SHIFT 12
françois romieuc0e45c12011-01-03 15:08:04 +0000306 EPHY_RXER_NUM = 0x7c,
307 OCPDR = 0xb0, /* OCP GPHY access */
308#define OCPDR_WRITE_CMD 0x80000000
309#define OCPDR_READ_CMD 0x00000000
310#define OCPDR_REG_MASK 0x7f
311#define OCPDR_GPHY_REG_SHIFT 16
312#define OCPDR_DATA_MASK 0xffff
313 OCPAR = 0xb4,
314#define OCPAR_FLAG 0x80000000
315#define OCPAR_GPHY_WRITE_CMD 0x8000f060
316#define OCPAR_GPHY_READ_CMD 0x0000f060
françois romieue6de30d2011-01-03 15:08:37 +0000317 RDSAR1 = 0xd0 /* 8168c only. Undocumented on 8168dp */
françois romieuc0e45c12011-01-03 15:08:04 +0000318};
319
Francois Romieu07d3f512007-02-21 22:40:46 +0100320enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100322 SYSErr = 0x8000,
323 PCSTimeout = 0x4000,
324 SWInt = 0x0100,
325 TxDescUnavail = 0x0080,
326 RxFIFOOver = 0x0040,
327 LinkChg = 0x0020,
328 RxOverflow = 0x0010,
329 TxErr = 0x0008,
330 TxOK = 0x0004,
331 RxErr = 0x0002,
332 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200335 RxFOVF = (1 << 23),
336 RxRWT = (1 << 22),
337 RxRES = (1 << 21),
338 RxRUNT = (1 << 20),
339 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100342 CmdReset = 0x10,
343 CmdRxEnb = 0x08,
344 CmdTxEnb = 0x04,
345 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Francois Romieu275391a2007-02-23 23:50:28 +0100347 /* TXPoll register p.5 */
348 HPQ = 0x80, /* Poll cmd on the high prio queue */
349 NPQ = 0x40, /* Poll cmd on the low prio queue */
350 FSWInt = 0x01, /* Forced software interrupt */
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100353 Cfg9346_Lock = 0x00,
354 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100357 AcceptErr = 0x20,
358 AcceptRunt = 0x10,
359 AcceptBroadcast = 0x08,
360 AcceptMulticast = 0x04,
361 AcceptMyPhys = 0x02,
362 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100365 RxCfgFIFOShift = 13,
366 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* TxConfigBits */
369 TxInterFrameGapShift = 24,
370 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
371
Francois Romieu5d06a992006-02-23 00:47:58 +0100372 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200373 LEDS1 = (1 << 7),
374 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200375 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200376 Speed_down = (1 << 4),
377 MEMMAP = (1 << 3),
378 IOMAP = (1 << 2),
379 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100380 PMEnable = (1 << 0), /* Power Management Enable */
381
Francois Romieu6dccd162007-02-13 23:38:05 +0100382 /* Config2 register p. 25 */
383 PCI_Clock_66MHz = 0x01,
384 PCI_Clock_33MHz = 0x00,
385
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100386 /* Config3 register p.25 */
387 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
388 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200389 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100390
Francois Romieu5d06a992006-02-23 00:47:58 +0100391 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100392 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
393 MWF = (1 << 5), /* Accept Multicast wakeup frame */
394 UWF = (1 << 4), /* Accept Unicast wakeup frame */
395 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100396 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* TBICSR p.28 */
399 TBIReset = 0x80000000,
400 TBILoopback = 0x40000000,
401 TBINwEnable = 0x20000000,
402 TBINwRestart = 0x10000000,
403 TBILinkOk = 0x02000000,
404 TBINwComplete = 0x01000000,
405
406 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200407 EnableBist = (1 << 15), // 8168 8101
408 Mac_dbgo_oe = (1 << 14), // 8168 8101
409 Normal_mode = (1 << 13), // unused
410 Force_half_dup = (1 << 12), // 8168 8101
411 Force_rxflow_en = (1 << 11), // 8168 8101
412 Force_txflow_en = (1 << 10), // 8168 8101
413 Cxpl_dbg_sel = (1 << 9), // 8168 8101
414 ASF = (1 << 8), // 8168 8101
415 PktCntrDisable = (1 << 7), // 8168 8101
416 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 RxVlan = (1 << 6),
418 RxChkSum = (1 << 5),
419 PCIDAC = (1 << 4),
420 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100421 INTT_0 = 0x0000, // 8168
422 INTT_1 = 0x0001, // 8168
423 INTT_2 = 0x0002, // 8168
424 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100427 TBI_Enable = 0x80,
428 TxFlowCtrl = 0x40,
429 RxFlowCtrl = 0x20,
430 _1000bpsF = 0x10,
431 _100bps = 0x08,
432 _10bps = 0x04,
433 LinkStatus = 0x02,
434 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100437 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200438
439 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100440 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441};
442
Francois Romieu07d3f512007-02-21 22:40:46 +0100443enum desc_status_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
445 RingEnd = (1 << 30), /* End of descriptor ring */
446 FirstFrag = (1 << 29), /* First segment of a packet */
447 LastFrag = (1 << 28), /* Final segment of a packet */
448
449 /* Tx private */
450 LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
451 MSSShift = 16, /* MSS value position */
452 MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
453 IPCS = (1 << 18), /* Calculate IP checksum */
454 UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
455 TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
456 TxVlanTag = (1 << 17), /* Add VLAN tag */
457
458 /* Rx private */
459 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
460 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
461
462#define RxProtoUDP (PID1)
463#define RxProtoTCP (PID0)
464#define RxProtoIP (PID1 | PID0)
465#define RxProtoMask RxProtoIP
466
467 IPFail = (1 << 16), /* IP checksum failed */
468 UDPFail = (1 << 15), /* UDP/IP checksum failed */
469 TCPFail = (1 << 14), /* TCP/IP checksum failed */
470 RxVlanTag = (1 << 16), /* VLAN tag available */
471};
472
473#define RsvdMask 0x3fffc000
474
475struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200476 __le32 opts1;
477 __le32 opts2;
478 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479};
480
481struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200482 __le32 opts1;
483 __le32 opts2;
484 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485};
486
487struct ring_info {
488 struct sk_buff *skb;
489 u32 len;
490 u8 __pad[sizeof(void *) - sizeof(u32)];
491};
492
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200493enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200494 RTL_FEATURE_WOL = (1 << 0),
495 RTL_FEATURE_MSI = (1 << 1),
496 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200497};
498
Ivan Vecera355423d2009-02-06 21:49:57 -0800499struct rtl8169_counters {
500 __le64 tx_packets;
501 __le64 rx_packets;
502 __le64 tx_errors;
503 __le32 rx_errors;
504 __le16 rx_missed;
505 __le16 align_errors;
506 __le32 tx_one_collision;
507 __le32 tx_multi_collision;
508 __le64 rx_unicast;
509 __le64 rx_broadcast;
510 __le32 rx_multicast;
511 __le16 tx_aborted;
512 __le16 tx_underun;
513};
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515struct rtl8169_private {
516 void __iomem *mmio_addr; /* memory map physical address */
517 struct pci_dev *pci_dev; /* Index of PCI device */
David Howellsc4028952006-11-22 14:57:56 +0000518 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700519 struct napi_struct napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 spinlock_t lock; /* spin lock flag */
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200521 u32 msg_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int chipset;
523 int mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
525 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
526 u32 dirty_rx;
527 u32 dirty_tx;
528 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
529 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
530 dma_addr_t TxPhyAddr;
531 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000532 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct timer_list timer;
535 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100536 u16 intr_event;
537 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 u16 intr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int phy_1000_ctrl_reg;
540#ifdef CONFIG_R8169_VLAN
541 struct vlan_group *vlgrp;
542#endif
françois romieuc0e45c12011-01-03 15:08:04 +0000543
544 struct mdio_ops {
545 void (*write)(void __iomem *, int, int);
546 int (*read)(void __iomem *, int);
547 } mdio_ops;
548
françois romieu065c27c2011-01-03 15:08:12 +0000549 struct pll_power_ops {
550 void (*down)(struct rtl8169_private *);
551 void (*up)(struct rtl8169_private *);
552 } pll_power_ops;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int (*set_speed)(struct net_device *, u8 autoneg, u16 speed, u8 duplex);
Francois Romieuccdffb92008-07-26 14:26:06 +0200555 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000556 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100557 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000558 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800560 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200561 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000562 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200563 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200564
565 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800566 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000567 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000568
569 const struct firmware *fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570};
571
Ralf Baechle979b6c12005-06-13 14:30:40 -0700572MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700575MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200576module_param_named(debug, debug.msg_enable, int, 0);
577MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578MODULE_LICENSE("GPL");
579MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000580MODULE_FIRMWARE(FIRMWARE_8168D_1);
581MODULE_FIRMWARE(FIRMWARE_8168D_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800582MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000585static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
586 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100587static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100589static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100591static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200593static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700595 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200596static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200598static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700599static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200602 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
françois romieub646d902011-01-03 15:08:21 +0000604static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
605{
606 void __iomem *ioaddr = tp->mmio_addr;
607 int i;
608
609 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
610 for (i = 0; i < 20; i++) {
611 udelay(100);
612 if (RTL_R32(OCPAR) & OCPAR_FLAG)
613 break;
614 }
615 return RTL_R32(OCPDR);
616}
617
618static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
619{
620 void __iomem *ioaddr = tp->mmio_addr;
621 int i;
622
623 RTL_W32(OCPDR, data);
624 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
625 for (i = 0; i < 20; i++) {
626 udelay(100);
627 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
628 break;
629 }
630}
631
632static void rtl8168_oob_notify(void __iomem *ioaddr, u8 cmd)
633{
634 int i;
635
636 RTL_W8(ERIDR, cmd);
637 RTL_W32(ERIAR, 0x800010e8);
638 msleep(2);
639 for (i = 0; i < 5; i++) {
640 udelay(100);
641 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
642 break;
643 }
644
645 ocp_write(ioaddr, 0x1, 0x30, 0x00000001);
646}
647
648#define OOB_CMD_RESET 0x00
649#define OOB_CMD_DRIVER_START 0x05
650#define OOB_CMD_DRIVER_STOP 0x06
651
652static void rtl8168_driver_start(struct rtl8169_private *tp)
653{
654 int i;
655
656 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
657
658 for (i = 0; i < 10; i++) {
659 msleep(10);
660 if (ocp_read(tp, 0x0f, 0x0010) & 0x00000800)
661 break;
662 }
663}
664
665static void rtl8168_driver_stop(struct rtl8169_private *tp)
666{
667 int i;
668
669 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
670
671 for (i = 0; i < 10; i++) {
672 msleep(10);
673 if ((ocp_read(tp, 0x0f, 0x0010) & 0x00000800) == 0)
674 break;
675 }
676}
677
678
françois romieu4da19632011-01-03 15:07:55 +0000679static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 int i;
682
Francois Romieua6baf3a2007-11-08 23:23:21 +0100683 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Francois Romieu23714082006-01-29 00:49:09 +0100685 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100686 /*
687 * Check if the RTL8169 has completed writing to the specified
688 * MII register.
689 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200690 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 break;
Francois Romieu23714082006-01-29 00:49:09 +0100692 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700694 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700695 * According to hardware specs a 20us delay is required after write
696 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700697 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700698 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
françois romieu4da19632011-01-03 15:07:55 +0000701static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 int i, value = -1;
704
Francois Romieua6baf3a2007-11-08 23:23:21 +0100705 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Francois Romieu23714082006-01-29 00:49:09 +0100707 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100708 /*
709 * Check if the RTL8169 has completed retrieving data from
710 * the specified MII register.
711 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100713 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 break;
715 }
Francois Romieu23714082006-01-29 00:49:09 +0100716 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700718 /*
719 * According to hardware specs a 20us delay is required after read
720 * complete indication, but before sending next command.
721 */
722 udelay(20);
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return value;
725}
726
françois romieuc0e45c12011-01-03 15:08:04 +0000727static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
728{
729 int i;
730
731 RTL_W32(OCPDR, data |
732 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
733 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
734 RTL_W32(EPHY_RXER_NUM, 0);
735
736 for (i = 0; i < 100; i++) {
737 mdelay(1);
738 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
739 break;
740 }
741}
742
743static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
744{
745 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
746 (value & OCPDR_DATA_MASK));
747}
748
749static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
750{
751 int i;
752
753 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
754
755 mdelay(1);
756 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
757 RTL_W32(EPHY_RXER_NUM, 0);
758
759 for (i = 0; i < 100; i++) {
760 mdelay(1);
761 if (RTL_R32(OCPAR) & OCPAR_FLAG)
762 break;
763 }
764
765 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
766}
767
françois romieue6de30d2011-01-03 15:08:37 +0000768#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
769
770static void r8168dp_2_mdio_start(void __iomem *ioaddr)
771{
772 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
773}
774
775static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
776{
777 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
778}
779
780static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
781{
782 r8168dp_2_mdio_start(ioaddr);
783
784 r8169_mdio_write(ioaddr, reg_addr, value);
785
786 r8168dp_2_mdio_stop(ioaddr);
787}
788
789static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
790{
791 int value;
792
793 r8168dp_2_mdio_start(ioaddr);
794
795 value = r8169_mdio_read(ioaddr, reg_addr);
796
797 r8168dp_2_mdio_stop(ioaddr);
798
799 return value;
800}
801
françois romieu4da19632011-01-03 15:07:55 +0000802static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200803{
françois romieuc0e45c12011-01-03 15:08:04 +0000804 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200805}
806
françois romieu4da19632011-01-03 15:07:55 +0000807static int rtl_readphy(struct rtl8169_private *tp, int location)
808{
françois romieuc0e45c12011-01-03 15:08:04 +0000809 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000810}
811
812static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
813{
814 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
815}
816
817static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000818{
819 int val;
820
françois romieu4da19632011-01-03 15:07:55 +0000821 val = rtl_readphy(tp, reg_addr);
822 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000823}
824
Francois Romieuccdffb92008-07-26 14:26:06 +0200825static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
826 int val)
827{
828 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200829
françois romieu4da19632011-01-03 15:07:55 +0000830 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200831}
832
833static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
834{
835 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200836
françois romieu4da19632011-01-03 15:07:55 +0000837 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200838}
839
Francois Romieudacf8152008-08-02 20:44:13 +0200840static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
841{
842 unsigned int i;
843
844 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
845 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
846
847 for (i = 0; i < 100; i++) {
848 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
849 break;
850 udelay(10);
851 }
852}
853
854static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
855{
856 u16 value = 0xffff;
857 unsigned int i;
858
859 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
860
861 for (i = 0; i < 100; i++) {
862 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
863 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
864 break;
865 }
866 udelay(10);
867 }
868
869 return value;
870}
871
872static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
873{
874 unsigned int i;
875
876 RTL_W32(CSIDR, value);
877 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
878 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
879
880 for (i = 0; i < 100; i++) {
881 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
882 break;
883 udelay(10);
884 }
885}
886
887static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
888{
889 u32 value = ~0x00;
890 unsigned int i;
891
892 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
893 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
894
895 for (i = 0; i < 100; i++) {
896 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
897 value = RTL_R32(CSIDR);
898 break;
899 }
900 udelay(10);
901 }
902
903 return value;
904}
905
françois romieudaf9df62009-10-07 12:44:20 +0000906static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
907{
908 u8 value = 0xff;
909 unsigned int i;
910
911 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
912
913 for (i = 0; i < 300; i++) {
914 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
915 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
916 break;
917 }
918 udelay(100);
919 }
920
921 return value;
922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
925{
926 RTL_W16(IntrMask, 0x0000);
927
928 RTL_W16(IntrStatus, 0xffff);
929}
930
931static void rtl8169_asic_down(void __iomem *ioaddr)
932{
933 RTL_W8(ChipCmd, 0x00);
934 rtl8169_irq_mask_and_ack(ioaddr);
935 RTL_R16(CPlusCmd);
936}
937
françois romieu4da19632011-01-03 15:07:55 +0000938static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
françois romieu4da19632011-01-03 15:07:55 +0000940 void __iomem *ioaddr = tp->mmio_addr;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return RTL_R32(TBICSR) & TBIReset;
943}
944
françois romieu4da19632011-01-03 15:07:55 +0000945static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
françois romieu4da19632011-01-03 15:07:55 +0000947 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
951{
952 return RTL_R32(TBICSR) & TBILinkOk;
953}
954
955static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
956{
957 return RTL_R8(PHYstatus) & LinkStatus;
958}
959
françois romieu4da19632011-01-03 15:07:55 +0000960static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
françois romieu4da19632011-01-03 15:07:55 +0000962 void __iomem *ioaddr = tp->mmio_addr;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
965}
966
françois romieu4da19632011-01-03 15:07:55 +0000967static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 unsigned int val;
970
françois romieu4da19632011-01-03 15:07:55 +0000971 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
972 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000975static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieu07d3f512007-02-21 22:40:46 +0100976 struct rtl8169_private *tp,
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000977 void __iomem *ioaddr,
978 bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 unsigned long flags;
981
982 spin_lock_irqsave(&tp->lock, flags);
983 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000984 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000985 if (pm)
986 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +0100988 if (net_ratelimit())
989 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200990 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +0000992 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000993 if (pm)
994 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 spin_unlock_irqrestore(&tp->lock, flags);
997}
998
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000999static void rtl8169_check_link_status(struct net_device *dev,
1000 struct rtl8169_private *tp,
1001 void __iomem *ioaddr)
1002{
1003 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1004}
1005
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001006#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1007
1008static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1009{
1010 void __iomem *ioaddr = tp->mmio_addr;
1011 u8 options;
1012 u32 wolopts = 0;
1013
1014 options = RTL_R8(Config1);
1015 if (!(options & PMEnable))
1016 return 0;
1017
1018 options = RTL_R8(Config3);
1019 if (options & LinkUp)
1020 wolopts |= WAKE_PHY;
1021 if (options & MagicPacket)
1022 wolopts |= WAKE_MAGIC;
1023
1024 options = RTL_R8(Config5);
1025 if (options & UWF)
1026 wolopts |= WAKE_UCAST;
1027 if (options & BWF)
1028 wolopts |= WAKE_BCAST;
1029 if (options & MWF)
1030 wolopts |= WAKE_MCAST;
1031
1032 return wolopts;
1033}
1034
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001035static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1036{
1037 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001038
1039 spin_lock_irq(&tp->lock);
1040
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001041 wol->supported = WAKE_ANY;
1042 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001043
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001044 spin_unlock_irq(&tp->lock);
1045}
1046
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001047static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001048{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001049 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001050 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001051 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001052 u32 opt;
1053 u16 reg;
1054 u8 mask;
1055 } cfg[] = {
1056 { WAKE_ANY, Config1, PMEnable },
1057 { WAKE_PHY, Config3, LinkUp },
1058 { WAKE_MAGIC, Config3, MagicPacket },
1059 { WAKE_UCAST, Config5, UWF },
1060 { WAKE_BCAST, Config5, BWF },
1061 { WAKE_MCAST, Config5, MWF },
1062 { WAKE_ANY, Config5, LanWake }
1063 };
1064
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001065 RTL_W8(Cfg9346, Cfg9346_Unlock);
1066
1067 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1068 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001069 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001070 options |= cfg[i].mask;
1071 RTL_W8(cfg[i].reg, options);
1072 }
1073
1074 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001075}
1076
1077static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1078{
1079 struct rtl8169_private *tp = netdev_priv(dev);
1080
1081 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001082
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001083 if (wol->wolopts)
1084 tp->features |= RTL_FEATURE_WOL;
1085 else
1086 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001087 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001088 spin_unlock_irq(&tp->lock);
1089
françois romieuea809072010-11-08 13:23:58 +00001090 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1091
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001092 return 0;
1093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095static void rtl8169_get_drvinfo(struct net_device *dev,
1096 struct ethtool_drvinfo *info)
1097{
1098 struct rtl8169_private *tp = netdev_priv(dev);
1099
1100 strcpy(info->driver, MODULENAME);
1101 strcpy(info->version, RTL8169_VERSION);
1102 strcpy(info->bus_info, pci_name(tp->pci_dev));
1103}
1104
1105static int rtl8169_get_regs_len(struct net_device *dev)
1106{
1107 return R8169_REGS_SIZE;
1108}
1109
1110static int rtl8169_set_speed_tbi(struct net_device *dev,
1111 u8 autoneg, u16 speed, u8 duplex)
1112{
1113 struct rtl8169_private *tp = netdev_priv(dev);
1114 void __iomem *ioaddr = tp->mmio_addr;
1115 int ret = 0;
1116 u32 reg;
1117
1118 reg = RTL_R32(TBICSR);
1119 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1120 (duplex == DUPLEX_FULL)) {
1121 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1122 } else if (autoneg == AUTONEG_ENABLE)
1123 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1124 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001125 netif_warn(tp, link, dev,
1126 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ret = -EOPNOTSUPP;
1128 }
1129
1130 return ret;
1131}
1132
1133static int rtl8169_set_speed_xmii(struct net_device *dev,
1134 u8 autoneg, u16 speed, u8 duplex)
1135{
1136 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001137 int giga_ctrl, bmcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Hayes Wang716b50a2011-02-22 17:26:18 +08001139 rtl_writephy(tp, 0x1f, 0x0000);
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001142 int auto_nego;
1143
françois romieu4da19632011-01-03 15:07:55 +00001144 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001145 auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
1146 ADVERTISE_100HALF | ADVERTISE_100FULL);
françois romieu3577aa12009-05-19 10:46:48 +00001147 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1148
françois romieu4da19632011-01-03 15:07:55 +00001149 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001150 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1151
1152 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1153 if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
1154 (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
1155 (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
1156 (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
1157 (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
1158 (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
1159 (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
Hayes Wang5a5e4442011-02-22 17:26:21 +08001160 (tp->mac_version != RTL_GIGA_MAC_VER_16) &&
1161 (tp->mac_version != RTL_GIGA_MAC_VER_29) &&
1162 (tp->mac_version != RTL_GIGA_MAC_VER_30)) {
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001163 giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
Joe Perchesbf82c182010-02-09 11:49:50 +00001164 } else {
1165 netif_info(tp, link, dev,
1166 "PHY does not support 1000Mbps\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02001167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
françois romieu3577aa12009-05-19 10:46:48 +00001169 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001170
françois romieu4da19632011-01-03 15:07:55 +00001171 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1172 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001173 } else {
1174 giga_ctrl = 0;
1175
1176 if (speed == SPEED_10)
1177 bmcr = 0;
1178 else if (speed == SPEED_100)
1179 bmcr = BMCR_SPEED100;
1180 else
1181 return -EINVAL;
1182
1183 if (duplex == DUPLEX_FULL)
1184 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001185 }
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 tp->phy_1000_ctrl_reg = giga_ctrl;
1188
françois romieu4da19632011-01-03 15:07:55 +00001189 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001190
1191 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
1192 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
1193 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001194 rtl_writephy(tp, 0x17, 0x2138);
1195 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001196 } else {
françois romieu4da19632011-01-03 15:07:55 +00001197 rtl_writephy(tp, 0x17, 0x2108);
1198 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001199 }
1200 }
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203}
1204
1205static int rtl8169_set_speed(struct net_device *dev,
1206 u8 autoneg, u16 speed, u8 duplex)
1207{
1208 struct rtl8169_private *tp = netdev_priv(dev);
1209 int ret;
1210
1211 ret = tp->set_speed(dev, autoneg, speed, duplex);
1212
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001213 if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1215
1216 return ret;
1217}
1218
1219static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1220{
1221 struct rtl8169_private *tp = netdev_priv(dev);
1222 unsigned long flags;
1223 int ret;
1224
1225 spin_lock_irqsave(&tp->lock, flags);
1226 ret = rtl8169_set_speed(dev, cmd->autoneg, cmd->speed, cmd->duplex);
1227 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return ret;
1230}
1231
1232static u32 rtl8169_get_rx_csum(struct net_device *dev)
1233{
1234 struct rtl8169_private *tp = netdev_priv(dev);
1235
1236 return tp->cp_cmd & RxChkSum;
1237}
1238
1239static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
1240{
1241 struct rtl8169_private *tp = netdev_priv(dev);
1242 void __iomem *ioaddr = tp->mmio_addr;
1243 unsigned long flags;
1244
1245 spin_lock_irqsave(&tp->lock, flags);
1246
1247 if (data)
1248 tp->cp_cmd |= RxChkSum;
1249 else
1250 tp->cp_cmd &= ~RxChkSum;
1251
1252 RTL_W16(CPlusCmd, tp->cp_cmd);
1253 RTL_R16(CPlusCmd);
1254
1255 spin_unlock_irqrestore(&tp->lock, flags);
1256
1257 return 0;
1258}
1259
1260#ifdef CONFIG_R8169_VLAN
1261
1262static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1263 struct sk_buff *skb)
1264{
Jesse Grosseab6d182010-10-20 13:56:03 +00001265 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1267}
1268
1269static void rtl8169_vlan_rx_register(struct net_device *dev,
1270 struct vlan_group *grp)
1271{
1272 struct rtl8169_private *tp = netdev_priv(dev);
1273 void __iomem *ioaddr = tp->mmio_addr;
1274 unsigned long flags;
1275
1276 spin_lock_irqsave(&tp->lock, flags);
1277 tp->vlgrp = grp;
Simon Wunderlich05af2142009-10-24 06:47:33 -07001278 /*
1279 * Do not disable RxVlan on 8110SCd.
1280 */
1281 if (tp->vlgrp || (tp->mac_version == RTL_GIGA_MAC_VER_05))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 tp->cp_cmd |= RxVlan;
1283 else
1284 tp->cp_cmd &= ~RxVlan;
1285 RTL_W16(CPlusCmd, tp->cp_cmd);
1286 RTL_R16(CPlusCmd);
1287 spin_unlock_irqrestore(&tp->lock, flags);
1288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
Eric Dumazet630b9432010-03-31 02:08:31 +00001291 struct sk_buff *skb, int polling)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 u32 opts2 = le32_to_cpu(desc->opts2);
Francois Romieu865c6522008-05-11 14:51:00 +02001294 struct vlan_group *vlgrp = tp->vlgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int ret;
1296
Francois Romieu865c6522008-05-11 14:51:00 +02001297 if (vlgrp && (opts2 & RxVlanTag)) {
Eric Dumazet2edae082010-09-06 18:46:39 +00001298 u16 vtag = swab16(opts2 & 0xffff);
1299
1300 if (likely(polling))
1301 vlan_gro_receive(&tp->napi, vlgrp, vtag, skb);
1302 else
1303 __vlan_hwaccel_rx(skb, vlgrp, vtag, polling);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 ret = 0;
1305 } else
1306 ret = -1;
1307 desc->opts2 = 0;
1308 return ret;
1309}
1310
1311#else /* !CONFIG_R8169_VLAN */
1312
1313static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1314 struct sk_buff *skb)
1315{
1316 return 0;
1317}
1318
1319static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
Eric Dumazet630b9432010-03-31 02:08:31 +00001320 struct sk_buff *skb, int polling)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 return -1;
1323}
1324
1325#endif
1326
Francois Romieuccdffb92008-07-26 14:26:06 +02001327static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct rtl8169_private *tp = netdev_priv(dev);
1330 void __iomem *ioaddr = tp->mmio_addr;
1331 u32 status;
1332
1333 cmd->supported =
1334 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1335 cmd->port = PORT_FIBRE;
1336 cmd->transceiver = XCVR_INTERNAL;
1337
1338 status = RTL_R32(TBICSR);
1339 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1340 cmd->autoneg = !!(status & TBINwEnable);
1341
1342 cmd->speed = SPEED_1000;
1343 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001344
1345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Francois Romieuccdffb92008-07-26 14:26:06 +02001348static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Francois Romieuccdffb92008-07-26 14:26:06 +02001352 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1356{
1357 struct rtl8169_private *tp = netdev_priv(dev);
1358 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001359 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 spin_lock_irqsave(&tp->lock, flags);
1362
Francois Romieuccdffb92008-07-26 14:26:06 +02001363 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001366 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1370 void *p)
1371{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001372 struct rtl8169_private *tp = netdev_priv(dev);
1373 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Francois Romieu5b0384f2006-08-16 16:00:01 +02001375 if (regs->len > R8169_REGS_SIZE)
1376 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Francois Romieu5b0384f2006-08-16 16:00:01 +02001378 spin_lock_irqsave(&tp->lock, flags);
1379 memcpy_fromio(p, tp->mmio_addr, regs->len);
1380 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001383static u32 rtl8169_get_msglevel(struct net_device *dev)
1384{
1385 struct rtl8169_private *tp = netdev_priv(dev);
1386
1387 return tp->msg_enable;
1388}
1389
1390static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1391{
1392 struct rtl8169_private *tp = netdev_priv(dev);
1393
1394 tp->msg_enable = value;
1395}
1396
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001397static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1398 "tx_packets",
1399 "rx_packets",
1400 "tx_errors",
1401 "rx_errors",
1402 "rx_missed",
1403 "align_errors",
1404 "tx_single_collisions",
1405 "tx_multi_collisions",
1406 "unicast",
1407 "broadcast",
1408 "multicast",
1409 "tx_aborted",
1410 "tx_underrun",
1411};
1412
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001413static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001414{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001415 switch (sset) {
1416 case ETH_SS_STATS:
1417 return ARRAY_SIZE(rtl8169_gstrings);
1418 default:
1419 return -EOPNOTSUPP;
1420 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001421}
1422
Ivan Vecera355423d2009-02-06 21:49:57 -08001423static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001424{
1425 struct rtl8169_private *tp = netdev_priv(dev);
1426 void __iomem *ioaddr = tp->mmio_addr;
1427 struct rtl8169_counters *counters;
1428 dma_addr_t paddr;
1429 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001430 int wait = 1000;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001431 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001432
Ivan Vecera355423d2009-02-06 21:49:57 -08001433 /*
1434 * Some chips are unable to dump tally counters when the receiver
1435 * is disabled.
1436 */
1437 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1438 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001439
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001440 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001441 if (!counters)
1442 return;
1443
1444 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001445 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001446 RTL_W32(CounterAddrLow, cmd);
1447 RTL_W32(CounterAddrLow, cmd | CounterDump);
1448
Ivan Vecera355423d2009-02-06 21:49:57 -08001449 while (wait--) {
1450 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1451 /* copy updated counters */
1452 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001453 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001454 }
1455 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001456 }
1457
1458 RTL_W32(CounterAddrLow, 0);
1459 RTL_W32(CounterAddrHigh, 0);
1460
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001461 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001462}
1463
Ivan Vecera355423d2009-02-06 21:49:57 -08001464static void rtl8169_get_ethtool_stats(struct net_device *dev,
1465 struct ethtool_stats *stats, u64 *data)
1466{
1467 struct rtl8169_private *tp = netdev_priv(dev);
1468
1469 ASSERT_RTNL();
1470
1471 rtl8169_update_counters(dev);
1472
1473 data[0] = le64_to_cpu(tp->counters.tx_packets);
1474 data[1] = le64_to_cpu(tp->counters.rx_packets);
1475 data[2] = le64_to_cpu(tp->counters.tx_errors);
1476 data[3] = le32_to_cpu(tp->counters.rx_errors);
1477 data[4] = le16_to_cpu(tp->counters.rx_missed);
1478 data[5] = le16_to_cpu(tp->counters.align_errors);
1479 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1480 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1481 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1482 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1483 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1484 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1485 data[12] = le16_to_cpu(tp->counters.tx_underun);
1486}
1487
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001488static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1489{
1490 switch(stringset) {
1491 case ETH_SS_STATS:
1492 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1493 break;
1494 }
1495}
1496
Jeff Garzik7282d492006-09-13 14:30:00 -04001497static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 .get_drvinfo = rtl8169_get_drvinfo,
1499 .get_regs_len = rtl8169_get_regs_len,
1500 .get_link = ethtool_op_get_link,
1501 .get_settings = rtl8169_get_settings,
1502 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001503 .get_msglevel = rtl8169_get_msglevel,
1504 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 .get_rx_csum = rtl8169_get_rx_csum,
1506 .set_rx_csum = rtl8169_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 .set_tx_csum = ethtool_op_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 .set_tso = ethtool_op_set_tso,
1510 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001511 .get_wol = rtl8169_get_wol,
1512 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001513 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001514 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001515 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516};
1517
Francois Romieu07d3f512007-02-21 22:40:46 +01001518static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1519 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Francois Romieu0e485152007-02-20 00:00:26 +01001521 /*
1522 * The driver currently handles the 8168Bf and the 8168Be identically
1523 * but they can be identified more specifically through the test below
1524 * if needed:
1525 *
1526 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001527 *
1528 * Same thing for the 8101Eb and the 8101Ec:
1529 *
1530 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001531 */
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001532 static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001534 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int mac_version;
1536 } mac_info[] = {
Francois Romieu5b538df2008-07-20 16:22:45 +02001537 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001538 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1539 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001540 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001541
françois romieue6de30d2011-01-03 15:08:37 +00001542 /* 8168DP family. */
1543 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1544 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1545
Francois Romieuef808d52008-06-29 13:10:54 +02001546 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001547 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001548 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001549 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001550 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001551 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1552 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001553 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001554 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001555 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001556
1557 /* 8168B family. */
1558 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1559 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1560 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1561 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1562
1563 /* 8101 family. */
Hayes Wang5a5e4442011-02-22 17:26:21 +08001564 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1565 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1566 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001567 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1568 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1569 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1570 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1571 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1572 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001573 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001574 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001575 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001576 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1577 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001578 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1579 /* FIXME: where did these entries come from ? -- FR */
1580 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1581 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1582
1583 /* 8110 family. */
1584 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1585 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1586 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1587 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1588 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1589 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1590
Jean Delvaref21b75e2009-05-26 20:54:48 -07001591 /* Catch-all */
1592 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }, *p = mac_info;
1594 u32 reg;
1595
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001596 reg = RTL_R32(TxConfig);
1597 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 p++;
1599 tp->mac_version = p->mac_version;
1600}
1601
1602static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1603{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001604 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
Francois Romieu867763c2007-08-17 18:21:58 +02001607struct phy_reg {
1608 u16 reg;
1609 u16 val;
1610};
1611
françois romieu4da19632011-01-03 15:07:55 +00001612static void rtl_writephy_batch(struct rtl8169_private *tp,
1613 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001614{
1615 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001616 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001617 regs++;
1618 }
1619}
1620
françois romieubca03d52011-01-03 15:07:31 +00001621#define PHY_READ 0x00000000
1622#define PHY_DATA_OR 0x10000000
1623#define PHY_DATA_AND 0x20000000
1624#define PHY_BJMPN 0x30000000
1625#define PHY_READ_EFUSE 0x40000000
1626#define PHY_READ_MAC_BYTE 0x50000000
1627#define PHY_WRITE_MAC_BYTE 0x60000000
1628#define PHY_CLEAR_READCOUNT 0x70000000
1629#define PHY_WRITE 0x80000000
1630#define PHY_READCOUNT_EQ_SKIP 0x90000000
1631#define PHY_COMP_EQ_SKIPN 0xa0000000
1632#define PHY_COMP_NEQ_SKIPN 0xb0000000
1633#define PHY_WRITE_PREVIOUS 0xc0000000
1634#define PHY_SKIPN 0xd0000000
1635#define PHY_DELAY_MS 0xe0000000
1636#define PHY_WRITE_ERI_WORD 0xf0000000
1637
1638static void
1639rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1640{
françois romieubca03d52011-01-03 15:07:31 +00001641 __le32 *phytable = (__le32 *)fw->data;
1642 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001643 size_t index, fw_size = fw->size / sizeof(*phytable);
1644 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001645
1646 if (fw->size % sizeof(*phytable)) {
1647 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1648 return;
1649 }
1650
hayeswang42b82dc2011-01-10 02:07:25 +00001651 for (index = 0; index < fw_size; index++) {
1652 u32 action = le32_to_cpu(phytable[index]);
1653 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001654
hayeswang42b82dc2011-01-10 02:07:25 +00001655 switch(action & 0xf0000000) {
1656 case PHY_READ:
1657 case PHY_DATA_OR:
1658 case PHY_DATA_AND:
1659 case PHY_READ_EFUSE:
1660 case PHY_CLEAR_READCOUNT:
1661 case PHY_WRITE:
1662 case PHY_WRITE_PREVIOUS:
1663 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001664 break;
1665
hayeswang42b82dc2011-01-10 02:07:25 +00001666 case PHY_BJMPN:
1667 if (regno > index) {
1668 netif_err(tp, probe, tp->dev,
1669 "Out of range of firmware\n");
1670 return;
1671 }
1672 break;
1673 case PHY_READCOUNT_EQ_SKIP:
1674 if (index + 2 >= fw_size) {
1675 netif_err(tp, probe, tp->dev,
1676 "Out of range of firmware\n");
1677 return;
1678 }
1679 break;
1680 case PHY_COMP_EQ_SKIPN:
1681 case PHY_COMP_NEQ_SKIPN:
1682 case PHY_SKIPN:
1683 if (index + 1 + regno >= fw_size) {
1684 netif_err(tp, probe, tp->dev,
1685 "Out of range of firmware\n");
1686 return;
1687 }
1688 break;
1689
1690 case PHY_READ_MAC_BYTE:
1691 case PHY_WRITE_MAC_BYTE:
1692 case PHY_WRITE_ERI_WORD:
1693 default:
1694 netif_err(tp, probe, tp->dev,
1695 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001696 return;
1697 }
1698 }
1699
hayeswang42b82dc2011-01-10 02:07:25 +00001700 predata = 0;
1701 count = 0;
1702
1703 for (index = 0; index < fw_size; ) {
1704 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001705 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001706 u32 regno = (action & 0x0fff0000) >> 16;
1707
1708 if (!action)
1709 break;
françois romieubca03d52011-01-03 15:07:31 +00001710
1711 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001712 case PHY_READ:
1713 predata = rtl_readphy(tp, regno);
1714 count++;
1715 index++;
françois romieubca03d52011-01-03 15:07:31 +00001716 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001717 case PHY_DATA_OR:
1718 predata |= data;
1719 index++;
1720 break;
1721 case PHY_DATA_AND:
1722 predata &= data;
1723 index++;
1724 break;
1725 case PHY_BJMPN:
1726 index -= regno;
1727 break;
1728 case PHY_READ_EFUSE:
1729 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1730 index++;
1731 break;
1732 case PHY_CLEAR_READCOUNT:
1733 count = 0;
1734 index++;
1735 break;
1736 case PHY_WRITE:
1737 rtl_writephy(tp, regno, data);
1738 index++;
1739 break;
1740 case PHY_READCOUNT_EQ_SKIP:
1741 if (count == data)
1742 index += 2;
1743 else
1744 index += 1;
1745 break;
1746 case PHY_COMP_EQ_SKIPN:
1747 if (predata == data)
1748 index += regno;
1749 index++;
1750 break;
1751 case PHY_COMP_NEQ_SKIPN:
1752 if (predata != data)
1753 index += regno;
1754 index++;
1755 break;
1756 case PHY_WRITE_PREVIOUS:
1757 rtl_writephy(tp, regno, predata);
1758 index++;
1759 break;
1760 case PHY_SKIPN:
1761 index += regno + 1;
1762 break;
1763 case PHY_DELAY_MS:
1764 mdelay(data);
1765 index++;
1766 break;
1767
1768 case PHY_READ_MAC_BYTE:
1769 case PHY_WRITE_MAC_BYTE:
1770 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001771 default:
1772 BUG();
1773 }
1774 }
1775}
1776
françois romieuf1e02ed2011-01-13 13:07:53 +00001777static void rtl_release_firmware(struct rtl8169_private *tp)
1778{
1779 release_firmware(tp->fw);
1780 tp->fw = NULL;
1781}
1782
1783static int rtl_apply_firmware(struct rtl8169_private *tp, const char *fw_name)
1784{
1785 const struct firmware **fw = &tp->fw;
1786 int rc = !*fw;
1787
1788 if (rc) {
1789 rc = request_firmware(fw, fw_name, &tp->pci_dev->dev);
1790 if (rc < 0)
1791 goto out;
1792 }
1793
1794 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1795 rtl_phy_write_fw(tp, *fw);
1796out:
1797 return rc;
1798}
1799
françois romieu4da19632011-01-03 15:07:55 +00001800static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001802 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001803 { 0x1f, 0x0001 },
1804 { 0x06, 0x006e },
1805 { 0x08, 0x0708 },
1806 { 0x15, 0x4000 },
1807 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
françois romieu0b9b5712009-08-10 19:44:56 +00001809 { 0x1f, 0x0001 },
1810 { 0x03, 0x00a1 },
1811 { 0x02, 0x0008 },
1812 { 0x01, 0x0120 },
1813 { 0x00, 0x1000 },
1814 { 0x04, 0x0800 },
1815 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
françois romieu0b9b5712009-08-10 19:44:56 +00001817 { 0x03, 0xff41 },
1818 { 0x02, 0xdf60 },
1819 { 0x01, 0x0140 },
1820 { 0x00, 0x0077 },
1821 { 0x04, 0x7800 },
1822 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
françois romieu0b9b5712009-08-10 19:44:56 +00001824 { 0x03, 0x802f },
1825 { 0x02, 0x4f02 },
1826 { 0x01, 0x0409 },
1827 { 0x00, 0xf0f9 },
1828 { 0x04, 0x9800 },
1829 { 0x04, 0x9000 },
1830
1831 { 0x03, 0xdf01 },
1832 { 0x02, 0xdf20 },
1833 { 0x01, 0xff95 },
1834 { 0x00, 0xba00 },
1835 { 0x04, 0xa800 },
1836 { 0x04, 0xa000 },
1837
1838 { 0x03, 0xff41 },
1839 { 0x02, 0xdf20 },
1840 { 0x01, 0x0140 },
1841 { 0x00, 0x00bb },
1842 { 0x04, 0xb800 },
1843 { 0x04, 0xb000 },
1844
1845 { 0x03, 0xdf41 },
1846 { 0x02, 0xdc60 },
1847 { 0x01, 0x6340 },
1848 { 0x00, 0x007d },
1849 { 0x04, 0xd800 },
1850 { 0x04, 0xd000 },
1851
1852 { 0x03, 0xdf01 },
1853 { 0x02, 0xdf20 },
1854 { 0x01, 0x100a },
1855 { 0x00, 0xa0ff },
1856 { 0x04, 0xf800 },
1857 { 0x04, 0xf000 },
1858
1859 { 0x1f, 0x0000 },
1860 { 0x0b, 0x0000 },
1861 { 0x00, 0x9200 }
1862 };
1863
françois romieu4da19632011-01-03 15:07:55 +00001864 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
françois romieu4da19632011-01-03 15:07:55 +00001867static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001868{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001869 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001870 { 0x1f, 0x0002 },
1871 { 0x01, 0x90d0 },
1872 { 0x1f, 0x0000 }
1873 };
1874
françois romieu4da19632011-01-03 15:07:55 +00001875 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001876}
1877
françois romieu4da19632011-01-03 15:07:55 +00001878static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001879{
1880 struct pci_dev *pdev = tp->pci_dev;
1881 u16 vendor_id, device_id;
1882
1883 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1884 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1885
1886 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1887 return;
1888
françois romieu4da19632011-01-03 15:07:55 +00001889 rtl_writephy(tp, 0x1f, 0x0001);
1890 rtl_writephy(tp, 0x10, 0xf01b);
1891 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001892}
1893
françois romieu4da19632011-01-03 15:07:55 +00001894static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001895{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001896 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00001897 { 0x1f, 0x0001 },
1898 { 0x04, 0x0000 },
1899 { 0x03, 0x00a1 },
1900 { 0x02, 0x0008 },
1901 { 0x01, 0x0120 },
1902 { 0x00, 0x1000 },
1903 { 0x04, 0x0800 },
1904 { 0x04, 0x9000 },
1905 { 0x03, 0x802f },
1906 { 0x02, 0x4f02 },
1907 { 0x01, 0x0409 },
1908 { 0x00, 0xf099 },
1909 { 0x04, 0x9800 },
1910 { 0x04, 0xa000 },
1911 { 0x03, 0xdf01 },
1912 { 0x02, 0xdf20 },
1913 { 0x01, 0xff95 },
1914 { 0x00, 0xba00 },
1915 { 0x04, 0xa800 },
1916 { 0x04, 0xf000 },
1917 { 0x03, 0xdf01 },
1918 { 0x02, 0xdf20 },
1919 { 0x01, 0x101a },
1920 { 0x00, 0xa0ff },
1921 { 0x04, 0xf800 },
1922 { 0x04, 0x0000 },
1923 { 0x1f, 0x0000 },
1924
1925 { 0x1f, 0x0001 },
1926 { 0x10, 0xf41b },
1927 { 0x14, 0xfb54 },
1928 { 0x18, 0xf5c7 },
1929 { 0x1f, 0x0000 },
1930
1931 { 0x1f, 0x0001 },
1932 { 0x17, 0x0cc0 },
1933 { 0x1f, 0x0000 }
1934 };
1935
françois romieu4da19632011-01-03 15:07:55 +00001936 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00001937
françois romieu4da19632011-01-03 15:07:55 +00001938 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00001939}
1940
françois romieu4da19632011-01-03 15:07:55 +00001941static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00001942{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001943 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00001944 { 0x1f, 0x0001 },
1945 { 0x04, 0x0000 },
1946 { 0x03, 0x00a1 },
1947 { 0x02, 0x0008 },
1948 { 0x01, 0x0120 },
1949 { 0x00, 0x1000 },
1950 { 0x04, 0x0800 },
1951 { 0x04, 0x9000 },
1952 { 0x03, 0x802f },
1953 { 0x02, 0x4f02 },
1954 { 0x01, 0x0409 },
1955 { 0x00, 0xf099 },
1956 { 0x04, 0x9800 },
1957 { 0x04, 0xa000 },
1958 { 0x03, 0xdf01 },
1959 { 0x02, 0xdf20 },
1960 { 0x01, 0xff95 },
1961 { 0x00, 0xba00 },
1962 { 0x04, 0xa800 },
1963 { 0x04, 0xf000 },
1964 { 0x03, 0xdf01 },
1965 { 0x02, 0xdf20 },
1966 { 0x01, 0x101a },
1967 { 0x00, 0xa0ff },
1968 { 0x04, 0xf800 },
1969 { 0x04, 0x0000 },
1970 { 0x1f, 0x0000 },
1971
1972 { 0x1f, 0x0001 },
1973 { 0x0b, 0x8480 },
1974 { 0x1f, 0x0000 },
1975
1976 { 0x1f, 0x0001 },
1977 { 0x18, 0x67c7 },
1978 { 0x04, 0x2000 },
1979 { 0x03, 0x002f },
1980 { 0x02, 0x4360 },
1981 { 0x01, 0x0109 },
1982 { 0x00, 0x3022 },
1983 { 0x04, 0x2800 },
1984 { 0x1f, 0x0000 },
1985
1986 { 0x1f, 0x0001 },
1987 { 0x17, 0x0cc0 },
1988 { 0x1f, 0x0000 }
1989 };
1990
françois romieu4da19632011-01-03 15:07:55 +00001991 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00001992}
1993
françois romieu4da19632011-01-03 15:07:55 +00001994static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02001995{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001996 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02001997 { 0x10, 0xf41b },
1998 { 0x1f, 0x0000 }
1999 };
2000
françois romieu4da19632011-01-03 15:07:55 +00002001 rtl_writephy(tp, 0x1f, 0x0001);
2002 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002003
françois romieu4da19632011-01-03 15:07:55 +00002004 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002005}
2006
françois romieu4da19632011-01-03 15:07:55 +00002007static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002008{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002009 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002010 { 0x1f, 0x0001 },
2011 { 0x10, 0xf41b },
2012 { 0x1f, 0x0000 }
2013 };
2014
françois romieu4da19632011-01-03 15:07:55 +00002015 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002016}
2017
françois romieu4da19632011-01-03 15:07:55 +00002018static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002019{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002020 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002021 { 0x1f, 0x0000 },
2022 { 0x1d, 0x0f00 },
2023 { 0x1f, 0x0002 },
2024 { 0x0c, 0x1ec8 },
2025 { 0x1f, 0x0000 }
2026 };
2027
françois romieu4da19632011-01-03 15:07:55 +00002028 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002029}
2030
françois romieu4da19632011-01-03 15:07:55 +00002031static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002032{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002033 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002034 { 0x1f, 0x0001 },
2035 { 0x1d, 0x3d98 },
2036 { 0x1f, 0x0000 }
2037 };
2038
françois romieu4da19632011-01-03 15:07:55 +00002039 rtl_writephy(tp, 0x1f, 0x0000);
2040 rtl_patchphy(tp, 0x14, 1 << 5);
2041 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002042
françois romieu4da19632011-01-03 15:07:55 +00002043 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002044}
2045
françois romieu4da19632011-01-03 15:07:55 +00002046static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002047{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002048 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002049 { 0x1f, 0x0001 },
2050 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002051 { 0x1f, 0x0002 },
2052 { 0x00, 0x88d4 },
2053 { 0x01, 0x82b1 },
2054 { 0x03, 0x7002 },
2055 { 0x08, 0x9e30 },
2056 { 0x09, 0x01f0 },
2057 { 0x0a, 0x5500 },
2058 { 0x0c, 0x00c8 },
2059 { 0x1f, 0x0003 },
2060 { 0x12, 0xc096 },
2061 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002062 { 0x1f, 0x0000 },
2063 { 0x1f, 0x0000 },
2064 { 0x09, 0x2000 },
2065 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002066 };
2067
françois romieu4da19632011-01-03 15:07:55 +00002068 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002069
françois romieu4da19632011-01-03 15:07:55 +00002070 rtl_patchphy(tp, 0x14, 1 << 5);
2071 rtl_patchphy(tp, 0x0d, 1 << 5);
2072 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002073}
2074
françois romieu4da19632011-01-03 15:07:55 +00002075static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002076{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002077 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002078 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002079 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002080 { 0x03, 0x802f },
2081 { 0x02, 0x4f02 },
2082 { 0x01, 0x0409 },
2083 { 0x00, 0xf099 },
2084 { 0x04, 0x9800 },
2085 { 0x04, 0x9000 },
2086 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002087 { 0x1f, 0x0002 },
2088 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002089 { 0x06, 0x0761 },
2090 { 0x1f, 0x0003 },
2091 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002092 { 0x1f, 0x0000 }
2093 };
2094
françois romieu4da19632011-01-03 15:07:55 +00002095 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002096
françois romieu4da19632011-01-03 15:07:55 +00002097 rtl_patchphy(tp, 0x16, 1 << 0);
2098 rtl_patchphy(tp, 0x14, 1 << 5);
2099 rtl_patchphy(tp, 0x0d, 1 << 5);
2100 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002101}
2102
françois romieu4da19632011-01-03 15:07:55 +00002103static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002104{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002105 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002106 { 0x1f, 0x0001 },
2107 { 0x12, 0x2300 },
2108 { 0x1d, 0x3d98 },
2109 { 0x1f, 0x0002 },
2110 { 0x0c, 0x7eb8 },
2111 { 0x06, 0x5461 },
2112 { 0x1f, 0x0003 },
2113 { 0x16, 0x0f0a },
2114 { 0x1f, 0x0000 }
2115 };
2116
françois romieu4da19632011-01-03 15:07:55 +00002117 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002118
françois romieu4da19632011-01-03 15:07:55 +00002119 rtl_patchphy(tp, 0x16, 1 << 0);
2120 rtl_patchphy(tp, 0x14, 1 << 5);
2121 rtl_patchphy(tp, 0x0d, 1 << 5);
2122 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002123}
2124
françois romieu4da19632011-01-03 15:07:55 +00002125static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002126{
françois romieu4da19632011-01-03 15:07:55 +00002127 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002128}
2129
françois romieubca03d52011-01-03 15:07:31 +00002130static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002131{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002132 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002133 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002134 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002135 { 0x06, 0x4064 },
2136 { 0x07, 0x2863 },
2137 { 0x08, 0x059c },
2138 { 0x09, 0x26b4 },
2139 { 0x0a, 0x6a19 },
2140 { 0x0b, 0xdcc8 },
2141 { 0x10, 0xf06d },
2142 { 0x14, 0x7f68 },
2143 { 0x18, 0x7fd9 },
2144 { 0x1c, 0xf0ff },
2145 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002146 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002147 { 0x12, 0xf49f },
2148 { 0x13, 0x070b },
2149 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002150 { 0x14, 0x94c0 },
2151
2152 /*
2153 * Tx Error Issue
2154 * enhance line driver power
2155 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002156 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002157 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002158 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002159 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002160 { 0x06, 0x5561 },
2161
2162 /*
2163 * Can not link to 1Gbps with bad cable
2164 * Decrease SNR threshold form 21.07dB to 19.04dB
2165 */
2166 { 0x1f, 0x0001 },
2167 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002168
2169 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002170 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002171 };
françois romieubca03d52011-01-03 15:07:31 +00002172 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002173
françois romieu4da19632011-01-03 15:07:55 +00002174 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002175
françois romieubca03d52011-01-03 15:07:31 +00002176 /*
2177 * Rx Error Issue
2178 * Fine Tune Switching regulator parameter
2179 */
françois romieu4da19632011-01-03 15:07:55 +00002180 rtl_writephy(tp, 0x1f, 0x0002);
2181 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2182 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002183
françois romieudaf9df62009-10-07 12:44:20 +00002184 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002185 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002186 { 0x1f, 0x0002 },
2187 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002188 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002189 { 0x05, 0x8330 },
2190 { 0x06, 0x669a },
2191 { 0x1f, 0x0002 }
2192 };
2193 int val;
2194
françois romieu4da19632011-01-03 15:07:55 +00002195 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002196
françois romieu4da19632011-01-03 15:07:55 +00002197 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002198
2199 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002200 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002201 0x0065, 0x0066, 0x0067, 0x0068,
2202 0x0069, 0x006a, 0x006b, 0x006c
2203 };
2204 int i;
2205
françois romieu4da19632011-01-03 15:07:55 +00002206 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002207
2208 val &= 0xff00;
2209 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002210 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002211 }
2212 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002213 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002214 { 0x1f, 0x0002 },
2215 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002216 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002217 { 0x05, 0x8330 },
2218 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002219 };
2220
françois romieu4da19632011-01-03 15:07:55 +00002221 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002222 }
2223
françois romieubca03d52011-01-03 15:07:31 +00002224 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002225 rtl_writephy(tp, 0x1f, 0x0002);
2226 rtl_patchphy(tp, 0x0d, 0x0300);
2227 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002228
françois romieubca03d52011-01-03 15:07:31 +00002229 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002230 rtl_writephy(tp, 0x1f, 0x0002);
2231 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2232 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002233
françois romieu4da19632011-01-03 15:07:55 +00002234 rtl_writephy(tp, 0x1f, 0x0005);
2235 rtl_writephy(tp, 0x05, 0x001b);
françois romieuf1e02ed2011-01-13 13:07:53 +00002236 if ((rtl_readphy(tp, 0x06) != 0xbf00) ||
2237 (rtl_apply_firmware(tp, FIRMWARE_8168D_1) < 0)) {
françois romieubca03d52011-01-03 15:07:31 +00002238 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2239 }
2240
françois romieu4da19632011-01-03 15:07:55 +00002241 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002242}
2243
françois romieubca03d52011-01-03 15:07:31 +00002244static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002245{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002246 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002247 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002248 { 0x1f, 0x0001 },
2249 { 0x06, 0x4064 },
2250 { 0x07, 0x2863 },
2251 { 0x08, 0x059c },
2252 { 0x09, 0x26b4 },
2253 { 0x0a, 0x6a19 },
2254 { 0x0b, 0xdcc8 },
2255 { 0x10, 0xf06d },
2256 { 0x14, 0x7f68 },
2257 { 0x18, 0x7fd9 },
2258 { 0x1c, 0xf0ff },
2259 { 0x1d, 0x3d9c },
2260 { 0x1f, 0x0003 },
2261 { 0x12, 0xf49f },
2262 { 0x13, 0x070b },
2263 { 0x1a, 0x05ad },
2264 { 0x14, 0x94c0 },
2265
françois romieubca03d52011-01-03 15:07:31 +00002266 /*
2267 * Tx Error Issue
2268 * enhance line driver power
2269 */
françois romieudaf9df62009-10-07 12:44:20 +00002270 { 0x1f, 0x0002 },
2271 { 0x06, 0x5561 },
2272 { 0x1f, 0x0005 },
2273 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002274 { 0x06, 0x5561 },
2275
2276 /*
2277 * Can not link to 1Gbps with bad cable
2278 * Decrease SNR threshold form 21.07dB to 19.04dB
2279 */
2280 { 0x1f, 0x0001 },
2281 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002282
2283 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002284 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002285 };
françois romieubca03d52011-01-03 15:07:31 +00002286 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002287
françois romieu4da19632011-01-03 15:07:55 +00002288 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002289
2290 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002291 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002292 { 0x1f, 0x0002 },
2293 { 0x05, 0x669a },
2294 { 0x1f, 0x0005 },
2295 { 0x05, 0x8330 },
2296 { 0x06, 0x669a },
2297
2298 { 0x1f, 0x0002 }
2299 };
2300 int val;
2301
françois romieu4da19632011-01-03 15:07:55 +00002302 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002303
françois romieu4da19632011-01-03 15:07:55 +00002304 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002305 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002306 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002307 0x0065, 0x0066, 0x0067, 0x0068,
2308 0x0069, 0x006a, 0x006b, 0x006c
2309 };
2310 int i;
2311
françois romieu4da19632011-01-03 15:07:55 +00002312 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002313
2314 val &= 0xff00;
2315 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002316 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002317 }
2318 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002319 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002320 { 0x1f, 0x0002 },
2321 { 0x05, 0x2642 },
2322 { 0x1f, 0x0005 },
2323 { 0x05, 0x8330 },
2324 { 0x06, 0x2642 }
2325 };
2326
françois romieu4da19632011-01-03 15:07:55 +00002327 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002328 }
2329
françois romieubca03d52011-01-03 15:07:31 +00002330 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002331 rtl_writephy(tp, 0x1f, 0x0002);
2332 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2333 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002334
françois romieubca03d52011-01-03 15:07:31 +00002335 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002336 rtl_writephy(tp, 0x1f, 0x0002);
2337 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002338
françois romieu4da19632011-01-03 15:07:55 +00002339 rtl_writephy(tp, 0x1f, 0x0005);
2340 rtl_writephy(tp, 0x05, 0x001b);
françois romieuf1e02ed2011-01-13 13:07:53 +00002341 if ((rtl_readphy(tp, 0x06) != 0xb300) ||
2342 (rtl_apply_firmware(tp, FIRMWARE_8168D_2) < 0)) {
françois romieubca03d52011-01-03 15:07:31 +00002343 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2344 }
2345
françois romieu4da19632011-01-03 15:07:55 +00002346 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002347}
2348
françois romieu4da19632011-01-03 15:07:55 +00002349static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002350{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002351 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002352 { 0x1f, 0x0002 },
2353 { 0x10, 0x0008 },
2354 { 0x0d, 0x006c },
2355
2356 { 0x1f, 0x0000 },
2357 { 0x0d, 0xf880 },
2358
2359 { 0x1f, 0x0001 },
2360 { 0x17, 0x0cc0 },
2361
2362 { 0x1f, 0x0001 },
2363 { 0x0b, 0xa4d8 },
2364 { 0x09, 0x281c },
2365 { 0x07, 0x2883 },
2366 { 0x0a, 0x6b35 },
2367 { 0x1d, 0x3da4 },
2368 { 0x1c, 0xeffd },
2369 { 0x14, 0x7f52 },
2370 { 0x18, 0x7fc6 },
2371 { 0x08, 0x0601 },
2372 { 0x06, 0x4063 },
2373 { 0x10, 0xf074 },
2374 { 0x1f, 0x0003 },
2375 { 0x13, 0x0789 },
2376 { 0x12, 0xf4bd },
2377 { 0x1a, 0x04fd },
2378 { 0x14, 0x84b0 },
2379 { 0x1f, 0x0000 },
2380 { 0x00, 0x9200 },
2381
2382 { 0x1f, 0x0005 },
2383 { 0x01, 0x0340 },
2384 { 0x1f, 0x0001 },
2385 { 0x04, 0x4000 },
2386 { 0x03, 0x1d21 },
2387 { 0x02, 0x0c32 },
2388 { 0x01, 0x0200 },
2389 { 0x00, 0x5554 },
2390 { 0x04, 0x4800 },
2391 { 0x04, 0x4000 },
2392 { 0x04, 0xf000 },
2393 { 0x03, 0xdf01 },
2394 { 0x02, 0xdf20 },
2395 { 0x01, 0x101a },
2396 { 0x00, 0xa0ff },
2397 { 0x04, 0xf800 },
2398 { 0x04, 0xf000 },
2399 { 0x1f, 0x0000 },
2400
2401 { 0x1f, 0x0007 },
2402 { 0x1e, 0x0023 },
2403 { 0x16, 0x0000 },
2404 { 0x1f, 0x0000 }
2405 };
2406
françois romieu4da19632011-01-03 15:07:55 +00002407 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002408}
2409
françois romieue6de30d2011-01-03 15:08:37 +00002410static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2411{
2412 static const struct phy_reg phy_reg_init[] = {
2413 { 0x1f, 0x0001 },
2414 { 0x17, 0x0cc0 },
2415
2416 { 0x1f, 0x0007 },
2417 { 0x1e, 0x002d },
2418 { 0x18, 0x0040 },
2419 { 0x1f, 0x0000 }
2420 };
2421
2422 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2423 rtl_patchphy(tp, 0x0d, 1 << 5);
2424}
2425
françois romieu4da19632011-01-03 15:07:55 +00002426static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002427{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002428 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002429 { 0x1f, 0x0003 },
2430 { 0x08, 0x441d },
2431 { 0x01, 0x9100 },
2432 { 0x1f, 0x0000 }
2433 };
2434
françois romieu4da19632011-01-03 15:07:55 +00002435 rtl_writephy(tp, 0x1f, 0x0000);
2436 rtl_patchphy(tp, 0x11, 1 << 12);
2437 rtl_patchphy(tp, 0x19, 1 << 13);
2438 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002439
françois romieu4da19632011-01-03 15:07:55 +00002440 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002441}
2442
Hayes Wang5a5e4442011-02-22 17:26:21 +08002443static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2444{
2445 static const struct phy_reg phy_reg_init[] = {
2446 { 0x1f, 0x0005 },
2447 { 0x1a, 0x0000 },
2448 { 0x1f, 0x0000 },
2449
2450 { 0x1f, 0x0004 },
2451 { 0x1c, 0x0000 },
2452 { 0x1f, 0x0000 },
2453
2454 { 0x1f, 0x0001 },
2455 { 0x15, 0x7701 },
2456 { 0x1f, 0x0000 }
2457 };
2458
2459 /* Disable ALDPS before ram code */
2460 rtl_writephy(tp, 0x1f, 0x0000);
2461 rtl_writephy(tp, 0x18, 0x0310);
2462 msleep(100);
2463
2464 if (rtl_apply_firmware(tp, FIRMWARE_8105E_1) < 0)
2465 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2466
2467 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2468}
2469
Francois Romieu5615d9f2007-08-17 17:50:46 +02002470static void rtl_hw_phy_config(struct net_device *dev)
2471{
2472 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002473
2474 rtl8169_print_mac_version(tp);
2475
2476 switch (tp->mac_version) {
2477 case RTL_GIGA_MAC_VER_01:
2478 break;
2479 case RTL_GIGA_MAC_VER_02:
2480 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002481 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002482 break;
2483 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002484 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002485 break;
françois romieu2e9558562009-08-10 19:44:19 +00002486 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002487 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002488 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002489 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002490 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002491 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002492 case RTL_GIGA_MAC_VER_07:
2493 case RTL_GIGA_MAC_VER_08:
2494 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002495 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002496 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002497 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002498 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002499 break;
2500 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002501 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002502 break;
2503 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002504 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002505 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002506 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002507 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002508 break;
2509 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002510 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002511 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002512 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002513 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002514 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002515 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002516 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002517 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002518 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002519 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002520 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002521 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002522 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002523 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002524 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002525 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002526 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002527 break;
2528 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002529 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002530 break;
2531 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002532 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002533 break;
françois romieue6de30d2011-01-03 15:08:37 +00002534 case RTL_GIGA_MAC_VER_28:
2535 rtl8168d_4_hw_phy_config(tp);
2536 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002537 case RTL_GIGA_MAC_VER_29:
2538 case RTL_GIGA_MAC_VER_30:
2539 rtl8105e_hw_phy_config(tp);
2540 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002541
Francois Romieu5615d9f2007-08-17 17:50:46 +02002542 default:
2543 break;
2544 }
2545}
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547static void rtl8169_phy_timer(unsigned long __opaque)
2548{
2549 struct net_device *dev = (struct net_device *)__opaque;
2550 struct rtl8169_private *tp = netdev_priv(dev);
2551 struct timer_list *timer = &tp->timer;
2552 void __iomem *ioaddr = tp->mmio_addr;
2553 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2554
Francois Romieubcf0bf92006-07-26 23:14:13 +02002555 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Francois Romieu64e4bfb2006-08-17 12:43:06 +02002557 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 return;
2559
2560 spin_lock_irq(&tp->lock);
2561
françois romieu4da19632011-01-03 15:07:55 +00002562 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002563 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 * A busy loop could burn quite a few cycles on nowadays CPU.
2565 * Let's delay the execution of the timer for a few ticks.
2566 */
2567 timeout = HZ/10;
2568 goto out_mod_timer;
2569 }
2570
2571 if (tp->link_ok(ioaddr))
2572 goto out_unlock;
2573
Joe Perchesbf82c182010-02-09 11:49:50 +00002574 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
françois romieu4da19632011-01-03 15:07:55 +00002576 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578out_mod_timer:
2579 mod_timer(timer, jiffies + timeout);
2580out_unlock:
2581 spin_unlock_irq(&tp->lock);
2582}
2583
2584static inline void rtl8169_delete_timer(struct net_device *dev)
2585{
2586 struct rtl8169_private *tp = netdev_priv(dev);
2587 struct timer_list *timer = &tp->timer;
2588
Francois Romieue179bb72007-08-17 15:05:21 +02002589 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return;
2591
2592 del_timer_sync(timer);
2593}
2594
2595static inline void rtl8169_request_timer(struct net_device *dev)
2596{
2597 struct rtl8169_private *tp = netdev_priv(dev);
2598 struct timer_list *timer = &tp->timer;
2599
Francois Romieue179bb72007-08-17 15:05:21 +02002600 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return;
2602
Francois Romieu2efa53f2007-03-09 00:00:05 +01002603 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
2606#ifdef CONFIG_NET_POLL_CONTROLLER
2607/*
2608 * Polling 'interrupt' - used by things like netconsole to send skbs
2609 * without having to re-enable interrupts. It's not called while
2610 * the interrupt routine is executing.
2611 */
2612static void rtl8169_netpoll(struct net_device *dev)
2613{
2614 struct rtl8169_private *tp = netdev_priv(dev);
2615 struct pci_dev *pdev = tp->pci_dev;
2616
2617 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002618 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 enable_irq(pdev->irq);
2620}
2621#endif
2622
2623static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2624 void __iomem *ioaddr)
2625{
2626 iounmap(ioaddr);
2627 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002628 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 pci_disable_device(pdev);
2630 free_netdev(dev);
2631}
2632
Francois Romieubf793292006-11-01 00:53:05 +01002633static void rtl8169_phy_reset(struct net_device *dev,
2634 struct rtl8169_private *tp)
2635{
Francois Romieu07d3f512007-02-21 22:40:46 +01002636 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002637
françois romieu4da19632011-01-03 15:07:55 +00002638 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002639 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002640 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002641 return;
2642 msleep(1);
2643 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002644 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002645}
2646
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002647static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002649 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002650
Francois Romieu5615d9f2007-08-17 17:50:46 +02002651 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002652
Marcus Sundberg773328942008-07-10 21:28:08 +02002653 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2654 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2655 RTL_W8(0x82, 0x01);
2656 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002657
Francois Romieu6dccd162007-02-13 23:38:05 +01002658 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2659
2660 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2661 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002662
Francois Romieubcf0bf92006-07-26 23:14:13 +02002663 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002664 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2665 RTL_W8(0x82, 0x01);
2666 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002668 }
2669
Francois Romieubf793292006-11-01 00:53:05 +01002670 rtl8169_phy_reset(dev, tp);
2671
Francois Romieu901dda22007-02-21 00:10:20 +01002672 /*
2673 * rtl8169_set_speed_xmii takes good care of the Fast Ethernet
2674 * only 8101. Don't panic.
2675 */
2676 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002677
Joe Perchesbf82c182010-02-09 11:49:50 +00002678 if (RTL_R8(PHYstatus) & TBI_Enable)
2679 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002680}
2681
Francois Romieu773d2022007-01-31 23:47:43 +01002682static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2683{
2684 void __iomem *ioaddr = tp->mmio_addr;
2685 u32 high;
2686 u32 low;
2687
2688 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2689 high = addr[4] | (addr[5] << 8);
2690
2691 spin_lock_irq(&tp->lock);
2692
2693 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002694
Francois Romieu773d2022007-01-31 23:47:43 +01002695 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002696 RTL_R32(MAC4);
2697
Francois Romieu78f1cd02010-03-27 19:35:46 -07002698 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002699 RTL_R32(MAC0);
2700
Francois Romieu773d2022007-01-31 23:47:43 +01002701 RTL_W8(Cfg9346, Cfg9346_Lock);
2702
2703 spin_unlock_irq(&tp->lock);
2704}
2705
2706static int rtl_set_mac_address(struct net_device *dev, void *p)
2707{
2708 struct rtl8169_private *tp = netdev_priv(dev);
2709 struct sockaddr *addr = p;
2710
2711 if (!is_valid_ether_addr(addr->sa_data))
2712 return -EADDRNOTAVAIL;
2713
2714 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2715
2716 rtl_rar_set(tp, dev->dev_addr);
2717
2718 return 0;
2719}
2720
Francois Romieu5f787a12006-08-17 13:02:36 +02002721static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2722{
2723 struct rtl8169_private *tp = netdev_priv(dev);
2724 struct mii_ioctl_data *data = if_mii(ifr);
2725
Francois Romieu8b4ab282008-11-19 22:05:25 -08002726 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2727}
Francois Romieu5f787a12006-08-17 13:02:36 +02002728
Francois Romieu8b4ab282008-11-19 22:05:25 -08002729static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2730{
Francois Romieu5f787a12006-08-17 13:02:36 +02002731 switch (cmd) {
2732 case SIOCGMIIPHY:
2733 data->phy_id = 32; /* Internal PHY */
2734 return 0;
2735
2736 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002737 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002738 return 0;
2739
2740 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002741 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002742 return 0;
2743 }
2744 return -EOPNOTSUPP;
2745}
2746
Francois Romieu8b4ab282008-11-19 22:05:25 -08002747static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2748{
2749 return -EOPNOTSUPP;
2750}
2751
Francois Romieu0e485152007-02-20 00:00:26 +01002752static const struct rtl_cfg_info {
2753 void (*hw_start)(struct net_device *);
2754 unsigned int region;
2755 unsigned int align;
2756 u16 intr_event;
2757 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002758 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002759 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002760} rtl_cfg_infos [] = {
2761 [RTL_CFG_0] = {
2762 .hw_start = rtl_hw_start_8169,
2763 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002764 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002765 .intr_event = SYSErr | LinkChg | RxOverflow |
2766 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002767 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002768 .features = RTL_FEATURE_GMII,
2769 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002770 },
2771 [RTL_CFG_1] = {
2772 .hw_start = rtl_hw_start_8168,
2773 .region = 2,
2774 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002775 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002776 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002777 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002778 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2779 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002780 },
2781 [RTL_CFG_2] = {
2782 .hw_start = rtl_hw_start_8101,
2783 .region = 2,
2784 .align = 8,
2785 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2786 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002787 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002788 .features = RTL_FEATURE_MSI,
2789 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002790 }
2791};
2792
Francois Romieufbac58f2007-10-04 22:51:38 +02002793/* Cfg9346_Unlock assumed. */
2794static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2795 const struct rtl_cfg_info *cfg)
2796{
2797 unsigned msi = 0;
2798 u8 cfg2;
2799
2800 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002801 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002802 if (pci_enable_msi(pdev)) {
2803 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2804 } else {
2805 cfg2 |= MSIEnable;
2806 msi = RTL_FEATURE_MSI;
2807 }
2808 }
2809 RTL_W8(Config2, cfg2);
2810 return msi;
2811}
2812
2813static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2814{
2815 if (tp->features & RTL_FEATURE_MSI) {
2816 pci_disable_msi(pdev);
2817 tp->features &= ~RTL_FEATURE_MSI;
2818 }
2819}
2820
Francois Romieu8b4ab282008-11-19 22:05:25 -08002821static const struct net_device_ops rtl8169_netdev_ops = {
2822 .ndo_open = rtl8169_open,
2823 .ndo_stop = rtl8169_close,
2824 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002825 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002826 .ndo_tx_timeout = rtl8169_tx_timeout,
2827 .ndo_validate_addr = eth_validate_addr,
2828 .ndo_change_mtu = rtl8169_change_mtu,
2829 .ndo_set_mac_address = rtl_set_mac_address,
2830 .ndo_do_ioctl = rtl8169_ioctl,
2831 .ndo_set_multicast_list = rtl_set_rx_mode,
2832#ifdef CONFIG_R8169_VLAN
2833 .ndo_vlan_rx_register = rtl8169_vlan_rx_register,
2834#endif
2835#ifdef CONFIG_NET_POLL_CONTROLLER
2836 .ndo_poll_controller = rtl8169_netpoll,
2837#endif
2838
2839};
2840
françois romieuc0e45c12011-01-03 15:08:04 +00002841static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2842{
2843 struct mdio_ops *ops = &tp->mdio_ops;
2844
2845 switch (tp->mac_version) {
2846 case RTL_GIGA_MAC_VER_27:
2847 ops->write = r8168dp_1_mdio_write;
2848 ops->read = r8168dp_1_mdio_read;
2849 break;
françois romieue6de30d2011-01-03 15:08:37 +00002850 case RTL_GIGA_MAC_VER_28:
2851 ops->write = r8168dp_2_mdio_write;
2852 ops->read = r8168dp_2_mdio_read;
2853 break;
françois romieuc0e45c12011-01-03 15:08:04 +00002854 default:
2855 ops->write = r8169_mdio_write;
2856 ops->read = r8169_mdio_read;
2857 break;
2858 }
2859}
2860
françois romieu065c27c2011-01-03 15:08:12 +00002861static void r810x_phy_power_down(struct rtl8169_private *tp)
2862{
2863 rtl_writephy(tp, 0x1f, 0x0000);
2864 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2865}
2866
2867static void r810x_phy_power_up(struct rtl8169_private *tp)
2868{
2869 rtl_writephy(tp, 0x1f, 0x0000);
2870 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2871}
2872
2873static void r810x_pll_power_down(struct rtl8169_private *tp)
2874{
2875 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2876 rtl_writephy(tp, 0x1f, 0x0000);
2877 rtl_writephy(tp, MII_BMCR, 0x0000);
2878 return;
2879 }
2880
2881 r810x_phy_power_down(tp);
2882}
2883
2884static void r810x_pll_power_up(struct rtl8169_private *tp)
2885{
2886 r810x_phy_power_up(tp);
2887}
2888
2889static void r8168_phy_power_up(struct rtl8169_private *tp)
2890{
2891 rtl_writephy(tp, 0x1f, 0x0000);
2892 rtl_writephy(tp, 0x0e, 0x0000);
2893 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2894}
2895
2896static void r8168_phy_power_down(struct rtl8169_private *tp)
2897{
2898 rtl_writephy(tp, 0x1f, 0x0000);
2899 rtl_writephy(tp, 0x0e, 0x0200);
2900 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2901}
2902
2903static void r8168_pll_power_down(struct rtl8169_private *tp)
2904{
2905 void __iomem *ioaddr = tp->mmio_addr;
2906
2907 if (tp->mac_version == RTL_GIGA_MAC_VER_27)
2908 return;
2909
2910 if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
2911 (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
2912 (RTL_R16(CPlusCmd) & ASF)) {
2913 return;
2914 }
2915
2916 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2917 rtl_writephy(tp, 0x1f, 0x0000);
2918 rtl_writephy(tp, MII_BMCR, 0x0000);
2919
2920 RTL_W32(RxConfig, RTL_R32(RxConfig) |
2921 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
2922 return;
2923 }
2924
2925 r8168_phy_power_down(tp);
2926
2927 switch (tp->mac_version) {
2928 case RTL_GIGA_MAC_VER_25:
2929 case RTL_GIGA_MAC_VER_26:
2930 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
2931 break;
2932 }
2933}
2934
2935static void r8168_pll_power_up(struct rtl8169_private *tp)
2936{
2937 void __iomem *ioaddr = tp->mmio_addr;
2938
2939 if (tp->mac_version == RTL_GIGA_MAC_VER_27)
2940 return;
2941
2942 switch (tp->mac_version) {
2943 case RTL_GIGA_MAC_VER_25:
2944 case RTL_GIGA_MAC_VER_26:
2945 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
2946 break;
2947 }
2948
2949 r8168_phy_power_up(tp);
2950}
2951
2952static void rtl_pll_power_op(struct rtl8169_private *tp,
2953 void (*op)(struct rtl8169_private *))
2954{
2955 if (op)
2956 op(tp);
2957}
2958
2959static void rtl_pll_power_down(struct rtl8169_private *tp)
2960{
2961 rtl_pll_power_op(tp, tp->pll_power_ops.down);
2962}
2963
2964static void rtl_pll_power_up(struct rtl8169_private *tp)
2965{
2966 rtl_pll_power_op(tp, tp->pll_power_ops.up);
2967}
2968
2969static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
2970{
2971 struct pll_power_ops *ops = &tp->pll_power_ops;
2972
2973 switch (tp->mac_version) {
2974 case RTL_GIGA_MAC_VER_07:
2975 case RTL_GIGA_MAC_VER_08:
2976 case RTL_GIGA_MAC_VER_09:
2977 case RTL_GIGA_MAC_VER_10:
2978 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08002979 case RTL_GIGA_MAC_VER_29:
2980 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00002981 ops->down = r810x_pll_power_down;
2982 ops->up = r810x_pll_power_up;
2983 break;
2984
2985 case RTL_GIGA_MAC_VER_11:
2986 case RTL_GIGA_MAC_VER_12:
2987 case RTL_GIGA_MAC_VER_17:
2988 case RTL_GIGA_MAC_VER_18:
2989 case RTL_GIGA_MAC_VER_19:
2990 case RTL_GIGA_MAC_VER_20:
2991 case RTL_GIGA_MAC_VER_21:
2992 case RTL_GIGA_MAC_VER_22:
2993 case RTL_GIGA_MAC_VER_23:
2994 case RTL_GIGA_MAC_VER_24:
2995 case RTL_GIGA_MAC_VER_25:
2996 case RTL_GIGA_MAC_VER_26:
2997 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00002998 case RTL_GIGA_MAC_VER_28:
françois romieu065c27c2011-01-03 15:08:12 +00002999 ops->down = r8168_pll_power_down;
3000 ops->up = r8168_pll_power_up;
3001 break;
3002
3003 default:
3004 ops->down = NULL;
3005 ops->up = NULL;
3006 break;
3007 }
3008}
3009
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003010static int __devinit
3011rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3012{
Francois Romieu0e485152007-02-20 00:00:26 +01003013 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3014 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003016 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003017 struct net_device *dev;
3018 void __iomem *ioaddr;
Francois Romieu07d3f512007-02-21 22:40:46 +01003019 unsigned int i;
3020 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003022 if (netif_msg_drv(&debug)) {
3023 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3024 MODULENAME, RTL8169_VERSION);
3025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003028 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003029 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003030 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003031 rc = -ENOMEM;
3032 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
3034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003036 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003038 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003039 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003040 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Francois Romieuccdffb92008-07-26 14:26:06 +02003042 mii = &tp->mii;
3043 mii->dev = dev;
3044 mii->mdio_read = rtl_mdio_read;
3045 mii->mdio_write = rtl_mdio_write;
3046 mii->phy_id_mask = 0x1f;
3047 mii->reg_num_mask = 0x1f;
3048 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3051 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003052 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003053 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003054 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
3056
françois romieu87aeec72010-04-26 11:42:06 +00003057 if (pci_set_mwi(pdev) < 0)
3058 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003061 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003062 netif_err(tp, probe, dev,
3063 "region #%d not an MMIO resource, aborting\n",
3064 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003066 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003070 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003071 netif_err(tp, probe, dev,
3072 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003074 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
3076
3077 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003078 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003079 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003080 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
3082
3083 tp->cp_cmd = PCIMulRW | RxChkSum;
3084
3085 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003086 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 tp->cp_cmd |= PCIDAC;
3088 dev->features |= NETIF_F_HIGHDMA;
3089 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003090 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003092 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003093 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
3095 }
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003098 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003099 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003100 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003102 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104
David S. Miller4300e8c2010-03-26 10:23:30 -07003105 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3106 if (!tp->pcie_cap)
3107 netif_info(tp, probe, dev, "no PCI Express capability\n");
3108
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003109 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
3111 /* Soft reset the chip. */
3112 RTL_W8(ChipCmd, CmdReset);
3113
3114 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003115 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3117 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003118 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
3120
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003121 RTL_W16(IntrStatus, 0xffff);
3122
françois romieuca52efd2009-07-24 12:34:19 +00003123 pci_set_master(pdev);
3124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 /* Identify chip attached to board */
3126 rtl8169_get_mac_version(tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
françois romieuc0e45c12011-01-03 15:08:04 +00003128 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003129 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003130
Jean Delvaref21b75e2009-05-26 20:54:48 -07003131 /* Use appropriate default if unknown */
3132 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003133 netif_notice(tp, probe, dev,
3134 "unknown MAC, using family default\n");
Jean Delvaref21b75e2009-05-26 20:54:48 -07003135 tp->mac_version = cfg->default_ver;
3136 }
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Roel Kluincee60c32008-04-17 22:35:54 +02003140 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (tp->mac_version == rtl_chip_info[i].mac_version)
3142 break;
3143 }
Roel Kluincee60c32008-04-17 22:35:54 +02003144 if (i == ARRAY_SIZE(rtl_chip_info)) {
Jean Delvaref21b75e2009-05-26 20:54:48 -07003145 dev_err(&pdev->dev,
3146 "driver bug, MAC version not found in rtl_chip_info\n");
françois romieu87aeec72010-04-26 11:42:06 +00003147 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 }
3149 tp->chipset = i;
3150
Francois Romieu5d06a992006-02-23 00:47:58 +01003151 RTL_W8(Cfg9346, Cfg9346_Unlock);
3152 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3153 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003154 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3155 tp->features |= RTL_FEATURE_WOL;
3156 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3157 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003158 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003159 RTL_W8(Cfg9346, Cfg9346_Lock);
3160
Francois Romieu66ec5d42007-11-06 22:56:10 +01003161 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3162 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 tp->set_speed = rtl8169_set_speed_tbi;
3164 tp->get_settings = rtl8169_gset_tbi;
3165 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3166 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3167 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003168 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Francois Romieu64e4bfb2006-08-17 12:43:06 +02003170 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 } else {
3172 tp->set_speed = rtl8169_set_speed_xmii;
3173 tp->get_settings = rtl8169_gset_xmii;
3174 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3175 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3176 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003177 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 }
3179
Francois Romieudf58ef52008-10-09 14:35:58 -07003180 spin_lock_init(&tp->lock);
3181
Petr Vandrovec738e1e62008-10-12 20:58:29 -07003182 tp->mmio_addr = ioaddr;
3183
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003184 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 for (i = 0; i < MAC_ADDR_LEN; i++)
3186 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003187 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3191 dev->irq = pdev->irq;
3192 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003194 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196#ifdef CONFIG_R8169_VLAN
3197 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198#endif
Eric Dumazet2edae082010-09-06 18:46:39 +00003199 dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003202 tp->hw_start = cfg->hw_start;
3203 tp->intr_event = cfg->intr_event;
3204 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Francois Romieu2efa53f2007-03-09 00:00:05 +01003206 init_timer(&tp->timer);
3207 tp->timer.data = (unsigned long) dev;
3208 tp->timer.function = rtl8169_phy_timer;
3209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003211 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003212 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214 pci_set_drvdata(pdev, dev);
3215
Joe Perchesbf82c182010-02-09 11:49:50 +00003216 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3217 rtl_chip_info[tp->chipset].name,
3218 dev->base_addr, dev->dev_addr,
3219 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
françois romieue6de30d2011-01-03 15:08:37 +00003221 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3222 (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
françois romieub646d902011-01-03 15:08:21 +00003223 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003224 }
françois romieub646d902011-01-03 15:08:21 +00003225
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003226 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Alan Sternf3ec4f82010-06-08 15:23:51 -04003228 if (pci_dev_run_wake(pdev))
3229 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003230
Ivan Vecera0d672e92011-02-15 02:08:39 +00003231 netif_carrier_off(dev);
3232
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003233out:
3234 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
françois romieu87aeec72010-04-26 11:42:06 +00003236err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003237 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003238 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003239err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003240 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003241err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003242 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003243 pci_disable_device(pdev);
3244err_out_free_dev_1:
3245 free_netdev(dev);
3246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Francois Romieu07d3f512007-02-21 22:40:46 +01003249static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 struct net_device *dev = pci_get_drvdata(pdev);
3252 struct rtl8169_private *tp = netdev_priv(dev);
3253
françois romieue6de30d2011-01-03 15:08:37 +00003254 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3255 (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
françois romieub646d902011-01-03 15:08:21 +00003256 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003257 }
françois romieub646d902011-01-03 15:08:21 +00003258
Tejun Heo23f333a2010-12-12 16:45:14 +01003259 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003260
françois romieuf1e02ed2011-01-13 13:07:53 +00003261 rtl_release_firmware(tp);
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003264
Alan Sternf3ec4f82010-06-08 15:23:51 -04003265 if (pci_dev_run_wake(pdev))
3266 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003267
Ivan Veceracc098dc2009-11-29 23:12:52 -08003268 /* restore original MAC address */
3269 rtl_rar_set(tp, dev->perm_addr);
3270
Francois Romieufbac58f2007-10-04 22:51:38 +02003271 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3273 pci_set_drvdata(pdev, NULL);
3274}
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276static int rtl8169_open(struct net_device *dev)
3277{
3278 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003279 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003281 int retval = -ENOMEM;
3282
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003283 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Neil Hormanc0cd8842010-03-29 13:16:02 -07003285 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003287 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003289 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3290 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003292 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003294 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3295 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003297 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
3299 retval = rtl8169_init_ring(dev);
3300 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003301 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
David Howellsc4028952006-11-22 14:57:56 +00003303 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Francois Romieu99f252b2007-04-02 22:59:59 +02003305 smp_mb();
3306
Francois Romieufbac58f2007-10-04 22:51:38 +02003307 retval = request_irq(dev->irq, rtl8169_interrupt,
3308 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003309 dev->name, dev);
3310 if (retval < 0)
3311 goto err_release_ring_2;
3312
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003313 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003314
françois romieueee3a962011-01-08 02:17:26 +00003315 rtl8169_init_phy(dev, tp);
3316
3317 /*
3318 * Pretend we are using VLANs; This bypasses a nasty bug where
3319 * Interrupts stop flowing on high load on 8110SCd controllers.
3320 */
3321 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3322 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | RxVlan);
3323
françois romieu065c27c2011-01-03 15:08:12 +00003324 rtl_pll_power_up(tp);
3325
Francois Romieu07ce4062007-02-23 23:36:39 +01003326 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
3328 rtl8169_request_timer(dev);
3329
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003330 tp->saved_wolopts = 0;
3331 pm_runtime_put_noidle(&pdev->dev);
3332
françois romieueee3a962011-01-08 02:17:26 +00003333 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334out:
3335 return retval;
3336
Francois Romieu99f252b2007-04-02 22:59:59 +02003337err_release_ring_2:
3338 rtl8169_rx_clear(tp);
3339err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003340 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3341 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003342 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003343err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003344 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3345 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003346 tp->TxDescArray = NULL;
3347err_pm_runtime_put:
3348 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 goto out;
3350}
3351
françois romieue6de30d2011-01-03 15:08:37 +00003352static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
françois romieue6de30d2011-01-03 15:08:37 +00003354 void __iomem *ioaddr = tp->mmio_addr;
3355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 /* Disable interrupts */
3357 rtl8169_irq_mask_and_ack(ioaddr);
3358
françois romieue6de30d2011-01-03 15:08:37 +00003359 if (tp->mac_version == RTL_GIGA_MAC_VER_28) {
3360 while (RTL_R8(TxPoll) & NPQ)
3361 udelay(20);
3362
3363 }
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 /* Reset the chipset */
3366 RTL_W8(ChipCmd, CmdReset);
3367
3368 /* PCI commit */
3369 RTL_R8(ChipCmd);
3370}
3371
Francois Romieu7f796d82007-06-11 23:04:41 +02003372static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003373{
3374 void __iomem *ioaddr = tp->mmio_addr;
3375 u32 cfg = rtl8169_rx_config;
3376
3377 cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
3378 RTL_W32(RxConfig, cfg);
3379
3380 /* Set DMA burst size and Interframe Gap Time */
3381 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3382 (InterFrameGap << TxInterFrameGapShift));
3383}
3384
Francois Romieu07ce4062007-02-23 23:36:39 +01003385static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
3387 struct rtl8169_private *tp = netdev_priv(dev);
3388 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01003389 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
3391 /* Soft reset the chip. */
3392 RTL_W8(ChipCmd, CmdReset);
3393
3394 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003395 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3397 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003398 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 }
3400
Francois Romieu07ce4062007-02-23 23:36:39 +01003401 tp->hw_start(dev);
3402
Francois Romieu07ce4062007-02-23 23:36:39 +01003403 netif_start_queue(dev);
3404}
3405
3406
Francois Romieu7f796d82007-06-11 23:04:41 +02003407static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3408 void __iomem *ioaddr)
3409{
3410 /*
3411 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3412 * register to be written before TxDescAddrLow to work.
3413 * Switching from MMIO to I/O access fixes the issue as well.
3414 */
3415 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003416 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003417 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003418 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003419}
3420
3421static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3422{
3423 u16 cmd;
3424
3425 cmd = RTL_R16(CPlusCmd);
3426 RTL_W16(CPlusCmd, cmd);
3427 return cmd;
3428}
3429
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003430static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003431{
3432 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003433 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003434}
3435
Francois Romieu6dccd162007-02-13 23:38:05 +01003436static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3437{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003438 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003439 u32 mac_version;
3440 u32 clk;
3441 u32 val;
3442 } cfg2_info [] = {
3443 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3444 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3445 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3446 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3447 }, *p = cfg2_info;
3448 unsigned int i;
3449 u32 clk;
3450
3451 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003452 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003453 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3454 RTL_W32(0x7c, p->val);
3455 break;
3456 }
3457 }
3458}
3459
Francois Romieu07ce4062007-02-23 23:36:39 +01003460static void rtl_hw_start_8169(struct net_device *dev)
3461{
3462 struct rtl8169_private *tp = netdev_priv(dev);
3463 void __iomem *ioaddr = tp->mmio_addr;
3464 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003465
Francois Romieu9cb427b2006-11-02 00:10:16 +01003466 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3467 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3468 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3469 }
3470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003472 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3473 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3474 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3475 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3476 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3477
françois romieuf0298f82011-01-03 15:07:42 +00003478 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003480 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Francois Romieuc946b302007-10-04 00:42:50 +02003482 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3483 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3484 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3485 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3486 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Francois Romieu7f796d82007-06-11 23:04:41 +02003488 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003489
3490 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3491 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
Joe Perches06fa7352007-10-18 21:15:00 +02003492 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003494 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
3496
Francois Romieubcf0bf92006-07-26 23:14:13 +02003497 RTL_W16(CPlusCmd, tp->cp_cmd);
3498
Francois Romieu6dccd162007-02-13 23:38:05 +01003499 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 /*
3502 * Undocumented corner. Supposedly:
3503 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3504 */
3505 RTL_W16(IntrMitigate, 0x0000);
3506
Francois Romieu7f796d82007-06-11 23:04:41 +02003507 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003508
Francois Romieuc946b302007-10-04 00:42:50 +02003509 if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
3510 (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
3511 (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
3512 (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
3513 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3514 rtl_set_rx_tx_config_registers(tp);
3515 }
3516
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003518
3519 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3520 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
3522 RTL_W32(RxMissed, 0);
3523
Francois Romieu07ce4062007-02-23 23:36:39 +01003524 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
3526 /* no early-rx interrupts */
3527 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003528
3529 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003530 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Francois Romieu9c14cea2008-07-05 00:21:15 +02003533static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003534{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003535 struct net_device *dev = pci_get_drvdata(pdev);
3536 struct rtl8169_private *tp = netdev_priv(dev);
3537 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003538
Francois Romieu9c14cea2008-07-05 00:21:15 +02003539 if (cap) {
3540 u16 ctl;
3541
3542 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3543 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3544 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3545 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003546}
3547
françois romieu650e8d52011-01-03 15:08:29 +00003548static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003549{
3550 u32 csi;
3551
3552 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003553 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3554}
3555
françois romieue6de30d2011-01-03 15:08:37 +00003556static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3557{
3558 rtl_csi_access_enable(ioaddr, 0x17000000);
3559}
3560
françois romieu650e8d52011-01-03 15:08:29 +00003561static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3562{
3563 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003564}
3565
3566struct ephy_info {
3567 unsigned int offset;
3568 u16 mask;
3569 u16 bits;
3570};
3571
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003572static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003573{
3574 u16 w;
3575
3576 while (len-- > 0) {
3577 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3578 rtl_ephy_write(ioaddr, e->offset, w);
3579 e++;
3580 }
3581}
3582
Francois Romieub726e492008-06-28 12:22:59 +02003583static void rtl_disable_clock_request(struct pci_dev *pdev)
3584{
3585 struct net_device *dev = pci_get_drvdata(pdev);
3586 struct rtl8169_private *tp = netdev_priv(dev);
3587 int cap = tp->pcie_cap;
3588
3589 if (cap) {
3590 u16 ctl;
3591
3592 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3593 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3594 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3595 }
3596}
3597
françois romieue6de30d2011-01-03 15:08:37 +00003598static void rtl_enable_clock_request(struct pci_dev *pdev)
3599{
3600 struct net_device *dev = pci_get_drvdata(pdev);
3601 struct rtl8169_private *tp = netdev_priv(dev);
3602 int cap = tp->pcie_cap;
3603
3604 if (cap) {
3605 u16 ctl;
3606
3607 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3608 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3609 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3610 }
3611}
3612
Francois Romieub726e492008-06-28 12:22:59 +02003613#define R8168_CPCMD_QUIRK_MASK (\
3614 EnableBist | \
3615 Mac_dbgo_oe | \
3616 Force_half_dup | \
3617 Force_rxflow_en | \
3618 Force_txflow_en | \
3619 Cxpl_dbg_sel | \
3620 ASF | \
3621 PktCntrDisable | \
3622 Mac_dbgo_sel)
3623
Francois Romieu219a1e92008-06-28 11:58:39 +02003624static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3625{
Francois Romieub726e492008-06-28 12:22:59 +02003626 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3627
3628 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3629
Francois Romieu2e68ae42008-06-28 12:00:55 +02003630 rtl_tx_performance_tweak(pdev,
3631 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003632}
3633
3634static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3635{
3636 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003637
françois romieuf0298f82011-01-03 15:07:42 +00003638 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003639
3640 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003641}
3642
3643static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3644{
Francois Romieub726e492008-06-28 12:22:59 +02003645 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3646
3647 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3648
Francois Romieu219a1e92008-06-28 11:58:39 +02003649 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003650
3651 rtl_disable_clock_request(pdev);
3652
3653 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003654}
3655
Francois Romieuef3386f2008-06-29 12:24:30 +02003656static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003657{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003658 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003659 { 0x01, 0, 0x0001 },
3660 { 0x02, 0x0800, 0x1000 },
3661 { 0x03, 0, 0x0042 },
3662 { 0x06, 0x0080, 0x0000 },
3663 { 0x07, 0, 0x2000 }
3664 };
3665
françois romieu650e8d52011-01-03 15:08:29 +00003666 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003667
3668 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3669
Francois Romieu219a1e92008-06-28 11:58:39 +02003670 __rtl_hw_start_8168cp(ioaddr, pdev);
3671}
3672
Francois Romieuef3386f2008-06-29 12:24:30 +02003673static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3674{
françois romieu650e8d52011-01-03 15:08:29 +00003675 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003676
3677 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3678
3679 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3680
3681 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3682}
3683
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003684static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3685{
françois romieu650e8d52011-01-03 15:08:29 +00003686 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003687
3688 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3689
3690 /* Magic. */
3691 RTL_W8(DBG_REG, 0x20);
3692
françois romieuf0298f82011-01-03 15:07:42 +00003693 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003694
3695 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3696
3697 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3698}
3699
Francois Romieu219a1e92008-06-28 11:58:39 +02003700static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3701{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003702 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003703 { 0x02, 0x0800, 0x1000 },
3704 { 0x03, 0, 0x0002 },
3705 { 0x06, 0x0080, 0x0000 }
3706 };
3707
françois romieu650e8d52011-01-03 15:08:29 +00003708 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003709
3710 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3711
3712 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3713
Francois Romieu219a1e92008-06-28 11:58:39 +02003714 __rtl_hw_start_8168cp(ioaddr, pdev);
3715}
3716
3717static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3718{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003719 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003720 { 0x01, 0, 0x0001 },
3721 { 0x03, 0x0400, 0x0220 }
3722 };
3723
françois romieu650e8d52011-01-03 15:08:29 +00003724 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003725
3726 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3727
Francois Romieu219a1e92008-06-28 11:58:39 +02003728 __rtl_hw_start_8168cp(ioaddr, pdev);
3729}
3730
Francois Romieu197ff762008-06-28 13:16:02 +02003731static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3732{
3733 rtl_hw_start_8168c_2(ioaddr, pdev);
3734}
3735
Francois Romieu6fb07052008-06-29 11:54:28 +02003736static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3737{
françois romieu650e8d52011-01-03 15:08:29 +00003738 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02003739
3740 __rtl_hw_start_8168cp(ioaddr, pdev);
3741}
3742
Francois Romieu5b538df2008-07-20 16:22:45 +02003743static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3744{
françois romieu650e8d52011-01-03 15:08:29 +00003745 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02003746
3747 rtl_disable_clock_request(pdev);
3748
françois romieuf0298f82011-01-03 15:07:42 +00003749 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02003750
3751 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3752
3753 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3754}
3755
françois romieue6de30d2011-01-03 15:08:37 +00003756static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
3757{
3758 static const struct ephy_info e_info_8168d_4[] = {
3759 { 0x0b, ~0, 0x48 },
3760 { 0x19, 0x20, 0x50 },
3761 { 0x0c, ~0, 0x20 }
3762 };
3763 int i;
3764
3765 rtl_csi_access_enable_1(ioaddr);
3766
3767 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3768
3769 RTL_W8(MaxTxPacketSize, TxPacketMax);
3770
3771 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
3772 const struct ephy_info *e = e_info_8168d_4 + i;
3773 u16 w;
3774
3775 w = rtl_ephy_read(ioaddr, e->offset);
3776 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
3777 }
3778
3779 rtl_enable_clock_request(pdev);
3780}
3781
Francois Romieu07ce4062007-02-23 23:36:39 +01003782static void rtl_hw_start_8168(struct net_device *dev)
3783{
Francois Romieu2dd99532007-06-11 23:22:52 +02003784 struct rtl8169_private *tp = netdev_priv(dev);
3785 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01003786 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02003787
3788 RTL_W8(Cfg9346, Cfg9346_Unlock);
3789
françois romieuf0298f82011-01-03 15:07:42 +00003790 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02003791
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003792 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02003793
Francois Romieu0e485152007-02-20 00:00:26 +01003794 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02003795
3796 RTL_W16(CPlusCmd, tp->cp_cmd);
3797
Francois Romieu0e485152007-02-20 00:00:26 +01003798 RTL_W16(IntrMitigate, 0x5151);
3799
3800 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01003801 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
3802 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01003803 tp->intr_event |= RxFIFOOver | PCSTimeout;
3804 tp->intr_event &= ~RxOverflow;
3805 }
Francois Romieu2dd99532007-06-11 23:22:52 +02003806
3807 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3808
Francois Romieub8363902008-06-01 12:31:57 +02003809 rtl_set_rx_mode(dev);
3810
3811 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3812 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02003813
3814 RTL_R8(IntrMask);
3815
Francois Romieu219a1e92008-06-28 11:58:39 +02003816 switch (tp->mac_version) {
3817 case RTL_GIGA_MAC_VER_11:
3818 rtl_hw_start_8168bb(ioaddr, pdev);
3819 break;
3820
3821 case RTL_GIGA_MAC_VER_12:
3822 case RTL_GIGA_MAC_VER_17:
3823 rtl_hw_start_8168bef(ioaddr, pdev);
3824 break;
3825
3826 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02003827 rtl_hw_start_8168cp_1(ioaddr, pdev);
Francois Romieu219a1e92008-06-28 11:58:39 +02003828 break;
3829
3830 case RTL_GIGA_MAC_VER_19:
3831 rtl_hw_start_8168c_1(ioaddr, pdev);
3832 break;
3833
3834 case RTL_GIGA_MAC_VER_20:
3835 rtl_hw_start_8168c_2(ioaddr, pdev);
3836 break;
3837
Francois Romieu197ff762008-06-28 13:16:02 +02003838 case RTL_GIGA_MAC_VER_21:
3839 rtl_hw_start_8168c_3(ioaddr, pdev);
3840 break;
3841
Francois Romieu6fb07052008-06-29 11:54:28 +02003842 case RTL_GIGA_MAC_VER_22:
3843 rtl_hw_start_8168c_4(ioaddr, pdev);
3844 break;
3845
Francois Romieuef3386f2008-06-29 12:24:30 +02003846 case RTL_GIGA_MAC_VER_23:
3847 rtl_hw_start_8168cp_2(ioaddr, pdev);
3848 break;
3849
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003850 case RTL_GIGA_MAC_VER_24:
3851 rtl_hw_start_8168cp_3(ioaddr, pdev);
3852 break;
3853
Francois Romieu5b538df2008-07-20 16:22:45 +02003854 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00003855 case RTL_GIGA_MAC_VER_26:
3856 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02003857 rtl_hw_start_8168d(ioaddr, pdev);
3858 break;
3859
françois romieue6de30d2011-01-03 15:08:37 +00003860 case RTL_GIGA_MAC_VER_28:
3861 rtl_hw_start_8168d_4(ioaddr, pdev);
3862 break;
3863
Francois Romieu219a1e92008-06-28 11:58:39 +02003864 default:
3865 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
3866 dev->name, tp->mac_version);
3867 break;
3868 }
Francois Romieu2dd99532007-06-11 23:22:52 +02003869
Francois Romieu0e485152007-02-20 00:00:26 +01003870 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3871
Francois Romieub8363902008-06-01 12:31:57 +02003872 RTL_W8(Cfg9346, Cfg9346_Lock);
3873
Francois Romieu2dd99532007-06-11 23:22:52 +02003874 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003875
Francois Romieu0e485152007-02-20 00:00:26 +01003876 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003877}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
Francois Romieu2857ffb2008-08-02 21:08:49 +02003879#define R810X_CPCMD_QUIRK_MASK (\
3880 EnableBist | \
3881 Mac_dbgo_oe | \
3882 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00003883 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02003884 Force_txflow_en | \
3885 Cxpl_dbg_sel | \
3886 ASF | \
3887 PktCntrDisable | \
3888 PCIDAC | \
3889 PCIMulRW)
3890
3891static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
3892{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003893 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003894 { 0x01, 0, 0x6e65 },
3895 { 0x02, 0, 0x091f },
3896 { 0x03, 0, 0xc2f9 },
3897 { 0x06, 0, 0xafb5 },
3898 { 0x07, 0, 0x0e00 },
3899 { 0x19, 0, 0xec80 },
3900 { 0x01, 0, 0x2e65 },
3901 { 0x01, 0, 0x6e65 }
3902 };
3903 u8 cfg1;
3904
françois romieu650e8d52011-01-03 15:08:29 +00003905 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003906
3907 RTL_W8(DBG_REG, FIX_NAK_1);
3908
3909 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3910
3911 RTL_W8(Config1,
3912 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
3913 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3914
3915 cfg1 = RTL_R8(Config1);
3916 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
3917 RTL_W8(Config1, cfg1 & ~LEDS0);
3918
3919 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
3920
3921 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
3922}
3923
3924static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
3925{
françois romieu650e8d52011-01-03 15:08:29 +00003926 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003927
3928 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3929
3930 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
3931 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3932
3933 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R810X_CPCMD_QUIRK_MASK);
3934}
3935
3936static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
3937{
3938 rtl_hw_start_8102e_2(ioaddr, pdev);
3939
3940 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
3941}
3942
Hayes Wang5a5e4442011-02-22 17:26:21 +08003943static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
3944{
3945 static const struct ephy_info e_info_8105e_1[] = {
3946 { 0x07, 0, 0x4000 },
3947 { 0x19, 0, 0x0200 },
3948 { 0x19, 0, 0x0020 },
3949 { 0x1e, 0, 0x2000 },
3950 { 0x03, 0, 0x0001 },
3951 { 0x19, 0, 0x0100 },
3952 { 0x19, 0, 0x0004 },
3953 { 0x0a, 0, 0x0020 }
3954 };
3955
3956 /* Force LAN exit from ASPM if Rx/Tx are not idel */
3957 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
3958
3959 /* disable Early Tally Counter */
3960 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
3961
3962 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
3963 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
3964
3965 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
3966}
3967
3968static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
3969{
3970 rtl_hw_start_8105e_1(ioaddr, pdev);
3971 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
3972}
3973
Francois Romieu07ce4062007-02-23 23:36:39 +01003974static void rtl_hw_start_8101(struct net_device *dev)
3975{
Francois Romieucdf1a602007-06-11 23:29:50 +02003976 struct rtl8169_private *tp = netdev_priv(dev);
3977 void __iomem *ioaddr = tp->mmio_addr;
3978 struct pci_dev *pdev = tp->pci_dev;
3979
Francois Romieue3cf0cc2007-08-17 14:55:46 +02003980 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
3981 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02003982 int cap = tp->pcie_cap;
3983
3984 if (cap) {
3985 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
3986 PCI_EXP_DEVCTL_NOSNOOP_EN);
3987 }
Francois Romieucdf1a602007-06-11 23:29:50 +02003988 }
3989
Francois Romieu2857ffb2008-08-02 21:08:49 +02003990 switch (tp->mac_version) {
3991 case RTL_GIGA_MAC_VER_07:
3992 rtl_hw_start_8102e_1(ioaddr, pdev);
3993 break;
3994
3995 case RTL_GIGA_MAC_VER_08:
3996 rtl_hw_start_8102e_3(ioaddr, pdev);
3997 break;
3998
3999 case RTL_GIGA_MAC_VER_09:
4000 rtl_hw_start_8102e_2(ioaddr, pdev);
4001 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004002
4003 case RTL_GIGA_MAC_VER_29:
4004 rtl_hw_start_8105e_1(ioaddr, pdev);
4005 break;
4006 case RTL_GIGA_MAC_VER_30:
4007 rtl_hw_start_8105e_2(ioaddr, pdev);
4008 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004009 }
4010
4011 RTL_W8(Cfg9346, Cfg9346_Unlock);
4012
françois romieuf0298f82011-01-03 15:07:42 +00004013 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004014
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004015 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004016
4017 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
4018
4019 RTL_W16(CPlusCmd, tp->cp_cmd);
4020
4021 RTL_W16(IntrMitigate, 0x0000);
4022
4023 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4024
4025 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4026 rtl_set_rx_tx_config_registers(tp);
4027
4028 RTL_W8(Cfg9346, Cfg9346_Lock);
4029
4030 RTL_R8(IntrMask);
4031
Francois Romieucdf1a602007-06-11 23:29:50 +02004032 rtl_set_rx_mode(dev);
4033
Francois Romieu0e485152007-02-20 00:00:26 +01004034 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4035
Francois Romieucdf1a602007-06-11 23:29:50 +02004036 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004037
Francois Romieu0e485152007-02-20 00:00:26 +01004038 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
4041static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4044 return -EINVAL;
4045
4046 dev->mtu = new_mtu;
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048}
4049
4050static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4051{
Al Viro95e09182007-12-22 18:55:39 +00004052 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4054}
4055
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004056static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4057 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004059 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004060 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004061
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004062 kfree(*data_buff);
4063 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 rtl8169_make_unusable_by_asic(desc);
4065}
4066
4067static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4068{
4069 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4070
4071 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4072}
4073
4074static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4075 u32 rx_buf_sz)
4076{
4077 desc->addr = cpu_to_le64(mapping);
4078 wmb();
4079 rtl8169_mark_to_asic(desc, rx_buf_sz);
4080}
4081
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004082static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004084 return (void *)ALIGN((long)data, 16);
4085}
4086
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004087static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4088 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004089{
4090 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004092 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004093 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004094 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004096 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4097 if (!data)
4098 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004099
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004100 if (rtl8169_align(data) != data) {
4101 kfree(data);
4102 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4103 if (!data)
4104 return NULL;
4105 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004106
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004107 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004108 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004109 if (unlikely(dma_mapping_error(d, mapping))) {
4110 if (net_ratelimit())
4111 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004112 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
4115 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004116 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004117
4118err_out:
4119 kfree(data);
4120 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121}
4122
4123static void rtl8169_rx_clear(struct rtl8169_private *tp)
4124{
Francois Romieu07d3f512007-02-21 22:40:46 +01004125 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
4127 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004128 if (tp->Rx_databuff[i]) {
4129 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 tp->RxDescArray + i);
4131 }
4132 }
4133}
4134
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004135static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004137 desc->opts1 |= cpu_to_le32(RingEnd);
4138}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004139
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004140static int rtl8169_rx_fill(struct rtl8169_private *tp)
4141{
4142 unsigned int i;
4143
4144 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004145 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004146
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004147 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004149
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004150 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004151 if (!data) {
4152 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004153 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004154 }
4155 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004158 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4159 return 0;
4160
4161err_out:
4162 rtl8169_rx_clear(tp);
4163 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
4166static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4167{
4168 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4169}
4170
4171static int rtl8169_init_ring(struct net_device *dev)
4172{
4173 struct rtl8169_private *tp = netdev_priv(dev);
4174
4175 rtl8169_init_ring_indexes(tp);
4176
4177 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004178 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004180 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
4182
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004183static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 struct TxDesc *desc)
4185{
4186 unsigned int len = tx_skb->len;
4187
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004188 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4189
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 desc->opts1 = 0x00;
4191 desc->opts2 = 0x00;
4192 desc->addr = 0x00;
4193 tx_skb->len = 0;
4194}
4195
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004196static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4197 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
4199 unsigned int i;
4200
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004201 for (i = 0; i < n; i++) {
4202 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 struct ring_info *tx_skb = tp->tx_skb + entry;
4204 unsigned int len = tx_skb->len;
4205
4206 if (len) {
4207 struct sk_buff *skb = tx_skb->skb;
4208
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004209 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 tp->TxDescArray + entry);
4211 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004212 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 dev_kfree_skb(skb);
4214 tx_skb->skb = NULL;
4215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 }
4217 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004218}
4219
4220static void rtl8169_tx_clear(struct rtl8169_private *tp)
4221{
4222 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 tp->cur_tx = tp->dirty_tx = 0;
4224}
4225
David Howellsc4028952006-11-22 14:57:56 +00004226static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
4228 struct rtl8169_private *tp = netdev_priv(dev);
4229
David Howellsc4028952006-11-22 14:57:56 +00004230 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 schedule_delayed_work(&tp->task, 4);
4232}
4233
4234static void rtl8169_wait_for_quiescence(struct net_device *dev)
4235{
4236 struct rtl8169_private *tp = netdev_priv(dev);
4237 void __iomem *ioaddr = tp->mmio_addr;
4238
4239 synchronize_irq(dev->irq);
4240
4241 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004242 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 rtl8169_irq_mask_and_ack(ioaddr);
4245
David S. Millerd1d08d12008-01-07 20:53:33 -08004246 tp->intr_mask = 0xffff;
4247 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004248 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
David Howellsc4028952006-11-22 14:57:56 +00004251static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252{
David Howellsc4028952006-11-22 14:57:56 +00004253 struct rtl8169_private *tp =
4254 container_of(work, struct rtl8169_private, task.work);
4255 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 int ret;
4257
Francois Romieueb2a0212007-02-15 23:37:21 +01004258 rtnl_lock();
4259
4260 if (!netif_running(dev))
4261 goto out_unlock;
4262
4263 rtl8169_wait_for_quiescence(dev);
4264 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
4266 ret = rtl8169_open(dev);
4267 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004268 if (net_ratelimit())
4269 netif_err(tp, drv, dev,
4270 "reinit failure (status = %d). Rescheduling\n",
4271 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4273 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004274
4275out_unlock:
4276 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277}
4278
David Howellsc4028952006-11-22 14:57:56 +00004279static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
David Howellsc4028952006-11-22 14:57:56 +00004281 struct rtl8169_private *tp =
4282 container_of(work, struct rtl8169_private, task.work);
4283 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Francois Romieueb2a0212007-02-15 23:37:21 +01004285 rtnl_lock();
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004288 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
4290 rtl8169_wait_for_quiescence(dev);
4291
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004292 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 rtl8169_tx_clear(tp);
4294
4295 if (tp->dirty_rx == tp->cur_rx) {
4296 rtl8169_init_ring_indexes(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004297 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 netif_wake_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004299 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 } else {
Joe Perchesbf82c182010-02-09 11:49:50 +00004301 if (net_ratelimit())
4302 netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 rtl8169_schedule_work(dev, rtl8169_reset_task);
4304 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004305
4306out_unlock:
4307 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
4310static void rtl8169_tx_timeout(struct net_device *dev)
4311{
4312 struct rtl8169_private *tp = netdev_priv(dev);
4313
françois romieue6de30d2011-01-03 15:08:37 +00004314 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
4316 /* Let's wait a bit while any (async) irq lands on */
4317 rtl8169_schedule_work(dev, rtl8169_reset_task);
4318}
4319
4320static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4321 u32 opts1)
4322{
4323 struct skb_shared_info *info = skb_shinfo(skb);
4324 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004325 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004326 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 entry = tp->cur_tx;
4329 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4330 skb_frag_t *frag = info->frags + cur_frag;
4331 dma_addr_t mapping;
4332 u32 status, len;
4333 void *addr;
4334
4335 entry = (entry + 1) % NUM_TX_DESC;
4336
4337 txd = tp->TxDescArray + entry;
4338 len = frag->size;
4339 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004340 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004341 if (unlikely(dma_mapping_error(d, mapping))) {
4342 if (net_ratelimit())
4343 netif_err(tp, drv, tp->dev,
4344 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004345 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
4348 /* anti gcc 2.95.3 bugware (sic) */
4349 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4350
4351 txd->opts1 = cpu_to_le32(status);
4352 txd->addr = cpu_to_le64(mapping);
4353
4354 tp->tx_skb[entry].len = len;
4355 }
4356
4357 if (cur_frag) {
4358 tp->tx_skb[entry].skb = skb;
4359 txd->opts1 |= cpu_to_le32(LastFrag);
4360 }
4361
4362 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004363
4364err_out:
4365 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4366 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367}
4368
4369static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
4370{
4371 if (dev->features & NETIF_F_TSO) {
Herbert Xu79671682006-06-22 02:40:14 -07004372 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374 if (mss)
4375 return LargeSend | ((mss & MSSMask) << MSSShift);
4376 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07004377 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004378 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
4380 if (ip->protocol == IPPROTO_TCP)
4381 return IPCS | TCPCS;
4382 else if (ip->protocol == IPPROTO_UDP)
4383 return IPCS | UDPCS;
4384 WARN_ON(1); /* we need a WARN() */
4385 }
4386 return 0;
4387}
4388
Stephen Hemminger613573252009-08-31 19:50:58 +00004389static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4390 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
4392 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004393 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 struct TxDesc *txd = tp->TxDescArray + entry;
4395 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004396 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 dma_addr_t mapping;
4398 u32 status, len;
4399 u32 opts1;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004400 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004403 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004404 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 }
4406
4407 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004408 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004410 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004411 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004412 if (unlikely(dma_mapping_error(d, mapping))) {
4413 if (net_ratelimit())
4414 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004415 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
4418 tp->tx_skb[entry].len = len;
4419 txd->addr = cpu_to_le64(mapping);
4420 txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4421
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004422 opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
4423
4424 frags = rtl8169_xmit_frags(tp, skb, opts1);
4425 if (frags < 0)
4426 goto err_dma_1;
4427 else if (frags)
4428 opts1 |= FirstFrag;
4429 else {
4430 opts1 |= FirstFrag | LastFrag;
4431 tp->tx_skb[entry].skb = skb;
4432 }
4433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 wmb();
4435
4436 /* anti gcc 2.95.3 bugware (sic) */
4437 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4438 txd->opts1 = cpu_to_le32(status);
4439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 tp->cur_tx += frags + 1;
4441
David Dillow4c020a92010-03-03 16:33:10 +00004442 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Francois Romieu275391a2007-02-23 23:50:28 +01004444 RTL_W8(TxPoll, NPQ); /* set polling bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
4446 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4447 netif_stop_queue(dev);
4448 smp_rmb();
4449 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4450 netif_wake_queue(dev);
4451 }
4452
Stephen Hemminger613573252009-08-31 19:50:58 +00004453 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004455err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004456 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004457err_dma_0:
4458 dev_kfree_skb(skb);
4459 dev->stats.tx_dropped++;
4460 return NETDEV_TX_OK;
4461
4462err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004464 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004465 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
4467
4468static void rtl8169_pcierr_interrupt(struct net_device *dev)
4469{
4470 struct rtl8169_private *tp = netdev_priv(dev);
4471 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 u16 pci_status, pci_cmd;
4473
4474 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4475 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4476
Joe Perchesbf82c182010-02-09 11:49:50 +00004477 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4478 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
4480 /*
4481 * The recovery sequence below admits a very elaborated explanation:
4482 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004483 * - I did not see what else could be done;
4484 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 *
4486 * Feel free to adjust to your needs.
4487 */
Francois Romieua27993f2006-12-18 00:04:19 +01004488 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004489 pci_cmd &= ~PCI_COMMAND_PARITY;
4490 else
4491 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4492
4493 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
4495 pci_write_config_word(pdev, PCI_STATUS,
4496 pci_status & (PCI_STATUS_DETECTED_PARITY |
4497 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4498 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4499
4500 /* The infamous DAC f*ckup only happens at boot time */
4501 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004502 void __iomem *ioaddr = tp->mmio_addr;
4503
Joe Perchesbf82c182010-02-09 11:49:50 +00004504 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 tp->cp_cmd &= ~PCIDAC;
4506 RTL_W16(CPlusCmd, tp->cp_cmd);
4507 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 }
4509
françois romieue6de30d2011-01-03 15:08:37 +00004510 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004511
4512 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513}
4514
Francois Romieu07d3f512007-02-21 22:40:46 +01004515static void rtl8169_tx_interrupt(struct net_device *dev,
4516 struct rtl8169_private *tp,
4517 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518{
4519 unsigned int dirty_tx, tx_left;
4520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 dirty_tx = tp->dirty_tx;
4522 smp_rmb();
4523 tx_left = tp->cur_tx - dirty_tx;
4524
4525 while (tx_left > 0) {
4526 unsigned int entry = dirty_tx % NUM_TX_DESC;
4527 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 u32 status;
4529
4530 rmb();
4531 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4532 if (status & DescOwn)
4533 break;
4534
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004535 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4536 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004538 dev->stats.tx_packets++;
4539 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004540 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 tx_skb->skb = NULL;
4542 }
4543 dirty_tx++;
4544 tx_left--;
4545 }
4546
4547 if (tp->dirty_tx != dirty_tx) {
4548 tp->dirty_tx = dirty_tx;
4549 smp_wmb();
4550 if (netif_queue_stopped(dev) &&
4551 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4552 netif_wake_queue(dev);
4553 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004554 /*
4555 * 8168 hack: TxPoll requests are lost when the Tx packets are
4556 * too close. Let's kick an extra TxPoll request when a burst
4557 * of start_xmit activity is detected (if it is not detected,
4558 * it is slow enough). -- FR
4559 */
4560 smp_rmb();
4561 if (tp->cur_tx != dirty_tx)
4562 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 }
4564}
4565
Francois Romieu126fa4b2005-05-12 20:09:17 -04004566static inline int rtl8169_fragmented_frame(u32 status)
4567{
4568 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4569}
4570
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004571static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 u32 status = opts1 & RxProtoMask;
4574
4575 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004576 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 skb->ip_summed = CHECKSUM_UNNECESSARY;
4578 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004579 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580}
4581
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004582static struct sk_buff *rtl8169_try_rx_copy(void *data,
4583 struct rtl8169_private *tp,
4584 int pkt_size,
4585 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004587 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004588 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004590 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004591 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004592 prefetch(data);
4593 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4594 if (skb)
4595 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004596 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4597
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004598 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599}
4600
Eric Dumazet630b9432010-03-31 02:08:31 +00004601/*
4602 * Warning : rtl8169_rx_interrupt() might be called :
4603 * 1) from NAPI (softirq) context
4604 * (polling = 1 : we should call netif_receive_skb())
4605 * 2) from process context (rtl8169_reset_task())
4606 * (polling = 0 : we must call netif_rx() instead)
4607 */
Francois Romieu07d3f512007-02-21 22:40:46 +01004608static int rtl8169_rx_interrupt(struct net_device *dev,
4609 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004610 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
4612 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004613 unsigned int count;
Eric Dumazet630b9432010-03-31 02:08:31 +00004614 int polling = (budget != ~(u32)0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 cur_rx = tp->cur_rx;
4617 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004618 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004620 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004622 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 u32 status;
4624
4625 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004626 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
4628 if (status & DescOwn)
4629 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004630 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004631 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4632 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004633 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004635 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004637 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004638 if (status & RxFOVF) {
4639 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004640 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004641 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004642 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004644 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004645 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Francois Romieu126fa4b2005-05-12 20:09:17 -04004648 /*
4649 * The driver does not support incoming fragmented
4650 * frames. They are seen as a symptom of over-mtu
4651 * sized frames.
4652 */
4653 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004654 dev->stats.rx_dropped++;
4655 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004656 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004657 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004658 }
4659
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004660 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4661 tp, pkt_size, addr);
4662 rtl8169_mark_to_asic(desc, rx_buf_sz);
4663 if (!skb) {
4664 dev->stats.rx_dropped++;
4665 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
4667
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004668 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 skb_put(skb, pkt_size);
4670 skb->protocol = eth_type_trans(skb, dev);
4671
Eric Dumazet630b9432010-03-31 02:08:31 +00004672 if (rtl8169_rx_vlan_skb(tp, desc, skb, polling) < 0) {
4673 if (likely(polling))
Eric Dumazet2edae082010-09-06 18:46:39 +00004674 napi_gro_receive(&tp->napi, skb);
Eric Dumazet630b9432010-03-31 02:08:31 +00004675 else
4676 netif_rx(skb);
4677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Francois Romieucebf8cc2007-10-18 12:06:54 +02004679 dev->stats.rx_bytes += pkt_size;
4680 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 }
Francois Romieu6dccd162007-02-13 23:38:05 +01004682
4683 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00004684 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01004685 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4686 desc->opts2 = 0;
4687 cur_rx++;
4688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
4690
4691 count = cur_rx - tp->cur_rx;
4692 tp->cur_rx = cur_rx;
4693
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004694 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
4696 return count;
4697}
4698
Francois Romieu07d3f512007-02-21 22:40:46 +01004699static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
Francois Romieu07d3f512007-02-21 22:40:46 +01004701 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02004705 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
David Dillowf11a3772009-05-22 15:29:34 +00004707 /* loop handling interrupts until we have no new ones or
4708 * we hit a invalid/hotplug case.
4709 */
Francois Romieu865c6522008-05-11 14:51:00 +02004710 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00004711 while (status && status != 0xffff) {
4712 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
David Dillowf11a3772009-05-22 15:29:34 +00004714 /* Handle all of the error cases first. These will reset
4715 * the chip, so just exit the loop.
4716 */
4717 if (unlikely(!netif_running(dev))) {
4718 rtl8169_asic_down(ioaddr);
4719 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 }
David Dillowf11a3772009-05-22 15:29:34 +00004721
Francois Romieu1519e572011-02-03 12:02:36 +01004722 if (unlikely(status & RxFIFOOver)) {
4723 switch (tp->mac_version) {
4724 /* Work around for rx fifo overflow */
4725 case RTL_GIGA_MAC_VER_11:
4726 case RTL_GIGA_MAC_VER_22:
4727 case RTL_GIGA_MAC_VER_26:
4728 netif_stop_queue(dev);
4729 rtl8169_tx_timeout(dev);
4730 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01004731 /* Testers needed. */
4732 case RTL_GIGA_MAC_VER_17:
4733 case RTL_GIGA_MAC_VER_19:
4734 case RTL_GIGA_MAC_VER_20:
4735 case RTL_GIGA_MAC_VER_21:
4736 case RTL_GIGA_MAC_VER_23:
4737 case RTL_GIGA_MAC_VER_24:
4738 case RTL_GIGA_MAC_VER_27:
4739 case RTL_GIGA_MAC_VER_28:
Francois Romieu1519e572011-02-03 12:02:36 +01004740 /* Experimental science. Pktgen proof. */
4741 case RTL_GIGA_MAC_VER_12:
4742 case RTL_GIGA_MAC_VER_25:
4743 if (status == RxFIFOOver)
4744 goto done;
4745 break;
4746 default:
4747 break;
4748 }
David Dillowf11a3772009-05-22 15:29:34 +00004749 }
4750
4751 if (unlikely(status & SYSErr)) {
4752 rtl8169_pcierr_interrupt(dev);
4753 break;
4754 }
4755
4756 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00004757 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00004758
4759 /* We need to see the lastest version of tp->intr_mask to
4760 * avoid ignoring an MSI interrupt and having to wait for
4761 * another event which may never come.
4762 */
4763 smp_rmb();
4764 if (status & tp->intr_mask & tp->napi_event) {
4765 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
4766 tp->intr_mask = ~tp->napi_event;
4767
4768 if (likely(napi_schedule_prep(&tp->napi)))
4769 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00004770 else
4771 netif_info(tp, intr, dev,
4772 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00004773 }
4774
4775 /* We only get a new MSI interrupt when all active irq
4776 * sources on the chip have been acknowledged. So, ack
4777 * everything we've seen and check if new sources have become
4778 * active to avoid blocking all interrupts from the chip.
4779 */
4780 RTL_W16(IntrStatus,
4781 (status & RxFIFOOver) ? (status | RxOverflow) : status);
4782 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 }
Francois Romieu1519e572011-02-03 12:02:36 +01004784done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 return IRQ_RETVAL(handled);
4786}
4787
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004788static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004790 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
4791 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004793 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004795 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 rtl8169_tx_interrupt(dev, tp, ioaddr);
4797
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004798 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004799 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00004800
4801 /* We need for force the visibility of tp->intr_mask
4802 * for other CPUs, as we can loose an MSI interrupt
4803 * and potentially wait for a retransmit timeout if we don't.
4804 * The posted write to IntrMask is safe, as it will
4805 * eventually make it to the chip and we won't loose anything
4806 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 */
David Dillowf11a3772009-05-22 15:29:34 +00004808 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00004809 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01004810 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 }
4812
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004813 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Francois Romieu523a6092008-09-10 22:28:56 +02004816static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
4817{
4818 struct rtl8169_private *tp = netdev_priv(dev);
4819
4820 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
4821 return;
4822
4823 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
4824 RTL_W32(RxMissed, 0);
4825}
4826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827static void rtl8169_down(struct net_device *dev)
4828{
4829 struct rtl8169_private *tp = netdev_priv(dev);
4830 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
4832 rtl8169_delete_timer(dev);
4833
4834 netif_stop_queue(dev);
4835
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01004836 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01004837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 spin_lock_irq(&tp->lock);
4839
4840 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004841 /*
4842 * At this point device interrupts can not be enabled in any function,
4843 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
4844 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
4845 */
Francois Romieu523a6092008-09-10 22:28:56 +02004846 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
4848 spin_unlock_irq(&tp->lock);
4849
4850 synchronize_irq(dev->irq);
4851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004853 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 rtl8169_tx_clear(tp);
4856
4857 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004858
4859 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
4861
4862static int rtl8169_close(struct net_device *dev)
4863{
4864 struct rtl8169_private *tp = netdev_priv(dev);
4865 struct pci_dev *pdev = tp->pci_dev;
4866
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004867 pm_runtime_get_sync(&pdev->dev);
4868
Ivan Vecera355423d2009-02-06 21:49:57 -08004869 /* update counters before going down */
4870 rtl8169_update_counters(dev);
4871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 rtl8169_down(dev);
4873
4874 free_irq(dev->irq, dev);
4875
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004876 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4877 tp->RxPhyAddr);
4878 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4879 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 tp->TxDescArray = NULL;
4881 tp->RxDescArray = NULL;
4882
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004883 pm_runtime_put_sync(&pdev->dev);
4884
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 return 0;
4886}
4887
Francois Romieu07ce4062007-02-23 23:36:39 +01004888static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889{
4890 struct rtl8169_private *tp = netdev_priv(dev);
4891 void __iomem *ioaddr = tp->mmio_addr;
4892 unsigned long flags;
4893 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01004894 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 u32 tmp = 0;
4896
4897 if (dev->flags & IFF_PROMISC) {
4898 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00004899 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 rx_mode =
4901 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4902 AcceptAllPhys;
4903 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00004904 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00004905 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 /* Too many to filter perfectly -- accept all multicasts. */
4907 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4908 mc_filter[1] = mc_filter[0] = 0xffffffff;
4909 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00004910 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01004911
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 rx_mode = AcceptBroadcast | AcceptMyPhys;
4913 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00004914 netdev_for_each_mc_addr(ha, dev) {
4915 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4917 rx_mode |= AcceptMulticast;
4918 }
4919 }
4920
4921 spin_lock_irqsave(&tp->lock, flags);
4922
4923 tmp = rtl8169_rx_config | rx_mode |
4924 (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
4925
Francois Romieuf887cce2008-07-17 22:24:18 +02004926 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01004927 u32 data = mc_filter[0];
4928
4929 mc_filter[0] = swab32(mc_filter[1]);
4930 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02004931 }
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07004934 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
Francois Romieu57a9f232007-06-04 22:10:15 +02004936 RTL_W32(RxConfig, tmp);
4937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 spin_unlock_irqrestore(&tp->lock, flags);
4939}
4940
4941/**
4942 * rtl8169_get_stats - Get rtl8169 read/write statistics
4943 * @dev: The Ethernet Device to get statistics for
4944 *
4945 * Get TX/RX statistics for rtl8169
4946 */
4947static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
4948{
4949 struct rtl8169_private *tp = netdev_priv(dev);
4950 void __iomem *ioaddr = tp->mmio_addr;
4951 unsigned long flags;
4952
4953 if (netif_running(dev)) {
4954 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02004955 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 spin_unlock_irqrestore(&tp->lock, flags);
4957 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02004958
Francois Romieucebf8cc2007-10-18 12:06:54 +02004959 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004962static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01004963{
françois romieu065c27c2011-01-03 15:08:12 +00004964 struct rtl8169_private *tp = netdev_priv(dev);
4965
Francois Romieu5d06a992006-02-23 00:47:58 +01004966 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004967 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01004968
françois romieu065c27c2011-01-03 15:08:12 +00004969 rtl_pll_power_down(tp);
4970
Francois Romieu5d06a992006-02-23 00:47:58 +01004971 netif_device_detach(dev);
4972 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004973}
Francois Romieu5d06a992006-02-23 00:47:58 +01004974
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004975#ifdef CONFIG_PM
4976
4977static int rtl8169_suspend(struct device *device)
4978{
4979 struct pci_dev *pdev = to_pci_dev(device);
4980 struct net_device *dev = pci_get_drvdata(pdev);
4981
4982 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02004983
Francois Romieu5d06a992006-02-23 00:47:58 +01004984 return 0;
4985}
4986
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004987static void __rtl8169_resume(struct net_device *dev)
4988{
françois romieu065c27c2011-01-03 15:08:12 +00004989 struct rtl8169_private *tp = netdev_priv(dev);
4990
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004991 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00004992
4993 rtl_pll_power_up(tp);
4994
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004995 rtl8169_schedule_work(dev, rtl8169_reset_task);
4996}
4997
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004998static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01004999{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005000 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005001 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005002 struct rtl8169_private *tp = netdev_priv(dev);
5003
5004 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005005
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005006 if (netif_running(dev))
5007 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005008
Francois Romieu5d06a992006-02-23 00:47:58 +01005009 return 0;
5010}
5011
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005012static int rtl8169_runtime_suspend(struct device *device)
5013{
5014 struct pci_dev *pdev = to_pci_dev(device);
5015 struct net_device *dev = pci_get_drvdata(pdev);
5016 struct rtl8169_private *tp = netdev_priv(dev);
5017
5018 if (!tp->TxDescArray)
5019 return 0;
5020
5021 spin_lock_irq(&tp->lock);
5022 tp->saved_wolopts = __rtl8169_get_wol(tp);
5023 __rtl8169_set_wol(tp, WAKE_ANY);
5024 spin_unlock_irq(&tp->lock);
5025
5026 rtl8169_net_suspend(dev);
5027
5028 return 0;
5029}
5030
5031static int rtl8169_runtime_resume(struct device *device)
5032{
5033 struct pci_dev *pdev = to_pci_dev(device);
5034 struct net_device *dev = pci_get_drvdata(pdev);
5035 struct rtl8169_private *tp = netdev_priv(dev);
5036
5037 if (!tp->TxDescArray)
5038 return 0;
5039
5040 spin_lock_irq(&tp->lock);
5041 __rtl8169_set_wol(tp, tp->saved_wolopts);
5042 tp->saved_wolopts = 0;
5043 spin_unlock_irq(&tp->lock);
5044
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005045 rtl8169_init_phy(dev, tp);
5046
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005047 __rtl8169_resume(dev);
5048
5049 return 0;
5050}
5051
5052static int rtl8169_runtime_idle(struct device *device)
5053{
5054 struct pci_dev *pdev = to_pci_dev(device);
5055 struct net_device *dev = pci_get_drvdata(pdev);
5056 struct rtl8169_private *tp = netdev_priv(dev);
5057
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005058 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005059}
5060
Alexey Dobriyan47145212009-12-14 18:00:08 -08005061static const struct dev_pm_ops rtl8169_pm_ops = {
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005062 .suspend = rtl8169_suspend,
5063 .resume = rtl8169_resume,
5064 .freeze = rtl8169_suspend,
5065 .thaw = rtl8169_resume,
5066 .poweroff = rtl8169_suspend,
5067 .restore = rtl8169_resume,
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005068 .runtime_suspend = rtl8169_runtime_suspend,
5069 .runtime_resume = rtl8169_runtime_resume,
5070 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005071};
5072
5073#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5074
5075#else /* !CONFIG_PM */
5076
5077#define RTL8169_PM_OPS NULL
5078
5079#endif /* !CONFIG_PM */
5080
Francois Romieu1765f952008-09-13 17:21:40 +02005081static void rtl_shutdown(struct pci_dev *pdev)
5082{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005083 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005084 struct rtl8169_private *tp = netdev_priv(dev);
5085 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005086
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005087 rtl8169_net_suspend(dev);
5088
Ivan Veceracc098dc2009-11-29 23:12:52 -08005089 /* restore original MAC address */
5090 rtl_rar_set(tp, dev->perm_addr);
5091
françois romieu4bb3f522009-06-17 11:41:45 +00005092 spin_lock_irq(&tp->lock);
5093
5094 rtl8169_asic_down(ioaddr);
5095
5096 spin_unlock_irq(&tp->lock);
5097
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005098 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005099 /* WoL fails with some 8168 when the receiver is disabled. */
5100 if (tp->features & RTL_FEATURE_WOL) {
5101 pci_clear_master(pdev);
5102
5103 RTL_W8(ChipCmd, CmdRxEnb);
5104 /* PCI commit */
5105 RTL_R8(ChipCmd);
5106 }
5107
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005108 pci_wake_from_d3(pdev, true);
5109 pci_set_power_state(pdev, PCI_D3hot);
5110 }
5111}
Francois Romieu5d06a992006-02-23 00:47:58 +01005112
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113static struct pci_driver rtl8169_pci_driver = {
5114 .name = MODULENAME,
5115 .id_table = rtl8169_pci_tbl,
5116 .probe = rtl8169_init_one,
5117 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005118 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005119 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120};
5121
Francois Romieu07d3f512007-02-21 22:40:46 +01005122static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
Jeff Garzik29917622006-08-19 17:48:59 -04005124 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125}
5126
Francois Romieu07d3f512007-02-21 22:40:46 +01005127static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128{
5129 pci_unregister_driver(&rtl8169_pci_driver);
5130}
5131
5132module_init(rtl8169_init_module);
5133module_exit(rtl8169_cleanup_module);