blob: 7ffdb80adf40fd7ffd0e716e777b7b2e9f7e35ef [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>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Francois Romieu99f252b2007-04-02 22:59:59 +020030#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
32#include <asm/irq.h>
33
Francois Romieu865c6522008-05-11 14:51:00 +020034#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define MODULENAME "r8169"
36#define PFX MODULENAME ": "
37
françois romieubca03d52011-01-03 15:07:31 +000038#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
40
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define _R(NAME,MAC,MASK) \
130 { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
131
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800132static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 const char *name;
134 u8 mac_version;
135 u32 RxConfigMask; /* Clears the bits supported by this chip */
136} rtl_chip_info[] = {
Francois Romieuba6eb6e2007-06-11 23:35:18 +0200137 _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
138 _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
139 _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
140 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
141 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +0100142 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
Francois Romieu2857ffb2008-08-02 21:08:49 +0200143 _R("RTL8102e", RTL_GIGA_MAC_VER_07, 0xff7e1880), // PCI-E
144 _R("RTL8102e", RTL_GIGA_MAC_VER_08, 0xff7e1880), // PCI-E
145 _R("RTL8102e", RTL_GIGA_MAC_VER_09, 0xff7e1880), // PCI-E
146 _R("RTL8101e", RTL_GIGA_MAC_VER_10, 0xff7e1880), // PCI-E
Francois Romieubcf0bf92006-07-26 23:14:13 +0200147 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
148 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
149 _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
150 _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200151 _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880), // PCI-E 8139
152 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880), // PCI-E
153 _R("RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880), // PCI-E
154 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880), // PCI-E
155 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880), // PCI-E
Francois Romieu197ff762008-06-28 13:16:02 +0200156 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880), // PCI-E
Francois Romieu6fb07052008-06-29 11:54:28 +0200157 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, 0xff7e1880), // PCI-E
Francois Romieuef3386f2008-06-29 12:24:30 +0200158 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, 0xff7e1880), // PCI-E
Francois Romieu7f3e3d32008-07-20 18:53:20 +0200159 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, 0xff7e1880), // PCI-E
Francois Romieu5b538df2008-07-20 16:22:45 +0200160 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, 0xff7e1880), // PCI-E
françois romieudaf9df62009-10-07 12:44:20 +0000161 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, 0xff7e1880), // PCI-E
162 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, 0xff7e1880), // PCI-E
françois romieue6de30d2011-01-03 15:08:37 +0000163 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, 0xff7e1880), // PCI-E
164 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28, 0xff7e1880) // PCI-E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166#undef _R
167
Francois Romieubcf0bf92006-07-26 23:14:13 +0200168enum cfg_version {
169 RTL_CFG_0 = 0x00,
170 RTL_CFG_1,
171 RTL_CFG_2
172};
173
Francois Romieu07ce4062007-02-23 23:36:39 +0100174static void rtl_hw_start_8169(struct net_device *);
175static void rtl_hw_start_8168(struct net_device *);
176static void rtl_hw_start_8101(struct net_device *);
177
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000178static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200179 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200180 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200181 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100182 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200183 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
184 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200185 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200186 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
187 { PCI_VENDOR_ID_LINKSYS, 0x1032,
188 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100189 { 0x0001, 0x8168,
190 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 {0,},
192};
193
194MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
195
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000196static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700197static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200198static struct {
199 u32 msg_enable;
200} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Francois Romieu07d3f512007-02-21 22:40:46 +0100202enum rtl_registers {
203 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100204 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100205 MAR0 = 8, /* Multicast filter. */
206 CounterAddrLow = 0x10,
207 CounterAddrHigh = 0x14,
208 TxDescStartAddrLow = 0x20,
209 TxDescStartAddrHigh = 0x24,
210 TxHDescStartAddrLow = 0x28,
211 TxHDescStartAddrHigh = 0x2c,
212 FLASH = 0x30,
213 ERSR = 0x36,
214 ChipCmd = 0x37,
215 TxPoll = 0x38,
216 IntrMask = 0x3c,
217 IntrStatus = 0x3e,
218 TxConfig = 0x40,
219 RxConfig = 0x44,
220 RxMissed = 0x4c,
221 Cfg9346 = 0x50,
222 Config0 = 0x51,
223 Config1 = 0x52,
224 Config2 = 0x53,
225 Config3 = 0x54,
226 Config4 = 0x55,
227 Config5 = 0x56,
228 MultiIntr = 0x5c,
229 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100230 PHYstatus = 0x6c,
231 RxMaxSize = 0xda,
232 CPlusCmd = 0xe0,
233 IntrMitigate = 0xe2,
234 RxDescAddrLow = 0xe4,
235 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000236 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
237
238#define NoEarlyTx 0x3f /* Max value : no early transmit. */
239
240 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
241
242#define TxPacketMax (8064 >> 7)
243
Francois Romieu07d3f512007-02-21 22:40:46 +0100244 FuncEvent = 0xf0,
245 FuncEventMask = 0xf4,
246 FuncPresetState = 0xf8,
247 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
Francois Romieuf162a5d2008-06-01 22:37:49 +0200250enum rtl8110_registers {
251 TBICSR = 0x64,
252 TBI_ANAR = 0x68,
253 TBI_LPAR = 0x6a,
254};
255
256enum rtl8168_8101_registers {
257 CSIDR = 0x64,
258 CSIAR = 0x68,
259#define CSIAR_FLAG 0x80000000
260#define CSIAR_WRITE_CMD 0x80000000
261#define CSIAR_BYTE_ENABLE 0x0f
262#define CSIAR_BYTE_ENABLE_SHIFT 12
263#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000264 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200265 EPHYAR = 0x80,
266#define EPHYAR_FLAG 0x80000000
267#define EPHYAR_WRITE_CMD 0x80000000
268#define EPHYAR_REG_MASK 0x1f
269#define EPHYAR_REG_SHIFT 16
270#define EPHYAR_DATA_MASK 0xffff
271 DBG_REG = 0xd1,
272#define FIX_NAK_1 (1 << 4)
273#define FIX_NAK_2 (1 << 3)
françois romieudaf9df62009-10-07 12:44:20 +0000274 EFUSEAR = 0xdc,
275#define EFUSEAR_FLAG 0x80000000
276#define EFUSEAR_WRITE_CMD 0x80000000
277#define EFUSEAR_READ_CMD 0x00000000
278#define EFUSEAR_REG_MASK 0x03ff
279#define EFUSEAR_REG_SHIFT 8
280#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200281};
282
françois romieuc0e45c12011-01-03 15:08:04 +0000283enum rtl8168_registers {
françois romieub646d902011-01-03 15:08:21 +0000284 ERIDR = 0x70,
285 ERIAR = 0x74,
286#define ERIAR_FLAG 0x80000000
287#define ERIAR_WRITE_CMD 0x80000000
288#define ERIAR_READ_CMD 0x00000000
289#define ERIAR_ADDR_BYTE_ALIGN 4
290#define ERIAR_EXGMAC 0
291#define ERIAR_MSIX 1
292#define ERIAR_ASF 2
293#define ERIAR_TYPE_SHIFT 16
294#define ERIAR_BYTEEN 0x0f
295#define ERIAR_BYTEEN_SHIFT 12
françois romieuc0e45c12011-01-03 15:08:04 +0000296 EPHY_RXER_NUM = 0x7c,
297 OCPDR = 0xb0, /* OCP GPHY access */
298#define OCPDR_WRITE_CMD 0x80000000
299#define OCPDR_READ_CMD 0x00000000
300#define OCPDR_REG_MASK 0x7f
301#define OCPDR_GPHY_REG_SHIFT 16
302#define OCPDR_DATA_MASK 0xffff
303 OCPAR = 0xb4,
304#define OCPAR_FLAG 0x80000000
305#define OCPAR_GPHY_WRITE_CMD 0x8000f060
306#define OCPAR_GPHY_READ_CMD 0x0000f060
françois romieue6de30d2011-01-03 15:08:37 +0000307 RDSAR1 = 0xd0 /* 8168c only. Undocumented on 8168dp */
françois romieuc0e45c12011-01-03 15:08:04 +0000308};
309
Francois Romieu07d3f512007-02-21 22:40:46 +0100310enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100312 SYSErr = 0x8000,
313 PCSTimeout = 0x4000,
314 SWInt = 0x0100,
315 TxDescUnavail = 0x0080,
316 RxFIFOOver = 0x0040,
317 LinkChg = 0x0020,
318 RxOverflow = 0x0010,
319 TxErr = 0x0008,
320 TxOK = 0x0004,
321 RxErr = 0x0002,
322 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200325 RxFOVF = (1 << 23),
326 RxRWT = (1 << 22),
327 RxRES = (1 << 21),
328 RxRUNT = (1 << 20),
329 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100332 CmdReset = 0x10,
333 CmdRxEnb = 0x08,
334 CmdTxEnb = 0x04,
335 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Francois Romieu275391a2007-02-23 23:50:28 +0100337 /* TXPoll register p.5 */
338 HPQ = 0x80, /* Poll cmd on the high prio queue */
339 NPQ = 0x40, /* Poll cmd on the low prio queue */
340 FSWInt = 0x01, /* Forced software interrupt */
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100343 Cfg9346_Lock = 0x00,
344 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100347 AcceptErr = 0x20,
348 AcceptRunt = 0x10,
349 AcceptBroadcast = 0x08,
350 AcceptMulticast = 0x04,
351 AcceptMyPhys = 0x02,
352 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100355 RxCfgFIFOShift = 13,
356 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 /* TxConfigBits */
359 TxInterFrameGapShift = 24,
360 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
361
Francois Romieu5d06a992006-02-23 00:47:58 +0100362 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200363 LEDS1 = (1 << 7),
364 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200365 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200366 Speed_down = (1 << 4),
367 MEMMAP = (1 << 3),
368 IOMAP = (1 << 2),
369 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100370 PMEnable = (1 << 0), /* Power Management Enable */
371
Francois Romieu6dccd162007-02-13 23:38:05 +0100372 /* Config2 register p. 25 */
373 PCI_Clock_66MHz = 0x01,
374 PCI_Clock_33MHz = 0x00,
375
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100376 /* Config3 register p.25 */
377 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
378 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200379 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100380
Francois Romieu5d06a992006-02-23 00:47:58 +0100381 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100382 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
383 MWF = (1 << 5), /* Accept Multicast wakeup frame */
384 UWF = (1 << 4), /* Accept Unicast wakeup frame */
385 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100386 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* TBICSR p.28 */
389 TBIReset = 0x80000000,
390 TBILoopback = 0x40000000,
391 TBINwEnable = 0x20000000,
392 TBINwRestart = 0x10000000,
393 TBILinkOk = 0x02000000,
394 TBINwComplete = 0x01000000,
395
396 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200397 EnableBist = (1 << 15), // 8168 8101
398 Mac_dbgo_oe = (1 << 14), // 8168 8101
399 Normal_mode = (1 << 13), // unused
400 Force_half_dup = (1 << 12), // 8168 8101
401 Force_rxflow_en = (1 << 11), // 8168 8101
402 Force_txflow_en = (1 << 10), // 8168 8101
403 Cxpl_dbg_sel = (1 << 9), // 8168 8101
404 ASF = (1 << 8), // 8168 8101
405 PktCntrDisable = (1 << 7), // 8168 8101
406 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 RxVlan = (1 << 6),
408 RxChkSum = (1 << 5),
409 PCIDAC = (1 << 4),
410 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100411 INTT_0 = 0x0000, // 8168
412 INTT_1 = 0x0001, // 8168
413 INTT_2 = 0x0002, // 8168
414 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100417 TBI_Enable = 0x80,
418 TxFlowCtrl = 0x40,
419 RxFlowCtrl = 0x20,
420 _1000bpsF = 0x10,
421 _100bps = 0x08,
422 _10bps = 0x04,
423 LinkStatus = 0x02,
424 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100427 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200428
429 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100430 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431};
432
Francois Romieu07d3f512007-02-21 22:40:46 +0100433enum desc_status_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
435 RingEnd = (1 << 30), /* End of descriptor ring */
436 FirstFrag = (1 << 29), /* First segment of a packet */
437 LastFrag = (1 << 28), /* Final segment of a packet */
438
439 /* Tx private */
440 LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
441 MSSShift = 16, /* MSS value position */
442 MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
443 IPCS = (1 << 18), /* Calculate IP checksum */
444 UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
445 TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
446 TxVlanTag = (1 << 17), /* Add VLAN tag */
447
448 /* Rx private */
449 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
450 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
451
452#define RxProtoUDP (PID1)
453#define RxProtoTCP (PID0)
454#define RxProtoIP (PID1 | PID0)
455#define RxProtoMask RxProtoIP
456
457 IPFail = (1 << 16), /* IP checksum failed */
458 UDPFail = (1 << 15), /* UDP/IP checksum failed */
459 TCPFail = (1 << 14), /* TCP/IP checksum failed */
460 RxVlanTag = (1 << 16), /* VLAN tag available */
461};
462
463#define RsvdMask 0x3fffc000
464
465struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200466 __le32 opts1;
467 __le32 opts2;
468 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469};
470
471struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200472 __le32 opts1;
473 __le32 opts2;
474 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475};
476
477struct ring_info {
478 struct sk_buff *skb;
479 u32 len;
480 u8 __pad[sizeof(void *) - sizeof(u32)];
481};
482
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200483enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200484 RTL_FEATURE_WOL = (1 << 0),
485 RTL_FEATURE_MSI = (1 << 1),
486 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200487};
488
Ivan Vecera355423d2009-02-06 21:49:57 -0800489struct rtl8169_counters {
490 __le64 tx_packets;
491 __le64 rx_packets;
492 __le64 tx_errors;
493 __le32 rx_errors;
494 __le16 rx_missed;
495 __le16 align_errors;
496 __le32 tx_one_collision;
497 __le32 tx_multi_collision;
498 __le64 rx_unicast;
499 __le64 rx_broadcast;
500 __le32 rx_multicast;
501 __le16 tx_aborted;
502 __le16 tx_underun;
503};
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505struct rtl8169_private {
506 void __iomem *mmio_addr; /* memory map physical address */
507 struct pci_dev *pci_dev; /* Index of PCI device */
David Howellsc4028952006-11-22 14:57:56 +0000508 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700509 struct napi_struct napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 spinlock_t lock; /* spin lock flag */
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200511 u32 msg_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 int chipset;
513 int mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
515 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
516 u32 dirty_rx;
517 u32 dirty_tx;
518 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
519 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
520 dma_addr_t TxPhyAddr;
521 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000522 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct timer_list timer;
525 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100526 u16 intr_event;
527 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 u16 intr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 int phy_1000_ctrl_reg;
530#ifdef CONFIG_R8169_VLAN
531 struct vlan_group *vlgrp;
532#endif
françois romieuc0e45c12011-01-03 15:08:04 +0000533
534 struct mdio_ops {
535 void (*write)(void __iomem *, int, int);
536 int (*read)(void __iomem *, int);
537 } mdio_ops;
538
françois romieu065c27c2011-01-03 15:08:12 +0000539 struct pll_power_ops {
540 void (*down)(struct rtl8169_private *);
541 void (*up)(struct rtl8169_private *);
542 } pll_power_ops;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 int (*set_speed)(struct net_device *, u8 autoneg, u16 speed, u8 duplex);
Francois Romieuccdffb92008-07-26 14:26:06 +0200545 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000546 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100547 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000548 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800550 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200551 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000552 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200553 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200554
555 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800556 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000557 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000558
559 const struct firmware *fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560};
561
Ralf Baechle979b6c12005-06-13 14:30:40 -0700562MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700565MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200566module_param_named(debug, debug.msg_enable, int, 0);
567MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568MODULE_LICENSE("GPL");
569MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000570MODULE_FIRMWARE(FIRMWARE_8168D_1);
571MODULE_FIRMWARE(FIRMWARE_8168D_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000574static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
575 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100576static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100578static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100580static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200582static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700584 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200585static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200587static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700588static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200591 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
françois romieub646d902011-01-03 15:08:21 +0000593static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
594{
595 void __iomem *ioaddr = tp->mmio_addr;
596 int i;
597
598 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
599 for (i = 0; i < 20; i++) {
600 udelay(100);
601 if (RTL_R32(OCPAR) & OCPAR_FLAG)
602 break;
603 }
604 return RTL_R32(OCPDR);
605}
606
607static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
608{
609 void __iomem *ioaddr = tp->mmio_addr;
610 int i;
611
612 RTL_W32(OCPDR, data);
613 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
614 for (i = 0; i < 20; i++) {
615 udelay(100);
616 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
617 break;
618 }
619}
620
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800621static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000622{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800623 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000624 int i;
625
626 RTL_W8(ERIDR, cmd);
627 RTL_W32(ERIAR, 0x800010e8);
628 msleep(2);
629 for (i = 0; i < 5; i++) {
630 udelay(100);
631 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
632 break;
633 }
634
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800635 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000636}
637
638#define OOB_CMD_RESET 0x00
639#define OOB_CMD_DRIVER_START 0x05
640#define OOB_CMD_DRIVER_STOP 0x06
641
642static void rtl8168_driver_start(struct rtl8169_private *tp)
643{
644 int i;
645
646 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
647
648 for (i = 0; i < 10; i++) {
649 msleep(10);
650 if (ocp_read(tp, 0x0f, 0x0010) & 0x00000800)
651 break;
652 }
653}
654
655static void rtl8168_driver_stop(struct rtl8169_private *tp)
656{
657 int i;
658
659 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
660
661 for (i = 0; i < 10; i++) {
662 msleep(10);
663 if ((ocp_read(tp, 0x0f, 0x0010) & 0x00000800) == 0)
664 break;
665 }
666}
667
668
françois romieu4da19632011-01-03 15:07:55 +0000669static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 int i;
672
Francois Romieua6baf3a2007-11-08 23:23:21 +0100673 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Francois Romieu23714082006-01-29 00:49:09 +0100675 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100676 /*
677 * Check if the RTL8169 has completed writing to the specified
678 * MII register.
679 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200680 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 break;
Francois Romieu23714082006-01-29 00:49:09 +0100682 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700684 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700685 * According to hardware specs a 20us delay is required after write
686 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700687 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700688 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
françois romieu4da19632011-01-03 15:07:55 +0000691static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 int i, value = -1;
694
Francois Romieua6baf3a2007-11-08 23:23:21 +0100695 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Francois Romieu23714082006-01-29 00:49:09 +0100697 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100698 /*
699 * Check if the RTL8169 has completed retrieving data from
700 * the specified MII register.
701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100703 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 break;
705 }
Francois Romieu23714082006-01-29 00:49:09 +0100706 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700708 /*
709 * According to hardware specs a 20us delay is required after read
710 * complete indication, but before sending next command.
711 */
712 udelay(20);
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return value;
715}
716
françois romieuc0e45c12011-01-03 15:08:04 +0000717static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
718{
719 int i;
720
721 RTL_W32(OCPDR, data |
722 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
723 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
724 RTL_W32(EPHY_RXER_NUM, 0);
725
726 for (i = 0; i < 100; i++) {
727 mdelay(1);
728 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
729 break;
730 }
731}
732
733static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
734{
735 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
736 (value & OCPDR_DATA_MASK));
737}
738
739static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
740{
741 int i;
742
743 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
744
745 mdelay(1);
746 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
747 RTL_W32(EPHY_RXER_NUM, 0);
748
749 for (i = 0; i < 100; i++) {
750 mdelay(1);
751 if (RTL_R32(OCPAR) & OCPAR_FLAG)
752 break;
753 }
754
755 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
756}
757
françois romieue6de30d2011-01-03 15:08:37 +0000758#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
759
760static void r8168dp_2_mdio_start(void __iomem *ioaddr)
761{
762 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
763}
764
765static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
766{
767 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
768}
769
770static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
771{
772 r8168dp_2_mdio_start(ioaddr);
773
774 r8169_mdio_write(ioaddr, reg_addr, value);
775
776 r8168dp_2_mdio_stop(ioaddr);
777}
778
779static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
780{
781 int value;
782
783 r8168dp_2_mdio_start(ioaddr);
784
785 value = r8169_mdio_read(ioaddr, reg_addr);
786
787 r8168dp_2_mdio_stop(ioaddr);
788
789 return value;
790}
791
françois romieu4da19632011-01-03 15:07:55 +0000792static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200793{
françois romieuc0e45c12011-01-03 15:08:04 +0000794 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200795}
796
françois romieu4da19632011-01-03 15:07:55 +0000797static int rtl_readphy(struct rtl8169_private *tp, int location)
798{
françois romieuc0e45c12011-01-03 15:08:04 +0000799 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000800}
801
802static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
803{
804 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
805}
806
807static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000808{
809 int val;
810
françois romieu4da19632011-01-03 15:07:55 +0000811 val = rtl_readphy(tp, reg_addr);
812 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000813}
814
Francois Romieuccdffb92008-07-26 14:26:06 +0200815static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
816 int val)
817{
818 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200819
françois romieu4da19632011-01-03 15:07:55 +0000820 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200821}
822
823static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
824{
825 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200826
françois romieu4da19632011-01-03 15:07:55 +0000827 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200828}
829
Francois Romieudacf8152008-08-02 20:44:13 +0200830static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
831{
832 unsigned int i;
833
834 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
835 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
836
837 for (i = 0; i < 100; i++) {
838 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
839 break;
840 udelay(10);
841 }
842}
843
844static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
845{
846 u16 value = 0xffff;
847 unsigned int i;
848
849 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
850
851 for (i = 0; i < 100; i++) {
852 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
853 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
854 break;
855 }
856 udelay(10);
857 }
858
859 return value;
860}
861
862static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
863{
864 unsigned int i;
865
866 RTL_W32(CSIDR, value);
867 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
868 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
869
870 for (i = 0; i < 100; i++) {
871 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
872 break;
873 udelay(10);
874 }
875}
876
877static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
878{
879 u32 value = ~0x00;
880 unsigned int i;
881
882 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
883 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
884
885 for (i = 0; i < 100; i++) {
886 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
887 value = RTL_R32(CSIDR);
888 break;
889 }
890 udelay(10);
891 }
892
893 return value;
894}
895
françois romieudaf9df62009-10-07 12:44:20 +0000896static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
897{
898 u8 value = 0xff;
899 unsigned int i;
900
901 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
902
903 for (i = 0; i < 300; i++) {
904 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
905 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
906 break;
907 }
908 udelay(100);
909 }
910
911 return value;
912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
915{
916 RTL_W16(IntrMask, 0x0000);
917
918 RTL_W16(IntrStatus, 0xffff);
919}
920
921static void rtl8169_asic_down(void __iomem *ioaddr)
922{
923 RTL_W8(ChipCmd, 0x00);
924 rtl8169_irq_mask_and_ack(ioaddr);
925 RTL_R16(CPlusCmd);
926}
927
françois romieu4da19632011-01-03 15:07:55 +0000928static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
françois romieu4da19632011-01-03 15:07:55 +0000930 void __iomem *ioaddr = tp->mmio_addr;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return RTL_R32(TBICSR) & TBIReset;
933}
934
françois romieu4da19632011-01-03 15:07:55 +0000935static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
françois romieu4da19632011-01-03 15:07:55 +0000937 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
941{
942 return RTL_R32(TBICSR) & TBILinkOk;
943}
944
945static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
946{
947 return RTL_R8(PHYstatus) & LinkStatus;
948}
949
françois romieu4da19632011-01-03 15:07:55 +0000950static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
françois romieu4da19632011-01-03 15:07:55 +0000952 void __iomem *ioaddr = tp->mmio_addr;
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
955}
956
françois romieu4da19632011-01-03 15:07:55 +0000957static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
959 unsigned int val;
960
françois romieu4da19632011-01-03 15:07:55 +0000961 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
962 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000965static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieu07d3f512007-02-21 22:40:46 +0100966 struct rtl8169_private *tp,
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000967 void __iomem *ioaddr,
968 bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 unsigned long flags;
971
972 spin_lock_irqsave(&tp->lock, flags);
973 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000974 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000975 if (pm)
976 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +0100978 if (net_ratelimit())
979 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200980 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +0000982 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000983 if (pm)
984 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 spin_unlock_irqrestore(&tp->lock, flags);
987}
988
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000989static void rtl8169_check_link_status(struct net_device *dev,
990 struct rtl8169_private *tp,
991 void __iomem *ioaddr)
992{
993 __rtl8169_check_link_status(dev, tp, ioaddr, false);
994}
995
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000996#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
997
998static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
999{
1000 void __iomem *ioaddr = tp->mmio_addr;
1001 u8 options;
1002 u32 wolopts = 0;
1003
1004 options = RTL_R8(Config1);
1005 if (!(options & PMEnable))
1006 return 0;
1007
1008 options = RTL_R8(Config3);
1009 if (options & LinkUp)
1010 wolopts |= WAKE_PHY;
1011 if (options & MagicPacket)
1012 wolopts |= WAKE_MAGIC;
1013
1014 options = RTL_R8(Config5);
1015 if (options & UWF)
1016 wolopts |= WAKE_UCAST;
1017 if (options & BWF)
1018 wolopts |= WAKE_BCAST;
1019 if (options & MWF)
1020 wolopts |= WAKE_MCAST;
1021
1022 return wolopts;
1023}
1024
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001025static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1026{
1027 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001028
1029 spin_lock_irq(&tp->lock);
1030
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001031 wol->supported = WAKE_ANY;
1032 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001033
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001034 spin_unlock_irq(&tp->lock);
1035}
1036
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001037static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001038{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001039 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001040 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001041 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001042 u32 opt;
1043 u16 reg;
1044 u8 mask;
1045 } cfg[] = {
1046 { WAKE_ANY, Config1, PMEnable },
1047 { WAKE_PHY, Config3, LinkUp },
1048 { WAKE_MAGIC, Config3, MagicPacket },
1049 { WAKE_UCAST, Config5, UWF },
1050 { WAKE_BCAST, Config5, BWF },
1051 { WAKE_MCAST, Config5, MWF },
1052 { WAKE_ANY, Config5, LanWake }
1053 };
1054
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001055 RTL_W8(Cfg9346, Cfg9346_Unlock);
1056
1057 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1058 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001059 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001060 options |= cfg[i].mask;
1061 RTL_W8(cfg[i].reg, options);
1062 }
1063
1064 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001065}
1066
1067static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1068{
1069 struct rtl8169_private *tp = netdev_priv(dev);
1070
1071 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001072
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001073 if (wol->wolopts)
1074 tp->features |= RTL_FEATURE_WOL;
1075 else
1076 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001077 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001078 spin_unlock_irq(&tp->lock);
1079
françois romieuea809072010-11-08 13:23:58 +00001080 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1081
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001082 return 0;
1083}
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085static void rtl8169_get_drvinfo(struct net_device *dev,
1086 struct ethtool_drvinfo *info)
1087{
1088 struct rtl8169_private *tp = netdev_priv(dev);
1089
1090 strcpy(info->driver, MODULENAME);
1091 strcpy(info->version, RTL8169_VERSION);
1092 strcpy(info->bus_info, pci_name(tp->pci_dev));
1093}
1094
1095static int rtl8169_get_regs_len(struct net_device *dev)
1096{
1097 return R8169_REGS_SIZE;
1098}
1099
1100static int rtl8169_set_speed_tbi(struct net_device *dev,
1101 u8 autoneg, u16 speed, u8 duplex)
1102{
1103 struct rtl8169_private *tp = netdev_priv(dev);
1104 void __iomem *ioaddr = tp->mmio_addr;
1105 int ret = 0;
1106 u32 reg;
1107
1108 reg = RTL_R32(TBICSR);
1109 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1110 (duplex == DUPLEX_FULL)) {
1111 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1112 } else if (autoneg == AUTONEG_ENABLE)
1113 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1114 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001115 netif_warn(tp, link, dev,
1116 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 ret = -EOPNOTSUPP;
1118 }
1119
1120 return ret;
1121}
1122
1123static int rtl8169_set_speed_xmii(struct net_device *dev,
1124 u8 autoneg, u16 speed, u8 duplex)
1125{
1126 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001127 int giga_ctrl, bmcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001130 int auto_nego;
1131
françois romieu4da19632011-01-03 15:07:55 +00001132 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001133 auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
1134 ADVERTISE_100HALF | ADVERTISE_100FULL);
françois romieu3577aa12009-05-19 10:46:48 +00001135 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1136
françois romieu4da19632011-01-03 15:07:55 +00001137 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001138 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1139
1140 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1141 if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
1142 (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
1143 (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
1144 (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
1145 (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
1146 (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
1147 (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
1148 (tp->mac_version != RTL_GIGA_MAC_VER_16)) {
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001149 giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
Joe Perchesbf82c182010-02-09 11:49:50 +00001150 } else {
1151 netif_info(tp, link, dev,
1152 "PHY does not support 1000Mbps\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
françois romieu3577aa12009-05-19 10:46:48 +00001155 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001156
françois romieu3577aa12009-05-19 10:46:48 +00001157 if ((tp->mac_version == RTL_GIGA_MAC_VER_11) ||
1158 (tp->mac_version == RTL_GIGA_MAC_VER_12) ||
1159 (tp->mac_version >= RTL_GIGA_MAC_VER_17)) {
1160 /*
1161 * Wake up the PHY.
1162 * Vendor specific (0x1f) and reserved (0x0e) MII
1163 * registers.
1164 */
françois romieu4da19632011-01-03 15:07:55 +00001165 rtl_writephy(tp, 0x1f, 0x0000);
1166 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001167 }
1168
françois romieu4da19632011-01-03 15:07:55 +00001169 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1170 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001171 } else {
1172 giga_ctrl = 0;
1173
1174 if (speed == SPEED_10)
1175 bmcr = 0;
1176 else if (speed == SPEED_100)
1177 bmcr = BMCR_SPEED100;
1178 else
1179 return -EINVAL;
1180
1181 if (duplex == DUPLEX_FULL)
1182 bmcr |= BMCR_FULLDPLX;
1183
françois romieu4da19632011-01-03 15:07:55 +00001184 rtl_writephy(tp, 0x1f, 0x0000);
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. */
Francois Romieu2857ffb2008-08-02 21:08:49 +02001564 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1565 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1566 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1567 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1568 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1569 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001570 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001571 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001572 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001573 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1574 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001575 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1576 /* FIXME: where did these entries come from ? -- FR */
1577 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1578 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1579
1580 /* 8110 family. */
1581 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1582 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1583 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1584 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1585 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1586 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1587
Jean Delvaref21b75e2009-05-26 20:54:48 -07001588 /* Catch-all */
1589 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }, *p = mac_info;
1591 u32 reg;
1592
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001593 reg = RTL_R32(TxConfig);
1594 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 p++;
1596 tp->mac_version = p->mac_version;
1597}
1598
1599static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1600{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001601 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
Francois Romieu867763c2007-08-17 18:21:58 +02001604struct phy_reg {
1605 u16 reg;
1606 u16 val;
1607};
1608
françois romieu4da19632011-01-03 15:07:55 +00001609static void rtl_writephy_batch(struct rtl8169_private *tp,
1610 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001611{
1612 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001613 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001614 regs++;
1615 }
1616}
1617
françois romieubca03d52011-01-03 15:07:31 +00001618#define PHY_READ 0x00000000
1619#define PHY_DATA_OR 0x10000000
1620#define PHY_DATA_AND 0x20000000
1621#define PHY_BJMPN 0x30000000
1622#define PHY_READ_EFUSE 0x40000000
1623#define PHY_READ_MAC_BYTE 0x50000000
1624#define PHY_WRITE_MAC_BYTE 0x60000000
1625#define PHY_CLEAR_READCOUNT 0x70000000
1626#define PHY_WRITE 0x80000000
1627#define PHY_READCOUNT_EQ_SKIP 0x90000000
1628#define PHY_COMP_EQ_SKIPN 0xa0000000
1629#define PHY_COMP_NEQ_SKIPN 0xb0000000
1630#define PHY_WRITE_PREVIOUS 0xc0000000
1631#define PHY_SKIPN 0xd0000000
1632#define PHY_DELAY_MS 0xe0000000
1633#define PHY_WRITE_ERI_WORD 0xf0000000
1634
1635static void
1636rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1637{
françois romieubca03d52011-01-03 15:07:31 +00001638 __le32 *phytable = (__le32 *)fw->data;
1639 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001640 size_t index, fw_size = fw->size / sizeof(*phytable);
1641 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001642
1643 if (fw->size % sizeof(*phytable)) {
1644 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1645 return;
1646 }
1647
hayeswang42b82dc2011-01-10 02:07:25 +00001648 for (index = 0; index < fw_size; index++) {
1649 u32 action = le32_to_cpu(phytable[index]);
1650 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001651
hayeswang42b82dc2011-01-10 02:07:25 +00001652 switch(action & 0xf0000000) {
1653 case PHY_READ:
1654 case PHY_DATA_OR:
1655 case PHY_DATA_AND:
1656 case PHY_READ_EFUSE:
1657 case PHY_CLEAR_READCOUNT:
1658 case PHY_WRITE:
1659 case PHY_WRITE_PREVIOUS:
1660 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001661 break;
1662
hayeswang42b82dc2011-01-10 02:07:25 +00001663 case PHY_BJMPN:
1664 if (regno > index) {
1665 netif_err(tp, probe, tp->dev,
1666 "Out of range of firmware\n");
1667 return;
1668 }
1669 break;
1670 case PHY_READCOUNT_EQ_SKIP:
1671 if (index + 2 >= fw_size) {
1672 netif_err(tp, probe, tp->dev,
1673 "Out of range of firmware\n");
1674 return;
1675 }
1676 break;
1677 case PHY_COMP_EQ_SKIPN:
1678 case PHY_COMP_NEQ_SKIPN:
1679 case PHY_SKIPN:
1680 if (index + 1 + regno >= fw_size) {
1681 netif_err(tp, probe, tp->dev,
1682 "Out of range of firmware\n");
1683 return;
1684 }
1685 break;
1686
1687 case PHY_READ_MAC_BYTE:
1688 case PHY_WRITE_MAC_BYTE:
1689 case PHY_WRITE_ERI_WORD:
1690 default:
1691 netif_err(tp, probe, tp->dev,
1692 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001693 return;
1694 }
1695 }
1696
hayeswang42b82dc2011-01-10 02:07:25 +00001697 predata = 0;
1698 count = 0;
1699
1700 for (index = 0; index < fw_size; ) {
1701 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001702 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001703 u32 regno = (action & 0x0fff0000) >> 16;
1704
1705 if (!action)
1706 break;
françois romieubca03d52011-01-03 15:07:31 +00001707
1708 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001709 case PHY_READ:
1710 predata = rtl_readphy(tp, regno);
1711 count++;
1712 index++;
françois romieubca03d52011-01-03 15:07:31 +00001713 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001714 case PHY_DATA_OR:
1715 predata |= data;
1716 index++;
1717 break;
1718 case PHY_DATA_AND:
1719 predata &= data;
1720 index++;
1721 break;
1722 case PHY_BJMPN:
1723 index -= regno;
1724 break;
1725 case PHY_READ_EFUSE:
1726 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1727 index++;
1728 break;
1729 case PHY_CLEAR_READCOUNT:
1730 count = 0;
1731 index++;
1732 break;
1733 case PHY_WRITE:
1734 rtl_writephy(tp, regno, data);
1735 index++;
1736 break;
1737 case PHY_READCOUNT_EQ_SKIP:
1738 if (count == data)
1739 index += 2;
1740 else
1741 index += 1;
1742 break;
1743 case PHY_COMP_EQ_SKIPN:
1744 if (predata == data)
1745 index += regno;
1746 index++;
1747 break;
1748 case PHY_COMP_NEQ_SKIPN:
1749 if (predata != data)
1750 index += regno;
1751 index++;
1752 break;
1753 case PHY_WRITE_PREVIOUS:
1754 rtl_writephy(tp, regno, predata);
1755 index++;
1756 break;
1757 case PHY_SKIPN:
1758 index += regno + 1;
1759 break;
1760 case PHY_DELAY_MS:
1761 mdelay(data);
1762 index++;
1763 break;
1764
1765 case PHY_READ_MAC_BYTE:
1766 case PHY_WRITE_MAC_BYTE:
1767 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001768 default:
1769 BUG();
1770 }
1771 }
1772}
1773
françois romieuf1e02ed2011-01-13 13:07:53 +00001774static void rtl_release_firmware(struct rtl8169_private *tp)
1775{
1776 release_firmware(tp->fw);
1777 tp->fw = NULL;
1778}
1779
1780static int rtl_apply_firmware(struct rtl8169_private *tp, const char *fw_name)
1781{
1782 const struct firmware **fw = &tp->fw;
1783 int rc = !*fw;
1784
1785 if (rc) {
1786 rc = request_firmware(fw, fw_name, &tp->pci_dev->dev);
1787 if (rc < 0)
1788 goto out;
1789 }
1790
1791 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1792 rtl_phy_write_fw(tp, *fw);
1793out:
1794 return rc;
1795}
1796
françois romieu4da19632011-01-03 15:07:55 +00001797static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001799 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001800 { 0x1f, 0x0001 },
1801 { 0x06, 0x006e },
1802 { 0x08, 0x0708 },
1803 { 0x15, 0x4000 },
1804 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
françois romieu0b9b5712009-08-10 19:44:56 +00001806 { 0x1f, 0x0001 },
1807 { 0x03, 0x00a1 },
1808 { 0x02, 0x0008 },
1809 { 0x01, 0x0120 },
1810 { 0x00, 0x1000 },
1811 { 0x04, 0x0800 },
1812 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
françois romieu0b9b5712009-08-10 19:44:56 +00001814 { 0x03, 0xff41 },
1815 { 0x02, 0xdf60 },
1816 { 0x01, 0x0140 },
1817 { 0x00, 0x0077 },
1818 { 0x04, 0x7800 },
1819 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
françois romieu0b9b5712009-08-10 19:44:56 +00001821 { 0x03, 0x802f },
1822 { 0x02, 0x4f02 },
1823 { 0x01, 0x0409 },
1824 { 0x00, 0xf0f9 },
1825 { 0x04, 0x9800 },
1826 { 0x04, 0x9000 },
1827
1828 { 0x03, 0xdf01 },
1829 { 0x02, 0xdf20 },
1830 { 0x01, 0xff95 },
1831 { 0x00, 0xba00 },
1832 { 0x04, 0xa800 },
1833 { 0x04, 0xa000 },
1834
1835 { 0x03, 0xff41 },
1836 { 0x02, 0xdf20 },
1837 { 0x01, 0x0140 },
1838 { 0x00, 0x00bb },
1839 { 0x04, 0xb800 },
1840 { 0x04, 0xb000 },
1841
1842 { 0x03, 0xdf41 },
1843 { 0x02, 0xdc60 },
1844 { 0x01, 0x6340 },
1845 { 0x00, 0x007d },
1846 { 0x04, 0xd800 },
1847 { 0x04, 0xd000 },
1848
1849 { 0x03, 0xdf01 },
1850 { 0x02, 0xdf20 },
1851 { 0x01, 0x100a },
1852 { 0x00, 0xa0ff },
1853 { 0x04, 0xf800 },
1854 { 0x04, 0xf000 },
1855
1856 { 0x1f, 0x0000 },
1857 { 0x0b, 0x0000 },
1858 { 0x00, 0x9200 }
1859 };
1860
françois romieu4da19632011-01-03 15:07:55 +00001861 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
françois romieu4da19632011-01-03 15:07:55 +00001864static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001865{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001866 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001867 { 0x1f, 0x0002 },
1868 { 0x01, 0x90d0 },
1869 { 0x1f, 0x0000 }
1870 };
1871
françois romieu4da19632011-01-03 15:07:55 +00001872 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001873}
1874
françois romieu4da19632011-01-03 15:07:55 +00001875static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001876{
1877 struct pci_dev *pdev = tp->pci_dev;
1878 u16 vendor_id, device_id;
1879
1880 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1881 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1882
1883 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1884 return;
1885
françois romieu4da19632011-01-03 15:07:55 +00001886 rtl_writephy(tp, 0x1f, 0x0001);
1887 rtl_writephy(tp, 0x10, 0xf01b);
1888 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001889}
1890
françois romieu4da19632011-01-03 15:07:55 +00001891static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001892{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001893 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00001894 { 0x1f, 0x0001 },
1895 { 0x04, 0x0000 },
1896 { 0x03, 0x00a1 },
1897 { 0x02, 0x0008 },
1898 { 0x01, 0x0120 },
1899 { 0x00, 0x1000 },
1900 { 0x04, 0x0800 },
1901 { 0x04, 0x9000 },
1902 { 0x03, 0x802f },
1903 { 0x02, 0x4f02 },
1904 { 0x01, 0x0409 },
1905 { 0x00, 0xf099 },
1906 { 0x04, 0x9800 },
1907 { 0x04, 0xa000 },
1908 { 0x03, 0xdf01 },
1909 { 0x02, 0xdf20 },
1910 { 0x01, 0xff95 },
1911 { 0x00, 0xba00 },
1912 { 0x04, 0xa800 },
1913 { 0x04, 0xf000 },
1914 { 0x03, 0xdf01 },
1915 { 0x02, 0xdf20 },
1916 { 0x01, 0x101a },
1917 { 0x00, 0xa0ff },
1918 { 0x04, 0xf800 },
1919 { 0x04, 0x0000 },
1920 { 0x1f, 0x0000 },
1921
1922 { 0x1f, 0x0001 },
1923 { 0x10, 0xf41b },
1924 { 0x14, 0xfb54 },
1925 { 0x18, 0xf5c7 },
1926 { 0x1f, 0x0000 },
1927
1928 { 0x1f, 0x0001 },
1929 { 0x17, 0x0cc0 },
1930 { 0x1f, 0x0000 }
1931 };
1932
françois romieu4da19632011-01-03 15:07:55 +00001933 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00001934
françois romieu4da19632011-01-03 15:07:55 +00001935 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00001936}
1937
françois romieu4da19632011-01-03 15:07:55 +00001938static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00001939{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001940 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00001941 { 0x1f, 0x0001 },
1942 { 0x04, 0x0000 },
1943 { 0x03, 0x00a1 },
1944 { 0x02, 0x0008 },
1945 { 0x01, 0x0120 },
1946 { 0x00, 0x1000 },
1947 { 0x04, 0x0800 },
1948 { 0x04, 0x9000 },
1949 { 0x03, 0x802f },
1950 { 0x02, 0x4f02 },
1951 { 0x01, 0x0409 },
1952 { 0x00, 0xf099 },
1953 { 0x04, 0x9800 },
1954 { 0x04, 0xa000 },
1955 { 0x03, 0xdf01 },
1956 { 0x02, 0xdf20 },
1957 { 0x01, 0xff95 },
1958 { 0x00, 0xba00 },
1959 { 0x04, 0xa800 },
1960 { 0x04, 0xf000 },
1961 { 0x03, 0xdf01 },
1962 { 0x02, 0xdf20 },
1963 { 0x01, 0x101a },
1964 { 0x00, 0xa0ff },
1965 { 0x04, 0xf800 },
1966 { 0x04, 0x0000 },
1967 { 0x1f, 0x0000 },
1968
1969 { 0x1f, 0x0001 },
1970 { 0x0b, 0x8480 },
1971 { 0x1f, 0x0000 },
1972
1973 { 0x1f, 0x0001 },
1974 { 0x18, 0x67c7 },
1975 { 0x04, 0x2000 },
1976 { 0x03, 0x002f },
1977 { 0x02, 0x4360 },
1978 { 0x01, 0x0109 },
1979 { 0x00, 0x3022 },
1980 { 0x04, 0x2800 },
1981 { 0x1f, 0x0000 },
1982
1983 { 0x1f, 0x0001 },
1984 { 0x17, 0x0cc0 },
1985 { 0x1f, 0x0000 }
1986 };
1987
françois romieu4da19632011-01-03 15:07:55 +00001988 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00001989}
1990
françois romieu4da19632011-01-03 15:07:55 +00001991static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02001992{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001993 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02001994 { 0x10, 0xf41b },
1995 { 0x1f, 0x0000 }
1996 };
1997
françois romieu4da19632011-01-03 15:07:55 +00001998 rtl_writephy(tp, 0x1f, 0x0001);
1999 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002000
françois romieu4da19632011-01-03 15:07:55 +00002001 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002002}
2003
françois romieu4da19632011-01-03 15:07:55 +00002004static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002005{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002006 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002007 { 0x1f, 0x0001 },
2008 { 0x10, 0xf41b },
2009 { 0x1f, 0x0000 }
2010 };
2011
françois romieu4da19632011-01-03 15:07:55 +00002012 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002013}
2014
françois romieu4da19632011-01-03 15:07:55 +00002015static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002016{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002017 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002018 { 0x1f, 0x0000 },
2019 { 0x1d, 0x0f00 },
2020 { 0x1f, 0x0002 },
2021 { 0x0c, 0x1ec8 },
2022 { 0x1f, 0x0000 }
2023 };
2024
françois romieu4da19632011-01-03 15:07:55 +00002025 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002026}
2027
françois romieu4da19632011-01-03 15:07:55 +00002028static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002029{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002030 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002031 { 0x1f, 0x0001 },
2032 { 0x1d, 0x3d98 },
2033 { 0x1f, 0x0000 }
2034 };
2035
françois romieu4da19632011-01-03 15:07:55 +00002036 rtl_writephy(tp, 0x1f, 0x0000);
2037 rtl_patchphy(tp, 0x14, 1 << 5);
2038 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002039
françois romieu4da19632011-01-03 15:07:55 +00002040 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002041}
2042
françois romieu4da19632011-01-03 15:07:55 +00002043static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002044{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002045 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002046 { 0x1f, 0x0001 },
2047 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002048 { 0x1f, 0x0002 },
2049 { 0x00, 0x88d4 },
2050 { 0x01, 0x82b1 },
2051 { 0x03, 0x7002 },
2052 { 0x08, 0x9e30 },
2053 { 0x09, 0x01f0 },
2054 { 0x0a, 0x5500 },
2055 { 0x0c, 0x00c8 },
2056 { 0x1f, 0x0003 },
2057 { 0x12, 0xc096 },
2058 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002059 { 0x1f, 0x0000 },
2060 { 0x1f, 0x0000 },
2061 { 0x09, 0x2000 },
2062 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002063 };
2064
françois romieu4da19632011-01-03 15:07:55 +00002065 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002066
françois romieu4da19632011-01-03 15:07:55 +00002067 rtl_patchphy(tp, 0x14, 1 << 5);
2068 rtl_patchphy(tp, 0x0d, 1 << 5);
2069 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002070}
2071
françois romieu4da19632011-01-03 15:07:55 +00002072static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002073{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002074 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002075 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002076 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002077 { 0x03, 0x802f },
2078 { 0x02, 0x4f02 },
2079 { 0x01, 0x0409 },
2080 { 0x00, 0xf099 },
2081 { 0x04, 0x9800 },
2082 { 0x04, 0x9000 },
2083 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002084 { 0x1f, 0x0002 },
2085 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002086 { 0x06, 0x0761 },
2087 { 0x1f, 0x0003 },
2088 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002089 { 0x1f, 0x0000 }
2090 };
2091
françois romieu4da19632011-01-03 15:07:55 +00002092 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002093
françois romieu4da19632011-01-03 15:07:55 +00002094 rtl_patchphy(tp, 0x16, 1 << 0);
2095 rtl_patchphy(tp, 0x14, 1 << 5);
2096 rtl_patchphy(tp, 0x0d, 1 << 5);
2097 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002098}
2099
françois romieu4da19632011-01-03 15:07:55 +00002100static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002101{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002102 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002103 { 0x1f, 0x0001 },
2104 { 0x12, 0x2300 },
2105 { 0x1d, 0x3d98 },
2106 { 0x1f, 0x0002 },
2107 { 0x0c, 0x7eb8 },
2108 { 0x06, 0x5461 },
2109 { 0x1f, 0x0003 },
2110 { 0x16, 0x0f0a },
2111 { 0x1f, 0x0000 }
2112 };
2113
françois romieu4da19632011-01-03 15:07:55 +00002114 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002115
françois romieu4da19632011-01-03 15:07:55 +00002116 rtl_patchphy(tp, 0x16, 1 << 0);
2117 rtl_patchphy(tp, 0x14, 1 << 5);
2118 rtl_patchphy(tp, 0x0d, 1 << 5);
2119 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002120}
2121
françois romieu4da19632011-01-03 15:07:55 +00002122static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002123{
françois romieu4da19632011-01-03 15:07:55 +00002124 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002125}
2126
françois romieubca03d52011-01-03 15:07:31 +00002127static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002128{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002129 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002130 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002131 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002132 { 0x06, 0x4064 },
2133 { 0x07, 0x2863 },
2134 { 0x08, 0x059c },
2135 { 0x09, 0x26b4 },
2136 { 0x0a, 0x6a19 },
2137 { 0x0b, 0xdcc8 },
2138 { 0x10, 0xf06d },
2139 { 0x14, 0x7f68 },
2140 { 0x18, 0x7fd9 },
2141 { 0x1c, 0xf0ff },
2142 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002143 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002144 { 0x12, 0xf49f },
2145 { 0x13, 0x070b },
2146 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002147 { 0x14, 0x94c0 },
2148
2149 /*
2150 * Tx Error Issue
2151 * enhance line driver power
2152 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002153 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002154 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002155 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002156 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002157 { 0x06, 0x5561 },
2158
2159 /*
2160 * Can not link to 1Gbps with bad cable
2161 * Decrease SNR threshold form 21.07dB to 19.04dB
2162 */
2163 { 0x1f, 0x0001 },
2164 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002165
2166 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002167 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002168 };
françois romieubca03d52011-01-03 15:07:31 +00002169 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002170
françois romieu4da19632011-01-03 15:07:55 +00002171 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002172
françois romieubca03d52011-01-03 15:07:31 +00002173 /*
2174 * Rx Error Issue
2175 * Fine Tune Switching regulator parameter
2176 */
françois romieu4da19632011-01-03 15:07:55 +00002177 rtl_writephy(tp, 0x1f, 0x0002);
2178 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2179 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002180
françois romieudaf9df62009-10-07 12:44:20 +00002181 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002182 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002183 { 0x1f, 0x0002 },
2184 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002185 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002186 { 0x05, 0x8330 },
2187 { 0x06, 0x669a },
2188 { 0x1f, 0x0002 }
2189 };
2190 int val;
2191
françois romieu4da19632011-01-03 15:07:55 +00002192 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002193
françois romieu4da19632011-01-03 15:07:55 +00002194 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002195
2196 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002197 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002198 0x0065, 0x0066, 0x0067, 0x0068,
2199 0x0069, 0x006a, 0x006b, 0x006c
2200 };
2201 int i;
2202
françois romieu4da19632011-01-03 15:07:55 +00002203 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002204
2205 val &= 0xff00;
2206 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002207 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002208 }
2209 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002210 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002211 { 0x1f, 0x0002 },
2212 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002213 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002214 { 0x05, 0x8330 },
2215 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002216 };
2217
françois romieu4da19632011-01-03 15:07:55 +00002218 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002219 }
2220
françois romieubca03d52011-01-03 15:07:31 +00002221 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002222 rtl_writephy(tp, 0x1f, 0x0002);
2223 rtl_patchphy(tp, 0x0d, 0x0300);
2224 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002225
françois romieubca03d52011-01-03 15:07:31 +00002226 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002227 rtl_writephy(tp, 0x1f, 0x0002);
2228 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2229 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002230
françois romieu4da19632011-01-03 15:07:55 +00002231 rtl_writephy(tp, 0x1f, 0x0005);
2232 rtl_writephy(tp, 0x05, 0x001b);
françois romieuf1e02ed2011-01-13 13:07:53 +00002233 if ((rtl_readphy(tp, 0x06) != 0xbf00) ||
2234 (rtl_apply_firmware(tp, FIRMWARE_8168D_1) < 0)) {
françois romieubca03d52011-01-03 15:07:31 +00002235 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2236 }
2237
françois romieu4da19632011-01-03 15:07:55 +00002238 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002239}
2240
françois romieubca03d52011-01-03 15:07:31 +00002241static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002242{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002243 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002244 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002245 { 0x1f, 0x0001 },
2246 { 0x06, 0x4064 },
2247 { 0x07, 0x2863 },
2248 { 0x08, 0x059c },
2249 { 0x09, 0x26b4 },
2250 { 0x0a, 0x6a19 },
2251 { 0x0b, 0xdcc8 },
2252 { 0x10, 0xf06d },
2253 { 0x14, 0x7f68 },
2254 { 0x18, 0x7fd9 },
2255 { 0x1c, 0xf0ff },
2256 { 0x1d, 0x3d9c },
2257 { 0x1f, 0x0003 },
2258 { 0x12, 0xf49f },
2259 { 0x13, 0x070b },
2260 { 0x1a, 0x05ad },
2261 { 0x14, 0x94c0 },
2262
françois romieubca03d52011-01-03 15:07:31 +00002263 /*
2264 * Tx Error Issue
2265 * enhance line driver power
2266 */
françois romieudaf9df62009-10-07 12:44:20 +00002267 { 0x1f, 0x0002 },
2268 { 0x06, 0x5561 },
2269 { 0x1f, 0x0005 },
2270 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002271 { 0x06, 0x5561 },
2272
2273 /*
2274 * Can not link to 1Gbps with bad cable
2275 * Decrease SNR threshold form 21.07dB to 19.04dB
2276 */
2277 { 0x1f, 0x0001 },
2278 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002279
2280 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002281 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002282 };
françois romieubca03d52011-01-03 15:07:31 +00002283 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002284
françois romieu4da19632011-01-03 15:07:55 +00002285 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002286
2287 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002288 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002289 { 0x1f, 0x0002 },
2290 { 0x05, 0x669a },
2291 { 0x1f, 0x0005 },
2292 { 0x05, 0x8330 },
2293 { 0x06, 0x669a },
2294
2295 { 0x1f, 0x0002 }
2296 };
2297 int val;
2298
françois romieu4da19632011-01-03 15:07:55 +00002299 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002300
françois romieu4da19632011-01-03 15:07:55 +00002301 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002302 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002303 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002304 0x0065, 0x0066, 0x0067, 0x0068,
2305 0x0069, 0x006a, 0x006b, 0x006c
2306 };
2307 int i;
2308
françois romieu4da19632011-01-03 15:07:55 +00002309 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002310
2311 val &= 0xff00;
2312 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002313 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002314 }
2315 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002316 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002317 { 0x1f, 0x0002 },
2318 { 0x05, 0x2642 },
2319 { 0x1f, 0x0005 },
2320 { 0x05, 0x8330 },
2321 { 0x06, 0x2642 }
2322 };
2323
françois romieu4da19632011-01-03 15:07:55 +00002324 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002325 }
2326
françois romieubca03d52011-01-03 15:07:31 +00002327 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002328 rtl_writephy(tp, 0x1f, 0x0002);
2329 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2330 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002331
françois romieubca03d52011-01-03 15:07:31 +00002332 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002333 rtl_writephy(tp, 0x1f, 0x0002);
2334 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002335
françois romieu4da19632011-01-03 15:07:55 +00002336 rtl_writephy(tp, 0x1f, 0x0005);
2337 rtl_writephy(tp, 0x05, 0x001b);
françois romieuf1e02ed2011-01-13 13:07:53 +00002338 if ((rtl_readphy(tp, 0x06) != 0xb300) ||
2339 (rtl_apply_firmware(tp, FIRMWARE_8168D_2) < 0)) {
françois romieubca03d52011-01-03 15:07:31 +00002340 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2341 }
2342
françois romieu4da19632011-01-03 15:07:55 +00002343 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002344}
2345
françois romieu4da19632011-01-03 15:07:55 +00002346static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002347{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002348 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002349 { 0x1f, 0x0002 },
2350 { 0x10, 0x0008 },
2351 { 0x0d, 0x006c },
2352
2353 { 0x1f, 0x0000 },
2354 { 0x0d, 0xf880 },
2355
2356 { 0x1f, 0x0001 },
2357 { 0x17, 0x0cc0 },
2358
2359 { 0x1f, 0x0001 },
2360 { 0x0b, 0xa4d8 },
2361 { 0x09, 0x281c },
2362 { 0x07, 0x2883 },
2363 { 0x0a, 0x6b35 },
2364 { 0x1d, 0x3da4 },
2365 { 0x1c, 0xeffd },
2366 { 0x14, 0x7f52 },
2367 { 0x18, 0x7fc6 },
2368 { 0x08, 0x0601 },
2369 { 0x06, 0x4063 },
2370 { 0x10, 0xf074 },
2371 { 0x1f, 0x0003 },
2372 { 0x13, 0x0789 },
2373 { 0x12, 0xf4bd },
2374 { 0x1a, 0x04fd },
2375 { 0x14, 0x84b0 },
2376 { 0x1f, 0x0000 },
2377 { 0x00, 0x9200 },
2378
2379 { 0x1f, 0x0005 },
2380 { 0x01, 0x0340 },
2381 { 0x1f, 0x0001 },
2382 { 0x04, 0x4000 },
2383 { 0x03, 0x1d21 },
2384 { 0x02, 0x0c32 },
2385 { 0x01, 0x0200 },
2386 { 0x00, 0x5554 },
2387 { 0x04, 0x4800 },
2388 { 0x04, 0x4000 },
2389 { 0x04, 0xf000 },
2390 { 0x03, 0xdf01 },
2391 { 0x02, 0xdf20 },
2392 { 0x01, 0x101a },
2393 { 0x00, 0xa0ff },
2394 { 0x04, 0xf800 },
2395 { 0x04, 0xf000 },
2396 { 0x1f, 0x0000 },
2397
2398 { 0x1f, 0x0007 },
2399 { 0x1e, 0x0023 },
2400 { 0x16, 0x0000 },
2401 { 0x1f, 0x0000 }
2402 };
2403
françois romieu4da19632011-01-03 15:07:55 +00002404 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002405}
2406
françois romieue6de30d2011-01-03 15:08:37 +00002407static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2408{
2409 static const struct phy_reg phy_reg_init[] = {
2410 { 0x1f, 0x0001 },
2411 { 0x17, 0x0cc0 },
2412
2413 { 0x1f, 0x0007 },
2414 { 0x1e, 0x002d },
2415 { 0x18, 0x0040 },
2416 { 0x1f, 0x0000 }
2417 };
2418
2419 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2420 rtl_patchphy(tp, 0x0d, 1 << 5);
2421}
2422
françois romieu4da19632011-01-03 15:07:55 +00002423static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002424{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002425 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002426 { 0x1f, 0x0003 },
2427 { 0x08, 0x441d },
2428 { 0x01, 0x9100 },
2429 { 0x1f, 0x0000 }
2430 };
2431
françois romieu4da19632011-01-03 15:07:55 +00002432 rtl_writephy(tp, 0x1f, 0x0000);
2433 rtl_patchphy(tp, 0x11, 1 << 12);
2434 rtl_patchphy(tp, 0x19, 1 << 13);
2435 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002436
françois romieu4da19632011-01-03 15:07:55 +00002437 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002438}
2439
Francois Romieu5615d9f2007-08-17 17:50:46 +02002440static void rtl_hw_phy_config(struct net_device *dev)
2441{
2442 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002443
2444 rtl8169_print_mac_version(tp);
2445
2446 switch (tp->mac_version) {
2447 case RTL_GIGA_MAC_VER_01:
2448 break;
2449 case RTL_GIGA_MAC_VER_02:
2450 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002451 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002452 break;
2453 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002454 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002455 break;
françois romieu2e9558562009-08-10 19:44:19 +00002456 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002457 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002458 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002459 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002460 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002461 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002462 case RTL_GIGA_MAC_VER_07:
2463 case RTL_GIGA_MAC_VER_08:
2464 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002465 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002466 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002467 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002468 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002469 break;
2470 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002471 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002472 break;
2473 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002474 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002475 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002476 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002477 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002478 break;
2479 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002480 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002481 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002482 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002483 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002484 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002485 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002486 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002487 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002488 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002489 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002490 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002491 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002492 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002493 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002494 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002495 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002496 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002497 break;
2498 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002499 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002500 break;
2501 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002502 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002503 break;
françois romieue6de30d2011-01-03 15:08:37 +00002504 case RTL_GIGA_MAC_VER_28:
2505 rtl8168d_4_hw_phy_config(tp);
2506 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002507
Francois Romieu5615d9f2007-08-17 17:50:46 +02002508 default:
2509 break;
2510 }
2511}
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513static void rtl8169_phy_timer(unsigned long __opaque)
2514{
2515 struct net_device *dev = (struct net_device *)__opaque;
2516 struct rtl8169_private *tp = netdev_priv(dev);
2517 struct timer_list *timer = &tp->timer;
2518 void __iomem *ioaddr = tp->mmio_addr;
2519 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2520
Francois Romieubcf0bf92006-07-26 23:14:13 +02002521 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Francois Romieu64e4bfb2006-08-17 12:43:06 +02002523 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return;
2525
2526 spin_lock_irq(&tp->lock);
2527
françois romieu4da19632011-01-03 15:07:55 +00002528 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002529 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 * A busy loop could burn quite a few cycles on nowadays CPU.
2531 * Let's delay the execution of the timer for a few ticks.
2532 */
2533 timeout = HZ/10;
2534 goto out_mod_timer;
2535 }
2536
2537 if (tp->link_ok(ioaddr))
2538 goto out_unlock;
2539
Joe Perchesbf82c182010-02-09 11:49:50 +00002540 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
françois romieu4da19632011-01-03 15:07:55 +00002542 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544out_mod_timer:
2545 mod_timer(timer, jiffies + timeout);
2546out_unlock:
2547 spin_unlock_irq(&tp->lock);
2548}
2549
2550static inline void rtl8169_delete_timer(struct net_device *dev)
2551{
2552 struct rtl8169_private *tp = netdev_priv(dev);
2553 struct timer_list *timer = &tp->timer;
2554
Francois Romieue179bb72007-08-17 15:05:21 +02002555 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return;
2557
2558 del_timer_sync(timer);
2559}
2560
2561static inline void rtl8169_request_timer(struct net_device *dev)
2562{
2563 struct rtl8169_private *tp = netdev_priv(dev);
2564 struct timer_list *timer = &tp->timer;
2565
Francois Romieue179bb72007-08-17 15:05:21 +02002566 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return;
2568
Francois Romieu2efa53f2007-03-09 00:00:05 +01002569 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
2572#ifdef CONFIG_NET_POLL_CONTROLLER
2573/*
2574 * Polling 'interrupt' - used by things like netconsole to send skbs
2575 * without having to re-enable interrupts. It's not called while
2576 * the interrupt routine is executing.
2577 */
2578static void rtl8169_netpoll(struct net_device *dev)
2579{
2580 struct rtl8169_private *tp = netdev_priv(dev);
2581 struct pci_dev *pdev = tp->pci_dev;
2582
2583 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002584 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 enable_irq(pdev->irq);
2586}
2587#endif
2588
2589static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2590 void __iomem *ioaddr)
2591{
2592 iounmap(ioaddr);
2593 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002594 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 pci_disable_device(pdev);
2596 free_netdev(dev);
2597}
2598
Francois Romieubf793292006-11-01 00:53:05 +01002599static void rtl8169_phy_reset(struct net_device *dev,
2600 struct rtl8169_private *tp)
2601{
Francois Romieu07d3f512007-02-21 22:40:46 +01002602 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002603
françois romieu4da19632011-01-03 15:07:55 +00002604 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002605 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002606 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002607 return;
2608 msleep(1);
2609 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002610 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002611}
2612
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002613static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002615 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002616
Francois Romieu5615d9f2007-08-17 17:50:46 +02002617 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002618
Marcus Sundberg773328942008-07-10 21:28:08 +02002619 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2620 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2621 RTL_W8(0x82, 0x01);
2622 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002623
Francois Romieu6dccd162007-02-13 23:38:05 +01002624 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2625
2626 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2627 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002628
Francois Romieubcf0bf92006-07-26 23:14:13 +02002629 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002630 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2631 RTL_W8(0x82, 0x01);
2632 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002633 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002634 }
2635
Francois Romieubf793292006-11-01 00:53:05 +01002636 rtl8169_phy_reset(dev, tp);
2637
Francois Romieu901dda22007-02-21 00:10:20 +01002638 /*
2639 * rtl8169_set_speed_xmii takes good care of the Fast Ethernet
2640 * only 8101. Don't panic.
2641 */
2642 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002643
Joe Perchesbf82c182010-02-09 11:49:50 +00002644 if (RTL_R8(PHYstatus) & TBI_Enable)
2645 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002646}
2647
Francois Romieu773d2022007-01-31 23:47:43 +01002648static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2649{
2650 void __iomem *ioaddr = tp->mmio_addr;
2651 u32 high;
2652 u32 low;
2653
2654 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2655 high = addr[4] | (addr[5] << 8);
2656
2657 spin_lock_irq(&tp->lock);
2658
2659 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002660
Francois Romieu773d2022007-01-31 23:47:43 +01002661 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002662 RTL_R32(MAC4);
2663
Francois Romieu78f1cd02010-03-27 19:35:46 -07002664 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002665 RTL_R32(MAC0);
2666
Francois Romieu773d2022007-01-31 23:47:43 +01002667 RTL_W8(Cfg9346, Cfg9346_Lock);
2668
2669 spin_unlock_irq(&tp->lock);
2670}
2671
2672static int rtl_set_mac_address(struct net_device *dev, void *p)
2673{
2674 struct rtl8169_private *tp = netdev_priv(dev);
2675 struct sockaddr *addr = p;
2676
2677 if (!is_valid_ether_addr(addr->sa_data))
2678 return -EADDRNOTAVAIL;
2679
2680 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2681
2682 rtl_rar_set(tp, dev->dev_addr);
2683
2684 return 0;
2685}
2686
Francois Romieu5f787a12006-08-17 13:02:36 +02002687static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2688{
2689 struct rtl8169_private *tp = netdev_priv(dev);
2690 struct mii_ioctl_data *data = if_mii(ifr);
2691
Francois Romieu8b4ab282008-11-19 22:05:25 -08002692 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2693}
Francois Romieu5f787a12006-08-17 13:02:36 +02002694
Francois Romieu8b4ab282008-11-19 22:05:25 -08002695static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2696{
Francois Romieu5f787a12006-08-17 13:02:36 +02002697 switch (cmd) {
2698 case SIOCGMIIPHY:
2699 data->phy_id = 32; /* Internal PHY */
2700 return 0;
2701
2702 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002703 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002704 return 0;
2705
2706 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002707 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002708 return 0;
2709 }
2710 return -EOPNOTSUPP;
2711}
2712
Francois Romieu8b4ab282008-11-19 22:05:25 -08002713static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2714{
2715 return -EOPNOTSUPP;
2716}
2717
Francois Romieu0e485152007-02-20 00:00:26 +01002718static const struct rtl_cfg_info {
2719 void (*hw_start)(struct net_device *);
2720 unsigned int region;
2721 unsigned int align;
2722 u16 intr_event;
2723 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002724 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002725 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002726} rtl_cfg_infos [] = {
2727 [RTL_CFG_0] = {
2728 .hw_start = rtl_hw_start_8169,
2729 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002730 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002731 .intr_event = SYSErr | LinkChg | RxOverflow |
2732 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002733 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002734 .features = RTL_FEATURE_GMII,
2735 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002736 },
2737 [RTL_CFG_1] = {
2738 .hw_start = rtl_hw_start_8168,
2739 .region = 2,
2740 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002741 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002742 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002743 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002744 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2745 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002746 },
2747 [RTL_CFG_2] = {
2748 .hw_start = rtl_hw_start_8101,
2749 .region = 2,
2750 .align = 8,
2751 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2752 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002753 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002754 .features = RTL_FEATURE_MSI,
2755 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002756 }
2757};
2758
Francois Romieufbac58f2007-10-04 22:51:38 +02002759/* Cfg9346_Unlock assumed. */
2760static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2761 const struct rtl_cfg_info *cfg)
2762{
2763 unsigned msi = 0;
2764 u8 cfg2;
2765
2766 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002767 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002768 if (pci_enable_msi(pdev)) {
2769 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2770 } else {
2771 cfg2 |= MSIEnable;
2772 msi = RTL_FEATURE_MSI;
2773 }
2774 }
2775 RTL_W8(Config2, cfg2);
2776 return msi;
2777}
2778
2779static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2780{
2781 if (tp->features & RTL_FEATURE_MSI) {
2782 pci_disable_msi(pdev);
2783 tp->features &= ~RTL_FEATURE_MSI;
2784 }
2785}
2786
Francois Romieu8b4ab282008-11-19 22:05:25 -08002787static const struct net_device_ops rtl8169_netdev_ops = {
2788 .ndo_open = rtl8169_open,
2789 .ndo_stop = rtl8169_close,
2790 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002791 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002792 .ndo_tx_timeout = rtl8169_tx_timeout,
2793 .ndo_validate_addr = eth_validate_addr,
2794 .ndo_change_mtu = rtl8169_change_mtu,
2795 .ndo_set_mac_address = rtl_set_mac_address,
2796 .ndo_do_ioctl = rtl8169_ioctl,
2797 .ndo_set_multicast_list = rtl_set_rx_mode,
2798#ifdef CONFIG_R8169_VLAN
2799 .ndo_vlan_rx_register = rtl8169_vlan_rx_register,
2800#endif
2801#ifdef CONFIG_NET_POLL_CONTROLLER
2802 .ndo_poll_controller = rtl8169_netpoll,
2803#endif
2804
2805};
2806
françois romieuc0e45c12011-01-03 15:08:04 +00002807static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2808{
2809 struct mdio_ops *ops = &tp->mdio_ops;
2810
2811 switch (tp->mac_version) {
2812 case RTL_GIGA_MAC_VER_27:
2813 ops->write = r8168dp_1_mdio_write;
2814 ops->read = r8168dp_1_mdio_read;
2815 break;
françois romieue6de30d2011-01-03 15:08:37 +00002816 case RTL_GIGA_MAC_VER_28:
2817 ops->write = r8168dp_2_mdio_write;
2818 ops->read = r8168dp_2_mdio_read;
2819 break;
françois romieuc0e45c12011-01-03 15:08:04 +00002820 default:
2821 ops->write = r8169_mdio_write;
2822 ops->read = r8169_mdio_read;
2823 break;
2824 }
2825}
2826
françois romieu065c27c2011-01-03 15:08:12 +00002827static void r810x_phy_power_down(struct rtl8169_private *tp)
2828{
2829 rtl_writephy(tp, 0x1f, 0x0000);
2830 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2831}
2832
2833static void r810x_phy_power_up(struct rtl8169_private *tp)
2834{
2835 rtl_writephy(tp, 0x1f, 0x0000);
2836 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2837}
2838
2839static void r810x_pll_power_down(struct rtl8169_private *tp)
2840{
2841 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2842 rtl_writephy(tp, 0x1f, 0x0000);
2843 rtl_writephy(tp, MII_BMCR, 0x0000);
2844 return;
2845 }
2846
2847 r810x_phy_power_down(tp);
2848}
2849
2850static void r810x_pll_power_up(struct rtl8169_private *tp)
2851{
2852 r810x_phy_power_up(tp);
2853}
2854
2855static void r8168_phy_power_up(struct rtl8169_private *tp)
2856{
2857 rtl_writephy(tp, 0x1f, 0x0000);
2858 rtl_writephy(tp, 0x0e, 0x0000);
2859 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2860}
2861
2862static void r8168_phy_power_down(struct rtl8169_private *tp)
2863{
2864 rtl_writephy(tp, 0x1f, 0x0000);
2865 rtl_writephy(tp, 0x0e, 0x0200);
2866 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2867}
2868
2869static void r8168_pll_power_down(struct rtl8169_private *tp)
2870{
2871 void __iomem *ioaddr = tp->mmio_addr;
2872
Hayes Wang5d2e1952011-02-22 17:26:22 +08002873 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
2874 (tp->mac_version == RTL_GIGA_MAC_VER_28)) &&
2875 (ocp_read(tp, 0x0f, 0x0010) & 0x00008000)) {
françois romieu065c27c2011-01-03 15:08:12 +00002876 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08002877 }
françois romieu065c27c2011-01-03 15:08:12 +00002878
2879 if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
2880 (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
2881 (RTL_R16(CPlusCmd) & ASF)) {
2882 return;
2883 }
2884
2885 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2886 rtl_writephy(tp, 0x1f, 0x0000);
2887 rtl_writephy(tp, MII_BMCR, 0x0000);
2888
2889 RTL_W32(RxConfig, RTL_R32(RxConfig) |
2890 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
2891 return;
2892 }
2893
2894 r8168_phy_power_down(tp);
2895
2896 switch (tp->mac_version) {
2897 case RTL_GIGA_MAC_VER_25:
2898 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08002899 case RTL_GIGA_MAC_VER_27:
2900 case RTL_GIGA_MAC_VER_28:
françois romieu065c27c2011-01-03 15:08:12 +00002901 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
2902 break;
2903 }
2904}
2905
2906static void r8168_pll_power_up(struct rtl8169_private *tp)
2907{
2908 void __iomem *ioaddr = tp->mmio_addr;
2909
Hayes Wang5d2e1952011-02-22 17:26:22 +08002910 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
2911 (tp->mac_version == RTL_GIGA_MAC_VER_28)) &&
2912 (ocp_read(tp, 0x0f, 0x0010) & 0x00008000)) {
françois romieu065c27c2011-01-03 15:08:12 +00002913 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08002914 }
françois romieu065c27c2011-01-03 15:08:12 +00002915
2916 switch (tp->mac_version) {
2917 case RTL_GIGA_MAC_VER_25:
2918 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08002919 case RTL_GIGA_MAC_VER_27:
2920 case RTL_GIGA_MAC_VER_28:
françois romieu065c27c2011-01-03 15:08:12 +00002921 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
2922 break;
2923 }
2924
2925 r8168_phy_power_up(tp);
2926}
2927
2928static void rtl_pll_power_op(struct rtl8169_private *tp,
2929 void (*op)(struct rtl8169_private *))
2930{
2931 if (op)
2932 op(tp);
2933}
2934
2935static void rtl_pll_power_down(struct rtl8169_private *tp)
2936{
2937 rtl_pll_power_op(tp, tp->pll_power_ops.down);
2938}
2939
2940static void rtl_pll_power_up(struct rtl8169_private *tp)
2941{
2942 rtl_pll_power_op(tp, tp->pll_power_ops.up);
2943}
2944
2945static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
2946{
2947 struct pll_power_ops *ops = &tp->pll_power_ops;
2948
2949 switch (tp->mac_version) {
2950 case RTL_GIGA_MAC_VER_07:
2951 case RTL_GIGA_MAC_VER_08:
2952 case RTL_GIGA_MAC_VER_09:
2953 case RTL_GIGA_MAC_VER_10:
2954 case RTL_GIGA_MAC_VER_16:
2955 ops->down = r810x_pll_power_down;
2956 ops->up = r810x_pll_power_up;
2957 break;
2958
2959 case RTL_GIGA_MAC_VER_11:
2960 case RTL_GIGA_MAC_VER_12:
2961 case RTL_GIGA_MAC_VER_17:
2962 case RTL_GIGA_MAC_VER_18:
2963 case RTL_GIGA_MAC_VER_19:
2964 case RTL_GIGA_MAC_VER_20:
2965 case RTL_GIGA_MAC_VER_21:
2966 case RTL_GIGA_MAC_VER_22:
2967 case RTL_GIGA_MAC_VER_23:
2968 case RTL_GIGA_MAC_VER_24:
2969 case RTL_GIGA_MAC_VER_25:
2970 case RTL_GIGA_MAC_VER_26:
2971 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00002972 case RTL_GIGA_MAC_VER_28:
françois romieu065c27c2011-01-03 15:08:12 +00002973 ops->down = r8168_pll_power_down;
2974 ops->up = r8168_pll_power_up;
2975 break;
2976
2977 default:
2978 ops->down = NULL;
2979 ops->up = NULL;
2980 break;
2981 }
2982}
2983
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002984static int __devinit
2985rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
2986{
Francois Romieu0e485152007-02-20 00:00:26 +01002987 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
2988 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02002990 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002991 struct net_device *dev;
2992 void __iomem *ioaddr;
Francois Romieu07d3f512007-02-21 22:40:46 +01002993 unsigned int i;
2994 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002996 if (netif_msg_drv(&debug)) {
2997 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
2998 MODULENAME, RTL8169_VERSION);
2999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003002 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003003 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003004 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003005 rc = -ENOMEM;
3006 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 }
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003010 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003012 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003013 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003014 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Francois Romieuccdffb92008-07-26 14:26:06 +02003016 mii = &tp->mii;
3017 mii->dev = dev;
3018 mii->mdio_read = rtl_mdio_read;
3019 mii->mdio_write = rtl_mdio_write;
3020 mii->phy_id_mask = 0x1f;
3021 mii->reg_num_mask = 0x1f;
3022 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3023
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003024 /* disable ASPM completely as that cause random device stop working
3025 * problems as well as full system hangs for some PCIe devices users */
3026 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3027 PCIE_LINK_STATE_CLKPM);
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3030 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003031 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003032 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003033 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
3035
françois romieu87aeec72010-04-26 11:42:06 +00003036 if (pci_set_mwi(pdev) < 0)
3037 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003040 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003041 netif_err(tp, probe, dev,
3042 "region #%d not an MMIO resource, aborting\n",
3043 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003045 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003049 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003050 netif_err(tp, probe, dev,
3051 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003053 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
3055
3056 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003057 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003058 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003059 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
3061
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003062 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003065 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 tp->cp_cmd |= PCIDAC;
3067 dev->features |= NETIF_F_HIGHDMA;
3068 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003069 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003071 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003072 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
3074 }
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003077 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003078 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003079 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003081 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
3083
David S. Miller4300e8c2010-03-26 10:23:30 -07003084 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3085 if (!tp->pcie_cap)
3086 netif_info(tp, probe, dev, "no PCI Express capability\n");
3087
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003088 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
3090 /* Soft reset the chip. */
3091 RTL_W8(ChipCmd, CmdReset);
3092
3093 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003094 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3096 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003097 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
3099
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003100 RTL_W16(IntrStatus, 0xffff);
3101
françois romieuca52efd2009-07-24 12:34:19 +00003102 pci_set_master(pdev);
3103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 /* Identify chip attached to board */
3105 rtl8169_get_mac_version(tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
françois romieuc0e45c12011-01-03 15:08:04 +00003107 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003108 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003109
Jean Delvaref21b75e2009-05-26 20:54:48 -07003110 /* Use appropriate default if unknown */
3111 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003112 netif_notice(tp, probe, dev,
3113 "unknown MAC, using family default\n");
Jean Delvaref21b75e2009-05-26 20:54:48 -07003114 tp->mac_version = cfg->default_ver;
3115 }
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Roel Kluincee60c32008-04-17 22:35:54 +02003119 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (tp->mac_version == rtl_chip_info[i].mac_version)
3121 break;
3122 }
Roel Kluincee60c32008-04-17 22:35:54 +02003123 if (i == ARRAY_SIZE(rtl_chip_info)) {
Jean Delvaref21b75e2009-05-26 20:54:48 -07003124 dev_err(&pdev->dev,
3125 "driver bug, MAC version not found in rtl_chip_info\n");
françois romieu87aeec72010-04-26 11:42:06 +00003126 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
3128 tp->chipset = i;
3129
Francois Romieu5d06a992006-02-23 00:47:58 +01003130 RTL_W8(Cfg9346, Cfg9346_Unlock);
3131 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3132 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003133 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3134 tp->features |= RTL_FEATURE_WOL;
3135 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3136 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003137 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003138 RTL_W8(Cfg9346, Cfg9346_Lock);
3139
Francois Romieu66ec5d42007-11-06 22:56:10 +01003140 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3141 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 tp->set_speed = rtl8169_set_speed_tbi;
3143 tp->get_settings = rtl8169_gset_tbi;
3144 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3145 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3146 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003147 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Francois Romieu64e4bfb2006-08-17 12:43:06 +02003149 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 } else {
3151 tp->set_speed = rtl8169_set_speed_xmii;
3152 tp->get_settings = rtl8169_gset_xmii;
3153 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3154 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3155 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003156 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
3158
Francois Romieudf58ef52008-10-09 14:35:58 -07003159 spin_lock_init(&tp->lock);
3160
Petr Vandrovec738e1e62008-10-12 20:58:29 -07003161 tp->mmio_addr = ioaddr;
3162
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003163 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 for (i = 0; i < MAC_ADDR_LEN; i++)
3165 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003166 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3170 dev->irq = pdev->irq;
3171 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003173 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
3175#ifdef CONFIG_R8169_VLAN
3176 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177#endif
Eric Dumazet2edae082010-09-06 18:46:39 +00003178 dev->features |= NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003181 tp->hw_start = cfg->hw_start;
3182 tp->intr_event = cfg->intr_event;
3183 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Francois Romieu2efa53f2007-03-09 00:00:05 +01003185 init_timer(&tp->timer);
3186 tp->timer.data = (unsigned long) dev;
3187 tp->timer.function = rtl8169_phy_timer;
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003190 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003191 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 pci_set_drvdata(pdev, dev);
3194
Joe Perchesbf82c182010-02-09 11:49:50 +00003195 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3196 rtl_chip_info[tp->chipset].name,
3197 dev->base_addr, dev->dev_addr,
3198 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
françois romieue6de30d2011-01-03 15:08:37 +00003200 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3201 (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
françois romieub646d902011-01-03 15:08:21 +00003202 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003203 }
françois romieub646d902011-01-03 15:08:21 +00003204
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003205 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Alan Sternf3ec4f82010-06-08 15:23:51 -04003207 if (pci_dev_run_wake(pdev))
3208 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003209
Ivan Vecera0d672e92011-02-15 02:08:39 +00003210 netif_carrier_off(dev);
3211
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003212out:
3213 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
françois romieu87aeec72010-04-26 11:42:06 +00003215err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003216 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003217 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003218err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003219 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003220err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003221 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003222 pci_disable_device(pdev);
3223err_out_free_dev_1:
3224 free_netdev(dev);
3225 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
Francois Romieu07d3f512007-02-21 22:40:46 +01003228static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 struct net_device *dev = pci_get_drvdata(pdev);
3231 struct rtl8169_private *tp = netdev_priv(dev);
3232
françois romieue6de30d2011-01-03 15:08:37 +00003233 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3234 (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
françois romieub646d902011-01-03 15:08:21 +00003235 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003236 }
françois romieub646d902011-01-03 15:08:21 +00003237
Tejun Heo23f333a2010-12-12 16:45:14 +01003238 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003239
françois romieuf1e02ed2011-01-13 13:07:53 +00003240 rtl_release_firmware(tp);
3241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003243
Alan Sternf3ec4f82010-06-08 15:23:51 -04003244 if (pci_dev_run_wake(pdev))
3245 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003246
Ivan Veceracc098dc2009-11-29 23:12:52 -08003247 /* restore original MAC address */
3248 rtl_rar_set(tp, dev->perm_addr);
3249
Francois Romieufbac58f2007-10-04 22:51:38 +02003250 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3252 pci_set_drvdata(pdev, NULL);
3253}
3254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255static int rtl8169_open(struct net_device *dev)
3256{
3257 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003258 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003260 int retval = -ENOMEM;
3261
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003262 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Neil Hormanc0cd8842010-03-29 13:16:02 -07003264 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003266 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003268 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3269 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003271 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003273 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3274 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003276 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
3278 retval = rtl8169_init_ring(dev);
3279 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003280 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
David Howellsc4028952006-11-22 14:57:56 +00003282 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Francois Romieu99f252b2007-04-02 22:59:59 +02003284 smp_mb();
3285
Francois Romieufbac58f2007-10-04 22:51:38 +02003286 retval = request_irq(dev->irq, rtl8169_interrupt,
3287 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003288 dev->name, dev);
3289 if (retval < 0)
3290 goto err_release_ring_2;
3291
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003292 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003293
françois romieueee3a962011-01-08 02:17:26 +00003294 rtl8169_init_phy(dev, tp);
3295
3296 /*
3297 * Pretend we are using VLANs; This bypasses a nasty bug where
3298 * Interrupts stop flowing on high load on 8110SCd controllers.
3299 */
3300 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3301 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | RxVlan);
3302
françois romieu065c27c2011-01-03 15:08:12 +00003303 rtl_pll_power_up(tp);
3304
Francois Romieu07ce4062007-02-23 23:36:39 +01003305 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
3307 rtl8169_request_timer(dev);
3308
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003309 tp->saved_wolopts = 0;
3310 pm_runtime_put_noidle(&pdev->dev);
3311
françois romieueee3a962011-01-08 02:17:26 +00003312 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313out:
3314 return retval;
3315
Francois Romieu99f252b2007-04-02 22:59:59 +02003316err_release_ring_2:
3317 rtl8169_rx_clear(tp);
3318err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003319 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3320 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003321 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003322err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003323 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3324 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003325 tp->TxDescArray = NULL;
3326err_pm_runtime_put:
3327 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 goto out;
3329}
3330
françois romieue6de30d2011-01-03 15:08:37 +00003331static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
françois romieue6de30d2011-01-03 15:08:37 +00003333 void __iomem *ioaddr = tp->mmio_addr;
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 /* Disable interrupts */
3336 rtl8169_irq_mask_and_ack(ioaddr);
3337
Hayes Wang5d2e1952011-02-22 17:26:22 +08003338 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3339 tp->mac_version == RTL_GIGA_MAC_VER_28) {
françois romieue6de30d2011-01-03 15:08:37 +00003340 while (RTL_R8(TxPoll) & NPQ)
3341 udelay(20);
3342
3343 }
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 /* Reset the chipset */
3346 RTL_W8(ChipCmd, CmdReset);
3347
3348 /* PCI commit */
3349 RTL_R8(ChipCmd);
3350}
3351
Francois Romieu7f796d82007-06-11 23:04:41 +02003352static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003353{
3354 void __iomem *ioaddr = tp->mmio_addr;
3355 u32 cfg = rtl8169_rx_config;
3356
3357 cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
3358 RTL_W32(RxConfig, cfg);
3359
3360 /* Set DMA burst size and Interframe Gap Time */
3361 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3362 (InterFrameGap << TxInterFrameGapShift));
3363}
3364
Francois Romieu07ce4062007-02-23 23:36:39 +01003365static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
3367 struct rtl8169_private *tp = netdev_priv(dev);
3368 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01003369 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
3371 /* Soft reset the chip. */
3372 RTL_W8(ChipCmd, CmdReset);
3373
3374 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003375 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3377 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003378 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 }
3380
Francois Romieu07ce4062007-02-23 23:36:39 +01003381 tp->hw_start(dev);
3382
Francois Romieu07ce4062007-02-23 23:36:39 +01003383 netif_start_queue(dev);
3384}
3385
3386
Francois Romieu7f796d82007-06-11 23:04:41 +02003387static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3388 void __iomem *ioaddr)
3389{
3390 /*
3391 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3392 * register to be written before TxDescAddrLow to work.
3393 * Switching from MMIO to I/O access fixes the issue as well.
3394 */
3395 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003396 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003397 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003398 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003399}
3400
3401static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3402{
3403 u16 cmd;
3404
3405 cmd = RTL_R16(CPlusCmd);
3406 RTL_W16(CPlusCmd, cmd);
3407 return cmd;
3408}
3409
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003410static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003411{
3412 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003413 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003414}
3415
Francois Romieu6dccd162007-02-13 23:38:05 +01003416static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3417{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003418 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003419 u32 mac_version;
3420 u32 clk;
3421 u32 val;
3422 } cfg2_info [] = {
3423 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3424 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3425 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3426 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3427 }, *p = cfg2_info;
3428 unsigned int i;
3429 u32 clk;
3430
3431 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003432 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003433 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3434 RTL_W32(0x7c, p->val);
3435 break;
3436 }
3437 }
3438}
3439
Francois Romieu07ce4062007-02-23 23:36:39 +01003440static void rtl_hw_start_8169(struct net_device *dev)
3441{
3442 struct rtl8169_private *tp = netdev_priv(dev);
3443 void __iomem *ioaddr = tp->mmio_addr;
3444 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003445
Francois Romieu9cb427b2006-11-02 00:10:16 +01003446 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3447 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3448 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3449 }
3450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003452 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3453 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3454 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3455 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3456 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3457
françois romieuf0298f82011-01-03 15:07:42 +00003458 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003460 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Francois Romieuc946b302007-10-04 00:42:50 +02003462 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3463 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3464 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3465 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3466 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Francois Romieu7f796d82007-06-11 23:04:41 +02003468 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003469
3470 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3471 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
Joe Perches06fa7352007-10-18 21:15:00 +02003472 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003474 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 }
3476
Francois Romieubcf0bf92006-07-26 23:14:13 +02003477 RTL_W16(CPlusCmd, tp->cp_cmd);
3478
Francois Romieu6dccd162007-02-13 23:38:05 +01003479 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 /*
3482 * Undocumented corner. Supposedly:
3483 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3484 */
3485 RTL_W16(IntrMitigate, 0x0000);
3486
Francois Romieu7f796d82007-06-11 23:04:41 +02003487 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003488
Francois Romieuc946b302007-10-04 00:42:50 +02003489 if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
3490 (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
3491 (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
3492 (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
3493 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3494 rtl_set_rx_tx_config_registers(tp);
3495 }
3496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003498
3499 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3500 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
3502 RTL_W32(RxMissed, 0);
3503
Francois Romieu07ce4062007-02-23 23:36:39 +01003504 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
3506 /* no early-rx interrupts */
3507 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003508
3509 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003510 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003511}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Francois Romieu9c14cea2008-07-05 00:21:15 +02003513static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003514{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003515 struct net_device *dev = pci_get_drvdata(pdev);
3516 struct rtl8169_private *tp = netdev_priv(dev);
3517 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003518
Francois Romieu9c14cea2008-07-05 00:21:15 +02003519 if (cap) {
3520 u16 ctl;
3521
3522 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3523 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3524 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3525 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003526}
3527
françois romieu650e8d52011-01-03 15:08:29 +00003528static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003529{
3530 u32 csi;
3531
3532 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003533 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3534}
3535
françois romieue6de30d2011-01-03 15:08:37 +00003536static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3537{
3538 rtl_csi_access_enable(ioaddr, 0x17000000);
3539}
3540
françois romieu650e8d52011-01-03 15:08:29 +00003541static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3542{
3543 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003544}
3545
3546struct ephy_info {
3547 unsigned int offset;
3548 u16 mask;
3549 u16 bits;
3550};
3551
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003552static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003553{
3554 u16 w;
3555
3556 while (len-- > 0) {
3557 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3558 rtl_ephy_write(ioaddr, e->offset, w);
3559 e++;
3560 }
3561}
3562
Francois Romieub726e492008-06-28 12:22:59 +02003563static void rtl_disable_clock_request(struct pci_dev *pdev)
3564{
3565 struct net_device *dev = pci_get_drvdata(pdev);
3566 struct rtl8169_private *tp = netdev_priv(dev);
3567 int cap = tp->pcie_cap;
3568
3569 if (cap) {
3570 u16 ctl;
3571
3572 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3573 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3574 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3575 }
3576}
3577
françois romieue6de30d2011-01-03 15:08:37 +00003578static void rtl_enable_clock_request(struct pci_dev *pdev)
3579{
3580 struct net_device *dev = pci_get_drvdata(pdev);
3581 struct rtl8169_private *tp = netdev_priv(dev);
3582 int cap = tp->pcie_cap;
3583
3584 if (cap) {
3585 u16 ctl;
3586
3587 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3588 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3589 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3590 }
3591}
3592
Francois Romieub726e492008-06-28 12:22:59 +02003593#define R8168_CPCMD_QUIRK_MASK (\
3594 EnableBist | \
3595 Mac_dbgo_oe | \
3596 Force_half_dup | \
3597 Force_rxflow_en | \
3598 Force_txflow_en | \
3599 Cxpl_dbg_sel | \
3600 ASF | \
3601 PktCntrDisable | \
3602 Mac_dbgo_sel)
3603
Francois Romieu219a1e92008-06-28 11:58:39 +02003604static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3605{
Francois Romieub726e492008-06-28 12:22:59 +02003606 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3607
3608 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3609
Francois Romieu2e68ae42008-06-28 12:00:55 +02003610 rtl_tx_performance_tweak(pdev,
3611 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003612}
3613
3614static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3615{
3616 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003617
françois romieuf0298f82011-01-03 15:07:42 +00003618 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003619
3620 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003621}
3622
3623static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3624{
Francois Romieub726e492008-06-28 12:22:59 +02003625 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3626
3627 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3628
Francois Romieu219a1e92008-06-28 11:58:39 +02003629 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003630
3631 rtl_disable_clock_request(pdev);
3632
3633 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003634}
3635
Francois Romieuef3386f2008-06-29 12:24:30 +02003636static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003637{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003638 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003639 { 0x01, 0, 0x0001 },
3640 { 0x02, 0x0800, 0x1000 },
3641 { 0x03, 0, 0x0042 },
3642 { 0x06, 0x0080, 0x0000 },
3643 { 0x07, 0, 0x2000 }
3644 };
3645
françois romieu650e8d52011-01-03 15:08:29 +00003646 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003647
3648 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3649
Francois Romieu219a1e92008-06-28 11:58:39 +02003650 __rtl_hw_start_8168cp(ioaddr, pdev);
3651}
3652
Francois Romieuef3386f2008-06-29 12:24:30 +02003653static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3654{
françois romieu650e8d52011-01-03 15:08:29 +00003655 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003656
3657 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3658
3659 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3660
3661 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3662}
3663
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003664static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3665{
françois romieu650e8d52011-01-03 15:08:29 +00003666 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003667
3668 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3669
3670 /* Magic. */
3671 RTL_W8(DBG_REG, 0x20);
3672
françois romieuf0298f82011-01-03 15:07:42 +00003673 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003674
3675 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3676
3677 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3678}
3679
Francois Romieu219a1e92008-06-28 11:58:39 +02003680static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3681{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003682 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003683 { 0x02, 0x0800, 0x1000 },
3684 { 0x03, 0, 0x0002 },
3685 { 0x06, 0x0080, 0x0000 }
3686 };
3687
françois romieu650e8d52011-01-03 15:08:29 +00003688 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003689
3690 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3691
3692 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3693
Francois Romieu219a1e92008-06-28 11:58:39 +02003694 __rtl_hw_start_8168cp(ioaddr, pdev);
3695}
3696
3697static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3698{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003699 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003700 { 0x01, 0, 0x0001 },
3701 { 0x03, 0x0400, 0x0220 }
3702 };
3703
françois romieu650e8d52011-01-03 15:08:29 +00003704 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003705
3706 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3707
Francois Romieu219a1e92008-06-28 11:58:39 +02003708 __rtl_hw_start_8168cp(ioaddr, pdev);
3709}
3710
Francois Romieu197ff762008-06-28 13:16:02 +02003711static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3712{
3713 rtl_hw_start_8168c_2(ioaddr, pdev);
3714}
3715
Francois Romieu6fb07052008-06-29 11:54:28 +02003716static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3717{
françois romieu650e8d52011-01-03 15:08:29 +00003718 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02003719
3720 __rtl_hw_start_8168cp(ioaddr, pdev);
3721}
3722
Francois Romieu5b538df2008-07-20 16:22:45 +02003723static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3724{
françois romieu650e8d52011-01-03 15:08:29 +00003725 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02003726
3727 rtl_disable_clock_request(pdev);
3728
françois romieuf0298f82011-01-03 15:07:42 +00003729 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02003730
3731 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3732
3733 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3734}
3735
françois romieue6de30d2011-01-03 15:08:37 +00003736static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
3737{
3738 static const struct ephy_info e_info_8168d_4[] = {
3739 { 0x0b, ~0, 0x48 },
3740 { 0x19, 0x20, 0x50 },
3741 { 0x0c, ~0, 0x20 }
3742 };
3743 int i;
3744
3745 rtl_csi_access_enable_1(ioaddr);
3746
3747 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3748
3749 RTL_W8(MaxTxPacketSize, TxPacketMax);
3750
3751 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
3752 const struct ephy_info *e = e_info_8168d_4 + i;
3753 u16 w;
3754
3755 w = rtl_ephy_read(ioaddr, e->offset);
3756 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
3757 }
3758
3759 rtl_enable_clock_request(pdev);
3760}
3761
Francois Romieu07ce4062007-02-23 23:36:39 +01003762static void rtl_hw_start_8168(struct net_device *dev)
3763{
Francois Romieu2dd99532007-06-11 23:22:52 +02003764 struct rtl8169_private *tp = netdev_priv(dev);
3765 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01003766 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02003767
3768 RTL_W8(Cfg9346, Cfg9346_Unlock);
3769
françois romieuf0298f82011-01-03 15:07:42 +00003770 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02003771
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003772 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02003773
Francois Romieu0e485152007-02-20 00:00:26 +01003774 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02003775
3776 RTL_W16(CPlusCmd, tp->cp_cmd);
3777
Francois Romieu0e485152007-02-20 00:00:26 +01003778 RTL_W16(IntrMitigate, 0x5151);
3779
3780 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01003781 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
3782 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01003783 tp->intr_event |= RxFIFOOver | PCSTimeout;
3784 tp->intr_event &= ~RxOverflow;
3785 }
Francois Romieu2dd99532007-06-11 23:22:52 +02003786
3787 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3788
Francois Romieub8363902008-06-01 12:31:57 +02003789 rtl_set_rx_mode(dev);
3790
3791 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3792 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02003793
3794 RTL_R8(IntrMask);
3795
Francois Romieu219a1e92008-06-28 11:58:39 +02003796 switch (tp->mac_version) {
3797 case RTL_GIGA_MAC_VER_11:
3798 rtl_hw_start_8168bb(ioaddr, pdev);
3799 break;
3800
3801 case RTL_GIGA_MAC_VER_12:
3802 case RTL_GIGA_MAC_VER_17:
3803 rtl_hw_start_8168bef(ioaddr, pdev);
3804 break;
3805
3806 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02003807 rtl_hw_start_8168cp_1(ioaddr, pdev);
Francois Romieu219a1e92008-06-28 11:58:39 +02003808 break;
3809
3810 case RTL_GIGA_MAC_VER_19:
3811 rtl_hw_start_8168c_1(ioaddr, pdev);
3812 break;
3813
3814 case RTL_GIGA_MAC_VER_20:
3815 rtl_hw_start_8168c_2(ioaddr, pdev);
3816 break;
3817
Francois Romieu197ff762008-06-28 13:16:02 +02003818 case RTL_GIGA_MAC_VER_21:
3819 rtl_hw_start_8168c_3(ioaddr, pdev);
3820 break;
3821
Francois Romieu6fb07052008-06-29 11:54:28 +02003822 case RTL_GIGA_MAC_VER_22:
3823 rtl_hw_start_8168c_4(ioaddr, pdev);
3824 break;
3825
Francois Romieuef3386f2008-06-29 12:24:30 +02003826 case RTL_GIGA_MAC_VER_23:
3827 rtl_hw_start_8168cp_2(ioaddr, pdev);
3828 break;
3829
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003830 case RTL_GIGA_MAC_VER_24:
3831 rtl_hw_start_8168cp_3(ioaddr, pdev);
3832 break;
3833
Francois Romieu5b538df2008-07-20 16:22:45 +02003834 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00003835 case RTL_GIGA_MAC_VER_26:
3836 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02003837 rtl_hw_start_8168d(ioaddr, pdev);
3838 break;
3839
françois romieue6de30d2011-01-03 15:08:37 +00003840 case RTL_GIGA_MAC_VER_28:
3841 rtl_hw_start_8168d_4(ioaddr, pdev);
3842 break;
3843
Francois Romieu219a1e92008-06-28 11:58:39 +02003844 default:
3845 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
3846 dev->name, tp->mac_version);
3847 break;
3848 }
Francois Romieu2dd99532007-06-11 23:22:52 +02003849
Francois Romieu0e485152007-02-20 00:00:26 +01003850 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3851
Francois Romieub8363902008-06-01 12:31:57 +02003852 RTL_W8(Cfg9346, Cfg9346_Lock);
3853
Francois Romieu2dd99532007-06-11 23:22:52 +02003854 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003855
Francois Romieu0e485152007-02-20 00:00:26 +01003856 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003857}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
Francois Romieu2857ffb2008-08-02 21:08:49 +02003859#define R810X_CPCMD_QUIRK_MASK (\
3860 EnableBist | \
3861 Mac_dbgo_oe | \
3862 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00003863 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02003864 Force_txflow_en | \
3865 Cxpl_dbg_sel | \
3866 ASF | \
3867 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003868 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003869
3870static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
3871{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003872 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003873 { 0x01, 0, 0x6e65 },
3874 { 0x02, 0, 0x091f },
3875 { 0x03, 0, 0xc2f9 },
3876 { 0x06, 0, 0xafb5 },
3877 { 0x07, 0, 0x0e00 },
3878 { 0x19, 0, 0xec80 },
3879 { 0x01, 0, 0x2e65 },
3880 { 0x01, 0, 0x6e65 }
3881 };
3882 u8 cfg1;
3883
françois romieu650e8d52011-01-03 15:08:29 +00003884 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003885
3886 RTL_W8(DBG_REG, FIX_NAK_1);
3887
3888 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3889
3890 RTL_W8(Config1,
3891 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
3892 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3893
3894 cfg1 = RTL_R8(Config1);
3895 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
3896 RTL_W8(Config1, cfg1 & ~LEDS0);
3897
Francois Romieu2857ffb2008-08-02 21:08:49 +02003898 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
3899}
3900
3901static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
3902{
françois romieu650e8d52011-01-03 15:08:29 +00003903 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003904
3905 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3906
3907 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
3908 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003909}
3910
3911static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
3912{
3913 rtl_hw_start_8102e_2(ioaddr, pdev);
3914
3915 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
3916}
3917
Francois Romieu07ce4062007-02-23 23:36:39 +01003918static void rtl_hw_start_8101(struct net_device *dev)
3919{
Francois Romieucdf1a602007-06-11 23:29:50 +02003920 struct rtl8169_private *tp = netdev_priv(dev);
3921 void __iomem *ioaddr = tp->mmio_addr;
3922 struct pci_dev *pdev = tp->pci_dev;
3923
Francois Romieue3cf0cc2007-08-17 14:55:46 +02003924 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
3925 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02003926 int cap = tp->pcie_cap;
3927
3928 if (cap) {
3929 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
3930 PCI_EXP_DEVCTL_NOSNOOP_EN);
3931 }
Francois Romieucdf1a602007-06-11 23:29:50 +02003932 }
3933
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003934 RTL_W8(Cfg9346, Cfg9346_Unlock);
3935
Francois Romieu2857ffb2008-08-02 21:08:49 +02003936 switch (tp->mac_version) {
3937 case RTL_GIGA_MAC_VER_07:
3938 rtl_hw_start_8102e_1(ioaddr, pdev);
3939 break;
3940
3941 case RTL_GIGA_MAC_VER_08:
3942 rtl_hw_start_8102e_3(ioaddr, pdev);
3943 break;
3944
3945 case RTL_GIGA_MAC_VER_09:
3946 rtl_hw_start_8102e_2(ioaddr, pdev);
3947 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02003948 }
3949
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003950 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02003951
françois romieuf0298f82011-01-03 15:07:42 +00003952 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02003953
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003954 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02003955
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003956 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02003957 RTL_W16(CPlusCmd, tp->cp_cmd);
3958
3959 RTL_W16(IntrMitigate, 0x0000);
3960
3961 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3962
3963 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3964 rtl_set_rx_tx_config_registers(tp);
3965
Francois Romieucdf1a602007-06-11 23:29:50 +02003966 RTL_R8(IntrMask);
3967
Francois Romieucdf1a602007-06-11 23:29:50 +02003968 rtl_set_rx_mode(dev);
3969
3970 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003971
Francois Romieu0e485152007-02-20 00:00:26 +01003972 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
3975static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
3976{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
3978 return -EINVAL;
3979
3980 dev->mtu = new_mtu;
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00003981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982}
3983
3984static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
3985{
Al Viro95e09182007-12-22 18:55:39 +00003986 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
3988}
3989
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003990static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
3991 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00003993 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00003994 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00003995
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003996 kfree(*data_buff);
3997 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 rtl8169_make_unusable_by_asic(desc);
3999}
4000
4001static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4002{
4003 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4004
4005 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4006}
4007
4008static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4009 u32 rx_buf_sz)
4010{
4011 desc->addr = cpu_to_le64(mapping);
4012 wmb();
4013 rtl8169_mark_to_asic(desc, rx_buf_sz);
4014}
4015
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004016static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004018 return (void *)ALIGN((long)data, 16);
4019}
4020
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004021static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4022 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004023{
4024 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004026 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004027 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004028 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004030 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4031 if (!data)
4032 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004033
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004034 if (rtl8169_align(data) != data) {
4035 kfree(data);
4036 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4037 if (!data)
4038 return NULL;
4039 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004040
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004041 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004042 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004043 if (unlikely(dma_mapping_error(d, mapping))) {
4044 if (net_ratelimit())
4045 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004046 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
4049 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004050 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004051
4052err_out:
4053 kfree(data);
4054 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
4057static void rtl8169_rx_clear(struct rtl8169_private *tp)
4058{
Francois Romieu07d3f512007-02-21 22:40:46 +01004059 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
4061 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004062 if (tp->Rx_databuff[i]) {
4063 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 tp->RxDescArray + i);
4065 }
4066 }
4067}
4068
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004069static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004071 desc->opts1 |= cpu_to_le32(RingEnd);
4072}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004073
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004074static int rtl8169_rx_fill(struct rtl8169_private *tp)
4075{
4076 unsigned int i;
4077
4078 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004079 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004080
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004081 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004083
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004084 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004085 if (!data) {
4086 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004087 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004088 }
4089 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004092 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4093 return 0;
4094
4095err_out:
4096 rtl8169_rx_clear(tp);
4097 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098}
4099
4100static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4101{
4102 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4103}
4104
4105static int rtl8169_init_ring(struct net_device *dev)
4106{
4107 struct rtl8169_private *tp = netdev_priv(dev);
4108
4109 rtl8169_init_ring_indexes(tp);
4110
4111 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004112 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004114 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115}
4116
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004117static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 struct TxDesc *desc)
4119{
4120 unsigned int len = tx_skb->len;
4121
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004122 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 desc->opts1 = 0x00;
4125 desc->opts2 = 0x00;
4126 desc->addr = 0x00;
4127 tx_skb->len = 0;
4128}
4129
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004130static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4131 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
4133 unsigned int i;
4134
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004135 for (i = 0; i < n; i++) {
4136 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 struct ring_info *tx_skb = tp->tx_skb + entry;
4138 unsigned int len = tx_skb->len;
4139
4140 if (len) {
4141 struct sk_buff *skb = tx_skb->skb;
4142
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004143 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 tp->TxDescArray + entry);
4145 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004146 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 dev_kfree_skb(skb);
4148 tx_skb->skb = NULL;
4149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 }
4151 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004152}
4153
4154static void rtl8169_tx_clear(struct rtl8169_private *tp)
4155{
4156 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 tp->cur_tx = tp->dirty_tx = 0;
4158}
4159
David Howellsc4028952006-11-22 14:57:56 +00004160static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
4162 struct rtl8169_private *tp = netdev_priv(dev);
4163
David Howellsc4028952006-11-22 14:57:56 +00004164 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 schedule_delayed_work(&tp->task, 4);
4166}
4167
4168static void rtl8169_wait_for_quiescence(struct net_device *dev)
4169{
4170 struct rtl8169_private *tp = netdev_priv(dev);
4171 void __iomem *ioaddr = tp->mmio_addr;
4172
4173 synchronize_irq(dev->irq);
4174
4175 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004176 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
4178 rtl8169_irq_mask_and_ack(ioaddr);
4179
David S. Millerd1d08d12008-01-07 20:53:33 -08004180 tp->intr_mask = 0xffff;
4181 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004182 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183}
4184
David Howellsc4028952006-11-22 14:57:56 +00004185static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
David Howellsc4028952006-11-22 14:57:56 +00004187 struct rtl8169_private *tp =
4188 container_of(work, struct rtl8169_private, task.work);
4189 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 int ret;
4191
Francois Romieueb2a0212007-02-15 23:37:21 +01004192 rtnl_lock();
4193
4194 if (!netif_running(dev))
4195 goto out_unlock;
4196
4197 rtl8169_wait_for_quiescence(dev);
4198 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
4200 ret = rtl8169_open(dev);
4201 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004202 if (net_ratelimit())
4203 netif_err(tp, drv, dev,
4204 "reinit failure (status = %d). Rescheduling\n",
4205 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4207 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004208
4209out_unlock:
4210 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211}
4212
David Howellsc4028952006-11-22 14:57:56 +00004213static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
David Howellsc4028952006-11-22 14:57:56 +00004215 struct rtl8169_private *tp =
4216 container_of(work, struct rtl8169_private, task.work);
4217 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Francois Romieueb2a0212007-02-15 23:37:21 +01004219 rtnl_lock();
4220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004222 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
4224 rtl8169_wait_for_quiescence(dev);
4225
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004226 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 rtl8169_tx_clear(tp);
4228
4229 if (tp->dirty_rx == tp->cur_rx) {
4230 rtl8169_init_ring_indexes(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004231 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 netif_wake_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004233 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 } else {
Joe Perchesbf82c182010-02-09 11:49:50 +00004235 if (net_ratelimit())
4236 netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 rtl8169_schedule_work(dev, rtl8169_reset_task);
4238 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004239
4240out_unlock:
4241 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
4243
4244static void rtl8169_tx_timeout(struct net_device *dev)
4245{
4246 struct rtl8169_private *tp = netdev_priv(dev);
4247
françois romieue6de30d2011-01-03 15:08:37 +00004248 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250 /* Let's wait a bit while any (async) irq lands on */
4251 rtl8169_schedule_work(dev, rtl8169_reset_task);
4252}
4253
4254static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4255 u32 opts1)
4256{
4257 struct skb_shared_info *info = skb_shinfo(skb);
4258 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004259 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004260 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
4262 entry = tp->cur_tx;
4263 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4264 skb_frag_t *frag = info->frags + cur_frag;
4265 dma_addr_t mapping;
4266 u32 status, len;
4267 void *addr;
4268
4269 entry = (entry + 1) % NUM_TX_DESC;
4270
4271 txd = tp->TxDescArray + entry;
4272 len = frag->size;
4273 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004274 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004275 if (unlikely(dma_mapping_error(d, mapping))) {
4276 if (net_ratelimit())
4277 netif_err(tp, drv, tp->dev,
4278 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004279 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
4282 /* anti gcc 2.95.3 bugware (sic) */
4283 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4284
4285 txd->opts1 = cpu_to_le32(status);
4286 txd->addr = cpu_to_le64(mapping);
4287
4288 tp->tx_skb[entry].len = len;
4289 }
4290
4291 if (cur_frag) {
4292 tp->tx_skb[entry].skb = skb;
4293 txd->opts1 |= cpu_to_le32(LastFrag);
4294 }
4295
4296 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004297
4298err_out:
4299 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4300 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301}
4302
4303static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
4304{
4305 if (dev->features & NETIF_F_TSO) {
Herbert Xu79671682006-06-22 02:40:14 -07004306 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 if (mss)
4309 return LargeSend | ((mss & MSSMask) << MSSShift);
4310 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07004311 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004312 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
4314 if (ip->protocol == IPPROTO_TCP)
4315 return IPCS | TCPCS;
4316 else if (ip->protocol == IPPROTO_UDP)
4317 return IPCS | UDPCS;
4318 WARN_ON(1); /* we need a WARN() */
4319 }
4320 return 0;
4321}
4322
Stephen Hemminger613573252009-08-31 19:50:58 +00004323static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4324 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
4326 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004327 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 struct TxDesc *txd = tp->TxDescArray + entry;
4329 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004330 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 dma_addr_t mapping;
4332 u32 status, len;
4333 u32 opts1;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004334 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004337 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004338 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 }
4340
4341 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004342 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004344 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004345 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004346 if (unlikely(dma_mapping_error(d, mapping))) {
4347 if (net_ratelimit())
4348 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004349 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
4352 tp->tx_skb[entry].len = len;
4353 txd->addr = cpu_to_le64(mapping);
4354 txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4355
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004356 opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
4357
4358 frags = rtl8169_xmit_frags(tp, skb, opts1);
4359 if (frags < 0)
4360 goto err_dma_1;
4361 else if (frags)
4362 opts1 |= FirstFrag;
4363 else {
4364 opts1 |= FirstFrag | LastFrag;
4365 tp->tx_skb[entry].skb = skb;
4366 }
4367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 wmb();
4369
4370 /* anti gcc 2.95.3 bugware (sic) */
4371 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4372 txd->opts1 = cpu_to_le32(status);
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 tp->cur_tx += frags + 1;
4375
David Dillow4c020a92010-03-03 16:33:10 +00004376 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Francois Romieu275391a2007-02-23 23:50:28 +01004378 RTL_W8(TxPoll, NPQ); /* set polling bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
4380 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4381 netif_stop_queue(dev);
4382 smp_rmb();
4383 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4384 netif_wake_queue(dev);
4385 }
4386
Stephen Hemminger613573252009-08-31 19:50:58 +00004387 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004389err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004390 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004391err_dma_0:
4392 dev_kfree_skb(skb);
4393 dev->stats.tx_dropped++;
4394 return NETDEV_TX_OK;
4395
4396err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004398 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004399 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400}
4401
4402static void rtl8169_pcierr_interrupt(struct net_device *dev)
4403{
4404 struct rtl8169_private *tp = netdev_priv(dev);
4405 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 u16 pci_status, pci_cmd;
4407
4408 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4409 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4410
Joe Perchesbf82c182010-02-09 11:49:50 +00004411 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4412 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
4414 /*
4415 * The recovery sequence below admits a very elaborated explanation:
4416 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004417 * - I did not see what else could be done;
4418 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 *
4420 * Feel free to adjust to your needs.
4421 */
Francois Romieua27993f2006-12-18 00:04:19 +01004422 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004423 pci_cmd &= ~PCI_COMMAND_PARITY;
4424 else
4425 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4426
4427 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429 pci_write_config_word(pdev, PCI_STATUS,
4430 pci_status & (PCI_STATUS_DETECTED_PARITY |
4431 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4432 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4433
4434 /* The infamous DAC f*ckup only happens at boot time */
4435 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004436 void __iomem *ioaddr = tp->mmio_addr;
4437
Joe Perchesbf82c182010-02-09 11:49:50 +00004438 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 tp->cp_cmd &= ~PCIDAC;
4440 RTL_W16(CPlusCmd, tp->cp_cmd);
4441 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 }
4443
françois romieue6de30d2011-01-03 15:08:37 +00004444 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004445
4446 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448
Francois Romieu07d3f512007-02-21 22:40:46 +01004449static void rtl8169_tx_interrupt(struct net_device *dev,
4450 struct rtl8169_private *tp,
4451 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452{
4453 unsigned int dirty_tx, tx_left;
4454
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 dirty_tx = tp->dirty_tx;
4456 smp_rmb();
4457 tx_left = tp->cur_tx - dirty_tx;
4458
4459 while (tx_left > 0) {
4460 unsigned int entry = dirty_tx % NUM_TX_DESC;
4461 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 u32 status;
4463
4464 rmb();
4465 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4466 if (status & DescOwn)
4467 break;
4468
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004469 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4470 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004472 dev->stats.tx_packets++;
4473 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004474 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 tx_skb->skb = NULL;
4476 }
4477 dirty_tx++;
4478 tx_left--;
4479 }
4480
4481 if (tp->dirty_tx != dirty_tx) {
4482 tp->dirty_tx = dirty_tx;
4483 smp_wmb();
4484 if (netif_queue_stopped(dev) &&
4485 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4486 netif_wake_queue(dev);
4487 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004488 /*
4489 * 8168 hack: TxPoll requests are lost when the Tx packets are
4490 * too close. Let's kick an extra TxPoll request when a burst
4491 * of start_xmit activity is detected (if it is not detected,
4492 * it is slow enough). -- FR
4493 */
4494 smp_rmb();
4495 if (tp->cur_tx != dirty_tx)
4496 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 }
4498}
4499
Francois Romieu126fa4b2005-05-12 20:09:17 -04004500static inline int rtl8169_fragmented_frame(u32 status)
4501{
4502 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4503}
4504
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004505static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 u32 status = opts1 & RxProtoMask;
4508
4509 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004510 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 skb->ip_summed = CHECKSUM_UNNECESSARY;
4512 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004513 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004516static struct sk_buff *rtl8169_try_rx_copy(void *data,
4517 struct rtl8169_private *tp,
4518 int pkt_size,
4519 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004521 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004522 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004524 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004525 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004526 prefetch(data);
4527 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4528 if (skb)
4529 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004530 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4531
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004532 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533}
4534
Eric Dumazet630b9432010-03-31 02:08:31 +00004535/*
4536 * Warning : rtl8169_rx_interrupt() might be called :
4537 * 1) from NAPI (softirq) context
4538 * (polling = 1 : we should call netif_receive_skb())
4539 * 2) from process context (rtl8169_reset_task())
4540 * (polling = 0 : we must call netif_rx() instead)
4541 */
Francois Romieu07d3f512007-02-21 22:40:46 +01004542static int rtl8169_rx_interrupt(struct net_device *dev,
4543 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004544 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
4546 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004547 unsigned int count;
Eric Dumazet630b9432010-03-31 02:08:31 +00004548 int polling = (budget != ~(u32)0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 cur_rx = tp->cur_rx;
4551 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004552 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004554 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004556 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 u32 status;
4558
4559 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004560 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 if (status & DescOwn)
4563 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004564 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004565 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4566 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004567 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004569 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004571 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004572 if (status & RxFOVF) {
4573 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004574 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004575 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004576 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004578 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004579 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Francois Romieu126fa4b2005-05-12 20:09:17 -04004582 /*
4583 * The driver does not support incoming fragmented
4584 * frames. They are seen as a symptom of over-mtu
4585 * sized frames.
4586 */
4587 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004588 dev->stats.rx_dropped++;
4589 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004590 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004591 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004592 }
4593
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004594 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4595 tp, pkt_size, addr);
4596 rtl8169_mark_to_asic(desc, rx_buf_sz);
4597 if (!skb) {
4598 dev->stats.rx_dropped++;
4599 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 }
4601
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004602 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 skb_put(skb, pkt_size);
4604 skb->protocol = eth_type_trans(skb, dev);
4605
Eric Dumazet630b9432010-03-31 02:08:31 +00004606 if (rtl8169_rx_vlan_skb(tp, desc, skb, polling) < 0) {
4607 if (likely(polling))
Eric Dumazet2edae082010-09-06 18:46:39 +00004608 napi_gro_receive(&tp->napi, skb);
Eric Dumazet630b9432010-03-31 02:08:31 +00004609 else
4610 netif_rx(skb);
4611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Francois Romieucebf8cc2007-10-18 12:06:54 +02004613 dev->stats.rx_bytes += pkt_size;
4614 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
Francois Romieu6dccd162007-02-13 23:38:05 +01004616
4617 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00004618 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01004619 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4620 desc->opts2 = 0;
4621 cur_rx++;
4622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 }
4624
4625 count = cur_rx - tp->cur_rx;
4626 tp->cur_rx = cur_rx;
4627
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004628 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
4630 return count;
4631}
4632
Francois Romieu07d3f512007-02-21 22:40:46 +01004633static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
Francois Romieu07d3f512007-02-21 22:40:46 +01004635 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02004639 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
David Dillowf11a3772009-05-22 15:29:34 +00004641 /* loop handling interrupts until we have no new ones or
4642 * we hit a invalid/hotplug case.
4643 */
Francois Romieu865c6522008-05-11 14:51:00 +02004644 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00004645 while (status && status != 0xffff) {
4646 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
David Dillowf11a3772009-05-22 15:29:34 +00004648 /* Handle all of the error cases first. These will reset
4649 * the chip, so just exit the loop.
4650 */
4651 if (unlikely(!netif_running(dev))) {
4652 rtl8169_asic_down(ioaddr);
4653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 }
David Dillowf11a3772009-05-22 15:29:34 +00004655
Francois Romieu1519e572011-02-03 12:02:36 +01004656 if (unlikely(status & RxFIFOOver)) {
4657 switch (tp->mac_version) {
4658 /* Work around for rx fifo overflow */
4659 case RTL_GIGA_MAC_VER_11:
4660 case RTL_GIGA_MAC_VER_22:
4661 case RTL_GIGA_MAC_VER_26:
4662 netif_stop_queue(dev);
4663 rtl8169_tx_timeout(dev);
4664 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01004665 /* Testers needed. */
4666 case RTL_GIGA_MAC_VER_17:
4667 case RTL_GIGA_MAC_VER_19:
4668 case RTL_GIGA_MAC_VER_20:
4669 case RTL_GIGA_MAC_VER_21:
4670 case RTL_GIGA_MAC_VER_23:
4671 case RTL_GIGA_MAC_VER_24:
4672 case RTL_GIGA_MAC_VER_27:
4673 case RTL_GIGA_MAC_VER_28:
Francois Romieu1519e572011-02-03 12:02:36 +01004674 /* Experimental science. Pktgen proof. */
4675 case RTL_GIGA_MAC_VER_12:
4676 case RTL_GIGA_MAC_VER_25:
4677 if (status == RxFIFOOver)
4678 goto done;
4679 break;
4680 default:
4681 break;
4682 }
David Dillowf11a3772009-05-22 15:29:34 +00004683 }
4684
4685 if (unlikely(status & SYSErr)) {
4686 rtl8169_pcierr_interrupt(dev);
4687 break;
4688 }
4689
4690 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00004691 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00004692
4693 /* We need to see the lastest version of tp->intr_mask to
4694 * avoid ignoring an MSI interrupt and having to wait for
4695 * another event which may never come.
4696 */
4697 smp_rmb();
4698 if (status & tp->intr_mask & tp->napi_event) {
4699 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
4700 tp->intr_mask = ~tp->napi_event;
4701
4702 if (likely(napi_schedule_prep(&tp->napi)))
4703 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00004704 else
4705 netif_info(tp, intr, dev,
4706 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00004707 }
4708
4709 /* We only get a new MSI interrupt when all active irq
4710 * sources on the chip have been acknowledged. So, ack
4711 * everything we've seen and check if new sources have become
4712 * active to avoid blocking all interrupts from the chip.
4713 */
4714 RTL_W16(IntrStatus,
4715 (status & RxFIFOOver) ? (status | RxOverflow) : status);
4716 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 }
Francois Romieu1519e572011-02-03 12:02:36 +01004718done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 return IRQ_RETVAL(handled);
4720}
4721
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004722static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004724 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
4725 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004727 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004729 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 rtl8169_tx_interrupt(dev, tp, ioaddr);
4731
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004732 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004733 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00004734
4735 /* We need for force the visibility of tp->intr_mask
4736 * for other CPUs, as we can loose an MSI interrupt
4737 * and potentially wait for a retransmit timeout if we don't.
4738 * The posted write to IntrMask is safe, as it will
4739 * eventually make it to the chip and we won't loose anything
4740 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 */
David Dillowf11a3772009-05-22 15:29:34 +00004742 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00004743 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01004744 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 }
4746
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004747 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
Francois Romieu523a6092008-09-10 22:28:56 +02004750static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
4751{
4752 struct rtl8169_private *tp = netdev_priv(dev);
4753
4754 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
4755 return;
4756
4757 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
4758 RTL_W32(RxMissed, 0);
4759}
4760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761static void rtl8169_down(struct net_device *dev)
4762{
4763 struct rtl8169_private *tp = netdev_priv(dev);
4764 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
4766 rtl8169_delete_timer(dev);
4767
4768 netif_stop_queue(dev);
4769
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01004770 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01004771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 spin_lock_irq(&tp->lock);
4773
4774 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004775 /*
4776 * At this point device interrupts can not be enabled in any function,
4777 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
4778 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
4779 */
Francois Romieu523a6092008-09-10 22:28:56 +02004780 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
4782 spin_unlock_irq(&tp->lock);
4783
4784 synchronize_irq(dev->irq);
4785
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004787 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 rtl8169_tx_clear(tp);
4790
4791 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004792
4793 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794}
4795
4796static int rtl8169_close(struct net_device *dev)
4797{
4798 struct rtl8169_private *tp = netdev_priv(dev);
4799 struct pci_dev *pdev = tp->pci_dev;
4800
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004801 pm_runtime_get_sync(&pdev->dev);
4802
Ivan Vecera355423d2009-02-06 21:49:57 -08004803 /* update counters before going down */
4804 rtl8169_update_counters(dev);
4805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 rtl8169_down(dev);
4807
4808 free_irq(dev->irq, dev);
4809
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004810 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4811 tp->RxPhyAddr);
4812 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4813 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 tp->TxDescArray = NULL;
4815 tp->RxDescArray = NULL;
4816
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004817 pm_runtime_put_sync(&pdev->dev);
4818
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 return 0;
4820}
4821
Francois Romieu07ce4062007-02-23 23:36:39 +01004822static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823{
4824 struct rtl8169_private *tp = netdev_priv(dev);
4825 void __iomem *ioaddr = tp->mmio_addr;
4826 unsigned long flags;
4827 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01004828 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 u32 tmp = 0;
4830
4831 if (dev->flags & IFF_PROMISC) {
4832 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00004833 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 rx_mode =
4835 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4836 AcceptAllPhys;
4837 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00004838 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00004839 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 /* Too many to filter perfectly -- accept all multicasts. */
4841 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4842 mc_filter[1] = mc_filter[0] = 0xffffffff;
4843 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00004844 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01004845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 rx_mode = AcceptBroadcast | AcceptMyPhys;
4847 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00004848 netdev_for_each_mc_addr(ha, dev) {
4849 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4851 rx_mode |= AcceptMulticast;
4852 }
4853 }
4854
4855 spin_lock_irqsave(&tp->lock, flags);
4856
4857 tmp = rtl8169_rx_config | rx_mode |
4858 (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
4859
Francois Romieuf887cce2008-07-17 22:24:18 +02004860 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01004861 u32 data = mc_filter[0];
4862
4863 mc_filter[0] = swab32(mc_filter[1]);
4864 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02004865 }
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07004868 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Francois Romieu57a9f232007-06-04 22:10:15 +02004870 RTL_W32(RxConfig, tmp);
4871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 spin_unlock_irqrestore(&tp->lock, flags);
4873}
4874
4875/**
4876 * rtl8169_get_stats - Get rtl8169 read/write statistics
4877 * @dev: The Ethernet Device to get statistics for
4878 *
4879 * Get TX/RX statistics for rtl8169
4880 */
4881static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
4882{
4883 struct rtl8169_private *tp = netdev_priv(dev);
4884 void __iomem *ioaddr = tp->mmio_addr;
4885 unsigned long flags;
4886
4887 if (netif_running(dev)) {
4888 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02004889 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 spin_unlock_irqrestore(&tp->lock, flags);
4891 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02004892
Francois Romieucebf8cc2007-10-18 12:06:54 +02004893 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894}
4895
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004896static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01004897{
françois romieu065c27c2011-01-03 15:08:12 +00004898 struct rtl8169_private *tp = netdev_priv(dev);
4899
Francois Romieu5d06a992006-02-23 00:47:58 +01004900 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004901 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01004902
françois romieu065c27c2011-01-03 15:08:12 +00004903 rtl_pll_power_down(tp);
4904
Francois Romieu5d06a992006-02-23 00:47:58 +01004905 netif_device_detach(dev);
4906 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004907}
Francois Romieu5d06a992006-02-23 00:47:58 +01004908
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004909#ifdef CONFIG_PM
4910
4911static int rtl8169_suspend(struct device *device)
4912{
4913 struct pci_dev *pdev = to_pci_dev(device);
4914 struct net_device *dev = pci_get_drvdata(pdev);
4915
4916 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02004917
Francois Romieu5d06a992006-02-23 00:47:58 +01004918 return 0;
4919}
4920
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004921static void __rtl8169_resume(struct net_device *dev)
4922{
françois romieu065c27c2011-01-03 15:08:12 +00004923 struct rtl8169_private *tp = netdev_priv(dev);
4924
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004925 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00004926
4927 rtl_pll_power_up(tp);
4928
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004929 rtl8169_schedule_work(dev, rtl8169_reset_task);
4930}
4931
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004932static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01004933{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004934 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01004935 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00004936 struct rtl8169_private *tp = netdev_priv(dev);
4937
4938 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01004939
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004940 if (netif_running(dev))
4941 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01004942
Francois Romieu5d06a992006-02-23 00:47:58 +01004943 return 0;
4944}
4945
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004946static int rtl8169_runtime_suspend(struct device *device)
4947{
4948 struct pci_dev *pdev = to_pci_dev(device);
4949 struct net_device *dev = pci_get_drvdata(pdev);
4950 struct rtl8169_private *tp = netdev_priv(dev);
4951
4952 if (!tp->TxDescArray)
4953 return 0;
4954
4955 spin_lock_irq(&tp->lock);
4956 tp->saved_wolopts = __rtl8169_get_wol(tp);
4957 __rtl8169_set_wol(tp, WAKE_ANY);
4958 spin_unlock_irq(&tp->lock);
4959
4960 rtl8169_net_suspend(dev);
4961
4962 return 0;
4963}
4964
4965static int rtl8169_runtime_resume(struct device *device)
4966{
4967 struct pci_dev *pdev = to_pci_dev(device);
4968 struct net_device *dev = pci_get_drvdata(pdev);
4969 struct rtl8169_private *tp = netdev_priv(dev);
4970
4971 if (!tp->TxDescArray)
4972 return 0;
4973
4974 spin_lock_irq(&tp->lock);
4975 __rtl8169_set_wol(tp, tp->saved_wolopts);
4976 tp->saved_wolopts = 0;
4977 spin_unlock_irq(&tp->lock);
4978
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00004979 rtl8169_init_phy(dev, tp);
4980
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004981 __rtl8169_resume(dev);
4982
4983 return 0;
4984}
4985
4986static int rtl8169_runtime_idle(struct device *device)
4987{
4988 struct pci_dev *pdev = to_pci_dev(device);
4989 struct net_device *dev = pci_get_drvdata(pdev);
4990 struct rtl8169_private *tp = netdev_priv(dev);
4991
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00004992 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004993}
4994
Alexey Dobriyan47145212009-12-14 18:00:08 -08004995static const struct dev_pm_ops rtl8169_pm_ops = {
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00004996 .suspend = rtl8169_suspend,
4997 .resume = rtl8169_resume,
4998 .freeze = rtl8169_suspend,
4999 .thaw = rtl8169_resume,
5000 .poweroff = rtl8169_suspend,
5001 .restore = rtl8169_resume,
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005002 .runtime_suspend = rtl8169_runtime_suspend,
5003 .runtime_resume = rtl8169_runtime_resume,
5004 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005005};
5006
5007#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5008
5009#else /* !CONFIG_PM */
5010
5011#define RTL8169_PM_OPS NULL
5012
5013#endif /* !CONFIG_PM */
5014
Francois Romieu1765f952008-09-13 17:21:40 +02005015static void rtl_shutdown(struct pci_dev *pdev)
5016{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005017 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005018 struct rtl8169_private *tp = netdev_priv(dev);
5019 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005020
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005021 rtl8169_net_suspend(dev);
5022
Ivan Veceracc098dc2009-11-29 23:12:52 -08005023 /* restore original MAC address */
5024 rtl_rar_set(tp, dev->perm_addr);
5025
françois romieu4bb3f522009-06-17 11:41:45 +00005026 spin_lock_irq(&tp->lock);
5027
5028 rtl8169_asic_down(ioaddr);
5029
5030 spin_unlock_irq(&tp->lock);
5031
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005032 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005033 /* WoL fails with some 8168 when the receiver is disabled. */
5034 if (tp->features & RTL_FEATURE_WOL) {
5035 pci_clear_master(pdev);
5036
5037 RTL_W8(ChipCmd, CmdRxEnb);
5038 /* PCI commit */
5039 RTL_R8(ChipCmd);
5040 }
5041
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005042 pci_wake_from_d3(pdev, true);
5043 pci_set_power_state(pdev, PCI_D3hot);
5044 }
5045}
Francois Romieu5d06a992006-02-23 00:47:58 +01005046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047static struct pci_driver rtl8169_pci_driver = {
5048 .name = MODULENAME,
5049 .id_table = rtl8169_pci_tbl,
5050 .probe = rtl8169_init_one,
5051 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005052 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005053 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054};
5055
Francois Romieu07d3f512007-02-21 22:40:46 +01005056static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057{
Jeff Garzik29917622006-08-19 17:48:59 -04005058 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059}
5060
Francois Romieu07d3f512007-02-21 22:40:46 +01005061static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062{
5063 pci_unregister_driver(&rtl8169_pci_driver);
5064}
5065
5066module_init(rtl8169_init_module);
5067module_exit(rtl8169_cleanup_module);