blob: 397c36810a159ad7dfc143a819303e059008fe56 [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"
Hayes Wang5a5e4442011-02-22 17:26:21 +080040#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#ifdef RTL8169_DEBUG
43#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020044 if (!(expr)) { \
45 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070046 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020047 }
Joe Perches06fa7352007-10-18 21:15:00 +020048#define dprintk(fmt, args...) \
49 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#else
51#define assert(expr) do {} while (0)
52#define dprintk(fmt, args...) do {} while (0)
53#endif /* RTL8169_DEBUG */
54
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020055#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070056 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define TX_BUFFS_AVAIL(tp) \
59 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
62 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050063static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* MAC address length */
66#define MAC_ADDR_LEN 6
67
Francois Romieu9c14cea2008-07-05 00:21:15 +020068#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
70#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
71#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
73#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
74
75#define R8169_REGS_SIZE 256
76#define R8169_NAPI_WEIGHT 64
77#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
78#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
79#define RX_BUF_SIZE 1536 /* Rx Buffer size */
80#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
81#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
82
83#define RTL8169_TX_TIMEOUT (6*HZ)
84#define RTL8169_PHY_TIMEOUT (10*HZ)
85
françois romieuea8dbdd2009-03-15 01:10:50 +000086#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
87#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020088#define RTL_EEPROM_SIG_ADDR 0x0000
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* write/read MMIO register */
91#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
92#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
93#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
94#define RTL_R8(reg) readb (ioaddr + (reg))
95#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000096#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98enum mac_version {
Jean Delvaref21b75e2009-05-26 20:54:48 -070099 RTL_GIGA_MAC_NONE = 0x00,
Francois Romieuba6eb6e2007-06-11 23:35:18 +0200100 RTL_GIGA_MAC_VER_01 = 0x01, // 8169
101 RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
102 RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
103 RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
104 RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +0100105 RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
Francois Romieu2857ffb2008-08-02 21:08:49 +0200106 RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
107 RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
108 RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
109 RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
Francois Romieu2dd99532007-06-11 23:22:52 +0200110 RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200111 RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
112 RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
113 RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
114 RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
115 RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
116 RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
117 RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
118 RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
Francois Romieu197ff762008-06-28 13:16:02 +0200119 RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
Francois Romieu6fb07052008-06-29 11:54:28 +0200120 RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
Francois Romieuef3386f2008-06-29 12:24:30 +0200121 RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
Francois Romieu7f3e3d32008-07-20 18:53:20 +0200122 RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
Francois Romieu5b538df2008-07-20 16:22:45 +0200123 RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
françois romieudaf9df62009-10-07 12:44:20 +0000124 RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
125 RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
françois romieue6de30d2011-01-03 15:08:37 +0000126 RTL_GIGA_MAC_VER_27 = 0x1b, // 8168DP
127 RTL_GIGA_MAC_VER_28 = 0x1c, // 8168DP
Hayes Wang5a5e4442011-02-22 17:26:21 +0800128 RTL_GIGA_MAC_VER_29 = 0x1d, // 8105E
129 RTL_GIGA_MAC_VER_30 = 0x1e, // 8105E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#define _R(NAME,MAC,MASK) \
133 { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
134
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800135static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 const char *name;
137 u8 mac_version;
138 u32 RxConfigMask; /* Clears the bits supported by this chip */
139} rtl_chip_info[] = {
Francois Romieuba6eb6e2007-06-11 23:35:18 +0200140 _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
141 _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
142 _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
143 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
144 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +0100145 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
Francois Romieu2857ffb2008-08-02 21:08:49 +0200146 _R("RTL8102e", RTL_GIGA_MAC_VER_07, 0xff7e1880), // PCI-E
147 _R("RTL8102e", RTL_GIGA_MAC_VER_08, 0xff7e1880), // PCI-E
148 _R("RTL8102e", RTL_GIGA_MAC_VER_09, 0xff7e1880), // PCI-E
149 _R("RTL8101e", RTL_GIGA_MAC_VER_10, 0xff7e1880), // PCI-E
Francois Romieubcf0bf92006-07-26 23:14:13 +0200150 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
151 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
152 _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
153 _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200154 _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880), // PCI-E 8139
155 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880), // PCI-E
156 _R("RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880), // PCI-E
157 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880), // PCI-E
158 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880), // PCI-E
Francois Romieu197ff762008-06-28 13:16:02 +0200159 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880), // PCI-E
Francois Romieu6fb07052008-06-29 11:54:28 +0200160 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, 0xff7e1880), // PCI-E
Francois Romieuef3386f2008-06-29 12:24:30 +0200161 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, 0xff7e1880), // PCI-E
Francois Romieu7f3e3d32008-07-20 18:53:20 +0200162 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, 0xff7e1880), // PCI-E
Francois Romieu5b538df2008-07-20 16:22:45 +0200163 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, 0xff7e1880), // PCI-E
françois romieudaf9df62009-10-07 12:44:20 +0000164 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, 0xff7e1880), // PCI-E
165 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, 0xff7e1880), // PCI-E
françois romieue6de30d2011-01-03 15:08:37 +0000166 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, 0xff7e1880), // PCI-E
Hayes Wang5a5e4442011-02-22 17:26:21 +0800167 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28, 0xff7e1880), // PCI-E
168 _R("RTL8105e", RTL_GIGA_MAC_VER_29, 0xff7e1880), // PCI-E
169 _R("RTL8105e", RTL_GIGA_MAC_VER_30, 0xff7e1880) // PCI-E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170};
171#undef _R
172
François Romieu953a12c2011-04-24 17:38:48 +0200173static const struct rtl_firmware_info {
174 int mac_version;
175 const char *fw_name;
176} rtl_firmware_infos[] = {
177 { .mac_version = RTL_GIGA_MAC_VER_25, .fw_name = FIRMWARE_8168D_1 },
178 { .mac_version = RTL_GIGA_MAC_VER_26, .fw_name = FIRMWARE_8168D_2 },
179 { .mac_version = RTL_GIGA_MAC_VER_29, .fw_name = FIRMWARE_8105E_1 },
180 { .mac_version = RTL_GIGA_MAC_VER_30, .fw_name = FIRMWARE_8105E_1 }
181};
182
Francois Romieubcf0bf92006-07-26 23:14:13 +0200183enum cfg_version {
184 RTL_CFG_0 = 0x00,
185 RTL_CFG_1,
186 RTL_CFG_2
187};
188
Francois Romieu07ce4062007-02-23 23:36:39 +0100189static void rtl_hw_start_8169(struct net_device *);
190static void rtl_hw_start_8168(struct net_device *);
191static void rtl_hw_start_8101(struct net_device *);
192
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000193static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200194 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200195 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200196 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100197 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200198 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
199 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200200 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200201 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
202 { PCI_VENDOR_ID_LINKSYS, 0x1032,
203 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100204 { 0x0001, 0x8168,
205 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 {0,},
207};
208
209MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
210
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000211static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700212static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200213static struct {
214 u32 msg_enable;
215} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Francois Romieu07d3f512007-02-21 22:40:46 +0100217enum rtl_registers {
218 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100219 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100220 MAR0 = 8, /* Multicast filter. */
221 CounterAddrLow = 0x10,
222 CounterAddrHigh = 0x14,
223 TxDescStartAddrLow = 0x20,
224 TxDescStartAddrHigh = 0x24,
225 TxHDescStartAddrLow = 0x28,
226 TxHDescStartAddrHigh = 0x2c,
227 FLASH = 0x30,
228 ERSR = 0x36,
229 ChipCmd = 0x37,
230 TxPoll = 0x38,
231 IntrMask = 0x3c,
232 IntrStatus = 0x3e,
233 TxConfig = 0x40,
234 RxConfig = 0x44,
235 RxMissed = 0x4c,
236 Cfg9346 = 0x50,
237 Config0 = 0x51,
238 Config1 = 0x52,
239 Config2 = 0x53,
240 Config3 = 0x54,
241 Config4 = 0x55,
242 Config5 = 0x56,
243 MultiIntr = 0x5c,
244 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100245 PHYstatus = 0x6c,
246 RxMaxSize = 0xda,
247 CPlusCmd = 0xe0,
248 IntrMitigate = 0xe2,
249 RxDescAddrLow = 0xe4,
250 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000251 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
252
253#define NoEarlyTx 0x3f /* Max value : no early transmit. */
254
255 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
256
257#define TxPacketMax (8064 >> 7)
258
Francois Romieu07d3f512007-02-21 22:40:46 +0100259 FuncEvent = 0xf0,
260 FuncEventMask = 0xf4,
261 FuncPresetState = 0xf8,
262 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
264
Francois Romieuf162a5d2008-06-01 22:37:49 +0200265enum rtl8110_registers {
266 TBICSR = 0x64,
267 TBI_ANAR = 0x68,
268 TBI_LPAR = 0x6a,
269};
270
271enum rtl8168_8101_registers {
272 CSIDR = 0x64,
273 CSIAR = 0x68,
274#define CSIAR_FLAG 0x80000000
275#define CSIAR_WRITE_CMD 0x80000000
276#define CSIAR_BYTE_ENABLE 0x0f
277#define CSIAR_BYTE_ENABLE_SHIFT 12
278#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000279 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200280 EPHYAR = 0x80,
281#define EPHYAR_FLAG 0x80000000
282#define EPHYAR_WRITE_CMD 0x80000000
283#define EPHYAR_REG_MASK 0x1f
284#define EPHYAR_REG_SHIFT 16
285#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800286 DLLPR = 0xd0,
287#define PM_SWITCH (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200288 DBG_REG = 0xd1,
289#define FIX_NAK_1 (1 << 4)
290#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800291 TWSI = 0xd2,
292 MCU = 0xd3,
293#define EN_NDP (1 << 3)
294#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000295 EFUSEAR = 0xdc,
296#define EFUSEAR_FLAG 0x80000000
297#define EFUSEAR_WRITE_CMD 0x80000000
298#define EFUSEAR_READ_CMD 0x00000000
299#define EFUSEAR_REG_MASK 0x03ff
300#define EFUSEAR_REG_SHIFT 8
301#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200302};
303
françois romieuc0e45c12011-01-03 15:08:04 +0000304enum rtl8168_registers {
françois romieub646d902011-01-03 15:08:21 +0000305 ERIDR = 0x70,
306 ERIAR = 0x74,
307#define ERIAR_FLAG 0x80000000
308#define ERIAR_WRITE_CMD 0x80000000
309#define ERIAR_READ_CMD 0x00000000
310#define ERIAR_ADDR_BYTE_ALIGN 4
311#define ERIAR_EXGMAC 0
312#define ERIAR_MSIX 1
313#define ERIAR_ASF 2
314#define ERIAR_TYPE_SHIFT 16
315#define ERIAR_BYTEEN 0x0f
316#define ERIAR_BYTEEN_SHIFT 12
françois romieuc0e45c12011-01-03 15:08:04 +0000317 EPHY_RXER_NUM = 0x7c,
318 OCPDR = 0xb0, /* OCP GPHY access */
319#define OCPDR_WRITE_CMD 0x80000000
320#define OCPDR_READ_CMD 0x00000000
321#define OCPDR_REG_MASK 0x7f
322#define OCPDR_GPHY_REG_SHIFT 16
323#define OCPDR_DATA_MASK 0xffff
324 OCPAR = 0xb4,
325#define OCPAR_FLAG 0x80000000
326#define OCPAR_GPHY_WRITE_CMD 0x8000f060
327#define OCPAR_GPHY_READ_CMD 0x0000f060
françois romieue6de30d2011-01-03 15:08:37 +0000328 RDSAR1 = 0xd0 /* 8168c only. Undocumented on 8168dp */
françois romieuc0e45c12011-01-03 15:08:04 +0000329};
330
Francois Romieu07d3f512007-02-21 22:40:46 +0100331enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100333 SYSErr = 0x8000,
334 PCSTimeout = 0x4000,
335 SWInt = 0x0100,
336 TxDescUnavail = 0x0080,
337 RxFIFOOver = 0x0040,
338 LinkChg = 0x0020,
339 RxOverflow = 0x0010,
340 TxErr = 0x0008,
341 TxOK = 0x0004,
342 RxErr = 0x0002,
343 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200346 RxFOVF = (1 << 23),
347 RxRWT = (1 << 22),
348 RxRES = (1 << 21),
349 RxRUNT = (1 << 20),
350 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100353 CmdReset = 0x10,
354 CmdRxEnb = 0x08,
355 CmdTxEnb = 0x04,
356 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Francois Romieu275391a2007-02-23 23:50:28 +0100358 /* TXPoll register p.5 */
359 HPQ = 0x80, /* Poll cmd on the high prio queue */
360 NPQ = 0x40, /* Poll cmd on the low prio queue */
361 FSWInt = 0x01, /* Forced software interrupt */
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100364 Cfg9346_Lock = 0x00,
365 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100368 AcceptErr = 0x20,
369 AcceptRunt = 0x10,
370 AcceptBroadcast = 0x08,
371 AcceptMulticast = 0x04,
372 AcceptMyPhys = 0x02,
373 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100376 RxCfgFIFOShift = 13,
377 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* TxConfigBits */
380 TxInterFrameGapShift = 24,
381 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
382
Francois Romieu5d06a992006-02-23 00:47:58 +0100383 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200384 LEDS1 = (1 << 7),
385 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200386 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200387 Speed_down = (1 << 4),
388 MEMMAP = (1 << 3),
389 IOMAP = (1 << 2),
390 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100391 PMEnable = (1 << 0), /* Power Management Enable */
392
Francois Romieu6dccd162007-02-13 23:38:05 +0100393 /* Config2 register p. 25 */
394 PCI_Clock_66MHz = 0x01,
395 PCI_Clock_33MHz = 0x00,
396
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100397 /* Config3 register p.25 */
398 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
399 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200400 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100401
Francois Romieu5d06a992006-02-23 00:47:58 +0100402 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100403 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
404 MWF = (1 << 5), /* Accept Multicast wakeup frame */
405 UWF = (1 << 4), /* Accept Unicast wakeup frame */
406 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100407 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* TBICSR p.28 */
410 TBIReset = 0x80000000,
411 TBILoopback = 0x40000000,
412 TBINwEnable = 0x20000000,
413 TBINwRestart = 0x10000000,
414 TBILinkOk = 0x02000000,
415 TBINwComplete = 0x01000000,
416
417 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200418 EnableBist = (1 << 15), // 8168 8101
419 Mac_dbgo_oe = (1 << 14), // 8168 8101
420 Normal_mode = (1 << 13), // unused
421 Force_half_dup = (1 << 12), // 8168 8101
422 Force_rxflow_en = (1 << 11), // 8168 8101
423 Force_txflow_en = (1 << 10), // 8168 8101
424 Cxpl_dbg_sel = (1 << 9), // 8168 8101
425 ASF = (1 << 8), // 8168 8101
426 PktCntrDisable = (1 << 7), // 8168 8101
427 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 RxVlan = (1 << 6),
429 RxChkSum = (1 << 5),
430 PCIDAC = (1 << 4),
431 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100432 INTT_0 = 0x0000, // 8168
433 INTT_1 = 0x0001, // 8168
434 INTT_2 = 0x0002, // 8168
435 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100438 TBI_Enable = 0x80,
439 TxFlowCtrl = 0x40,
440 RxFlowCtrl = 0x20,
441 _1000bpsF = 0x10,
442 _100bps = 0x08,
443 _10bps = 0x04,
444 LinkStatus = 0x02,
445 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100448 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200449
450 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100451 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452};
453
Francois Romieu07d3f512007-02-21 22:40:46 +0100454enum desc_status_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
456 RingEnd = (1 << 30), /* End of descriptor ring */
457 FirstFrag = (1 << 29), /* First segment of a packet */
458 LastFrag = (1 << 28), /* Final segment of a packet */
459
460 /* Tx private */
461 LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
462 MSSShift = 16, /* MSS value position */
463 MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
464 IPCS = (1 << 18), /* Calculate IP checksum */
465 UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
466 TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
467 TxVlanTag = (1 << 17), /* Add VLAN tag */
468
469 /* Rx private */
470 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
471 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
472
473#define RxProtoUDP (PID1)
474#define RxProtoTCP (PID0)
475#define RxProtoIP (PID1 | PID0)
476#define RxProtoMask RxProtoIP
477
478 IPFail = (1 << 16), /* IP checksum failed */
479 UDPFail = (1 << 15), /* UDP/IP checksum failed */
480 TCPFail = (1 << 14), /* TCP/IP checksum failed */
481 RxVlanTag = (1 << 16), /* VLAN tag available */
482};
483
484#define RsvdMask 0x3fffc000
485
486struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200487 __le32 opts1;
488 __le32 opts2;
489 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490};
491
492struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200493 __le32 opts1;
494 __le32 opts2;
495 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496};
497
498struct ring_info {
499 struct sk_buff *skb;
500 u32 len;
501 u8 __pad[sizeof(void *) - sizeof(u32)];
502};
503
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200504enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200505 RTL_FEATURE_WOL = (1 << 0),
506 RTL_FEATURE_MSI = (1 << 1),
507 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200508};
509
Ivan Vecera355423d2009-02-06 21:49:57 -0800510struct rtl8169_counters {
511 __le64 tx_packets;
512 __le64 rx_packets;
513 __le64 tx_errors;
514 __le32 rx_errors;
515 __le16 rx_missed;
516 __le16 align_errors;
517 __le32 tx_one_collision;
518 __le32 tx_multi_collision;
519 __le64 rx_unicast;
520 __le64 rx_broadcast;
521 __le32 rx_multicast;
522 __le16 tx_aborted;
523 __le16 tx_underun;
524};
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526struct rtl8169_private {
527 void __iomem *mmio_addr; /* memory map physical address */
528 struct pci_dev *pci_dev; /* Index of PCI device */
David Howellsc4028952006-11-22 14:57:56 +0000529 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700530 struct napi_struct napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 spinlock_t lock; /* spin lock flag */
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200532 u32 msg_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int chipset;
534 int mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
536 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
537 u32 dirty_rx;
538 u32 dirty_tx;
539 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
540 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
541 dma_addr_t TxPhyAddr;
542 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000543 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct timer_list timer;
546 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100547 u16 intr_event;
548 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 u16 intr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int phy_1000_ctrl_reg;
françois romieuc0e45c12011-01-03 15:08:04 +0000551
552 struct mdio_ops {
553 void (*write)(void __iomem *, int, int);
554 int (*read)(void __iomem *, int);
555 } mdio_ops;
556
françois romieu065c27c2011-01-03 15:08:12 +0000557 struct pll_power_ops {
558 void (*down)(struct rtl8169_private *);
559 void (*up)(struct rtl8169_private *);
560 } pll_power_ops;
561
Oliver Neukum54405cd2011-01-06 21:55:13 +0100562 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200563 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000564 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100565 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000566 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800568 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200569 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000570 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200571 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200572
573 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800574 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000575 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000576
577 const struct firmware *fw;
François Romieu953a12c2011-04-24 17:38:48 +0200578#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579};
580
Ralf Baechle979b6c12005-06-13 14:30:40 -0700581MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700584MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200585module_param_named(debug, debug.msg_enable, int, 0);
586MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587MODULE_LICENSE("GPL");
588MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000589MODULE_FIRMWARE(FIRMWARE_8168D_1);
590MODULE_FIRMWARE(FIRMWARE_8168D_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800591MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000594static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
595 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100596static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100598static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100600static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200602static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700604 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200605static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200607static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700608static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200611 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
françois romieub646d902011-01-03 15:08:21 +0000613static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
614{
615 void __iomem *ioaddr = tp->mmio_addr;
616 int i;
617
618 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
619 for (i = 0; i < 20; i++) {
620 udelay(100);
621 if (RTL_R32(OCPAR) & OCPAR_FLAG)
622 break;
623 }
624 return RTL_R32(OCPDR);
625}
626
627static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
628{
629 void __iomem *ioaddr = tp->mmio_addr;
630 int i;
631
632 RTL_W32(OCPDR, data);
633 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
634 for (i = 0; i < 20; i++) {
635 udelay(100);
636 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
637 break;
638 }
639}
640
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800641static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000642{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800643 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000644 int i;
645
646 RTL_W8(ERIDR, cmd);
647 RTL_W32(ERIAR, 0x800010e8);
648 msleep(2);
649 for (i = 0; i < 5; i++) {
650 udelay(100);
651 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
652 break;
653 }
654
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800655 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000656}
657
658#define OOB_CMD_RESET 0x00
659#define OOB_CMD_DRIVER_START 0x05
660#define OOB_CMD_DRIVER_STOP 0x06
661
662static void rtl8168_driver_start(struct rtl8169_private *tp)
663{
664 int i;
665
666 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
667
668 for (i = 0; i < 10; i++) {
669 msleep(10);
670 if (ocp_read(tp, 0x0f, 0x0010) & 0x00000800)
671 break;
672 }
673}
674
675static void rtl8168_driver_stop(struct rtl8169_private *tp)
676{
677 int i;
678
679 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
680
681 for (i = 0; i < 10; i++) {
682 msleep(10);
683 if ((ocp_read(tp, 0x0f, 0x0010) & 0x00000800) == 0)
684 break;
685 }
686}
687
688
françois romieu4da19632011-01-03 15:07:55 +0000689static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 int i;
692
Francois Romieua6baf3a2007-11-08 23:23:21 +0100693 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Francois Romieu23714082006-01-29 00:49:09 +0100695 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100696 /*
697 * Check if the RTL8169 has completed writing to the specified
698 * MII register.
699 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200700 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
Francois Romieu23714082006-01-29 00:49:09 +0100702 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700704 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700705 * According to hardware specs a 20us delay is required after write
706 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700707 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700708 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
françois romieu4da19632011-01-03 15:07:55 +0000711static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 int i, value = -1;
714
Francois Romieua6baf3a2007-11-08 23:23:21 +0100715 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Francois Romieu23714082006-01-29 00:49:09 +0100717 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100718 /*
719 * Check if the RTL8169 has completed retrieving data from
720 * the specified MII register.
721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100723 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
725 }
Francois Romieu23714082006-01-29 00:49:09 +0100726 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700728 /*
729 * According to hardware specs a 20us delay is required after read
730 * complete indication, but before sending next command.
731 */
732 udelay(20);
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return value;
735}
736
françois romieuc0e45c12011-01-03 15:08:04 +0000737static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
738{
739 int i;
740
741 RTL_W32(OCPDR, data |
742 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
743 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
744 RTL_W32(EPHY_RXER_NUM, 0);
745
746 for (i = 0; i < 100; i++) {
747 mdelay(1);
748 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
749 break;
750 }
751}
752
753static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
754{
755 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
756 (value & OCPDR_DATA_MASK));
757}
758
759static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
760{
761 int i;
762
763 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
764
765 mdelay(1);
766 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
767 RTL_W32(EPHY_RXER_NUM, 0);
768
769 for (i = 0; i < 100; i++) {
770 mdelay(1);
771 if (RTL_R32(OCPAR) & OCPAR_FLAG)
772 break;
773 }
774
775 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
776}
777
françois romieue6de30d2011-01-03 15:08:37 +0000778#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
779
780static void r8168dp_2_mdio_start(void __iomem *ioaddr)
781{
782 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
783}
784
785static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
786{
787 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
788}
789
790static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
791{
792 r8168dp_2_mdio_start(ioaddr);
793
794 r8169_mdio_write(ioaddr, reg_addr, value);
795
796 r8168dp_2_mdio_stop(ioaddr);
797}
798
799static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
800{
801 int value;
802
803 r8168dp_2_mdio_start(ioaddr);
804
805 value = r8169_mdio_read(ioaddr, reg_addr);
806
807 r8168dp_2_mdio_stop(ioaddr);
808
809 return value;
810}
811
françois romieu4da19632011-01-03 15:07:55 +0000812static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200813{
françois romieuc0e45c12011-01-03 15:08:04 +0000814 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200815}
816
françois romieu4da19632011-01-03 15:07:55 +0000817static int rtl_readphy(struct rtl8169_private *tp, int location)
818{
françois romieuc0e45c12011-01-03 15:08:04 +0000819 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000820}
821
822static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
823{
824 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
825}
826
827static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000828{
829 int val;
830
françois romieu4da19632011-01-03 15:07:55 +0000831 val = rtl_readphy(tp, reg_addr);
832 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000833}
834
Francois Romieuccdffb92008-07-26 14:26:06 +0200835static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
836 int val)
837{
838 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200839
françois romieu4da19632011-01-03 15:07:55 +0000840 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200841}
842
843static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
844{
845 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200846
françois romieu4da19632011-01-03 15:07:55 +0000847 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200848}
849
Francois Romieudacf8152008-08-02 20:44:13 +0200850static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
851{
852 unsigned int i;
853
854 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
855 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
856
857 for (i = 0; i < 100; i++) {
858 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
859 break;
860 udelay(10);
861 }
862}
863
864static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
865{
866 u16 value = 0xffff;
867 unsigned int i;
868
869 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
870
871 for (i = 0; i < 100; i++) {
872 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
873 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
874 break;
875 }
876 udelay(10);
877 }
878
879 return value;
880}
881
882static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
883{
884 unsigned int i;
885
886 RTL_W32(CSIDR, value);
887 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
888 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
889
890 for (i = 0; i < 100; i++) {
891 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
892 break;
893 udelay(10);
894 }
895}
896
897static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
898{
899 u32 value = ~0x00;
900 unsigned int i;
901
902 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
903 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
904
905 for (i = 0; i < 100; i++) {
906 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
907 value = RTL_R32(CSIDR);
908 break;
909 }
910 udelay(10);
911 }
912
913 return value;
914}
915
françois romieudaf9df62009-10-07 12:44:20 +0000916static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
917{
918 u8 value = 0xff;
919 unsigned int i;
920
921 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
922
923 for (i = 0; i < 300; i++) {
924 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
925 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
926 break;
927 }
928 udelay(100);
929 }
930
931 return value;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
935{
936 RTL_W16(IntrMask, 0x0000);
937
938 RTL_W16(IntrStatus, 0xffff);
939}
940
941static void rtl8169_asic_down(void __iomem *ioaddr)
942{
943 RTL_W8(ChipCmd, 0x00);
944 rtl8169_irq_mask_and_ack(ioaddr);
945 RTL_R16(CPlusCmd);
946}
947
françois romieu4da19632011-01-03 15:07:55 +0000948static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
françois romieu4da19632011-01-03 15:07:55 +0000950 void __iomem *ioaddr = tp->mmio_addr;
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return RTL_R32(TBICSR) & TBIReset;
953}
954
françois romieu4da19632011-01-03 15:07:55 +0000955static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
françois romieu4da19632011-01-03 15:07:55 +0000957 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
961{
962 return RTL_R32(TBICSR) & TBILinkOk;
963}
964
965static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
966{
967 return RTL_R8(PHYstatus) & LinkStatus;
968}
969
françois romieu4da19632011-01-03 15:07:55 +0000970static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
françois romieu4da19632011-01-03 15:07:55 +0000972 void __iomem *ioaddr = tp->mmio_addr;
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
975}
976
françois romieu4da19632011-01-03 15:07:55 +0000977static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 unsigned int val;
980
françois romieu4da19632011-01-03 15:07:55 +0000981 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
982 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000985static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieu07d3f512007-02-21 22:40:46 +0100986 struct rtl8169_private *tp,
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000987 void __iomem *ioaddr,
988 bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 unsigned long flags;
991
992 spin_lock_irqsave(&tp->lock, flags);
993 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000994 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +0000995 if (pm)
996 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +0100998 if (net_ratelimit())
999 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001000 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001002 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001003 if (pm)
1004 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 spin_unlock_irqrestore(&tp->lock, flags);
1007}
1008
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001009static void rtl8169_check_link_status(struct net_device *dev,
1010 struct rtl8169_private *tp,
1011 void __iomem *ioaddr)
1012{
1013 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1014}
1015
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001016#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1017
1018static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1019{
1020 void __iomem *ioaddr = tp->mmio_addr;
1021 u8 options;
1022 u32 wolopts = 0;
1023
1024 options = RTL_R8(Config1);
1025 if (!(options & PMEnable))
1026 return 0;
1027
1028 options = RTL_R8(Config3);
1029 if (options & LinkUp)
1030 wolopts |= WAKE_PHY;
1031 if (options & MagicPacket)
1032 wolopts |= WAKE_MAGIC;
1033
1034 options = RTL_R8(Config5);
1035 if (options & UWF)
1036 wolopts |= WAKE_UCAST;
1037 if (options & BWF)
1038 wolopts |= WAKE_BCAST;
1039 if (options & MWF)
1040 wolopts |= WAKE_MCAST;
1041
1042 return wolopts;
1043}
1044
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001045static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1046{
1047 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001048
1049 spin_lock_irq(&tp->lock);
1050
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001051 wol->supported = WAKE_ANY;
1052 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001053
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001054 spin_unlock_irq(&tp->lock);
1055}
1056
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001057static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001058{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001059 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001060 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001061 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001062 u32 opt;
1063 u16 reg;
1064 u8 mask;
1065 } cfg[] = {
1066 { WAKE_ANY, Config1, PMEnable },
1067 { WAKE_PHY, Config3, LinkUp },
1068 { WAKE_MAGIC, Config3, MagicPacket },
1069 { WAKE_UCAST, Config5, UWF },
1070 { WAKE_BCAST, Config5, BWF },
1071 { WAKE_MCAST, Config5, MWF },
1072 { WAKE_ANY, Config5, LanWake }
1073 };
1074
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001075 RTL_W8(Cfg9346, Cfg9346_Unlock);
1076
1077 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1078 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001079 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001080 options |= cfg[i].mask;
1081 RTL_W8(cfg[i].reg, options);
1082 }
1083
1084 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001085}
1086
1087static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1088{
1089 struct rtl8169_private *tp = netdev_priv(dev);
1090
1091 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001092
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001093 if (wol->wolopts)
1094 tp->features |= RTL_FEATURE_WOL;
1095 else
1096 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001097 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001098 spin_unlock_irq(&tp->lock);
1099
françois romieuea809072010-11-08 13:23:58 +00001100 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1101
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001102 return 0;
1103}
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105static void rtl8169_get_drvinfo(struct net_device *dev,
1106 struct ethtool_drvinfo *info)
1107{
1108 struct rtl8169_private *tp = netdev_priv(dev);
1109
1110 strcpy(info->driver, MODULENAME);
1111 strcpy(info->version, RTL8169_VERSION);
1112 strcpy(info->bus_info, pci_name(tp->pci_dev));
1113}
1114
1115static int rtl8169_get_regs_len(struct net_device *dev)
1116{
1117 return R8169_REGS_SIZE;
1118}
1119
1120static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001121 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 struct rtl8169_private *tp = netdev_priv(dev);
1124 void __iomem *ioaddr = tp->mmio_addr;
1125 int ret = 0;
1126 u32 reg;
1127
1128 reg = RTL_R32(TBICSR);
1129 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1130 (duplex == DUPLEX_FULL)) {
1131 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1132 } else if (autoneg == AUTONEG_ENABLE)
1133 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1134 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001135 netif_warn(tp, link, dev,
1136 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 ret = -EOPNOTSUPP;
1138 }
1139
1140 return ret;
1141}
1142
1143static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001144 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001147 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001148 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Hayes Wang716b50a2011-02-22 17:26:18 +08001150 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001153 int auto_nego;
1154
françois romieu4da19632011-01-03 15:07:55 +00001155 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001156 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1157 ADVERTISE_100HALF | ADVERTISE_100FULL);
1158
1159 if (adv & ADVERTISED_10baseT_Half)
1160 auto_nego |= ADVERTISE_10HALF;
1161 if (adv & ADVERTISED_10baseT_Full)
1162 auto_nego |= ADVERTISE_10FULL;
1163 if (adv & ADVERTISED_100baseT_Half)
1164 auto_nego |= ADVERTISE_100HALF;
1165 if (adv & ADVERTISED_100baseT_Full)
1166 auto_nego |= ADVERTISE_100FULL;
1167
françois romieu3577aa12009-05-19 10:46:48 +00001168 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1169
françois romieu4da19632011-01-03 15:07:55 +00001170 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001171 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1172
1173 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1174 if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
1175 (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
1176 (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
1177 (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
1178 (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
1179 (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
1180 (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
Hayes Wang5a5e4442011-02-22 17:26:21 +08001181 (tp->mac_version != RTL_GIGA_MAC_VER_16) &&
1182 (tp->mac_version != RTL_GIGA_MAC_VER_29) &&
1183 (tp->mac_version != RTL_GIGA_MAC_VER_30)) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001184 if (adv & ADVERTISED_1000baseT_Half)
1185 giga_ctrl |= ADVERTISE_1000HALF;
1186 if (adv & ADVERTISED_1000baseT_Full)
1187 giga_ctrl |= ADVERTISE_1000FULL;
1188 } else if (adv & (ADVERTISED_1000baseT_Half |
1189 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001190 netif_info(tp, link, dev,
1191 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001192 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
françois romieu3577aa12009-05-19 10:46:48 +00001195 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001196
françois romieu4da19632011-01-03 15:07:55 +00001197 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1198 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001199 } else {
1200 giga_ctrl = 0;
1201
1202 if (speed == SPEED_10)
1203 bmcr = 0;
1204 else if (speed == SPEED_100)
1205 bmcr = BMCR_SPEED100;
1206 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001207 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001208
1209 if (duplex == DUPLEX_FULL)
1210 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001211 }
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 tp->phy_1000_ctrl_reg = giga_ctrl;
1214
françois romieu4da19632011-01-03 15:07:55 +00001215 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001216
1217 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
1218 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
1219 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001220 rtl_writephy(tp, 0x17, 0x2138);
1221 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001222 } else {
françois romieu4da19632011-01-03 15:07:55 +00001223 rtl_writephy(tp, 0x17, 0x2108);
1224 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001225 }
1226 }
1227
Oliver Neukum54405cd2011-01-06 21:55:13 +01001228 rc = 0;
1229out:
1230 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001234 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
1236 struct rtl8169_private *tp = netdev_priv(dev);
1237 int ret;
1238
Oliver Neukum54405cd2011-01-06 21:55:13 +01001239 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001241 if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1243
1244 return ret;
1245}
1246
1247static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1248{
1249 struct rtl8169_private *tp = netdev_priv(dev);
1250 unsigned long flags;
1251 int ret;
1252
1253 spin_lock_irqsave(&tp->lock, flags);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001254 ret = rtl8169_set_speed(dev,
1255 cmd->autoneg, cmd->speed, cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return ret;
1259}
1260
1261static u32 rtl8169_get_rx_csum(struct net_device *dev)
1262{
1263 struct rtl8169_private *tp = netdev_priv(dev);
1264
1265 return tp->cp_cmd & RxChkSum;
1266}
1267
1268static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
1269{
1270 struct rtl8169_private *tp = netdev_priv(dev);
1271 void __iomem *ioaddr = tp->mmio_addr;
1272 unsigned long flags;
1273
1274 spin_lock_irqsave(&tp->lock, flags);
1275
1276 if (data)
1277 tp->cp_cmd |= RxChkSum;
1278 else
1279 tp->cp_cmd &= ~RxChkSum;
1280
1281 RTL_W16(CPlusCmd, tp->cp_cmd);
1282 RTL_R16(CPlusCmd);
1283
1284 spin_unlock_irqrestore(&tp->lock, flags);
1285
1286 return 0;
1287}
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1290 struct sk_buff *skb)
1291{
Jesse Grosseab6d182010-10-20 13:56:03 +00001292 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1294}
1295
Francois Romieu7a8fc772011-03-01 17:18:33 +01001296#define NETIF_F_HW_VLAN_TX_RX (NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX)
1297
1298static void rtl8169_vlan_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
1300 struct rtl8169_private *tp = netdev_priv(dev);
1301 void __iomem *ioaddr = tp->mmio_addr;
1302 unsigned long flags;
1303
1304 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu7a8fc772011-03-01 17:18:33 +01001305 if (dev->features & NETIF_F_HW_VLAN_RX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 tp->cp_cmd |= RxVlan;
1307 else
1308 tp->cp_cmd &= ~RxVlan;
1309 RTL_W16(CPlusCmd, tp->cp_cmd);
Francois Romieu7a8fc772011-03-01 17:18:33 +01001310 /* PCI commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 RTL_R16(CPlusCmd);
1312 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu7a8fc772011-03-01 17:18:33 +01001313
1314 dev->vlan_features = dev->features &~ NETIF_F_HW_VLAN_TX_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Francois Romieu7a8fc772011-03-01 17:18:33 +01001317static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Francois Romieu7a8fc772011-03-01 17:18:33 +01001321 if (opts2 & RxVlanTag)
1322 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Francois Romieuccdffb92008-07-26 14:26:06 +02001327static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct rtl8169_private *tp = netdev_priv(dev);
1330 void __iomem *ioaddr = tp->mmio_addr;
1331 u32 status;
1332
1333 cmd->supported =
1334 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1335 cmd->port = PORT_FIBRE;
1336 cmd->transceiver = XCVR_INTERNAL;
1337
1338 status = RTL_R32(TBICSR);
1339 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1340 cmd->autoneg = !!(status & TBINwEnable);
1341
1342 cmd->speed = SPEED_1000;
1343 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001344
1345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Francois Romieuccdffb92008-07-26 14:26:06 +02001348static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
1350 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Francois Romieuccdffb92008-07-26 14:26:06 +02001352 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1356{
1357 struct rtl8169_private *tp = netdev_priv(dev);
1358 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001359 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 spin_lock_irqsave(&tp->lock, flags);
1362
Francois Romieuccdffb92008-07-26 14:26:06 +02001363 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001366 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1370 void *p)
1371{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001372 struct rtl8169_private *tp = netdev_priv(dev);
1373 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Francois Romieu5b0384f2006-08-16 16:00:01 +02001375 if (regs->len > R8169_REGS_SIZE)
1376 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Francois Romieu5b0384f2006-08-16 16:00:01 +02001378 spin_lock_irqsave(&tp->lock, flags);
1379 memcpy_fromio(p, tp->mmio_addr, regs->len);
1380 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001383static u32 rtl8169_get_msglevel(struct net_device *dev)
1384{
1385 struct rtl8169_private *tp = netdev_priv(dev);
1386
1387 return tp->msg_enable;
1388}
1389
1390static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1391{
1392 struct rtl8169_private *tp = netdev_priv(dev);
1393
1394 tp->msg_enable = value;
1395}
1396
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001397static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1398 "tx_packets",
1399 "rx_packets",
1400 "tx_errors",
1401 "rx_errors",
1402 "rx_missed",
1403 "align_errors",
1404 "tx_single_collisions",
1405 "tx_multi_collisions",
1406 "unicast",
1407 "broadcast",
1408 "multicast",
1409 "tx_aborted",
1410 "tx_underrun",
1411};
1412
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001413static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001414{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001415 switch (sset) {
1416 case ETH_SS_STATS:
1417 return ARRAY_SIZE(rtl8169_gstrings);
1418 default:
1419 return -EOPNOTSUPP;
1420 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001421}
1422
Ivan Vecera355423d2009-02-06 21:49:57 -08001423static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001424{
1425 struct rtl8169_private *tp = netdev_priv(dev);
1426 void __iomem *ioaddr = tp->mmio_addr;
1427 struct rtl8169_counters *counters;
1428 dma_addr_t paddr;
1429 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001430 int wait = 1000;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001431 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001432
Ivan Vecera355423d2009-02-06 21:49:57 -08001433 /*
1434 * Some chips are unable to dump tally counters when the receiver
1435 * is disabled.
1436 */
1437 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1438 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001439
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001440 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001441 if (!counters)
1442 return;
1443
1444 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001445 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001446 RTL_W32(CounterAddrLow, cmd);
1447 RTL_W32(CounterAddrLow, cmd | CounterDump);
1448
Ivan Vecera355423d2009-02-06 21:49:57 -08001449 while (wait--) {
1450 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1451 /* copy updated counters */
1452 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001453 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001454 }
1455 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001456 }
1457
1458 RTL_W32(CounterAddrLow, 0);
1459 RTL_W32(CounterAddrHigh, 0);
1460
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001461 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001462}
1463
Ivan Vecera355423d2009-02-06 21:49:57 -08001464static void rtl8169_get_ethtool_stats(struct net_device *dev,
1465 struct ethtool_stats *stats, u64 *data)
1466{
1467 struct rtl8169_private *tp = netdev_priv(dev);
1468
1469 ASSERT_RTNL();
1470
1471 rtl8169_update_counters(dev);
1472
1473 data[0] = le64_to_cpu(tp->counters.tx_packets);
1474 data[1] = le64_to_cpu(tp->counters.rx_packets);
1475 data[2] = le64_to_cpu(tp->counters.tx_errors);
1476 data[3] = le32_to_cpu(tp->counters.rx_errors);
1477 data[4] = le16_to_cpu(tp->counters.rx_missed);
1478 data[5] = le16_to_cpu(tp->counters.align_errors);
1479 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1480 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1481 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1482 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1483 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1484 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1485 data[12] = le16_to_cpu(tp->counters.tx_underun);
1486}
1487
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001488static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1489{
1490 switch(stringset) {
1491 case ETH_SS_STATS:
1492 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1493 break;
1494 }
1495}
1496
Francois Romieu7a8fc772011-03-01 17:18:33 +01001497static int rtl8169_set_flags(struct net_device *dev, u32 data)
1498{
1499 struct rtl8169_private *tp = netdev_priv(dev);
1500 unsigned long old_feat = dev->features;
1501 int rc;
1502
1503 if ((tp->mac_version == RTL_GIGA_MAC_VER_05) &&
1504 !(data & ETH_FLAG_RXVLAN)) {
1505 netif_info(tp, drv, dev, "8110SCd requires hardware Rx VLAN\n");
1506 return -EINVAL;
1507 }
1508
1509 rc = ethtool_op_set_flags(dev, data, ETH_FLAG_TXVLAN | ETH_FLAG_RXVLAN);
1510 if (rc)
1511 return rc;
1512
1513 if ((old_feat ^ dev->features) & NETIF_F_HW_VLAN_RX)
1514 rtl8169_vlan_mode(dev);
1515
1516 return 0;
1517}
1518
Jeff Garzik7282d492006-09-13 14:30:00 -04001519static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 .get_drvinfo = rtl8169_get_drvinfo,
1521 .get_regs_len = rtl8169_get_regs_len,
1522 .get_link = ethtool_op_get_link,
1523 .get_settings = rtl8169_get_settings,
1524 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001525 .get_msglevel = rtl8169_get_msglevel,
1526 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 .get_rx_csum = rtl8169_get_rx_csum,
1528 .set_rx_csum = rtl8169_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 .set_tx_csum = ethtool_op_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 .set_tso = ethtool_op_set_tso,
1532 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001533 .get_wol = rtl8169_get_wol,
1534 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001535 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001536 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001537 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Francois Romieu7a8fc772011-03-01 17:18:33 +01001538 .set_flags = rtl8169_set_flags,
1539 .get_flags = ethtool_op_get_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540};
1541
Francois Romieu07d3f512007-02-21 22:40:46 +01001542static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1543 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Francois Romieu0e485152007-02-20 00:00:26 +01001545 /*
1546 * The driver currently handles the 8168Bf and the 8168Be identically
1547 * but they can be identified more specifically through the test below
1548 * if needed:
1549 *
1550 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001551 *
1552 * Same thing for the 8101Eb and the 8101Ec:
1553 *
1554 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001555 */
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001556 static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001558 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 int mac_version;
1560 } mac_info[] = {
Francois Romieu5b538df2008-07-20 16:22:45 +02001561 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001562 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1563 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001564 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001565
françois romieue6de30d2011-01-03 15:08:37 +00001566 /* 8168DP family. */
1567 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1568 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
1569
Francois Romieuef808d52008-06-29 13:10:54 +02001570 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001571 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001572 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001573 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001574 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001575 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1576 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001577 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001578 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001579 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001580
1581 /* 8168B family. */
1582 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1583 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1584 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1585 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1586
1587 /* 8101 family. */
Hayes Wang5a5e4442011-02-22 17:26:21 +08001588 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1589 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1590 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001591 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1592 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1593 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1594 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1595 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1596 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001597 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001598 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001599 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001600 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1601 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001602 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1603 /* FIXME: where did these entries come from ? -- FR */
1604 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1605 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1606
1607 /* 8110 family. */
1608 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1609 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1610 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1611 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1612 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1613 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1614
Jean Delvaref21b75e2009-05-26 20:54:48 -07001615 /* Catch-all */
1616 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }, *p = mac_info;
1618 u32 reg;
1619
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001620 reg = RTL_R32(TxConfig);
1621 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 p++;
1623 tp->mac_version = p->mac_version;
1624}
1625
1626static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1627{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001628 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Francois Romieu867763c2007-08-17 18:21:58 +02001631struct phy_reg {
1632 u16 reg;
1633 u16 val;
1634};
1635
françois romieu4da19632011-01-03 15:07:55 +00001636static void rtl_writephy_batch(struct rtl8169_private *tp,
1637 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001638{
1639 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001640 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001641 regs++;
1642 }
1643}
1644
françois romieubca03d52011-01-03 15:07:31 +00001645#define PHY_READ 0x00000000
1646#define PHY_DATA_OR 0x10000000
1647#define PHY_DATA_AND 0x20000000
1648#define PHY_BJMPN 0x30000000
1649#define PHY_READ_EFUSE 0x40000000
1650#define PHY_READ_MAC_BYTE 0x50000000
1651#define PHY_WRITE_MAC_BYTE 0x60000000
1652#define PHY_CLEAR_READCOUNT 0x70000000
1653#define PHY_WRITE 0x80000000
1654#define PHY_READCOUNT_EQ_SKIP 0x90000000
1655#define PHY_COMP_EQ_SKIPN 0xa0000000
1656#define PHY_COMP_NEQ_SKIPN 0xb0000000
1657#define PHY_WRITE_PREVIOUS 0xc0000000
1658#define PHY_SKIPN 0xd0000000
1659#define PHY_DELAY_MS 0xe0000000
1660#define PHY_WRITE_ERI_WORD 0xf0000000
1661
1662static void
1663rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1664{
françois romieubca03d52011-01-03 15:07:31 +00001665 __le32 *phytable = (__le32 *)fw->data;
1666 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001667 size_t index, fw_size = fw->size / sizeof(*phytable);
1668 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001669
1670 if (fw->size % sizeof(*phytable)) {
1671 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1672 return;
1673 }
1674
hayeswang42b82dc2011-01-10 02:07:25 +00001675 for (index = 0; index < fw_size; index++) {
1676 u32 action = le32_to_cpu(phytable[index]);
1677 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001678
hayeswang42b82dc2011-01-10 02:07:25 +00001679 switch(action & 0xf0000000) {
1680 case PHY_READ:
1681 case PHY_DATA_OR:
1682 case PHY_DATA_AND:
1683 case PHY_READ_EFUSE:
1684 case PHY_CLEAR_READCOUNT:
1685 case PHY_WRITE:
1686 case PHY_WRITE_PREVIOUS:
1687 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001688 break;
1689
hayeswang42b82dc2011-01-10 02:07:25 +00001690 case PHY_BJMPN:
1691 if (regno > index) {
1692 netif_err(tp, probe, tp->dev,
1693 "Out of range of firmware\n");
1694 return;
1695 }
1696 break;
1697 case PHY_READCOUNT_EQ_SKIP:
1698 if (index + 2 >= fw_size) {
1699 netif_err(tp, probe, tp->dev,
1700 "Out of range of firmware\n");
1701 return;
1702 }
1703 break;
1704 case PHY_COMP_EQ_SKIPN:
1705 case PHY_COMP_NEQ_SKIPN:
1706 case PHY_SKIPN:
1707 if (index + 1 + regno >= fw_size) {
1708 netif_err(tp, probe, tp->dev,
1709 "Out of range of firmware\n");
1710 return;
1711 }
1712 break;
1713
1714 case PHY_READ_MAC_BYTE:
1715 case PHY_WRITE_MAC_BYTE:
1716 case PHY_WRITE_ERI_WORD:
1717 default:
1718 netif_err(tp, probe, tp->dev,
1719 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001720 return;
1721 }
1722 }
1723
hayeswang42b82dc2011-01-10 02:07:25 +00001724 predata = 0;
1725 count = 0;
1726
1727 for (index = 0; index < fw_size; ) {
1728 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001729 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001730 u32 regno = (action & 0x0fff0000) >> 16;
1731
1732 if (!action)
1733 break;
françois romieubca03d52011-01-03 15:07:31 +00001734
1735 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001736 case PHY_READ:
1737 predata = rtl_readphy(tp, regno);
1738 count++;
1739 index++;
françois romieubca03d52011-01-03 15:07:31 +00001740 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001741 case PHY_DATA_OR:
1742 predata |= data;
1743 index++;
1744 break;
1745 case PHY_DATA_AND:
1746 predata &= data;
1747 index++;
1748 break;
1749 case PHY_BJMPN:
1750 index -= regno;
1751 break;
1752 case PHY_READ_EFUSE:
1753 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1754 index++;
1755 break;
1756 case PHY_CLEAR_READCOUNT:
1757 count = 0;
1758 index++;
1759 break;
1760 case PHY_WRITE:
1761 rtl_writephy(tp, regno, data);
1762 index++;
1763 break;
1764 case PHY_READCOUNT_EQ_SKIP:
1765 if (count == data)
1766 index += 2;
1767 else
1768 index += 1;
1769 break;
1770 case PHY_COMP_EQ_SKIPN:
1771 if (predata == data)
1772 index += regno;
1773 index++;
1774 break;
1775 case PHY_COMP_NEQ_SKIPN:
1776 if (predata != data)
1777 index += regno;
1778 index++;
1779 break;
1780 case PHY_WRITE_PREVIOUS:
1781 rtl_writephy(tp, regno, predata);
1782 index++;
1783 break;
1784 case PHY_SKIPN:
1785 index += regno + 1;
1786 break;
1787 case PHY_DELAY_MS:
1788 mdelay(data);
1789 index++;
1790 break;
1791
1792 case PHY_READ_MAC_BYTE:
1793 case PHY_WRITE_MAC_BYTE:
1794 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001795 default:
1796 BUG();
1797 }
1798 }
1799}
1800
françois romieuf1e02ed2011-01-13 13:07:53 +00001801static void rtl_release_firmware(struct rtl8169_private *tp)
1802{
François Romieu953a12c2011-04-24 17:38:48 +02001803 if (!IS_ERR_OR_NULL(tp->fw))
1804 release_firmware(tp->fw);
1805 tp->fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001806}
1807
François Romieu953a12c2011-04-24 17:38:48 +02001808static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001809{
François Romieu953a12c2011-04-24 17:38:48 +02001810 const struct firmware *fw = tp->fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001811
1812 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
François Romieu953a12c2011-04-24 17:38:48 +02001813 if (!IS_ERR_OR_NULL(fw))
1814 rtl_phy_write_fw(tp, fw);
1815}
1816
1817static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1818{
1819 if (rtl_readphy(tp, reg) != val)
1820 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1821 else
1822 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00001823}
1824
françois romieu4da19632011-01-03 15:07:55 +00001825static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001827 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001828 { 0x1f, 0x0001 },
1829 { 0x06, 0x006e },
1830 { 0x08, 0x0708 },
1831 { 0x15, 0x4000 },
1832 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
françois romieu0b9b5712009-08-10 19:44:56 +00001834 { 0x1f, 0x0001 },
1835 { 0x03, 0x00a1 },
1836 { 0x02, 0x0008 },
1837 { 0x01, 0x0120 },
1838 { 0x00, 0x1000 },
1839 { 0x04, 0x0800 },
1840 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
françois romieu0b9b5712009-08-10 19:44:56 +00001842 { 0x03, 0xff41 },
1843 { 0x02, 0xdf60 },
1844 { 0x01, 0x0140 },
1845 { 0x00, 0x0077 },
1846 { 0x04, 0x7800 },
1847 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
françois romieu0b9b5712009-08-10 19:44:56 +00001849 { 0x03, 0x802f },
1850 { 0x02, 0x4f02 },
1851 { 0x01, 0x0409 },
1852 { 0x00, 0xf0f9 },
1853 { 0x04, 0x9800 },
1854 { 0x04, 0x9000 },
1855
1856 { 0x03, 0xdf01 },
1857 { 0x02, 0xdf20 },
1858 { 0x01, 0xff95 },
1859 { 0x00, 0xba00 },
1860 { 0x04, 0xa800 },
1861 { 0x04, 0xa000 },
1862
1863 { 0x03, 0xff41 },
1864 { 0x02, 0xdf20 },
1865 { 0x01, 0x0140 },
1866 { 0x00, 0x00bb },
1867 { 0x04, 0xb800 },
1868 { 0x04, 0xb000 },
1869
1870 { 0x03, 0xdf41 },
1871 { 0x02, 0xdc60 },
1872 { 0x01, 0x6340 },
1873 { 0x00, 0x007d },
1874 { 0x04, 0xd800 },
1875 { 0x04, 0xd000 },
1876
1877 { 0x03, 0xdf01 },
1878 { 0x02, 0xdf20 },
1879 { 0x01, 0x100a },
1880 { 0x00, 0xa0ff },
1881 { 0x04, 0xf800 },
1882 { 0x04, 0xf000 },
1883
1884 { 0x1f, 0x0000 },
1885 { 0x0b, 0x0000 },
1886 { 0x00, 0x9200 }
1887 };
1888
françois romieu4da19632011-01-03 15:07:55 +00001889 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
françois romieu4da19632011-01-03 15:07:55 +00001892static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001893{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001894 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001895 { 0x1f, 0x0002 },
1896 { 0x01, 0x90d0 },
1897 { 0x1f, 0x0000 }
1898 };
1899
françois romieu4da19632011-01-03 15:07:55 +00001900 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001901}
1902
françois romieu4da19632011-01-03 15:07:55 +00001903static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001904{
1905 struct pci_dev *pdev = tp->pci_dev;
1906 u16 vendor_id, device_id;
1907
1908 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1909 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1910
1911 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1912 return;
1913
françois romieu4da19632011-01-03 15:07:55 +00001914 rtl_writephy(tp, 0x1f, 0x0001);
1915 rtl_writephy(tp, 0x10, 0xf01b);
1916 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001917}
1918
françois romieu4da19632011-01-03 15:07:55 +00001919static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001920{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001921 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00001922 { 0x1f, 0x0001 },
1923 { 0x04, 0x0000 },
1924 { 0x03, 0x00a1 },
1925 { 0x02, 0x0008 },
1926 { 0x01, 0x0120 },
1927 { 0x00, 0x1000 },
1928 { 0x04, 0x0800 },
1929 { 0x04, 0x9000 },
1930 { 0x03, 0x802f },
1931 { 0x02, 0x4f02 },
1932 { 0x01, 0x0409 },
1933 { 0x00, 0xf099 },
1934 { 0x04, 0x9800 },
1935 { 0x04, 0xa000 },
1936 { 0x03, 0xdf01 },
1937 { 0x02, 0xdf20 },
1938 { 0x01, 0xff95 },
1939 { 0x00, 0xba00 },
1940 { 0x04, 0xa800 },
1941 { 0x04, 0xf000 },
1942 { 0x03, 0xdf01 },
1943 { 0x02, 0xdf20 },
1944 { 0x01, 0x101a },
1945 { 0x00, 0xa0ff },
1946 { 0x04, 0xf800 },
1947 { 0x04, 0x0000 },
1948 { 0x1f, 0x0000 },
1949
1950 { 0x1f, 0x0001 },
1951 { 0x10, 0xf41b },
1952 { 0x14, 0xfb54 },
1953 { 0x18, 0xf5c7 },
1954 { 0x1f, 0x0000 },
1955
1956 { 0x1f, 0x0001 },
1957 { 0x17, 0x0cc0 },
1958 { 0x1f, 0x0000 }
1959 };
1960
françois romieu4da19632011-01-03 15:07:55 +00001961 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00001962
françois romieu4da19632011-01-03 15:07:55 +00001963 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00001964}
1965
françois romieu4da19632011-01-03 15:07:55 +00001966static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00001967{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001968 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00001969 { 0x1f, 0x0001 },
1970 { 0x04, 0x0000 },
1971 { 0x03, 0x00a1 },
1972 { 0x02, 0x0008 },
1973 { 0x01, 0x0120 },
1974 { 0x00, 0x1000 },
1975 { 0x04, 0x0800 },
1976 { 0x04, 0x9000 },
1977 { 0x03, 0x802f },
1978 { 0x02, 0x4f02 },
1979 { 0x01, 0x0409 },
1980 { 0x00, 0xf099 },
1981 { 0x04, 0x9800 },
1982 { 0x04, 0xa000 },
1983 { 0x03, 0xdf01 },
1984 { 0x02, 0xdf20 },
1985 { 0x01, 0xff95 },
1986 { 0x00, 0xba00 },
1987 { 0x04, 0xa800 },
1988 { 0x04, 0xf000 },
1989 { 0x03, 0xdf01 },
1990 { 0x02, 0xdf20 },
1991 { 0x01, 0x101a },
1992 { 0x00, 0xa0ff },
1993 { 0x04, 0xf800 },
1994 { 0x04, 0x0000 },
1995 { 0x1f, 0x0000 },
1996
1997 { 0x1f, 0x0001 },
1998 { 0x0b, 0x8480 },
1999 { 0x1f, 0x0000 },
2000
2001 { 0x1f, 0x0001 },
2002 { 0x18, 0x67c7 },
2003 { 0x04, 0x2000 },
2004 { 0x03, 0x002f },
2005 { 0x02, 0x4360 },
2006 { 0x01, 0x0109 },
2007 { 0x00, 0x3022 },
2008 { 0x04, 0x2800 },
2009 { 0x1f, 0x0000 },
2010
2011 { 0x1f, 0x0001 },
2012 { 0x17, 0x0cc0 },
2013 { 0x1f, 0x0000 }
2014 };
2015
françois romieu4da19632011-01-03 15:07:55 +00002016 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002017}
2018
françois romieu4da19632011-01-03 15:07:55 +00002019static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002020{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002021 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002022 { 0x10, 0xf41b },
2023 { 0x1f, 0x0000 }
2024 };
2025
françois romieu4da19632011-01-03 15:07:55 +00002026 rtl_writephy(tp, 0x1f, 0x0001);
2027 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002028
françois romieu4da19632011-01-03 15:07:55 +00002029 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002030}
2031
françois romieu4da19632011-01-03 15:07:55 +00002032static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002033{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002034 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002035 { 0x1f, 0x0001 },
2036 { 0x10, 0xf41b },
2037 { 0x1f, 0x0000 }
2038 };
2039
françois romieu4da19632011-01-03 15:07:55 +00002040 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002041}
2042
françois romieu4da19632011-01-03 15:07:55 +00002043static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002044{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002045 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002046 { 0x1f, 0x0000 },
2047 { 0x1d, 0x0f00 },
2048 { 0x1f, 0x0002 },
2049 { 0x0c, 0x1ec8 },
2050 { 0x1f, 0x0000 }
2051 };
2052
françois romieu4da19632011-01-03 15:07:55 +00002053 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002054}
2055
françois romieu4da19632011-01-03 15:07:55 +00002056static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002057{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002058 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002059 { 0x1f, 0x0001 },
2060 { 0x1d, 0x3d98 },
2061 { 0x1f, 0x0000 }
2062 };
2063
françois romieu4da19632011-01-03 15:07:55 +00002064 rtl_writephy(tp, 0x1f, 0x0000);
2065 rtl_patchphy(tp, 0x14, 1 << 5);
2066 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002067
françois romieu4da19632011-01-03 15:07:55 +00002068 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002069}
2070
françois romieu4da19632011-01-03 15:07:55 +00002071static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002072{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002073 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002074 { 0x1f, 0x0001 },
2075 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002076 { 0x1f, 0x0002 },
2077 { 0x00, 0x88d4 },
2078 { 0x01, 0x82b1 },
2079 { 0x03, 0x7002 },
2080 { 0x08, 0x9e30 },
2081 { 0x09, 0x01f0 },
2082 { 0x0a, 0x5500 },
2083 { 0x0c, 0x00c8 },
2084 { 0x1f, 0x0003 },
2085 { 0x12, 0xc096 },
2086 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002087 { 0x1f, 0x0000 },
2088 { 0x1f, 0x0000 },
2089 { 0x09, 0x2000 },
2090 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002091 };
2092
françois romieu4da19632011-01-03 15:07:55 +00002093 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002094
françois romieu4da19632011-01-03 15:07:55 +00002095 rtl_patchphy(tp, 0x14, 1 << 5);
2096 rtl_patchphy(tp, 0x0d, 1 << 5);
2097 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002098}
2099
françois romieu4da19632011-01-03 15:07:55 +00002100static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002101{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002102 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002103 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002104 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002105 { 0x03, 0x802f },
2106 { 0x02, 0x4f02 },
2107 { 0x01, 0x0409 },
2108 { 0x00, 0xf099 },
2109 { 0x04, 0x9800 },
2110 { 0x04, 0x9000 },
2111 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002112 { 0x1f, 0x0002 },
2113 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002114 { 0x06, 0x0761 },
2115 { 0x1f, 0x0003 },
2116 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002117 { 0x1f, 0x0000 }
2118 };
2119
françois romieu4da19632011-01-03 15:07:55 +00002120 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002121
françois romieu4da19632011-01-03 15:07:55 +00002122 rtl_patchphy(tp, 0x16, 1 << 0);
2123 rtl_patchphy(tp, 0x14, 1 << 5);
2124 rtl_patchphy(tp, 0x0d, 1 << 5);
2125 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002126}
2127
françois romieu4da19632011-01-03 15:07:55 +00002128static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002129{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002130 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002131 { 0x1f, 0x0001 },
2132 { 0x12, 0x2300 },
2133 { 0x1d, 0x3d98 },
2134 { 0x1f, 0x0002 },
2135 { 0x0c, 0x7eb8 },
2136 { 0x06, 0x5461 },
2137 { 0x1f, 0x0003 },
2138 { 0x16, 0x0f0a },
2139 { 0x1f, 0x0000 }
2140 };
2141
françois romieu4da19632011-01-03 15:07:55 +00002142 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002143
françois romieu4da19632011-01-03 15:07:55 +00002144 rtl_patchphy(tp, 0x16, 1 << 0);
2145 rtl_patchphy(tp, 0x14, 1 << 5);
2146 rtl_patchphy(tp, 0x0d, 1 << 5);
2147 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002148}
2149
françois romieu4da19632011-01-03 15:07:55 +00002150static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002151{
françois romieu4da19632011-01-03 15:07:55 +00002152 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002153}
2154
françois romieubca03d52011-01-03 15:07:31 +00002155static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002156{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002157 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002158 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002159 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002160 { 0x06, 0x4064 },
2161 { 0x07, 0x2863 },
2162 { 0x08, 0x059c },
2163 { 0x09, 0x26b4 },
2164 { 0x0a, 0x6a19 },
2165 { 0x0b, 0xdcc8 },
2166 { 0x10, 0xf06d },
2167 { 0x14, 0x7f68 },
2168 { 0x18, 0x7fd9 },
2169 { 0x1c, 0xf0ff },
2170 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002171 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002172 { 0x12, 0xf49f },
2173 { 0x13, 0x070b },
2174 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002175 { 0x14, 0x94c0 },
2176
2177 /*
2178 * Tx Error Issue
2179 * enhance line driver power
2180 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002181 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002182 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002183 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002184 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002185 { 0x06, 0x5561 },
2186
2187 /*
2188 * Can not link to 1Gbps with bad cable
2189 * Decrease SNR threshold form 21.07dB to 19.04dB
2190 */
2191 { 0x1f, 0x0001 },
2192 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002193
2194 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002195 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002196 };
françois romieubca03d52011-01-03 15:07:31 +00002197 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002198
françois romieu4da19632011-01-03 15:07:55 +00002199 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002200
françois romieubca03d52011-01-03 15:07:31 +00002201 /*
2202 * Rx Error Issue
2203 * Fine Tune Switching regulator parameter
2204 */
françois romieu4da19632011-01-03 15:07:55 +00002205 rtl_writephy(tp, 0x1f, 0x0002);
2206 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2207 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002208
françois romieudaf9df62009-10-07 12:44:20 +00002209 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002210 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002211 { 0x1f, 0x0002 },
2212 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002213 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002214 { 0x05, 0x8330 },
2215 { 0x06, 0x669a },
2216 { 0x1f, 0x0002 }
2217 };
2218 int val;
2219
françois romieu4da19632011-01-03 15:07:55 +00002220 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002221
françois romieu4da19632011-01-03 15:07:55 +00002222 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002223
2224 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002225 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002226 0x0065, 0x0066, 0x0067, 0x0068,
2227 0x0069, 0x006a, 0x006b, 0x006c
2228 };
2229 int i;
2230
françois romieu4da19632011-01-03 15:07:55 +00002231 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002232
2233 val &= 0xff00;
2234 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002235 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002236 }
2237 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002238 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002239 { 0x1f, 0x0002 },
2240 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002241 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002242 { 0x05, 0x8330 },
2243 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002244 };
2245
françois romieu4da19632011-01-03 15:07:55 +00002246 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002247 }
2248
françois romieubca03d52011-01-03 15:07:31 +00002249 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002250 rtl_writephy(tp, 0x1f, 0x0002);
2251 rtl_patchphy(tp, 0x0d, 0x0300);
2252 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002253
françois romieubca03d52011-01-03 15:07:31 +00002254 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002255 rtl_writephy(tp, 0x1f, 0x0002);
2256 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2257 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002258
françois romieu4da19632011-01-03 15:07:55 +00002259 rtl_writephy(tp, 0x1f, 0x0005);
2260 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002261
2262 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002263
françois romieu4da19632011-01-03 15:07:55 +00002264 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002265}
2266
françois romieubca03d52011-01-03 15:07:31 +00002267static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002268{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002269 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002270 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002271 { 0x1f, 0x0001 },
2272 { 0x06, 0x4064 },
2273 { 0x07, 0x2863 },
2274 { 0x08, 0x059c },
2275 { 0x09, 0x26b4 },
2276 { 0x0a, 0x6a19 },
2277 { 0x0b, 0xdcc8 },
2278 { 0x10, 0xf06d },
2279 { 0x14, 0x7f68 },
2280 { 0x18, 0x7fd9 },
2281 { 0x1c, 0xf0ff },
2282 { 0x1d, 0x3d9c },
2283 { 0x1f, 0x0003 },
2284 { 0x12, 0xf49f },
2285 { 0x13, 0x070b },
2286 { 0x1a, 0x05ad },
2287 { 0x14, 0x94c0 },
2288
françois romieubca03d52011-01-03 15:07:31 +00002289 /*
2290 * Tx Error Issue
2291 * enhance line driver power
2292 */
françois romieudaf9df62009-10-07 12:44:20 +00002293 { 0x1f, 0x0002 },
2294 { 0x06, 0x5561 },
2295 { 0x1f, 0x0005 },
2296 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002297 { 0x06, 0x5561 },
2298
2299 /*
2300 * Can not link to 1Gbps with bad cable
2301 * Decrease SNR threshold form 21.07dB to 19.04dB
2302 */
2303 { 0x1f, 0x0001 },
2304 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002305
2306 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002307 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002308 };
françois romieubca03d52011-01-03 15:07:31 +00002309 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002310
françois romieu4da19632011-01-03 15:07:55 +00002311 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002312
2313 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002314 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002315 { 0x1f, 0x0002 },
2316 { 0x05, 0x669a },
2317 { 0x1f, 0x0005 },
2318 { 0x05, 0x8330 },
2319 { 0x06, 0x669a },
2320
2321 { 0x1f, 0x0002 }
2322 };
2323 int val;
2324
françois romieu4da19632011-01-03 15:07:55 +00002325 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002326
françois romieu4da19632011-01-03 15:07:55 +00002327 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002328 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002329 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002330 0x0065, 0x0066, 0x0067, 0x0068,
2331 0x0069, 0x006a, 0x006b, 0x006c
2332 };
2333 int i;
2334
françois romieu4da19632011-01-03 15:07:55 +00002335 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002336
2337 val &= 0xff00;
2338 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002339 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002340 }
2341 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002342 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002343 { 0x1f, 0x0002 },
2344 { 0x05, 0x2642 },
2345 { 0x1f, 0x0005 },
2346 { 0x05, 0x8330 },
2347 { 0x06, 0x2642 }
2348 };
2349
françois romieu4da19632011-01-03 15:07:55 +00002350 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002351 }
2352
françois romieubca03d52011-01-03 15:07:31 +00002353 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002354 rtl_writephy(tp, 0x1f, 0x0002);
2355 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2356 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002357
françois romieubca03d52011-01-03 15:07:31 +00002358 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002359 rtl_writephy(tp, 0x1f, 0x0002);
2360 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002361
françois romieu4da19632011-01-03 15:07:55 +00002362 rtl_writephy(tp, 0x1f, 0x0005);
2363 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002364
2365 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002366
françois romieu4da19632011-01-03 15:07:55 +00002367 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002368}
2369
françois romieu4da19632011-01-03 15:07:55 +00002370static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002371{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002372 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002373 { 0x1f, 0x0002 },
2374 { 0x10, 0x0008 },
2375 { 0x0d, 0x006c },
2376
2377 { 0x1f, 0x0000 },
2378 { 0x0d, 0xf880 },
2379
2380 { 0x1f, 0x0001 },
2381 { 0x17, 0x0cc0 },
2382
2383 { 0x1f, 0x0001 },
2384 { 0x0b, 0xa4d8 },
2385 { 0x09, 0x281c },
2386 { 0x07, 0x2883 },
2387 { 0x0a, 0x6b35 },
2388 { 0x1d, 0x3da4 },
2389 { 0x1c, 0xeffd },
2390 { 0x14, 0x7f52 },
2391 { 0x18, 0x7fc6 },
2392 { 0x08, 0x0601 },
2393 { 0x06, 0x4063 },
2394 { 0x10, 0xf074 },
2395 { 0x1f, 0x0003 },
2396 { 0x13, 0x0789 },
2397 { 0x12, 0xf4bd },
2398 { 0x1a, 0x04fd },
2399 { 0x14, 0x84b0 },
2400 { 0x1f, 0x0000 },
2401 { 0x00, 0x9200 },
2402
2403 { 0x1f, 0x0005 },
2404 { 0x01, 0x0340 },
2405 { 0x1f, 0x0001 },
2406 { 0x04, 0x4000 },
2407 { 0x03, 0x1d21 },
2408 { 0x02, 0x0c32 },
2409 { 0x01, 0x0200 },
2410 { 0x00, 0x5554 },
2411 { 0x04, 0x4800 },
2412 { 0x04, 0x4000 },
2413 { 0x04, 0xf000 },
2414 { 0x03, 0xdf01 },
2415 { 0x02, 0xdf20 },
2416 { 0x01, 0x101a },
2417 { 0x00, 0xa0ff },
2418 { 0x04, 0xf800 },
2419 { 0x04, 0xf000 },
2420 { 0x1f, 0x0000 },
2421
2422 { 0x1f, 0x0007 },
2423 { 0x1e, 0x0023 },
2424 { 0x16, 0x0000 },
2425 { 0x1f, 0x0000 }
2426 };
2427
françois romieu4da19632011-01-03 15:07:55 +00002428 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002429}
2430
françois romieue6de30d2011-01-03 15:08:37 +00002431static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2432{
2433 static const struct phy_reg phy_reg_init[] = {
2434 { 0x1f, 0x0001 },
2435 { 0x17, 0x0cc0 },
2436
2437 { 0x1f, 0x0007 },
2438 { 0x1e, 0x002d },
2439 { 0x18, 0x0040 },
2440 { 0x1f, 0x0000 }
2441 };
2442
2443 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2444 rtl_patchphy(tp, 0x0d, 1 << 5);
2445}
2446
françois romieu4da19632011-01-03 15:07:55 +00002447static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002448{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002449 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002450 { 0x1f, 0x0003 },
2451 { 0x08, 0x441d },
2452 { 0x01, 0x9100 },
2453 { 0x1f, 0x0000 }
2454 };
2455
françois romieu4da19632011-01-03 15:07:55 +00002456 rtl_writephy(tp, 0x1f, 0x0000);
2457 rtl_patchphy(tp, 0x11, 1 << 12);
2458 rtl_patchphy(tp, 0x19, 1 << 13);
2459 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002460
françois romieu4da19632011-01-03 15:07:55 +00002461 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002462}
2463
Hayes Wang5a5e4442011-02-22 17:26:21 +08002464static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2465{
2466 static const struct phy_reg phy_reg_init[] = {
2467 { 0x1f, 0x0005 },
2468 { 0x1a, 0x0000 },
2469 { 0x1f, 0x0000 },
2470
2471 { 0x1f, 0x0004 },
2472 { 0x1c, 0x0000 },
2473 { 0x1f, 0x0000 },
2474
2475 { 0x1f, 0x0001 },
2476 { 0x15, 0x7701 },
2477 { 0x1f, 0x0000 }
2478 };
2479
2480 /* Disable ALDPS before ram code */
2481 rtl_writephy(tp, 0x1f, 0x0000);
2482 rtl_writephy(tp, 0x18, 0x0310);
2483 msleep(100);
2484
François Romieu953a12c2011-04-24 17:38:48 +02002485 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002486
2487 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2488}
2489
Francois Romieu5615d9f2007-08-17 17:50:46 +02002490static void rtl_hw_phy_config(struct net_device *dev)
2491{
2492 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002493
2494 rtl8169_print_mac_version(tp);
2495
2496 switch (tp->mac_version) {
2497 case RTL_GIGA_MAC_VER_01:
2498 break;
2499 case RTL_GIGA_MAC_VER_02:
2500 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002501 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002502 break;
2503 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002504 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002505 break;
françois romieu2e9558562009-08-10 19:44:19 +00002506 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002507 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002508 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002509 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002510 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002511 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002512 case RTL_GIGA_MAC_VER_07:
2513 case RTL_GIGA_MAC_VER_08:
2514 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002515 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002516 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002517 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002518 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002519 break;
2520 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002521 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002522 break;
2523 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002524 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002525 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002526 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002527 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002528 break;
2529 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002530 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002531 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002532 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002533 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002534 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002535 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002536 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002537 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002538 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002539 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002540 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002541 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002542 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002543 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002544 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002545 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002546 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002547 break;
2548 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002549 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002550 break;
2551 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002552 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002553 break;
françois romieue6de30d2011-01-03 15:08:37 +00002554 case RTL_GIGA_MAC_VER_28:
2555 rtl8168d_4_hw_phy_config(tp);
2556 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002557 case RTL_GIGA_MAC_VER_29:
2558 case RTL_GIGA_MAC_VER_30:
2559 rtl8105e_hw_phy_config(tp);
2560 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002561
Francois Romieu5615d9f2007-08-17 17:50:46 +02002562 default:
2563 break;
2564 }
2565}
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567static void rtl8169_phy_timer(unsigned long __opaque)
2568{
2569 struct net_device *dev = (struct net_device *)__opaque;
2570 struct rtl8169_private *tp = netdev_priv(dev);
2571 struct timer_list *timer = &tp->timer;
2572 void __iomem *ioaddr = tp->mmio_addr;
2573 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2574
Francois Romieubcf0bf92006-07-26 23:14:13 +02002575 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Francois Romieu64e4bfb2006-08-17 12:43:06 +02002577 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return;
2579
2580 spin_lock_irq(&tp->lock);
2581
françois romieu4da19632011-01-03 15:07:55 +00002582 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002583 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 * A busy loop could burn quite a few cycles on nowadays CPU.
2585 * Let's delay the execution of the timer for a few ticks.
2586 */
2587 timeout = HZ/10;
2588 goto out_mod_timer;
2589 }
2590
2591 if (tp->link_ok(ioaddr))
2592 goto out_unlock;
2593
Joe Perchesbf82c182010-02-09 11:49:50 +00002594 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
françois romieu4da19632011-01-03 15:07:55 +00002596 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598out_mod_timer:
2599 mod_timer(timer, jiffies + timeout);
2600out_unlock:
2601 spin_unlock_irq(&tp->lock);
2602}
2603
2604static inline void rtl8169_delete_timer(struct net_device *dev)
2605{
2606 struct rtl8169_private *tp = netdev_priv(dev);
2607 struct timer_list *timer = &tp->timer;
2608
Francois Romieue179bb72007-08-17 15:05:21 +02002609 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return;
2611
2612 del_timer_sync(timer);
2613}
2614
2615static inline void rtl8169_request_timer(struct net_device *dev)
2616{
2617 struct rtl8169_private *tp = netdev_priv(dev);
2618 struct timer_list *timer = &tp->timer;
2619
Francois Romieue179bb72007-08-17 15:05:21 +02002620 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return;
2622
Francois Romieu2efa53f2007-03-09 00:00:05 +01002623 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
2626#ifdef CONFIG_NET_POLL_CONTROLLER
2627/*
2628 * Polling 'interrupt' - used by things like netconsole to send skbs
2629 * without having to re-enable interrupts. It's not called while
2630 * the interrupt routine is executing.
2631 */
2632static void rtl8169_netpoll(struct net_device *dev)
2633{
2634 struct rtl8169_private *tp = netdev_priv(dev);
2635 struct pci_dev *pdev = tp->pci_dev;
2636
2637 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002638 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 enable_irq(pdev->irq);
2640}
2641#endif
2642
2643static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2644 void __iomem *ioaddr)
2645{
2646 iounmap(ioaddr);
2647 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002648 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 pci_disable_device(pdev);
2650 free_netdev(dev);
2651}
2652
Francois Romieubf793292006-11-01 00:53:05 +01002653static void rtl8169_phy_reset(struct net_device *dev,
2654 struct rtl8169_private *tp)
2655{
Francois Romieu07d3f512007-02-21 22:40:46 +01002656 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002657
françois romieu4da19632011-01-03 15:07:55 +00002658 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002659 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002660 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002661 return;
2662 msleep(1);
2663 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002664 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002665}
2666
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002667static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002669 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002670
Francois Romieu5615d9f2007-08-17 17:50:46 +02002671 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002672
Marcus Sundberg773328942008-07-10 21:28:08 +02002673 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2674 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2675 RTL_W8(0x82, 0x01);
2676 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002677
Francois Romieu6dccd162007-02-13 23:38:05 +01002678 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2679
2680 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2681 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002682
Francois Romieubcf0bf92006-07-26 23:14:13 +02002683 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002684 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2685 RTL_W8(0x82, 0x01);
2686 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002687 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002688 }
2689
Francois Romieubf793292006-11-01 00:53:05 +01002690 rtl8169_phy_reset(dev, tp);
2691
Oliver Neukum54405cd2011-01-06 21:55:13 +01002692 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
2693 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2694 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
Eric Dumazetdb552b32011-03-19 16:39:11 +01002695 (tp->mii.supports_gmii ?
Oliver Neukum54405cd2011-01-06 21:55:13 +01002696 ADVERTISED_1000baseT_Half |
Eric Dumazetdb552b32011-03-19 16:39:11 +01002697 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002698
Joe Perchesbf82c182010-02-09 11:49:50 +00002699 if (RTL_R8(PHYstatus) & TBI_Enable)
2700 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002701}
2702
Francois Romieu773d2022007-01-31 23:47:43 +01002703static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2704{
2705 void __iomem *ioaddr = tp->mmio_addr;
2706 u32 high;
2707 u32 low;
2708
2709 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2710 high = addr[4] | (addr[5] << 8);
2711
2712 spin_lock_irq(&tp->lock);
2713
2714 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002715
Francois Romieu773d2022007-01-31 23:47:43 +01002716 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002717 RTL_R32(MAC4);
2718
Francois Romieu78f1cd02010-03-27 19:35:46 -07002719 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002720 RTL_R32(MAC0);
2721
Francois Romieu773d2022007-01-31 23:47:43 +01002722 RTL_W8(Cfg9346, Cfg9346_Lock);
2723
2724 spin_unlock_irq(&tp->lock);
2725}
2726
2727static int rtl_set_mac_address(struct net_device *dev, void *p)
2728{
2729 struct rtl8169_private *tp = netdev_priv(dev);
2730 struct sockaddr *addr = p;
2731
2732 if (!is_valid_ether_addr(addr->sa_data))
2733 return -EADDRNOTAVAIL;
2734
2735 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2736
2737 rtl_rar_set(tp, dev->dev_addr);
2738
2739 return 0;
2740}
2741
Francois Romieu5f787a12006-08-17 13:02:36 +02002742static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2743{
2744 struct rtl8169_private *tp = netdev_priv(dev);
2745 struct mii_ioctl_data *data = if_mii(ifr);
2746
Francois Romieu8b4ab282008-11-19 22:05:25 -08002747 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2748}
Francois Romieu5f787a12006-08-17 13:02:36 +02002749
Francois Romieu8b4ab282008-11-19 22:05:25 -08002750static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2751{
Francois Romieu5f787a12006-08-17 13:02:36 +02002752 switch (cmd) {
2753 case SIOCGMIIPHY:
2754 data->phy_id = 32; /* Internal PHY */
2755 return 0;
2756
2757 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002758 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002759 return 0;
2760
2761 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002762 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002763 return 0;
2764 }
2765 return -EOPNOTSUPP;
2766}
2767
Francois Romieu8b4ab282008-11-19 22:05:25 -08002768static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2769{
2770 return -EOPNOTSUPP;
2771}
2772
Francois Romieu0e485152007-02-20 00:00:26 +01002773static const struct rtl_cfg_info {
2774 void (*hw_start)(struct net_device *);
2775 unsigned int region;
2776 unsigned int align;
2777 u16 intr_event;
2778 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002779 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002780 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002781} rtl_cfg_infos [] = {
2782 [RTL_CFG_0] = {
2783 .hw_start = rtl_hw_start_8169,
2784 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002785 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002786 .intr_event = SYSErr | LinkChg | RxOverflow |
2787 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002788 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002789 .features = RTL_FEATURE_GMII,
2790 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002791 },
2792 [RTL_CFG_1] = {
2793 .hw_start = rtl_hw_start_8168,
2794 .region = 2,
2795 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002796 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002797 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002798 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002799 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2800 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002801 },
2802 [RTL_CFG_2] = {
2803 .hw_start = rtl_hw_start_8101,
2804 .region = 2,
2805 .align = 8,
2806 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2807 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002808 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002809 .features = RTL_FEATURE_MSI,
2810 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002811 }
2812};
2813
Francois Romieufbac58f2007-10-04 22:51:38 +02002814/* Cfg9346_Unlock assumed. */
2815static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2816 const struct rtl_cfg_info *cfg)
2817{
2818 unsigned msi = 0;
2819 u8 cfg2;
2820
2821 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002822 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002823 if (pci_enable_msi(pdev)) {
2824 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2825 } else {
2826 cfg2 |= MSIEnable;
2827 msi = RTL_FEATURE_MSI;
2828 }
2829 }
2830 RTL_W8(Config2, cfg2);
2831 return msi;
2832}
2833
2834static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2835{
2836 if (tp->features & RTL_FEATURE_MSI) {
2837 pci_disable_msi(pdev);
2838 tp->features &= ~RTL_FEATURE_MSI;
2839 }
2840}
2841
Francois Romieu8b4ab282008-11-19 22:05:25 -08002842static const struct net_device_ops rtl8169_netdev_ops = {
2843 .ndo_open = rtl8169_open,
2844 .ndo_stop = rtl8169_close,
2845 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002846 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002847 .ndo_tx_timeout = rtl8169_tx_timeout,
2848 .ndo_validate_addr = eth_validate_addr,
2849 .ndo_change_mtu = rtl8169_change_mtu,
2850 .ndo_set_mac_address = rtl_set_mac_address,
2851 .ndo_do_ioctl = rtl8169_ioctl,
2852 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002853#ifdef CONFIG_NET_POLL_CONTROLLER
2854 .ndo_poll_controller = rtl8169_netpoll,
2855#endif
2856
2857};
2858
françois romieuc0e45c12011-01-03 15:08:04 +00002859static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2860{
2861 struct mdio_ops *ops = &tp->mdio_ops;
2862
2863 switch (tp->mac_version) {
2864 case RTL_GIGA_MAC_VER_27:
2865 ops->write = r8168dp_1_mdio_write;
2866 ops->read = r8168dp_1_mdio_read;
2867 break;
françois romieue6de30d2011-01-03 15:08:37 +00002868 case RTL_GIGA_MAC_VER_28:
2869 ops->write = r8168dp_2_mdio_write;
2870 ops->read = r8168dp_2_mdio_read;
2871 break;
françois romieuc0e45c12011-01-03 15:08:04 +00002872 default:
2873 ops->write = r8169_mdio_write;
2874 ops->read = r8169_mdio_read;
2875 break;
2876 }
2877}
2878
françois romieu065c27c2011-01-03 15:08:12 +00002879static void r810x_phy_power_down(struct rtl8169_private *tp)
2880{
2881 rtl_writephy(tp, 0x1f, 0x0000);
2882 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2883}
2884
2885static void r810x_phy_power_up(struct rtl8169_private *tp)
2886{
2887 rtl_writephy(tp, 0x1f, 0x0000);
2888 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2889}
2890
2891static void r810x_pll_power_down(struct rtl8169_private *tp)
2892{
2893 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2894 rtl_writephy(tp, 0x1f, 0x0000);
2895 rtl_writephy(tp, MII_BMCR, 0x0000);
2896 return;
2897 }
2898
2899 r810x_phy_power_down(tp);
2900}
2901
2902static void r810x_pll_power_up(struct rtl8169_private *tp)
2903{
2904 r810x_phy_power_up(tp);
2905}
2906
2907static void r8168_phy_power_up(struct rtl8169_private *tp)
2908{
2909 rtl_writephy(tp, 0x1f, 0x0000);
2910 rtl_writephy(tp, 0x0e, 0x0000);
2911 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2912}
2913
2914static void r8168_phy_power_down(struct rtl8169_private *tp)
2915{
2916 rtl_writephy(tp, 0x1f, 0x0000);
2917 rtl_writephy(tp, 0x0e, 0x0200);
2918 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2919}
2920
2921static void r8168_pll_power_down(struct rtl8169_private *tp)
2922{
2923 void __iomem *ioaddr = tp->mmio_addr;
2924
Hayes Wang5d2e1952011-02-22 17:26:22 +08002925 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
2926 (tp->mac_version == RTL_GIGA_MAC_VER_28)) &&
2927 (ocp_read(tp, 0x0f, 0x0010) & 0x00008000)) {
françois romieu065c27c2011-01-03 15:08:12 +00002928 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08002929 }
françois romieu065c27c2011-01-03 15:08:12 +00002930
2931 if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
2932 (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
2933 (RTL_R16(CPlusCmd) & ASF)) {
2934 return;
2935 }
2936
2937 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2938 rtl_writephy(tp, 0x1f, 0x0000);
2939 rtl_writephy(tp, MII_BMCR, 0x0000);
2940
2941 RTL_W32(RxConfig, RTL_R32(RxConfig) |
2942 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
2943 return;
2944 }
2945
2946 r8168_phy_power_down(tp);
2947
2948 switch (tp->mac_version) {
2949 case RTL_GIGA_MAC_VER_25:
2950 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08002951 case RTL_GIGA_MAC_VER_27:
2952 case RTL_GIGA_MAC_VER_28:
françois romieu065c27c2011-01-03 15:08:12 +00002953 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
2954 break;
2955 }
2956}
2957
2958static void r8168_pll_power_up(struct rtl8169_private *tp)
2959{
2960 void __iomem *ioaddr = tp->mmio_addr;
2961
Hayes Wang5d2e1952011-02-22 17:26:22 +08002962 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
2963 (tp->mac_version == RTL_GIGA_MAC_VER_28)) &&
2964 (ocp_read(tp, 0x0f, 0x0010) & 0x00008000)) {
françois romieu065c27c2011-01-03 15:08:12 +00002965 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08002966 }
françois romieu065c27c2011-01-03 15:08:12 +00002967
2968 switch (tp->mac_version) {
2969 case RTL_GIGA_MAC_VER_25:
2970 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08002971 case RTL_GIGA_MAC_VER_27:
2972 case RTL_GIGA_MAC_VER_28:
françois romieu065c27c2011-01-03 15:08:12 +00002973 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
2974 break;
2975 }
2976
2977 r8168_phy_power_up(tp);
2978}
2979
2980static void rtl_pll_power_op(struct rtl8169_private *tp,
2981 void (*op)(struct rtl8169_private *))
2982{
2983 if (op)
2984 op(tp);
2985}
2986
2987static void rtl_pll_power_down(struct rtl8169_private *tp)
2988{
2989 rtl_pll_power_op(tp, tp->pll_power_ops.down);
2990}
2991
2992static void rtl_pll_power_up(struct rtl8169_private *tp)
2993{
2994 rtl_pll_power_op(tp, tp->pll_power_ops.up);
2995}
2996
2997static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
2998{
2999 struct pll_power_ops *ops = &tp->pll_power_ops;
3000
3001 switch (tp->mac_version) {
3002 case RTL_GIGA_MAC_VER_07:
3003 case RTL_GIGA_MAC_VER_08:
3004 case RTL_GIGA_MAC_VER_09:
3005 case RTL_GIGA_MAC_VER_10:
3006 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003007 case RTL_GIGA_MAC_VER_29:
3008 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003009 ops->down = r810x_pll_power_down;
3010 ops->up = r810x_pll_power_up;
3011 break;
3012
3013 case RTL_GIGA_MAC_VER_11:
3014 case RTL_GIGA_MAC_VER_12:
3015 case RTL_GIGA_MAC_VER_17:
3016 case RTL_GIGA_MAC_VER_18:
3017 case RTL_GIGA_MAC_VER_19:
3018 case RTL_GIGA_MAC_VER_20:
3019 case RTL_GIGA_MAC_VER_21:
3020 case RTL_GIGA_MAC_VER_22:
3021 case RTL_GIGA_MAC_VER_23:
3022 case RTL_GIGA_MAC_VER_24:
3023 case RTL_GIGA_MAC_VER_25:
3024 case RTL_GIGA_MAC_VER_26:
3025 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003026 case RTL_GIGA_MAC_VER_28:
françois romieu065c27c2011-01-03 15:08:12 +00003027 ops->down = r8168_pll_power_down;
3028 ops->up = r8168_pll_power_up;
3029 break;
3030
3031 default:
3032 ops->down = NULL;
3033 ops->up = NULL;
3034 break;
3035 }
3036}
3037
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003038static int __devinit
3039rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3040{
Francois Romieu0e485152007-02-20 00:00:26 +01003041 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3042 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003044 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003045 struct net_device *dev;
3046 void __iomem *ioaddr;
Francois Romieu07d3f512007-02-21 22:40:46 +01003047 unsigned int i;
3048 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003050 if (netif_msg_drv(&debug)) {
3051 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3052 MODULENAME, RTL8169_VERSION);
3053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003056 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003057 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003058 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003059 rc = -ENOMEM;
3060 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003064 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003066 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003067 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003068 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Francois Romieuccdffb92008-07-26 14:26:06 +02003070 mii = &tp->mii;
3071 mii->dev = dev;
3072 mii->mdio_read = rtl_mdio_read;
3073 mii->mdio_write = rtl_mdio_write;
3074 mii->phy_id_mask = 0x1f;
3075 mii->reg_num_mask = 0x1f;
3076 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3077
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003078 /* disable ASPM completely as that cause random device stop working
3079 * problems as well as full system hangs for some PCIe devices users */
3080 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3081 PCIE_LINK_STATE_CLKPM);
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3084 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003085 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003086 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003087 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
3089
françois romieu87aeec72010-04-26 11:42:06 +00003090 if (pci_set_mwi(pdev) < 0)
3091 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003094 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003095 netif_err(tp, probe, dev,
3096 "region #%d not an MMIO resource, aborting\n",
3097 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003099 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003103 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003104 netif_err(tp, probe, dev,
3105 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003107 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
3109
3110 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003111 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003112 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003113 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
3115
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003116 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003119 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 tp->cp_cmd |= PCIDAC;
3121 dev->features |= NETIF_F_HIGHDMA;
3122 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003123 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003125 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003126 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
3128 }
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003131 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003132 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003133 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003135 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 }
3137
David S. Miller4300e8c2010-03-26 10:23:30 -07003138 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3139 if (!tp->pcie_cap)
3140 netif_info(tp, probe, dev, "no PCI Express capability\n");
3141
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003142 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
3144 /* Soft reset the chip. */
3145 RTL_W8(ChipCmd, CmdReset);
3146
3147 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003148 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3150 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003151 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
3153
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003154 RTL_W16(IntrStatus, 0xffff);
3155
françois romieuca52efd2009-07-24 12:34:19 +00003156 pci_set_master(pdev);
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 /* Identify chip attached to board */
3159 rtl8169_get_mac_version(tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Francois Romieu7a8fc772011-03-01 17:18:33 +01003161 /*
3162 * Pretend we are using VLANs; This bypasses a nasty bug where
3163 * Interrupts stop flowing on high load on 8110SCd controllers.
3164 */
3165 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3166 tp->cp_cmd |= RxVlan;
3167
françois romieuc0e45c12011-01-03 15:08:04 +00003168 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003169 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003170
Jean Delvaref21b75e2009-05-26 20:54:48 -07003171 /* Use appropriate default if unknown */
3172 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003173 netif_notice(tp, probe, dev,
3174 "unknown MAC, using family default\n");
Jean Delvaref21b75e2009-05-26 20:54:48 -07003175 tp->mac_version = cfg->default_ver;
3176 }
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Roel Kluincee60c32008-04-17 22:35:54 +02003180 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 if (tp->mac_version == rtl_chip_info[i].mac_version)
3182 break;
3183 }
Roel Kluincee60c32008-04-17 22:35:54 +02003184 if (i == ARRAY_SIZE(rtl_chip_info)) {
Jean Delvaref21b75e2009-05-26 20:54:48 -07003185 dev_err(&pdev->dev,
3186 "driver bug, MAC version not found in rtl_chip_info\n");
françois romieu87aeec72010-04-26 11:42:06 +00003187 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
3189 tp->chipset = i;
3190
Francois Romieu5d06a992006-02-23 00:47:58 +01003191 RTL_W8(Cfg9346, Cfg9346_Unlock);
3192 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3193 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003194 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3195 tp->features |= RTL_FEATURE_WOL;
3196 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3197 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003198 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003199 RTL_W8(Cfg9346, Cfg9346_Lock);
3200
Francois Romieu66ec5d42007-11-06 22:56:10 +01003201 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3202 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 tp->set_speed = rtl8169_set_speed_tbi;
3204 tp->get_settings = rtl8169_gset_tbi;
3205 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3206 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3207 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003208 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Francois Romieu64e4bfb2006-08-17 12:43:06 +02003210 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 } else {
3212 tp->set_speed = rtl8169_set_speed_xmii;
3213 tp->get_settings = rtl8169_gset_xmii;
3214 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3215 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3216 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003217 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
3219
Francois Romieudf58ef52008-10-09 14:35:58 -07003220 spin_lock_init(&tp->lock);
3221
Petr Vandrovec738e1e62008-10-12 20:58:29 -07003222 tp->mmio_addr = ioaddr;
3223
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003224 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 for (i = 0; i < MAC_ADDR_LEN; i++)
3226 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003227 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3231 dev->irq = pdev->irq;
3232 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003234 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Francois Romieu7a8fc772011-03-01 17:18:33 +01003236 dev->features |= NETIF_F_HW_VLAN_TX_RX | NETIF_F_GRO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
3238 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003239 tp->hw_start = cfg->hw_start;
3240 tp->intr_event = cfg->intr_event;
3241 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Francois Romieu2efa53f2007-03-09 00:00:05 +01003243 init_timer(&tp->timer);
3244 tp->timer.data = (unsigned long) dev;
3245 tp->timer.function = rtl8169_phy_timer;
3246
François Romieu953a12c2011-04-24 17:38:48 +02003247 tp->fw = RTL_FIRMWARE_UNKNOWN;
3248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003250 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003251 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 pci_set_drvdata(pdev, dev);
3254
Joe Perchesbf82c182010-02-09 11:49:50 +00003255 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
3256 rtl_chip_info[tp->chipset].name,
3257 dev->base_addr, dev->dev_addr,
3258 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
françois romieue6de30d2011-01-03 15:08:37 +00003260 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3261 (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
françois romieub646d902011-01-03 15:08:21 +00003262 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003263 }
françois romieub646d902011-01-03 15:08:21 +00003264
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003265 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Alan Sternf3ec4f82010-06-08 15:23:51 -04003267 if (pci_dev_run_wake(pdev))
3268 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003269
Ivan Vecera0d672e92011-02-15 02:08:39 +00003270 netif_carrier_off(dev);
3271
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003272out:
3273 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
françois romieu87aeec72010-04-26 11:42:06 +00003275err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003276 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003277 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003278err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003279 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003280err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003281 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003282 pci_disable_device(pdev);
3283err_out_free_dev_1:
3284 free_netdev(dev);
3285 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
Francois Romieu07d3f512007-02-21 22:40:46 +01003288static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
3290 struct net_device *dev = pci_get_drvdata(pdev);
3291 struct rtl8169_private *tp = netdev_priv(dev);
3292
françois romieue6de30d2011-01-03 15:08:37 +00003293 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
3294 (tp->mac_version == RTL_GIGA_MAC_VER_28)) {
françois romieub646d902011-01-03 15:08:21 +00003295 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003296 }
françois romieub646d902011-01-03 15:08:21 +00003297
Tejun Heo23f333a2010-12-12 16:45:14 +01003298 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003299
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003301
François Romieu953a12c2011-04-24 17:38:48 +02003302 rtl_release_firmware(tp);
3303
Alan Sternf3ec4f82010-06-08 15:23:51 -04003304 if (pci_dev_run_wake(pdev))
3305 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003306
Ivan Veceracc098dc2009-11-29 23:12:52 -08003307 /* restore original MAC address */
3308 rtl_rar_set(tp, dev->perm_addr);
3309
Francois Romieufbac58f2007-10-04 22:51:38 +02003310 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3312 pci_set_drvdata(pdev, NULL);
3313}
3314
François Romieu953a12c2011-04-24 17:38:48 +02003315static void rtl_request_firmware(struct rtl8169_private *tp)
3316{
3317 int i;
3318
3319 /* Return early if the firmware is already loaded / cached. */
3320 if (!IS_ERR(tp->fw))
3321 goto out;
3322
3323 for (i = 0; i < ARRAY_SIZE(rtl_firmware_infos); i++) {
3324 const struct rtl_firmware_info *info = rtl_firmware_infos + i;
3325
3326 if (info->mac_version == tp->mac_version) {
3327 const char *name = info->fw_name;
3328 int rc;
3329
3330 rc = request_firmware(&tp->fw, name, &tp->pci_dev->dev);
3331 if (rc < 0) {
3332 netif_warn(tp, ifup, tp->dev, "unable to load "
3333 "firmware patch %s (%d)\n", name, rc);
3334 goto out_disable_request_firmware;
3335 }
3336 goto out;
3337 }
3338 }
3339
3340out_disable_request_firmware:
3341 tp->fw = NULL;
3342out:
3343 return;
3344}
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346static int rtl8169_open(struct net_device *dev)
3347{
3348 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003349 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003351 int retval = -ENOMEM;
3352
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003353 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
Neil Hormanc0cd8842010-03-29 13:16:02 -07003355 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003357 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003359 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3360 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003362 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003364 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3365 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003367 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
3369 retval = rtl8169_init_ring(dev);
3370 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003371 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
David Howellsc4028952006-11-22 14:57:56 +00003373 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Francois Romieu99f252b2007-04-02 22:59:59 +02003375 smp_mb();
3376
François Romieu953a12c2011-04-24 17:38:48 +02003377 rtl_request_firmware(tp);
3378
Francois Romieufbac58f2007-10-04 22:51:38 +02003379 retval = request_irq(dev->irq, rtl8169_interrupt,
3380 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003381 dev->name, dev);
3382 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003383 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003384
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003385 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003386
françois romieueee3a962011-01-08 02:17:26 +00003387 rtl8169_init_phy(dev, tp);
3388
Francois Romieu7a8fc772011-03-01 17:18:33 +01003389 rtl8169_vlan_mode(dev);
françois romieueee3a962011-01-08 02:17:26 +00003390
françois romieu065c27c2011-01-03 15:08:12 +00003391 rtl_pll_power_up(tp);
3392
Francois Romieu07ce4062007-02-23 23:36:39 +01003393 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
3395 rtl8169_request_timer(dev);
3396
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003397 tp->saved_wolopts = 0;
3398 pm_runtime_put_noidle(&pdev->dev);
3399
françois romieueee3a962011-01-08 02:17:26 +00003400 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401out:
3402 return retval;
3403
François Romieu953a12c2011-04-24 17:38:48 +02003404err_release_fw_2:
3405 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003406 rtl8169_rx_clear(tp);
3407err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003408 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3409 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003410 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003411err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003412 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3413 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003414 tp->TxDescArray = NULL;
3415err_pm_runtime_put:
3416 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 goto out;
3418}
3419
françois romieue6de30d2011-01-03 15:08:37 +00003420static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
françois romieue6de30d2011-01-03 15:08:37 +00003422 void __iomem *ioaddr = tp->mmio_addr;
3423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 /* Disable interrupts */
3425 rtl8169_irq_mask_and_ack(ioaddr);
3426
Hayes Wang5d2e1952011-02-22 17:26:22 +08003427 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3428 tp->mac_version == RTL_GIGA_MAC_VER_28) {
françois romieue6de30d2011-01-03 15:08:37 +00003429 while (RTL_R8(TxPoll) & NPQ)
3430 udelay(20);
3431
3432 }
3433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 /* Reset the chipset */
3435 RTL_W8(ChipCmd, CmdReset);
3436
3437 /* PCI commit */
3438 RTL_R8(ChipCmd);
3439}
3440
Francois Romieu7f796d82007-06-11 23:04:41 +02003441static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003442{
3443 void __iomem *ioaddr = tp->mmio_addr;
3444 u32 cfg = rtl8169_rx_config;
3445
3446 cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
3447 RTL_W32(RxConfig, cfg);
3448
3449 /* Set DMA burst size and Interframe Gap Time */
3450 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3451 (InterFrameGap << TxInterFrameGapShift));
3452}
3453
Francois Romieu07ce4062007-02-23 23:36:39 +01003454static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455{
3456 struct rtl8169_private *tp = netdev_priv(dev);
3457 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01003458 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
3460 /* Soft reset the chip. */
3461 RTL_W8(ChipCmd, CmdReset);
3462
3463 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003464 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3466 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003467 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
3469
Francois Romieu07ce4062007-02-23 23:36:39 +01003470 tp->hw_start(dev);
3471
Francois Romieu07ce4062007-02-23 23:36:39 +01003472 netif_start_queue(dev);
3473}
3474
3475
Francois Romieu7f796d82007-06-11 23:04:41 +02003476static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3477 void __iomem *ioaddr)
3478{
3479 /*
3480 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3481 * register to be written before TxDescAddrLow to work.
3482 * Switching from MMIO to I/O access fixes the issue as well.
3483 */
3484 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003485 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003486 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003487 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003488}
3489
3490static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3491{
3492 u16 cmd;
3493
3494 cmd = RTL_R16(CPlusCmd);
3495 RTL_W16(CPlusCmd, cmd);
3496 return cmd;
3497}
3498
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003499static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003500{
3501 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003502 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003503}
3504
Francois Romieu6dccd162007-02-13 23:38:05 +01003505static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3506{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003507 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003508 u32 mac_version;
3509 u32 clk;
3510 u32 val;
3511 } cfg2_info [] = {
3512 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3513 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3514 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3515 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3516 }, *p = cfg2_info;
3517 unsigned int i;
3518 u32 clk;
3519
3520 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003521 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003522 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3523 RTL_W32(0x7c, p->val);
3524 break;
3525 }
3526 }
3527}
3528
Francois Romieu07ce4062007-02-23 23:36:39 +01003529static void rtl_hw_start_8169(struct net_device *dev)
3530{
3531 struct rtl8169_private *tp = netdev_priv(dev);
3532 void __iomem *ioaddr = tp->mmio_addr;
3533 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003534
Francois Romieu9cb427b2006-11-02 00:10:16 +01003535 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3536 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3537 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3538 }
3539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003541 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3542 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3543 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3544 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3545 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3546
françois romieuf0298f82011-01-03 15:07:42 +00003547 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003549 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Francois Romieuc946b302007-10-04 00:42:50 +02003551 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3552 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3553 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3554 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3555 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Francois Romieu7f796d82007-06-11 23:04:41 +02003557 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003558
3559 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3560 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
Joe Perches06fa7352007-10-18 21:15:00 +02003561 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003563 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
3565
Francois Romieubcf0bf92006-07-26 23:14:13 +02003566 RTL_W16(CPlusCmd, tp->cp_cmd);
3567
Francois Romieu6dccd162007-02-13 23:38:05 +01003568 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 /*
3571 * Undocumented corner. Supposedly:
3572 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3573 */
3574 RTL_W16(IntrMitigate, 0x0000);
3575
Francois Romieu7f796d82007-06-11 23:04:41 +02003576 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003577
Francois Romieuc946b302007-10-04 00:42:50 +02003578 if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
3579 (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
3580 (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
3581 (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
3582 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3583 rtl_set_rx_tx_config_registers(tp);
3584 }
3585
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003587
3588 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3589 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591 RTL_W32(RxMissed, 0);
3592
Francois Romieu07ce4062007-02-23 23:36:39 +01003593 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
3595 /* no early-rx interrupts */
3596 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003597
3598 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003599 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003600}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Francois Romieu9c14cea2008-07-05 00:21:15 +02003602static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003603{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003604 struct net_device *dev = pci_get_drvdata(pdev);
3605 struct rtl8169_private *tp = netdev_priv(dev);
3606 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003607
Francois Romieu9c14cea2008-07-05 00:21:15 +02003608 if (cap) {
3609 u16 ctl;
3610
3611 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3612 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3613 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3614 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003615}
3616
françois romieu650e8d52011-01-03 15:08:29 +00003617static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003618{
3619 u32 csi;
3620
3621 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003622 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3623}
3624
françois romieue6de30d2011-01-03 15:08:37 +00003625static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3626{
3627 rtl_csi_access_enable(ioaddr, 0x17000000);
3628}
3629
françois romieu650e8d52011-01-03 15:08:29 +00003630static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3631{
3632 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003633}
3634
3635struct ephy_info {
3636 unsigned int offset;
3637 u16 mask;
3638 u16 bits;
3639};
3640
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003641static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003642{
3643 u16 w;
3644
3645 while (len-- > 0) {
3646 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3647 rtl_ephy_write(ioaddr, e->offset, w);
3648 e++;
3649 }
3650}
3651
Francois Romieub726e492008-06-28 12:22:59 +02003652static void rtl_disable_clock_request(struct pci_dev *pdev)
3653{
3654 struct net_device *dev = pci_get_drvdata(pdev);
3655 struct rtl8169_private *tp = netdev_priv(dev);
3656 int cap = tp->pcie_cap;
3657
3658 if (cap) {
3659 u16 ctl;
3660
3661 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3662 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3663 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3664 }
3665}
3666
françois romieue6de30d2011-01-03 15:08:37 +00003667static void rtl_enable_clock_request(struct pci_dev *pdev)
3668{
3669 struct net_device *dev = pci_get_drvdata(pdev);
3670 struct rtl8169_private *tp = netdev_priv(dev);
3671 int cap = tp->pcie_cap;
3672
3673 if (cap) {
3674 u16 ctl;
3675
3676 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3677 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3678 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3679 }
3680}
3681
Francois Romieub726e492008-06-28 12:22:59 +02003682#define R8168_CPCMD_QUIRK_MASK (\
3683 EnableBist | \
3684 Mac_dbgo_oe | \
3685 Force_half_dup | \
3686 Force_rxflow_en | \
3687 Force_txflow_en | \
3688 Cxpl_dbg_sel | \
3689 ASF | \
3690 PktCntrDisable | \
3691 Mac_dbgo_sel)
3692
Francois Romieu219a1e92008-06-28 11:58:39 +02003693static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3694{
Francois Romieub726e492008-06-28 12:22:59 +02003695 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3696
3697 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3698
Francois Romieu2e68ae42008-06-28 12:00:55 +02003699 rtl_tx_performance_tweak(pdev,
3700 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003701}
3702
3703static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3704{
3705 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003706
françois romieuf0298f82011-01-03 15:07:42 +00003707 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003708
3709 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003710}
3711
3712static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3713{
Francois Romieub726e492008-06-28 12:22:59 +02003714 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3715
3716 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3717
Francois Romieu219a1e92008-06-28 11:58:39 +02003718 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003719
3720 rtl_disable_clock_request(pdev);
3721
3722 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003723}
3724
Francois Romieuef3386f2008-06-29 12:24:30 +02003725static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003726{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003727 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003728 { 0x01, 0, 0x0001 },
3729 { 0x02, 0x0800, 0x1000 },
3730 { 0x03, 0, 0x0042 },
3731 { 0x06, 0x0080, 0x0000 },
3732 { 0x07, 0, 0x2000 }
3733 };
3734
françois romieu650e8d52011-01-03 15:08:29 +00003735 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003736
3737 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3738
Francois Romieu219a1e92008-06-28 11:58:39 +02003739 __rtl_hw_start_8168cp(ioaddr, pdev);
3740}
3741
Francois Romieuef3386f2008-06-29 12:24:30 +02003742static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3743{
françois romieu650e8d52011-01-03 15:08:29 +00003744 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003745
3746 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3747
3748 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3749
3750 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3751}
3752
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003753static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3754{
françois romieu650e8d52011-01-03 15:08:29 +00003755 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003756
3757 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3758
3759 /* Magic. */
3760 RTL_W8(DBG_REG, 0x20);
3761
françois romieuf0298f82011-01-03 15:07:42 +00003762 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003763
3764 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3765
3766 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3767}
3768
Francois Romieu219a1e92008-06-28 11:58:39 +02003769static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3770{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003771 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003772 { 0x02, 0x0800, 0x1000 },
3773 { 0x03, 0, 0x0002 },
3774 { 0x06, 0x0080, 0x0000 }
3775 };
3776
françois romieu650e8d52011-01-03 15:08:29 +00003777 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003778
3779 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3780
3781 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3782
Francois Romieu219a1e92008-06-28 11:58:39 +02003783 __rtl_hw_start_8168cp(ioaddr, pdev);
3784}
3785
3786static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3787{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003788 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003789 { 0x01, 0, 0x0001 },
3790 { 0x03, 0x0400, 0x0220 }
3791 };
3792
françois romieu650e8d52011-01-03 15:08:29 +00003793 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003794
3795 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3796
Francois Romieu219a1e92008-06-28 11:58:39 +02003797 __rtl_hw_start_8168cp(ioaddr, pdev);
3798}
3799
Francois Romieu197ff762008-06-28 13:16:02 +02003800static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3801{
3802 rtl_hw_start_8168c_2(ioaddr, pdev);
3803}
3804
Francois Romieu6fb07052008-06-29 11:54:28 +02003805static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3806{
françois romieu650e8d52011-01-03 15:08:29 +00003807 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02003808
3809 __rtl_hw_start_8168cp(ioaddr, pdev);
3810}
3811
Francois Romieu5b538df2008-07-20 16:22:45 +02003812static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3813{
françois romieu650e8d52011-01-03 15:08:29 +00003814 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02003815
3816 rtl_disable_clock_request(pdev);
3817
françois romieuf0298f82011-01-03 15:07:42 +00003818 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02003819
3820 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3821
3822 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3823}
3824
françois romieue6de30d2011-01-03 15:08:37 +00003825static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
3826{
3827 static const struct ephy_info e_info_8168d_4[] = {
3828 { 0x0b, ~0, 0x48 },
3829 { 0x19, 0x20, 0x50 },
3830 { 0x0c, ~0, 0x20 }
3831 };
3832 int i;
3833
3834 rtl_csi_access_enable_1(ioaddr);
3835
3836 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3837
3838 RTL_W8(MaxTxPacketSize, TxPacketMax);
3839
3840 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
3841 const struct ephy_info *e = e_info_8168d_4 + i;
3842 u16 w;
3843
3844 w = rtl_ephy_read(ioaddr, e->offset);
3845 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
3846 }
3847
3848 rtl_enable_clock_request(pdev);
3849}
3850
Francois Romieu07ce4062007-02-23 23:36:39 +01003851static void rtl_hw_start_8168(struct net_device *dev)
3852{
Francois Romieu2dd99532007-06-11 23:22:52 +02003853 struct rtl8169_private *tp = netdev_priv(dev);
3854 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01003855 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02003856
3857 RTL_W8(Cfg9346, Cfg9346_Unlock);
3858
françois romieuf0298f82011-01-03 15:07:42 +00003859 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02003860
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003861 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02003862
Francois Romieu0e485152007-02-20 00:00:26 +01003863 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02003864
3865 RTL_W16(CPlusCmd, tp->cp_cmd);
3866
Francois Romieu0e485152007-02-20 00:00:26 +01003867 RTL_W16(IntrMitigate, 0x5151);
3868
3869 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01003870 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
3871 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01003872 tp->intr_event |= RxFIFOOver | PCSTimeout;
3873 tp->intr_event &= ~RxOverflow;
3874 }
Francois Romieu2dd99532007-06-11 23:22:52 +02003875
3876 rtl_set_rx_tx_desc_registers(tp, ioaddr);
3877
Francois Romieub8363902008-06-01 12:31:57 +02003878 rtl_set_rx_mode(dev);
3879
3880 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3881 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02003882
3883 RTL_R8(IntrMask);
3884
Francois Romieu219a1e92008-06-28 11:58:39 +02003885 switch (tp->mac_version) {
3886 case RTL_GIGA_MAC_VER_11:
3887 rtl_hw_start_8168bb(ioaddr, pdev);
3888 break;
3889
3890 case RTL_GIGA_MAC_VER_12:
3891 case RTL_GIGA_MAC_VER_17:
3892 rtl_hw_start_8168bef(ioaddr, pdev);
3893 break;
3894
3895 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02003896 rtl_hw_start_8168cp_1(ioaddr, pdev);
Francois Romieu219a1e92008-06-28 11:58:39 +02003897 break;
3898
3899 case RTL_GIGA_MAC_VER_19:
3900 rtl_hw_start_8168c_1(ioaddr, pdev);
3901 break;
3902
3903 case RTL_GIGA_MAC_VER_20:
3904 rtl_hw_start_8168c_2(ioaddr, pdev);
3905 break;
3906
Francois Romieu197ff762008-06-28 13:16:02 +02003907 case RTL_GIGA_MAC_VER_21:
3908 rtl_hw_start_8168c_3(ioaddr, pdev);
3909 break;
3910
Francois Romieu6fb07052008-06-29 11:54:28 +02003911 case RTL_GIGA_MAC_VER_22:
3912 rtl_hw_start_8168c_4(ioaddr, pdev);
3913 break;
3914
Francois Romieuef3386f2008-06-29 12:24:30 +02003915 case RTL_GIGA_MAC_VER_23:
3916 rtl_hw_start_8168cp_2(ioaddr, pdev);
3917 break;
3918
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003919 case RTL_GIGA_MAC_VER_24:
3920 rtl_hw_start_8168cp_3(ioaddr, pdev);
3921 break;
3922
Francois Romieu5b538df2008-07-20 16:22:45 +02003923 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00003924 case RTL_GIGA_MAC_VER_26:
3925 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02003926 rtl_hw_start_8168d(ioaddr, pdev);
3927 break;
3928
françois romieue6de30d2011-01-03 15:08:37 +00003929 case RTL_GIGA_MAC_VER_28:
3930 rtl_hw_start_8168d_4(ioaddr, pdev);
3931 break;
3932
Francois Romieu219a1e92008-06-28 11:58:39 +02003933 default:
3934 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
3935 dev->name, tp->mac_version);
3936 break;
3937 }
Francois Romieu2dd99532007-06-11 23:22:52 +02003938
Francois Romieu0e485152007-02-20 00:00:26 +01003939 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3940
Francois Romieub8363902008-06-01 12:31:57 +02003941 RTL_W8(Cfg9346, Cfg9346_Lock);
3942
Francois Romieu2dd99532007-06-11 23:22:52 +02003943 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003944
Francois Romieu0e485152007-02-20 00:00:26 +01003945 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003946}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
Francois Romieu2857ffb2008-08-02 21:08:49 +02003948#define R810X_CPCMD_QUIRK_MASK (\
3949 EnableBist | \
3950 Mac_dbgo_oe | \
3951 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00003952 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02003953 Force_txflow_en | \
3954 Cxpl_dbg_sel | \
3955 ASF | \
3956 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003957 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02003958
3959static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
3960{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003961 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02003962 { 0x01, 0, 0x6e65 },
3963 { 0x02, 0, 0x091f },
3964 { 0x03, 0, 0xc2f9 },
3965 { 0x06, 0, 0xafb5 },
3966 { 0x07, 0, 0x0e00 },
3967 { 0x19, 0, 0xec80 },
3968 { 0x01, 0, 0x2e65 },
3969 { 0x01, 0, 0x6e65 }
3970 };
3971 u8 cfg1;
3972
françois romieu650e8d52011-01-03 15:08:29 +00003973 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003974
3975 RTL_W8(DBG_REG, FIX_NAK_1);
3976
3977 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3978
3979 RTL_W8(Config1,
3980 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
3981 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3982
3983 cfg1 = RTL_R8(Config1);
3984 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
3985 RTL_W8(Config1, cfg1 & ~LEDS0);
3986
Francois Romieu2857ffb2008-08-02 21:08:49 +02003987 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
3988}
3989
3990static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
3991{
françois romieu650e8d52011-01-03 15:08:29 +00003992 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003993
3994 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3995
3996 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
3997 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02003998}
3999
4000static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4001{
4002 rtl_hw_start_8102e_2(ioaddr, pdev);
4003
4004 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4005}
4006
Hayes Wang5a5e4442011-02-22 17:26:21 +08004007static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4008{
4009 static const struct ephy_info e_info_8105e_1[] = {
4010 { 0x07, 0, 0x4000 },
4011 { 0x19, 0, 0x0200 },
4012 { 0x19, 0, 0x0020 },
4013 { 0x1e, 0, 0x2000 },
4014 { 0x03, 0, 0x0001 },
4015 { 0x19, 0, 0x0100 },
4016 { 0x19, 0, 0x0004 },
4017 { 0x0a, 0, 0x0020 }
4018 };
4019
4020 /* Force LAN exit from ASPM if Rx/Tx are not idel */
4021 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4022
4023 /* disable Early Tally Counter */
4024 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4025
4026 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4027 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4028
4029 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4030}
4031
4032static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4033{
4034 rtl_hw_start_8105e_1(ioaddr, pdev);
4035 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4036}
4037
Francois Romieu07ce4062007-02-23 23:36:39 +01004038static void rtl_hw_start_8101(struct net_device *dev)
4039{
Francois Romieucdf1a602007-06-11 23:29:50 +02004040 struct rtl8169_private *tp = netdev_priv(dev);
4041 void __iomem *ioaddr = tp->mmio_addr;
4042 struct pci_dev *pdev = tp->pci_dev;
4043
Francois Romieue3cf0cc2007-08-17 14:55:46 +02004044 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
4045 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02004046 int cap = tp->pcie_cap;
4047
4048 if (cap) {
4049 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4050 PCI_EXP_DEVCTL_NOSNOOP_EN);
4051 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004052 }
4053
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004054 RTL_W8(Cfg9346, Cfg9346_Unlock);
4055
Francois Romieu2857ffb2008-08-02 21:08:49 +02004056 switch (tp->mac_version) {
4057 case RTL_GIGA_MAC_VER_07:
4058 rtl_hw_start_8102e_1(ioaddr, pdev);
4059 break;
4060
4061 case RTL_GIGA_MAC_VER_08:
4062 rtl_hw_start_8102e_3(ioaddr, pdev);
4063 break;
4064
4065 case RTL_GIGA_MAC_VER_09:
4066 rtl_hw_start_8102e_2(ioaddr, pdev);
4067 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004068
4069 case RTL_GIGA_MAC_VER_29:
4070 rtl_hw_start_8105e_1(ioaddr, pdev);
4071 break;
4072 case RTL_GIGA_MAC_VER_30:
4073 rtl_hw_start_8105e_2(ioaddr, pdev);
4074 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004075 }
4076
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004077 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004078
françois romieuf0298f82011-01-03 15:07:42 +00004079 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004080
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004081 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004082
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004083 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004084 RTL_W16(CPlusCmd, tp->cp_cmd);
4085
4086 RTL_W16(IntrMitigate, 0x0000);
4087
4088 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4089
4090 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4091 rtl_set_rx_tx_config_registers(tp);
4092
Francois Romieucdf1a602007-06-11 23:29:50 +02004093 RTL_R8(IntrMask);
4094
Francois Romieucdf1a602007-06-11 23:29:50 +02004095 rtl_set_rx_mode(dev);
4096
4097 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004098
Francois Romieu0e485152007-02-20 00:00:26 +01004099 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100}
4101
4102static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4105 return -EINVAL;
4106
4107 dev->mtu = new_mtu;
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
4110
4111static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4112{
Al Viro95e09182007-12-22 18:55:39 +00004113 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4115}
4116
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004117static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4118 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004120 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004121 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004122
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004123 kfree(*data_buff);
4124 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 rtl8169_make_unusable_by_asic(desc);
4126}
4127
4128static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4129{
4130 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4131
4132 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4133}
4134
4135static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4136 u32 rx_buf_sz)
4137{
4138 desc->addr = cpu_to_le64(mapping);
4139 wmb();
4140 rtl8169_mark_to_asic(desc, rx_buf_sz);
4141}
4142
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004143static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004145 return (void *)ALIGN((long)data, 16);
4146}
4147
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004148static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4149 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004150{
4151 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004153 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004154 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004155 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004157 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4158 if (!data)
4159 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004160
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004161 if (rtl8169_align(data) != data) {
4162 kfree(data);
4163 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4164 if (!data)
4165 return NULL;
4166 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004167
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004168 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004169 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004170 if (unlikely(dma_mapping_error(d, mapping))) {
4171 if (net_ratelimit())
4172 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004173 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
4176 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004177 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004178
4179err_out:
4180 kfree(data);
4181 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
4183
4184static void rtl8169_rx_clear(struct rtl8169_private *tp)
4185{
Francois Romieu07d3f512007-02-21 22:40:46 +01004186 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
4188 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004189 if (tp->Rx_databuff[i]) {
4190 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 tp->RxDescArray + i);
4192 }
4193 }
4194}
4195
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004196static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004198 desc->opts1 |= cpu_to_le32(RingEnd);
4199}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004200
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004201static int rtl8169_rx_fill(struct rtl8169_private *tp)
4202{
4203 unsigned int i;
4204
4205 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004206 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004207
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004208 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004210
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004211 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004212 if (!data) {
4213 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004214 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004215 }
4216 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004219 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4220 return 0;
4221
4222err_out:
4223 rtl8169_rx_clear(tp);
4224 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225}
4226
4227static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4228{
4229 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4230}
4231
4232static int rtl8169_init_ring(struct net_device *dev)
4233{
4234 struct rtl8169_private *tp = netdev_priv(dev);
4235
4236 rtl8169_init_ring_indexes(tp);
4237
4238 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004239 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004241 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
4243
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004244static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 struct TxDesc *desc)
4246{
4247 unsigned int len = tx_skb->len;
4248
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004249 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 desc->opts1 = 0x00;
4252 desc->opts2 = 0x00;
4253 desc->addr = 0x00;
4254 tx_skb->len = 0;
4255}
4256
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004257static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4258 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259{
4260 unsigned int i;
4261
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004262 for (i = 0; i < n; i++) {
4263 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 struct ring_info *tx_skb = tp->tx_skb + entry;
4265 unsigned int len = tx_skb->len;
4266
4267 if (len) {
4268 struct sk_buff *skb = tx_skb->skb;
4269
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004270 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 tp->TxDescArray + entry);
4272 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004273 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 dev_kfree_skb(skb);
4275 tx_skb->skb = NULL;
4276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 }
4278 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004279}
4280
4281static void rtl8169_tx_clear(struct rtl8169_private *tp)
4282{
4283 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 tp->cur_tx = tp->dirty_tx = 0;
4285}
4286
David Howellsc4028952006-11-22 14:57:56 +00004287static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
4289 struct rtl8169_private *tp = netdev_priv(dev);
4290
David Howellsc4028952006-11-22 14:57:56 +00004291 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 schedule_delayed_work(&tp->task, 4);
4293}
4294
4295static void rtl8169_wait_for_quiescence(struct net_device *dev)
4296{
4297 struct rtl8169_private *tp = netdev_priv(dev);
4298 void __iomem *ioaddr = tp->mmio_addr;
4299
4300 synchronize_irq(dev->irq);
4301
4302 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004303 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
4305 rtl8169_irq_mask_and_ack(ioaddr);
4306
David S. Millerd1d08d12008-01-07 20:53:33 -08004307 tp->intr_mask = 0xffff;
4308 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004309 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310}
4311
David Howellsc4028952006-11-22 14:57:56 +00004312static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
David Howellsc4028952006-11-22 14:57:56 +00004314 struct rtl8169_private *tp =
4315 container_of(work, struct rtl8169_private, task.work);
4316 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 int ret;
4318
Francois Romieueb2a0212007-02-15 23:37:21 +01004319 rtnl_lock();
4320
4321 if (!netif_running(dev))
4322 goto out_unlock;
4323
4324 rtl8169_wait_for_quiescence(dev);
4325 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
4327 ret = rtl8169_open(dev);
4328 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004329 if (net_ratelimit())
4330 netif_err(tp, drv, dev,
4331 "reinit failure (status = %d). Rescheduling\n",
4332 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4334 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004335
4336out_unlock:
4337 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338}
4339
David Howellsc4028952006-11-22 14:57:56 +00004340static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341{
David Howellsc4028952006-11-22 14:57:56 +00004342 struct rtl8169_private *tp =
4343 container_of(work, struct rtl8169_private, task.work);
4344 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Francois Romieueb2a0212007-02-15 23:37:21 +01004346 rtnl_lock();
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004349 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
4351 rtl8169_wait_for_quiescence(dev);
4352
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004353 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 rtl8169_tx_clear(tp);
4355
4356 if (tp->dirty_rx == tp->cur_rx) {
4357 rtl8169_init_ring_indexes(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004358 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 netif_wake_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004360 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 } else {
Joe Perchesbf82c182010-02-09 11:49:50 +00004362 if (net_ratelimit())
4363 netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 rtl8169_schedule_work(dev, rtl8169_reset_task);
4365 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004366
4367out_unlock:
4368 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369}
4370
4371static void rtl8169_tx_timeout(struct net_device *dev)
4372{
4373 struct rtl8169_private *tp = netdev_priv(dev);
4374
françois romieue6de30d2011-01-03 15:08:37 +00004375 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
4377 /* Let's wait a bit while any (async) irq lands on */
4378 rtl8169_schedule_work(dev, rtl8169_reset_task);
4379}
4380
4381static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
4382 u32 opts1)
4383{
4384 struct skb_shared_info *info = skb_shinfo(skb);
4385 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004386 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004387 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 entry = tp->cur_tx;
4390 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4391 skb_frag_t *frag = info->frags + cur_frag;
4392 dma_addr_t mapping;
4393 u32 status, len;
4394 void *addr;
4395
4396 entry = (entry + 1) % NUM_TX_DESC;
4397
4398 txd = tp->TxDescArray + entry;
4399 len = frag->size;
4400 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004401 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004402 if (unlikely(dma_mapping_error(d, mapping))) {
4403 if (net_ratelimit())
4404 netif_err(tp, drv, tp->dev,
4405 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004406 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
4409 /* anti gcc 2.95.3 bugware (sic) */
4410 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4411
4412 txd->opts1 = cpu_to_le32(status);
4413 txd->addr = cpu_to_le64(mapping);
4414
4415 tp->tx_skb[entry].len = len;
4416 }
4417
4418 if (cur_frag) {
4419 tp->tx_skb[entry].skb = skb;
4420 txd->opts1 |= cpu_to_le32(LastFrag);
4421 }
4422
4423 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004424
4425err_out:
4426 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4427 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
4430static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
4431{
4432 if (dev->features & NETIF_F_TSO) {
Herbert Xu79671682006-06-22 02:40:14 -07004433 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435 if (mss)
4436 return LargeSend | ((mss & MSSMask) << MSSShift);
4437 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07004438 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004439 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
4441 if (ip->protocol == IPPROTO_TCP)
4442 return IPCS | TCPCS;
4443 else if (ip->protocol == IPPROTO_UDP)
4444 return IPCS | UDPCS;
4445 WARN_ON(1); /* we need a WARN() */
4446 }
4447 return 0;
4448}
4449
Stephen Hemminger613573252009-08-31 19:50:58 +00004450static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4451 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452{
4453 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004454 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 struct TxDesc *txd = tp->TxDescArray + entry;
4456 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004457 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 dma_addr_t mapping;
4459 u32 status, len;
4460 u32 opts1;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004461 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004464 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004465 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 }
4467
4468 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004469 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004471 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004472 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004473 if (unlikely(dma_mapping_error(d, mapping))) {
4474 if (net_ratelimit())
4475 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004476 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
4479 tp->tx_skb[entry].len = len;
4480 txd->addr = cpu_to_le64(mapping);
4481 txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4482
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004483 opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
4484
4485 frags = rtl8169_xmit_frags(tp, skb, opts1);
4486 if (frags < 0)
4487 goto err_dma_1;
4488 else if (frags)
4489 opts1 |= FirstFrag;
4490 else {
4491 opts1 |= FirstFrag | LastFrag;
4492 tp->tx_skb[entry].skb = skb;
4493 }
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 wmb();
4496
4497 /* anti gcc 2.95.3 bugware (sic) */
4498 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
4499 txd->opts1 = cpu_to_le32(status);
4500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 tp->cur_tx += frags + 1;
4502
David Dillow4c020a92010-03-03 16:33:10 +00004503 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
Francois Romieu275391a2007-02-23 23:50:28 +01004505 RTL_W8(TxPoll, NPQ); /* set polling bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
4507 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4508 netif_stop_queue(dev);
4509 smp_rmb();
4510 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4511 netif_wake_queue(dev);
4512 }
4513
Stephen Hemminger613573252009-08-31 19:50:58 +00004514 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004516err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004517 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004518err_dma_0:
4519 dev_kfree_skb(skb);
4520 dev->stats.tx_dropped++;
4521 return NETDEV_TX_OK;
4522
4523err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004525 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004526 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527}
4528
4529static void rtl8169_pcierr_interrupt(struct net_device *dev)
4530{
4531 struct rtl8169_private *tp = netdev_priv(dev);
4532 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 u16 pci_status, pci_cmd;
4534
4535 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4536 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4537
Joe Perchesbf82c182010-02-09 11:49:50 +00004538 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4539 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
4541 /*
4542 * The recovery sequence below admits a very elaborated explanation:
4543 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004544 * - I did not see what else could be done;
4545 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 *
4547 * Feel free to adjust to your needs.
4548 */
Francois Romieua27993f2006-12-18 00:04:19 +01004549 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004550 pci_cmd &= ~PCI_COMMAND_PARITY;
4551 else
4552 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4553
4554 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
4556 pci_write_config_word(pdev, PCI_STATUS,
4557 pci_status & (PCI_STATUS_DETECTED_PARITY |
4558 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4559 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4560
4561 /* The infamous DAC f*ckup only happens at boot time */
4562 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004563 void __iomem *ioaddr = tp->mmio_addr;
4564
Joe Perchesbf82c182010-02-09 11:49:50 +00004565 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 tp->cp_cmd &= ~PCIDAC;
4567 RTL_W16(CPlusCmd, tp->cp_cmd);
4568 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 }
4570
françois romieue6de30d2011-01-03 15:08:37 +00004571 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004572
4573 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574}
4575
Francois Romieu07d3f512007-02-21 22:40:46 +01004576static void rtl8169_tx_interrupt(struct net_device *dev,
4577 struct rtl8169_private *tp,
4578 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579{
4580 unsigned int dirty_tx, tx_left;
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 dirty_tx = tp->dirty_tx;
4583 smp_rmb();
4584 tx_left = tp->cur_tx - dirty_tx;
4585
4586 while (tx_left > 0) {
4587 unsigned int entry = dirty_tx % NUM_TX_DESC;
4588 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 u32 status;
4590
4591 rmb();
4592 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4593 if (status & DescOwn)
4594 break;
4595
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004596 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4597 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004599 dev->stats.tx_packets++;
4600 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004601 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 tx_skb->skb = NULL;
4603 }
4604 dirty_tx++;
4605 tx_left--;
4606 }
4607
4608 if (tp->dirty_tx != dirty_tx) {
4609 tp->dirty_tx = dirty_tx;
4610 smp_wmb();
4611 if (netif_queue_stopped(dev) &&
4612 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4613 netif_wake_queue(dev);
4614 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004615 /*
4616 * 8168 hack: TxPoll requests are lost when the Tx packets are
4617 * too close. Let's kick an extra TxPoll request when a burst
4618 * of start_xmit activity is detected (if it is not detected,
4619 * it is slow enough). -- FR
4620 */
4621 smp_rmb();
4622 if (tp->cur_tx != dirty_tx)
4623 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 }
4625}
4626
Francois Romieu126fa4b2005-05-12 20:09:17 -04004627static inline int rtl8169_fragmented_frame(u32 status)
4628{
4629 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4630}
4631
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004632static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 u32 status = opts1 & RxProtoMask;
4635
4636 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004637 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 skb->ip_summed = CHECKSUM_UNNECESSARY;
4639 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004640 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641}
4642
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004643static struct sk_buff *rtl8169_try_rx_copy(void *data,
4644 struct rtl8169_private *tp,
4645 int pkt_size,
4646 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004648 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004649 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004651 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004652 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004653 prefetch(data);
4654 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4655 if (skb)
4656 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004657 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4658
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004659 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660}
4661
Eric Dumazet630b9432010-03-31 02:08:31 +00004662/*
4663 * Warning : rtl8169_rx_interrupt() might be called :
4664 * 1) from NAPI (softirq) context
4665 * (polling = 1 : we should call netif_receive_skb())
4666 * 2) from process context (rtl8169_reset_task())
4667 * (polling = 0 : we must call netif_rx() instead)
4668 */
Francois Romieu07d3f512007-02-21 22:40:46 +01004669static int rtl8169_rx_interrupt(struct net_device *dev,
4670 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004671 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672{
4673 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004674 unsigned int count;
Eric Dumazet630b9432010-03-31 02:08:31 +00004675 int polling = (budget != ~(u32)0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 cur_rx = tp->cur_rx;
4678 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004679 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004681 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004683 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 u32 status;
4685
4686 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004687 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
4689 if (status & DescOwn)
4690 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004691 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004692 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4693 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004694 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004696 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004698 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004699 if (status & RxFOVF) {
4700 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004701 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004702 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004703 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004705 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004706 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Francois Romieu126fa4b2005-05-12 20:09:17 -04004709 /*
4710 * The driver does not support incoming fragmented
4711 * frames. They are seen as a symptom of over-mtu
4712 * sized frames.
4713 */
4714 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004715 dev->stats.rx_dropped++;
4716 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004717 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004718 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004719 }
4720
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004721 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4722 tp, pkt_size, addr);
4723 rtl8169_mark_to_asic(desc, rx_buf_sz);
4724 if (!skb) {
4725 dev->stats.rx_dropped++;
4726 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 }
4728
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004729 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 skb_put(skb, pkt_size);
4731 skb->protocol = eth_type_trans(skb, dev);
4732
Francois Romieu7a8fc772011-03-01 17:18:33 +01004733 rtl8169_rx_vlan_tag(desc, skb);
4734
4735 if (likely(polling))
4736 napi_gro_receive(&tp->napi, skb);
4737 else
4738 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Francois Romieucebf8cc2007-10-18 12:06:54 +02004740 dev->stats.rx_bytes += pkt_size;
4741 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 }
Francois Romieu6dccd162007-02-13 23:38:05 +01004743
4744 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00004745 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01004746 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4747 desc->opts2 = 0;
4748 cur_rx++;
4749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 }
4751
4752 count = cur_rx - tp->cur_rx;
4753 tp->cur_rx = cur_rx;
4754
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004755 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
4757 return count;
4758}
4759
Francois Romieu07d3f512007-02-21 22:40:46 +01004760static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
Francois Romieu07d3f512007-02-21 22:40:46 +01004762 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02004766 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
David Dillowf11a3772009-05-22 15:29:34 +00004768 /* loop handling interrupts until we have no new ones or
4769 * we hit a invalid/hotplug case.
4770 */
Francois Romieu865c6522008-05-11 14:51:00 +02004771 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00004772 while (status && status != 0xffff) {
4773 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
David Dillowf11a3772009-05-22 15:29:34 +00004775 /* Handle all of the error cases first. These will reset
4776 * the chip, so just exit the loop.
4777 */
4778 if (unlikely(!netif_running(dev))) {
4779 rtl8169_asic_down(ioaddr);
4780 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
David Dillowf11a3772009-05-22 15:29:34 +00004782
Francois Romieu1519e572011-02-03 12:02:36 +01004783 if (unlikely(status & RxFIFOOver)) {
4784 switch (tp->mac_version) {
4785 /* Work around for rx fifo overflow */
4786 case RTL_GIGA_MAC_VER_11:
4787 case RTL_GIGA_MAC_VER_22:
4788 case RTL_GIGA_MAC_VER_26:
4789 netif_stop_queue(dev);
4790 rtl8169_tx_timeout(dev);
4791 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01004792 /* Testers needed. */
4793 case RTL_GIGA_MAC_VER_17:
4794 case RTL_GIGA_MAC_VER_19:
4795 case RTL_GIGA_MAC_VER_20:
4796 case RTL_GIGA_MAC_VER_21:
4797 case RTL_GIGA_MAC_VER_23:
4798 case RTL_GIGA_MAC_VER_24:
4799 case RTL_GIGA_MAC_VER_27:
4800 case RTL_GIGA_MAC_VER_28:
Francois Romieu1519e572011-02-03 12:02:36 +01004801 /* Experimental science. Pktgen proof. */
4802 case RTL_GIGA_MAC_VER_12:
4803 case RTL_GIGA_MAC_VER_25:
4804 if (status == RxFIFOOver)
4805 goto done;
4806 break;
4807 default:
4808 break;
4809 }
David Dillowf11a3772009-05-22 15:29:34 +00004810 }
4811
4812 if (unlikely(status & SYSErr)) {
4813 rtl8169_pcierr_interrupt(dev);
4814 break;
4815 }
4816
4817 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00004818 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00004819
4820 /* We need to see the lastest version of tp->intr_mask to
4821 * avoid ignoring an MSI interrupt and having to wait for
4822 * another event which may never come.
4823 */
4824 smp_rmb();
4825 if (status & tp->intr_mask & tp->napi_event) {
4826 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
4827 tp->intr_mask = ~tp->napi_event;
4828
4829 if (likely(napi_schedule_prep(&tp->napi)))
4830 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00004831 else
4832 netif_info(tp, intr, dev,
4833 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00004834 }
4835
4836 /* We only get a new MSI interrupt when all active irq
4837 * sources on the chip have been acknowledged. So, ack
4838 * everything we've seen and check if new sources have become
4839 * active to avoid blocking all interrupts from the chip.
4840 */
4841 RTL_W16(IntrStatus,
4842 (status & RxFIFOOver) ? (status | RxOverflow) : status);
4843 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 }
Francois Romieu1519e572011-02-03 12:02:36 +01004845done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 return IRQ_RETVAL(handled);
4847}
4848
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004849static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004851 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
4852 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004854 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004856 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 rtl8169_tx_interrupt(dev, tp, ioaddr);
4858
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004859 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08004860 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00004861
4862 /* We need for force the visibility of tp->intr_mask
4863 * for other CPUs, as we can loose an MSI interrupt
4864 * and potentially wait for a retransmit timeout if we don't.
4865 * The posted write to IntrMask is safe, as it will
4866 * eventually make it to the chip and we won't loose anything
4867 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 */
David Dillowf11a3772009-05-22 15:29:34 +00004869 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00004870 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01004871 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 }
4873
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004874 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Francois Romieu523a6092008-09-10 22:28:56 +02004877static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
4878{
4879 struct rtl8169_private *tp = netdev_priv(dev);
4880
4881 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
4882 return;
4883
4884 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
4885 RTL_W32(RxMissed, 0);
4886}
4887
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888static void rtl8169_down(struct net_device *dev)
4889{
4890 struct rtl8169_private *tp = netdev_priv(dev);
4891 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
4893 rtl8169_delete_timer(dev);
4894
4895 netif_stop_queue(dev);
4896
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01004897 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01004898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 spin_lock_irq(&tp->lock);
4900
4901 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004902 /*
4903 * At this point device interrupts can not be enabled in any function,
4904 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
4905 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
4906 */
Francois Romieu523a6092008-09-10 22:28:56 +02004907 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
4909 spin_unlock_irq(&tp->lock);
4910
4911 synchronize_irq(dev->irq);
4912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07004914 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 rtl8169_tx_clear(tp);
4917
4918 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00004919
4920 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921}
4922
4923static int rtl8169_close(struct net_device *dev)
4924{
4925 struct rtl8169_private *tp = netdev_priv(dev);
4926 struct pci_dev *pdev = tp->pci_dev;
4927
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004928 pm_runtime_get_sync(&pdev->dev);
4929
Ivan Vecera355423d2009-02-06 21:49:57 -08004930 /* update counters before going down */
4931 rtl8169_update_counters(dev);
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 rtl8169_down(dev);
4934
4935 free_irq(dev->irq, dev);
4936
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00004937 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
4938 tp->RxPhyAddr);
4939 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
4940 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 tp->TxDescArray = NULL;
4942 tp->RxDescArray = NULL;
4943
Rafael J. Wysockie1759442010-03-14 14:33:51 +00004944 pm_runtime_put_sync(&pdev->dev);
4945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 return 0;
4947}
4948
Francois Romieu07ce4062007-02-23 23:36:39 +01004949static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950{
4951 struct rtl8169_private *tp = netdev_priv(dev);
4952 void __iomem *ioaddr = tp->mmio_addr;
4953 unsigned long flags;
4954 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01004955 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 u32 tmp = 0;
4957
4958 if (dev->flags & IFF_PROMISC) {
4959 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00004960 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 rx_mode =
4962 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
4963 AcceptAllPhys;
4964 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00004965 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00004966 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 /* Too many to filter perfectly -- accept all multicasts. */
4968 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
4969 mc_filter[1] = mc_filter[0] = 0xffffffff;
4970 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00004971 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01004972
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 rx_mode = AcceptBroadcast | AcceptMyPhys;
4974 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00004975 netdev_for_each_mc_addr(ha, dev) {
4976 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
4978 rx_mode |= AcceptMulticast;
4979 }
4980 }
4981
4982 spin_lock_irqsave(&tp->lock, flags);
4983
4984 tmp = rtl8169_rx_config | rx_mode |
4985 (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
4986
Francois Romieuf887cce2008-07-17 22:24:18 +02004987 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01004988 u32 data = mc_filter[0];
4989
4990 mc_filter[0] = swab32(mc_filter[1]);
4991 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02004992 }
4993
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07004995 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
Francois Romieu57a9f232007-06-04 22:10:15 +02004997 RTL_W32(RxConfig, tmp);
4998
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 spin_unlock_irqrestore(&tp->lock, flags);
5000}
5001
5002/**
5003 * rtl8169_get_stats - Get rtl8169 read/write statistics
5004 * @dev: The Ethernet Device to get statistics for
5005 *
5006 * Get TX/RX statistics for rtl8169
5007 */
5008static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5009{
5010 struct rtl8169_private *tp = netdev_priv(dev);
5011 void __iomem *ioaddr = tp->mmio_addr;
5012 unsigned long flags;
5013
5014 if (netif_running(dev)) {
5015 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005016 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 spin_unlock_irqrestore(&tp->lock, flags);
5018 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005019
Francois Romieucebf8cc2007-10-18 12:06:54 +02005020 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021}
5022
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005023static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005024{
françois romieu065c27c2011-01-03 15:08:12 +00005025 struct rtl8169_private *tp = netdev_priv(dev);
5026
Francois Romieu5d06a992006-02-23 00:47:58 +01005027 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005028 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005029
françois romieu065c27c2011-01-03 15:08:12 +00005030 rtl_pll_power_down(tp);
5031
Francois Romieu5d06a992006-02-23 00:47:58 +01005032 netif_device_detach(dev);
5033 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005034}
Francois Romieu5d06a992006-02-23 00:47:58 +01005035
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005036#ifdef CONFIG_PM
5037
5038static int rtl8169_suspend(struct device *device)
5039{
5040 struct pci_dev *pdev = to_pci_dev(device);
5041 struct net_device *dev = pci_get_drvdata(pdev);
5042
5043 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005044
Francois Romieu5d06a992006-02-23 00:47:58 +01005045 return 0;
5046}
5047
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005048static void __rtl8169_resume(struct net_device *dev)
5049{
françois romieu065c27c2011-01-03 15:08:12 +00005050 struct rtl8169_private *tp = netdev_priv(dev);
5051
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005052 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005053
5054 rtl_pll_power_up(tp);
5055
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005056 rtl8169_schedule_work(dev, rtl8169_reset_task);
5057}
5058
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005059static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005060{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005061 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005062 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005063 struct rtl8169_private *tp = netdev_priv(dev);
5064
5065 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005066
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005067 if (netif_running(dev))
5068 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005069
Francois Romieu5d06a992006-02-23 00:47:58 +01005070 return 0;
5071}
5072
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005073static int rtl8169_runtime_suspend(struct device *device)
5074{
5075 struct pci_dev *pdev = to_pci_dev(device);
5076 struct net_device *dev = pci_get_drvdata(pdev);
5077 struct rtl8169_private *tp = netdev_priv(dev);
5078
5079 if (!tp->TxDescArray)
5080 return 0;
5081
5082 spin_lock_irq(&tp->lock);
5083 tp->saved_wolopts = __rtl8169_get_wol(tp);
5084 __rtl8169_set_wol(tp, WAKE_ANY);
5085 spin_unlock_irq(&tp->lock);
5086
5087 rtl8169_net_suspend(dev);
5088
5089 return 0;
5090}
5091
5092static int rtl8169_runtime_resume(struct device *device)
5093{
5094 struct pci_dev *pdev = to_pci_dev(device);
5095 struct net_device *dev = pci_get_drvdata(pdev);
5096 struct rtl8169_private *tp = netdev_priv(dev);
5097
5098 if (!tp->TxDescArray)
5099 return 0;
5100
5101 spin_lock_irq(&tp->lock);
5102 __rtl8169_set_wol(tp, tp->saved_wolopts);
5103 tp->saved_wolopts = 0;
5104 spin_unlock_irq(&tp->lock);
5105
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005106 rtl8169_init_phy(dev, tp);
5107
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005108 __rtl8169_resume(dev);
5109
5110 return 0;
5111}
5112
5113static int rtl8169_runtime_idle(struct device *device)
5114{
5115 struct pci_dev *pdev = to_pci_dev(device);
5116 struct net_device *dev = pci_get_drvdata(pdev);
5117 struct rtl8169_private *tp = netdev_priv(dev);
5118
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005119 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005120}
5121
Alexey Dobriyan47145212009-12-14 18:00:08 -08005122static const struct dev_pm_ops rtl8169_pm_ops = {
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005123 .suspend = rtl8169_suspend,
5124 .resume = rtl8169_resume,
5125 .freeze = rtl8169_suspend,
5126 .thaw = rtl8169_resume,
5127 .poweroff = rtl8169_suspend,
5128 .restore = rtl8169_resume,
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005129 .runtime_suspend = rtl8169_runtime_suspend,
5130 .runtime_resume = rtl8169_runtime_resume,
5131 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005132};
5133
5134#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5135
5136#else /* !CONFIG_PM */
5137
5138#define RTL8169_PM_OPS NULL
5139
5140#endif /* !CONFIG_PM */
5141
Francois Romieu1765f952008-09-13 17:21:40 +02005142static void rtl_shutdown(struct pci_dev *pdev)
5143{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005144 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005145 struct rtl8169_private *tp = netdev_priv(dev);
5146 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005147
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005148 rtl8169_net_suspend(dev);
5149
Ivan Veceracc098dc2009-11-29 23:12:52 -08005150 /* restore original MAC address */
5151 rtl_rar_set(tp, dev->perm_addr);
5152
françois romieu4bb3f522009-06-17 11:41:45 +00005153 spin_lock_irq(&tp->lock);
5154
5155 rtl8169_asic_down(ioaddr);
5156
5157 spin_unlock_irq(&tp->lock);
5158
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005159 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005160 /* WoL fails with some 8168 when the receiver is disabled. */
5161 if (tp->features & RTL_FEATURE_WOL) {
5162 pci_clear_master(pdev);
5163
5164 RTL_W8(ChipCmd, CmdRxEnb);
5165 /* PCI commit */
5166 RTL_R8(ChipCmd);
5167 }
5168
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005169 pci_wake_from_d3(pdev, true);
5170 pci_set_power_state(pdev, PCI_D3hot);
5171 }
5172}
Francois Romieu5d06a992006-02-23 00:47:58 +01005173
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174static struct pci_driver rtl8169_pci_driver = {
5175 .name = MODULENAME,
5176 .id_table = rtl8169_pci_tbl,
5177 .probe = rtl8169_init_one,
5178 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005179 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005180 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181};
5182
Francois Romieu07d3f512007-02-21 22:40:46 +01005183static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184{
Jeff Garzik29917622006-08-19 17:48:59 -04005185 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186}
5187
Francois Romieu07d3f512007-02-21 22:40:46 +01005188static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189{
5190 pci_unregister_driver(&rtl8169_pci_driver);
5191}
5192
5193module_init(rtl8169_init_module);
5194module_exit(rtl8169_cleanup_module);