blob: fb03e6ff37162f4d35616852a0ee42821f47b27c [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"
hayeswang01dc7fe2011-03-21 01:50:28 +000040#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080042#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#ifdef RTL8169_DEBUG
45#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020046 if (!(expr)) { \
47 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070048 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020049 }
Joe Perches06fa7352007-10-18 21:15:00 +020050#define dprintk(fmt, args...) \
51 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#else
53#define assert(expr) do {} while (0)
54#define dprintk(fmt, args...) do {} while (0)
55#endif /* RTL8169_DEBUG */
56
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020057#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070058 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define TX_BUFFS_AVAIL(tp) \
61 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
64 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050065static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* MAC address length */
68#define MAC_ADDR_LEN 6
69
Francois Romieu9c14cea2008-07-05 00:21:15 +020070#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
72#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
73#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76
77#define R8169_REGS_SIZE 256
78#define R8169_NAPI_WEIGHT 64
79#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81#define RX_BUF_SIZE 1536 /* Rx Buffer size */
82#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
84
85#define RTL8169_TX_TIMEOUT (6*HZ)
86#define RTL8169_PHY_TIMEOUT (10*HZ)
87
françois romieuea8dbdd2009-03-15 01:10:50 +000088#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020090#define RTL_EEPROM_SIG_ADDR 0x0000
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* write/read MMIO register */
93#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96#define RTL_R8(reg) readb (ioaddr + (reg))
97#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000098#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100enum mac_version {
Jean Delvaref21b75e2009-05-26 20:54:48 -0700101 RTL_GIGA_MAC_NONE = 0x00,
Francois Romieuba6eb6e2007-06-11 23:35:18 +0200102 RTL_GIGA_MAC_VER_01 = 0x01, // 8169
103 RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
104 RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
105 RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
106 RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +0100107 RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
Francois Romieu2857ffb2008-08-02 21:08:49 +0200108 RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
109 RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
110 RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
111 RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
Francois Romieu2dd99532007-06-11 23:22:52 +0200112 RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200113 RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
114 RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
115 RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
116 RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
117 RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
118 RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
119 RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
120 RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
Francois Romieu197ff762008-06-28 13:16:02 +0200121 RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
Francois Romieu6fb07052008-06-29 11:54:28 +0200122 RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
Francois Romieuef3386f2008-06-29 12:24:30 +0200123 RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
Francois Romieu7f3e3d32008-07-20 18:53:20 +0200124 RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
Francois Romieu5b538df2008-07-20 16:22:45 +0200125 RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
françois romieudaf9df62009-10-07 12:44:20 +0000126 RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
127 RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
françois romieue6de30d2011-01-03 15:08:37 +0000128 RTL_GIGA_MAC_VER_27 = 0x1b, // 8168DP
129 RTL_GIGA_MAC_VER_28 = 0x1c, // 8168DP
Hayes Wang5a5e4442011-02-22 17:26:21 +0800130 RTL_GIGA_MAC_VER_29 = 0x1d, // 8105E
131 RTL_GIGA_MAC_VER_30 = 0x1e, // 8105E
hayeswang4804b3b2011-03-21 01:50:29 +0000132 RTL_GIGA_MAC_VER_31 = 0x1f, // 8168DP
hayeswang01dc7fe2011-03-21 01:50:28 +0000133 RTL_GIGA_MAC_VER_32 = 0x20, // 8168E
134 RTL_GIGA_MAC_VER_33 = 0x21, // 8168E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Francois Romieu2b7b4312011-04-18 22:53:24 -0700137enum rtl_tx_desc_version {
138 RTL_TD_0 = 0,
139 RTL_TD_1 = 1,
140};
141
142#define _R(NAME,MAC,TD) \
143 { .name = NAME, .mac_version = MAC, .txd_version = TD }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800145static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 const char *name;
147 u8 mac_version;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700148 enum rtl_tx_desc_version txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149} rtl_chip_info[] = {
Francois Romieu2b7b4312011-04-18 22:53:24 -0700150 _R("RTL8169", RTL_GIGA_MAC_VER_01, RTL_TD_0), // 8169
151 _R("RTL8169s", RTL_GIGA_MAC_VER_02, RTL_TD_0), // 8169S
152 _R("RTL8110s", RTL_GIGA_MAC_VER_03, RTL_TD_0), // 8110S
153 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, RTL_TD_0), // 8169SB
154 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, RTL_TD_0), // 8110SCd
155 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, RTL_TD_0), // 8110SCe
156 _R("RTL8102e", RTL_GIGA_MAC_VER_07, RTL_TD_1), // PCI-E
157 _R("RTL8102e", RTL_GIGA_MAC_VER_08, RTL_TD_1), // PCI-E
158 _R("RTL8102e", RTL_GIGA_MAC_VER_09, RTL_TD_1), // PCI-E
159 _R("RTL8101e", RTL_GIGA_MAC_VER_10, RTL_TD_0), // PCI-E
160 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, RTL_TD_0), // PCI-E
161 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, RTL_TD_0), // PCI-E
162 _R("RTL8101e", RTL_GIGA_MAC_VER_13, RTL_TD_0), // PCI-E 8139
163 _R("RTL8100e", RTL_GIGA_MAC_VER_14, RTL_TD_0), // PCI-E 8139
164 _R("RTL8100e", RTL_GIGA_MAC_VER_15, RTL_TD_0), // PCI-E 8139
165 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, RTL_TD_0), // PCI-E
166 _R("RTL8101e", RTL_GIGA_MAC_VER_16, RTL_TD_0), // PCI-E
167 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, RTL_TD_1), // PCI-E
168 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, RTL_TD_1), // PCI-E
169 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, RTL_TD_1), // PCI-E
170 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, RTL_TD_1), // PCI-E
171 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, RTL_TD_1), // PCI-E
172 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, RTL_TD_1), // PCI-E
173 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, RTL_TD_1), // PCI-E
174 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, RTL_TD_1), // PCI-E
175 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, RTL_TD_1), // PCI-E
176 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, RTL_TD_1), // PCI-E
177 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28, RTL_TD_1), // PCI-E
178 _R("RTL8105e", RTL_GIGA_MAC_VER_29, RTL_TD_1), // PCI-E
179 _R("RTL8105e", RTL_GIGA_MAC_VER_30, RTL_TD_1), // PCI-E
180 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_31, RTL_TD_1), // PCI-E
181 _R("RTL8168e/8111e", RTL_GIGA_MAC_VER_32, RTL_TD_1), // PCI-E
182 _R("RTL8168e/8111e", RTL_GIGA_MAC_VER_33, RTL_TD_1) // PCI-E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184#undef _R
185
Francois Romieubcf0bf92006-07-26 23:14:13 +0200186enum cfg_version {
187 RTL_CFG_0 = 0x00,
188 RTL_CFG_1,
189 RTL_CFG_2
190};
191
Francois Romieu07ce4062007-02-23 23:36:39 +0100192static void rtl_hw_start_8169(struct net_device *);
193static void rtl_hw_start_8168(struct net_device *);
194static void rtl_hw_start_8101(struct net_device *);
195
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000196static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200197 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200198 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200199 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100200 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200201 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
202 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200203 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200204 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
205 { PCI_VENDOR_ID_LINKSYS, 0x1032,
206 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100207 { 0x0001, 0x8168,
208 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 {0,},
210};
211
212MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
213
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000214static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700215static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200216static struct {
217 u32 msg_enable;
218} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Francois Romieu07d3f512007-02-21 22:40:46 +0100220enum rtl_registers {
221 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100222 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100223 MAR0 = 8, /* Multicast filter. */
224 CounterAddrLow = 0x10,
225 CounterAddrHigh = 0x14,
226 TxDescStartAddrLow = 0x20,
227 TxDescStartAddrHigh = 0x24,
228 TxHDescStartAddrLow = 0x28,
229 TxHDescStartAddrHigh = 0x2c,
230 FLASH = 0x30,
231 ERSR = 0x36,
232 ChipCmd = 0x37,
233 TxPoll = 0x38,
234 IntrMask = 0x3c,
235 IntrStatus = 0x3e,
236 TxConfig = 0x40,
237 RxConfig = 0x44,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700238
239#define RTL_RX_CONFIG_MASK 0xff7e1880u
240
Francois Romieu07d3f512007-02-21 22:40:46 +0100241 RxMissed = 0x4c,
242 Cfg9346 = 0x50,
243 Config0 = 0x51,
244 Config1 = 0x52,
245 Config2 = 0x53,
246 Config3 = 0x54,
247 Config4 = 0x55,
248 Config5 = 0x56,
249 MultiIntr = 0x5c,
250 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100251 PHYstatus = 0x6c,
252 RxMaxSize = 0xda,
253 CPlusCmd = 0xe0,
254 IntrMitigate = 0xe2,
255 RxDescAddrLow = 0xe4,
256 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000257 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
258
259#define NoEarlyTx 0x3f /* Max value : no early transmit. */
260
261 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
262
263#define TxPacketMax (8064 >> 7)
264
Francois Romieu07d3f512007-02-21 22:40:46 +0100265 FuncEvent = 0xf0,
266 FuncEventMask = 0xf4,
267 FuncPresetState = 0xf8,
268 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
Francois Romieuf162a5d2008-06-01 22:37:49 +0200271enum rtl8110_registers {
272 TBICSR = 0x64,
273 TBI_ANAR = 0x68,
274 TBI_LPAR = 0x6a,
275};
276
277enum rtl8168_8101_registers {
278 CSIDR = 0x64,
279 CSIAR = 0x68,
280#define CSIAR_FLAG 0x80000000
281#define CSIAR_WRITE_CMD 0x80000000
282#define CSIAR_BYTE_ENABLE 0x0f
283#define CSIAR_BYTE_ENABLE_SHIFT 12
284#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000285 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200286 EPHYAR = 0x80,
287#define EPHYAR_FLAG 0x80000000
288#define EPHYAR_WRITE_CMD 0x80000000
289#define EPHYAR_REG_MASK 0x1f
290#define EPHYAR_REG_SHIFT 16
291#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800292 DLLPR = 0xd0,
293#define PM_SWITCH (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200294 DBG_REG = 0xd1,
295#define FIX_NAK_1 (1 << 4)
296#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800297 TWSI = 0xd2,
298 MCU = 0xd3,
299#define EN_NDP (1 << 3)
300#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000301 EFUSEAR = 0xdc,
302#define EFUSEAR_FLAG 0x80000000
303#define EFUSEAR_WRITE_CMD 0x80000000
304#define EFUSEAR_READ_CMD 0x00000000
305#define EFUSEAR_REG_MASK 0x03ff
306#define EFUSEAR_REG_SHIFT 8
307#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200308};
309
françois romieuc0e45c12011-01-03 15:08:04 +0000310enum rtl8168_registers {
françois romieub646d902011-01-03 15:08:21 +0000311 ERIDR = 0x70,
312 ERIAR = 0x74,
313#define ERIAR_FLAG 0x80000000
314#define ERIAR_WRITE_CMD 0x80000000
315#define ERIAR_READ_CMD 0x00000000
316#define ERIAR_ADDR_BYTE_ALIGN 4
317#define ERIAR_EXGMAC 0
318#define ERIAR_MSIX 1
319#define ERIAR_ASF 2
320#define ERIAR_TYPE_SHIFT 16
321#define ERIAR_BYTEEN 0x0f
322#define ERIAR_BYTEEN_SHIFT 12
françois romieuc0e45c12011-01-03 15:08:04 +0000323 EPHY_RXER_NUM = 0x7c,
324 OCPDR = 0xb0, /* OCP GPHY access */
325#define OCPDR_WRITE_CMD 0x80000000
326#define OCPDR_READ_CMD 0x00000000
327#define OCPDR_REG_MASK 0x7f
328#define OCPDR_GPHY_REG_SHIFT 16
329#define OCPDR_DATA_MASK 0xffff
330 OCPAR = 0xb4,
331#define OCPAR_FLAG 0x80000000
332#define OCPAR_GPHY_WRITE_CMD 0x8000f060
333#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000334 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
335 MISC = 0xf0, /* 8168e only. */
336 txpla_rst = (1 << 29)
françois romieuc0e45c12011-01-03 15:08:04 +0000337};
338
Francois Romieu07d3f512007-02-21 22:40:46 +0100339enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100341 SYSErr = 0x8000,
342 PCSTimeout = 0x4000,
343 SWInt = 0x0100,
344 TxDescUnavail = 0x0080,
345 RxFIFOOver = 0x0040,
346 LinkChg = 0x0020,
347 RxOverflow = 0x0010,
348 TxErr = 0x0008,
349 TxOK = 0x0004,
350 RxErr = 0x0002,
351 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200354 RxFOVF = (1 << 23),
355 RxRWT = (1 << 22),
356 RxRES = (1 << 21),
357 RxRUNT = (1 << 20),
358 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100361 CmdReset = 0x10,
362 CmdRxEnb = 0x08,
363 CmdTxEnb = 0x04,
364 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Francois Romieu275391a2007-02-23 23:50:28 +0100366 /* TXPoll register p.5 */
367 HPQ = 0x80, /* Poll cmd on the high prio queue */
368 NPQ = 0x40, /* Poll cmd on the low prio queue */
369 FSWInt = 0x01, /* Forced software interrupt */
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100372 Cfg9346_Lock = 0x00,
373 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100376 AcceptErr = 0x20,
377 AcceptRunt = 0x10,
378 AcceptBroadcast = 0x08,
379 AcceptMulticast = 0x04,
380 AcceptMyPhys = 0x02,
381 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100384 RxCfgFIFOShift = 13,
385 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* TxConfigBits */
388 TxInterFrameGapShift = 24,
389 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
390
Francois Romieu5d06a992006-02-23 00:47:58 +0100391 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200392 LEDS1 = (1 << 7),
393 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200394 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200395 Speed_down = (1 << 4),
396 MEMMAP = (1 << 3),
397 IOMAP = (1 << 2),
398 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100399 PMEnable = (1 << 0), /* Power Management Enable */
400
Francois Romieu6dccd162007-02-13 23:38:05 +0100401 /* Config2 register p. 25 */
402 PCI_Clock_66MHz = 0x01,
403 PCI_Clock_33MHz = 0x00,
404
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100405 /* Config3 register p.25 */
406 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
407 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200408 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100409
Francois Romieu5d06a992006-02-23 00:47:58 +0100410 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100411 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
412 MWF = (1 << 5), /* Accept Multicast wakeup frame */
413 UWF = (1 << 4), /* Accept Unicast wakeup frame */
hayeswang01dc7fe2011-03-21 01:50:28 +0000414 spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100415 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100416 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* TBICSR p.28 */
419 TBIReset = 0x80000000,
420 TBILoopback = 0x40000000,
421 TBINwEnable = 0x20000000,
422 TBINwRestart = 0x10000000,
423 TBILinkOk = 0x02000000,
424 TBINwComplete = 0x01000000,
425
426 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200427 EnableBist = (1 << 15), // 8168 8101
428 Mac_dbgo_oe = (1 << 14), // 8168 8101
429 Normal_mode = (1 << 13), // unused
430 Force_half_dup = (1 << 12), // 8168 8101
431 Force_rxflow_en = (1 << 11), // 8168 8101
432 Force_txflow_en = (1 << 10), // 8168 8101
433 Cxpl_dbg_sel = (1 << 9), // 8168 8101
434 ASF = (1 << 8), // 8168 8101
435 PktCntrDisable = (1 << 7), // 8168 8101
436 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 RxVlan = (1 << 6),
438 RxChkSum = (1 << 5),
439 PCIDAC = (1 << 4),
440 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100441 INTT_0 = 0x0000, // 8168
442 INTT_1 = 0x0001, // 8168
443 INTT_2 = 0x0002, // 8168
444 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100447 TBI_Enable = 0x80,
448 TxFlowCtrl = 0x40,
449 RxFlowCtrl = 0x20,
450 _1000bpsF = 0x10,
451 _100bps = 0x08,
452 _10bps = 0x04,
453 LinkStatus = 0x02,
454 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100457 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200458
459 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100460 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461};
462
Francois Romieu2b7b4312011-04-18 22:53:24 -0700463enum rtl_desc_bit {
464 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
466 RingEnd = (1 << 30), /* End of descriptor ring */
467 FirstFrag = (1 << 29), /* First segment of a packet */
468 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700469};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Francois Romieu2b7b4312011-04-18 22:53:24 -0700471/* Generic case. */
472enum rtl_tx_desc_bit {
473 /* First doubleword. */
474 TD_LSO = (1 << 27), /* Large Send Offload */
475#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Francois Romieu2b7b4312011-04-18 22:53:24 -0700477 /* Second doubleword. */
478 TxVlanTag = (1 << 17), /* Add VLAN tag */
479};
480
481/* 8169, 8168b and 810x except 8102e. */
482enum rtl_tx_desc_bit_0 {
483 /* First doubleword. */
484#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
485 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
486 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
487 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
488};
489
490/* 8102e, 8168c and beyond. */
491enum rtl_tx_desc_bit_1 {
492 /* Second doubleword. */
493#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
494 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
495 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
496 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
497};
498
499static const struct rtl_tx_desc_info {
500 struct {
501 u32 udp;
502 u32 tcp;
503 } checksum;
504 u16 mss_shift;
505 u16 opts_offset;
506} tx_desc_info [] = {
507 [RTL_TD_0] = {
508 .checksum = {
509 .udp = TD0_IP_CS | TD0_UDP_CS,
510 .tcp = TD0_IP_CS | TD0_TCP_CS
511 },
512 .mss_shift = TD0_MSS_SHIFT,
513 .opts_offset = 0
514 },
515 [RTL_TD_1] = {
516 .checksum = {
517 .udp = TD1_IP_CS | TD1_UDP_CS,
518 .tcp = TD1_IP_CS | TD1_TCP_CS
519 },
520 .mss_shift = TD1_MSS_SHIFT,
521 .opts_offset = 1
522 }
523};
524
525enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Rx private */
527 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
528 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
529
530#define RxProtoUDP (PID1)
531#define RxProtoTCP (PID0)
532#define RxProtoIP (PID1 | PID0)
533#define RxProtoMask RxProtoIP
534
535 IPFail = (1 << 16), /* IP checksum failed */
536 UDPFail = (1 << 15), /* UDP/IP checksum failed */
537 TCPFail = (1 << 14), /* TCP/IP checksum failed */
538 RxVlanTag = (1 << 16), /* VLAN tag available */
539};
540
541#define RsvdMask 0x3fffc000
542
543struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200544 __le32 opts1;
545 __le32 opts2;
546 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547};
548
549struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200550 __le32 opts1;
551 __le32 opts2;
552 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553};
554
555struct ring_info {
556 struct sk_buff *skb;
557 u32 len;
558 u8 __pad[sizeof(void *) - sizeof(u32)];
559};
560
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200561enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200562 RTL_FEATURE_WOL = (1 << 0),
563 RTL_FEATURE_MSI = (1 << 1),
564 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200565};
566
Ivan Vecera355423d2009-02-06 21:49:57 -0800567struct rtl8169_counters {
568 __le64 tx_packets;
569 __le64 rx_packets;
570 __le64 tx_errors;
571 __le32 rx_errors;
572 __le16 rx_missed;
573 __le16 align_errors;
574 __le32 tx_one_collision;
575 __le32 tx_multi_collision;
576 __le64 rx_unicast;
577 __le64 rx_broadcast;
578 __le32 rx_multicast;
579 __le16 tx_aborted;
580 __le16 tx_underun;
581};
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583struct rtl8169_private {
584 void __iomem *mmio_addr; /* memory map physical address */
585 struct pci_dev *pci_dev; /* Index of PCI device */
David Howellsc4028952006-11-22 14:57:56 +0000586 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700587 struct napi_struct napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 spinlock_t lock; /* spin lock flag */
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200589 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700590 u16 txd_version;
591 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
593 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
594 u32 dirty_rx;
595 u32 dirty_tx;
596 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
597 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
598 dma_addr_t TxPhyAddr;
599 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000600 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct timer_list timer;
603 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100604 u16 intr_event;
605 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 u16 intr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int phy_1000_ctrl_reg;
françois romieuc0e45c12011-01-03 15:08:04 +0000608
609 struct mdio_ops {
610 void (*write)(void __iomem *, int, int);
611 int (*read)(void __iomem *, int);
612 } mdio_ops;
613
françois romieu065c27c2011-01-03 15:08:12 +0000614 struct pll_power_ops {
615 void (*down)(struct rtl8169_private *);
616 void (*up)(struct rtl8169_private *);
617 } pll_power_ops;
618
Oliver Neukum54405cd2011-01-06 21:55:13 +0100619 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200620 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000621 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100622 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000623 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800625 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200626 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000627 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200628 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200629
630 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800631 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000632 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000633
634 const struct firmware *fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635};
636
Ralf Baechle979b6c12005-06-13 14:30:40 -0700637MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700640MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200641module_param_named(debug, debug.msg_enable, int, 0);
642MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643MODULE_LICENSE("GPL");
644MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000645MODULE_FIRMWARE(FIRMWARE_8168D_1);
646MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000647MODULE_FIRMWARE(FIRMWARE_8168E_1);
648MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800649MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000652static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
653 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100654static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100656static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100658static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200660static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700662 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200663static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200665static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700666static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200669 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
françois romieub646d902011-01-03 15:08:21 +0000671static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
672{
673 void __iomem *ioaddr = tp->mmio_addr;
674 int i;
675
676 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
677 for (i = 0; i < 20; i++) {
678 udelay(100);
679 if (RTL_R32(OCPAR) & OCPAR_FLAG)
680 break;
681 }
682 return RTL_R32(OCPDR);
683}
684
685static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
686{
687 void __iomem *ioaddr = tp->mmio_addr;
688 int i;
689
690 RTL_W32(OCPDR, data);
691 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
692 for (i = 0; i < 20; i++) {
693 udelay(100);
694 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
695 break;
696 }
697}
698
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800699static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000700{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800701 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000702 int i;
703
704 RTL_W8(ERIDR, cmd);
705 RTL_W32(ERIAR, 0x800010e8);
706 msleep(2);
707 for (i = 0; i < 5; i++) {
708 udelay(100);
709 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
710 break;
711 }
712
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800713 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000714}
715
716#define OOB_CMD_RESET 0x00
717#define OOB_CMD_DRIVER_START 0x05
718#define OOB_CMD_DRIVER_STOP 0x06
719
720static void rtl8168_driver_start(struct rtl8169_private *tp)
721{
722 int i;
hayeswang4804b3b2011-03-21 01:50:29 +0000723 u32 reg;
françois romieub646d902011-01-03 15:08:21 +0000724
725 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
726
hayeswang4804b3b2011-03-21 01:50:29 +0000727 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
728 reg = 0xb8;
729 else
730 reg = 0x10;
731
françois romieub646d902011-01-03 15:08:21 +0000732 for (i = 0; i < 10; i++) {
733 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000734 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000735 break;
736 }
737}
738
739static void rtl8168_driver_stop(struct rtl8169_private *tp)
740{
741 int i;
hayeswang4804b3b2011-03-21 01:50:29 +0000742 u32 reg;
françois romieub646d902011-01-03 15:08:21 +0000743
744 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
745
hayeswang4804b3b2011-03-21 01:50:29 +0000746 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
747 reg = 0xb8;
748 else
749 reg = 0x10;
750
françois romieub646d902011-01-03 15:08:21 +0000751 for (i = 0; i < 10; i++) {
752 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000753 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000754 break;
755 }
756}
757
hayeswang4804b3b2011-03-21 01:50:29 +0000758static int r8168dp_check_dash(struct rtl8169_private *tp)
759{
760 u32 reg;
761
762 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
763 reg = 0xb8;
764 else
765 reg = 0x10;
766
767 if (ocp_read(tp, 0xF, reg) & 0x00008000)
768 return 1;
769 else
770 return 0;
771}
françois romieub646d902011-01-03 15:08:21 +0000772
françois romieu4da19632011-01-03 15:07:55 +0000773static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 int i;
776
Francois Romieua6baf3a2007-11-08 23:23:21 +0100777 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Francois Romieu23714082006-01-29 00:49:09 +0100779 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100780 /*
781 * Check if the RTL8169 has completed writing to the specified
782 * MII register.
783 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200784 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 break;
Francois Romieu23714082006-01-29 00:49:09 +0100786 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700788 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700789 * According to hardware specs a 20us delay is required after write
790 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700791 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700792 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
françois romieu4da19632011-01-03 15:07:55 +0000795static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 int i, value = -1;
798
Francois Romieua6baf3a2007-11-08 23:23:21 +0100799 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Francois Romieu23714082006-01-29 00:49:09 +0100801 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100802 /*
803 * Check if the RTL8169 has completed retrieving data from
804 * the specified MII register.
805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100807 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 break;
809 }
Francois Romieu23714082006-01-29 00:49:09 +0100810 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700812 /*
813 * According to hardware specs a 20us delay is required after read
814 * complete indication, but before sending next command.
815 */
816 udelay(20);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return value;
819}
820
françois romieuc0e45c12011-01-03 15:08:04 +0000821static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
822{
823 int i;
824
825 RTL_W32(OCPDR, data |
826 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
827 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
828 RTL_W32(EPHY_RXER_NUM, 0);
829
830 for (i = 0; i < 100; i++) {
831 mdelay(1);
832 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
833 break;
834 }
835}
836
837static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
838{
839 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
840 (value & OCPDR_DATA_MASK));
841}
842
843static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
844{
845 int i;
846
847 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
848
849 mdelay(1);
850 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
851 RTL_W32(EPHY_RXER_NUM, 0);
852
853 for (i = 0; i < 100; i++) {
854 mdelay(1);
855 if (RTL_R32(OCPAR) & OCPAR_FLAG)
856 break;
857 }
858
859 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
860}
861
françois romieue6de30d2011-01-03 15:08:37 +0000862#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
863
864static void r8168dp_2_mdio_start(void __iomem *ioaddr)
865{
866 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
867}
868
869static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
870{
871 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
872}
873
874static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
875{
876 r8168dp_2_mdio_start(ioaddr);
877
878 r8169_mdio_write(ioaddr, reg_addr, value);
879
880 r8168dp_2_mdio_stop(ioaddr);
881}
882
883static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
884{
885 int value;
886
887 r8168dp_2_mdio_start(ioaddr);
888
889 value = r8169_mdio_read(ioaddr, reg_addr);
890
891 r8168dp_2_mdio_stop(ioaddr);
892
893 return value;
894}
895
françois romieu4da19632011-01-03 15:07:55 +0000896static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200897{
françois romieuc0e45c12011-01-03 15:08:04 +0000898 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200899}
900
françois romieu4da19632011-01-03 15:07:55 +0000901static int rtl_readphy(struct rtl8169_private *tp, int location)
902{
françois romieuc0e45c12011-01-03 15:08:04 +0000903 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000904}
905
906static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
907{
908 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
909}
910
911static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000912{
913 int val;
914
françois romieu4da19632011-01-03 15:07:55 +0000915 val = rtl_readphy(tp, reg_addr);
916 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000917}
918
Francois Romieuccdffb92008-07-26 14:26:06 +0200919static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
920 int val)
921{
922 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200923
françois romieu4da19632011-01-03 15:07:55 +0000924 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200925}
926
927static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
928{
929 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200930
françois romieu4da19632011-01-03 15:07:55 +0000931 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200932}
933
Francois Romieudacf8152008-08-02 20:44:13 +0200934static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
935{
936 unsigned int i;
937
938 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
939 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
940
941 for (i = 0; i < 100; i++) {
942 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
943 break;
944 udelay(10);
945 }
946}
947
948static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
949{
950 u16 value = 0xffff;
951 unsigned int i;
952
953 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
954
955 for (i = 0; i < 100; i++) {
956 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
957 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
958 break;
959 }
960 udelay(10);
961 }
962
963 return value;
964}
965
966static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
967{
968 unsigned int i;
969
970 RTL_W32(CSIDR, value);
971 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
972 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
973
974 for (i = 0; i < 100; i++) {
975 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
976 break;
977 udelay(10);
978 }
979}
980
981static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
982{
983 u32 value = ~0x00;
984 unsigned int i;
985
986 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
987 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
988
989 for (i = 0; i < 100; i++) {
990 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
991 value = RTL_R32(CSIDR);
992 break;
993 }
994 udelay(10);
995 }
996
997 return value;
998}
999
françois romieudaf9df62009-10-07 12:44:20 +00001000static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1001{
1002 u8 value = 0xff;
1003 unsigned int i;
1004
1005 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1006
1007 for (i = 0; i < 300; i++) {
1008 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1009 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1010 break;
1011 }
1012 udelay(100);
1013 }
1014
1015 return value;
1016}
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1019{
1020 RTL_W16(IntrMask, 0x0000);
1021
1022 RTL_W16(IntrStatus, 0xffff);
1023}
1024
1025static void rtl8169_asic_down(void __iomem *ioaddr)
1026{
1027 RTL_W8(ChipCmd, 0x00);
1028 rtl8169_irq_mask_and_ack(ioaddr);
1029 RTL_R16(CPlusCmd);
1030}
1031
françois romieu4da19632011-01-03 15:07:55 +00001032static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
françois romieu4da19632011-01-03 15:07:55 +00001034 void __iomem *ioaddr = tp->mmio_addr;
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return RTL_R32(TBICSR) & TBIReset;
1037}
1038
françois romieu4da19632011-01-03 15:07:55 +00001039static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
françois romieu4da19632011-01-03 15:07:55 +00001041 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1045{
1046 return RTL_R32(TBICSR) & TBILinkOk;
1047}
1048
1049static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1050{
1051 return RTL_R8(PHYstatus) & LinkStatus;
1052}
1053
françois romieu4da19632011-01-03 15:07:55 +00001054static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
françois romieu4da19632011-01-03 15:07:55 +00001056 void __iomem *ioaddr = tp->mmio_addr;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1059}
1060
françois romieu4da19632011-01-03 15:07:55 +00001061static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 unsigned int val;
1064
françois romieu4da19632011-01-03 15:07:55 +00001065 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1066 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001069static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieu07d3f512007-02-21 22:40:46 +01001070 struct rtl8169_private *tp,
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001071 void __iomem *ioaddr,
1072 bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 unsigned long flags;
1075
1076 spin_lock_irqsave(&tp->lock, flags);
1077 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001078 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001079 if (pm)
1080 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001082 if (net_ratelimit())
1083 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001084 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001086 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001087 if (pm)
1088 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 spin_unlock_irqrestore(&tp->lock, flags);
1091}
1092
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001093static void rtl8169_check_link_status(struct net_device *dev,
1094 struct rtl8169_private *tp,
1095 void __iomem *ioaddr)
1096{
1097 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1098}
1099
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001100#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1101
1102static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1103{
1104 void __iomem *ioaddr = tp->mmio_addr;
1105 u8 options;
1106 u32 wolopts = 0;
1107
1108 options = RTL_R8(Config1);
1109 if (!(options & PMEnable))
1110 return 0;
1111
1112 options = RTL_R8(Config3);
1113 if (options & LinkUp)
1114 wolopts |= WAKE_PHY;
1115 if (options & MagicPacket)
1116 wolopts |= WAKE_MAGIC;
1117
1118 options = RTL_R8(Config5);
1119 if (options & UWF)
1120 wolopts |= WAKE_UCAST;
1121 if (options & BWF)
1122 wolopts |= WAKE_BCAST;
1123 if (options & MWF)
1124 wolopts |= WAKE_MCAST;
1125
1126 return wolopts;
1127}
1128
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001129static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1130{
1131 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001132
1133 spin_lock_irq(&tp->lock);
1134
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001135 wol->supported = WAKE_ANY;
1136 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001137
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001138 spin_unlock_irq(&tp->lock);
1139}
1140
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001141static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001142{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001143 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001144 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001145 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001146 u32 opt;
1147 u16 reg;
1148 u8 mask;
1149 } cfg[] = {
1150 { WAKE_ANY, Config1, PMEnable },
1151 { WAKE_PHY, Config3, LinkUp },
1152 { WAKE_MAGIC, Config3, MagicPacket },
1153 { WAKE_UCAST, Config5, UWF },
1154 { WAKE_BCAST, Config5, BWF },
1155 { WAKE_MCAST, Config5, MWF },
1156 { WAKE_ANY, Config5, LanWake }
1157 };
1158
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001159 RTL_W8(Cfg9346, Cfg9346_Unlock);
1160
1161 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1162 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001163 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001164 options |= cfg[i].mask;
1165 RTL_W8(cfg[i].reg, options);
1166 }
1167
1168 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001169}
1170
1171static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1172{
1173 struct rtl8169_private *tp = netdev_priv(dev);
1174
1175 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001176
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001177 if (wol->wolopts)
1178 tp->features |= RTL_FEATURE_WOL;
1179 else
1180 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001181 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001182 spin_unlock_irq(&tp->lock);
1183
françois romieuea809072010-11-08 13:23:58 +00001184 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1185
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001186 return 0;
1187}
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189static void rtl8169_get_drvinfo(struct net_device *dev,
1190 struct ethtool_drvinfo *info)
1191{
1192 struct rtl8169_private *tp = netdev_priv(dev);
1193
1194 strcpy(info->driver, MODULENAME);
1195 strcpy(info->version, RTL8169_VERSION);
1196 strcpy(info->bus_info, pci_name(tp->pci_dev));
1197}
1198
1199static int rtl8169_get_regs_len(struct net_device *dev)
1200{
1201 return R8169_REGS_SIZE;
1202}
1203
1204static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001205 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 struct rtl8169_private *tp = netdev_priv(dev);
1208 void __iomem *ioaddr = tp->mmio_addr;
1209 int ret = 0;
1210 u32 reg;
1211
1212 reg = RTL_R32(TBICSR);
1213 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1214 (duplex == DUPLEX_FULL)) {
1215 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1216 } else if (autoneg == AUTONEG_ENABLE)
1217 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1218 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001219 netif_warn(tp, link, dev,
1220 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 ret = -EOPNOTSUPP;
1222 }
1223
1224 return ret;
1225}
1226
1227static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001228 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001231 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001232 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Hayes Wang716b50a2011-02-22 17:26:18 +08001234 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001237 int auto_nego;
1238
françois romieu4da19632011-01-03 15:07:55 +00001239 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001240 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1241 ADVERTISE_100HALF | ADVERTISE_100FULL);
1242
1243 if (adv & ADVERTISED_10baseT_Half)
1244 auto_nego |= ADVERTISE_10HALF;
1245 if (adv & ADVERTISED_10baseT_Full)
1246 auto_nego |= ADVERTISE_10FULL;
1247 if (adv & ADVERTISED_100baseT_Half)
1248 auto_nego |= ADVERTISE_100HALF;
1249 if (adv & ADVERTISED_100baseT_Full)
1250 auto_nego |= ADVERTISE_100FULL;
1251
françois romieu3577aa12009-05-19 10:46:48 +00001252 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1253
françois romieu4da19632011-01-03 15:07:55 +00001254 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001255 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1256
1257 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1258 if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
1259 (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
1260 (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
1261 (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
1262 (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
1263 (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
1264 (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
Hayes Wang5a5e4442011-02-22 17:26:21 +08001265 (tp->mac_version != RTL_GIGA_MAC_VER_16) &&
1266 (tp->mac_version != RTL_GIGA_MAC_VER_29) &&
1267 (tp->mac_version != RTL_GIGA_MAC_VER_30)) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001268 if (adv & ADVERTISED_1000baseT_Half)
1269 giga_ctrl |= ADVERTISE_1000HALF;
1270 if (adv & ADVERTISED_1000baseT_Full)
1271 giga_ctrl |= ADVERTISE_1000FULL;
1272 } else if (adv & (ADVERTISED_1000baseT_Half |
1273 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001274 netif_info(tp, link, dev,
1275 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001276 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
françois romieu3577aa12009-05-19 10:46:48 +00001279 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001280
françois romieu4da19632011-01-03 15:07:55 +00001281 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1282 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001283 } else {
1284 giga_ctrl = 0;
1285
1286 if (speed == SPEED_10)
1287 bmcr = 0;
1288 else if (speed == SPEED_100)
1289 bmcr = BMCR_SPEED100;
1290 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001291 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001292
1293 if (duplex == DUPLEX_FULL)
1294 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001295 }
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 tp->phy_1000_ctrl_reg = giga_ctrl;
1298
françois romieu4da19632011-01-03 15:07:55 +00001299 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001300
1301 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
1302 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
1303 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001304 rtl_writephy(tp, 0x17, 0x2138);
1305 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001306 } else {
françois romieu4da19632011-01-03 15:07:55 +00001307 rtl_writephy(tp, 0x17, 0x2108);
1308 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001309 }
1310 }
1311
Oliver Neukum54405cd2011-01-06 21:55:13 +01001312 rc = 0;
1313out:
1314 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001318 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct rtl8169_private *tp = netdev_priv(dev);
1321 int ret;
1322
Oliver Neukum54405cd2011-01-06 21:55:13 +01001323 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001325 if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1327
1328 return ret;
1329}
1330
1331static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1332{
1333 struct rtl8169_private *tp = netdev_priv(dev);
1334 unsigned long flags;
1335 int ret;
1336
1337 spin_lock_irqsave(&tp->lock, flags);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001338 ret = rtl8169_set_speed(dev,
1339 cmd->autoneg, cmd->speed, cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return ret;
1343}
1344
Michał Mirosław350fb322011-04-08 06:35:56 +00001345static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001347 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001348 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Michał Mirosław350fb322011-04-08 06:35:56 +00001350 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Michał Mirosław350fb322011-04-08 06:35:56 +00001353static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 struct rtl8169_private *tp = netdev_priv(dev);
1356 void __iomem *ioaddr = tp->mmio_addr;
1357 unsigned long flags;
1358
1359 spin_lock_irqsave(&tp->lock, flags);
1360
Michał Mirosław350fb322011-04-08 06:35:56 +00001361 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 tp->cp_cmd |= RxChkSum;
1363 else
1364 tp->cp_cmd &= ~RxChkSum;
1365
Michał Mirosław350fb322011-04-08 06:35:56 +00001366 if (dev->features & NETIF_F_HW_VLAN_RX)
1367 tp->cp_cmd |= RxVlan;
1368 else
1369 tp->cp_cmd &= ~RxVlan;
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 RTL_W16(CPlusCmd, tp->cp_cmd);
1372 RTL_R16(CPlusCmd);
1373
1374 spin_unlock_irqrestore(&tp->lock, flags);
1375
1376 return 0;
1377}
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1380 struct sk_buff *skb)
1381{
Jesse Grosseab6d182010-10-20 13:56:03 +00001382 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1384}
1385
Francois Romieu7a8fc772011-03-01 17:18:33 +01001386static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
1388 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Francois Romieu7a8fc772011-03-01 17:18:33 +01001390 if (opts2 & RxVlanTag)
1391 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Francois Romieuccdffb92008-07-26 14:26:06 +02001396static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct rtl8169_private *tp = netdev_priv(dev);
1399 void __iomem *ioaddr = tp->mmio_addr;
1400 u32 status;
1401
1402 cmd->supported =
1403 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1404 cmd->port = PORT_FIBRE;
1405 cmd->transceiver = XCVR_INTERNAL;
1406
1407 status = RTL_R32(TBICSR);
1408 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1409 cmd->autoneg = !!(status & TBINwEnable);
1410
1411 cmd->speed = SPEED_1000;
1412 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001413
1414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Francois Romieuccdffb92008-07-26 14:26:06 +02001417static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Francois Romieuccdffb92008-07-26 14:26:06 +02001421 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1425{
1426 struct rtl8169_private *tp = netdev_priv(dev);
1427 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001428 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 spin_lock_irqsave(&tp->lock, flags);
1431
Francois Romieuccdffb92008-07-26 14:26:06 +02001432 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001435 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
1438static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1439 void *p)
1440{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001441 struct rtl8169_private *tp = netdev_priv(dev);
1442 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Francois Romieu5b0384f2006-08-16 16:00:01 +02001444 if (regs->len > R8169_REGS_SIZE)
1445 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Francois Romieu5b0384f2006-08-16 16:00:01 +02001447 spin_lock_irqsave(&tp->lock, flags);
1448 memcpy_fromio(p, tp->mmio_addr, regs->len);
1449 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001452static u32 rtl8169_get_msglevel(struct net_device *dev)
1453{
1454 struct rtl8169_private *tp = netdev_priv(dev);
1455
1456 return tp->msg_enable;
1457}
1458
1459static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1460{
1461 struct rtl8169_private *tp = netdev_priv(dev);
1462
1463 tp->msg_enable = value;
1464}
1465
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001466static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1467 "tx_packets",
1468 "rx_packets",
1469 "tx_errors",
1470 "rx_errors",
1471 "rx_missed",
1472 "align_errors",
1473 "tx_single_collisions",
1474 "tx_multi_collisions",
1475 "unicast",
1476 "broadcast",
1477 "multicast",
1478 "tx_aborted",
1479 "tx_underrun",
1480};
1481
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001482static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001483{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001484 switch (sset) {
1485 case ETH_SS_STATS:
1486 return ARRAY_SIZE(rtl8169_gstrings);
1487 default:
1488 return -EOPNOTSUPP;
1489 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001490}
1491
Ivan Vecera355423d2009-02-06 21:49:57 -08001492static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001493{
1494 struct rtl8169_private *tp = netdev_priv(dev);
1495 void __iomem *ioaddr = tp->mmio_addr;
1496 struct rtl8169_counters *counters;
1497 dma_addr_t paddr;
1498 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001499 int wait = 1000;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001500 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001501
Ivan Vecera355423d2009-02-06 21:49:57 -08001502 /*
1503 * Some chips are unable to dump tally counters when the receiver
1504 * is disabled.
1505 */
1506 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1507 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001508
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001509 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001510 if (!counters)
1511 return;
1512
1513 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001514 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001515 RTL_W32(CounterAddrLow, cmd);
1516 RTL_W32(CounterAddrLow, cmd | CounterDump);
1517
Ivan Vecera355423d2009-02-06 21:49:57 -08001518 while (wait--) {
1519 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1520 /* copy updated counters */
1521 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001522 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001523 }
1524 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001525 }
1526
1527 RTL_W32(CounterAddrLow, 0);
1528 RTL_W32(CounterAddrHigh, 0);
1529
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001530 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001531}
1532
Ivan Vecera355423d2009-02-06 21:49:57 -08001533static void rtl8169_get_ethtool_stats(struct net_device *dev,
1534 struct ethtool_stats *stats, u64 *data)
1535{
1536 struct rtl8169_private *tp = netdev_priv(dev);
1537
1538 ASSERT_RTNL();
1539
1540 rtl8169_update_counters(dev);
1541
1542 data[0] = le64_to_cpu(tp->counters.tx_packets);
1543 data[1] = le64_to_cpu(tp->counters.rx_packets);
1544 data[2] = le64_to_cpu(tp->counters.tx_errors);
1545 data[3] = le32_to_cpu(tp->counters.rx_errors);
1546 data[4] = le16_to_cpu(tp->counters.rx_missed);
1547 data[5] = le16_to_cpu(tp->counters.align_errors);
1548 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1549 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1550 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1551 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1552 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1553 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1554 data[12] = le16_to_cpu(tp->counters.tx_underun);
1555}
1556
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001557static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1558{
1559 switch(stringset) {
1560 case ETH_SS_STATS:
1561 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1562 break;
1563 }
1564}
1565
Jeff Garzik7282d492006-09-13 14:30:00 -04001566static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 .get_drvinfo = rtl8169_get_drvinfo,
1568 .get_regs_len = rtl8169_get_regs_len,
1569 .get_link = ethtool_op_get_link,
1570 .get_settings = rtl8169_get_settings,
1571 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001572 .get_msglevel = rtl8169_get_msglevel,
1573 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001575 .get_wol = rtl8169_get_wol,
1576 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001577 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001578 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001579 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580};
1581
Francois Romieu07d3f512007-02-21 22:40:46 +01001582static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1583 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Francois Romieu0e485152007-02-20 00:00:26 +01001585 /*
1586 * The driver currently handles the 8168Bf and the 8168Be identically
1587 * but they can be identified more specifically through the test below
1588 * if needed:
1589 *
1590 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001591 *
1592 * Same thing for the 8101Eb and the 8101Ec:
1593 *
1594 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001595 */
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001596 static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001598 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 int mac_version;
1600 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001601 /* 8168E family. */
1602 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1603 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1604 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1605
Francois Romieu5b538df2008-07-20 16:22:45 +02001606 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001607 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1608 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001609 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001610
françois romieue6de30d2011-01-03 15:08:37 +00001611 /* 8168DP family. */
1612 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1613 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001614 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001615
Francois Romieuef808d52008-06-29 13:10:54 +02001616 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001617 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001618 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001619 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001620 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001621 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1622 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001623 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001624 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001625 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001626
1627 /* 8168B family. */
1628 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1629 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1630 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1631 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1632
1633 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001634 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001635 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1636 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1637 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001638 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1639 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1640 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1641 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1642 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1643 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001644 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001645 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001646 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001647 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1648 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001649 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1650 /* FIXME: where did these entries come from ? -- FR */
1651 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1652 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1653
1654 /* 8110 family. */
1655 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1656 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1657 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1658 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1659 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1660 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1661
Jean Delvaref21b75e2009-05-26 20:54:48 -07001662 /* Catch-all */
1663 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }, *p = mac_info;
1665 u32 reg;
1666
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001667 reg = RTL_R32(TxConfig);
1668 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 p++;
1670 tp->mac_version = p->mac_version;
1671}
1672
1673static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1674{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001675 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
Francois Romieu867763c2007-08-17 18:21:58 +02001678struct phy_reg {
1679 u16 reg;
1680 u16 val;
1681};
1682
françois romieu4da19632011-01-03 15:07:55 +00001683static void rtl_writephy_batch(struct rtl8169_private *tp,
1684 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001685{
1686 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001687 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001688 regs++;
1689 }
1690}
1691
françois romieubca03d52011-01-03 15:07:31 +00001692#define PHY_READ 0x00000000
1693#define PHY_DATA_OR 0x10000000
1694#define PHY_DATA_AND 0x20000000
1695#define PHY_BJMPN 0x30000000
1696#define PHY_READ_EFUSE 0x40000000
1697#define PHY_READ_MAC_BYTE 0x50000000
1698#define PHY_WRITE_MAC_BYTE 0x60000000
1699#define PHY_CLEAR_READCOUNT 0x70000000
1700#define PHY_WRITE 0x80000000
1701#define PHY_READCOUNT_EQ_SKIP 0x90000000
1702#define PHY_COMP_EQ_SKIPN 0xa0000000
1703#define PHY_COMP_NEQ_SKIPN 0xb0000000
1704#define PHY_WRITE_PREVIOUS 0xc0000000
1705#define PHY_SKIPN 0xd0000000
1706#define PHY_DELAY_MS 0xe0000000
1707#define PHY_WRITE_ERI_WORD 0xf0000000
1708
1709static void
1710rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1711{
françois romieubca03d52011-01-03 15:07:31 +00001712 __le32 *phytable = (__le32 *)fw->data;
1713 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001714 size_t index, fw_size = fw->size / sizeof(*phytable);
1715 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001716
1717 if (fw->size % sizeof(*phytable)) {
1718 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1719 return;
1720 }
1721
hayeswang42b82dc2011-01-10 02:07:25 +00001722 for (index = 0; index < fw_size; index++) {
1723 u32 action = le32_to_cpu(phytable[index]);
1724 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001725
hayeswang42b82dc2011-01-10 02:07:25 +00001726 switch(action & 0xf0000000) {
1727 case PHY_READ:
1728 case PHY_DATA_OR:
1729 case PHY_DATA_AND:
1730 case PHY_READ_EFUSE:
1731 case PHY_CLEAR_READCOUNT:
1732 case PHY_WRITE:
1733 case PHY_WRITE_PREVIOUS:
1734 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001735 break;
1736
hayeswang42b82dc2011-01-10 02:07:25 +00001737 case PHY_BJMPN:
1738 if (regno > index) {
1739 netif_err(tp, probe, tp->dev,
1740 "Out of range of firmware\n");
1741 return;
1742 }
1743 break;
1744 case PHY_READCOUNT_EQ_SKIP:
1745 if (index + 2 >= fw_size) {
1746 netif_err(tp, probe, tp->dev,
1747 "Out of range of firmware\n");
1748 return;
1749 }
1750 break;
1751 case PHY_COMP_EQ_SKIPN:
1752 case PHY_COMP_NEQ_SKIPN:
1753 case PHY_SKIPN:
1754 if (index + 1 + regno >= fw_size) {
1755 netif_err(tp, probe, tp->dev,
1756 "Out of range of firmware\n");
1757 return;
1758 }
1759 break;
1760
1761 case PHY_READ_MAC_BYTE:
1762 case PHY_WRITE_MAC_BYTE:
1763 case PHY_WRITE_ERI_WORD:
1764 default:
1765 netif_err(tp, probe, tp->dev,
1766 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001767 return;
1768 }
1769 }
1770
hayeswang42b82dc2011-01-10 02:07:25 +00001771 predata = 0;
1772 count = 0;
1773
1774 for (index = 0; index < fw_size; ) {
1775 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001776 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001777 u32 regno = (action & 0x0fff0000) >> 16;
1778
1779 if (!action)
1780 break;
françois romieubca03d52011-01-03 15:07:31 +00001781
1782 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001783 case PHY_READ:
1784 predata = rtl_readphy(tp, regno);
1785 count++;
1786 index++;
françois romieubca03d52011-01-03 15:07:31 +00001787 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001788 case PHY_DATA_OR:
1789 predata |= data;
1790 index++;
1791 break;
1792 case PHY_DATA_AND:
1793 predata &= data;
1794 index++;
1795 break;
1796 case PHY_BJMPN:
1797 index -= regno;
1798 break;
1799 case PHY_READ_EFUSE:
1800 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1801 index++;
1802 break;
1803 case PHY_CLEAR_READCOUNT:
1804 count = 0;
1805 index++;
1806 break;
1807 case PHY_WRITE:
1808 rtl_writephy(tp, regno, data);
1809 index++;
1810 break;
1811 case PHY_READCOUNT_EQ_SKIP:
1812 if (count == data)
1813 index += 2;
1814 else
1815 index += 1;
1816 break;
1817 case PHY_COMP_EQ_SKIPN:
1818 if (predata == data)
1819 index += regno;
1820 index++;
1821 break;
1822 case PHY_COMP_NEQ_SKIPN:
1823 if (predata != data)
1824 index += regno;
1825 index++;
1826 break;
1827 case PHY_WRITE_PREVIOUS:
1828 rtl_writephy(tp, regno, predata);
1829 index++;
1830 break;
1831 case PHY_SKIPN:
1832 index += regno + 1;
1833 break;
1834 case PHY_DELAY_MS:
1835 mdelay(data);
1836 index++;
1837 break;
1838
1839 case PHY_READ_MAC_BYTE:
1840 case PHY_WRITE_MAC_BYTE:
1841 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001842 default:
1843 BUG();
1844 }
1845 }
1846}
1847
françois romieuf1e02ed2011-01-13 13:07:53 +00001848static void rtl_release_firmware(struct rtl8169_private *tp)
1849{
1850 release_firmware(tp->fw);
1851 tp->fw = NULL;
1852}
1853
1854static int rtl_apply_firmware(struct rtl8169_private *tp, const char *fw_name)
1855{
1856 const struct firmware **fw = &tp->fw;
1857 int rc = !*fw;
1858
1859 if (rc) {
1860 rc = request_firmware(fw, fw_name, &tp->pci_dev->dev);
1861 if (rc < 0)
1862 goto out;
1863 }
1864
1865 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
1866 rtl_phy_write_fw(tp, *fw);
1867out:
1868 return rc;
1869}
1870
françois romieu4da19632011-01-03 15:07:55 +00001871static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001873 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001874 { 0x1f, 0x0001 },
1875 { 0x06, 0x006e },
1876 { 0x08, 0x0708 },
1877 { 0x15, 0x4000 },
1878 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
françois romieu0b9b5712009-08-10 19:44:56 +00001880 { 0x1f, 0x0001 },
1881 { 0x03, 0x00a1 },
1882 { 0x02, 0x0008 },
1883 { 0x01, 0x0120 },
1884 { 0x00, 0x1000 },
1885 { 0x04, 0x0800 },
1886 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
françois romieu0b9b5712009-08-10 19:44:56 +00001888 { 0x03, 0xff41 },
1889 { 0x02, 0xdf60 },
1890 { 0x01, 0x0140 },
1891 { 0x00, 0x0077 },
1892 { 0x04, 0x7800 },
1893 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
françois romieu0b9b5712009-08-10 19:44:56 +00001895 { 0x03, 0x802f },
1896 { 0x02, 0x4f02 },
1897 { 0x01, 0x0409 },
1898 { 0x00, 0xf0f9 },
1899 { 0x04, 0x9800 },
1900 { 0x04, 0x9000 },
1901
1902 { 0x03, 0xdf01 },
1903 { 0x02, 0xdf20 },
1904 { 0x01, 0xff95 },
1905 { 0x00, 0xba00 },
1906 { 0x04, 0xa800 },
1907 { 0x04, 0xa000 },
1908
1909 { 0x03, 0xff41 },
1910 { 0x02, 0xdf20 },
1911 { 0x01, 0x0140 },
1912 { 0x00, 0x00bb },
1913 { 0x04, 0xb800 },
1914 { 0x04, 0xb000 },
1915
1916 { 0x03, 0xdf41 },
1917 { 0x02, 0xdc60 },
1918 { 0x01, 0x6340 },
1919 { 0x00, 0x007d },
1920 { 0x04, 0xd800 },
1921 { 0x04, 0xd000 },
1922
1923 { 0x03, 0xdf01 },
1924 { 0x02, 0xdf20 },
1925 { 0x01, 0x100a },
1926 { 0x00, 0xa0ff },
1927 { 0x04, 0xf800 },
1928 { 0x04, 0xf000 },
1929
1930 { 0x1f, 0x0000 },
1931 { 0x0b, 0x0000 },
1932 { 0x00, 0x9200 }
1933 };
1934
françois romieu4da19632011-01-03 15:07:55 +00001935 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
françois romieu4da19632011-01-03 15:07:55 +00001938static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001939{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001940 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001941 { 0x1f, 0x0002 },
1942 { 0x01, 0x90d0 },
1943 { 0x1f, 0x0000 }
1944 };
1945
françois romieu4da19632011-01-03 15:07:55 +00001946 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001947}
1948
françois romieu4da19632011-01-03 15:07:55 +00001949static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001950{
1951 struct pci_dev *pdev = tp->pci_dev;
1952 u16 vendor_id, device_id;
1953
1954 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1955 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1956
1957 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1958 return;
1959
françois romieu4da19632011-01-03 15:07:55 +00001960 rtl_writephy(tp, 0x1f, 0x0001);
1961 rtl_writephy(tp, 0x10, 0xf01b);
1962 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001963}
1964
françois romieu4da19632011-01-03 15:07:55 +00001965static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001966{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001967 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00001968 { 0x1f, 0x0001 },
1969 { 0x04, 0x0000 },
1970 { 0x03, 0x00a1 },
1971 { 0x02, 0x0008 },
1972 { 0x01, 0x0120 },
1973 { 0x00, 0x1000 },
1974 { 0x04, 0x0800 },
1975 { 0x04, 0x9000 },
1976 { 0x03, 0x802f },
1977 { 0x02, 0x4f02 },
1978 { 0x01, 0x0409 },
1979 { 0x00, 0xf099 },
1980 { 0x04, 0x9800 },
1981 { 0x04, 0xa000 },
1982 { 0x03, 0xdf01 },
1983 { 0x02, 0xdf20 },
1984 { 0x01, 0xff95 },
1985 { 0x00, 0xba00 },
1986 { 0x04, 0xa800 },
1987 { 0x04, 0xf000 },
1988 { 0x03, 0xdf01 },
1989 { 0x02, 0xdf20 },
1990 { 0x01, 0x101a },
1991 { 0x00, 0xa0ff },
1992 { 0x04, 0xf800 },
1993 { 0x04, 0x0000 },
1994 { 0x1f, 0x0000 },
1995
1996 { 0x1f, 0x0001 },
1997 { 0x10, 0xf41b },
1998 { 0x14, 0xfb54 },
1999 { 0x18, 0xf5c7 },
2000 { 0x1f, 0x0000 },
2001
2002 { 0x1f, 0x0001 },
2003 { 0x17, 0x0cc0 },
2004 { 0x1f, 0x0000 }
2005 };
2006
françois romieu4da19632011-01-03 15:07:55 +00002007 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002008
françois romieu4da19632011-01-03 15:07:55 +00002009 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002010}
2011
françois romieu4da19632011-01-03 15:07:55 +00002012static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002013{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002014 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002015 { 0x1f, 0x0001 },
2016 { 0x04, 0x0000 },
2017 { 0x03, 0x00a1 },
2018 { 0x02, 0x0008 },
2019 { 0x01, 0x0120 },
2020 { 0x00, 0x1000 },
2021 { 0x04, 0x0800 },
2022 { 0x04, 0x9000 },
2023 { 0x03, 0x802f },
2024 { 0x02, 0x4f02 },
2025 { 0x01, 0x0409 },
2026 { 0x00, 0xf099 },
2027 { 0x04, 0x9800 },
2028 { 0x04, 0xa000 },
2029 { 0x03, 0xdf01 },
2030 { 0x02, 0xdf20 },
2031 { 0x01, 0xff95 },
2032 { 0x00, 0xba00 },
2033 { 0x04, 0xa800 },
2034 { 0x04, 0xf000 },
2035 { 0x03, 0xdf01 },
2036 { 0x02, 0xdf20 },
2037 { 0x01, 0x101a },
2038 { 0x00, 0xa0ff },
2039 { 0x04, 0xf800 },
2040 { 0x04, 0x0000 },
2041 { 0x1f, 0x0000 },
2042
2043 { 0x1f, 0x0001 },
2044 { 0x0b, 0x8480 },
2045 { 0x1f, 0x0000 },
2046
2047 { 0x1f, 0x0001 },
2048 { 0x18, 0x67c7 },
2049 { 0x04, 0x2000 },
2050 { 0x03, 0x002f },
2051 { 0x02, 0x4360 },
2052 { 0x01, 0x0109 },
2053 { 0x00, 0x3022 },
2054 { 0x04, 0x2800 },
2055 { 0x1f, 0x0000 },
2056
2057 { 0x1f, 0x0001 },
2058 { 0x17, 0x0cc0 },
2059 { 0x1f, 0x0000 }
2060 };
2061
françois romieu4da19632011-01-03 15:07:55 +00002062 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002063}
2064
françois romieu4da19632011-01-03 15:07:55 +00002065static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002066{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002067 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002068 { 0x10, 0xf41b },
2069 { 0x1f, 0x0000 }
2070 };
2071
françois romieu4da19632011-01-03 15:07:55 +00002072 rtl_writephy(tp, 0x1f, 0x0001);
2073 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002074
françois romieu4da19632011-01-03 15:07:55 +00002075 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002076}
2077
françois romieu4da19632011-01-03 15:07:55 +00002078static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002079{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002080 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002081 { 0x1f, 0x0001 },
2082 { 0x10, 0xf41b },
2083 { 0x1f, 0x0000 }
2084 };
2085
françois romieu4da19632011-01-03 15:07:55 +00002086 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002087}
2088
françois romieu4da19632011-01-03 15:07:55 +00002089static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002090{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002091 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002092 { 0x1f, 0x0000 },
2093 { 0x1d, 0x0f00 },
2094 { 0x1f, 0x0002 },
2095 { 0x0c, 0x1ec8 },
2096 { 0x1f, 0x0000 }
2097 };
2098
françois romieu4da19632011-01-03 15:07:55 +00002099 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002100}
2101
françois romieu4da19632011-01-03 15:07:55 +00002102static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002103{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002104 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002105 { 0x1f, 0x0001 },
2106 { 0x1d, 0x3d98 },
2107 { 0x1f, 0x0000 }
2108 };
2109
françois romieu4da19632011-01-03 15:07:55 +00002110 rtl_writephy(tp, 0x1f, 0x0000);
2111 rtl_patchphy(tp, 0x14, 1 << 5);
2112 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002113
françois romieu4da19632011-01-03 15:07:55 +00002114 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002115}
2116
françois romieu4da19632011-01-03 15:07:55 +00002117static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002118{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002119 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002120 { 0x1f, 0x0001 },
2121 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002122 { 0x1f, 0x0002 },
2123 { 0x00, 0x88d4 },
2124 { 0x01, 0x82b1 },
2125 { 0x03, 0x7002 },
2126 { 0x08, 0x9e30 },
2127 { 0x09, 0x01f0 },
2128 { 0x0a, 0x5500 },
2129 { 0x0c, 0x00c8 },
2130 { 0x1f, 0x0003 },
2131 { 0x12, 0xc096 },
2132 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002133 { 0x1f, 0x0000 },
2134 { 0x1f, 0x0000 },
2135 { 0x09, 0x2000 },
2136 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002137 };
2138
françois romieu4da19632011-01-03 15:07:55 +00002139 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002140
françois romieu4da19632011-01-03 15:07:55 +00002141 rtl_patchphy(tp, 0x14, 1 << 5);
2142 rtl_patchphy(tp, 0x0d, 1 << 5);
2143 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002144}
2145
françois romieu4da19632011-01-03 15:07:55 +00002146static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002147{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002148 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002149 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002150 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002151 { 0x03, 0x802f },
2152 { 0x02, 0x4f02 },
2153 { 0x01, 0x0409 },
2154 { 0x00, 0xf099 },
2155 { 0x04, 0x9800 },
2156 { 0x04, 0x9000 },
2157 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002158 { 0x1f, 0x0002 },
2159 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002160 { 0x06, 0x0761 },
2161 { 0x1f, 0x0003 },
2162 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002163 { 0x1f, 0x0000 }
2164 };
2165
françois romieu4da19632011-01-03 15:07:55 +00002166 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002167
françois romieu4da19632011-01-03 15:07:55 +00002168 rtl_patchphy(tp, 0x16, 1 << 0);
2169 rtl_patchphy(tp, 0x14, 1 << 5);
2170 rtl_patchphy(tp, 0x0d, 1 << 5);
2171 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002172}
2173
françois romieu4da19632011-01-03 15:07:55 +00002174static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002175{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002176 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002177 { 0x1f, 0x0001 },
2178 { 0x12, 0x2300 },
2179 { 0x1d, 0x3d98 },
2180 { 0x1f, 0x0002 },
2181 { 0x0c, 0x7eb8 },
2182 { 0x06, 0x5461 },
2183 { 0x1f, 0x0003 },
2184 { 0x16, 0x0f0a },
2185 { 0x1f, 0x0000 }
2186 };
2187
françois romieu4da19632011-01-03 15:07:55 +00002188 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002189
françois romieu4da19632011-01-03 15:07:55 +00002190 rtl_patchphy(tp, 0x16, 1 << 0);
2191 rtl_patchphy(tp, 0x14, 1 << 5);
2192 rtl_patchphy(tp, 0x0d, 1 << 5);
2193 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002194}
2195
françois romieu4da19632011-01-03 15:07:55 +00002196static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002197{
françois romieu4da19632011-01-03 15:07:55 +00002198 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002199}
2200
françois romieubca03d52011-01-03 15:07:31 +00002201static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002202{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002203 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002204 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002205 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002206 { 0x06, 0x4064 },
2207 { 0x07, 0x2863 },
2208 { 0x08, 0x059c },
2209 { 0x09, 0x26b4 },
2210 { 0x0a, 0x6a19 },
2211 { 0x0b, 0xdcc8 },
2212 { 0x10, 0xf06d },
2213 { 0x14, 0x7f68 },
2214 { 0x18, 0x7fd9 },
2215 { 0x1c, 0xf0ff },
2216 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002217 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002218 { 0x12, 0xf49f },
2219 { 0x13, 0x070b },
2220 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002221 { 0x14, 0x94c0 },
2222
2223 /*
2224 * Tx Error Issue
2225 * enhance line driver power
2226 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002227 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002228 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002229 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002230 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002231 { 0x06, 0x5561 },
2232
2233 /*
2234 * Can not link to 1Gbps with bad cable
2235 * Decrease SNR threshold form 21.07dB to 19.04dB
2236 */
2237 { 0x1f, 0x0001 },
2238 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002239
2240 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002241 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002242 };
françois romieubca03d52011-01-03 15:07:31 +00002243 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002244
françois romieu4da19632011-01-03 15:07:55 +00002245 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002246
françois romieubca03d52011-01-03 15:07:31 +00002247 /*
2248 * Rx Error Issue
2249 * Fine Tune Switching regulator parameter
2250 */
françois romieu4da19632011-01-03 15:07:55 +00002251 rtl_writephy(tp, 0x1f, 0x0002);
2252 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2253 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002254
françois romieudaf9df62009-10-07 12:44:20 +00002255 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002256 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002257 { 0x1f, 0x0002 },
2258 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002259 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002260 { 0x05, 0x8330 },
2261 { 0x06, 0x669a },
2262 { 0x1f, 0x0002 }
2263 };
2264 int val;
2265
françois romieu4da19632011-01-03 15:07:55 +00002266 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002267
françois romieu4da19632011-01-03 15:07:55 +00002268 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002269
2270 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002271 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002272 0x0065, 0x0066, 0x0067, 0x0068,
2273 0x0069, 0x006a, 0x006b, 0x006c
2274 };
2275 int i;
2276
françois romieu4da19632011-01-03 15:07:55 +00002277 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002278
2279 val &= 0xff00;
2280 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002281 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002282 }
2283 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002284 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002285 { 0x1f, 0x0002 },
2286 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002287 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002288 { 0x05, 0x8330 },
2289 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002290 };
2291
françois romieu4da19632011-01-03 15:07:55 +00002292 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002293 }
2294
françois romieubca03d52011-01-03 15:07:31 +00002295 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002296 rtl_writephy(tp, 0x1f, 0x0002);
2297 rtl_patchphy(tp, 0x0d, 0x0300);
2298 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002299
françois romieubca03d52011-01-03 15:07:31 +00002300 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002301 rtl_writephy(tp, 0x1f, 0x0002);
2302 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2303 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002304
françois romieu4da19632011-01-03 15:07:55 +00002305 rtl_writephy(tp, 0x1f, 0x0005);
2306 rtl_writephy(tp, 0x05, 0x001b);
françois romieuf1e02ed2011-01-13 13:07:53 +00002307 if ((rtl_readphy(tp, 0x06) != 0xbf00) ||
2308 (rtl_apply_firmware(tp, FIRMWARE_8168D_1) < 0)) {
françois romieubca03d52011-01-03 15:07:31 +00002309 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2310 }
2311
françois romieu4da19632011-01-03 15:07:55 +00002312 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002313}
2314
françois romieubca03d52011-01-03 15:07:31 +00002315static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002316{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002317 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002318 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002319 { 0x1f, 0x0001 },
2320 { 0x06, 0x4064 },
2321 { 0x07, 0x2863 },
2322 { 0x08, 0x059c },
2323 { 0x09, 0x26b4 },
2324 { 0x0a, 0x6a19 },
2325 { 0x0b, 0xdcc8 },
2326 { 0x10, 0xf06d },
2327 { 0x14, 0x7f68 },
2328 { 0x18, 0x7fd9 },
2329 { 0x1c, 0xf0ff },
2330 { 0x1d, 0x3d9c },
2331 { 0x1f, 0x0003 },
2332 { 0x12, 0xf49f },
2333 { 0x13, 0x070b },
2334 { 0x1a, 0x05ad },
2335 { 0x14, 0x94c0 },
2336
françois romieubca03d52011-01-03 15:07:31 +00002337 /*
2338 * Tx Error Issue
2339 * enhance line driver power
2340 */
françois romieudaf9df62009-10-07 12:44:20 +00002341 { 0x1f, 0x0002 },
2342 { 0x06, 0x5561 },
2343 { 0x1f, 0x0005 },
2344 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002345 { 0x06, 0x5561 },
2346
2347 /*
2348 * Can not link to 1Gbps with bad cable
2349 * Decrease SNR threshold form 21.07dB to 19.04dB
2350 */
2351 { 0x1f, 0x0001 },
2352 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002353
2354 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002355 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002356 };
françois romieubca03d52011-01-03 15:07:31 +00002357 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002358
françois romieu4da19632011-01-03 15:07:55 +00002359 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002360
2361 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002362 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002363 { 0x1f, 0x0002 },
2364 { 0x05, 0x669a },
2365 { 0x1f, 0x0005 },
2366 { 0x05, 0x8330 },
2367 { 0x06, 0x669a },
2368
2369 { 0x1f, 0x0002 }
2370 };
2371 int val;
2372
françois romieu4da19632011-01-03 15:07:55 +00002373 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002374
françois romieu4da19632011-01-03 15:07:55 +00002375 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002376 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002377 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002378 0x0065, 0x0066, 0x0067, 0x0068,
2379 0x0069, 0x006a, 0x006b, 0x006c
2380 };
2381 int i;
2382
françois romieu4da19632011-01-03 15:07:55 +00002383 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002384
2385 val &= 0xff00;
2386 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002387 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002388 }
2389 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002390 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002391 { 0x1f, 0x0002 },
2392 { 0x05, 0x2642 },
2393 { 0x1f, 0x0005 },
2394 { 0x05, 0x8330 },
2395 { 0x06, 0x2642 }
2396 };
2397
françois romieu4da19632011-01-03 15:07:55 +00002398 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002399 }
2400
françois romieubca03d52011-01-03 15:07:31 +00002401 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002402 rtl_writephy(tp, 0x1f, 0x0002);
2403 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2404 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002405
françois romieubca03d52011-01-03 15:07:31 +00002406 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002407 rtl_writephy(tp, 0x1f, 0x0002);
2408 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002409
françois romieu4da19632011-01-03 15:07:55 +00002410 rtl_writephy(tp, 0x1f, 0x0005);
2411 rtl_writephy(tp, 0x05, 0x001b);
françois romieuf1e02ed2011-01-13 13:07:53 +00002412 if ((rtl_readphy(tp, 0x06) != 0xb300) ||
2413 (rtl_apply_firmware(tp, FIRMWARE_8168D_2) < 0)) {
françois romieubca03d52011-01-03 15:07:31 +00002414 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2415 }
2416
françois romieu4da19632011-01-03 15:07:55 +00002417 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002418}
2419
françois romieu4da19632011-01-03 15:07:55 +00002420static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002421{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002422 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002423 { 0x1f, 0x0002 },
2424 { 0x10, 0x0008 },
2425 { 0x0d, 0x006c },
2426
2427 { 0x1f, 0x0000 },
2428 { 0x0d, 0xf880 },
2429
2430 { 0x1f, 0x0001 },
2431 { 0x17, 0x0cc0 },
2432
2433 { 0x1f, 0x0001 },
2434 { 0x0b, 0xa4d8 },
2435 { 0x09, 0x281c },
2436 { 0x07, 0x2883 },
2437 { 0x0a, 0x6b35 },
2438 { 0x1d, 0x3da4 },
2439 { 0x1c, 0xeffd },
2440 { 0x14, 0x7f52 },
2441 { 0x18, 0x7fc6 },
2442 { 0x08, 0x0601 },
2443 { 0x06, 0x4063 },
2444 { 0x10, 0xf074 },
2445 { 0x1f, 0x0003 },
2446 { 0x13, 0x0789 },
2447 { 0x12, 0xf4bd },
2448 { 0x1a, 0x04fd },
2449 { 0x14, 0x84b0 },
2450 { 0x1f, 0x0000 },
2451 { 0x00, 0x9200 },
2452
2453 { 0x1f, 0x0005 },
2454 { 0x01, 0x0340 },
2455 { 0x1f, 0x0001 },
2456 { 0x04, 0x4000 },
2457 { 0x03, 0x1d21 },
2458 { 0x02, 0x0c32 },
2459 { 0x01, 0x0200 },
2460 { 0x00, 0x5554 },
2461 { 0x04, 0x4800 },
2462 { 0x04, 0x4000 },
2463 { 0x04, 0xf000 },
2464 { 0x03, 0xdf01 },
2465 { 0x02, 0xdf20 },
2466 { 0x01, 0x101a },
2467 { 0x00, 0xa0ff },
2468 { 0x04, 0xf800 },
2469 { 0x04, 0xf000 },
2470 { 0x1f, 0x0000 },
2471
2472 { 0x1f, 0x0007 },
2473 { 0x1e, 0x0023 },
2474 { 0x16, 0x0000 },
2475 { 0x1f, 0x0000 }
2476 };
2477
françois romieu4da19632011-01-03 15:07:55 +00002478 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002479}
2480
françois romieue6de30d2011-01-03 15:08:37 +00002481static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2482{
2483 static const struct phy_reg phy_reg_init[] = {
2484 { 0x1f, 0x0001 },
2485 { 0x17, 0x0cc0 },
2486
2487 { 0x1f, 0x0007 },
2488 { 0x1e, 0x002d },
2489 { 0x18, 0x0040 },
2490 { 0x1f, 0x0000 }
2491 };
2492
2493 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2494 rtl_patchphy(tp, 0x0d, 1 << 5);
2495}
2496
hayeswang01dc7fe2011-03-21 01:50:28 +00002497static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2498{
2499 static const struct phy_reg phy_reg_init[] = {
2500 /* Enable Delay cap */
2501 { 0x1f, 0x0005 },
2502 { 0x05, 0x8b80 },
2503 { 0x06, 0xc896 },
2504 { 0x1f, 0x0000 },
2505
2506 /* Channel estimation fine tune */
2507 { 0x1f, 0x0001 },
2508 { 0x0b, 0x6c20 },
2509 { 0x07, 0x2872 },
2510 { 0x1c, 0xefff },
2511 { 0x1f, 0x0003 },
2512 { 0x14, 0x6420 },
2513 { 0x1f, 0x0000 },
2514
2515 /* Update PFM & 10M TX idle timer */
2516 { 0x1f, 0x0007 },
2517 { 0x1e, 0x002f },
2518 { 0x15, 0x1919 },
2519 { 0x1f, 0x0000 },
2520
2521 { 0x1f, 0x0007 },
2522 { 0x1e, 0x00ac },
2523 { 0x18, 0x0006 },
2524 { 0x1f, 0x0000 }
2525 };
2526
2527 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2528
2529 /* DCO enable for 10M IDLE Power */
2530 rtl_writephy(tp, 0x1f, 0x0007);
2531 rtl_writephy(tp, 0x1e, 0x0023);
2532 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2533 rtl_writephy(tp, 0x1f, 0x0000);
2534
2535 /* For impedance matching */
2536 rtl_writephy(tp, 0x1f, 0x0002);
2537 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
2538 rtl_writephy(tp, 0x1F, 0x0000);
2539
2540 /* PHY auto speed down */
2541 rtl_writephy(tp, 0x1f, 0x0007);
2542 rtl_writephy(tp, 0x1e, 0x002d);
2543 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2544 rtl_writephy(tp, 0x1f, 0x0000);
2545 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2546
2547 rtl_writephy(tp, 0x1f, 0x0005);
2548 rtl_writephy(tp, 0x05, 0x8b86);
2549 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2550 rtl_writephy(tp, 0x1f, 0x0000);
2551
2552 rtl_writephy(tp, 0x1f, 0x0005);
2553 rtl_writephy(tp, 0x05, 0x8b85);
2554 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2555 rtl_writephy(tp, 0x1f, 0x0007);
2556 rtl_writephy(tp, 0x1e, 0x0020);
2557 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2558 rtl_writephy(tp, 0x1f, 0x0006);
2559 rtl_writephy(tp, 0x00, 0x5a00);
2560 rtl_writephy(tp, 0x1f, 0x0000);
2561 rtl_writephy(tp, 0x0d, 0x0007);
2562 rtl_writephy(tp, 0x0e, 0x003c);
2563 rtl_writephy(tp, 0x0d, 0x4007);
2564 rtl_writephy(tp, 0x0e, 0x0000);
2565 rtl_writephy(tp, 0x0d, 0x0000);
2566}
2567
2568static void rtl8168e_1_hw_phy_config(struct rtl8169_private *tp)
2569{
2570 if (rtl_apply_firmware(tp, FIRMWARE_8168E_1) < 0)
2571 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2572
2573 rtl8168e_hw_phy_config(tp);
2574}
2575
2576static void rtl8168e_2_hw_phy_config(struct rtl8169_private *tp)
2577{
2578 if (rtl_apply_firmware(tp, FIRMWARE_8168E_2) < 0)
2579 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2580
2581 rtl8168e_hw_phy_config(tp);
2582}
2583
françois romieu4da19632011-01-03 15:07:55 +00002584static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002585{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002586 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002587 { 0x1f, 0x0003 },
2588 { 0x08, 0x441d },
2589 { 0x01, 0x9100 },
2590 { 0x1f, 0x0000 }
2591 };
2592
françois romieu4da19632011-01-03 15:07:55 +00002593 rtl_writephy(tp, 0x1f, 0x0000);
2594 rtl_patchphy(tp, 0x11, 1 << 12);
2595 rtl_patchphy(tp, 0x19, 1 << 13);
2596 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002597
françois romieu4da19632011-01-03 15:07:55 +00002598 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002599}
2600
Hayes Wang5a5e4442011-02-22 17:26:21 +08002601static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2602{
2603 static const struct phy_reg phy_reg_init[] = {
2604 { 0x1f, 0x0005 },
2605 { 0x1a, 0x0000 },
2606 { 0x1f, 0x0000 },
2607
2608 { 0x1f, 0x0004 },
2609 { 0x1c, 0x0000 },
2610 { 0x1f, 0x0000 },
2611
2612 { 0x1f, 0x0001 },
2613 { 0x15, 0x7701 },
2614 { 0x1f, 0x0000 }
2615 };
2616
2617 /* Disable ALDPS before ram code */
2618 rtl_writephy(tp, 0x1f, 0x0000);
2619 rtl_writephy(tp, 0x18, 0x0310);
2620 msleep(100);
2621
2622 if (rtl_apply_firmware(tp, FIRMWARE_8105E_1) < 0)
2623 netif_warn(tp, probe, tp->dev, "unable to apply firmware patch\n");
2624
2625 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2626}
2627
Francois Romieu5615d9f2007-08-17 17:50:46 +02002628static void rtl_hw_phy_config(struct net_device *dev)
2629{
2630 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002631
2632 rtl8169_print_mac_version(tp);
2633
2634 switch (tp->mac_version) {
2635 case RTL_GIGA_MAC_VER_01:
2636 break;
2637 case RTL_GIGA_MAC_VER_02:
2638 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002639 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002640 break;
2641 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002642 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002643 break;
françois romieu2e9558562009-08-10 19:44:19 +00002644 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002645 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002646 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002647 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002648 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002649 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002650 case RTL_GIGA_MAC_VER_07:
2651 case RTL_GIGA_MAC_VER_08:
2652 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002653 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002654 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002655 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002656 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002657 break;
2658 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002659 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002660 break;
2661 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002662 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002663 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002664 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002665 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002666 break;
2667 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002668 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002669 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002670 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002671 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002672 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002673 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002674 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002675 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002676 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002677 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002678 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002679 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002680 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002681 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002682 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002683 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002684 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002685 break;
2686 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002687 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002688 break;
2689 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002690 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002691 break;
françois romieue6de30d2011-01-03 15:08:37 +00002692 case RTL_GIGA_MAC_VER_28:
2693 rtl8168d_4_hw_phy_config(tp);
2694 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002695 case RTL_GIGA_MAC_VER_29:
2696 case RTL_GIGA_MAC_VER_30:
2697 rtl8105e_hw_phy_config(tp);
2698 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002699 case RTL_GIGA_MAC_VER_32:
2700 rtl8168e_1_hw_phy_config(tp);
2701 break;
2702 case RTL_GIGA_MAC_VER_33:
2703 rtl8168e_2_hw_phy_config(tp);
2704 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002705
Francois Romieu5615d9f2007-08-17 17:50:46 +02002706 default:
2707 break;
2708 }
2709}
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711static void rtl8169_phy_timer(unsigned long __opaque)
2712{
2713 struct net_device *dev = (struct net_device *)__opaque;
2714 struct rtl8169_private *tp = netdev_priv(dev);
2715 struct timer_list *timer = &tp->timer;
2716 void __iomem *ioaddr = tp->mmio_addr;
2717 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2718
Francois Romieubcf0bf92006-07-26 23:14:13 +02002719 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Francois Romieu64e4bfb2006-08-17 12:43:06 +02002721 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return;
2723
2724 spin_lock_irq(&tp->lock);
2725
françois romieu4da19632011-01-03 15:07:55 +00002726 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002727 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 * A busy loop could burn quite a few cycles on nowadays CPU.
2729 * Let's delay the execution of the timer for a few ticks.
2730 */
2731 timeout = HZ/10;
2732 goto out_mod_timer;
2733 }
2734
2735 if (tp->link_ok(ioaddr))
2736 goto out_unlock;
2737
Joe Perchesbf82c182010-02-09 11:49:50 +00002738 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
françois romieu4da19632011-01-03 15:07:55 +00002740 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742out_mod_timer:
2743 mod_timer(timer, jiffies + timeout);
2744out_unlock:
2745 spin_unlock_irq(&tp->lock);
2746}
2747
2748static inline void rtl8169_delete_timer(struct net_device *dev)
2749{
2750 struct rtl8169_private *tp = netdev_priv(dev);
2751 struct timer_list *timer = &tp->timer;
2752
Francois Romieue179bb72007-08-17 15:05:21 +02002753 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return;
2755
2756 del_timer_sync(timer);
2757}
2758
2759static inline void rtl8169_request_timer(struct net_device *dev)
2760{
2761 struct rtl8169_private *tp = netdev_priv(dev);
2762 struct timer_list *timer = &tp->timer;
2763
Francois Romieue179bb72007-08-17 15:05:21 +02002764 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return;
2766
Francois Romieu2efa53f2007-03-09 00:00:05 +01002767 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
2770#ifdef CONFIG_NET_POLL_CONTROLLER
2771/*
2772 * Polling 'interrupt' - used by things like netconsole to send skbs
2773 * without having to re-enable interrupts. It's not called while
2774 * the interrupt routine is executing.
2775 */
2776static void rtl8169_netpoll(struct net_device *dev)
2777{
2778 struct rtl8169_private *tp = netdev_priv(dev);
2779 struct pci_dev *pdev = tp->pci_dev;
2780
2781 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002782 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 enable_irq(pdev->irq);
2784}
2785#endif
2786
2787static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2788 void __iomem *ioaddr)
2789{
2790 iounmap(ioaddr);
2791 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002792 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 pci_disable_device(pdev);
2794 free_netdev(dev);
2795}
2796
Francois Romieubf793292006-11-01 00:53:05 +01002797static void rtl8169_phy_reset(struct net_device *dev,
2798 struct rtl8169_private *tp)
2799{
Francois Romieu07d3f512007-02-21 22:40:46 +01002800 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002801
françois romieu4da19632011-01-03 15:07:55 +00002802 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002803 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002804 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002805 return;
2806 msleep(1);
2807 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002808 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002809}
2810
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002811static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002813 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002814
Francois Romieu5615d9f2007-08-17 17:50:46 +02002815 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002816
Marcus Sundberg773328942008-07-10 21:28:08 +02002817 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2818 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2819 RTL_W8(0x82, 0x01);
2820 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002821
Francois Romieu6dccd162007-02-13 23:38:05 +01002822 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2823
2824 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2825 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002826
Francois Romieubcf0bf92006-07-26 23:14:13 +02002827 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002828 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2829 RTL_W8(0x82, 0x01);
2830 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002831 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002832 }
2833
Francois Romieubf793292006-11-01 00:53:05 +01002834 rtl8169_phy_reset(dev, tp);
2835
Oliver Neukum54405cd2011-01-06 21:55:13 +01002836 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
2837 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2838 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
Eric Dumazetdb552b32011-03-19 16:39:11 +01002839 (tp->mii.supports_gmii ?
Oliver Neukum54405cd2011-01-06 21:55:13 +01002840 ADVERTISED_1000baseT_Half |
Eric Dumazetdb552b32011-03-19 16:39:11 +01002841 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002842
Joe Perchesbf82c182010-02-09 11:49:50 +00002843 if (RTL_R8(PHYstatus) & TBI_Enable)
2844 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002845}
2846
Francois Romieu773d2022007-01-31 23:47:43 +01002847static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2848{
2849 void __iomem *ioaddr = tp->mmio_addr;
2850 u32 high;
2851 u32 low;
2852
2853 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2854 high = addr[4] | (addr[5] << 8);
2855
2856 spin_lock_irq(&tp->lock);
2857
2858 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002859
Francois Romieu773d2022007-01-31 23:47:43 +01002860 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002861 RTL_R32(MAC4);
2862
Francois Romieu78f1cd02010-03-27 19:35:46 -07002863 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002864 RTL_R32(MAC0);
2865
Francois Romieu773d2022007-01-31 23:47:43 +01002866 RTL_W8(Cfg9346, Cfg9346_Lock);
2867
2868 spin_unlock_irq(&tp->lock);
2869}
2870
2871static int rtl_set_mac_address(struct net_device *dev, void *p)
2872{
2873 struct rtl8169_private *tp = netdev_priv(dev);
2874 struct sockaddr *addr = p;
2875
2876 if (!is_valid_ether_addr(addr->sa_data))
2877 return -EADDRNOTAVAIL;
2878
2879 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2880
2881 rtl_rar_set(tp, dev->dev_addr);
2882
2883 return 0;
2884}
2885
Francois Romieu5f787a12006-08-17 13:02:36 +02002886static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2887{
2888 struct rtl8169_private *tp = netdev_priv(dev);
2889 struct mii_ioctl_data *data = if_mii(ifr);
2890
Francois Romieu8b4ab282008-11-19 22:05:25 -08002891 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2892}
Francois Romieu5f787a12006-08-17 13:02:36 +02002893
Francois Romieu8b4ab282008-11-19 22:05:25 -08002894static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2895{
Francois Romieu5f787a12006-08-17 13:02:36 +02002896 switch (cmd) {
2897 case SIOCGMIIPHY:
2898 data->phy_id = 32; /* Internal PHY */
2899 return 0;
2900
2901 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002902 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002903 return 0;
2904
2905 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002906 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002907 return 0;
2908 }
2909 return -EOPNOTSUPP;
2910}
2911
Francois Romieu8b4ab282008-11-19 22:05:25 -08002912static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2913{
2914 return -EOPNOTSUPP;
2915}
2916
Francois Romieu0e485152007-02-20 00:00:26 +01002917static const struct rtl_cfg_info {
2918 void (*hw_start)(struct net_device *);
2919 unsigned int region;
2920 unsigned int align;
2921 u16 intr_event;
2922 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002923 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002924 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002925} rtl_cfg_infos [] = {
2926 [RTL_CFG_0] = {
2927 .hw_start = rtl_hw_start_8169,
2928 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002929 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002930 .intr_event = SYSErr | LinkChg | RxOverflow |
2931 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002932 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002933 .features = RTL_FEATURE_GMII,
2934 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002935 },
2936 [RTL_CFG_1] = {
2937 .hw_start = rtl_hw_start_8168,
2938 .region = 2,
2939 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002940 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002941 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002942 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002943 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2944 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002945 },
2946 [RTL_CFG_2] = {
2947 .hw_start = rtl_hw_start_8101,
2948 .region = 2,
2949 .align = 8,
2950 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2951 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002952 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002953 .features = RTL_FEATURE_MSI,
2954 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002955 }
2956};
2957
Francois Romieufbac58f2007-10-04 22:51:38 +02002958/* Cfg9346_Unlock assumed. */
2959static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2960 const struct rtl_cfg_info *cfg)
2961{
2962 unsigned msi = 0;
2963 u8 cfg2;
2964
2965 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002966 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002967 if (pci_enable_msi(pdev)) {
2968 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2969 } else {
2970 cfg2 |= MSIEnable;
2971 msi = RTL_FEATURE_MSI;
2972 }
2973 }
2974 RTL_W8(Config2, cfg2);
2975 return msi;
2976}
2977
2978static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2979{
2980 if (tp->features & RTL_FEATURE_MSI) {
2981 pci_disable_msi(pdev);
2982 tp->features &= ~RTL_FEATURE_MSI;
2983 }
2984}
2985
Francois Romieu8b4ab282008-11-19 22:05:25 -08002986static const struct net_device_ops rtl8169_netdev_ops = {
2987 .ndo_open = rtl8169_open,
2988 .ndo_stop = rtl8169_close,
2989 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002990 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002991 .ndo_tx_timeout = rtl8169_tx_timeout,
2992 .ndo_validate_addr = eth_validate_addr,
2993 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00002994 .ndo_fix_features = rtl8169_fix_features,
2995 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002996 .ndo_set_mac_address = rtl_set_mac_address,
2997 .ndo_do_ioctl = rtl8169_ioctl,
2998 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002999#ifdef CONFIG_NET_POLL_CONTROLLER
3000 .ndo_poll_controller = rtl8169_netpoll,
3001#endif
3002
3003};
3004
françois romieuc0e45c12011-01-03 15:08:04 +00003005static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3006{
3007 struct mdio_ops *ops = &tp->mdio_ops;
3008
3009 switch (tp->mac_version) {
3010 case RTL_GIGA_MAC_VER_27:
3011 ops->write = r8168dp_1_mdio_write;
3012 ops->read = r8168dp_1_mdio_read;
3013 break;
françois romieue6de30d2011-01-03 15:08:37 +00003014 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003015 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003016 ops->write = r8168dp_2_mdio_write;
3017 ops->read = r8168dp_2_mdio_read;
3018 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003019 default:
3020 ops->write = r8169_mdio_write;
3021 ops->read = r8169_mdio_read;
3022 break;
3023 }
3024}
3025
françois romieu065c27c2011-01-03 15:08:12 +00003026static void r810x_phy_power_down(struct rtl8169_private *tp)
3027{
3028 rtl_writephy(tp, 0x1f, 0x0000);
3029 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3030}
3031
3032static void r810x_phy_power_up(struct rtl8169_private *tp)
3033{
3034 rtl_writephy(tp, 0x1f, 0x0000);
3035 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3036}
3037
3038static void r810x_pll_power_down(struct rtl8169_private *tp)
3039{
3040 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3041 rtl_writephy(tp, 0x1f, 0x0000);
3042 rtl_writephy(tp, MII_BMCR, 0x0000);
3043 return;
3044 }
3045
3046 r810x_phy_power_down(tp);
3047}
3048
3049static void r810x_pll_power_up(struct rtl8169_private *tp)
3050{
3051 r810x_phy_power_up(tp);
3052}
3053
3054static void r8168_phy_power_up(struct rtl8169_private *tp)
3055{
3056 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003057 switch (tp->mac_version) {
3058 case RTL_GIGA_MAC_VER_11:
3059 case RTL_GIGA_MAC_VER_12:
3060 case RTL_GIGA_MAC_VER_17:
3061 case RTL_GIGA_MAC_VER_18:
3062 case RTL_GIGA_MAC_VER_19:
3063 case RTL_GIGA_MAC_VER_20:
3064 case RTL_GIGA_MAC_VER_21:
3065 case RTL_GIGA_MAC_VER_22:
3066 case RTL_GIGA_MAC_VER_23:
3067 case RTL_GIGA_MAC_VER_24:
3068 case RTL_GIGA_MAC_VER_25:
3069 case RTL_GIGA_MAC_VER_26:
3070 case RTL_GIGA_MAC_VER_27:
3071 case RTL_GIGA_MAC_VER_28:
3072 case RTL_GIGA_MAC_VER_31:
3073 rtl_writephy(tp, 0x0e, 0x0000);
3074 break;
3075 default:
3076 break;
3077 }
françois romieu065c27c2011-01-03 15:08:12 +00003078 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3079}
3080
3081static void r8168_phy_power_down(struct rtl8169_private *tp)
3082{
3083 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003084 switch (tp->mac_version) {
3085 case RTL_GIGA_MAC_VER_32:
3086 case RTL_GIGA_MAC_VER_33:
3087 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3088 break;
3089
3090 case RTL_GIGA_MAC_VER_11:
3091 case RTL_GIGA_MAC_VER_12:
3092 case RTL_GIGA_MAC_VER_17:
3093 case RTL_GIGA_MAC_VER_18:
3094 case RTL_GIGA_MAC_VER_19:
3095 case RTL_GIGA_MAC_VER_20:
3096 case RTL_GIGA_MAC_VER_21:
3097 case RTL_GIGA_MAC_VER_22:
3098 case RTL_GIGA_MAC_VER_23:
3099 case RTL_GIGA_MAC_VER_24:
3100 case RTL_GIGA_MAC_VER_25:
3101 case RTL_GIGA_MAC_VER_26:
3102 case RTL_GIGA_MAC_VER_27:
3103 case RTL_GIGA_MAC_VER_28:
3104 case RTL_GIGA_MAC_VER_31:
3105 rtl_writephy(tp, 0x0e, 0x0200);
3106 default:
3107 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3108 break;
3109 }
françois romieu065c27c2011-01-03 15:08:12 +00003110}
3111
3112static void r8168_pll_power_down(struct rtl8169_private *tp)
3113{
3114 void __iomem *ioaddr = tp->mmio_addr;
3115
Hayes Wang5d2e1952011-02-22 17:26:22 +08003116 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
hayeswang4804b3b2011-03-21 01:50:29 +00003117 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3118 (tp->mac_version == RTL_GIGA_MAC_VER_31)) &&
3119 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003120 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003121 }
françois romieu065c27c2011-01-03 15:08:12 +00003122
3123 if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
3124 (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
3125 (RTL_R16(CPlusCmd) & ASF)) {
3126 return;
3127 }
3128
hayeswang01dc7fe2011-03-21 01:50:28 +00003129 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3130 tp->mac_version == RTL_GIGA_MAC_VER_33)
3131 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3132
françois romieu065c27c2011-01-03 15:08:12 +00003133 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3134 rtl_writephy(tp, 0x1f, 0x0000);
3135 rtl_writephy(tp, MII_BMCR, 0x0000);
3136
3137 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3138 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3139 return;
3140 }
3141
3142 r8168_phy_power_down(tp);
3143
3144 switch (tp->mac_version) {
3145 case RTL_GIGA_MAC_VER_25:
3146 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003147 case RTL_GIGA_MAC_VER_27:
3148 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003149 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003150 case RTL_GIGA_MAC_VER_32:
3151 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003152 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3153 break;
3154 }
3155}
3156
3157static void r8168_pll_power_up(struct rtl8169_private *tp)
3158{
3159 void __iomem *ioaddr = tp->mmio_addr;
3160
Hayes Wang5d2e1952011-02-22 17:26:22 +08003161 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
hayeswang4804b3b2011-03-21 01:50:29 +00003162 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3163 (tp->mac_version == RTL_GIGA_MAC_VER_31)) &&
3164 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003165 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003166 }
françois romieu065c27c2011-01-03 15:08:12 +00003167
3168 switch (tp->mac_version) {
3169 case RTL_GIGA_MAC_VER_25:
3170 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003171 case RTL_GIGA_MAC_VER_27:
3172 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003173 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003174 case RTL_GIGA_MAC_VER_32:
3175 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003176 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3177 break;
3178 }
3179
3180 r8168_phy_power_up(tp);
3181}
3182
3183static void rtl_pll_power_op(struct rtl8169_private *tp,
3184 void (*op)(struct rtl8169_private *))
3185{
3186 if (op)
3187 op(tp);
3188}
3189
3190static void rtl_pll_power_down(struct rtl8169_private *tp)
3191{
3192 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3193}
3194
3195static void rtl_pll_power_up(struct rtl8169_private *tp)
3196{
3197 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3198}
3199
3200static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3201{
3202 struct pll_power_ops *ops = &tp->pll_power_ops;
3203
3204 switch (tp->mac_version) {
3205 case RTL_GIGA_MAC_VER_07:
3206 case RTL_GIGA_MAC_VER_08:
3207 case RTL_GIGA_MAC_VER_09:
3208 case RTL_GIGA_MAC_VER_10:
3209 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003210 case RTL_GIGA_MAC_VER_29:
3211 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003212 ops->down = r810x_pll_power_down;
3213 ops->up = r810x_pll_power_up;
3214 break;
3215
3216 case RTL_GIGA_MAC_VER_11:
3217 case RTL_GIGA_MAC_VER_12:
3218 case RTL_GIGA_MAC_VER_17:
3219 case RTL_GIGA_MAC_VER_18:
3220 case RTL_GIGA_MAC_VER_19:
3221 case RTL_GIGA_MAC_VER_20:
3222 case RTL_GIGA_MAC_VER_21:
3223 case RTL_GIGA_MAC_VER_22:
3224 case RTL_GIGA_MAC_VER_23:
3225 case RTL_GIGA_MAC_VER_24:
3226 case RTL_GIGA_MAC_VER_25:
3227 case RTL_GIGA_MAC_VER_26:
3228 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003229 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003230 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003231 case RTL_GIGA_MAC_VER_32:
3232 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003233 ops->down = r8168_pll_power_down;
3234 ops->up = r8168_pll_power_up;
3235 break;
3236
3237 default:
3238 ops->down = NULL;
3239 ops->up = NULL;
3240 break;
3241 }
3242}
3243
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003244static int __devinit
3245rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3246{
Francois Romieu0e485152007-02-20 00:00:26 +01003247 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3248 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003250 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003251 struct net_device *dev;
3252 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003253 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003254 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003256 if (netif_msg_drv(&debug)) {
3257 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3258 MODULENAME, RTL8169_VERSION);
3259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003262 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003263 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003264 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003265 rc = -ENOMEM;
3266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 }
3268
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003270 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003272 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003273 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003274 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Francois Romieuccdffb92008-07-26 14:26:06 +02003276 mii = &tp->mii;
3277 mii->dev = dev;
3278 mii->mdio_read = rtl_mdio_read;
3279 mii->mdio_write = rtl_mdio_write;
3280 mii->phy_id_mask = 0x1f;
3281 mii->reg_num_mask = 0x1f;
3282 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3283
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003284 /* disable ASPM completely as that cause random device stop working
3285 * problems as well as full system hangs for some PCIe devices users */
3286 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3287 PCIE_LINK_STATE_CLKPM);
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3290 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003291 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003292 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003293 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 }
3295
françois romieu87aeec72010-04-26 11:42:06 +00003296 if (pci_set_mwi(pdev) < 0)
3297 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003300 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003301 netif_err(tp, probe, dev,
3302 "region #%d not an MMIO resource, aborting\n",
3303 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003305 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003309 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003310 netif_err(tp, probe, dev,
3311 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003313 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
3315
3316 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003317 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003318 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003319 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003322 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003325 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 tp->cp_cmd |= PCIDAC;
3327 dev->features |= NETIF_F_HIGHDMA;
3328 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003329 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003331 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003332 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 }
3334 }
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003337 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003338 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003339 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003341 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343
David S. Miller4300e8c2010-03-26 10:23:30 -07003344 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3345 if (!tp->pcie_cap)
3346 netif_info(tp, probe, dev, "no PCI Express capability\n");
3347
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003348 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 /* Soft reset the chip. */
3351 RTL_W8(ChipCmd, CmdReset);
3352
3353 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003354 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3356 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003357 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
3359
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003360 RTL_W16(IntrStatus, 0xffff);
3361
françois romieuca52efd2009-07-24 12:34:19 +00003362 pci_set_master(pdev);
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 /* Identify chip attached to board */
3365 rtl8169_get_mac_version(tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Francois Romieu7a8fc772011-03-01 17:18:33 +01003367 /*
3368 * Pretend we are using VLANs; This bypasses a nasty bug where
3369 * Interrupts stop flowing on high load on 8110SCd controllers.
3370 */
3371 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3372 tp->cp_cmd |= RxVlan;
3373
françois romieuc0e45c12011-01-03 15:08:04 +00003374 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003375 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003376
Jean Delvaref21b75e2009-05-26 20:54:48 -07003377 /* Use appropriate default if unknown */
3378 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003379 netif_notice(tp, probe, dev,
3380 "unknown MAC, using family default\n");
Jean Delvaref21b75e2009-05-26 20:54:48 -07003381 tp->mac_version = cfg->default_ver;
3382 }
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Roel Kluincee60c32008-04-17 22:35:54 +02003386 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 if (tp->mac_version == rtl_chip_info[i].mac_version)
3388 break;
3389 }
Roel Kluincee60c32008-04-17 22:35:54 +02003390 if (i == ARRAY_SIZE(rtl_chip_info)) {
Jean Delvaref21b75e2009-05-26 20:54:48 -07003391 dev_err(&pdev->dev,
3392 "driver bug, MAC version not found in rtl_chip_info\n");
françois romieu87aeec72010-04-26 11:42:06 +00003393 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
Francois Romieu2b7b4312011-04-18 22:53:24 -07003395 chipset = i;
3396 tp->txd_version = rtl_chip_info[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Francois Romieu5d06a992006-02-23 00:47:58 +01003398 RTL_W8(Cfg9346, Cfg9346_Unlock);
3399 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3400 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003401 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3402 tp->features |= RTL_FEATURE_WOL;
3403 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3404 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003405 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003406 RTL_W8(Cfg9346, Cfg9346_Lock);
3407
Francois Romieu66ec5d42007-11-06 22:56:10 +01003408 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3409 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 tp->set_speed = rtl8169_set_speed_tbi;
3411 tp->get_settings = rtl8169_gset_tbi;
3412 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3413 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3414 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003415 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Francois Romieu64e4bfb2006-08-17 12:43:06 +02003417 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 } else {
3419 tp->set_speed = rtl8169_set_speed_xmii;
3420 tp->get_settings = rtl8169_gset_xmii;
3421 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3422 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3423 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003424 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
3426
Francois Romieudf58ef52008-10-09 14:35:58 -07003427 spin_lock_init(&tp->lock);
3428
Petr Vandrovec738e1e62008-10-12 20:58:29 -07003429 tp->mmio_addr = ioaddr;
3430
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003431 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 for (i = 0; i < MAC_ADDR_LEN; i++)
3433 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003434 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3438 dev->irq = pdev->irq;
3439 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003441 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Michał Mirosław350fb322011-04-08 06:35:56 +00003443 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3444 * properly for all devices */
3445 dev->features |= NETIF_F_RXCSUM |
3446 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3447
3448 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3449 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3450 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3451 NETIF_F_HIGHDMA;
3452
3453 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3454 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3455 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003458 tp->hw_start = cfg->hw_start;
3459 tp->intr_event = cfg->intr_event;
3460 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Francois Romieu2efa53f2007-03-09 00:00:05 +01003462 init_timer(&tp->timer);
3463 tp->timer.data = (unsigned long) dev;
3464 tp->timer.function = rtl8169_phy_timer;
3465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003467 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003468 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470 pci_set_drvdata(pdev, dev);
3471
Joe Perchesbf82c182010-02-09 11:49:50 +00003472 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu2b7b4312011-04-18 22:53:24 -07003473 rtl_chip_info[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003474 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
françois romieue6de30d2011-01-03 15:08:37 +00003476 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
hayeswang4804b3b2011-03-21 01:50:29 +00003477 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3478 (tp->mac_version == RTL_GIGA_MAC_VER_31)) {
françois romieub646d902011-01-03 15:08:21 +00003479 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003480 }
françois romieub646d902011-01-03 15:08:21 +00003481
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003482 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Alan Sternf3ec4f82010-06-08 15:23:51 -04003484 if (pci_dev_run_wake(pdev))
3485 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003486
Ivan Vecera0d672e92011-02-15 02:08:39 +00003487 netif_carrier_off(dev);
3488
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003489out:
3490 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
françois romieu87aeec72010-04-26 11:42:06 +00003492err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003493 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003494 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003495err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003496 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003497err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003498 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003499 pci_disable_device(pdev);
3500err_out_free_dev_1:
3501 free_netdev(dev);
3502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503}
3504
Francois Romieu07d3f512007-02-21 22:40:46 +01003505static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506{
3507 struct net_device *dev = pci_get_drvdata(pdev);
3508 struct rtl8169_private *tp = netdev_priv(dev);
3509
françois romieue6de30d2011-01-03 15:08:37 +00003510 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
hayeswang4804b3b2011-03-21 01:50:29 +00003511 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3512 (tp->mac_version == RTL_GIGA_MAC_VER_31)) {
françois romieub646d902011-01-03 15:08:21 +00003513 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003514 }
françois romieub646d902011-01-03 15:08:21 +00003515
Tejun Heo23f333a2010-12-12 16:45:14 +01003516 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003517
françois romieuf1e02ed2011-01-13 13:07:53 +00003518 rtl_release_firmware(tp);
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003521
Alan Sternf3ec4f82010-06-08 15:23:51 -04003522 if (pci_dev_run_wake(pdev))
3523 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003524
Ivan Veceracc098dc2009-11-29 23:12:52 -08003525 /* restore original MAC address */
3526 rtl_rar_set(tp, dev->perm_addr);
3527
Francois Romieufbac58f2007-10-04 22:51:38 +02003528 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3530 pci_set_drvdata(pdev, NULL);
3531}
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533static int rtl8169_open(struct net_device *dev)
3534{
3535 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003536 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003538 int retval = -ENOMEM;
3539
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003540 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Neil Hormanc0cd8842010-03-29 13:16:02 -07003542 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003544 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003546 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3547 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003549 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003551 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3552 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003554 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 retval = rtl8169_init_ring(dev);
3557 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003558 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
David Howellsc4028952006-11-22 14:57:56 +00003560 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Francois Romieu99f252b2007-04-02 22:59:59 +02003562 smp_mb();
3563
Francois Romieufbac58f2007-10-04 22:51:38 +02003564 retval = request_irq(dev->irq, rtl8169_interrupt,
3565 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003566 dev->name, dev);
3567 if (retval < 0)
3568 goto err_release_ring_2;
3569
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003570 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003571
françois romieueee3a962011-01-08 02:17:26 +00003572 rtl8169_init_phy(dev, tp);
3573
Michał Mirosław350fb322011-04-08 06:35:56 +00003574 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003575
françois romieu065c27c2011-01-03 15:08:12 +00003576 rtl_pll_power_up(tp);
3577
Francois Romieu07ce4062007-02-23 23:36:39 +01003578 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
3580 rtl8169_request_timer(dev);
3581
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003582 tp->saved_wolopts = 0;
3583 pm_runtime_put_noidle(&pdev->dev);
3584
françois romieueee3a962011-01-08 02:17:26 +00003585 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586out:
3587 return retval;
3588
Francois Romieu99f252b2007-04-02 22:59:59 +02003589err_release_ring_2:
3590 rtl8169_rx_clear(tp);
3591err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003592 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3593 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003594 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003595err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003596 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3597 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003598 tp->TxDescArray = NULL;
3599err_pm_runtime_put:
3600 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 goto out;
3602}
3603
françois romieue6de30d2011-01-03 15:08:37 +00003604static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
françois romieue6de30d2011-01-03 15:08:37 +00003606 void __iomem *ioaddr = tp->mmio_addr;
3607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 /* Disable interrupts */
3609 rtl8169_irq_mask_and_ack(ioaddr);
3610
Hayes Wang5d2e1952011-02-22 17:26:22 +08003611 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003612 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3613 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003614 while (RTL_R8(TxPoll) & NPQ)
3615 udelay(20);
3616
3617 }
3618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 /* Reset the chipset */
3620 RTL_W8(ChipCmd, CmdReset);
3621
3622 /* PCI commit */
3623 RTL_R8(ChipCmd);
3624}
3625
Francois Romieu7f796d82007-06-11 23:04:41 +02003626static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003627{
3628 void __iomem *ioaddr = tp->mmio_addr;
3629 u32 cfg = rtl8169_rx_config;
3630
Francois Romieu2b7b4312011-04-18 22:53:24 -07003631 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003632 RTL_W32(RxConfig, cfg);
3633
3634 /* Set DMA burst size and Interframe Gap Time */
3635 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3636 (InterFrameGap << TxInterFrameGapShift));
3637}
3638
Francois Romieu07ce4062007-02-23 23:36:39 +01003639static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
3641 struct rtl8169_private *tp = netdev_priv(dev);
3642 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01003643 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
3645 /* Soft reset the chip. */
3646 RTL_W8(ChipCmd, CmdReset);
3647
3648 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003649 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3651 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003652 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
3654
Francois Romieu07ce4062007-02-23 23:36:39 +01003655 tp->hw_start(dev);
3656
Francois Romieu07ce4062007-02-23 23:36:39 +01003657 netif_start_queue(dev);
3658}
3659
3660
Francois Romieu7f796d82007-06-11 23:04:41 +02003661static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3662 void __iomem *ioaddr)
3663{
3664 /*
3665 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3666 * register to be written before TxDescAddrLow to work.
3667 * Switching from MMIO to I/O access fixes the issue as well.
3668 */
3669 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003670 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003671 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003672 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003673}
3674
3675static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3676{
3677 u16 cmd;
3678
3679 cmd = RTL_R16(CPlusCmd);
3680 RTL_W16(CPlusCmd, cmd);
3681 return cmd;
3682}
3683
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003684static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003685{
3686 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003687 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003688}
3689
Francois Romieu6dccd162007-02-13 23:38:05 +01003690static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3691{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003692 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003693 u32 mac_version;
3694 u32 clk;
3695 u32 val;
3696 } cfg2_info [] = {
3697 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3698 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3699 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3700 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3701 }, *p = cfg2_info;
3702 unsigned int i;
3703 u32 clk;
3704
3705 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003706 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003707 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3708 RTL_W32(0x7c, p->val);
3709 break;
3710 }
3711 }
3712}
3713
Francois Romieu07ce4062007-02-23 23:36:39 +01003714static void rtl_hw_start_8169(struct net_device *dev)
3715{
3716 struct rtl8169_private *tp = netdev_priv(dev);
3717 void __iomem *ioaddr = tp->mmio_addr;
3718 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003719
Francois Romieu9cb427b2006-11-02 00:10:16 +01003720 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3721 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3722 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3723 }
3724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003726 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3727 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3728 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3729 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3730 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3731
françois romieuf0298f82011-01-03 15:07:42 +00003732 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003734 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Francois Romieuc946b302007-10-04 00:42:50 +02003736 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3737 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3738 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3739 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3740 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Francois Romieu7f796d82007-06-11 23:04:41 +02003742 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003743
3744 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3745 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
Joe Perches06fa7352007-10-18 21:15:00 +02003746 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003748 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 }
3750
Francois Romieubcf0bf92006-07-26 23:14:13 +02003751 RTL_W16(CPlusCmd, tp->cp_cmd);
3752
Francois Romieu6dccd162007-02-13 23:38:05 +01003753 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3754
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 /*
3756 * Undocumented corner. Supposedly:
3757 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3758 */
3759 RTL_W16(IntrMitigate, 0x0000);
3760
Francois Romieu7f796d82007-06-11 23:04:41 +02003761 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003762
Francois Romieuc946b302007-10-04 00:42:50 +02003763 if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
3764 (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
3765 (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
3766 (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
3767 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3768 rtl_set_rx_tx_config_registers(tp);
3769 }
3770
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003772
3773 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3774 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
3776 RTL_W32(RxMissed, 0);
3777
Francois Romieu07ce4062007-02-23 23:36:39 +01003778 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
3780 /* no early-rx interrupts */
3781 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003782
3783 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003784 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003785}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
Francois Romieu9c14cea2008-07-05 00:21:15 +02003787static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003788{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003789 struct net_device *dev = pci_get_drvdata(pdev);
3790 struct rtl8169_private *tp = netdev_priv(dev);
3791 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003792
Francois Romieu9c14cea2008-07-05 00:21:15 +02003793 if (cap) {
3794 u16 ctl;
3795
3796 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3797 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3798 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3799 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003800}
3801
françois romieu650e8d52011-01-03 15:08:29 +00003802static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003803{
3804 u32 csi;
3805
3806 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003807 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3808}
3809
françois romieue6de30d2011-01-03 15:08:37 +00003810static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3811{
3812 rtl_csi_access_enable(ioaddr, 0x17000000);
3813}
3814
françois romieu650e8d52011-01-03 15:08:29 +00003815static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3816{
3817 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003818}
3819
3820struct ephy_info {
3821 unsigned int offset;
3822 u16 mask;
3823 u16 bits;
3824};
3825
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003826static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003827{
3828 u16 w;
3829
3830 while (len-- > 0) {
3831 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3832 rtl_ephy_write(ioaddr, e->offset, w);
3833 e++;
3834 }
3835}
3836
Francois Romieub726e492008-06-28 12:22:59 +02003837static void rtl_disable_clock_request(struct pci_dev *pdev)
3838{
3839 struct net_device *dev = pci_get_drvdata(pdev);
3840 struct rtl8169_private *tp = netdev_priv(dev);
3841 int cap = tp->pcie_cap;
3842
3843 if (cap) {
3844 u16 ctl;
3845
3846 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3847 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3848 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3849 }
3850}
3851
françois romieue6de30d2011-01-03 15:08:37 +00003852static void rtl_enable_clock_request(struct pci_dev *pdev)
3853{
3854 struct net_device *dev = pci_get_drvdata(pdev);
3855 struct rtl8169_private *tp = netdev_priv(dev);
3856 int cap = tp->pcie_cap;
3857
3858 if (cap) {
3859 u16 ctl;
3860
3861 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3862 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3863 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3864 }
3865}
3866
Francois Romieub726e492008-06-28 12:22:59 +02003867#define R8168_CPCMD_QUIRK_MASK (\
3868 EnableBist | \
3869 Mac_dbgo_oe | \
3870 Force_half_dup | \
3871 Force_rxflow_en | \
3872 Force_txflow_en | \
3873 Cxpl_dbg_sel | \
3874 ASF | \
3875 PktCntrDisable | \
3876 Mac_dbgo_sel)
3877
Francois Romieu219a1e92008-06-28 11:58:39 +02003878static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3879{
Francois Romieub726e492008-06-28 12:22:59 +02003880 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3881
3882 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3883
Francois Romieu2e68ae42008-06-28 12:00:55 +02003884 rtl_tx_performance_tweak(pdev,
3885 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003886}
3887
3888static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3889{
3890 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003891
françois romieuf0298f82011-01-03 15:07:42 +00003892 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003893
3894 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003895}
3896
3897static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3898{
Francois Romieub726e492008-06-28 12:22:59 +02003899 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3900
3901 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3902
Francois Romieu219a1e92008-06-28 11:58:39 +02003903 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003904
3905 rtl_disable_clock_request(pdev);
3906
3907 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003908}
3909
Francois Romieuef3386f2008-06-29 12:24:30 +02003910static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003911{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003912 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003913 { 0x01, 0, 0x0001 },
3914 { 0x02, 0x0800, 0x1000 },
3915 { 0x03, 0, 0x0042 },
3916 { 0x06, 0x0080, 0x0000 },
3917 { 0x07, 0, 0x2000 }
3918 };
3919
françois romieu650e8d52011-01-03 15:08:29 +00003920 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003921
3922 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3923
Francois Romieu219a1e92008-06-28 11:58:39 +02003924 __rtl_hw_start_8168cp(ioaddr, pdev);
3925}
3926
Francois Romieuef3386f2008-06-29 12:24:30 +02003927static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3928{
françois romieu650e8d52011-01-03 15:08:29 +00003929 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003930
3931 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3932
3933 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3934
3935 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3936}
3937
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003938static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3939{
françois romieu650e8d52011-01-03 15:08:29 +00003940 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003941
3942 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3943
3944 /* Magic. */
3945 RTL_W8(DBG_REG, 0x20);
3946
françois romieuf0298f82011-01-03 15:07:42 +00003947 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003948
3949 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3950
3951 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3952}
3953
Francois Romieu219a1e92008-06-28 11:58:39 +02003954static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3955{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003956 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003957 { 0x02, 0x0800, 0x1000 },
3958 { 0x03, 0, 0x0002 },
3959 { 0x06, 0x0080, 0x0000 }
3960 };
3961
françois romieu650e8d52011-01-03 15:08:29 +00003962 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003963
3964 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3965
3966 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3967
Francois Romieu219a1e92008-06-28 11:58:39 +02003968 __rtl_hw_start_8168cp(ioaddr, pdev);
3969}
3970
3971static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3972{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003973 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003974 { 0x01, 0, 0x0001 },
3975 { 0x03, 0x0400, 0x0220 }
3976 };
3977
françois romieu650e8d52011-01-03 15:08:29 +00003978 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003979
3980 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3981
Francois Romieu219a1e92008-06-28 11:58:39 +02003982 __rtl_hw_start_8168cp(ioaddr, pdev);
3983}
3984
Francois Romieu197ff762008-06-28 13:16:02 +02003985static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3986{
3987 rtl_hw_start_8168c_2(ioaddr, pdev);
3988}
3989
Francois Romieu6fb07052008-06-29 11:54:28 +02003990static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3991{
françois romieu650e8d52011-01-03 15:08:29 +00003992 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02003993
3994 __rtl_hw_start_8168cp(ioaddr, pdev);
3995}
3996
Francois Romieu5b538df2008-07-20 16:22:45 +02003997static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3998{
françois romieu650e8d52011-01-03 15:08:29 +00003999 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004000
4001 rtl_disable_clock_request(pdev);
4002
françois romieuf0298f82011-01-03 15:07:42 +00004003 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004004
4005 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4006
4007 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4008}
4009
hayeswang4804b3b2011-03-21 01:50:29 +00004010static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4011{
4012 rtl_csi_access_enable_1(ioaddr);
4013
4014 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4015
4016 RTL_W8(MaxTxPacketSize, TxPacketMax);
4017
4018 rtl_disable_clock_request(pdev);
4019}
4020
françois romieue6de30d2011-01-03 15:08:37 +00004021static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4022{
4023 static const struct ephy_info e_info_8168d_4[] = {
4024 { 0x0b, ~0, 0x48 },
4025 { 0x19, 0x20, 0x50 },
4026 { 0x0c, ~0, 0x20 }
4027 };
4028 int i;
4029
4030 rtl_csi_access_enable_1(ioaddr);
4031
4032 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4033
4034 RTL_W8(MaxTxPacketSize, TxPacketMax);
4035
4036 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4037 const struct ephy_info *e = e_info_8168d_4 + i;
4038 u16 w;
4039
4040 w = rtl_ephy_read(ioaddr, e->offset);
4041 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4042 }
4043
4044 rtl_enable_clock_request(pdev);
4045}
4046
hayeswang01dc7fe2011-03-21 01:50:28 +00004047static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4048{
4049 static const struct ephy_info e_info_8168e[] = {
4050 { 0x00, 0x0200, 0x0100 },
4051 { 0x00, 0x0000, 0x0004 },
4052 { 0x06, 0x0002, 0x0001 },
4053 { 0x06, 0x0000, 0x0030 },
4054 { 0x07, 0x0000, 0x2000 },
4055 { 0x00, 0x0000, 0x0020 },
4056 { 0x03, 0x5800, 0x2000 },
4057 { 0x03, 0x0000, 0x0001 },
4058 { 0x01, 0x0800, 0x1000 },
4059 { 0x07, 0x0000, 0x4000 },
4060 { 0x1e, 0x0000, 0x2000 },
4061 { 0x19, 0xffff, 0xfe6c },
4062 { 0x0a, 0x0000, 0x0040 }
4063 };
4064
4065 rtl_csi_access_enable_2(ioaddr);
4066
4067 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4068
4069 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4070
4071 RTL_W8(MaxTxPacketSize, TxPacketMax);
4072
4073 rtl_disable_clock_request(pdev);
4074
4075 /* Reset tx FIFO pointer */
4076 RTL_W32(MISC, RTL_R32(MISC) | txpla_rst);
4077 RTL_W32(MISC, RTL_R32(MISC) & ~txpla_rst);
4078
4079 RTL_W8(Config5, RTL_R8(Config5) & ~spi_en);
4080}
4081
Francois Romieu07ce4062007-02-23 23:36:39 +01004082static void rtl_hw_start_8168(struct net_device *dev)
4083{
Francois Romieu2dd99532007-06-11 23:22:52 +02004084 struct rtl8169_private *tp = netdev_priv(dev);
4085 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004086 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004087
4088 RTL_W8(Cfg9346, Cfg9346_Unlock);
4089
françois romieuf0298f82011-01-03 15:07:42 +00004090 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004091
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004092 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004093
Francois Romieu0e485152007-02-20 00:00:26 +01004094 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004095
4096 RTL_W16(CPlusCmd, tp->cp_cmd);
4097
Francois Romieu0e485152007-02-20 00:00:26 +01004098 RTL_W16(IntrMitigate, 0x5151);
4099
4100 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004101 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4102 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004103 tp->intr_event |= RxFIFOOver | PCSTimeout;
4104 tp->intr_event &= ~RxOverflow;
4105 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004106
4107 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4108
Francois Romieub8363902008-06-01 12:31:57 +02004109 rtl_set_rx_mode(dev);
4110
4111 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4112 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004113
4114 RTL_R8(IntrMask);
4115
Francois Romieu219a1e92008-06-28 11:58:39 +02004116 switch (tp->mac_version) {
4117 case RTL_GIGA_MAC_VER_11:
4118 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004119 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004120
4121 case RTL_GIGA_MAC_VER_12:
4122 case RTL_GIGA_MAC_VER_17:
4123 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004124 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004125
4126 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004127 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004128 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004129
4130 case RTL_GIGA_MAC_VER_19:
4131 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004132 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004133
4134 case RTL_GIGA_MAC_VER_20:
4135 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004136 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004137
Francois Romieu197ff762008-06-28 13:16:02 +02004138 case RTL_GIGA_MAC_VER_21:
4139 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004140 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004141
Francois Romieu6fb07052008-06-29 11:54:28 +02004142 case RTL_GIGA_MAC_VER_22:
4143 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004144 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004145
Francois Romieuef3386f2008-06-29 12:24:30 +02004146 case RTL_GIGA_MAC_VER_23:
4147 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004148 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004149
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004150 case RTL_GIGA_MAC_VER_24:
4151 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004152 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004153
Francois Romieu5b538df2008-07-20 16:22:45 +02004154 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004155 case RTL_GIGA_MAC_VER_26:
4156 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004157 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004158 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004159
françois romieue6de30d2011-01-03 15:08:37 +00004160 case RTL_GIGA_MAC_VER_28:
4161 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004162 break;
4163 case RTL_GIGA_MAC_VER_31:
4164 rtl_hw_start_8168dp(ioaddr, pdev);
4165 break;
4166
hayeswang01dc7fe2011-03-21 01:50:28 +00004167 case RTL_GIGA_MAC_VER_32:
4168 case RTL_GIGA_MAC_VER_33:
4169 rtl_hw_start_8168e(ioaddr, pdev);
4170 break;
françois romieue6de30d2011-01-03 15:08:37 +00004171
Francois Romieu219a1e92008-06-28 11:58:39 +02004172 default:
4173 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4174 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004175 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004176 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004177
Francois Romieu0e485152007-02-20 00:00:26 +01004178 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4179
Francois Romieub8363902008-06-01 12:31:57 +02004180 RTL_W8(Cfg9346, Cfg9346_Lock);
4181
Francois Romieu2dd99532007-06-11 23:22:52 +02004182 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004183
Francois Romieu0e485152007-02-20 00:00:26 +01004184 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Francois Romieu2857ffb2008-08-02 21:08:49 +02004187#define R810X_CPCMD_QUIRK_MASK (\
4188 EnableBist | \
4189 Mac_dbgo_oe | \
4190 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004191 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004192 Force_txflow_en | \
4193 Cxpl_dbg_sel | \
4194 ASF | \
4195 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004196 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004197
4198static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4199{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004200 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004201 { 0x01, 0, 0x6e65 },
4202 { 0x02, 0, 0x091f },
4203 { 0x03, 0, 0xc2f9 },
4204 { 0x06, 0, 0xafb5 },
4205 { 0x07, 0, 0x0e00 },
4206 { 0x19, 0, 0xec80 },
4207 { 0x01, 0, 0x2e65 },
4208 { 0x01, 0, 0x6e65 }
4209 };
4210 u8 cfg1;
4211
françois romieu650e8d52011-01-03 15:08:29 +00004212 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004213
4214 RTL_W8(DBG_REG, FIX_NAK_1);
4215
4216 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4217
4218 RTL_W8(Config1,
4219 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4220 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4221
4222 cfg1 = RTL_R8(Config1);
4223 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4224 RTL_W8(Config1, cfg1 & ~LEDS0);
4225
Francois Romieu2857ffb2008-08-02 21:08:49 +02004226 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4227}
4228
4229static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4230{
françois romieu650e8d52011-01-03 15:08:29 +00004231 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004232
4233 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4234
4235 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4236 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004237}
4238
4239static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4240{
4241 rtl_hw_start_8102e_2(ioaddr, pdev);
4242
4243 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4244}
4245
Hayes Wang5a5e4442011-02-22 17:26:21 +08004246static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4247{
4248 static const struct ephy_info e_info_8105e_1[] = {
4249 { 0x07, 0, 0x4000 },
4250 { 0x19, 0, 0x0200 },
4251 { 0x19, 0, 0x0020 },
4252 { 0x1e, 0, 0x2000 },
4253 { 0x03, 0, 0x0001 },
4254 { 0x19, 0, 0x0100 },
4255 { 0x19, 0, 0x0004 },
4256 { 0x0a, 0, 0x0020 }
4257 };
4258
4259 /* Force LAN exit from ASPM if Rx/Tx are not idel */
4260 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4261
4262 /* disable Early Tally Counter */
4263 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4264
4265 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4266 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4267
4268 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4269}
4270
4271static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4272{
4273 rtl_hw_start_8105e_1(ioaddr, pdev);
4274 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4275}
4276
Francois Romieu07ce4062007-02-23 23:36:39 +01004277static void rtl_hw_start_8101(struct net_device *dev)
4278{
Francois Romieucdf1a602007-06-11 23:29:50 +02004279 struct rtl8169_private *tp = netdev_priv(dev);
4280 void __iomem *ioaddr = tp->mmio_addr;
4281 struct pci_dev *pdev = tp->pci_dev;
4282
Francois Romieue3cf0cc2007-08-17 14:55:46 +02004283 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
4284 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02004285 int cap = tp->pcie_cap;
4286
4287 if (cap) {
4288 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4289 PCI_EXP_DEVCTL_NOSNOOP_EN);
4290 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004291 }
4292
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004293 RTL_W8(Cfg9346, Cfg9346_Unlock);
4294
Francois Romieu2857ffb2008-08-02 21:08:49 +02004295 switch (tp->mac_version) {
4296 case RTL_GIGA_MAC_VER_07:
4297 rtl_hw_start_8102e_1(ioaddr, pdev);
4298 break;
4299
4300 case RTL_GIGA_MAC_VER_08:
4301 rtl_hw_start_8102e_3(ioaddr, pdev);
4302 break;
4303
4304 case RTL_GIGA_MAC_VER_09:
4305 rtl_hw_start_8102e_2(ioaddr, pdev);
4306 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004307
4308 case RTL_GIGA_MAC_VER_29:
4309 rtl_hw_start_8105e_1(ioaddr, pdev);
4310 break;
4311 case RTL_GIGA_MAC_VER_30:
4312 rtl_hw_start_8105e_2(ioaddr, pdev);
4313 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004314 }
4315
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004316 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004317
françois romieuf0298f82011-01-03 15:07:42 +00004318 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004319
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004320 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004321
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004322 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004323 RTL_W16(CPlusCmd, tp->cp_cmd);
4324
4325 RTL_W16(IntrMitigate, 0x0000);
4326
4327 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4328
4329 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4330 rtl_set_rx_tx_config_registers(tp);
4331
Francois Romieucdf1a602007-06-11 23:29:50 +02004332 RTL_R8(IntrMask);
4333
Francois Romieucdf1a602007-06-11 23:29:50 +02004334 rtl_set_rx_mode(dev);
4335
4336 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004337
Francois Romieu0e485152007-02-20 00:00:26 +01004338 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
4341static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4344 return -EINVAL;
4345
4346 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004347 netdev_update_features(dev);
4348
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350}
4351
4352static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4353{
Al Viro95e09182007-12-22 18:55:39 +00004354 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4356}
4357
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004358static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4359 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004361 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004362 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004363
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004364 kfree(*data_buff);
4365 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 rtl8169_make_unusable_by_asic(desc);
4367}
4368
4369static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4370{
4371 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4372
4373 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4374}
4375
4376static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4377 u32 rx_buf_sz)
4378{
4379 desc->addr = cpu_to_le64(mapping);
4380 wmb();
4381 rtl8169_mark_to_asic(desc, rx_buf_sz);
4382}
4383
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004384static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004386 return (void *)ALIGN((long)data, 16);
4387}
4388
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004389static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4390 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004391{
4392 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004394 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004395 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004396 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004398 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4399 if (!data)
4400 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004401
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004402 if (rtl8169_align(data) != data) {
4403 kfree(data);
4404 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4405 if (!data)
4406 return NULL;
4407 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004408
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004409 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004410 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004411 if (unlikely(dma_mapping_error(d, mapping))) {
4412 if (net_ratelimit())
4413 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004414 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
4417 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004418 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004419
4420err_out:
4421 kfree(data);
4422 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423}
4424
4425static void rtl8169_rx_clear(struct rtl8169_private *tp)
4426{
Francois Romieu07d3f512007-02-21 22:40:46 +01004427 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
4429 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004430 if (tp->Rx_databuff[i]) {
4431 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 tp->RxDescArray + i);
4433 }
4434 }
4435}
4436
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004437static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004439 desc->opts1 |= cpu_to_le32(RingEnd);
4440}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004441
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004442static int rtl8169_rx_fill(struct rtl8169_private *tp)
4443{
4444 unsigned int i;
4445
4446 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004447 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004448
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004449 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004451
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004452 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004453 if (!data) {
4454 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004455 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004456 }
4457 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004460 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4461 return 0;
4462
4463err_out:
4464 rtl8169_rx_clear(tp);
4465 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
4467
4468static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4469{
4470 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4471}
4472
4473static int rtl8169_init_ring(struct net_device *dev)
4474{
4475 struct rtl8169_private *tp = netdev_priv(dev);
4476
4477 rtl8169_init_ring_indexes(tp);
4478
4479 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004480 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004482 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483}
4484
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004485static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 struct TxDesc *desc)
4487{
4488 unsigned int len = tx_skb->len;
4489
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004490 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4491
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 desc->opts1 = 0x00;
4493 desc->opts2 = 0x00;
4494 desc->addr = 0x00;
4495 tx_skb->len = 0;
4496}
4497
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004498static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4499 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
4501 unsigned int i;
4502
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004503 for (i = 0; i < n; i++) {
4504 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 struct ring_info *tx_skb = tp->tx_skb + entry;
4506 unsigned int len = tx_skb->len;
4507
4508 if (len) {
4509 struct sk_buff *skb = tx_skb->skb;
4510
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004511 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 tp->TxDescArray + entry);
4513 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004514 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 dev_kfree_skb(skb);
4516 tx_skb->skb = NULL;
4517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 }
4519 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004520}
4521
4522static void rtl8169_tx_clear(struct rtl8169_private *tp)
4523{
4524 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 tp->cur_tx = tp->dirty_tx = 0;
4526}
4527
David Howellsc4028952006-11-22 14:57:56 +00004528static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529{
4530 struct rtl8169_private *tp = netdev_priv(dev);
4531
David Howellsc4028952006-11-22 14:57:56 +00004532 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 schedule_delayed_work(&tp->task, 4);
4534}
4535
4536static void rtl8169_wait_for_quiescence(struct net_device *dev)
4537{
4538 struct rtl8169_private *tp = netdev_priv(dev);
4539 void __iomem *ioaddr = tp->mmio_addr;
4540
4541 synchronize_irq(dev->irq);
4542
4543 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004544 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
4546 rtl8169_irq_mask_and_ack(ioaddr);
4547
David S. Millerd1d08d12008-01-07 20:53:33 -08004548 tp->intr_mask = 0xffff;
4549 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004550 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
4552
David Howellsc4028952006-11-22 14:57:56 +00004553static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
David Howellsc4028952006-11-22 14:57:56 +00004555 struct rtl8169_private *tp =
4556 container_of(work, struct rtl8169_private, task.work);
4557 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 int ret;
4559
Francois Romieueb2a0212007-02-15 23:37:21 +01004560 rtnl_lock();
4561
4562 if (!netif_running(dev))
4563 goto out_unlock;
4564
4565 rtl8169_wait_for_quiescence(dev);
4566 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
4568 ret = rtl8169_open(dev);
4569 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004570 if (net_ratelimit())
4571 netif_err(tp, drv, dev,
4572 "reinit failure (status = %d). Rescheduling\n",
4573 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4575 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004576
4577out_unlock:
4578 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579}
4580
David Howellsc4028952006-11-22 14:57:56 +00004581static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
David Howellsc4028952006-11-22 14:57:56 +00004583 struct rtl8169_private *tp =
4584 container_of(work, struct rtl8169_private, task.work);
4585 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Francois Romieueb2a0212007-02-15 23:37:21 +01004587 rtnl_lock();
4588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004590 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
4592 rtl8169_wait_for_quiescence(dev);
4593
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004594 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 rtl8169_tx_clear(tp);
4596
4597 if (tp->dirty_rx == tp->cur_rx) {
4598 rtl8169_init_ring_indexes(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004599 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 netif_wake_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004601 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 } else {
Joe Perchesbf82c182010-02-09 11:49:50 +00004603 if (net_ratelimit())
4604 netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 rtl8169_schedule_work(dev, rtl8169_reset_task);
4606 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004607
4608out_unlock:
4609 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610}
4611
4612static void rtl8169_tx_timeout(struct net_device *dev)
4613{
4614 struct rtl8169_private *tp = netdev_priv(dev);
4615
françois romieue6de30d2011-01-03 15:08:37 +00004616 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 /* Let's wait a bit while any (async) irq lands on */
4619 rtl8169_schedule_work(dev, rtl8169_reset_task);
4620}
4621
4622static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004623 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
4625 struct skb_shared_info *info = skb_shinfo(skb);
4626 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004627 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004628 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
4630 entry = tp->cur_tx;
4631 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4632 skb_frag_t *frag = info->frags + cur_frag;
4633 dma_addr_t mapping;
4634 u32 status, len;
4635 void *addr;
4636
4637 entry = (entry + 1) % NUM_TX_DESC;
4638
4639 txd = tp->TxDescArray + entry;
4640 len = frag->size;
4641 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004642 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004643 if (unlikely(dma_mapping_error(d, mapping))) {
4644 if (net_ratelimit())
4645 netif_err(tp, drv, tp->dev,
4646 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004647 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
4650 /* anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004651 status = opts[0] | len |
4652 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004655 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 txd->addr = cpu_to_le64(mapping);
4657
4658 tp->tx_skb[entry].len = len;
4659 }
4660
4661 if (cur_frag) {
4662 tp->tx_skb[entry].skb = skb;
4663 txd->opts1 |= cpu_to_le32(LastFrag);
4664 }
4665
4666 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004667
4668err_out:
4669 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4670 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671}
4672
Francois Romieu2b7b4312011-04-18 22:53:24 -07004673static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4674 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004676 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004677 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004678 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
Francois Romieu2b7b4312011-04-18 22:53:24 -07004680 if (mss) {
4681 opts[0] |= TD_LSO;
4682 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4683 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004684 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
4686 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004687 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004689 opts[offset] |= info->checksum.udp;
4690 else
4691 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693}
4694
Stephen Hemminger613573252009-08-31 19:50:58 +00004695static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4696 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
4698 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004699 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 struct TxDesc *txd = tp->TxDescArray + entry;
4701 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004702 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 dma_addr_t mapping;
4704 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004705 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004706 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004707
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004709 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004710 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 }
4712
4713 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004714 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004716 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004717 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004718 if (unlikely(dma_mapping_error(d, mapping))) {
4719 if (net_ratelimit())
4720 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004721 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
4724 tp->tx_skb[entry].len = len;
4725 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
Francois Romieu2b7b4312011-04-18 22:53:24 -07004727 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4728 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004729
Francois Romieu2b7b4312011-04-18 22:53:24 -07004730 rtl8169_tso_csum(tp, skb, opts);
4731
4732 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004733 if (frags < 0)
4734 goto err_dma_1;
4735 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004736 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004737 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004738 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004739 tp->tx_skb[entry].skb = skb;
4740 }
4741
Francois Romieu2b7b4312011-04-18 22:53:24 -07004742 txd->opts2 = cpu_to_le32(opts[1]);
4743
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 wmb();
4745
4746 /* anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004747 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 txd->opts1 = cpu_to_le32(status);
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 tp->cur_tx += frags + 1;
4751
David Dillow4c020a92010-03-03 16:33:10 +00004752 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Francois Romieu275391a2007-02-23 23:50:28 +01004754 RTL_W8(TxPoll, NPQ); /* set polling bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
4756 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4757 netif_stop_queue(dev);
4758 smp_rmb();
4759 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4760 netif_wake_queue(dev);
4761 }
4762
Stephen Hemminger613573252009-08-31 19:50:58 +00004763 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004765err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004766 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004767err_dma_0:
4768 dev_kfree_skb(skb);
4769 dev->stats.tx_dropped++;
4770 return NETDEV_TX_OK;
4771
4772err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004774 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004775 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776}
4777
4778static void rtl8169_pcierr_interrupt(struct net_device *dev)
4779{
4780 struct rtl8169_private *tp = netdev_priv(dev);
4781 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 u16 pci_status, pci_cmd;
4783
4784 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4785 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4786
Joe Perchesbf82c182010-02-09 11:49:50 +00004787 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4788 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
4790 /*
4791 * The recovery sequence below admits a very elaborated explanation:
4792 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004793 * - I did not see what else could be done;
4794 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 *
4796 * Feel free to adjust to your needs.
4797 */
Francois Romieua27993f2006-12-18 00:04:19 +01004798 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004799 pci_cmd &= ~PCI_COMMAND_PARITY;
4800 else
4801 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4802
4803 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
4805 pci_write_config_word(pdev, PCI_STATUS,
4806 pci_status & (PCI_STATUS_DETECTED_PARITY |
4807 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4808 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4809
4810 /* The infamous DAC f*ckup only happens at boot time */
4811 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004812 void __iomem *ioaddr = tp->mmio_addr;
4813
Joe Perchesbf82c182010-02-09 11:49:50 +00004814 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 tp->cp_cmd &= ~PCIDAC;
4816 RTL_W16(CPlusCmd, tp->cp_cmd);
4817 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 }
4819
françois romieue6de30d2011-01-03 15:08:37 +00004820 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004821
4822 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
Francois Romieu07d3f512007-02-21 22:40:46 +01004825static void rtl8169_tx_interrupt(struct net_device *dev,
4826 struct rtl8169_private *tp,
4827 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
4829 unsigned int dirty_tx, tx_left;
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 dirty_tx = tp->dirty_tx;
4832 smp_rmb();
4833 tx_left = tp->cur_tx - dirty_tx;
4834
4835 while (tx_left > 0) {
4836 unsigned int entry = dirty_tx % NUM_TX_DESC;
4837 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 u32 status;
4839
4840 rmb();
4841 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4842 if (status & DescOwn)
4843 break;
4844
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004845 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4846 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004848 dev->stats.tx_packets++;
4849 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004850 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 tx_skb->skb = NULL;
4852 }
4853 dirty_tx++;
4854 tx_left--;
4855 }
4856
4857 if (tp->dirty_tx != dirty_tx) {
4858 tp->dirty_tx = dirty_tx;
4859 smp_wmb();
4860 if (netif_queue_stopped(dev) &&
4861 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4862 netif_wake_queue(dev);
4863 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004864 /*
4865 * 8168 hack: TxPoll requests are lost when the Tx packets are
4866 * too close. Let's kick an extra TxPoll request when a burst
4867 * of start_xmit activity is detected (if it is not detected,
4868 * it is slow enough). -- FR
4869 */
4870 smp_rmb();
4871 if (tp->cur_tx != dirty_tx)
4872 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 }
4874}
4875
Francois Romieu126fa4b2005-05-12 20:09:17 -04004876static inline int rtl8169_fragmented_frame(u32 status)
4877{
4878 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4879}
4880
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004881static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 u32 status = opts1 & RxProtoMask;
4884
4885 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004886 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 skb->ip_summed = CHECKSUM_UNNECESSARY;
4888 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004889 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890}
4891
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004892static struct sk_buff *rtl8169_try_rx_copy(void *data,
4893 struct rtl8169_private *tp,
4894 int pkt_size,
4895 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004897 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004898 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004900 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004901 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004902 prefetch(data);
4903 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4904 if (skb)
4905 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004906 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4907
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004908 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909}
4910
Eric Dumazet630b9432010-03-31 02:08:31 +00004911/*
4912 * Warning : rtl8169_rx_interrupt() might be called :
4913 * 1) from NAPI (softirq) context
4914 * (polling = 1 : we should call netif_receive_skb())
4915 * 2) from process context (rtl8169_reset_task())
4916 * (polling = 0 : we must call netif_rx() instead)
4917 */
Francois Romieu07d3f512007-02-21 22:40:46 +01004918static int rtl8169_rx_interrupt(struct net_device *dev,
4919 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004920 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
4922 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004923 unsigned int count;
Eric Dumazet630b9432010-03-31 02:08:31 +00004924 int polling = (budget != ~(u32)0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 cur_rx = tp->cur_rx;
4927 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004928 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004930 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004932 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 u32 status;
4934
4935 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004936 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
4938 if (status & DescOwn)
4939 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004940 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004941 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4942 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004943 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004945 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004947 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004948 if (status & RxFOVF) {
4949 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004950 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004951 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004952 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004954 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004955 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Francois Romieu126fa4b2005-05-12 20:09:17 -04004958 /*
4959 * The driver does not support incoming fragmented
4960 * frames. They are seen as a symptom of over-mtu
4961 * sized frames.
4962 */
4963 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004964 dev->stats.rx_dropped++;
4965 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004966 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004967 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004968 }
4969
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004970 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4971 tp, pkt_size, addr);
4972 rtl8169_mark_to_asic(desc, rx_buf_sz);
4973 if (!skb) {
4974 dev->stats.rx_dropped++;
4975 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 }
4977
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004978 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 skb_put(skb, pkt_size);
4980 skb->protocol = eth_type_trans(skb, dev);
4981
Francois Romieu7a8fc772011-03-01 17:18:33 +01004982 rtl8169_rx_vlan_tag(desc, skb);
4983
4984 if (likely(polling))
4985 napi_gro_receive(&tp->napi, skb);
4986 else
4987 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
Francois Romieucebf8cc2007-10-18 12:06:54 +02004989 dev->stats.rx_bytes += pkt_size;
4990 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 }
Francois Romieu6dccd162007-02-13 23:38:05 +01004992
4993 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00004994 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01004995 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4996 desc->opts2 = 0;
4997 cur_rx++;
4998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 }
5000
5001 count = cur_rx - tp->cur_rx;
5002 tp->cur_rx = cur_rx;
5003
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005004 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
5006 return count;
5007}
5008
Francois Romieu07d3f512007-02-21 22:40:46 +01005009static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010{
Francois Romieu07d3f512007-02-21 22:40:46 +01005011 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005015 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
David Dillowf11a3772009-05-22 15:29:34 +00005017 /* loop handling interrupts until we have no new ones or
5018 * we hit a invalid/hotplug case.
5019 */
Francois Romieu865c6522008-05-11 14:51:00 +02005020 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005021 while (status && status != 0xffff) {
5022 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
David Dillowf11a3772009-05-22 15:29:34 +00005024 /* Handle all of the error cases first. These will reset
5025 * the chip, so just exit the loop.
5026 */
5027 if (unlikely(!netif_running(dev))) {
5028 rtl8169_asic_down(ioaddr);
5029 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 }
David Dillowf11a3772009-05-22 15:29:34 +00005031
Francois Romieu1519e572011-02-03 12:02:36 +01005032 if (unlikely(status & RxFIFOOver)) {
5033 switch (tp->mac_version) {
5034 /* Work around for rx fifo overflow */
5035 case RTL_GIGA_MAC_VER_11:
5036 case RTL_GIGA_MAC_VER_22:
5037 case RTL_GIGA_MAC_VER_26:
5038 netif_stop_queue(dev);
5039 rtl8169_tx_timeout(dev);
5040 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005041 /* Testers needed. */
5042 case RTL_GIGA_MAC_VER_17:
5043 case RTL_GIGA_MAC_VER_19:
5044 case RTL_GIGA_MAC_VER_20:
5045 case RTL_GIGA_MAC_VER_21:
5046 case RTL_GIGA_MAC_VER_23:
5047 case RTL_GIGA_MAC_VER_24:
5048 case RTL_GIGA_MAC_VER_27:
5049 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005050 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005051 /* Experimental science. Pktgen proof. */
5052 case RTL_GIGA_MAC_VER_12:
5053 case RTL_GIGA_MAC_VER_25:
5054 if (status == RxFIFOOver)
5055 goto done;
5056 break;
5057 default:
5058 break;
5059 }
David Dillowf11a3772009-05-22 15:29:34 +00005060 }
5061
5062 if (unlikely(status & SYSErr)) {
5063 rtl8169_pcierr_interrupt(dev);
5064 break;
5065 }
5066
5067 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005068 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005069
5070 /* We need to see the lastest version of tp->intr_mask to
5071 * avoid ignoring an MSI interrupt and having to wait for
5072 * another event which may never come.
5073 */
5074 smp_rmb();
5075 if (status & tp->intr_mask & tp->napi_event) {
5076 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5077 tp->intr_mask = ~tp->napi_event;
5078
5079 if (likely(napi_schedule_prep(&tp->napi)))
5080 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005081 else
5082 netif_info(tp, intr, dev,
5083 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005084 }
5085
5086 /* We only get a new MSI interrupt when all active irq
5087 * sources on the chip have been acknowledged. So, ack
5088 * everything we've seen and check if new sources have become
5089 * active to avoid blocking all interrupts from the chip.
5090 */
5091 RTL_W16(IntrStatus,
5092 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5093 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 }
Francois Romieu1519e572011-02-03 12:02:36 +01005095done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 return IRQ_RETVAL(handled);
5097}
5098
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005099static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005101 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5102 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005104 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005106 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 rtl8169_tx_interrupt(dev, tp, ioaddr);
5108
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005109 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005110 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005111
5112 /* We need for force the visibility of tp->intr_mask
5113 * for other CPUs, as we can loose an MSI interrupt
5114 * and potentially wait for a retransmit timeout if we don't.
5115 * The posted write to IntrMask is safe, as it will
5116 * eventually make it to the chip and we won't loose anything
5117 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 */
David Dillowf11a3772009-05-22 15:29:34 +00005119 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005120 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005121 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 }
5123
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005124 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
Francois Romieu523a6092008-09-10 22:28:56 +02005127static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5128{
5129 struct rtl8169_private *tp = netdev_priv(dev);
5130
5131 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5132 return;
5133
5134 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5135 RTL_W32(RxMissed, 0);
5136}
5137
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138static void rtl8169_down(struct net_device *dev)
5139{
5140 struct rtl8169_private *tp = netdev_priv(dev);
5141 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
5143 rtl8169_delete_timer(dev);
5144
5145 netif_stop_queue(dev);
5146
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005147 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005148
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 spin_lock_irq(&tp->lock);
5150
5151 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005152 /*
5153 * At this point device interrupts can not be enabled in any function,
5154 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5155 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5156 */
Francois Romieu523a6092008-09-10 22:28:56 +02005157 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
5159 spin_unlock_irq(&tp->lock);
5160
5161 synchronize_irq(dev->irq);
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005164 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 rtl8169_tx_clear(tp);
5167
5168 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005169
5170 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171}
5172
5173static int rtl8169_close(struct net_device *dev)
5174{
5175 struct rtl8169_private *tp = netdev_priv(dev);
5176 struct pci_dev *pdev = tp->pci_dev;
5177
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005178 pm_runtime_get_sync(&pdev->dev);
5179
Ivan Vecera355423d2009-02-06 21:49:57 -08005180 /* update counters before going down */
5181 rtl8169_update_counters(dev);
5182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 rtl8169_down(dev);
5184
5185 free_irq(dev->irq, dev);
5186
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005187 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5188 tp->RxPhyAddr);
5189 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5190 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 tp->TxDescArray = NULL;
5192 tp->RxDescArray = NULL;
5193
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005194 pm_runtime_put_sync(&pdev->dev);
5195
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 return 0;
5197}
5198
Francois Romieu07ce4062007-02-23 23:36:39 +01005199static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200{
5201 struct rtl8169_private *tp = netdev_priv(dev);
5202 void __iomem *ioaddr = tp->mmio_addr;
5203 unsigned long flags;
5204 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005205 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 u32 tmp = 0;
5207
5208 if (dev->flags & IFF_PROMISC) {
5209 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005210 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 rx_mode =
5212 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5213 AcceptAllPhys;
5214 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005215 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005216 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 /* Too many to filter perfectly -- accept all multicasts. */
5218 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5219 mc_filter[1] = mc_filter[0] = 0xffffffff;
5220 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005221 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 rx_mode = AcceptBroadcast | AcceptMyPhys;
5224 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005225 netdev_for_each_mc_addr(ha, dev) {
5226 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5228 rx_mode |= AcceptMulticast;
5229 }
5230 }
5231
5232 spin_lock_irqsave(&tp->lock, flags);
5233
5234 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005235 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
Francois Romieuf887cce2008-07-17 22:24:18 +02005237 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005238 u32 data = mc_filter[0];
5239
5240 mc_filter[0] = swab32(mc_filter[1]);
5241 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005242 }
5243
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005245 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
Francois Romieu57a9f232007-06-04 22:10:15 +02005247 RTL_W32(RxConfig, tmp);
5248
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 spin_unlock_irqrestore(&tp->lock, flags);
5250}
5251
5252/**
5253 * rtl8169_get_stats - Get rtl8169 read/write statistics
5254 * @dev: The Ethernet Device to get statistics for
5255 *
5256 * Get TX/RX statistics for rtl8169
5257 */
5258static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5259{
5260 struct rtl8169_private *tp = netdev_priv(dev);
5261 void __iomem *ioaddr = tp->mmio_addr;
5262 unsigned long flags;
5263
5264 if (netif_running(dev)) {
5265 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005266 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 spin_unlock_irqrestore(&tp->lock, flags);
5268 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005269
Francois Romieucebf8cc2007-10-18 12:06:54 +02005270 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271}
5272
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005273static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005274{
françois romieu065c27c2011-01-03 15:08:12 +00005275 struct rtl8169_private *tp = netdev_priv(dev);
5276
Francois Romieu5d06a992006-02-23 00:47:58 +01005277 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005278 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005279
françois romieu065c27c2011-01-03 15:08:12 +00005280 rtl_pll_power_down(tp);
5281
Francois Romieu5d06a992006-02-23 00:47:58 +01005282 netif_device_detach(dev);
5283 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005284}
Francois Romieu5d06a992006-02-23 00:47:58 +01005285
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005286#ifdef CONFIG_PM
5287
5288static int rtl8169_suspend(struct device *device)
5289{
5290 struct pci_dev *pdev = to_pci_dev(device);
5291 struct net_device *dev = pci_get_drvdata(pdev);
5292
5293 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005294
Francois Romieu5d06a992006-02-23 00:47:58 +01005295 return 0;
5296}
5297
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005298static void __rtl8169_resume(struct net_device *dev)
5299{
françois romieu065c27c2011-01-03 15:08:12 +00005300 struct rtl8169_private *tp = netdev_priv(dev);
5301
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005302 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005303
5304 rtl_pll_power_up(tp);
5305
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005306 rtl8169_schedule_work(dev, rtl8169_reset_task);
5307}
5308
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005309static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005310{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005311 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005312 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005313 struct rtl8169_private *tp = netdev_priv(dev);
5314
5315 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005316
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005317 if (netif_running(dev))
5318 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005319
Francois Romieu5d06a992006-02-23 00:47:58 +01005320 return 0;
5321}
5322
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005323static int rtl8169_runtime_suspend(struct device *device)
5324{
5325 struct pci_dev *pdev = to_pci_dev(device);
5326 struct net_device *dev = pci_get_drvdata(pdev);
5327 struct rtl8169_private *tp = netdev_priv(dev);
5328
5329 if (!tp->TxDescArray)
5330 return 0;
5331
5332 spin_lock_irq(&tp->lock);
5333 tp->saved_wolopts = __rtl8169_get_wol(tp);
5334 __rtl8169_set_wol(tp, WAKE_ANY);
5335 spin_unlock_irq(&tp->lock);
5336
5337 rtl8169_net_suspend(dev);
5338
5339 return 0;
5340}
5341
5342static int rtl8169_runtime_resume(struct device *device)
5343{
5344 struct pci_dev *pdev = to_pci_dev(device);
5345 struct net_device *dev = pci_get_drvdata(pdev);
5346 struct rtl8169_private *tp = netdev_priv(dev);
5347
5348 if (!tp->TxDescArray)
5349 return 0;
5350
5351 spin_lock_irq(&tp->lock);
5352 __rtl8169_set_wol(tp, tp->saved_wolopts);
5353 tp->saved_wolopts = 0;
5354 spin_unlock_irq(&tp->lock);
5355
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005356 rtl8169_init_phy(dev, tp);
5357
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005358 __rtl8169_resume(dev);
5359
5360 return 0;
5361}
5362
5363static int rtl8169_runtime_idle(struct device *device)
5364{
5365 struct pci_dev *pdev = to_pci_dev(device);
5366 struct net_device *dev = pci_get_drvdata(pdev);
5367 struct rtl8169_private *tp = netdev_priv(dev);
5368
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005369 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005370}
5371
Alexey Dobriyan47145212009-12-14 18:00:08 -08005372static const struct dev_pm_ops rtl8169_pm_ops = {
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005373 .suspend = rtl8169_suspend,
5374 .resume = rtl8169_resume,
5375 .freeze = rtl8169_suspend,
5376 .thaw = rtl8169_resume,
5377 .poweroff = rtl8169_suspend,
5378 .restore = rtl8169_resume,
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005379 .runtime_suspend = rtl8169_runtime_suspend,
5380 .runtime_resume = rtl8169_runtime_resume,
5381 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005382};
5383
5384#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5385
5386#else /* !CONFIG_PM */
5387
5388#define RTL8169_PM_OPS NULL
5389
5390#endif /* !CONFIG_PM */
5391
Francois Romieu1765f952008-09-13 17:21:40 +02005392static void rtl_shutdown(struct pci_dev *pdev)
5393{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005394 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005395 struct rtl8169_private *tp = netdev_priv(dev);
5396 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005397
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005398 rtl8169_net_suspend(dev);
5399
Ivan Veceracc098dc2009-11-29 23:12:52 -08005400 /* restore original MAC address */
5401 rtl_rar_set(tp, dev->perm_addr);
5402
françois romieu4bb3f522009-06-17 11:41:45 +00005403 spin_lock_irq(&tp->lock);
5404
5405 rtl8169_asic_down(ioaddr);
5406
5407 spin_unlock_irq(&tp->lock);
5408
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005409 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005410 /* WoL fails with some 8168 when the receiver is disabled. */
5411 if (tp->features & RTL_FEATURE_WOL) {
5412 pci_clear_master(pdev);
5413
5414 RTL_W8(ChipCmd, CmdRxEnb);
5415 /* PCI commit */
5416 RTL_R8(ChipCmd);
5417 }
5418
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005419 pci_wake_from_d3(pdev, true);
5420 pci_set_power_state(pdev, PCI_D3hot);
5421 }
5422}
Francois Romieu5d06a992006-02-23 00:47:58 +01005423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424static struct pci_driver rtl8169_pci_driver = {
5425 .name = MODULENAME,
5426 .id_table = rtl8169_pci_tbl,
5427 .probe = rtl8169_init_one,
5428 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005429 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005430 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431};
5432
Francois Romieu07d3f512007-02-21 22:40:46 +01005433static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434{
Jeff Garzik29917622006-08-19 17:48:59 -04005435 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436}
5437
Francois Romieu07d3f512007-02-21 22:40:46 +01005438static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439{
5440 pci_unregister_driver(&rtl8169_pci_driver);
5441}
5442
5443module_init(rtl8169_init_module);
5444module_exit(rtl8169_cleanup_module);